How to Build a Local Private Ethereum Blockchain with Mining

Note

For a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:

 

Recap

In our previous article (How to Run Geth on a Local Private Ethereum Blockchain), we discussed how to setup and run Geth on a local private Ethereum network.

In this article, we learn how to develop a local private Ethereum network that includes mining component.

 

Private blockchains with mining

We have shown you the steps to start a simple private blockchain. With some additional steps, you can set up private blockchains with mining enabled too. We will discuss the steps you need to take to get a mining blockchain up and running in the local environment here:

Setting up an environment

Let’s create another directory for a blockchain with mining. It’s very similar to how we set up an environment in the Private blockchain without mining article. In the following code, we create two more subdirectories, data and src, under hello_world_mining:

  $ cd hello_world_mining
  $ mkdir data
  $ mkdir src

When data and src are created, you list the information of the directories under
hello_world_mining to verify the names and locations.

Configuring the custom genesis file

When we created the blockchain without mining, we left the alloc section blank. We will prefund the accounts this time. You may want to tune the difficulty to a lower value in your first attempt. It is better to choose some random value for a nonce, in case it connects to any unknown nodes. Let’s have a look at the following configuration:

  {
  "config": {
  "chainId": 135,
  "homesteadBlock": 0,
  "eip155Block": 0,
  "eip158Block": 0
  },

 

“alloc” : {
“0x0000000000000000000000000000000000000001”: {“balance”: “111111111”},
“0x0000000000000000000000000000000000000002”: {“balance”: “222222222”}

},

“coinbase” : “0x0000000000000000000000000000000000000000”,
“difficulty” : “0x00001”, “extraData” : “”, “gasLimit” : “0x2fefd8”,
“nonce” : “0x0000000000000107”,
“mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”
}

The configuration here is the pre-funding
  address 0x0000000000000000000000000000000000000001 and the balance of 111111111  and 222222222 to the 0x0000000000000000000000000000000000000002 address.

 

One-to-One Live Blockchain Classes

Coding Bootcamps school offers One-to-One Live Blockchain Classes for Beginners.

 

Let’s take the opportunity to look at the other parameters here:

  • coinbase: A 160-bit address that gets the ether rewards from successful mining.
  • difficulty: The difficulty level of how hard it is to mine a block when a nonce discovers the block. The higher the value or the difficulty is, the more calculations that need is needed to be done to mine the block. The difficulty of the current block normally calculated using the timestamp and the difficulty of the previous block. Difficulty plays an important role in controlling the frequency of block generation. Difficulty is set to keep the waiting time of generating a block in the target range. When we read difficulty, we can interpret it using its reciprocal. For example, difficulty is set to 0 x 0400 in hexadecimal, which is 1,024 in decimal. The reciprocal of decimal 1,024 is 1/1024. Basically, 1/1024 indicates there will be 1 successful mining out of 1,024 hash operations on average. The faster your machine can finish 1,024 hash operations, the faster you  will get a valid block.
  • mixhash and nonce: mixhash is a 256-bit hash and nonce is a 64-bit hash. Both of them are used together to verify if the block is cryptographically mined and is a valid block. The verification requirement for them combined is defined in the yellow paper. The reason why both of them are needed rather than just a nonce is that a nonce can be falsified in an attack. It will cost a large amount of computational power of the network to discover the nonce was false. An intermediate calculation, which mixhash provides, can help to find out a nonce. And this calculation is not as expensive. So, when a suspicious mixhash is found when the block is being validated, the block can be discarded without us having to do any additional calculation to check the nonce.
  • parenthash: The Keccak 256-bit hash of an entire block header of the parent. It includes both mixhash and the nonce.
  • gaslimit: This is the upper limit of computations that any block can support. We will talk about this in detail in later in this series.
  • timestamp: This provides the Unix time() output at the block’s inception. The timestamp helps to verify the order of the blocks on the chain.
  • extraData: This is an optional space with a maximum of 32 bytes.

 

By now, you should understand the structure and content of a genesis file. Be our guest and customize a genesis file on your own. Doing so will deepen your understanding.

 

Next Article

In our next article (How to Run Geth on a Local Private Ethereum Blockchain with Mining), we discuss how to run Geth on a local private Ethereum network that includes mining component.

This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.

 

Resources

coming soon