Docker rapid construction of multi-cluster Ethernet network and deployment of intelligent contracts

Source: Internet
Author: User
Tags git clone docker hub docker compose docker run
This plan is to make the script of the private chain to container, to achieve the ability to build the block chain network quickly based on the configuration file.
Ethernet Square Intelligent Contract developers can be based on the Ethernet test network for contract testing, but if you want to carry out continuous integration and continuous testing (CI&CT), the establishment of a local controllable block chain network is very necessary, in addition to the future of the container cluster distributed deployment needs, I deliberately produced the relevant Docker image, so that beginners or want to build their own network users can not need too much understanding of the command, the case, run their own network. The ability to quickly run networks and deploy smart contracts to see the effects of smart contracts deployed has a significant impact on the beginner's ability to improve learning efficiency and improve learning dynamics.

A test environment construction instructions
The author has published the relevant Docker image on the Docker hub, the relevant Docker compose posted on the Git hub, readers can quickly build their own Ethernet block chain network according to the following description.
In order to quickly construct the multi node Cluster network environment of the Ethernet square, the author will start the parameterization of the Ethernet square, and start the Cluster service by Docker compose according to the need, and the following figure is an example to construct the block chain network.

The author intends to construct two Cluster,cluster0 and Cluster1, each of which has 4 geth nodes, each of which uses CLUSTER0 peer 00 as the 4 node on the boot Node,cluster1 as the verification node for mining , the 4 nodes of Cluster0 are connected to the network as normal nodes, all Geth nodes are generated using the same creation file, CLUSTER0 and Cluster1 are deployed in Docker containers respectively. The topology of the network deployment is shown in the figure above. Of course, according to the actual situation, the reader can not rigidly adhere to the above structure, you can deploy more cluster, each cluster can also have only 1 geth nodes, just follow the requirements of the Docker compose file can be modified.

Second, the test environment construction process
The practice of our deployment of the Docker are running on an Ubuntu Linux, this Ubuntu Linux is a virtual machine I came out of VMware, the virtual Machine network is a NAT way to share the host's external network. The main parameters are as follows:
Ubuntu 16.04 LTS RAM 4G
ip: gateway: hostname:ubu-blockchain2
Docker Network Bridge
gateway: because of the specified IP, the IP of the two cluster we are running is as follows:

In order to facilitate the reader's rapid environmental construction and application testing, I have uploaded the relevant resources needed to build the Ethereum test environment to Github,

The Docker images used are also uploaded to the Docker hub, see blockchain101/ethereum-geth:1.6.5,

2.1 First Please install Docker container and Docker-compose operating environment
Please refer to the Installation Docker container and Docker-compose operating environment.
The easiest place to use Docker-compose is to automatically pull images and build applications automatically from the Docker hub using the docker-compose.yaml that someone else has already prepared.
2.2 Readers can download Ethereum-docker resources from GitHub $ git clone copy code 2.3 Start test network directly via Docker-compose
$ CD ethereum-docker/ethereum-docker/ethereum-testnet-docker/
$ docker-compose-f docker-compose-ethereum-testnet.yaml up copy Code
Full download image, will appear Cluster0 and Cluster1 start interface, because the initial value is to do global initialization, generate each node account, generate node data file, if the designation based on Genesis file, will generate a block chain based on Genesis File block, In addition, the entire cluster will set a node as a bootnode, of course for usability, can be improved so that each cluster set a node as Bootnode, the default configuration Cluster0 four nodes are observation nodes, cluster1 Four nodes are validated mining node. Bootnode files, Genesis files, and Ethash dag files are placed in the Ethcluster_share directory as all cluster-shared files. The system also automatically creates a ethcluster directory for placing Cluster0 and Cluster1 block-chain data files.
Since it was the first time to start a mining node, need to establish DAG, this process will take a long time, in this process, we first do some configuration changes, because CLUSTER0 four nodes do not dig mine, our goal is to use the Genesis file to give these four nodes of the account has an initial ether, Give the 10,11,12,13 a ether to these four nodes, and this will require the accounts of these four nodes to be set to the Genesis file. Please follow the figure below to see the account number of the Cluster0 four nodes.

Then update the account content in the Alloc section of the Genesis file.

We need to reset the boot option for Docker compose, change the Cluster0 init_config from Y to Genesis_block_regen, restart Docker, and no more accounts for CLUSTER0. However, the block-chain Genesis blocks are rebuilt, and the cluster1 will regenerate the account number and initialize the Genesis with the new Genesis file, so that the network will confirm the changes to the Genesis file. Ensure that the four nodes of the Cluster0 have 10,11,12,13 initial ether respectively. Of course, this process can be completely automated one-time implementation, but in order to show the novice to the beginner of the features, or manual operation.

We are now waiting for the first start of the Docker Compose mining node to DAG all generated, stop the network, and restart the network based on the new Genesis restart the Block chain network.

When Cluster1 appears mining potential block log, the DAG is ready 30000 blocks, we stop the current Epoch0 docker, and restart compose Docker.
In addition, open two terminals, enter the command separately, and start the console connected to the Cluster0 Peer01 and Cluster1 Peer01
$ geth Attach #访问 cluster1 Peer01 Console
$ geth Attach #访问 cluster1 PEER01 Console Replication Code Note: If the local machine does not have a geth command, you can start the container to execute the geth command
$ docker run-i blockchain101/ethereum-geth:1.6.5 Geth attach #访问 cluster1 Peer01 Console
$ docker run-i blockchain101/ethereum-geth:1.6.5 Geth attach #访问 cluster1 peer01 console Copy code C Net.peercount and check the ether value of the main account on the Luster0 Peer01

The ether value of the account "0xc7f9a898d14f91fde9a3d37c150c6a4f9b104545" shown in the Genesis file is 11, and we also see that the account value is 11 in the console of Cluster1 Peer01. , in addition Eth.accounts[0] as a mining award for the default account also exists ether value.

Readers can check the ether values of several other accounts written in the Genesis file, or they can connect to other nodes.

Third, the compilation of intelligent contract
We'll compile the smart contract below, we'll compile a simple contract called Sample.sol, just make a value setting and query.
First, create a directory ethcontract in the current directory and create Sample.sol files in the directory
Contract Sample {
UINT public value;

function Sample (UINT v) {

function set (UINT V) {

function get () constant Returns (UINT) {
return value;
The reader of the copy code can use the Remix development tool to write and test the contract.

We use SOLC to execute contract compilation, execute command, generate contract interface Abi file and evm binary bin file
$ SOLC--abi-o ethcontract Ethcontract/sample.sol
$ SOLC--bin-o ethcontract Ethcontract/sample.sol
Copy code if the local machine has no SOLC, you can use the container to execute the SOLC command so that the Sample.abi and sample.bin two files are generated in the Ethcontract directory
$ docker Run-v ~/ethereum-docker/ethereum-docker/ethereum-testnet-docker/dockercomposefiles/ethcontract:/ Ethcontract blockchain101/ethereum-solc:0.4.11 Solc--bin--overwrite-o/ethcontract/ethcontract/sample.sol
$ docker Run-v ~/ethereum-docker/ethereum-docker/ethereum-testnet-docker/dockercomposefiles/ethcontract:/ Ethcontract blockchain101/ethereum-solc:0.4.11 solc--abi--overwrite-o Replication Code We need to modify these two files to add the corresponding variable assignment Sampleabi and Samplehex, so that when loading the contract Abi and bin, you can access the variable directly through the console.

Later we post the compiled version of this smart contract on the block chain network.

Iv. deployment and access to smart contracts
We connect to the CLUSTER0 Peer01 node to perform the release of the compiled contract above.
> Primary=eth.accounts[0]
> Balance=web3.fromwei (eth.getbalance (primary), "ether")
> Loadscript ("./ethcontract/sample.abi")
> Loadscript ("./ethcontract/sample.bin")
> Sample=eth.contract (Sampleabi)
> (10,{from:primary,data:samplehex,gas:3000000})
> Samplerecpt=eth.gettransactionreceipt (Thesample.transactionhash)
> (samplerecpt.contractaddress)
> ()
> samplecontract.set.sendTransaction (9, {from:primary, gas:3000000})
> ()

Copy Code

We open a terminal, open Cluster1 's Peer02 console, and go directly to the last terminal deployed smart contract address and set operation
> Primary=eth.accounts[0]
> Loadscript ("./ethcontract/sample.abi")
> Loadscript ("./ethcontract/sample.bin")
> Sample=eth.contract (Sampleabi)
> ("0X8087FA310B15579F7E1423AAFC199C10C91A5DFB")
> ()
> samplecontract.set.sendTransaction (6, {from:primary, gas:3000000})
> ()
Copy Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.