![]() |
VOOZH | about |
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.
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:
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.
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.
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.
1. The admin.nodeInfo 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.
> admin.nodeInfo
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()
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()
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.
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).
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.
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