Blockchain Network(Go-Ethereum)-Part-1

Martin Parmar
9 min readSep 2, 2021

--

Blockchain is decentralized distributed technology. It records the transactions into the distributed ledger once the transactions have been validated and verified by the network of peers. The transaction validation usually happens on consensus mechanisms [common agreement by the participating peers]. There are different consensus protocols such as POW(Proof of Work), POA(Proof of Authority), POS(Proof of Stack), etc.

Ethereum version 1.0 is on POW and the next upcoming version is based on POS. POW consensus is achieved after the miner solved the cryptographical puzzle such as finding the correct nonce for a particular block. To find a correct nonce, the miner needs to invest a huge amount of computation power. It is required high-end processors such as GPU(Graphics Processing Units) or ASIC(Application Specific Integrated Circuit) which can generate a terabyte of hash rate.

In this tutorial, we will see how to set up an Ethereum-Blockchain network using POW. We are going to create three nodes in one machine using the Go-Ethereum client. First, install the latest recommended version of Geth from here. After installing, set the path of geth up to bin directory and add into environment variable.

Check the running geth in the command window by typing geth command. You will see some synchronization on your screen. Stop the geth by pressing Ctrl+D.

Running node using geth command

Follow the step to create a multi-node (here three nodes)running as a full Ethereum node.

Step-1: Create Genesis file

Create a Genesis.json file that contains configurations for the first origin block. The other blocks will be added chronologically.

{
“config”: {
“chainId”: 416,
“homesteadBlock”: 0,
“eip150Block”: 0,
“eip155Block”: 0,
“eip158Block”: 0
},

“alloc” : {},
“coinbase” : “0x0000000000000000000000000000000000000000”,
“difficulty” : “0x100000”,
“extraData” : “”,
“gasLimit” : “0x1000000”,
“nonce” : “0x0000000000000042”,
“mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”
}

config: It contains a basic setting for the private network.

chainId: It sets the unique identity of private blockchain. It is introduced in EIP-155 to prevent replay attacks. The chainid is required to sign the transaction and produce a different than signed hash.

homesteadBlock: The first version of Ethereum, called the Frontier release, was essentially a beta release that allowed developers to learn, experiment, and begin building Ethereum decentralized apps and tools. HomesteadBlock is the second major version of the production release.

eip155Block/eip158Block: Homestead version was released with a few backward-incompatible protocol changes, and therefore requires a hard fork. These protocol changes/improvements are proposed through a process Ethereum Improvement Proposals (EIPs). Our chain however won’t be hard-forking for these changes, so leave as 0

difficulty: It controls the rate of the creation of the blocks. If the difficulty is low then the block creation will be higher or the rate of block creation is low in case of high difficulty. It also regulates the miner that how long it will take time to add a new block of the transaction into Blockchain.

gasLimit: It refers to the maximum amount of energy that you’re willing to spend on a particular transaction. To execute the transaction or smart contract faster, you need to pay higher gas fees.

nonce: It is the number used only once. This nonce as a target number is required to achieve by the miner to add block into the blockchain. This nonce related with the difficulty.

timestamp: It is just a time of block creation in milliseconds.

parentHash: It is the previous hash of the block. Each block has a previous block of hash in the blockchain.

mixhash: It is 256 bits of hash which is combined with a 64-bit nonce. It is already given into block so no one can generate any false hash using a nonce.

coinbase: It is the first account address where all the mining rewards will be deposited. You can also change this coinbase address to another one.

alloc: It includes some prefilled accounts and ethers before creating and running the node.

Step-2: Node creation

Create a new directory and run below command inside the directory. Make sure that genesis.json file is present in directory.

geth — datadir node1 init Genesis.json

— datadir: It is a directory for the blockchain node. It holds the ledger and key information. The keys such as private and public will be created after the first account creation. Here node1 directory is created and it holds two directories one geth (store transaction information)and the other keystore (store keys). The keystore directory is empty initially.

First node created

Step-3: Run the node with following parameters.

geth — datadir node1/ — syncmode “full” — port 30304 — http — http.addr “localhost” — http.port 8545 — http.api “personal,eth,net,web3,txpool,miner” — networkid 99 — identity node1 console

— syncmode: The node synchronization can be full of light. In full node synch, it downloads all the blocks information from the inception of the chain whereas the light node only downloads the block receipt. To run the node as miner, it is required to run as a full ethereum node.

— port: It is the port number of ethereum node on your machine. The default port will be assigned 30303 in case of an absent port number. If you are running two nodes in one machine then the port number must be different.

— http: It indicated that this node can participate with another node from outside.

— http.addr: It can be a localhost accessible in one machine or machine’s IP address that is accessible in a network of the organization. If you want to make it global then write 0.0.0.0 for the public.

— http.port: This node can be accessed using the HTTP address and port. This is very useful when you want to deploy your smart contract on your private blockchain network and perform the transaction using some wallet like metamask or ethereum mist then you need to specify the RPC address and port.

— http.api: The list of API is available to interact with the blockchain network. you can refer to the list of API here

— networkid: It is the unique network identity of your blockchain network. This becomes helpful when you want to add some node as a peer into this specific network. The network id also defines the current size of your blockchain network.

— identity: This defines the name of your blockchain node.

Running node-1

Step-4: Create second node node-2. Repeat step 2.

geth — datadir node2 init Genesis.json

Step-5: Repeat step 3 with minor modifications

geth — datadir node2/ — syncmode “full” — port 30305 — http — http.addr “localhost” — http.port 8546 — http.api “personal,eth,net,web3,txpool,miner” — networkid 99 — identity node2 console

Note:

  1. Define a new directory as node2.
  2. Change the port number as 30305 as 30304 is already used by node-1
  3. Change the http port from 8545 to 8546.
  4. Keep the network id same in second node.

After executing, you might be faced one error stating that “Fatal: Error starting protocol stack: Access is denied.”

Solution:

geth — datadir node2/ — syncmode “full” — port 30305 — http — http.addr “localhost” — http.port 8546 — http.api “personal,eth,net,web3,txpool,miner” — networkid 99 — identity node2 console — ipcdisable

Step-6: Now, create third node using above steps.

Now, you have three running node in your machine.

Step-8: Create a new account in each node using below command.

personal.newAccount()

Enter password and confirmed password. It will create private and key and public key as hexadecimal value.

“0x99ee6280a412715bfebba316e51d4bcd637b1b61” // account-id

Your public key will be your account id and private key will be stored at keystore directory.

Remember: Keep your private key secure and unrevealed to anyone.

Step-9: Start the miner in any node or all node. Here, we start in node-1.

miner.start(1) // 1 indicated one thread

Run miner for a while or you may continue running it. To stop miner type

miner.stop

Step-10: Check the balance in node-1 as coinbase account by

eth.getBalance(eth.accounts[0]) // Return test ether (ETH), not real one.

Step-11: Create blockchain network by adding node-2 and node-3 as a peer into node-1. First check the peers list in blockchain network.

i. admin.peers // this will return a blank array like []

ii. Run the admin.nodeInfo command on node-2 and node-3 respectively.

iii. Copy the enodeid from node-2. Type command directly to get enodeid [admin.nodeInfo.enode]

“enode://36ad93b862f4984838785708b8c767f181a0b6566ba8728fb0ff942b9a997d796cb9acec0329a065ed2daa1154c7f08071dfce699293bd9a0c978b1b195fa0ec@localhost:30305”

iii. Add enode of node2 into node1 as below:

admin.addPeer("enode://36ad93b862f4984838785708b8c767f181a0b6566ba8728fb0ff942b9a997d796cb9acec0329a065ed2daa1154c7f08071dfce699293bd9a0c978b1b195fa0ec@localhost:30305")

iv. Run the admin.peers either in node1 or in node2 to check the other peers details. Here, I am checking inside node1.

v. Repeat and add node3 as peer into node1 using above reference.

Adding node2 as peer into node1

Observations:

In the node1 console, you can see details of node2 as a peer. Check all details such as port number and name of the node. Try to fire command [admin.peers] in node2 console, you will see the details of node1. Check the local address and remote address as well.

Now, you have your private Blockchain network which has three peer nodes.

Step12: Perform the transaction from node1 to node2 or node3. Make sure that node1 has sufficient ETH to transfer and account address of node2.

eth.sendTransaction({from:eth.accounts[0], to:”0x7e9a1febe67cd9067d5096dd2723cc197e8bdf4f”, value: web3.toWei(1, “ether”)}) // Transfer the 1 ETH from node1 to node2

As all three nodes are added as peers, any transaction will get reflected into all the distributed ledger of three nodes.

Remember following things:

  1. You can stop any running node by pressing Ctrl+D.
  2. If you want to run it again then just repeat Step-4 with all parameters. Do not create it again using geth init.
  3. If any issue then you can delete the entire directory and create it again using Step-3.
  4. Peer discover can be done either manually or automatically using bootnode. Here, we have added peers manually. See the next, for automatic peer discovery.
  5. You can also run a node on different machines. Just add IP address instead of localhost. Also, add IP address in enode to add as peers. See next section.

Running Node on different machine using Bootnode [Peer Discovery]

Set-up bootnode: Automatic peer discovery:

  1. Stop all previously running nodes.
  2. Open another terminal and go to the main directory.
  3. Type: bootnode -genkey bootnode.key
  4. Copy node key from bootnode.key
  5. Get enode from bootnode using node key from above step by command
  6. bootnode -nodekeyhex 4b843f5dc7775d84923e6fce7557a0a6c8350eb67a0c27d9c79a5a462ec48de1 — writeaddress
  7. 4. Get Bootnode enode [71ed92687832d2c7d9b2a4134fe145eeb31a2afeaad2eaa193f498e442fcdceffcf2610c29088b7cf9f2765e6cc4054628f696c80ce0a2d19819918fb17aa5aa]
  8. 5. Run the bootnode by
  9. bootnode -nodekey bootnode.key — verbosity 9 — addr IP_address:30303
Running bootnode

Run node1 and node2 with the enode of bootnode so that the bootnode can discover it when node1 and node2 turn up.

geth — datadir node1/ — syncmode “full” — port 30304 — http — http.addr “IP_address” — http.port 8545 — http.api “personal,eth,net,web3,txpool,miner” — bootnodes “enode://71ed92687832d2c7d9b2a4134fe145eeb31a2afeaad2eaa193f498e442fcdceffcf2610c29088b7cf9f2765e6cc4054628f696c80ce0a2d19819918fb17aa5aa@IP_address:0?discport=30303” — networkid 99 — identity node1 console

geth — datadir node2/ — syncmode “full” — port 30305 — http — http.addr “IP_address” — http.port 8546 — http.api “personal,eth,net,web3,txpool,miner” — bootnodes “enode://71ed92687832d2c7d9b2a4134fe145eeb31a2afeaad2eaa193f498e442fcdceffcf2610c29088b7cf9f2765e6cc4054628f696c80ce0a2d19819918fb17aa5aa@IP_address:0?discport=30303” — networkid 99 — identity node2 console

After running both the node, go to bootnode and see that the bootnode discovers the node1 and node2. Now, you do not need to add it manually.

Peer discovery using bootnode

You can now add boonode enode in any created node on any machine to monitor running peers.
In nutshell, the article presents how to create a private blockchain network using geth (go-ethereum) client, adding node as peer into blockchain network using manual and using bootnode. Using this article, you can run multinode on the same machine or different machines.

References:

  1. https://geth.ethereum.org/docs/interface/private-network
  2. https://geth.ethereum.org/docs/getting-started/private-net
  3. https://geth.ethereum.org/downloads/
  4. https://medium.com/coinmonks/ethereum-setting-up-a-private-blockchain-67bbb96cf4f1

--

--

Martin Parmar

Working as an Assistant Professor. Research in Information Technology. Passionate about Blockchain Technology development using Ethereum, Solidity, WEB3 JS