以太坊给账户预分配以太币

创建私链目录

  1. mkdir /data/testchain

创建创始块json文件

  1. vi genesis.json
  2. {
  3. "config": {
  4. "chainId": 2018,
  5. "homesteadBlock": 0
  6. },
  7. "coinbase" : "0x0000000000000000000000000000000000000000",
  8. "difficulty" : "0x400",
  9. "gasLimit" : "0x2fefd8",
  10. "nonce" : "0x0000000000000142",
  11. "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  12. "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  13. "timestamp" : "0x00",
  14. "alloc": {
  15. }
  16. }

创建创始块

 
  1. geth init genesis.json --datadir /data/testchain/
 
  1. WARN [02-06|17:46:00] No etherbase set and no accounts found as default
  2. INFO [02-06|17:46:00] Allocated cache and file handles database=/root/testchain/geth/chaindata cache=16 handles=16
  3. INFO [02-06|17:46:00] Writing custom genesis block
  4. INFO [02-06|17:46:00] Successfully wrote genesis state database=chaindata hash=ac4e66…7f2921
  5. INFO [02-06|17:46:00] Allocated cache and file handles database=/root/testchain/geth/lightchaindata cache=16 handles=16
  6. INFO [02-06|17:46:00] Writing custom genesis block
  7. INFO [02-06|17:46:00] Successfully wrote genesis state database=lightchaindata

启动私链

 
  1. geth --datadir /data/testchain/ --networkid 2018 --rpc --rpcport "8845" --rpccorsdomain "*" --port "30333" --nodiscover

这样,第一个私链节点就已正常启动了,rpc端口和p2p端口都是可以自己随意定义的

第二个节点,前几步和之前一样,只是最后一步启动节点时的命令稍有变化

先查看第一个节点的nodeinfo,用于第二个节点启动
在第一个节点上连接到ipc console

 
  1. geth attach /data/testchain/geth.ipc
 
  1. Welcome to the Geth JavaScript console!
  2.  
  3. instance: Geth/Roadchain/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
  4. coinbase: 0x81e71d34e8a9e4382c36fd90c3f234549106addd
  5. at block: 6 (Tue, 06 Feb 2018 17:54:11 CST)
  6. datadir: /root/testchain
  7. modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
  8.  
  9. > admin.nodeInfo
  10. {
  11. enode: "enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@[::]:30333?discport=0",
  12. id: "1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f",
  13. ip: "::",
  14. listenAddr: "[::]:30333",
  15. name: "Geth/Roadchain/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9",
  16. ports: {
  17. discovery: 0,
  18. listener: 30333
  19. },
  20. protocols: {
  21. eth: {
  22. difficulty: 788096,
  23. genesis: "0x3782eafbc5ab71618f9a6aaa3506a385c50c20d3682ade9ea817e9025cadf804",
  24. head: "0x74ae4f44d9326a000cd4920e2f9cf4d85ff1b7289c5b04af91a6cc1b8ba032df",
  25. network: 2018
  26. }
  27. }
  28. }

enode的信息就是我们需要记录的enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@[::]:30333

这里需要把@后面的'[::]'替换为服务器的IP,例如192.168.1.11

下面开始在第二节点上启动geth

 
  1. geth --datadir /data/testchain/ --networkid "2018" --rpc --rpcport "8845" --rpccorsdomain "*" --port "30333" --bootnodes "enode://1f9cf6ef261966099b2d3498a2517a900318c141bea00edac71f1617dc6987852ce0239eea2d6490bd2af07409b2d623072ce3c1d3f3074dd914f31ba06a7c2f@192.168.1.11:30333"

现在就可以在两个节点上geth console中分别执行admin.peers查看两个节点是否都看到了对方

三,附加操作

创建账户
在geth console中执行,12345678就是账户密码,请自行修改

 
  1. > personal.newAccount("12345678")
  2. "0x81e71d34e8a9e4382c36fd90c3f234549106addd"

解锁账户

 
  1. personal.unlockAccount("0x81e71d34e8a9e4382c36fd90c3f234549106addd","12345678")

单机挖矿和停止挖矿

 
  1. > miner.start()
  2. null
  3. > miner.stop()
  4. true

查看账户余额

 

eth.getBalance("0x81e71d34e8a9e4382c36fd90c3f234549106addd")

通过创始块预分配账户余额
按照上面的步骤将第一节点启动后,创建一个账户,复制地址
编辑genesis.json,在alloc段,增加如下内容:

 

"alloc": {"0x81e71d34e8a9e4382c36fd90c3f234549106addd": { "balance": "20000000000000000000" }}

这里的地址就是刚才创建的地址,后面的balance就是你想预分配的余额

然后将数据目录下的geth目录删掉,重新创建创始块(keystore目录不要动)

rm -rf /data/testchain/geth

然后再重新执行geth init创建创始块的命令,然后再启动geth,就可以在console中查看到账户被预分配的余额了

你可能感兴趣的:(区块链技术)