Open In App

How to Add Another Miner to Existing Private Ethereum Blockchain?

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we’re going to add a miner to an existing private Ethereum blockchain. A private Ethereum blockchain is a decentralized network of computers that are used to validate and record transactions on the Ethereum platform. These transactions are recorded on a public ledger, known as the blockchain, which is maintained by a network of miners.

Miners are responsible for verifying the validity of transactions and adding them to the blockchain. In a private Ethereum blockchain, the miners are usually selected by the network administrator and have the ability to control which transactions are added to the blockchain.

Setup and Prerequisite Software

  • Ethereum mining software: This is the software that will run on your new miner and perform the actual mining process. Some popular options include Geth, Parity, or Ethminer.
  • An Ethereum client: An Ethereum client is software that connects your miner to the Ethereum network. Some popular options include Geth, Parity, or Infura.
  • An Ethereum wallet: You will need a wallet to store the Ethereum tokens that you earn through mining. There are several different wallet options available, such as MyEtherWallet, MetaMask, or Ledger Nano.

Configure the Genesis Block

In a private Ethereum blockchain, the genesis block is the first block in the blockchain and serves as the starting point for the entire network. It contains important information, such as the network’s initial state, configuration parameters, and consensus rules.

To configure the genesis block for your private Ethereum blockchain, you will need to create a configuration file that specifies the desired parameters for your network. This file is usually written in JSON format and should include the following information:

  1. Network ID: This is a unique identifier for your Ethereum network. It is used to distinguish your network from other Ethereum networks and prevent transactions from being broadcast to the wrong network.
  2. Genesis block: This is the initial state of your Ethereum network. It can include details such as the initial balance of the network’s smart contract, the initial allocation of tokens, and any other relevant data.
  3. Difficulty: This is a measure of how difficult it is to solve the mathematical puzzles that are used to validate transactions and add them to the blockchain. In a private Ethereum network, the difficulty can be set to a lower level to make it easier for miners to add blocks to the chain.
  4. Gas limit: This is the maximum amount of gas that can be consumed in a single block. Gas is a measure of the computational resources required to execute a transaction or smart contract.
  5. Alloc: This is a list of initial balances for specific Ethereum accounts. It can be used to allocate a certain amount of tokens to specific accounts when the network is first launched.

To create the configuration file, you can use a tool such as Ethereum Genesis Block Generator or manually write the file using a text editor. Once the configuration file is created, you can use it to initialize your private Ethereum network and create the genesis block.

Genesis Block

 

Setup First Node

Hence we’re done with the genesis block configuration file that specifies the desired parameters for your network. Now the next task will be initializing the blockchain.

Initializing the blockchain in a private Ethereum network involves creating the genesis block and setting up the network’s initial state. The genesis block is the first block in the blockchain and serves as the starting point for the entire network. It contains important information, such as the network’s initial state, configuration parameters, and consensus rules.

To initialize the blockchain, you will need to use the Ethereum client’s command line interface and specify a configuration file that contains the desired parameters for your network. This configuration file is usually written in JSON format and should include the network ID, difficulty, gas limit, and any other relevant data.

Here is an example of how to initialize the blockchain using the Geth Ethereum client:

1. Once the genesis.json file is saved, to create the first node, open the terminal window.

2. Navigate to the directory where you saved the configuration file.

3. Use the following command to initialize the blockchain:

geth –datadir blkchain101 init genesis.json

Here,
–datadir is use to specify the name of the directory where the local copy of the blockchain will be stored on the node.

This command will create a directory called blockchain101 in the project directory. This will initialize Node1.

4. Use the below command to start Node1 using Geth:

> geth –datadir <data_directory> –nodiscover –networkid 1234 console

Here,

<data_directory> is the directory where you want to store the blockchain data
<configuration_file> is the path to the configuration file.
 –nodiscover disables the peer discovery mechanism and enables manual peer addition.
–networkid 1234 is the identity of the Ethereum network, other peer nodes will also have the same network id. It can be any random integer value.

geth --datadir <data_directory> –nodiscover –networkid 1234 console command output” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth.png 1036w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-100.png 100w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-200.png 200w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-300.png 300w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-660.png 660w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-768.png 768w, https://media.geeksforgeeks.org/wp-content/uploads/20221229190418/geth-1024.png 1024w, ” sizes=”100vw” width=”1036″><figcaption> </figcaption></figure>
<p>Node1 is ready. Keep the terminal window open and running. We will open a new terminal window to setup Node2. It is important to note that the process of initializing the blockchain may vary depending on the specific Ethereum client you are using. Consult the documentation for your Ethereum client for more detailed instructions.</p>
<h3>Information about Nodes</h3>
<p><strong>1. </strong>The <strong>admin.nodeInfo</strong> command is used to display information about a node on the Ethereum blockchain. When you run this command in the Ethereum console, it will return an object with several properties that provide details about the node.</p>
<blockquote>
<p>> admin.nodeInfo</p>
</blockquote>
<div style=admin.nodeInfo command output

 

2. The personal.newAccount() command is used to create a new Ethereum account on the node. When you run this command, it will prompt you to enter a passphrase for the new account. You will need to enter the passphrase twice to confirm it. The passphrase is used to encrypt the private key for the account, so it’s important to choose a strong and unique passphrase.

> personal.newAccount()

personal.newAccount command output

 

3. The personal.listAccounts() command is used to display a list of all the accounts on the node. When you run this command, it will return an array of Ethereum addresses representing the accounts.

> personal.listAccounts()

personal.listAccounts command

 

Note: By following the above you will be able to setup the private first node and In order to create an second node you can again follow the above mentioned steps.

Setup Second Node

1. Once the first node is initialized and set up, open a new terminal window to set up Node2.

2. Navigate to the project folder that contains the genesis.json file.

3. Initialize the new node with the following command:

geth –datadir blkchain102 init genesis.json

4. This will create a new node whose data will be stored in a new directory called blkchain102. 

5. Use the below command to get Node 2 up and running with a Geth console:

geth –datadir blkchain102 –nodiscover –networkid 1234 –port 30304 console

Here,
Node1 is using the default port 30303 so port 30304 is used here for Node2.
Same networkid is used for Node2 as we want both nodes to be part of same network.
–datadir for Node2 will be blkchain102.

6. Setup the Node2 using the below command:

personal.newAccount()

Node2 is now ready. Keep both the terminals open, the Geth console (Node1) and the Geth console (Node2).

Connect the nodes

Connecting the nodes in a private Ethereum blockchain involves adding the second node’s address to the first node’s list of trusted nodes. This allows the two nodes to communicate with each other and share information about the blockchain, such as transactions and blocks.

To connect the nodes, you will need to use the Ethereum client’s command line interface on both nodes.

1. Run the below command on the terminal of both nodes:

admin.peers 

2. On the first node, you will need to enter the following command and this command will add the second node’s address to the first node’s list of trusted nodes.

> admin.addPeer(“enode://<second_node_public_key>@<second_node_ip_address>:<second_node_port>”)

Here,
Replace <second_node_public_key>, <second_node_ip_address>, and <second_node_port> with the second node’s public key, IP address, and port, respectively. 

3. On the second node, you will need to enter a similar command to add the first node’s address to the second node’s list of trusted nodes:

> admin.addPeer(“enode://<first_node_public_key>@<first_node_ip_address>:<first_node_port>”)

Here,
Replace <first_node_public_key>, <first_node_ip_address>, and <first_node_port> with the first node’s public key, IP address, and port, respectively.

Once the nodes are connected, they will be able to communicate with each other and share information about the blockchain. It is important to note that both nodes must be running and connected to the internet for this process to work.

Verify the blockchain

To verify that the nodes in a private Ethereum blockchain are connected, you can perform the following steps:

1. Check the blockchain data: You can also verify that the nodes are connected by checking the blockchain data on each node. The blockchain data should be the same on both nodes if they are connected. To check the blockchain data, use the following command on each node:

> eth.blockNumber

2. Check the mining status: If you are mining on the network, you can also verify that the nodes are connected by checking the mining status on each node. To check the mining status, use the following command on each node:

> eth.mining



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads