多验证者节点部署

注意:本文档仅适用于POA共识的多验证者节点部署。 geth自1.14.x之后不再支持Clique共识,所以编译solidity合约时,evm最高只支持到London

创建账户

# 创建节点目录
$ mkdir node0 node1 node2

# 创建账户1
$ geth account new --datadir ./node0
INFO [10-11|09:53:51.790] Maximum peer count                       ETH=50 total=50
INFO [10-11|09:53:51.791] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key:   0x553E33DcE20bc261869aB5b8B7c3F86De3d1C06B
Path of the secret key file: node0/keystore/UTC--2024-10-11T01-53-58.025264504Z--553e33dce20bc261869ab5b8b7c3f86de3d1c06b

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

# 创建账户2
$ geth account new --datadir ./node1
INFO [10-11|09:55:12.919] Maximum peer count                       ETH=50 total=50
INFO [10-11|09:55:12.919] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key:   0x28B188321dA923fCCf2678eA6E526a03BFb50adF
Path of the secret key file: node1/keystore/UTC--2024-10-11T01-55-19.151113014Z--28b188321da923fccf2678ea6e526a03bfb50adf

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

# 创建账户3
$ geth account new --datadir ./node2
INFO [10-11|09:55:47.318] Maximum peer count                       ETH=50 total=50
INFO [10-11|09:55:47.319] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:

Your new key was generated

Public address of the key:   0x46443c74496dCf0EB77beb5D60c81Ad7Cd536Cf3
Path of the secret key file: node2/keystore/UTC--2024-10-11T01-55-53.092518842Z--46443c74496dcf0eb77beb5d60c81ad7cd536cf3

- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!

初始化

# 初始化节点0
$ docker run --rm -v $(pwd)/node0:/root/.ethereum -v $(pwd)/genesis.json:/root/genesis.json ethereum/client-go:v1.13.15 init /root/genesis.json
INFO [10-11|02:18:54.853] Maximum peer count                       ETH=50 total=50
INFO [10-11|02:18:54.855] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-11|02:18:54.860] Set global gas cap                       cap=50,000,000
INFO [10-11|02:18:54.860] Initializing the KZG library             backend=gokzg
INFO [10-11|02:18:54.900] Defaulting to pebble as the backing database
INFO [10-11|02:18:54.900] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16.00MiB handles=16
INFO [10-11|02:18:54.924] Opened ancient database                  database=/root/.ethereum/geth/chaindata/ancient/chain readonly=false
INFO [10-11|02:18:54.925] State schema set to default              scheme=hash
INFO [10-11|02:18:54.925] Writing custom genesis block
INFO [10-11|02:18:54.926] Persisted trie from memory database      nodes=4 size=600.00B time=1.264217ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:18:54.940] Successfully wrote genesis state         database=chaindata hash=7a5bce..943bf9
INFO [10-11|02:18:54.940] Defaulting to pebble as the backing database
INFO [10-11|02:18:54.940] Allocated cache and file handles         database=/root/.ethereum/geth/lightchaindata cache=16.00MiB handles=16
INFO [10-11|02:18:54.962] Opened ancient database                  database=/root/.ethereum/geth/lightchaindata/ancient/chain readonly=false
INFO [10-11|02:18:54.962] State schema set to default              scheme=hash
INFO [10-11|02:18:54.962] Writing custom genesis block
INFO [10-11|02:18:54.963] Persisted trie from memory database      nodes=4 size=600.00B time=1.129877ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:18:54.976] Successfully wrote genesis state         database=lightchaindata hash=7a5bce..943bf9

# 初始化节点1
$ docker run --rm -v $(pwd)/node1:/root/.ethereum -v $(pwd)/genesis.json:/root/genesis.json ethereum/client-go:v1.13.15 init /root/genesis.json
INFO [10-11|02:19:58.497] Maximum peer count                       ETH=50 total=50
INFO [10-11|02:19:58.498] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-11|02:19:58.501] Set global gas cap                       cap=50,000,000
INFO [10-11|02:19:58.501] Initializing the KZG library             backend=gokzg
INFO [10-11|02:19:58.536] Defaulting to pebble as the backing database
INFO [10-11|02:19:58.536] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16.00MiB handles=16
INFO [10-11|02:19:58.562] Opened ancient database                  database=/root/.ethereum/geth/chaindata/ancient/chain readonly=false
INFO [10-11|02:19:58.562] State schema set to default              scheme=hash
INFO [10-11|02:19:58.563] Writing custom genesis block
INFO [10-11|02:19:58.565] Persisted trie from memory database      nodes=4 size=600.00B time=2.016678ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:19:58.577] Successfully wrote genesis state         database=chaindata hash=7a5bce..943bf9
INFO [10-11|02:19:58.577] Defaulting to pebble as the backing database
INFO [10-11|02:19:58.577] Allocated cache and file handles         database=/root/.ethereum/geth/lightchaindata cache=16.00MiB handles=16
INFO [10-11|02:19:58.598] Opened ancient database                  database=/root/.ethereum/geth/lightchaindata/ancient/chain readonly=false
INFO [10-11|02:19:58.598] State schema set to default              scheme=hash
INFO [10-11|02:19:58.598] Writing custom genesis block
INFO [10-11|02:19:58.599] Persisted trie from memory database      nodes=4 size=600.00B time=1.171228ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:19:58.611] Successfully wrote genesis state         database=lightchaindata hash=7a5bce..943bf9

# 初始化节点2
$ docker run --rm -v $(pwd)/node2:/root/.ethereum -v $(pwd)/genesis.json:/root/genesis.json ethereum/client-go:v1.13.15 init /root/genesis.json
INFO [10-11|02:20:19.446] Maximum peer count                       ETH=50 total=50
INFO [10-11|02:20:19.447] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-11|02:20:19.449] Set global gas cap                       cap=50,000,000
INFO [10-11|02:20:19.450] Initializing the KZG library             backend=gokzg
INFO [10-11|02:20:19.483] Defaulting to pebble as the backing database
INFO [10-11|02:20:19.483] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16.00MiB handles=16
INFO [10-11|02:20:19.507] Opened ancient database                  database=/root/.ethereum/geth/chaindata/ancient/chain readonly=false
INFO [10-11|02:20:19.507] State schema set to default              scheme=hash
INFO [10-11|02:20:19.507] Writing custom genesis block
INFO [10-11|02:20:19.508] Persisted trie from memory database      nodes=4 size=600.00B time=1.174364ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:20:19.520] Successfully wrote genesis state         database=chaindata hash=7a5bce..943bf9
INFO [10-11|02:20:19.521] Defaulting to pebble as the backing database
INFO [10-11|02:20:19.521] Allocated cache and file handles         database=/root/.ethereum/geth/lightchaindata cache=16.00MiB handles=16
INFO [10-11|02:20:19.542] Opened ancient database                  database=/root/.ethereum/geth/lightchaindata/ancient/chain readonly=false
INFO [10-11|02:20:19.542] State schema set to default              scheme=hash
INFO [10-11|02:20:19.542] Writing custom genesis block
INFO [10-11|02:20:19.544] Persisted trie from memory database      nodes=4 size=600.00B time=1.793947ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=0 livesize=0.00B
INFO [10-11|02:20:19.556] Successfully wrote genesis state         database=lightchaindata hash=7a5bce..943bf9

启动节点

compose 文件

docker方式启动,我们使用下面的docker-compose.yaml文件:


services:
  node0:
    image: ethereum/client-go:v1.13.15
    container_name: node0
    volumes:
      - ./node0:/root/.ethereum
      - ./genesis.json:/root/genesis.json
    ports:
      - 8545:8545  # HTTP RPC
      - 30303:30303  # P2P 网络
    command: >
      --networkid 377777
      --http
      --http.addr "0.0.0.0"
      --http.port 8545
      --http.corsdomain *
      --http.api eth,net,web3,personal,miner,admin,debug,txpool
      --allow-insecure-unlock
      --datadir /root/.ethereum
      --mine
      --miner.etherbase 553E33DcE20bc261869aB5b8B7c3F86De3d1C06B
      --miner.gasprice 10
      --unlock 553E33DcE20bc261869aB5b8B7c3F86De3d1C06B
      --password /root/.ethereum/password.txt
    networks:
      - ethereum-network

  node1:
    image: ethereum/client-go:v1.13.15
    container_name: node1
    volumes:
      - ./node1:/root/.ethereum
      - ./genesis.json:/root/genesis.json
    ports:
      - 8546:8545  # HTTP RPC
      - 30304:30303  # P2P 网络
    # bootnodes 信息,这里指定了 node0 的 enode 信息,其他节点可以用类似的方式指定
    command: >
      --networkid 377777
      --http
      --http.addr "0.0.0.0"
      --http.port 8545
      --http.api eth,net,web3,personal,miner,admin,debug,txpool
      --allow-insecure-unlock
      --datadir /root/.ethereum
      --bootnodes "enode://16a453ed853a1261106523de0f98eb9208b73c5d17fe8902f616aa78a8c0379b66f19be51aa4594a605e93c98eaaa4499d088141bb564e5c3b04533bca2dbf3c@node0:30303"
      --mine
      --miner.etherbase 28B188321dA923fCCf2678eA6E526a03BFb50adF
      --miner.gasprice 10
      --unlock 28B188321dA923fCCf2678eA6E526a03BFb50adF
      --password /root/.ethereum/password.txt
    networks:
      - ethereum-network

  node2:
    image: ethereum/client-go:v1.13.15
    container_name: node2
    volumes:
      - ./node2:/root/.ethereum
      - ./genesis.json:/root/genesis.json
    ports:
      - 8547:8545  # HTTP RPC
      - 30305:30303  # P2P 网络
    command: >
      --networkid 377777
      --http
      --http.addr "0.0.0.0"
      --http.port 8545
      --http.api eth,net,web3,personal,miner,admin,debug,txpool
      --allow-insecure-unlock
      --datadir /root/.ethereum
      --bootnodes "enode://16a453ed853a1261106523de0f98eb9208b73c5d17fe8902f616aa78a8c0379b66f19be51aa4594a605e93c98eaaa4499d088141bb564e5c3b04533bca2dbf3c@node0:30303"
      --mine
      --miner.etherbase 46443c74496dCf0EB77beb5D60c81Ad7Cd536Cf3
      --miner.gasprice 10
      --unlock 46443c74496dCf0EB77beb5D60c81Ad7Cd536Cf3
      --password /root/.ethereum/password.txt
    networks:
      - ethereum-network

networks:
  ethereum-network:
    driver: bridge

启动引导节点

引导节点帮助其他节点连接到网络。

# 这里我们以node0作为引导节点
$ docekr compose up -d node0
# 等待节点启动完成后通过geth attach获取节点的enode信息
$ docker exec -it node0 geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.13.15-stable-c5ba367e/linux-amd64/go1.21.9
coinbase: 0x553e33dce20bc261869ab5b8b7c3f86de3d1c06b
at block: 2 (Fri Oct 11 2024 02:28:40 GMT+0000 (UTC))
 datadir: /root/.ethereum
 modules: admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
> admin.nodeInfo
{
  enode: "enode://16a453ed853a1261106523de0f98eb9208b73c5d17fe8902f616aa78a8c0379b66f19be51aa4594a605e93c98eaaa4499d088141bb564e5c3b04533bca2dbf3c@127.0.0.1:30303",
  enr: "enr:-KO4QDZB7dyjt7owZEavKOPJcnYY7sdYtmZPAYdb8Ae5dSYgbuJBPWrus0XXKvddBvOLI_rAQrpQrUEFx0GJkIio8qeGAZJ5Ykflg2V0aMfGhLnGSDaAgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQIWpFPthToSYRBlI94PmOuSCLc8XRf-iQL2Fqp4qMA3m4RzbmFwwIN0Y3CCdl-DdWRwgnZf",
  id: "f25ed62576ac407895a2521d915ac2b416b02a37d01c3f373a4bc9a78dc88649",
  ip: "127.0.0.1",
  listenAddr: "[::]:30303",
  name: "Geth/v1.13.15-stable-c5ba367e/linux-amd64/go1.21.9",
  ports: {
    discovery: 30303,
    listener: 30303
  },
  protocols: {
    eth: {
      config: {
        berlinBlock: 0,
        byzantiumBlock: 0,
        chainId: 377777,
        clique: {...},
        constantinopleBlock: 0,
        eip150Block: 0,
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0,
        istanbulBlock: 0,
        petersburgBlock: 0
      },
      difficulty: 757,
      genesis: "0x7a5bce2da2aa676a1dec6d2d2066570d2d5e11cb2eafe10208321e0508943bf9",
      head: "0x21ea0c49448e4ab5e9cf50a36824a11233d8cbd31d664538e9962a53e75cdb41",
      network: 377777
    },
    snap: {}
  }
}

# 复制enode信息,并在其他节点的命令行中添加--bootnodes参数,启动其它节点
$ docker compose up -d node1/node2

至此,我们的私链已经启动成功,并包含3个验证者节点。

操作示例

# 连接到node0节点
$ docker exec -it node0 geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.13.15-stable-c5ba367e/linux-amd64/go1.21.9
coinbase: 0x553e33dce20bc261869ab5b8b7c3f86de3d1c06b
at block: 2 (Fri Oct 11 2024 02:28:40 GMT+0000 (UTC))
 datadir: /root/.ethereum
 modules: admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
# 查询指定账户余额
> eth.getBalance("0x46443c74496dCf0EB77beb5D60c81Ad7Cd536Cf3")
1e+27
> eth.getBalance("0x28B188321dA923fCCf2678eA6E526a03BFb50adF")
1e+27

# 进行转账交易
> eth.sendTransaction({to:"0xC524757EBF87AB13B99F9bb667CcCD26B3682281",from:"0x553E33DcE20bc261869aB5b8B7c3F86De3d1C06B",value: 1e19})
"0x2b9c235858d04594e6e82b8129e030062eded42f9dd424bf1cf4bda7e1568aef"

# 查询交易信息
> eth.getTransaction("0x2b9c235858d04594e6e82b8129e030062eded42f9dd424bf1cf4bda7e1568aef")
{
  blockHash: "0xc4a33c3c5d7691302874e987f6caacd50fb469fece60f14fdc35bab794ecfb49",
  blockNumber: 38,
  chainId: "0x5c3b1",
  from: "0x553e33dce20bc261869ab5b8b7c3f86de3d1c06b",
  gas: 21000,
  gasPrice: 10,
  hash: "0x2b9c235858d04594e6e82b8129e030062eded42f9dd424bf1cf4bda7e1568aef",
  input: "0x",
  nonce: 0,
  r: "0x7b5eb4e97eed492bdc75768e3e7437a977e54e3180546669d057b7951f720809",
  s: "0x42ded8ee861cebb208bc3801345448620a88ce771101f1f688d4112118e99f5c",
  to: "0xc524757ebf87ab13b99f9bb667cccd26b3682281",
  transactionIndex: 0,
  type: "0x0",
  v: "0xb8785",
  value: 10000000000000000000
}
# 查询交易回执信息
> eth.getTransactionReceipt("0x2b9c235858d04594e6e82b8129e030062eded42f9dd424bf1cf4bda7e1568aef")
{
  blockHash: "0xc4a33c3c5d7691302874e987f6caacd50fb469fece60f14fdc35bab794ecfb49",
  blockNumber: 38,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  effectiveGasPrice: 10,
  from: "0x553e33dce20bc261869ab5b8b7c3f86de3d1c06b",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x1",
  to: "0xc524757ebf87ab13b99f9bb667cccd26b3682281",
  transactionHash: "0x2b9c235858d04594e6e82b8129e030062eded42f9dd424bf1cf4bda7e1568aef",
  transactionIndex: 0,
  type: "0x0"
}

# 在node1节点查询0xc524757ebf87ab13b99f9bb667cccd26b3682281余额
$ docker exec -it node1 geth attach
Welcome to the Geth JavaScript console!

instance: Geth/v1.13.15-stable-c5ba367e/linux-amd64/go1.21.9
coinbase: 0x553e33dce20bc261869ab5b8b7c3f86de3d1c06b
at block: 2 (Fri Oct 11 2024 02:28:40 GMT+0000 (UTC))
 datadir: /root/.ethereum
 modules: admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
# 查询指定账户余额
> eth.getBalance("0xc524757ebf87ab13b99f9bb667cccd26b3682281")
8999999999933280560

孟斯特

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特