blockj: A simple blockchain project based on Java
The editor of Downcodes today brings you a simple blockchain (consortium chain) project based on Java - blockj. It includes encryption tools, wallets, P2P transmission, block synchronization, network consensus and other blockchain basic implementations. It is a very suitable project for beginners to learn blockchain technology.
Project structure
blockj is implemented using SpringBoot + Tio network framework. Its main structure is as follows:
1. Main program: implemented using SpringBoot framework.
2. P2P transmission: implemented using the t-io network framework.
Operating environment
The blockj project requires JDK 1.8 or above to run properly.
project module
The blockj project mainly contains the following three modules:
1. blockj-base: Basic public toolkit, including encryption tools, blocks, messages and other data models, as well as data storage and other functions.
2. blockj-miner: Blockchain main program, including miners, block synchronization, P2P network, RPC API and other functions.
3. blockj-client: client command line tool, mainly used to call Miner’s related APIs to realize user interaction with the blockchain.
quick start
The following steps teach you how to create a blockchain network and perform basic operations:
1. Create a genesis node (create a network)
First, you need to clone the project and compile and package it:
`bash
git clone https://gitee.com/blackfox/blockj.git
cd blockj
mvn clean package
`
Then, create the genesis node:
`bash
./miner genesis --repo=/data/genesis --enable-mining=true
`
2. Start Genesis Miner
`bash
./miner run --repo=/data/genesis
`
3. Start a new node (join the network)
In order for new nodes to join the existing network, you need to initialize Miner using the genesis block and then start Miner:
`bash
./miner init --repo=/data/miner1 --genesis=genesis.car --api.port=8002 --p2p.port=3456
./miner run --repo=/data/miner1
`
Client use
The following are some commonly used client command line operations:
1. View wallet list
`bash
./client wallet list
`
Output:
`
Address Balance Nonce
0x0d181310331612e107b5e0dfdf971cfb9de780adfds 800 10
0x2505bf54f3a63848e44a105e8de599ad08ae8c58 2400 0
0xb6258bc70240ee5daa213f671c08db51e50a4cbe 50800 30
cd3da3ec4195070e13a6d88b75101f6ceb427a8e 0 0
`
2. Create a new wallet
Create a normal wallet:
`bash
./client wallet new
`
Create a wallet with a mnemonic phrase (you can set a password and generate a keystore file):
`bash
./client wallet new --mnemonic=true 123456
`
3. Check wallet balance
`bash
./client wallet balance 0x2505bf54f3a63848e44a105e8de599ad08ae8c58
`
Output:
`
Address Balance
0x2505bf54f3a63848e44a105e8de599ad08ae8c58 2400
`
4. Transfer
`bash
./client chain send --from=0x0d181310331612e107b5e0dfdf971cfb9de780adfds 0x2505bf54f3a63848e44a105e8de599ad08ae8c58 123
`
Output:
`
Send message, CID: 05b6074241f1406cd1a68731d74cf612f55981692a3f4e5d9da01b13b4ee3631
`
5. Check the current chain height
`bash
./client chain head
`
6. View messages on the specified chain
`bash
./client chain getMessage 05b6074241f1406cd1a68731d74cf612f55981692a3f4e5d9da01b13b4ee3631
`
Output:
`
Message{version=1, from='0x0d181310331612e107b5e0dfdf971cfb9de780adfds', to='0x2505bf54f3a63848e44a105e8de599ad08ae8c58', value=123, timestamp=1672826743640, pub Key='PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCw5EXe33S25zZDT25sNYu1bjtBfaCwEGSgnGhJiE31fCfDsyE3pNFw7cC87VfQZQqiEdntMmztfpiDcRe1gv3aJJ4', cid='05b6074241f14 06cd1a68731d74cf612f55981692a3f4e5d9da01b13b4ee3631', status=APPENDING, nonce=2, params='null ', height=0, sign='30460221009A8B2750A6D986EB926B67163B740BBACDD07FE2D87F8FA9AE2F08424989477602210082C1C36EAEEC6367C023847F995291873F305B867 E9B65A5C68ED8A4293DB890'}
`
7. View block information at a specified height
`bash
./client chain getBlock 1
`
Output:
`
Block{header=BlockHeader{height=1, difficulty=28269553036454149273332760011886696253239742350009903329945699220681916416, nonce=703, createTime=1672813674, hash='0004c 262f7ead28cc66c9336d7a8335cb8fea5a06b0b1fd7488c3c9b140987cc', previousHash='ed5126ddd65f39a17739b8e26ea3edecfff6bf196148dc259d9a3eddeefc23d5'}, messages=[Message {version=0, from='B099', to='0xb6258bc70240ee5daa213f671c08db51e50a4cbe', value=50, timestamp=1672813674163, pubKey='PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSD1ErKp8AqSj4Ph9Jsj2Gvk7w1pyLg qDRiguC7JvjeGZJZ1si1qRYCAsVmu1UvYRqvhiCBgYDpmyuWK5VzD5KK4RNY', cid='3e1f8987b0d66b2de155e78aeef6984ab7cb6c3acdf03c835c01f4b4088fb90d', status=SUCCESS, nonce=0 , params='Miner Reward.', height=1, sign='304402202DF6EABBF5C81C41996C44F8E1230D44CAE8ADA9184B466D9B708ADC8B050225022049D74E4E99E0EA8E56208AD2E4B7B17C2320 DC3E6461A17C94D1820818559CA9 '}], blockSign='3046022100C04714C00642527AF6AA1DB2B537E5FD887F52999F66929AA2A928D4C6A4897A022100AF98888FDC825FFB0683E8D65494363790E5173F 3991AF61AB86DEEFEAF15D81'}
`
8. View the list of P2P nodes in the current network
`bash
./client net peers
`
9. View current node P2P connection information
`bash
./client net listen
`
Output:
`
P2P connection information, used to be connected by other nodes
127.0.0.1:2345
`
10. Manually connect a node
`bash
./client net connect 192.22.33.11:3456
`
summary
The blockj project is an entry-level project for learning blockchain technology. Through it, you can learn the basic concepts and implementation methods of blockchain. Of course, it currently only implements the POW consensus algorithm. If you want to use it in production projects, you need to modify and improve it according to your own project needs.
Hope this article is helpful to you.