Create your own private blockchain using Ethereum
Tags: ethereum, blockchain, geth, crypto
Published: 2022-03-17 21:04:50

In this post we will build a [Ethereum](https://ethereum.org/) Private Blockchain with multiple nodes using [Geth](https://geth.ethereum.org/).

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)](https://ethereum.org/en/developers/docs/evm/#top) 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:

```sh
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
```

You can see installation to other platforms [here](https://geth.ethereum.org/docs/install-and-build/installing-geth).

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.

```sh
mkdir my-blockchain
cd my-blockchain
touch genesis.json
```

Add the following content to _genesis.json_ file:

```json
{
  "config": {
    "chainId": 1234,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "difficulty": "4",
  "gasLimit": "8000000",
  "alloc": {}
}
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ogfpyum731hev9zeebm.png) 

The directory result should be something like this:
![Database Directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qago11alrc4ghxb7fnek.png)

## 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](https://metamask.io/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lq23v2ezfvlkcy19ul48.png) 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azesglt187xbz7jkdmr8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybskyt0dqqctv7n75zqa.png)

## Adding member peers

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

```sh
geth init --datadir node2 genesis.json
geth --datadir node2 --networkid 1234 --port 30304
```

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:

```sh
geth attach node2/geth.ipc
admin.nodeInfo
```

The result should be something like this:
![Node2 Informations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4abbpe1uyk1y4myz12v.png)

## 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:

```javascript
admin.addPeer("enode://431fa50a676b35dd750a68656cf8a822edb2c083ddf2359b6c246216dfbef0f1dad517b53b9ba0a35f4d2f1d44274ae4fc4b2bdf69e774af892b097d082eff1c@127.0.0.1:30304?discport=0")
```

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

```javascript
admin.peers
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm3zkvzzn6s5diz7w0j1.png) 

**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.

```javascript
personal.newAccount()
```

![Node1 Account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1tpp8o4hkmpyachvgfv.png)

You can see account balance executing:

```javascript
eth.getBalance("0xbd3156b239e2bb8d073406e67eba59a651be18f0")
```

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:

```javascript
eth.blockNumber
```

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

```javascript
miner.start() // Start Mining
miner.stop() // Stop Mining
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ndl3fggg6a1oey8brvq.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zebhmolhaxp1dtplpgw.png) 

## Next Steps

You can try execute other commands to more specif tests in both nodes. See all Geth CLI options [here](https://geth.ethereum.org/docs/interface/command-line-options). Explore the [Geth Documentation](https://geth.ethereum.org/docs/).

Go to the [next post](https://dev.to/jeffersonxavier/connecting-metamask-with-a-private-blockchain-2kne) to continue and MetaMask to your blockchain private network and execute some transactions.

## Reference
[Geth Documentation](https://geth.ethereum.org/docs/)
[Ethereum](https://ethereum.org/)