美文网首页
搭建以太坊私有链POA

搭建以太坊私有链POA

作者: 95加不满 | 来源:发表于2022-11-07 20:46 被阅读0次

目标:搭建3个共识节点,1个普通节点,采用poa共识。

下载安装包及镜像

参考下载地址 https://geth.ethereum.org/downloads/

curl -LO https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.10.8-26675454.tar.gz
cp abigen bootnode clef evm geth puppeth rlpdump /usr/local/bin/
docker pull ethereum/client-go:v1.10.8
docker pull statusteam/bootnode:v0.64.3

创建账户

mkdir /zxl/eth/node -p
cd /zxl/eth/node
mkdir node1 node2 node3 node4

docker run -i --rm -v $PWD/node1:/root ethereum/client-go:v1.10.8 --datadir /root/ account new
Password: 123456
Repeat password: 123456
Public address of the key:   0xBC1F42383f9567B43C219200D83C71CF144C1146
Path of the secret key file: /root/keystore/UTC--2021-05-19T06-30-54.972732175Z--381b085ed9f5674db928e74d7b4f0347d4b512c6

echo '0xBC1F42383f9567B43C219200D83C71CF144C1146' > node1/accounts.txt
echo '123456' >node1/password.txt

创建genesis文件

[root@NID001WOYWOYV007 node]# puppeth 
+-----------------------------------------------------------+
| Welcome to puppeth, your Ethereum private network manager |
|                                                           |
| This tool lets you create a new Ethereum network down to  |
| the genesis block, bootnodes, miners and ethstats servers |
| without the hassle that it would normally entail.         |
|                                                           |
| Puppeth uses SSH to dial in to remote servers, and builds |
| its network components out of Docker containers using the |
| docker-compose toolset.                                   |
+-----------------------------------------------------------+

Please specify a network name to administer (no spaces, hyphens or capital letters please)
> zxlnet
Sweet, you can set this via --network=zxlnet next time!
INFO [05-19|14:42:19.784] Administering Ethereum network           name=zxlnet
INFO [05-19|14:42:19.786] No remote machines to gather stats from 
What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> 2

 1. Modify existing configurations
 2. Export genesis configurations
 3. Remove genesis configuration
> 3
INFO [05-19|14:43:05.784] Genesis block destroyed 

What would you like to do? (default = stats)
 1. Show network stats
 2. Configure new genesis
 3. Track new remote server
 4. Deploy network components
> 2

What would you like to do? (default = create)
 1. Create new genesis from scratch
 2. Import already existing genesis
> 1

Which consensus engine to use? (default = clique)
 1. Ethash - proof-of-work
 2. Clique - proof-of-authority
> 2

How many seconds should blocks take? (default = 15)
> 10

Which accounts are allowed to seal? (mandatory at least one)
> 0x
> 0xBC1F42383f9567B43C219200D83C71CF144C1146
> 0x

Which accounts should be pre-funded? (advisable at least one)
> 0xBC1F42383f9567B43C219200D83C71CF144C1146
> 0x

Should the precompile-addresses (0x1 .. 0xff) be pre-funded with 1 wei? (advisable yes)
> yes

Specify your chain/network ID if you want an explicit one (default = random)
> 1218
INFO [05-19|14:44:40.599] Configured new genesis block 

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> 2

 1. Modify existing configurations
 2. Export genesis configurations
 3. Remove genesis configuration
> 2

Which folder to save the genesis specs into? (default = current)
  Will create zxlnet.json, zxlnet-aleth.json, zxlnet-harmony.json, zxlnet-parity.json
> 
INFO [05-19|14:45:07.401] Saved native genesis chain spec          path=zxlnet.json/zxlnet.json
ERROR[05-19|14:45:07.401] Failed to create Aleth chain spec        err="unsupported consensus engine"
ERROR[05-19|14:45:07.401] Failed to create Parity chain spec       err="unsupported consensus engine"
INFO [05-19|14:45:07.402] Saved genesis chain spec                 client=harmony path=zxlnet.json/zxlnet-harmony.json

What would you like to do? (default = stats)
 1. Show network stats
 2. Manage existing genesis
 3. Track new remote server
 4. Deploy network components
> ^C

复制文件至对应的目录

mv zxlnet.json genesis.json
cp genesis.json node1
cp genesis.json node2
cp genesis.json node3
cp genesis.json node4

删除 genesis.json 中 alloc字段中无效的账户 只保留创建的账户

genesis.json

{
  "config": {
    "chainId": 1218,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "clique": {
      "period": 10,
      "epoch": 30000
    }
  },
  "nonce": "0x0",
  "timestamp": "0x636a1be8",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000bc1f42383f9567b43c219200d83c71cf144c11468e744b43347d4b5127568185ee9f5664db92f27533b63bdd06eb613dd056718224893c4653c20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x1",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
    "bc1f42383f9567b43c219200d83c71cf144c1146": {
      "balance": "0x200000000000000000000000000000000000000000000000000000000000000"
    }
  },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

初始化节点

docker run -i --rm -v $PWD/node1:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
docker run -i --rm -v $PWD/node2:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
docker run -i --rm -v $PWD/node3:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
docker run -i --rm -v $PWD/node4:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json

查看节点enode

docker run --rm -v $PWD/node1:/root statusteam/bootnode:v0.64.3 --nodekey=/root/geth/nodekey  --writeaddress
970a32110f1e2cf251b8f65dfa55029bb52ddfbb5c3e7632bf9cca8c2ea29aa7c88dd1787aafa32297c41f6cb787994dd687a5003146a2d2001e72813011789c
docker run --rm -v $PWD/node2:/root statusteam/bootnode:v0.64.3 --nodekey=/root/geth/nodekey  --writeaddress
3df30f1617c1f74aa7b5f535dc4494b4b9a0a3369dd3de0870e17111846da27a7ce68d77c9ab4e518bed2e96117e3b6d39dff54bd132e43cc38fdfd72fd1c8b7
docker run --rm -v $PWD/node3:/root statusteam/bootnode:v0.64.3 --nodekey=/root/geth/nodekey  --writeaddress
bab291a6f105a4366115c78c0da2e7ce654acaeb702d1f586b32485eb3e213b37810bcf537b0bbffce191f198a7e41edae4e2ea1bc553a8fe671e33503844953
docker run --rm -v $PWD/node4:/root statusteam/bootnode:v0.64.3 --nodekey=/root/geth/nodekey  --writeaddress
a1cca894f28d39c3d6367693da384a5e457f87ca9058947eb23ac4bf89e6fa176ef63432e7b0180397a97b4855d406bd4ae30823f43ed4bd7291a99fe39b5b64

创建static-nodes.json

cat vi static-nodes.json
[
"enode://970a32110f1e2cf251b8f65dfa55029bb52ddfbb5c3e7632bf9cca8c2ea29aa7c88dd1787aafa32297c41f6cb787994dd687a5003146a2d2001e72813011789c@192.168.1.1218:30001?discport=0",
"enode://3df30f1617c1f74aa7b5f535dc4494b4b9a0a3369dd3de0870e17111846da27a7ce68d77c9ab4e518bed2e96117e3b6d39dff54bd132e43cc38fdfd72fd1c8b7@192.168.1.1218:30002?discport=0",
"enode://bab291a6f105a4366115c78c0da2e7ce654acaeb702d1f586b32485eb3e213b37810bcf537b0bbffce191f198a7e41edae4e2ea1bc553a8fe671e33503844953@192.168.1.1218:30003?discport=0",
"enode://a1cca894f28d39c3d6367693da384a5e457f87ca9058947eb23ac4bf89e6fa176ef63432e7b0180397a97b4855d406bd4ae30823f43ed4bd7291a99fe39b5b64@192.168.1.1218:30004?discport=0"
]

cp static-nodes.json trusted-nodes.json
cp static-nodes.json trusted-nodes.json node1/geth/
cp static-nodes.json trusted-nodes.json node2/geth/
cp static-nodes.json trusted-nodes.json node3/geth/
cp static-nodes.json trusted-nodes.json node4/geth/

创建启动节点脚本

  • 节点启动shell脚本
    tips: node1 node2和node3为共识节点,node4为普通节点。(脚本参数有区别)
node1 start.sh
#!/bin/sh
geth --networkid 1218 --datadir ./ --syncmode 'full' --nodiscover --maxpeers 300 -verbosity 6 --ipcdisable --port 30001 --rpc --rpcaddr 0.0.0.0 --rpcport 20001 --rpcapi 'admin,eth,net,web3' --ws --ws.port 8551 --ws.addr 0.0.0.0 --ws.api 'admin,eth,net,web3' --unlock 0xBC1F42383f9567B43C219200D83C71CF144C1146 --password password.txt --mine --allow-insecure-unlock --ws.origins '*'

node2 start.sh
#!/bin/sh
geth --networkid 1218 --datadir ./ --syncmode 'full' --nodiscover --maxpeers 300 -verbosity 6 --ipcdisable --port 30002 --rpc --rpcaddr 0.0.0.0 --rpcport 20002 --rpcapi 'admin,eth,net,web3' --ws --ws.port 8552 --ws.addr 0.0.0.0 --ws.api 'admin,eth,net,web3' --unlock 0x8E744b43347d4b5127568185Ee9f5664DB92 --password password.txt --mine --allow-insecure-unlock --ws.origins '*'

node3 start.sh
#!/bin/sh
geth --networkid 1218 --datadir ./ --syncmode 'full' --nodiscover --maxpeers 300 -verbosity 6 --ipcdisable --port 30003 --rpc --rpcaddr 0.0.0.0 --rpcport 20003 --rpcapi 'eth,net,web3' --ws --ws.port 8553 --ws.addr 0.0.0.0 --ws.api 'eth,net,web3' --unlock f27533b63bdd06eb613dd056718224893c4653c2--password password.txt --mine  --allow-insecure-unlock --ws.origins '*'

node4 start.sh
#!/bin/sh
geth --networkid 1218 --datadir ./ --syncmode 'full' --nodiscover --maxpeers 300 -verbosity 6 --ipcdisable --port 30004 --rpc --rpcaddr 0.0.0.0 --rpcport 20004 --rpcapi 'eth,net,web3' --ws --ws.port 8554 --ws.addr 0.0.0.0 --ws.api 'eth,net,web3' --allow-insecure-unlock --ws.origins '*'

  • 节点启动yaml脚本
node1 docker-compose.yaml
version: '2'
services:
  eth:
    container_name: eth1
    image: ethereum/client-go:v1.10.8
    restart: always
    working_dir: /opt
    entrypoint: ./start.sh
    volumes:
      - ./:/opt
    ports:
      - "30001:30001"
      - "20001:20001"
      - "8551:8551"

node2 docker-compose.yaml
version: '2'
services:
  eth:
    container_name: eth2
    image: ethereum/client-go:v1.10.8
    restart: always
    working_dir: /opt
    entrypoint: ./start.sh
    volumes:
      - ./:/opt
    ports:
      - "30002:30002"
      - "20002:20002"
      - "8552:8552"

node3 docker-compose.yaml
version: '2'
services:
  eth:
    container_name: eth3
    image: ethereum/client-go:v1.10.8
    restart: always
    working_dir: /opt
    entrypoint: ./start.sh
    volumes:
      - ./:/opt
    ports:
      - "30003:30003"
      - "20003:20003"
      - "8553:8553"

node4 docker-compose.yaml
version: '2'
services:
  eth:
    container_name: eth1
    image: ethereum/client-go:v1.10.8
    restart: always
    working_dir: /opt
    entrypoint: ./start.sh
    volumes:
      - ./:/opt
    ports:
      - "30004:30004"
      - "20004:20004"
      - "8554:8554"

启动节点

启动命令docker-compose up -d

到此,结束。


快捷操作脚本(可选)

  • init.sh
#!/bin/sh
echo 'node1'
docker run -i --rm -v $PWD/node1:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
echo 'node1 initialize finished'

echo 'node2'
docker run -i --rm -v $PWD/node2:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
echo 'node2 initialize finished'

echo 'node3'
docker run -i --rm -v $PWD/node3:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
echo 'node3 initialize finished'

echo 'node4'
docker run -i --rm -v $PWD/node4:/root ethereum/client-go:v1.10.8 --datadir /root/ init /root/genesis.json
echo 'node4 initialize finished'

  • startup.sh
#!/bin/sh

echo 'node1'
cd /bsn/eth/node/node1
docker-compose up -d 
echo 'node1 start up finished'

echo 'node2'
cd /bsn/eth/node/node2
docker-compose up -d
echo 'node2 start up finished'

echo 'node3'
cd /bsn/eth/node/node3
docker-compose up -d
echo 'node3 start up finished'

echo 'node4'
cd /bsn/eth/node/node4
docker-compose up -d
echo 'node4 start up finished'
  • shutdown.sh
#!/bin/sh

echo 'node1'
cd /bsn/eth/node/node1
docker-compose down
echo 'node1 down finished'

echo 'node2'
cd /bsn/eth/node/node2
docker-compose down
echo 'node2 dwon finished'

echo 'node3'
cd /bsn/eth/node/node3
docker-compose down
echo 'node3 down finished'

echo 'node4'
cd /bsn/eth/node/node4
docker-compose down
echo 'node4 down finished'
  • clear.sh
#!/bin/sh

echo 'node1'
cd /bsn/eth/node/node1/geth
rm -rf chaindata/ lightchaindata/ nodes/ triecache/       
echo 'node1 clear up finished'

echo 'node2'
cd /bsn/eth/node/node2/geth
rm -rf chaindata/ lightchaindata/ nodes/ triecache/
echo 'node2 clear up finished'

echo 'node3'
cd /bsn/eth/node/node3/geth
rm -rf chaindata/ lightchaindata/ nodes/ triecache/
echo 'node3 clear up finished'

echo 'node4'
cd /bsn/eth/node/node4/geth
rm -rf chaindata/ lightchaindata/ nodes/ triecache/
echo 'node4 clear up finished'

注意事项

新版版命令有变化,rpc 需改为http。

start.sh

#!/bin/sh
geth --networkid 5555 --datadir ./ --syncmode 'full' --nodiscover --maxpeers 300 -verbosity 6 --ipcdisable --port 30001 --rpc --rpcaddr 0.0.0.0 --rpcport 20001 --rpcapi 'admin,eth,net,web3,txpool,personal,clique' --ws --ws.port 8551 --ws.addr 0.0.0.0 --ws.api 'admin,eth,net,web3' --unlock 0xdf71263c079757551de2dc098f370c5387cb164b --password password.txt --mine --allow-insecure-unlock --ws.origins '*'

相关文章

网友评论

      本文标题:搭建以太坊私有链POA

      本文链接:https://www.haomeiwen.com/subject/tvxiortx.html