以太坊私网建立 (3) - 通过创世区块来初始化区块链

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()
INFO [10-15|07:58:46] Updated mining threads                   threads=0
INFO [10-15|07:58:46] Transaction pool price threshold updated price=18000000000
INFO [10-15|07:58:46] Starting mining operation 
null
> INFO [10-15|07:58:46] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=159.05µs
INFO [10-15|07:58:48] Successfully sealed new block            number=1 hash=cf9d01…e0ec7a
INFO [10-15|07:58:48]  mined potential block                  number=1 hash=cf9d01…e0ec7a
INFO [10-15|07:58:48] Commit new mining work                   number=2 txs=0 uncles=0 elapsed=107.264µs
INFO [10-15|07:58:48] Successfully sealed new block            number=2 hash=1619e2…814e17
INFO [10-15|07:58:48]  mined potential block                  number=2 hash=1619e2…814e17
INFO [10-15|07:58:48] Commit new mining work                   number=3 txs=0 uncles=0 elapsed=107.323µs
INFO [10-15|07:58:50] Successfully sealed new block            number=3 hash=3c1363…683a88
INFO [10-15|07:58:50]  mined potential block                  number=3 hash=3c1363…683a88
INFO [10-15|07:58:50] Commit new mining work                   number=4 txs=0 uncles=0 elapsed=113.813µs
INFO [10-15|07:58:51] Successfully sealed new block            number=4 hash=f5878d…646398
INFO [10-15|07:58:51]  mined potential block                  number=4 hash=f5878d…646398
INFO [10-15|07:58:51] Commit new mining work                   number=5 txs=0 uncles=0 elapsed=110.545µs
INFO [10-15|07:58:51] Successfully sealed new block            number=5 hash=0dba65…e37ab9
INFO [10-15|07:58:51]  mined potential block                  number=5 hash=0dba65…e37ab9
INFO [10-15|07:58:51] Commit new mining work                   number=6 txs=0 uncles=0 elapsed=92.426µs
INFO [10-15|07:58:51] Successfully sealed new block            number=6 hash=703015…47e9f9
INFO [10-15|07:58:51]  block reached canonical chain          number=1 hash=cf9d01…e0ec7a
INFO [10-15|07:58:51]  mined potential block                  number=6 hash=703015…47e9f9
INFO [10-15|07:58:51] Mining too far in the future             wait=2s
INFO [10-15|07:58:53] Commit new mining work                   number=7 txs=0 uncles=0 elapsed=2.001s
INFO [10-15|07:58:53] Successfully sealed new block            number=7 hash=98e5b7…8d2d1a
INFO [10-15|07:58:53]  block reached canonical chain          number=2 hash=1619e2…814e17
INFO [10-15|07:58:53]  mined potential block                  number=7 hash=98e5b7…8d2d1a
INFO [10-15|07:58:53] Commit new mining work                   number=8 txs=0 uncles=0 elapsed=171.147µs
INFO [10-15|07:58:54] Successfully sealed new block            number=8 hash=3a9142…fee188

你可能感兴趣的:(以太坊私网建立 (3) - 通过创世区块来初始化区块链)