Setup Ethereum private Blockchain and sync them

Posted By : Himanshu sharma | 12-Dec-2016

To setup a private ethereum blockchain in ubuntu .

 

You have to install ethereum first

 

``` sudo apt-get install software-properties-common

sudo add-apt-repository -y ppa:ethereum/ethereum

sudo apt-get update

sudo apt-get install ethereum

```

 

Then create a private network , To init the initial block of chain you run this command

``` geth --datadir "firstserver" init "CustomGenesis.json" ```

 

When you run this command it will generate a genesis hash which should be similar to all private ethereum network if you want them to synchronize. If they have different genesis hash then they will not synchronize

 

Here --datadir “datadir” means the directory which will store your initial block of chain and the block will store further chain in this directory .

 

And CustomGensis.json will contain this data

 

{

   "nonce": "0xlookatmeimanonce",

   "timestamp": "0x00",

   "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",

   "extraData": "0x00",

   "gasLimit": "0x08000000",

   "difficulty": "0x0400",

   "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",

   "coinbase": "0x3333333333333333333333333333333333333333",

   "alloc": {

“”:{

 

}

   }

}

 
 

When you run this command a folder will create with name “firstserver” which contain the inital blockchain . and then you can start the private block chain.

 
 

Then start a private blockchain by command

 

``` geth --datadir firstserver --networkid 1234 --rpcport 8000 --rpcaddr 127.0.0.1 --port 30303 --rpc --rpcapi="db,eth,net,web3,personal,web3"  --maxpeers 3 --nat=any --nodiscover --rpccorsdomain "*"  console ```

 

What this command does ?

 

--datadir define the folder of blockchain

--networkid define a network on which all node should connect “important” , if you create  multiple private blockchain all should have same network id . i will discuss it in detail later.

--port define for private blockchain.

--rpcport is the port on which the your webAPi will interact means if you are using web3.js you can connect to this port and access all detail by  api .

--rpcaddr is the ip address to which private blockchain is bind , if i want to connect to other blockchain then i need this otherwise it take by default 127.0.0.1 or localhost

--rpcapi define all api which are disclose to webAPi like web3.js or ethereumj , you can add more api as you want to access more data.

--maxpeers  define how many peer of ethereum private network that private chain can connect.

--nat value       NAT port mapping mechanism , i use any.

--nodiscover  is “important” , this is use for not to discover by public ethereum network.

----rpccorsdomain "*" allow cors origin domain.

 

You can understand more tags of geth from this , I have described which i think necessary.

https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options

Now you single private blockchain has been created.

You  can create connect to 127.0.0.1:8000 by ethereum webApi like WEB3.JS and ETHEREUMJ . I have used WEB3.JS so i recommend to use it . It is javascript api .

Now you have create a private blockchain you want to create multiple blockchain and synchronize then.

Create a new private blockchain follow above same steps to create another private blockchain

``` geth --datadir "secondserver" init "CustomGenesis.json" ```

Check It should have same genesis hash as the above and CustomGenesis.json should be same.

``` geth --datadir secondserver --networkid 1234 --rpcport 8001 --rpcaddr 127.0.0.1 --port 30304 --rpc --rpcapi="db,eth,net,web3,personal,web3"  --maxpeers 3 --nat=any --nodiscover --rpccorsdomain "*"  console ```

 

Now both the blockchain are created you need to connect both of them.

Type admin.nodeInfo in both node , this will give

 

```{

 enode: "enode://38b4bfee228ebfa5d48927da2320ec4054f19d4d85e0d348ac08bff855b51b44a9f2e858e89fd3abd1e2f27d668aed3fadc4f9a7248a3d97cdc3c6568f7247e3@[::]:30303?discport=0",

 id: "38b4bfee228ebfa5d48927da2320ec4054f19d4d85e0d348ac08bff855b51b44a9f2e858e89fd3abd1e2f27d668aed3fadc4f9a7248a3d97cdc3c6568f7247e3",

 ip: "::",

 listenAddr: "[::]:30303",

 name: "Geth/v1.4.18-stable/linux/go1.5.1",

 ports: {

   discovery: 0,

   listener: 30303

 },

 protocols: {

   eth: {

     difficulty: 72633354,

     genesis: "0xdf7b6336757c45a8891ebc017a9ce1d75eb850063ceb65e2b0bab8ddaaf8987a",

     head: "0xd7236bedc7fa4f2ccef26fd81dc2f5652656bbc3766bb135e31b515433b8ba9e",

     network: 1234

   }

 }

} ```

If you run for firstserver

Create a file static-nodes.json inside firstserver and secondserver folder and from above json copy ‘enode’

"enode://38b4bfee228ebfa5d48927da2320ec4054f19d4d85e0d348ac08bff855b51b44a9f2e858e89fd3abd1e2f27d668aed3fadc4f9a7248a3d97cdc3c6568f7247e3@[::]:30303?discport=0

And change [::] to the ip , I am running in localhost can use

"enode://38b4bfee228ebfa5d48927da2320ec4054f19d4d85e0d348ac08bff855b51b44a9f2e858e89fd3abd1e2f27d668aed3fadc4f9a7248a3d97cdc3c6568f7247e3@127.0.0.1:30303?discport=0”

This enode is of firstserver you can write it in static-node.json file to peers second server like this

[

"enode://38b4bfee228ebfa5d48927da2320ec4054f19d4d85e0d348ac08bff855b51b44a9f2e858e89fd3abd1e2f27d668aed3fadc4f9a7248a3d97cdc3c6568f7247e3@127.0.0.1:30303?discport=0”,

"enode://pubkey@ip:port",

........

]

You can add many enode  to connection by separating it by comma.

And them again run secondserver and run this command “admin.peers”  , you can see the a array in which peers detail are shown ,

And same you should do for firstserver , copy secondserver enode data and paste it in firstserver’s static-node.json file .

Then run “admin.peers” , you can see the array which second server detail .

Create a account in firstServer

personal.newAccount("mypasswd") , in console you can see a hash , If this is your first account this will we your coinbase too

Run secondserver by adding “etherbase”

``` geth --datadir secondserver --networkid 1234 --rpcport 8001 --rpcaddr 127.0.0.1 --port 30304 --rpc --rpcapi="db,eth,net,web3,personal,web3"  --maxpeers 3 --nat=any --etherbase=”hashByfirstserver” --nodiscover --rpccorsdomain "*"  console ```

Type eth.coinbase in both now you can see the coinbase is same for both

Now start mining by

miner.start(1)  1 is for one thread for mining.

Now you can see both server will synchronize with eachother

Run command  eth.getBalance(eth.coinbase).toNumber(); in both server the balance of both will increase and would we same.

THANKS

About Author

Author Image
Himanshu sharma

Himanshu has marvelous coding skills. In Free time too he loves to do coding and sometimes play Cricket.

Request for Proposal

Name is required

Comment is required

Sending message..