How to Create an Ethereum Private Network with Geth?

Introduction

An Ethereum Private Network is a fully private Blockchain. It is out-of-the-way from the core Ethereum network. It is largely generated by organizations to limit the read permissions of the Blockchain. The nodes through the right permissions would be able to contact this Blockchain. In this network, the nodes are not linked to the key network nodes. Their range is limited only to this private Blockchain.
The organizations are used Ethereum Private Network to keep private data. That would not be visible to masses outside their organization. It is similarly used for testing and investigating the Blockchain if somebody doesn’t want to use the public test networks. In this article, we will understand the creation of an Ethereum Private network with Geth step by step.

Description

Geth may be configured to run locally deprived of linking to any network on the internet after installation. A genesis block is also called the first block always present in every chain and network. The genesis block does not have any parents. We need a genesis.json file to create the first block.
A sample genesis.json file code snippet:

{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"mixhash":
"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x200",
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":
"0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0xffffffff",
"alloc": {
}
}

Steps to create a Private Network

• The genesis.json file should be delivered to Geth to initialize the private network.
• The Geth node essentials to store the blockchain data and account keys.
• This information should also be delivered to Geth while initializing the private network.
• The below geth init command makes ready the node with the genesis.json file. It initializes target data directory location to store the chain data and Keystore information:

C:\Windows\system32>geth init "C:\myeth\genesis.json" --datadir
"C:\myeth\chaindata"

Output:

How to Create an Ethereum Private Network with Geth?

• Geth can be started after a node is initialized with the genesis block.
• It uses IPC protocol by default and will be enabled.
• RPC options are essential to be if explicitly to ensure that the Geth node is reachable using RPC protocol.
• Perform the below command line for creating an environment as a Geth node:

geth --datadir "C:\myeth\chaindata" --rpc --rpcapi "eth,web3,miner,
admin,personal,net" --rpccorsdomain "*" --nodiscover --networkid 15

Output:

How to Create an Ethereum Private Network with Geth?

• The command enables RPC, modules, and APIs when executed with datadir information.
• Those are visible from this node instance when using RPC to connect.
• They are also networked of 15 denoting that it is a private network.
• The result of performing this command delivers useful insights.
• To begin with, the etherbase or coinbase is not set.
• The coinbase or etherbase account should be produced.
• This should be set before mining is started.
• It was likely to auto-start mining with this command itself.
• The information about the present database location is published on the screen.
• The output shows ChainID that is linked to a Homestead public network.
• A value of zero means it is not linked to a Homestead network.
• The output likewise comprises the encoded value.
• That is a node identifier on the network.
• They should make available this encode value to join this chain and network if more nodes want to join this network.
• The output displays that both IPC and RPC protocols are up and running and accepting requests near the end.
• The RPC endpoint is obtainable at http://127.0.0.1:8545 or http://localhost:8545
• The IPC is available at \\.\pipe\geth.ipc.
• Look at the undermentioned command line:

geth --datadir "C:\myeth\chaindata" --rpc --rpcapi
"eth,web3,miner,admin,personal,net" --rpccorsdomain "*" --
nodiscover --networkid 15

• The previous command will get the private Ethereum node up and running.
• The command runs as a service.
•  Extra commands cannot be performed over it.
• Open another command window on the same computer to achieve current running Geth nodes.
• Type the Geth attach ipc:\\.\pipe\geth.ipc command for linking using IPC protocol.
Output:

How to Create an Ethereum Private Network with Geth?

• Use the command Geth attach rpc:http://localhost:8545 to connect to a private Geth instance over the RPC endpoint.
• Use Geth attach rpc:http://127.0.0.1:8545 to attach to a locally running instance of Ethereum.
• The default RPC port is 8545 on which these endpoints are hosted.
• That can be altered using the -rpcport Geth command line option.
• The IP address may be different using the -rpcaddr option:

• Now set up the coinbase or etherbase account.
• A new account should be shaped firstly.
• Use the new account method of the personal object to create a new account.
• Provide a passphrase during creating a new account.
• That turns into a password for the account.
Output:

Private Network

• Account ID should be tagged as a coinbase or etherbase account.
• The Geth supplier has to change the coinbase address. miner object with the setEtherBase function.
• This way will change the present coinbase to the provided account.
Output:

Private network

• Run the command eth. coinbase to find the current coinbase account.

eth.coinbase

• It should give output the same account address that was recently created.

Output:

Private network

• Now mining can get started.
• Meanwhile we just have one miner.
• Totally rewards would go to this miner.
• The aforementioned coinbase account would be credited with Ethers.
• Execute the following command to start mining:
miner.start()
• We can similarly use the following command line:
miner.start(4)
Output:

Private network

• The parameter to the start method signifies the number of threads used for mining.
• This would result in mining getting started.
• The same may be watched from the original command window:
Output:

Private Network

• Use the command miner. stop() to stop Mining from the second command window.