构建以太坊网络
测试网络
搭建私有以太坊私有网络
搭建私有网络
# 1. Clone go-ethereum
git clone https://github.com/ethereum/go-ethereum.git
# 2. Build go-ethereum, make sure you have installed go, version >= 1.19
cd go-ethereum && make all
# 3. Copy geth to $GOPATH/bin
cp build/bin/geth $GOPATH/bin
# 3. Create private network with two nodes
cd $HOME/priveth && mkdir node1 node2
# 4. Create accounts
# On node1
geth --datadir $HOME/priveth/node1 account new
# Password: foobar
# Account1 -> Public address of the key: 0x46eE6c1779eB3421Fc355132D867804894eFB1F9
# Account2 -> Public address of the key: 0x4B3Beee7E6E067080f71336fF118E76E53C63cA3
# Signer address -> 0x3D43ed16b178611C0aBA87a00Dd6b2655aa89287
# On node2
geth --datadir $HOME/priveth/node2 account new
# Public address of the key: 0x69b24ce645c0Bbdfcc0FE4297804b82F888DE8Fe
# 5. Create genesis config
geth --datadir $HOME/priveth/node1 init genesis.json
geth --datadir $HOME/priveth/node2 init genesis.json
# 6. Start bootnode, 在其中一个 node 启动即可
bootnode -genkey boot.key
bootnode -nodekey boot.key -addr :30305
# 6. Start
# Start node1, locate to $HOME/priveth
geth --datadir node1 --port 30306 --bootnodes 'enode://3988b0cf1d24581e50d1a888e0c9588b1f7403477cf621f46c7add34b2bf786865601c4b6a07924e5f1eed250214192355b5ea5571e5887055e464ccb91dce3e@127.0.0.1:0?discport=30305' --networkid 12345 --unlock 0x3D43ed16b178611C0aBA87a00Dd6b2655aa89287 --password node1/walletpwd.txt --authrpc.port 8551 --http.vhosts=*
# Start node2
geth --datadir node2 --port 30307 --bootnodes 'enode://3988b0cf1d24581e50d1a888e0c9588b1f7403477cf621f46c7add34b2bf786865601c4b6a07924e5f1eed250214192355b5ea5571e5887055e464ccb91dce3e@127.0.0.1:0?discport=30305' --networkid 12345 --unlock 0x69b24ce645c0Bbdfcc0FE4297804b82F888DE8Fe --password node2/walletpwd.txt --authrpc.port 8552
# Attach
geth attach node1/geth.ipc
curl --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params": ["0x3D43ed16b178611C0aBA87a00Dd6b2655aa89287", "latest"], "id":2}' -H "Content-Type: application/json" localhost:8551
genesis.json
content
{
"config": {
"chainId": 12345,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"clique": {
"period": 5,
"epoch": 30000
}
},
"difficulty": "1",
"gasLimit": "800000000",
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000003D43ed16b178611C0aBA87a00Dd6b2655aa892870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"3D43ed16b178611C0aBA87a00Dd6b2655aa89287": { "balance": "500000" },
"69b24ce645c0Bbdfcc0FE4297804b82F888DE8Fe": { "balance": "500000" },
"46eE6c1779eB3421Fc355132D867804894eFB1F9": { "balance": "300000" },
"4B3Beee7E6E067080f71336fF118E76E53C63cA3": { "balance": "200000" }
}
}
可以看这个官方教程来理解更多关于私有网络的搭建: Private network