Build a Private Ethereum Node

private blockchain node in ethereum,

As discussed in my previous article, the geth is an official Ethereum client that can allow connecting with the main ethereum network, testing network or a custom private network.

In this article, I will run the geth to show how it can help a developer to run a private network within the personal computer.  I will use Ubuntu OS for the private node development, and by the end of this article, you will able to run your private ethereum node.

Steps to create a private node

1- Create The Directory 

Open the terminal and create a directory. In my case, I have created

 /farhan_tutorial/my_ethereum_private_node

blockchain geth

2) Creating Genesis Block

For any blockchain instance, it is essential to have a genesis block that will contain the initial configuration. We can write the Genesis block manually, but we will use the tool called Puppeth which is command line wizard.

It will ask some questions as:

Specify the network name in which you can type the network name as you want. The second option It will ask to “What would you like to do,” so write “2” in the command because we want to create a new genesis file. Then it will ask the consensus engine, therefore select “1” since we are interested in the proof of work consensus type. Ignore the pre-funded question since we don’t want any account to have any Ether in the beginning.

The network id for your private node must not be Mainnet, test networks ids so never use 1,2,3,4,5. I will use 4222 as an Id.

blocckhain provate node

After the network Id, it will ask you again question that what would you like to do. Select number 2 which is “manage existing genesis” and then export the file into JSON.

private blockchain node

This whole process will create the JSON file with the Ethereum’s genesis block configurations including difficulty level for mining, coinbase accounts, and much other information. In my case, the file name was created just as the network name we have entered before

e.g. farhan_tutorial_private_network.json

3 – Initialize the Genesis Block with Geth Client

Since we have now the genesis block file, we can initialize this block using the Geth client. Make sure you are still on the command line within the folder we have created before. Geth client will write the state of the genesis file by using the following command

geth --datadir . init  farhan_tutorial_private_network.json

Here make sure to putdatadir parameter which will ensure that all the data of the geth for this private network will save in the same directory rather than on any other directory like mainnet or testnet directory.

It is the most critical command; if you do not put a correct file name then you will face issues. The above statement makes sure that the ChainId inside the farhan_tutorial_private_network.json will be used by the geth.

blockchain private network4 – Create User Accounts or wallets

The above command will create two folders like keystore and geth. The Keystore folder is an empty folder, and there we will create some accounts. Make sure you are still on the terminal on the same path and then write the following command

geth --datadir  . account new

This command says that please create me a new account within this directory through geth. Therefore, geth will create a new account inside the keystore folder by itself.

blockchain private network

The command prompt will ask for the password of this new account, and then it will produce a file with the address, ciphertext and private key.

Create at least three accounts which we will use for the transactions between them. By default the first account will be considered as a mining account so the mining reward will be sent to that account.

For all accounts, I have created a single password file my_acc_pass.sec

5 – Create Start Node Command File

Now we are ready to start the latest blockchain node through our geth client. To start the node, we have to provide many important parameters. So it is better if you can write the whole command in the bash script file. So just create the new file like “start_my_ethereum_node.sh”.

Now write the command as below:

> geth --networkid 4222 --mine --minethread 2 --datadir “.” --nodiscover --rpc --rpcport 8585 --port 30303 --rpccorsdomain “*” --nat “any” --rpcapi  eth,web3,personal,net --unlock 0 –password ./my_acc_pass.sec --ipcpath “~/.ethereum/geth.ipc”

The first parameter is our network-id which we have provided at the time of creating the genesis file. The –mine parameter says that we need a mining process in our node, the mine thread parameter is used for the multi-threading at the time of mining in our case we have provided two threads.

We will deactivate the discovery protocol, so it does not need to connect with any pair. The RPC and RPC port will help us to connect the geth client (node) with any external caller in our case we will use MetaMask to connect with the RPC point.

The rpccorsdomain “*” will allow the node to connects from any domain. The RPC API will allow us to activate as many APIs we want. Right now we will use eth, web3, personal and net APIs. Another critical parameter is –unlock 0 (Here 0 represent the first account we have created, it is vital to unlock this account before using this as our coin based account. The last parameter –ipcpath provide the default endpoint path for any tool.

6 – Run the first Ethereum private node

Now go to the same directory where you have created the bash and other files then execute the file on the command prompt.

blockchain private node
Congrats the node is working

Troubleshooting:

Here make sure the “initialized chain configuration” ChainId is same as your genesis block’s configuration file. If you see ChainID is 1, then it means your genesis block is not properly initialized, therefore delete the geth directory and then complete the Step 3 again. Afterward, run the bash script file ./ start_my_ethereum_node.sh in the command prompt.

If you face any issue then make sure to use the flag inside the start_my_ethereum_node.sh –verbosity 5.

7 – Check the Mining information on the empty blocks

geth ethereum

Remember, one thing that private Ethereum node will do the mining process even if no transaction activities are happening. So this way your first account which we have unlocked at the time of starting the script, it will contain the mining rewards. Remembers that account was our coin base account.

So finally we have developed the Ethereum private node now in my next article how we can communicate with this node through other clients like Javascript console and Metamask.

Leave a Reply

Your email address will not be published. Required fields are marked *