Block chain : Create Private network

In previous part we have discussed how to setup ethereum on machine and writing smart contract. In this part we will try to set an private block chain network. As we already aware that there are 3 types of block chain network.

  1. Testrpc
  2. Public network — main network
  3. Private network

All the three types already discussed . Lets spend some time to discuss private network. There are few terms which need to be discuss.

  1. Private network — An Ethereum network is a private network if the nodes are not connected to the main network nodes. In this context private only means reserved or isolated, rather than protected or secure.
  2. Network ID — Since connections between nodes are valid only if peers have identical protocol version and network ID, you can effectively isolate your network by setting either of these to a non default value.
  3. Genesis block — Every blockchain starts with the genesis block. When you run geth with default settings for the first time, the main net genesis block is committed to the database. For a private network, you usually want a different genesis block
{
 “config”: {
 “chainId”: 15,
 “homesteadBlock”: 0,
 “eip155Block”: 0,
 “eip158Block”: 0
 },
 “difficulty”: “0x20000”,
 “gasLimit”: “0x2fefd8”,
 “alloc”: {
 “0x81704b617cAeEd1afcf09A0bC55f81ac1b1F1a24”: { “balance”: “300000” }
 }
}

Words used in above config are self explanatory we are not going into details.

That is all you need to know before set up private block chain network.

Step 1- Initialize the initial block

Initialize you ethereum node with genesis block with given configuration file. Type the following command on you gitbash.

geth — datadir “path to custom data folder” init genesis.json

Step 2- Start multiple instances ethereum

After initialization start multiple instances of ethereum on same/different machine. Make sure you change the port number in case you are starting it to same machine.

Type the following command to start the instances

geth -rpc -rpcport “8545” -rpccorsdomain “*” -datadir “directory path ” -port “30303” -rpcapi “db,eth,net,web3” -networkid <Any integer number except 1> -rpcaddr <Machine1- IP Address> console

Make sure all the instances start with same network ID. Repeat the same process of all the instances. Make sure you start 4–5 instances of ethereum.

Step 3- Get node info

Once your all the instances are up and running get their information to make the P2P network. Type below command to get the node info. Go to you geth console and type

> admin.nodeInfo

{
 enode: “enode://3bf61bc32932f355e0bf841a3e39c642a9946d17d6aa881b87c45aef45895b444@[::]:30303”,
 id: “3bf61bc32932f355e0bf841a3e39c642a9946d17d6aa881b87c45aef45895b,
 ip: “::”,
 listenAddr: “[::]:30303”,
 name: “Geth/v1.6.5-stable-cf87713d/linux-amd64/go1.8.1”,
 ports: {
 discovery: 30303,
 listener: 30303
 },
 protocols: {
 eth: {
 difficulty: 75510914491,
 genesis: “0xdf7228dbad7190ef4e2087b0c752dd451087f9bca86e718e04fa6e6577bbb2f9”,
 head: “0x7d465d14678f9eaad113a2b8d43a3d624c0f29332c751b670643e92299e7adbc”,
 network: 1010101010
 }
 }
}

Output will look like above. Repeat this for all the nodes. We will need enode to make P2P network.

Step 4.1 — Create P2P connection using console

We will use geth console to make the connection. Go to geth console and type the below command to add the peers

>admin.addPeer(“enode://0142c2c22ff518157dd1b1b40e45152c93ed5ee5cb1@IPADRESS:30303”)

Repeat this for all the nodes. You will get the success message.

There might be some issue with firewall and timezone , please fix it accordingly.

Step 4 .2— Create P2P network using Static nodes

We can create private network white starting the ethereum instance. We can create list of static nodes that need to be added into to network when instance start.

Go to data directory folder and create a json file called static-nodes.json and push all the nodes in array format like below

[
 “enode://53633f15d1e8fa35d67540abb25572fad5867b7e1942fc2a309f74aa6a726babadf7ca0613f42eb04dbaa1b734dc05b932dcbbe1e45b51ac721700c019a2a7ab@10.35.236.140:30303”
]

Step 5 — Check the connection

Now go to you ethereum console and type the following to get the list of nodes

> admin.peers

you will get output like this

[{
 caps: [“eth/62”, “eth/63”],
 id: “3bf61bc32932f355e0bf841a3e39c642a9946d17d6aa881b87c45aef45895b”,
 name: “Geth/v1.6.5-stable-cf87713d/linux-amd64/go1.8.1”,
 network: {
 localAddress: “IP”,
 remoteAddress: “IP”
 },
 protocols: {
 eth: {
 difficulty: 75510914491,
 head: “0x7d465”,
 version: 63
 }
 }
}]

That is all to create a private block chain network. You can deploy you smart contract on any of the node and same will be distributed over all the network.

In next part we will do end to end implementation. That will include create private network , write smart contract , build , deploy and use the contract using UI.

You can download the code from git hub

Next Part