BESU IBFT2.0 docker+静态引导部署文档
1、准备环境
OS |
|
|
ubuntu22.04 lts |
|
|
docker |
24.0.5 |
|
docker-compose |
v2.15.0 |
|
besu |
besu/v21.10.1/linux-x86_64/oracle-java-12 |
|
2、相关文档
1、besu官方文档
https://besu.hyperledger.org/private-networks/tutorials/ibft
2、besu二进制下载地址
https://hyperledger.jfrog.io/ui/native/besu-binaries/besu
3、创建网络配置文件ibftConfigFile.json
{
"genesis": {
"config": {
"chainId": 1337,
"muirglacierblock": 0,
"contractSizeLimit": 2147483647,
"ibft2": {
"blockperiodseconds": 2,
"epochlength": 30000,
"requesttimeoutseconds": 4
}
},
"nonce": "0x0",
"timestamp": "0x58ee40ba",
"gasLimit": "0x1fffffffffffff"
"difficulty": "0x1",
"mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"fe3b557e8fb62b89f4916b721be55ceb828dbd73": {
"privateKey": "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "0xad78ebc5ac6200000"
},
"627306090abaB3A6e1400e9345bC60c78a8BEf57": {
"privateKey": "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
},
"f17f52151EbEF6C7334FAD080c5704D77216b732": {
"privateKey": "ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
}
}
},
"blockchain": {
"nodes": {
"generate": true,
"count": 4
}
}
}
配置解释
genesis 创世块区
ibft2 共识机制
gasLimit 配置可以和开发商量目前设置为最大
contractSizeLimit 合约大小限制
固定配置
nonce
difficulty
mixHash 用于伊斯坦布尔区块识别
alloc 初始化钱包及金额
blockchain 初始化共识节点个数及key key.pub
4、创建创世块文件及节点key key.pub
# 二进制执行命令
besu operator generate-blockchain-config --config-file=ibftConfigFile.json --to=network --private-key-file-name=key
# docker方式执行 ibftConfigFile.json需要在当前目录下
docker run --rm -it --privileged --user root -v $PWD:/abc hyperledger/besu:21.10.1 operator generate-blockchain-config --config-file=/abc/ibftConfigFile.json --to=/abc/network --private-key-file-name=key
# network目录结构
network
├── genesis.json # 创世块文件
└── keys
├── 0xc003bf00eea7331fa961b65cd4cf873c9ba42b3c #账户地址
│ ├── key # 账户私钥
│ └── key.pub # 账户公钥 去掉0x 后面的就为enode
├── 0xd4390c18acfc2dc6145a2e929bc4dc42a89e3f76
│ ├── key
│ └── key.pub
├── 0xe4b2d18d8deb825ddc9f17f030f496718e592197
│ ├── key
│ └── key.pub
└── 0xfa1c09fe2f7ecdc50245ade6a80154bb97bb2b4c
├── key
└── key.pub
5、创建对等节点文件static-nodes.json
[
"enode://f3d968bbd2a0f93f2b4eb82ab37e3b4a99aa187b32ce4984b157c1a996a107cfc3171ddd2615f3de3a8a75486d96f55390f410a77293ca812254d095c449a493@10.0.7.189:30303",
"enode://0e9dcf6c0c59f333ddb9f2c6a727f2931939e478a9405e14325574ff6fc02f68d66408d366f2bf47f1ac97c82008720ee3c9a2256a0fb6098e5677cdf555277f@10.0.7.189:30305",
"enode://de4c0184b737cf882815eed85125add734695a6d35d40838f6fcfa158c3e11e5568daa84a0923392a5f2a58e09d815dc7fcc0a98739c849305155caab3e4d2d6@10.0.7.189:30304",
"enode://d6fba24ee98ef34c4f83b7f2940ba1ee65672222287a1713e53cc7cd4c35be64f4ccca9c3dbbb02b3dde1d84b7afbe05474be66ca25814d7a4aadd11839a2abb@10.0.7.189:30306"
]
配置解释
f3d968bbd2a0f93f2b4eb82ab37e3b4a99aa187b32ce4984b157c1a996a107cfc3171ddd2615f3de3a8a75486d96f55390f410a77293ca812254d095c449a493 节点地址标识 从key.pub中获取 将0x 去掉即可
10.0.7.189 节点IP
30303 p2p端口
6、创建节点启动目录
# 创建目录
mkdir node{1..4}/data -p
# 将账户地址下的key key.pub 分别拷贝到node1-node4 data目录中
cp network/0xc003bf00eea7331fa961b65cd4cf873c9ba42b3c/* node1/data/
cp network/0xd4390c18acfc2dc6145a2e929bc4dc42a89e3f76/* node2/data/
cp network/0xe4b2d18d8deb825ddc9f17f030f496718e592197/* node3/data/
cp network/0xfa1c09fe2f7ecdc50245ade6a80154bb97bb2b4c/* node4/data/
# 将创世块文件拷贝到node1-node4下
cp network/genesis.json node1/
cp network/genesis.json node2/
cp network/genesis.json node3/
cp network/genesis.json node4/
7、创建节点配置文件config.json
# Valid TOML config file 数据目录
data-path="/bs/data" # Path
# Chain 创世文件
genesis-file="/bs/genesis.json" # Path to the custom genesis file
# Mining 开启挖矿
miner-enabled=true
# miner-coinbase="0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"
# 发放奖励指定账户
miner-coinbase="0x0000000000000000000000000000000000000000"
min-gas-price="0"
# 以下3块(p2p\rpc\ws)的host注意设置本机可访问的内外网IP
p2p-host="0.0.0.0"
p2p-port=30303
#max-peers=42
# 第一次启动先注释掉,启动之后复制enodeURL配置好在打开注释,引导节点也是需要这样做,配置自己的enodeURL即可
#bootnodes=["enode://f3d968bbd2a0f93f2b4eb82ab37e3b4a99aa187b32ce4984b157c1a996a107cfc3171ddd2615f3de3a8a75486d96f55390f410a77293ca812254d095c449a493@10.0.10.73:30303"]
# RPC 接口配置
rpc-http-enabled=true
rpc-http-host="0.0.0.0"
rpc-http-port=8545
rpc-http-cors-origins=["all"]
# WS请求配置
rpc-ws-enabled=true
rpc-ws-host="0.0.0.0"
rpc-ws-port=2829
host-allowlist=["*"]
# 用于远程管理 管理方法需要添加ADMIN支持
#rpc-http-api=["ADMIN","ETH","NET","IBFT","WEB3"]
rpc-http-api=["ADMIN","EEA","WEB3","ETH","NET","PRIV","PERM","IBFT"]
rpc-ws-api=["ADMIN","EEA","WEB3","ETH","NET","PRIV","PERM","IBFT"]
revert-reason-enabled=true
# 用于调试
#logging="DEBUG"
# 将配置文件拷贝到node1-node4 下
cp config.json node1
cp config.json node2
cp config.json node3
cp config.json node4
注: 其他节点修改p2p-port 、rpc-http-port 、rpc-ws-port
8、创建docker-compose启动
创建启动脚本
#!/bin/sh
besu --config-file=/bs/config.json
创建docker-compose.yaml
version: '3'
services:
besu:
container_name: besu1
image: hyperledger/besu:21.10.1
restart: always
user: root
working_dir: /bs
entrypoint: ./start.sh
volumes:
- ./:/bs
ports:
- "30303:30303"
- "8545:8545"
- "2829:2829"
注 其他节点修改对应端口号
9、创世块文件genesis.json解读
{
"config" : {
"chainId" : 1337,
"muirglacierblock" : 0,
"ibft2" : {
"blockperiodseconds" : 2,
"epochlength" : 30000,
"requesttimeoutseconds" : 4
}
},
"nonce" : "0x0",
"timestamp" : "0x58ee40ba",
"gasLimit" : "0x1fffffffffffff",
"difficulty" : "0x1",
"mixHash" : "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
"coinbase" : "0x0000000000000000000000000000000000000000",
"alloc" : {
"fe3b557e8fb62b89f4916b721be55ceb828dbd73" : {
"privateKey" : "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
"comment" : "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance" : "0xad78ebc5ac6200000"
},
"627306090abaB3A6e1400e9345bC60c78a8BEf57" : {
"privateKey" : "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"comment" : "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance" : "90000000000000000000000"
},
"f17f52151EbEF6C7334FAD080c5704D77216b732" : {
"privateKey" : "ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
"comment" : "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance" : "90000000000000000000000"
}
},
"extraData" : "0xf87ea00000000000000000000000000000000000000000000000000000000000000000f85494aea9a4e1787587a091c2338d3d54e176a449230694f967d7e6ff24e610f6da5fe5153c7c42525cf3cb942d70b42854f074528ff203a051912755a4a3ada494d3ef04426c0ea78c7ae9c9a1f5074210ff926424808400000000c0"
}
extraData 来源是由初始化节点账户rlp编码后得到
生成命令 extraData在创建创世块文件是自动添加的 这里只是演示如何手动生成
besu rlp encode --from=toEncode.json
toEncode.json 内容是去掉0x后的账户地址
["c003bf00eea7331fa961b65cd4cf873c9ba42b3c","d4390c18acfc2dc6145a2e929bc4dc42a89e3f76","e4b2d18d8deb825ddc9f17f030f496718e592197","fa1c09fe2f7ecdc50245ade6a80154bb97bb2b4c"]
10、日志检查节点出块
2023-09-01 09:08:04.310+00:00 | nioEventLoopGroup-3-7 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:09.311+00:00 | EthScheduler-Timer-0 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:14.311+00:00 | EthScheduler-Timer-0 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:19.312+00:00 | EthScheduler-Timer-0 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:24.312+00:00 | EthScheduler-Timer-0 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:29.313+00:00 | EthScheduler-Timer-0 | INFO | FullSyncTargetManager | No sync target, waiting for peers: 4
2023-09-01 09:08:30.210+00:00 | pool-8-thread-1 | INFO | IbftRound | Importing block to chain. round=ConsensusRoundIdentifier{Sequence=1, Round=3}, hash=0x9caa1f5e9c024904218937a20cbd46439bae7689582ad8957bccd61caef9279c
2023-09-01 09:08:30.233+00:00 | pool-8-thread-1 | INFO | IbftBesuControllerBuilder | Imported #1 / 0 tx / 0 pending / 0 (0.0%) gas / (0x9caa1f5e9c024904218937a20cbd46439bae7689582ad8957bccd61caef9279c)
2023-09-01 09:08:32.058+00:00 | pool-8-thread-1 | INFO | IbftBesuControllerBuilder | Imported #2 / 0 tx / 0 pending / 0 (0.0%) gas / (0xf146d547483c2ff0ddc7543e16e4549905ae350d72db8e8ec17b70cdffbe90ed)
2023-09-01 09:08:32.058+00:00 | EthScheduler-Workers-0 | INFO | PersistBlockTask | Imported #2 / 0 tx / 0 om / 0 (0.0%) gas / (0xf146d547483c2ff0ddc7543e16e4549905ae350d72db8e8ec17b70cdffbe90ed) in 0.012s. Peers: 4
2023-09-01 09:08:34.045+00:00 | pool-8-thread-1 | INFO | IbftBesuControllerBuilder | Produced #3 / 0 tx / 0 pending / 0 (0.0%) gas / (0x336cc0da8bbdf3fdbd8146e7b83acd7c694a27374758b095fc542e2b03c3ddea)
2023-09-01 09:08:36.034+00:00 | pool-8-thread-1 | INFO | IbftBesuControllerBuilder | Imported #4 / 0 tx / 0 pending / 0 (0.0%) gas / (0xd730f2f86b0f8873db17d2b985fa55c11a054533b6a232764039b9ba0143b9d8)
11、添加共识节点
方法一
# 复制一个新的ibftConfigFile.json
# 新的ibftConfigFile.json 文件只是为了生成新曾共识节点的key 和key.pub,需要几个节点在"count"字段自行修改 这里新增一个共识 修改为1,命令生成的genesis.json 需要删除,并且命令--to不能覆盖之前的目录
besu operator generate-blockchain-config --config-file=ibftConfigFile.json --to=newnetwork --private-key-file-name=key
{
"genesis": {
"config": {
"chainId": 1337,
"muirglacierblock": 0,
"ibft2": {
"blockperiodseconds": 2,
"epochlength": 30000,
"requesttimeoutseconds": 4
}
},
"nonce": "0x0",
"timestamp": "0x58ee40ba",
"gasLimit": "0x1fffffffffffff"
"difficulty": "0x1",
"mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"fe3b557e8fb62b89f4916b721be55ceb828dbd73": {
"privateKey": "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "0xad78ebc5ac6200000"
},
"627306090abaB3A6e1400e9345bC60c78a8BEf57": {
"privateKey": "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
},
"f17f52151EbEF6C7334FAD080c5704D77216b732": {
"privateKey": "ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f",
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored",
"balance": "90000000000000000000000"
}
}
},
"blockchain": {
"nodes": {
"generate": true,
"count": 1
}
}
}
方法二
# besu --data-path=newnode public-key export-address 输出结果
[root@ip-172-31-40-249 test]# besu --data-path=besu public-key export-address
2023-09-01 17:43:55.869+08:00 | main | INFO | KeyPairUtil | Generated new public key 0xa226ec5acd80dda04332c12cdb318b75e833f782932dc249513950277ecd614619f29137673ebe4fe685b3102cb66f664b375c03242bf131203263a47d4742f2 and stored it to /bs/test/newnode/key
0xa3ae963858a88d04f451dc215103905884c73c6f
[root@ip-172-31-40-249 test]#
[root@ip-172-31-40-249 test]# ll newnode/
total 4
-rw------- 1 root root 66 Sep 1 17:43 key
0xa3ae963858a88d04f451dc215103905884c73c6f 为钱包地址
0xa226ec5acd80dda04332c12cdb318b75e833f782932dc249513950277ecd614619f29137673ebe4fe685b3102cb66f664b375c03242bf131203263a47d4742f2 及为key.pub
整理node5 启动文件
cp genesis.json node5
创建 config.json node5
创建 start.sh node5
创建 docker-compose.yaml node5
cp newnode/key node5/data
cp newnode/key.pub node5/data
共识提权
# 执行需要超过半数节点进行投票
curl -X POST --data '{"jsonrpc":"2.0","method":"ibft_proposeValidatorVote","params":["0xa3ae963858a88d04f451dc215103905884c73c6f", true], "id":1}' http://127.0.0.1:8545
12、共识查询
curl -X POST --data '{"jsonrpc":"2.0","method":"ibft_getValidatorsByBlockNumber","params":["latest"], "id":1}' http://127.0.0.1:8545
13、添加对等节点操作
# 添加节点
curl -X POST --data '{"jsonrpc":"2.0","method":"admin_addPeer","params":["enode://f59c0ab603377b6ec88b89d5bb41b98fc385030ab1e4b03752db6f7dab364559d92c757c13116ae6408d2d33f0138e7812eb8b696b2a22fe3332c4b5127b22a3@127.0.0.1:30304"],"id":1}' http://127.0.0.1:8545
# 查询快高
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":51}' http://127.0.0.1:8545
curl -X POST -H "Content-Type: application/json" --data '{ "query": "{block{number}}"}' http://localhost:8547/graphql
# 查询块hash
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x68B3", true],"id":1}' http://127.0.0.1:8545
# 查询块交易数
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["0xe8"],"id":51}' http://127.0.0.1:8545
curl -X POST -H "Content-Type: application/json" --data '{ "query": "{block(number:232){transactionCount}}"}' http://localhost:8547/graphql
# 其他命令详见
https://besu.hyperledger.org/public-networks/reference/api