Establish a cluster of nodes on the Ethereum private chain and conduct transactions

Original address: https://blog.csdn.net/ShuSheng0007/article/details/79391105

The first part introduces how to establish an Ethereum private node and deploy HelloWorldsmart contracts. This chapter records the method of establishing multiple nodes and generating clusters.

Overview

In order to run multiple instances of Ethereum nodes on the local network (on one machine), the following must be ensured: 
1. Each instance has a separate data directory (--datadir) 
2. Each instance runs with a separate Port. (both eth and rpc) (--port and -- rpcprot) 
3. In the case of a cluster, the instances must know each other. (the networkid is consistent) 
4. The only ipc communication endpoint or disable ipc. (Only one node can have ipc enabled, other nodes are disabled)

Create two private nodes

Enter the geth.exe directory and run the following two commands to create two private nodes. For details, please refer to Blockchain (1) Development Environment Construction and Hello World Contract Display

geth --datadir "%cd%\chain1" init genesis.json
geth --datadir "%cd%\chain2" init genesis.json
  • 1
  • 2

Start the node to be clustered

Start the first node

geth --identity "shusheng007eth1" --rpc --rpccorsdomain "*"
--datadir "%cd%\chain1" --port "30303" --rpcapi "db,eth,net,web3" --rpcaddr "192.168.10.34" --rpcport "8545" --networkid 95518 console
  • 1
  • 2

start the second node

geth --identity "shusheng007eth2" --datadir "%cd%\chain2"

Create a cluster

Enter in the cmd window of the first node shusheng007eth1admin.peers to view the cluster to which it belongs, and you can see that no cluster is currently joined. 
[] 
We are going to add the second node shusheng007eth2 to the first node:  Enter the command in the cmd window to which
the node shusheng007eth2admin.nodeInfo.enode belongs to view its node information

"enode://1b4237f05270ddc54fd2bc64fa67fbe7e3c48f1390328eec323cd4d6655ef27c7672cf7
508d55510f3eac79f01acb6a0d5d531e357718473916f4e9fc3f91848@0.0.0.0:30304"
  • 1
  • 2

Enter the command in the cmd window to which the first node shusheng007eth1 belongs

admin.addPeer("enode://1b4237f05270ddc54fd2bc64fa67fbe7e3c48f1390328eec323cd4d
6655ef27c7672cf7508d55510f3eac79f01acb6a0d5d531e357718473916f4e9fc3f91848@0.0.0.
0:30304")
  • 1
  • 2
  • 3

If the return value trueindicates that the addition was successful.

Then enter to admin.peersview the cluster information

[{
    caps: ["eth/63"],
    id: "1b4237f05270ddc54fd2bc64fa67fbe7e3c48f1390328eec323cd4d6655ef27c7672cf7
508d55510f3eac79f01acb6a0d5d531e357718473916f4e9fc3f91848",
    name: "Geth/shusheng007eth2/v1.7.3-stable-4bb3c89d/windows-amd64/go1.9",
    network: {
      localAddress: "127.0.0.1:55300",
      remoteAddress: "127.0.0.1:30304"
    },
    protocols: {
      eth: {
        difficulty: 16384,
        head: "0x6e92f8b23bcdfdf34dc813cfaf1d84b71beac80530506b5d63a2df10fe23a66
0",
        version: 63
      }
    }
}, {
    caps: ["eth/63"],
    id: "9aad661fcfefe032299c42f9e6a403d8dfadc4490d7a3ad58abedf82d93ecbb01234b75
6edad3631752b30927661e2a644e73e9fa0c116dbbbc45187d640ee5c",
    name: "Geth/v1.8.1-stable-1e67410e/linux-amd64/go1.9.4",
    network: {
      localAddress: "192.168.10.34:55371",
      remoteAddress: "212.92.127.8:30303"
    },
    protocols: {
      eth: "handshake"
    }
}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

It can be seen that it has been added successfully

Enter in the cmd window to which the second node belongs to admin.peersview the results

[{
    caps: ["eth/62", "eth/63"],
    id: "ebaf9d1817e7e6ceab10f1e046ef2ffc771bd8b08c50af59943df189c9a88233de24890
6b2ae040e6388b80036b3dce0baf78c0838841ae8a6998b889b309abf",
    name: "Geth/shusheng007eth/v1.7.3-stable-4bb3c89d/windows-amd64/go1.9",
    network: {
      localAddress: "127.0.0.1:30304",
      remoteAddress: "127.0.0.1:55300"
    },
    protocols: {
      eth: {
        difficulty: 48885005,
        head: "0xc4da03de3a2ceb7c0bd2ae3845e0cce71af4eec488560a750f062f1e9499c84
4",
        version: 63
      }
    }
}]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

trade

  1. Transfer Ethereum from the account of the first node to the second node. If both nodes are newly created, you need to create a new account. Use the name to view the list of dealers. If the output indicates that there is no account, use the command to create an account. You can create an account by entering the password twice after entering the command. If there is no money in the account, you can use the command to mine and stop mining. 
    eth.accounts
    []personal.newAccount()miner.start()miner.stop()
  2. In the first node window, enter to eth.getBalance(eth.accounts[1])view the balance  of the dealer
    10456923480000000000
  3. Unlock accountpersonal.unlockAccount(eth.accounts[1],"wangben123")
  4. account to the second node

    0x8cd913aa13f1f0e885b5cd0d3909c08ff7e08765"Send 0.5 ETH

    eth.sendTransaction({from:"0x82d1889786711ac0efe8de112f32a7c66fb8b256",to:"0x8
    cd913aa13f1f0e885b5cd0d3909c08ff7e08765",value:
    web3.toWei(0.5,"ether")})
    • 1
    • 2
    • 3

    The following message appears, indicating that the transaction was submitted successfully, waiting for confirmation

    INFO [02-27|16:30:36] Submitted transaction 
    fullhash=0x6bf586
    5d83c16fdb793c0d26f8dc12eb17eed7491cab2f19d6dd2b22bc330278
    recipient=0x8cD913aA1 3f1F0e885b5cD0d3909c08FF7e08765
  • 1
  • 2
  • 3
  • 4
查看即将确认的交易` eth.pendingTransactions` 
  • 1
  • 2
    [{
        blockHash: null,
        blockNumber: null,
        from: "0x82d1889786711ac0efe8de112f32a7c66fb8b256",
        gas: 90000,
        gasPrice: 36000000000,
        hash: "0x6bf5865d83c16fdb793c0d26f8dc12eb17eed7491cab2f19d6dd2b22bc330278",
        input: "0x",
        nonce: 5,
        r: "0xde67435a15f794b29660f90c9fc0ca0cb2d2080e5f1c267c9fb42890163bd688",
        s: "0xb0f9c2e6ab0ec029e51333f374786fd6e6082c4843ef868d4b427282f478a98",
        to: "0x8cd913aa13f1f0e885b5cd0d3909c08ff7e08765",
        transactionIndex: 0,
        v: "0x1b",
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  1. Mining confirmationminer.start(3); admin.sleepBlocks(1); iner.stop();
  2. Check the account balance eth.getBalance(eth.accounts[0]) 
    result in the second node window: 0.5500000000000000000 Ethereum  was successfully received , but the unit iswei

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326110527&siteId=291194637