genesis block file example
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "4",
"gasLimit": "2100000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": {
"balance": "300000"
},
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": {
"balance": "400000"
}
}
}
config, difficulty, gasLimit, alloc创始区块文件中,这几个文件是必须的。
Start Coding
创建创世区块文件
/Users/liyuechun/Desktop/1015/townodes
liyuechun:townodes yuechunli$ cat > genesis.json
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "4",
"gasLimit": "2100000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": {
"balance": "300000"
},
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": {
"balance": "400000"
}
}
}
^C
liyuechun:townodes yuechunli$ cat genesis.json
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "4",
"gasLimit": "2100000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": {
"balance": "300000"
},
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": {
"balance": "400000"
}
}
}
liyuechun:townodes yuechunli$
初始化区块链,并且创建一个文件夹来存储区块数据
liyuechun:townodes yuechunli$ geth init genesis.json --datadir blockchainData
WARN [10-15|07:50:09] No etherbase set and no accounts found as default
INFO [10-15|07:50:09] Allocated cache and file handles database=/Users/liyuechun/Desktop/1015/townodes/blockchainData/geth/chaindata cache=16 handles=16
INFO [10-15|07:50:09] Writing custom genesis block
INFO [10-15|07:50:09] Successfully wrote genesis state database=chaindata hash=884fa3…0409fd
INFO [10-15|07:50:09] Allocated cache and file handles database=/Users/liyuechun/Desktop/1015/townodes/blockchainData/geth/lightchaindata cache=16 handles=16
INFO [10-15|07:50:09] Writing custom genesis block
INFO [10-15|07:50:09] Successfully wrote genesis state database=lightchaindata hash=884fa3…0409fd
liyuechun:townodes yuechunli$
打开终端
geth --networkid 123 --datadir blockchainData console
警告
WARN [10-15
07:53:09] No etherbase set and no accounts found as default
出现这个警告的原因是因为,我们创世区块没有创建任何账号。
查看余额
> eth.getBalance("7df9a875a174b3bc565e6424a0050ebc1b2d1d82")
300000
>
开始 mining
> miner.start()
INFO [10-15|07:57:15] Updated mining threads threads=0
INFO [10-15|07:57:15] Transaction pool price threshold updated price=18000000000
ERROR[10-15|07:57:15] Cannot start mining without etherbase err="etherbase address must be explicitly specified"
Error: etherbase missing: etherbase address must be explicitly specified
at web3.js:3104:20
at web3.js:6191:15
at web3.js:5004:36
at
:1:1
>
如果你直接挖矿,会出现上面的错误。需要设置一个挖矿的账号。
> miner.setEtherbase("7df9a875a174b3bc565e6424a0050ebc1b2d1d82")
true
>
接下来开始挖矿。
> miner.start()