Blockchain Installation – Private Ethereum Blockchain

0
275

How to Set Up A Private Ethereum Blockchain?

To begin the installation process, you first need to set up the environment for the private Blockchain. This will make your system a node participating over the shared network of the Blockchain. You can also opt for the cloud where you can run your Blockchain node.

Also Read: Blockchain Advantages and Disadvantages

Environment Setup
Step 1: Start with the creation of a new folder or directory. The folder will be carrying all the binary files along with the corresponding configuration files.

Step 2: Now, visit the official website of Go Ethereum (geth). From this web page, download the suitable package for your operating system. It is possible that by the time you visit this web page, you will be encountered with the updated version of the package.

Step 3: The package will be downloaded in a compressed format. Unzip the downloaded directory and copy the geth binary file to your already created directory.

This geth binary is the Ethereum runtime where you need to feed some data before you can proceed ahead. This information will be provided to the geth binary with the help of a configuration file. The later part of this tutorial will help you understand the details of the configuration file.

<script src=”https://gist.github.com/toluhi/578d682c760c1342a97fd703cfe184e8.js”></script>

Whenever you want to run a private network then providing the information to geth binary is a mandatory step. This information will help in the creation of the first block.

Now, take a closer look at the configuration file.

config: This is the main Blockchain config file and it possesses various properties which are covered below.

“config”: { “chainId”: 1, “homesteadBlock”: 0, “eip155Block”: 0, “eip158Block”: 0 }

chainID: This is an integer value which acts as the chain identifier. It plays an important role in the protection against replay attack (or playback attack). In a replay attack, the intruder maliciously repeats or delays the valid data transmission. The attacker targets the network and thus, it is a kind of network attack.

Other properties include homesteadBlock and eip155Block. You can also add these to the config command but they are often used with the main network. So, in here, we will not be discussing them in detail.

alloc: With alloc, you can allocate ETH to addresses. These addresses are the pre-fund accounts.

coinbase: It is a default primary account and can also be referred to as etherbase. You will be displayed with a warning if you somehow ignore to set this value. But for now, it is safe as you can set it afterwards.

difficulty: Mining a block comes with a difficulty level and this entity determines that. If you are setting up a private Blockchain then setting a low difficulty level is recommended. This ensures a quick mining and faster transactions.

extraData: If you miss to set this value then by default it accepts the client version. Well actually, it is used to block the extra data.

gasLimit: With this entity, you can set the maximum limit for the amount of gas to be used for each block. With the higher value, you will be able to squeeze more and more of transactions into a single block.

mixHash: You can set it to zero (0) as it is irrelevant to the private network.

parentHash: This is same as the mixHash. Set it to zero (0) as it is irrelevant to the private network.

Get Started
The geth command requires two important parameters, which are a local database (or a newly created folder to store the Blockchain data) and an initialisation file.

geth –datadir ./datadir init init.json

The output of this command will appear as shown in the following screenshot.

After this command, you will get to see a datadir folder. This folder will get created under your working directory. This datadir folder will contain all the contents as shown in the below screenshot.

This validates that your private Ethereum Blockchain is running properly. Now, we will directly proceed towards the testing part. For this testing purpose, you first need to create an account on the Blockchain.

Creation of An Account on The BlockChain
For an account creation on the Blockchain, you can acquire one of the two mentioned ways. You can either do it by using the geth commands or by using the geth console. In here, we will help you with both the methods.

Creating an Account By Using geth Command:
geth –datadir ./datadir account new

Once this command runs successfully, you will be asked to enter the password. Do not misplace this password as you will need this later. The address of this newly created account will be displayed on the console as shown in the given screenshot.

Now, under the keystore directory, you will find a file containing the private key and all the other relevant information about this newly created account.

For every new account you create, there will be a corresponding file that gets generated automatically. To check this functionality, you can run the create account command again and you will notice another file in the very same directory.

Note: All the accounts are generated on the offline mode so that no other node gets to know the addresses of the accounts on the network. Once they are involved in the transaction then the relevant information gets included on the network. Now, we will be including the addresses in the transaction to help the network get aware of the newly created accounts. This transaction is then squeezed in the block. Launch the geth console by typing in the given command:

geth –datadir ./datadir console

This geth console offers you numerous libraries with the help of which you can interact with the Blockchain. We will be starting with the eth library.

The functionality that eth library provides is a bit vast. So, here’s a subset of it. You can try them on the geth console to check how do they work.

eth.accounts // returns the list of accounts on this blockchain
eth.getBalance(eth.accounts[0]) // returns the balance of the first account
eth.blockNumber // returns the current block number. 0 for a new blockchain

Now, if you look at the balances of the any newly created accounts then you will see 0 as the balance. But if you want to go ahead with the process then you need to have ether for your Ethereum Blockchain. For this, we will start with the mining.

Mining
To start with the mining process, you need to use the miner library. The following command will help you to start the mining.

miner.start(1) // 1 means run one mining thread

The console output will appear as the following screenshot.

Whenever you want to stop mining, run the following command:
miner.stop()

Now after the successful mining operation, locate the mined ether under the first account created on the Blockchain. This allocation happens on its own. During the mining process, a number of new blocks will be created and gets automatically added to the Blockchain. Most of these blocks will remain empty as there will be no transactions. The account balance of the first address (i.e., eth.getBalance(eth.accounts[0]) and the current block number (i.e., eth.blockNumber) will change its value from zero (0) to some other value.

Sending of Ether
By any chance, if you have created only one account then before we proceed ahead, you need to create a second account. Now, confirm the balance of this account by running the following command.

eth.getBalance(‘address’)

After this, you are required to send ether by the use of the below provided commands.

eth.sendTransaction({from:”address”, to:”address”, value: web3.toWei(amount, “ether”)})
e.g. eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(4, “ether”)})

The following lines of code will display an authentication error. So, if you want to send ether from an account, first you need to unlock that account for further operations. Here, to unlock this sender account, we will make use of the personal library as shown in the following command. Just run the following command and you are good to go.

personal.unlockAccount(eth.accounts[0], “password”)

For depicting the successful completion of the command, you will be returned with a true value. Once done, send the ether again and check for the balances of both the accounts.

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

With the last command, you will be returned with a hash of transaction. This differs from one node to another.

At this point, you must have calculated the value for eth.getBalance(eth.accounts[1]) as 4 but to your surprise, it will return to 0 again. This change of value occurred because the transaction hasn’t been updated in the private Ethereum blockchain yet.

The balance of the sender will also remain as it is. Until this transaction doesn’t get stored in the block, you will not be able to see the visible changes in the sender’s account balance. And do not forget to add this block to the Blockchain. For this whole process to work properly, you need to go for the mining process. So, let’s begin with the mining.

Run the command for mining: miner.start() and to stop, run: miner.stop()

When you pay attention to the console output, you will get to know that the first mined block is having only one transaction. Even the account balances of both the accounts will display the expected values.

Also, check for the transaction details by using the following commands.

eth.getTransaction(“transactionId”)
e.g.eth.getTransaction(“0xf94ece7d159bcc750862b92396e0d98c537593bfa2ecb3a21ec49b0d439592eb”)

If you have been through the successful mining operation then the getTransaction will return you the following code.

<script src=”https://gist.github.com/toluhi/0bae0f738ec08cbe0d75de290387566d.js”></script>

This will provide you the abundant information about the transaction. You can also get information about the block by running the following command.

eth.getBlock(“blockAddress”)
e.g.eth.getBlock(“0x1420da4e3dc647c651135b6d2f617e7046fc748c0321c567cec5e92df352ff54”)

The output for the above command will return something similar to the following.

<script src=”https://gist.github.com/toluhi/02fbc0d36c4bcdaf71aaf4266d20850e.js”></script>

Adding More Nodes to The Shared Network
Restart the console but this time, you will be using networkid as mentioned in the provided command.

geth –datadir ./datadir –networkid 2018 console

With this command, you will be able to restart the already created Blockchain but by using the networkid.

Now, to add a new node to this shared network, you need to run the below provided command in a new terminal.

geth –datadir ./datadir_new init init.json

Important points to remember:

  • The initialisation file (i.e., init.json) is a must.
  • You need to use a new datadir directory. This datadir folder will contain the local copy of the Blockchain. Along with that, each node will have its own copy.

Now, by using the same network id, run the new node on the network. Besides the network id, you are also required to mention the port. Remember that the first node is already using the default port. To make the node discoverable, add nodiscover. Though this last parameter is not mandatory but it is a good practice to include it in your command.

geth –datadir ./datadir_new –networkid 2018 –port 30306 –nodiscover console

Now, open the console of the second node and run the following command.

admin.nodeInfo

The output of this command will appear as shown below.

<script src=”https://gist.github.com/toluhi/2933421eb8dd2266e6d70e6a795327d9.js”></script>

Make sure you copy the value of the enode property and run it on the console of the first node. From the above information, the value of the enode property is given below.

admin.addPeer(enode://f7aa5b604056ff77dc561034f12874586b44b4a00e92355e7f750cfb43717ef1d0092f208b08661b209656d09540b11f6d0c6667a611674f7a75b718424d0c9a@[::]:30306?discport=0″)

On the successful completion of this function, you will be returned with a true value. Still, it doesn’t assure the successful addition of the node. For this confirmation, run the command: admin.peers. Here, you will be able to see the details of the newly added node.

<script src=”https://gist.github.com/toluhi/92069b53cd7fd8fc2229a89f8a68f840.js”></script>

If you mine the first node with the help of the command: miner.start(1), you will notice a visible increase in the block number of the second node.