DEV Community

Cover image for Create your own private blockchain using Ethereum
Jefferson Xavier
Jefferson Xavier

Posted on

Create your own private blockchain using Ethereum

In this post we will build a Ethereum Private Blockchain with multiple nodes using Geth.

The Ethereum private blockchain is a blockchain like ethereum, but the network used is not the Ethereum Main. When you use a private network all blockchain is totally apart from the Ethereum blockchain, it's possible change the configurations like mining difficult, access and other aspects.

At the end you will have your own Private Blockain executing in EVM (Ethereum Virtual Machine) with all the Ethereum features like transactions, smart contracts and others available in this network.

Get Start

Follow the next steps to build your Private Blockchain.

  • Install Geth
  • Define Genesis Block
  • Start the first blockchain node
  • Start the second blockchain node
  • Create the peer-to-peer connection
  • Mining Blocks and Create Transactions

Installing Geth (Go Ethereum)

Geth is a CLI with some resources to connect you to Ethereum network. It will be used to start our private network in local environment.

To install Geth in Ubuntu/Debian follow the following steps:

sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
Enter fullscreen mode Exit fullscreen mode

You can see installation to other platforms here.

Check if geth was installed successfully with the following command.

geth -h

This shows all available commands using geth.

The Genesis Block

All blockchains start with the genesis block. This block define the initial configuration to a blockchain. The configuration to genesis block is defined in genesis.json file.

So, let's create a folder to start the private network and create the genesis.json file.

mkdir my-blockchain
cd my-blockchain
touch genesis.json
Enter fullscreen mode Exit fullscreen mode

Add the following content to genesis.json file:

{
  "config": {
    "chainId": 1234,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "difficulty": "4",
  "gasLimit": "8000000",
  "alloc": {}
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • chainId: An integer greater than zero that defines a unique id for your private network. The main network has ID 1. If you supply your own custom network ID which is different than the main network, your nodes will not connect to other nodes and form a private network.. You can find a community-run registry of Ethereum networks at https://chainid.network.
  • homesteadBlock: Homestead is the first official stable version of the Ethereum protocol. If you plan to use this release, the attribute should be set to 0.
  • difficulty: Determines the difficulty of generating blocks.
  • gasLimit: Indicates the current network-wide gas consumption limit per unit. Gas is the fuel that is used to pay transaction fees on the Ethereum network.
  • alloc: Is used to start some accounts with positive balance in the network. At this case we not defined any account.
  • The other attributes refer to the protocols and attributes that will be used on the network, we set the default values for the ethereum network.

Start Database (The Bootnode)

The bootnode is the first node created when the blockchain is started. To start the database to first node execute:

geth init --datadir node1 genesis.json

The folder node1 will be created with the database to bootnode.

The terminal result must be something like this:
Database Creation Result

The directory result should be something like this:
Database Directory

Start Node

geth --datadir node1 --networkid 1234 --http --allow-insecure-unlock --nodiscover

This command will be start the node in private network with id 1234. The flag --http is used to enable Web App Access, we will use this in next post to connect Metamask with that private network. The flag --allow-insecure-unlock is used to permit execute transfers without a web application, we will use this in tests to this network. The flag --nodiscover is used to prevent node from trying to connect to others automatically.

The terminal result must be something like this:
Bootnode in execution

Done!! The blockchain in the private network is running. Let's go do some tests to verify this. Open other terminal window in the same folder my-blockchain and execute the following command:

geth attach node1/geth.ipc

This open a interactive javascript terminal to execute some tasks to this node. The terminal result should be something like this:

Javascript Terminal

To see node informations execute the following command in this javascript terminal opened:

admin.nodeInfo

This command show all informations about this node, and you can verify the node is active. Keep this window open for some more tests ahead.

Node Information

Adding member peers

Let's start the second node to our blockchain. To do this execute the following commands:

geth init --datadir node2 genesis.json
geth --datadir node2 --networkid 1234 --port 30304
Enter fullscreen mode Exit fullscreen mode

The commands are very similar to the previous ones with some minor differences:

  • The folder to database in this case is node2.
  • The flags --http and --allow-insecure-unlock are not necessary.
  • The flag --port it's necessary to differentiate this node from the previous one. The first node is running on default port 30303.

Open a new javascript terminal to second node created and verify the informations executing:

geth attach node2/geth.ipc
admin.nodeInfo
Enter fullscreen mode Exit fullscreen mode

The result should be something like this:
Node2 Informations

Connecting Peers

Now, let's create the peer-to-peer connection between the two nodes in blockchain.

Copy the enode from the nodeInfo of the second node, then execute the following command in javascript terminal of the first node:

admin.addPeer("enode://431fa50a676b35dd750a68656cf8a822edb2c083ddf2359b6c246216dfbef0f1dad517b53b9ba0a35f4d2f1d44274ae4fc4b2bdf69e774af892b097d082eff1c@127.0.0.1:30304?discport=0")
Enter fullscreen mode Exit fullscreen mode

And to see all peers connected execute to both nodes the following command:

admin.peers
Enter fullscreen mode Exit fullscreen mode

Any node must be have one peer connected.

Note the 30304 port referencing the second node in network. The result should be something like this:

Peers Result

Done!! Your blockchain is created with multiple nodes runing and connected. Let's create accounts and start mining to see data updated in both nodes.

Mining

To mine blocks it's necessary have a base account. Let's do this. In javascript terminal of the first node execute the command to create a new account and define the password required to this account, the public address from created account should be presented.

personal.newAccount()
Enter fullscreen mode Exit fullscreen mode

Node1 Account

You can see account balance executing:

eth.getBalance("0xbd3156b239e2bb8d073406e67eba59a651be18f0")
Enter fullscreen mode Exit fullscreen mode

Now, you can start the mining in the first node. Before that run the command to see the current height of the blocks, must be 0, because no blocks have been mined yet:

eth.blockNumber
Enter fullscreen mode Exit fullscreen mode

Start the mining, and stop after some seconds to see updated block.

miner.start() // Start Mining
miner.stop() // Stop Mining
Enter fullscreen mode Exit fullscreen mode

Now Verify again the blockNumber and balance of account created. Some blocks must have been created and values must have been earned as a reward.

Mining Results

You can verify the blockNumber also in javascript terminal of the second node after some seconds, the value must be the same presented in the first node. This means that in fact the nodes are connected and are updated.

Blockchain Structure

Image description

Next Steps

You can try execute other commands to more specif tests in both nodes. See all Geth CLI options here. Explore the Geth Documentation.

Go to the next post to continue and MetaMask to your blockchain private network and execute some transactions.

Reference

Geth Documentation
Ethereum

Top comments (8)

Collapse
 
yongchanghe profile image
Yongchang He

Excellent tutorial! And BTW would you please let me know how to build the network including two nodes that run on two computers connected with wifi or something? Thank you for the reply in advance!

Collapse
 
jeffersonxavier profile image
Jefferson Xavier • Edited

Hello, thanks for your comment. The proccess is the same, it's just necessary change the IP Address at the admin.addPeer step. So, change the part 127.0.0.1:30304 to something like NEW_IP:PORT.

Collapse
 
yongchanghe profile image
Yongchang He

Thank you for letting me know this!

Collapse
 
trivonhan profile image
Huỳnh Minh Trí

When I run node2 on the same computer, I've ran into error "Fatal: Error starting protocol stack: listen tcp 127.0.0.1:8551: bind: address already in use". I saw that node1 is running HTTP and Websocket on port 8551. How can I solve it?

Collapse
 
jeffersonxavier profile image
Jefferson Xavier

To execute in same computer it's necessary change port to node2

Collapse
 
aakash4dev profile image
Aakash Singh Rajput

personal.newAccount() is not working sir, what may be the problem ?

Collapse
 
jeffersonxavier profile image
Jefferson Xavier

Hello, the Ethereum changed some params and APIs. Now you can create new accounts using clef. See the new documentation to get more details: geth.ethereum.org/docs/getting-sta....

Collapse
 
trimarchi profile image
Matteo

Hello, excellent tutorial! Now ethash ideprecated, how did the configuration changes using clique? (genesis.json and geth command to start nodes) Thank you in advance!