创建私有链遇到的问题
1.新建一个目录ethereum,用于存放区块链等数据
在ethereum目录下新建文件genisis.json,作为私有链的创世块说明文件如下
{
“nonce”: “0x0000000000000042”,
“mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“difficulty”: “0x2000”,
“alloc”: {},
“coinbase”: “0x0000000000000000000000000000000000000000”,
“timestamp”: “0x00”,
“parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,
“extraData”: “test”,
“gasLimit”: “0xffffffff”
}
2.在ethereum目录下启动geth,以genesis.json中的创世块开始创建私有链。
geth --datadir ./ init genesis.json
问题
如果extraData内容不为0x开头则会报Fatal: invalid genesis file: json: cannot unmarshal hex string without 0x prefix into Go struct field Genesis.extraData of type hexutil.Bytes的错误
另外数据位数必须为偶数位,否则会报odd length的错误
将位数改为偶数后,会报出Fatal: Failed to write genesis block: genesis has no chain configuration的错误
此时需要添加config域如下
3.启动p2p节点A控制台:
geth --datadir ./ --networkid 4321 console
此命令将建立一个p2p网络,网络id为4321,区块链数据存放于ethereum目录中,若提示如下说明成功创建了私有链