Skip to content

Latest commit

 

History

History
111 lines (87 loc) · 3.88 KB

5 起多节点树状区块链.md

File metadata and controls

111 lines (87 loc) · 3.88 KB

00 准备工作

如下建立目录:

| LaunchMultiNodes
|-- Node1
|-- Node2

01 配置Node1

Node1文件夹中建立genesis.json文件,写入:

{
    "config": {
        "chainId": 7,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0
    },
    "alloc": {},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "difficulty": "0x20000",
    "extraData": "",
    "gasLimit": "0xffffffff",
    "nonce": "0x0000000000000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp": "0x00"
}

在VS Code中启动终端,切换路径到Node1文件夹下,然后执行:

geth1 --datadir ./gethdata --networkid 91036 --port 30303 --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --ws --wsaddr "localhost" --wsport "8546" --wsorigins "*" --nodiscover --allow-insecure-unlock --dev.period 1 --syncmode "full" init ./genesis.json && geth1 --datadir ./gethdata --networkid 91036 --port 30303 --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --ws --wsaddr "localhost" --wsport "8546" --wsorigins "*" --nodiscover --allow-insecure-unlock --dev.period 1 --syncmode "full" console

这样,第一个节点就被建立并启动了。

在打开的控制台中,输入:

for (i = 0; i < 4; i++) { personal.newAccount("123456")  }

再逐一解锁账户:

for (i = 0; i < 4; i++) { personal.unlockAccount(eth.accounts[i],"123456",15000)  }

最后,记录第一个节点的信息:

admin.nodeInfo.enode
// enode://51825a4e0dc506e716e1d0b7aa3b27eab62be8154e0071ee107607f517f23ef345668241c15688a7a3b54a2a498ca2bb3a1fb71a628da0694219fe78946bcc60@127.0.0.1:30303?discport=0

02 配置Node2

Node2文件夹下,创建genesis.json文件,在其中,写入和Node1/genesis.json一样的内容即可。

打开终端,切换到Node2文件夹下,执行:

geth1 --datadir ./gethdata --networkid 91036 --port 30304 --rpc --rpcaddr 127.0.0.1 --rpcport 8547 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --ws --wsaddr "localhost" --wsport "8548" --wsorigins "*" --nodiscover --allow-insecure-unlock --dev.period 1 --syncmode "full" init ./genesis.json && geth1 --datadir ./gethdata --networkid 91036 --port 30304 --rpc --rpcaddr 127.0.0.1 --rpcport 8547 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain "*" --ws --wsaddr "localhost" --wsport "8548" --wsorigins "*" --nodiscover --allow-insecure-unlock --dev.period 1 --syncmode "full" console

准备好在01步骤中获得的节点1信息,在打开的控制台中输入:

admin.addPeer("enode://51825a4e0dc506e716e1d0b7aa3b27eab62be8154e0071ee107607f517f23ef345668241c15688a7a3b54a2a498ca2bb3a1fb71a628da0694219fe78946bcc60@127.0.0.1:30303?discport=0")

验证链接:

admin.peers
/* [{
    caps: ["eth/63", "eth/64", "eth/65"],
    enode: "enode://51825a4e0dc506e716e1d0b7aa3b27eab62be8154e0071ee107607f517f23ef345668241c15688a7a3b54a2a498ca2bb3a1fb71a628da0694219fe78946bcc60@127.0.0.1:30303?discport=0",
    id: "35c63c48b6ffee787d7997032a05f098b741ab8f4758fcfb63d23cc59787e712",
    name: "Geth/v1.9.12-stable-27356db6/linux-amd64/go1.13.9",
    network: {
      inbound: false,
      localAddress: "127.0.0.1:45318",
      remoteAddress: "127.0.0.1:30303",
      static: true,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x87e12be375f03a318eda812e46d4fdf0c94cc5a030e4ccfbf04ef1b1651af19a",
        version: 65
      }
    }
}]
*/

连接建立。如果在节点1开始挖矿,节点2也会相应地输出信息。

实验完成。