以太坊创建创世区块



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 <anonymous>:1:1



如果你直接挖矿,会出现上面的错误。需要设置一个挖矿的账号。


> miner.setEtherbase("7df9a875a174b3bc565e6424a0050ebc1b2d1d82")
true

接下来开始挖矿。


> miner.start()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

devopen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值