Setting Up a Private Network In Ethereum
Crypto Guide
Live News

Keeping up with all the cryptocurrency news and updates is not an easy task, but we are up to the challenge! This way we can help our readers to keep track of changes in these fast growing currencies. Just like our site, lambo2btc.com is willing to face this challenge and on their site, they compile the features, key elements, and recent news involving bitcoin casino sites and which are the best to try!

Setting Up a Private Network In Ethereum

February 12, 2021      Anubha Jain

Do you want to be a dApp developer or just learn how Ethereum works out of curiosity for blockchains? Well, you are at the right place and right time of course. Blockchain developers are an escalating demand. The gaining popularity of Ethereum and other blockchains has created job opportunities for trained blockchain developers more than ever. It is the best time that you attain the right skills and prepare yourself for the ample opportunities out there.

In this tutorial, we will cover how to set up a private Ethereum network. You would require one or more systems with Windows 10 and you are good to begin. But first we will look at what a private network is.

Why set up a private network?

Ethereum private network is a completely private blockchain isolated from the mainnet or any test networks. Its reach is restricted to only members of this network. When you are doing development of a dApp, you can use a private network for testing. You would not have to sync up to any test network or wait for a faucet to donate you some test ethers. You can start the development right away on a private network for free. So, let’s begin.

Process

First step would be to download one of the standalone clients of Ethereum. We are using Go Ethereum for this tutorial. It is one of the three original implementations of the Ethereum protocol.

Geth: https://geth.ethereum.org/downloads/

Private Network Ethereum

Download Geth 1.9.25 for Windows (latest version at the time of this tutorial). Run the executable file and you will have the ethereum source code installed on your system.

Next you have to create a Genesis block. It is the first block of the blockchain. The other networks already have a genesis block and it is downloaded when you sync their blockchains. But here we are setting up a private chain and we have the responsibility to begin our blockchain by creating a custom genesis block file.

Create a file- makeGenesis.json and save it anywhere.

Private Network Ethereum

Contents of a genesis block:

config– setting configuration variables for ethereum.

chainId– identifies the current chain. Chain id for other ethereum networks are: mainnet-1, Testnet Ropsten- 3, Testnet Rinkeby-4, Testnet Kovan-42. Here, set chainId to some arbitrary number other than these predefined values.

homesteadBlock– set to 0, means you will be using the Homestead release of Ethereum.

eip150Block, eip155Block, eip158Block are not relevant for a private network. These are some EIPs introduced for the safety of the main network.

alloc– set of pre-filled wallet addresses to allocate ethers. It is kept empty here in a private blockchain.

coinbase– a wallet address to which all ethers (reward+transaction fees) collected from successful mining of this block goes to. It can be set to anything in the genesis block of a private blockchain.

difficulty– it is the difficulty level for finding nonce to discover this block. Higher difficulty means more calculations required to create a block. Set it to a low value to ensure blocks are mined quickly on a private blockchain.

gasLimit– it is the maximum amount of gas that can be used in each block. It signifies the maximum number of computations any block can support. Higher value effectively means more transactions in a block.

nonce– a 64-bit hash value when combined with mixHash, proves that a sufficient amount of computation has been done to create this block.

mixhash– a 256-bit hash value, when combined with nonce, proves that a sufficient amount of computation has been done to create this block. Set to 0 here because no proof of work puzzle is solved to create this block. This value is used into parentHash of the next block.

parentHash– a 256-bit hash of the parent block’s header. It points to the parent block to effectively build the chain of blocks. As this one is a genesis block, it is set to 0.

timestamp– time at this block inception. It is the output of Unix time function. Allows to verify the order of blocks within the chain.

Next, create a folder as an Ethereum data directory. It stores wallet files and blockchain data. If not specified its default location is %USERPROFILE%\AppData\Roaming\Ethereum.

Initialize genesis block command:

geth –datadir “D:\Ethereum Test Network\data” init “D:\Ethereum Test Network\makeGenesis.json”

Private Network Ethereum

This command creates a genesis block and the following files and folders in the data directory:

Private Network Ethereum

Chaindata stores the blockchain data files. lightchaindata stores data files for running a light client. Keystore stores the private keys for accounts in encrypted form.

To begin using Ethereum, you will have to create an account. This will be your Ethereum address which you can use to interact with other users on this private network.

geth –datadir “D:\Ethereum Test Network\data” account new

Provide a password to ensure safety for this account. Whenever you want to transact with other accounts, you would need this password.

Private Network Ethereum

The private key corresponding to this account is stored in keystore. You can check it in the keystore folder:

Now you have started your blockchain and created an account. Try creating other accounts as well. To interact with the blockchain, open geth console:

geth –datadir “D:\Ethereum Test Network\data” console

Use flag –nodiscover to disable the peer discovery mechanism for now.

You can try various commands in the geth console:

eth.accounts \\list accounts

Private Network Ethereum

eth.getBalance(eth.accounts[0]) // return

s the balance of the first account.

Private Network Ethereum

Right now you have an account with 0 ether balance. To obtain some funds, you can mine new blocks on this blockchain:

miner.start(1) //Run one thread of mining process

Private Network Ethereum

You would observe some blocks being mined. Check block number using eth.blockNumber

Private Network Ethereum

Now check your account balance. The newly mined ethers go to the first account address.

To send ethers to any other accounts, you would have to unlock your account:

personal.unlockAccount(eth.accounts[0])

Provide your password when asked for it. Returns true if the account is successfully unlocked.

Private Network Ethereum

To send ethers from one account to the other:

eth.sendTransaction( { from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, “ether”) })

Provide parameters like from, to and value. Any transaction in ethereum is carried out in wei (a unit of Ether, 1 ether = 1018 wei). You would have to convert ethers to wei using web3.toWei() function.

It returns a hash of the newly created transaction.

Private Network Ethereum

This transaction does not straight away goes to the blockchain. It has to be included in a block by a miner and then added to the blockchain. You would have to mine a block to validate this transaction. 

After mining, observe that a block with your newly created transaction has been added to the blockchain. Check account balance to ensure ethers have been transferred.

Private Network Ethereum

To get txn details from the hash of transaction, use:

eth.getTransaction(“0Xbb72b69d01ec37f043690e4762c3e0ed4e48969ab25783e7b8c2b895ffa2329e”)

Private Network Ethereum

Here from, to, value are details of the transaction. Nonce is a number of confirmed txns sent from this account previously. V, r and s are the details of digital signature of the account holder sending this transaction.

To add other members on this blockchain, you would have to add new nodes to our private ethereum network. You can create a new node on the same system using a different blockchain folder or you can create a node on a different computer.

Here, we are creating a node on the same system. We are using a new data folder to store the local copy of blockchain. It uses the same genesis block to point to the same blockchain:

geth –datadir “D:\Ethereum Test Network\data new 2” init “D:\Ethereum Test Network\makeGenesis.json”

Start the first node by providing its network id which is the same as the chain id specified in the genesis block:

geth –datadir “D:\Ethereum Test Network\data” –networkid 223 –nodiscover console

Open a new command prompt to start the new node: 

geth –datadir “D:\Ethereum Test Network\data node 2” –networkid 223 –port 30308 –nodiscover console

Use a different port for this node because you can’t communicate on the same port for two different nodes’ processes. Ethereum test networks by default use port 30303 for communication. For all the new nodes you add, use a different local port for communication.

Note that this new node is not synced to the current state of blockchain. So we will have to add peers to sync.

eth.blockNumber: 0

To add this new node as a peer to the first node, get node information:

admin.nodeInfo

Private Network Ethereum

enode uniquely represents each node and provides its ip and port information for connecting it with other nodes.

On the geth console of the first node, add second node as its peer:

admin.addPeer(<enode of new node>)

Private Network Ethereum

Now the two nodes have become peers and the new nodes syncs up the blockchain using the first node. You can create any number of nodes and set up Ethereum accounts. In the next tutorial, we will learn how you can develop and deploy decentralized applications on this private blockchain.

READ  Your Guide To What Is Ethereum And Its Various Applications
#Beginners guide to ethereum #DApp development #Learning ethereum #Private chain on ethereum
Anubha Jain
Anubha Jain

Anubha is a keen follower of the manifold applications of blockchain technology. She is a doctoral candidate researching privacy constraints of the blockchain and is working to enhance user privacy in cryptocurrencies. She is a strong supporter of the vision of user-owned internet and believes blockchain as the key to it.