一、二进制启动IBFT共识polygon本地网节点,二、二进制启动POS共识polygon本地网节点,三、docker启动POS共识的polygon本地网节点
参考文档:
注:共有两种方式获取polygon二进制程序,
polygon-edg
二进制程序wget https://github.com/0xPolygon/polygon-edge/releases/download/v0.3.3/polygon-edge_0.3.3_linux_amd64.tar.gz
tar zxvf polygon-edge_0.3.3_linux_amd64.tar.gz
polygon-edg
git clone https://github.com/0xPolygon/polygon-edge.git
cd polygon-edge/
go build main.go -o polygon-edge
mv polygon-edge /usr/local/bin
为 IBFT 初始化数据文件夹并生成验证者
为了启动并运行 IBFT您需要初始化数据文件夹,每个节点一个:
# 节点一
# ./polygon-edge secrets init --data-dir /opt/polygon/test-chain-1
[SECRETS INIT]
Public key (address) = 0xeeA57f423e4e9f3aa3D9e733Dc0671565ee3cF9D
Node ID = 16Uiu2HAm7uV6NMo46HQh5sPMf1nqRhZACdLsqjwhDdLngDPLK7JN
# 节点二
# ./polygon-edge secrets init --data-dir /opt/polygon/test-chain-2
[SECRETS INIT]
Public key (address) = 0x1aC54B4A7380ec77D7a429D58E36D0195033dA72
Node ID = 16Uiu2HAmBopavD9PeS6gEMZ6ftYWyguVSJpTQkCdgv84ySEUmnhR
# 节点三
# ./polygon-edge secrets init --data-dir /opt/polygon/test-chain-3
[SECRETS INIT]
Public key (address) = 0xEfD020642aCc83637d42544d7f394ff32FdC9Cce
Node ID = 16Uiu2HAmCnJ3qkiymdGX4EXcqKCZcCp5iktPdDYSnpSgwNWjWzQ5
# 节点四
# ./polygon-edge secrets init --data-dir /opt/polygon/test-chain-4
[SECRETS INIT]
Public key (address) = 0xe5dF525C99edb54085d1bc8d7bc94ec48D8c4c59
Node ID = 16Uiu2HAmJet7diAMWwJVB9q8vpvMpe8wkvaixqWMhhWNQHF9RFfh
为引导节点准备multiaddr连接字符串
# /opt/polygon/polygon-edge genesis --consensus ibft --chain-id 666666 --ibft-validators-prefix-path test-chain- --bootnode /ip4/127.0.0.1/tcp/10001/p2p/16Uiu2HAm7uV6NMo46HQh5sPMf1nqRhZACdLsqjwhDdLngDPLK7JN --bootnode /ip4/127.0.0.1/tcp/20001/p2p/16Uiu2HAmBopavD9PeS6gEMZ6ftYWyguVSJpTQkCdgv84ySEUmnhR --bootnode /ip4/127.0.0.1/tcp/30001/p2p/16Uiu2HAmCnJ3qkiymdGX4EXcqKCZcCp5iktPdDYSnpSgwNWjWzQ5 --bootnode /ip4/127.0.0.1/tcp/40001/p2p/16Uiu2HAmJet7diAMWwJVB9q8vpvMpe8wkvaixqWMhhWNQHF9RFfh --premine 0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2:6666666666666000000000000000000
[GENESIS SUCCESS]
Genesis written to ./genesis.json
注:该文件会在执行命令的当前目录生成一个genesis文件
参数解释:
因为我们试图在同一台机器上运行由 4 个节点组成的 Polygon Edge 网络,所以我们需要注意避免端口冲突。这就是为什么我们将使用以下推理来确定节点的每个服务器的侦听端口:
# 默认端口号
default-grpc: 9632,9633,9634,9635
default-p2p: 1478,1479,1480,1481
default-rpc: 8545,8546,8547,8548
# 我们使用的端口号
use-grpc: 10000,20000,30000,40000
use-p2p: 10001,20001,30001,40001
use-rpc: 10002,20002,30002,40002
/opt/polygon/polygon-edge server --data-dir /opt/polygon/test-chain-1 --chain /opt/polygon/genesis.json --grpc 0.0.0.0:10000 --libp2p 0.0.0.0:10001 --jsonrpc 0.0.0.0:10002 --seal
/opt/polygon/polygon-edge server --data-dir /opt/polygon/test-chain-2 --chain /opt/polygon/genesis.json --grpc 0.0.0.0:20000 --libp2p 0.0.0.0:20001 --jsonrpc 0.0.0.0:20002 --seal
/opt/polygon/polygon-edge server --data-dir /opt/polygon/test-chain-3 --chain /opt/polygon/genesis.json --grpc 0.0.0.0:30000 --libp2p 0.0.0.0:30001 --jsonrpc 0.0.0.0:30002 --seal
/opt/polygon/polygon-edge server --data-dir /opt/polygon/test-chain-4 --chain /opt/polygon/genesis.json --grpc 0.0.0.0:40000 --libp2p 0.0.0.0:40001 --jsonrpc 0.0.0.0:40002 --seal
注:启动以上节点时,应使用后台启动方式,建议可使用
nohup
或screen
进程管理工具
参数配置:
现在您已经设置了至少 1 个正在运行的客户端,您可以继续使用您在上面预先设置的帐户并通过指定 4 个节点中的任何一个的 JSON-RPC URL 来与区块链进行交互:
节点 1:http://localhost:10002
节点 2:http://localhost:20002
节点 3:http://localhost:30002
节点 4:http://localhost:40002
# curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":["latest", true],"id":1}' http://127.0.0.1:10002
{"jsonrpc":"2.0","id":1,"result":"0x11c"}
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2", "latest"],"id":1}' http://127.0.0.1:20002
{"jsonrpc":"2.0","id":1,"result":"0x5425296ac1cc75b84c90680000"}
注:共有两种方式获取polygon二进制程序,
polygon-edg
二进制程序wget https://github.com/0xPolygon/polygon-edge/releases/download/v0.3.3/polygon-edge_0.3.3_linux_amd64.tar.gz
tar zxvf polygon-edge_0.3.3_linux_amd64.tar.gz
polygon-edg
git clone https://github.com/0xPolygon/polygon-edge.git
cd polygon-edge/
go build main.go -o polygon-edge
mv polygon-edge /usr/local/bin
为 IBFT 初始化数据文件夹并生成验证者
# /opt/polygon/polygon-edge secrets init --data-dir /opt/polygon/test-chain
[SECRETS INIT]
Public key (address) = 0x2a2C8597F9fC2e8557Bf9dbfc818F97C3a79931c
Node ID = 16Uiu2HAkvses9iF7zfm4E5SjEn4AYhqSLTqyaRGseQzCXjNygahP
为引导节点准备multiaddr连接字符串
# /opt/polygon/polygon-edge genesis --pos --chain-id 666666 --ibft-validator 0x2a2C8597F9fC2e8557Bf9dbfc818F97C3a79931c --bootnode /ip4/127.0.0.1/tcp/10001/p2p/16Uiu2HAkvses9iF7zfm4E5SjEn4AYhqSLTqyaRGseQzCXjNygahP --premine 0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2:6666666666666000000000000000000
[GENESIS SUCCESS]
Genesis written to ./genesis.json
注:该文件会在执行命令的当前目录生成一个genesis文件
参数解释:
# /opt/polygon/polygon-edge server --data-dir /opt/polygon/test-chain --chain /opt/polygon/genesis.json --grpc 0.0.0.0:10000 --libp2p 0.0.0.0:10001 --jsonrpc 0.0.0.0:10002 --seal
注:启动以上节点时,应使用后台启动方式,建议可使用
nohup
或screen
进程管理工具
参数配置:
现在我们已经运行了一个POS共识的单节点,可使用该节点的JSON-RPC URL 来与区块链进行交互:
# curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":["latest", true],"id":1}' http://127.0.0.1:10002
{"jsonrpc":"2.0","id":1,"result":"0x50"}
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2", "latest"],"id":1}' http://127.0.0.1:10002
{"jsonrpc":"2.0","id":1,"result":"0x5425296ac1cc75b84c90680000"}
FROM alpine
RUN wget -O polygon.tar.gz https://github.com/0xPolygon/polygon-edge/releases/download/v0.3.3/polygon-edge_0.3.3_linux_amd64.tar.gz \
&& tar -xzvf polygon.tar.gz \
&& mv polygon-edge /usr/local/bin \
&& apk update \
&& apk add net-tools
EXPOSE 9632 1478 8545
WORKDIR /data
ENTRYPOINT ["polygon-edge"]
docker build . -t polygon-edge:v0.3.3
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
polygon-edge v0.3.3 0716fb295c1d 2 minutes ago 63.5MB
# cat init.sh
#!/bin/bash
docker run --rm -v $(pwd):/data polygon-edge:v0.3.3 secrets init --data-dir /data/chain-data
# sh init.sh
[SECRETS INIT]
Public key (address) = 0x7D6d2B7deD849Ed899aF49A4e1BaB75bCEa6181E
Node ID = 16Uiu2HAmSBjoU9rLpFKVT19x4sXje21BsfGYvvr6jTqpPjiBKbRj
# cat genesis.sh
#!/bin/bash
# 生成genesis文件
docker run --rm -v $(pwd):/data polygon-edge:v0.3.3 genesis --pos --chain-id 666666 --ibft-validator 0x7D6d2B7deD849Ed899aF49A4e1BaB75bCEa6181E --bootnode /ip4/127.0.0.1/tcp/10001/p2p/16Uiu2HAmSBjoU9rLpFKVT19x4sXje21BsfGYvvr6jTqpPjiBKbRj --premine 0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2:6666666666666000000000000000000
注:启动信息中的
validator
与bootnode
需要与上一步初始化后的节点信息一致
# sh genesis.sh
[GENESIS SUCCESS]
Genesis written to ./genesis.json
# cat run.sh
#!/bin/bash
#启动polygon
docker run -itd --restart=unless-stopped -v /opt/docker/polygon:/data -v /etc/timezone:/etc/timezone --name polygon -p 10000:10000 -p 10001:10001 -p 10002:10002 polygon-edge:v0.3.3 server --data-dir chain-data --chain genesis.json --grpc 0.0.0.0:10000 --libp2p 0.0.0.0:10001 --jsonrpc 0.0.0.0:10002 --seal
#polygon节点启动后日志查看
docker logs -f polygon --tail 10
# sh run.sh
// 节点启动
efab7aeec2a2cd44f54c2224991814a5a6df31fb190aafd4d8002a87c5626ae3
2022-04-27T12:26:31.125Z [INFO] polygon: Data dir: path=chain-data
2022-04-27T12:26:31.171Z [INFO] polygon.blockchain: genesis: hash=0xd32791b8e0160a9c575c53702b64abdb08853f03c9294af7c3d2af330167c6ff
2022-04-27T12:26:31.171Z [INFO] polygon.jsonrpc: http server started: addr=0.0.0.0:10002
2022-04-27T12:26:31.172Z [INFO] polygon.consensus.ibft: validator key: addr=0x7D6d2B7deD849Ed899aF49A4e1BaB75bCEa6181E
2022-04-27T12:26:31.172Z [INFO] polygon: GRPC server running: addr=0.0.0.0:10000
2022-04-27T12:26:31.172Z [INFO] polygon.network: LibP2P server running: addr=/ip4/172.17.0.4/tcp/10001/p2p/16Uiu2HAmSBjoU9rLpFKVT19x4sXje21BsfGYvvr6jTqpPjiBKbRj
2022-04-27T12:26:31.172Z [INFO] polygon.network: Omitting bootnode with same ID as host: id=16Uiu2HAmSBjoU9rLpFKVT19x4sXje21BsfGYvvr6jTqpPjiBKbRj
// 节点出块
2022-04-27T12:26:31.173Z [INFO] polygon.consensus.ibft: build block: number=1 txns=0
2022-04-27T12:26:31.175Z [INFO] polygon.consensus.ibft: state change: new=ValidateState
2022-04-27T12:26:31.177Z [INFO] polygon.consensus.ibft: state change: new=CommitState
2022-04-27T12:26:31.177Z [INFO] polygon.blockchain: write block: num=1 parent=0xd32791b8e0160a9c575c53702b64abdb08853f03c9294af7c3d2af330167c6ff
2022-04-27T12:26:31.179Z [INFO] polygon.blockchain: new block: number=1 hash=0x3e1c60b82748c2065416bc3fb1c7ddecc3429572b60f8047ed273e5fada2f80e txns=0 generation_time_in_seconds=1651062391
2022-04-27T12:26:31.179Z [INFO] polygon.consensus.ibft: block committed: sequence=1 hash=0x3e1c60b82748c2065416bc3fb1c7ddecc3429572b60f8047ed273e5fada2f80e validators=1 rounds=1 committed=1
2022-04-27T12:26:31.179Z [INFO] polygon.consensus.ibft: state change: new=AcceptState
2022-04-27T12:26:31.179Z [INFO] polygon.consensus.ibft.acceptState: Accept state: sequence=2 round=1
2022-04-27T12:26:31.179Z [INFO] polygon.consensus.ibft: current snapshot: validators=1
2022-04-27T12:26:31.180Z [INFO] polygon.consensus.ibft.acceptState: we are the proposer: block=2
2022-04-27T12:26:31.180Z [INFO] polygon.consensus.ibft: executed txns: failed =0 successful=0 remaining in pool=0
2022-04-27T12:26:31.180Z [INFO] polygon.consensus.ibft: build block: number=2 txns=0
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efab7aeec2a2 polygon-edge:v0.3.3 "polygon-edge server…" About a minute ago Up About a minute 1478/tcp, 8545/tcp, 9632/tcp, 0.0.0.0:10000-10002->10000-10002/tcp, :::10000-10002->10000-10002/tcp polygon
现在我们已经运行了一个POS共识的单节点polygon容器,可使用该节点的JSON-RPC URL 来与区块链进行交互:
# curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":["latest", true],"id":1}' http://127.0.0.1:10002
{"jsonrpc":"2.0","id":1,"result":"0x4b"}
# curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x6e60F5243e1a3F0Be3F407b5AFE9e5395ee82aa2", "latest"],"id":1}' http://127.0.0.1:10002
{"jsonrpc":"2.0","id":1,"result":"0x5425296ac1cc75b84c90680000"}
以上,就是今天分享的全部内容了。
希望大家通过以上方式可以解决自己的实际需求,解决自己目前所遇到的问题。
如果在部署过程中有任何疑问,可以添加我的个人微信【jemooner】,备注:地区-职业方向-昵称,欢迎来撩,加入区块链技术交流群,与更多的区块链技术大佬学习交流。
原创不易,码字不易。 觉得这篇文章对你有点用的话,麻烦你为本文点个赞,留言或转发一下,因为这将是我输出更多优质文章的动力,感谢!
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!