问题:之前配置了两个节点,设置两节点的port 和rpcport 不同之后,尝试添加节点建立连接,但是失败,addPeers 返回true,但数量就一直为0,错误如下:
启动节点1;
geth --datadir "MyChain" --networkid 2333 --rpc --rpcport 8545 -port 30003 --rpcaddr 127.0.0.1 --rpccorsdomain "*" --rpcapi eth,net,web3,personal --nodiscover --allow-insecure-unlock console console 2>> D://test-ethreum//Mychain//01.log
启动节点2(再开一个终端)
geth --datadir "Mychain2" --networkid 2333 --rpc --rpcport 8535 --port 30002 --rpcaddr 127.0.0.2 --rpccorsdomain "*" --rpcapi eth,net,web3,personal --nodiscover --ipcpath D://test-ethreum//Mychain2//geth1.ipc --allow-insecure-unlock console 2>> D://test-ethreum//Mychain2//02.log
在节点1中添加节点2的连接;
>admin.addPeer("enode://f8dcde9106436e80767bc684786fe85ea37d3b9b4d01167dd0ff65189ea1081596dc279fb15a19bea1a669026b29be6dca121d70d756b2fc2fb722a828af75a7@127.0.0.1:30002?discport=0")
true
> admin.peers
[]
添加返回true了,但是核验的时候显示没有节点。
解决:将节点2(Mychain2)删除,重新初始化配置文件,生成节点2
因为Mychain2中的数据是之前创建的,不是初次启动的过程(数据已过期 outdated),所以添加会失败,需要删除,重新初始化,生成创世区块,启动节点2
节点2 删除:
geth removedb Mychain2 data
#删除缓存,然后清理Mychain2中文件
#重新生成创世区块
geth --datadir "Mychain2" init genesis.json
#启动节点2
geth --datadir "Mychain2" --networkid 2333 --rpc --rpcport 8535 --port 30002 --rpcaddr 127.0.0.2 --rpccorsdomain "*" --rpcapi eth,net,web3,personal --nodiscover --ipcpath D://test-ethreum//Mychain2//geth1.ipc --allow-insecure-unlock console 2>> D://test-ethreum//Mychain2//02.log
#在节点1中连接节点2
> admin.addPeer("enode://26c7de0c1cce4ccaaf2a1987f8ebb618af1976f3d84805e40727bcf1d2b41648a0571867a3e75757c2f9da8e3f3ff21837fb36b6761defe2233632d2cf36440d@127.0.0.1:30002?discport=0")
true
> admin.peers
[{
caps: ["eth/64", "eth/65", "eth/66", "snap/1"],
enode: "enode://26c7de0c1cce4ccaaf2a1987f8ebb618af1976f3d84805e40727bcf1d2b41648a0571867a3e75757c2f9da8e3f3ff21837fb36b6761defe2233632d2cf36440d@127.0.0.1:30002?discport=0",
id: "5aaca3a0c06c7fa14a8f663b629ca2a59529f36a04a9222f090d7c539d0b53c8",
name: "Geth/v1.10.2-unstable/windows-amd64/go1.16.3",
network: {
inbound: false,
localAddress: "127.0.0.1:51708",
remoteAddress: "127.0.0.1:30002",
static: true,
trusted: false
},
protocols: {
eth: {
difficulty: 32,
head: "0x9501ae5d402284cf9bd7db980132b8dadd0cc1edd27a3b25cf5404f8d5d23179",
version: 66
},
snap: {
version: 1
}
}
}]
#美滋滋,成功了,节点2中也可以看到节点1的连接
> admin.peers
[{
caps: ["eth/64", "eth/65", "eth/66", "snap/1"],
enode: "enode://b83871bd2d30d672cf464c60efec469c0d0a54f93a2c9ff525a00672c827a682aa4398fbfc43593ce5256afa06a7389446fbf87a9ceb9d5ecaea671a2aa48887@127.0.0.1:51708",
id: "15ad008d0421c76f8da66ed457c4c9303af8615e637e56d8ae6aabea4842f3a8",
name: "Geth/v1.10.2-unstable/windows-amd64/go1.16.3",
network: {
inbound: true,
localAddress: "127.0.0.1:30002",
remoteAddress: "127.0.0.1:51708",
static: false,
trusted: false
},
protocols: {
eth: {
difficulty: 9128073,
head: "0x08fceb9eb5e93b33824238426eb5996cf8cb74613948c3d4031d097ca3acde69",
version: 66
},
snap: {
version: 1
}
}
}]
链接:https://ethereum.stackexchange.com/questions/12122/addpeer-doesnt-work