在一台主机上利用不同的端口创建一条私有链的多个客户端节点,记录命令:

重点:

  1. 需要先执行一遍init区块链的流程,目的是第二次init时能够事先给账户设置好充足的余额
  2. 两个节点的genesis.json文件要完全一致,此外,在节点1里生成账户之后,要把账户的key文件copy到节点2对应的文件夹里再初始化节点2,这样两节点才能链接成功。

创世块genesis.json

{
  "config": {
    "chainId": 91036,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0
  },
  "alloc": {
  },
  "coinbase": "0x0000000000000000000000000000000000000000",
  "difficulty": "0x20000",
  "extraData": "",
  "gasLimit": "0xffffffff",
  "nonce": "0x0000000000000042",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp": "0x00"
}

初始化区块链的命令:

# 初始化
$ geth1 --identity "MyEth" --rpc --rpcport "8545" --rpcapi "eth,net,personal,web3" --rpccorsdomain "*" --datadir gethdata --port "30303" --nodiscover --networkid 91036 --allow-insecure-unlock --dev.period 1 --syncmode "full" init genesis.json
# 启动
$ geth1 --identity "MyEth" --rpc --rpcport "8545" --rpcapi "eth,net,personal,web3" --rpccorsdomain "*" --datadir gethdata --port "30303" --nodiscover --networkid 91036 --allow-insecure-unlock --dev.period 1 --syncmode "full" console

geth1 --identity "MyEth" --rpc --rpcport "8541" --rpccorsdomain "*" --datadir gethdata --port "30301" --nodiscover --rpcapi "eth,net,personal,web3" --networkid 91036 init genesis.json
geth1 --identity "MyEth" --rpc --rpcport "8541" --rpccorsdomain "*" --datadir gethdata --port "30301" --nodiscover --rpcapi "eth,net,personal,web3" --networkid 91036 --allow-insecure-unlock --dev.period 1 console

第二个节点:

# 初始化
$ geth1 --identity "MyEth" --rpc --rpcport "8546" --rpcapi "eth,net,personal,web3" --rpccorsdomain "*" --datadir gethdata --port "30304" --nodiscover --networkid 91036 --allow-insecure-unlock --dev.period 1 --syncmode "full" init genesis.json
# 启动
$ geth1 --identity "MyEth" --rpc --rpcport "8546" --rpcapi "eth,net,personal,web3" --rpccorsdomain "*" --datadir gethdata --port "30304" --nodiscover --networkid 91036 --allow-insecure-unlock --dev.period 1 --syncmode "full" console

geth1 --identity "MyEth" --rpc --rpcport "8542" --rpccorsdomain "*" --datadir gethdata --port "30302" --nodiscover --rpcapi "eth,net,personal,web3" --networkid 91036 init genesis.json
geth1 --identity "MyEth" --rpc --rpcport "8542" --rpccorsdomain "*" --datadir gethdata --port "30302" --nodiscover --rpcapi "eth,net,personal,web3" --networkid 91036 --allow-insecure-unlock --dev.period 1 console

在控制台创建账户,参考命令:

> personal.newAccount("123456")

控制台返回

INFO [05-29|10:34:46.970] Your new key was generated               address=0x87E2D88cd788E8B8BE2682E356CB1f81BEC3116E
WARN [05-29|10:34:46.971] Please backup your key file!             path=/home/ubuntu/桌面/db-udgraduate/undergraduate-dbin2021/src/MapStore_VN/gethdata/keystore/UTC--2021-05-29T02-34-44.492901237Z--87e2d88cd788e8b8be2682e356cb1f81bec3116e
WARN [05-29|10:34:46.971] Please remember your password! 
"0x87e2d88cd788e8b8be2682e356cb1f81bec3116e"

上述返回内容中绿色字符串为账户名称,必须copy记录一下,(ctrl+shift+c)

下一步,退出Geth控制台(注意不是关闭窗口),参考命令:

> exit

此时节点账户已经创建好,下一步将账户名称粘贴到genesis.json