Note

If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, 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 miningarticle. 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.

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

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: