Build an Ethereum private chain multi-node environment

Original address https://blog.csdn.net/koastal/article/details/78749211

Build Node 00

Reference: http://blog.csdn.net/koastal/article/details/78737543

Build Node 01

In the experimental environment, nodes 00 and 01 run on the same server. If the nodes are on different servers, the same gensis.json needs to be used to initialize the block. The ports of different servers are not affected, and the default ports 8545 and 30303 can be used. .

Initialize the genesis block

geth --datadir /home/blockChain/data/01 init genesis.json
  • 1

start node 01

geth --networkid 14 --nodiscover --datadir / home / blockChain / data / 01 --port 61911 --rpcapi net, eth, web3, personal --rpc --rpcaddr ip_address --rpcport 8101 console


add node 00

On the console of node 00, view the enode of node 00

admin.nodeInfo.enode
"enode://a1e18dd40fbce856d8.......2eabd24e29a@[::]:30303?discport=0"
  • 1
  • 2

In the console of node 01, add node 00

admin.addPeer("enode://a1e18dd40fbce856d84b8c6872d4158ab152812a081d1608643fd8a9c7d650ad161b5ef0b0a2a94357d2d6f3a044b380445

admin.addPeer("enode://a1e18dd40fbce856d84b8c6872d4158ab152812a081d1608643fd8a9c7d650ad161b5ef0b0a2a94357d2d6f3a044b380445f9033550233f8ded232eabd24e29a@ip_address:30303")

After the connection is successful, node 01 will automatically and quickly synchronize the blocks of node 00.

View connected nodes

You can view the number of connected nodes and the list of connected nodes in the node 00 and 01 consoles

net.peerCount
admin.peers

Make transactions and deploy contracts

Both node 01 and node 00 can mine, and as long as one node is mining, the transactions of other nodes can also proceed normally. At node 01, you can query the balance of the account in node 00, and you can also transfer money between different nodes.

Use mist to connect node 00, deploy the smart contract, and let node 01 mine.

mist --rpc http://ip_address:port
  • 1

Deploy a simple smart contract

pragma solidity ^0.4.18;

contract MyContract {
    /* Constructor */
    string name;
    int num;
    function MyContract() public{
        name = "default";
        num = 1;
    }
    function setName(string v) public{
        name = v;
    }
    function getName() public view returns(string){
        return name;
    }
    function setNum(int n) public{
        num = n;
    }
    function addNum(int m) public view returns(int res){
        res = m + num;
    }
}

  • 22
  • 23

部署智能合约需要手续费,选择一个有充足余额的账户部署智能合约。 

合约部署完成之后,可以进行操作,读取合约不需要手续费,写入合约需要手续费。 

智能合约相关文档(中文翻译的不全,建议还是看英文文档): 
http://book.8btc.com/books/6/solidity-zh/_book/ 
https://solidity.readthedocs.io/en/develop/

Guess you like

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