How to set up a Private Ethereum Blockchain

Nethmi Pathirana
The Startup
Published in
7 min readOct 16, 2019

In this tutorial, I will give you a complete step by step guide on how to build a private Ethereum blockchain in a few minutes.

First, let’s have a look at what is Blockchain and Ethereum.

What is Blockchain?

Simply the Blockchain is a distributed database that can be securely and iteratively updated. Recently there have been developed a lot of applications employing blockchain, basically to facilitate secure and private final transactions. Blockchain acts as a universal spreadsheet that removes a central point of system failure and has been proposed as a way to secure online transactions.

The main features of blockchains are:

  • Distributed — Since blockchain is a distributed database, there is no centralized copy. It makes the system robust against hacks.
  • Secure — The distributed database is encrypted by private and public keys.
  • Public — There is no central authority to validate or record transactions in a blockchain which leads to a more transparent system with security.

What is Ethereum?

Ethereum is an open-source, public, blockchain-based distributed computing platform and operating system featuring smart contract functionality[1]. “Ether” is the crypto-fuel of the Ethereum network which is used to do transactions. The most sophisticated feature of Ethereum is Smart Contract which is a program that facilitates the exchange of money, content, property, shares or anything of value. The smart contract becomes a self-operating computer program that automatically executes when specific conditions are met. They run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference[2].

Ethereum blockchain network is simply a set of nodes connected to every other node to create a network. Each node runs a copy of the entire blockchain and competes to validate and create next block. Whenever a new block is added, the blockchain updates and is propagated to the entire network. Therefore, to become a node in the Ethereum network, your computer will have to download and update a copy of the entire blockchain.

You can build a “private” Ethereum network rather than the public network which can be used to make transactions and build smart contracts without needing real Ether. A private network is the best way to learn the concepts of blockchain without the need for real currency.

So, let’s start!

Step 1 — Environment Setup

First, you need to install Geth which is a command-line interface (CLI) tool that communicates with the Ethereum network and acts as the link between your computer and the rest of the Ethereum nodes.

Go to the Go Ethereum site. Download and install the binary for your operating system.

Step 2 — Configure the Genesis file

To run a private network, you need to provide geth with some basic information required to create the initial block. Every blockchain starts with a Genesis Block, the very first block in the chain. To create our private blockchain, we will create a genesis block with a custom genesis file. Then, ask Geth to use that genesis file to create our own genesis block.

Custom Genesis file

{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {},
"config": {"chainId": 987, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0}
}

Explanation on the config file;

  • chainId: A unique identifier of the new private blockchain
  • homesteadBlock: Homestead is the first production release of Ethereum and since the developers are already using this version the value of this parameter can be left as ‘0’.
  • eip155Block/eip158Block: EIP stands for “Ethereum Improvement Proposals”, these were implemented to release Homestead. In a private blockchain development, hard forks aren’t needed, hence the parameter value should be left as ‘0’.
  • difficulty: Controls the complexity of the mining puzzle and a lower value enables quicker mining.
  • gasLimit: Establishes an upper limit for executing smart contracts.
  • alloc: Allows allocation of Ether to a specific address.

Paste the above code in the genesis.json file and save it in a folder on your computer.

Next, open the terminal and run the following code snippet. This will instruct Geth to use genesis.json file to be the first block of your custom blockchain. We also specify a data directory where our private chain data will be stored. Choose a location on your computer (separate folder from the public Ethereum chain folder, if you have one) and Geth will create the data directory for you.

geth --rpc --rpcport "8085" --datadir /path_to_your_data_directory/TestChain init /path_to_folder/genesis.json

Once you run this snippet, you can see the following terminal result.

Step 3 — Create a Private Network

Next, we will start our private network to mine new blocks to our private chain. Run the following code snippet on your terminal. The parameter “networkid” is a unique id to identify our private blockchain network. The main Ethereum network has “1” as the networkid. Give any random number except “1” to create our unique network.

geth --rpc --rpcport "8085" --datadir /path_to_your_data_directory/TestChain --networkid 123 --nodiscover

Now your private network will go live. After running the above snippet, the terminal gives the following result.

Boom!! You successfully created the private blockchain network. Now you have to create accounts to do transactions on the network.

Step 4 — Create Accounts

Open another terminal window and run the following snippet to open Geth console.

geth attach /path_to_your_data_directory/TestChain/geth.ipc

You will get a JavaScript console like the following.

Now create a new account by typing personal.newAccount() and give a password. Simple!

Please remember the account address and password because it will be needed very often.

Check the balance of the account by typing eth.getBalance(“ENTER_ACCOUNT_ADDRESS_HERE”)

Here’s what I got

There are two ways to get ether to your account. You can mine blocks and get rewarded with ether or someone sends you some ether to your account. Since you are alone in your private network at this point, the only way is to mine some blocks and get rewarded.

Mining in the main Ethereum network is hard and need more computational power. However, mining blocks in a private network is easy since we specified a low difficulty level in the genesis.json file. To mine ether to your account, simply type miner.start() in the Geth console. After 10–15 seconds, check the balance again. To stop mining, run miner.stop()

And Boom!! Lots of ether in your account. But remember that, this is fake ether and you cannot use this ether to make transactions on the main Ethereum network. However, you can use this ether to test functions of the blockchain such as transfers, deploying smart contracts, etc.

As the next step, we will add more nodes to our private network. Hold on!

Step 5 — Adding more Nodes/Peers

To add a new node, run the commands in Step 2 to Step 4 in a new terminal.

Important Note:

  1. You must use the same genesis.json file to initiate the node.
  2. You must use a different data directory folder to the new node.
  3. Make sure to use the same networkid for the new node.
  4. You need to give a port number because the default port is already used by the first node.
1. geth --rpc --rpcport "8085" --datadir /path_to_your_data_directory/TestChain2 init /path_to_folder/genesis.json2. geth --rpc --rpcport "8085" --datadir /path_to_your_data_directory/TestChain2 --networkid 123 --nodiscover --port 30306

In the console of the second node, run admin.nodeInfo.enode You should get something similar to this.

Note:
[::] will be parsed as localhost (127.0.0.1). If your nodes are on a local network check each individual host machine and find your IP with ifconfig
If your peers are not on the local network, you need to know your external IP address (use a service) to construct the enode URL.

Copy this value and in the console of the first node run,

admin.addPeer("enode://f2da64f49c30a0038bba3391f40805d531510c473ec2bcc7c201631ba003c6f16fa09e03308e48f87d21c0fed1e4e0bc53428047f6dcf34da344d3f5bb69373b@[::]:30306?discport=0")

This will return true if successful, but that doesn’t mean the node was added successfully. To confirm run admin.peers and you should see the details of the node you just added.

If you start mining on the first node by running miner.start(), you will see the block number increase on the second node.

Congratulations! You just built your first private Ethereum blockchain, mined some ether and added more peers to the network!

Thanks for following this tutorial and hope you found this useful.

You have something to add? I’d love to hear your thoughts.

References

[1] https://en.wikipedia.org/wiki/Ethereum

[2] https://www.ethereum.org

Thanks for reading!! You have something to add? I’d love to hear your thoughts :)

--

--

Nethmi Pathirana
The Startup

Software Engineer @ Sysco Labs | Computer Science & Engineering Graduate @ University of Moratuwa