Blockchain: What is Mining?
Tags: blockchain, mining
Published: 2018-01-04 23:01:13

---
title: "Blockchain: What is Mining?"
published: true
description: A deeper look at a crucial concept of blockchains
tags: blockchain, mining
cover_image: https://thepracticaldev.s3.amazonaws.com/i/tweho1hnqb6qno3hedw5.jpg
---

## Some clarification

The purpose of mining is probably a little confusing at first. I will keep the Bitcoin blockchain as an example throughout this article. Mining is **NOT** about creating new bitcoins. Mining is the mechanism that allows the blockchain to be a decencentralized security. It secures the bitcoin system and enable a system without a central authority. Do not confuse the rewards given to miners ( new bitcoin ) with the process itself. 

## Mining in Bitcoin

Miners validate new transactions and record them on the global ledger ( blockchain ). On average, a block ( the structure containing transations ) is *mined* every 10 minutes. Miners compete to solve a difficult mathematical problem based on a cryptographic hash algorithm. The solution found is called the *Proof-Of-Work*. This proof proves that a miner did spend a lot of time and resources to solve the problem. When a block is 'solved', the transactions contained are considered *confirmed*, and the bitcoin concerned in the transactions can be spend. So, if you receive some bitcoin on your wallet, it will take approximately 10 minutes for your transaction to be confirmed.

Miners receive a reward when they solve the complex mathematical problem. There are two types of rewards: new bitcoins or transaction fees. The amount of bitcoins created decreases every 4 years ( every 210,000 blocks to be precise ). Today, a newly created block creates 12.5 bitcoins. This number will keep going down until no more bitcoin will be issued. This will happen around 2140, when around 21 millions bitcoins will have been created. After this date, no more bitcoin will be issued.

Miners can also receive rewards in the form of transaction fees. The winning miner can 'keep the change' on the block's transactions. As the amount of bitcoin created with each block diminishes, the transactions fees received by the miner will increase. After 2140, the winning miner will only receive transaction fees as his reward.

<img src='https://thepracticaldev.s3.amazonaws.com/i/m7p9fx3ctrrc74qwoncj.jpg' />

The amount of bitcoin issued with each block is divided by 2 every 210 000 blocks. So we can calculate the maximum amount of bitcoin with some code.

```javascript

// First 210 000 blocks reward
const start_reward = 50

// The reward is modified every 210000 blocks
const reward_interval = 210000

const max_btc = () => {
  // 50 BTC = 5000000000 Satoshis
  // Satoshis are the smallest denomination in bitcoin
  let current_reward = 50 * 10 ** 8
  let total_btc = 0
  while( current_reward > 0 ){
    total_btc += reward_interval * current_reward
    current_reward = current_reward / 2
  }

  return total_btc
}

console.log(`The maximum amount of BTC created will be ${max_btc()} Satoshis, or ${max_btc() / 10**8} BTC`)
// The maximum amount of BTC created will be 2100000000000000 Satoshis, or 21000000 BTC
```

So, yeah, 21 millions will be probably the maximum amount of bitcoin issued in the end!

## How does it work, anyway?

The question is, how can everyone in the network agree on an universal 'truth' about bitcoin ownership, without having to trust anyone in said network? The blockchain is not created or managed by any central authority. Every node has access to the public ledger of transactions that acts as an authoritative record. Somehow, every node in the network, acting on those insecure informations, come up to the same conclusions and are able to replicate the same ledger. Let's try to explore how this works.

It will probably help if we take a real life example. Meet block [#502426](https://blockchain.info/en/block/00000000000000000020c60222099aaebc6e7795784f74628ec640b223d3d339). We'll follow the lifecycle of this block from its construction to its final validation. Let's say the winning miner was called Joe.

### The previous block

In the world of Bitcoin, it takes approximately 10 minutes to validate a new block. Our miner Joe was competing to validate the block *502425*, the previous one. Unfortunately, someone else solved the problem before him. But, the end of one block's competition means the beginning of a new one. As soon as the block 502425 was mined, Joe updated his local copy of the blockchain and starts to create a *candidate block*, the block 502426. While Joe's computer ( or *node* ) was searching for the *Proof of Work* for the previous block, it was also listening for new transactions. Those new transactions are added to the *memory pool* or *transaction pool*. This is where transactions wait until they can be included in a new block and validated.

### Creating the candidate block

When Joe's node is notified that the current block has a valid Proof of Work, it starts constructing a candidate block by gathering the transactions in the transaction pool. It removes the transactions already present in the previous block, if there are any. The block is called a candidate block because it doesn't have a valid Proof of Work yet. 

So, we can see that block #502426 has 3189 transactions inside it. This was the number of transactions present in Joe's transaction pool when he created his candidate block.

<img src='https://thepracticaldev.s3.amazonaws.com/i/rwvlihren0yqfgiqh0gw.jpg' />

#### Coinbase transaction
The first thing Joe's node does is creating the *coinbase transaction*. Very simply, this is his reward for mining the block. This transaction says => *Pay Joe's wallet address xxx BTC to reward him for finding a valid Proof of Work*. This transaction is different from the other ones because, as I explained earlier, the bitcoins in the reward are created from nothing. They do not come from someone's wallet. Joe's node also calculates the transaction fees in the block.

*Joe's reward = Reward for mining block + transactions fees*

In this case, we can see that the block reward is 12.5 BTC ( *Block Reward in left column* ) and the transactions fees are equal to 4.86507997 BTC ( *Transaction fees in left column* ).

*12.5 + 4.86507997 = 17.36507997 BTC*

You can see the details of this transaction in the list below.

<img src='https://thepracticaldev.s3.amazonaws.com/i/2y6fzv5xk2ocpekaevj6.png' />

You can see *No Inputs (Newly Generated Coins)*. Like I said, coinbase transactions do not come from anyone's wallet, so they can't have any inputs. You only have the winning miner's wallet address here.

### Constructing the block header

In a previous article, I described what was contained in [a block](https://dev.to/damcosset/blockchain-what-is-in-a-block-48jo). Joe's node has the responsability to create a proper block header for the block he is mining. In the [article](https://dev.to/damcosset/blockchain-what-is-in-a-block-48jo), I mostly focused on the merkle tree, a summary of transactions and mentioned that there are three different sets of data in a block header: the previous block hash, the merkle tree root and data for the mining competition. We'll dig deeper in the last one.

#### Data fields

This metadata set contains:

- The Version: This is a version number to track the software and/or protocol upgrades.
- Timestamp: Seconds from Unix Epoch. When the block was created.
- Target: Proof of Work algorithm target for this block
- Nonce: Counter used for the Proof of Work algorithm

When the block #502426 was mined, the version number was 2. It becomes *0x20000000* when converted in little-endian format in 4 bytes. ( *Version in the left column* )

Next, we get the 4-byte timestamp. This is the number of seconds elapsed since the January 1, 1970. We see the timestamp for this block is *2018-01-03 21:12:39* ( *Timestamp in left column* ). If we convert this in seconds, we get *1515013959* seconds.

The target field defines the required Proof of Work to make this a valid block. To put it simply, the target is generated by the network and defines what makes a block's hash valid or not. If your hash is higher than the target, it is not valid. This is what is used to calculate the *difficulty*. In our block, the difficulty is *1,931,136,454,487.72*. Take a look at the block's hash:

*00000000000000000020c60222099aaebc6e7795784f74628ec640b223d3d339*

There are 18 leading zeros. This is our difficulty. Every hash with less than 18 leading zeros is invalid ( every hash with 17 leading zeros and less would be lower that the required target ).

The last field is the nonce. It is initialized to zero.

Everything is now ready for Joe's node to mine the block. The goal is to find a value for the nonce that will results in hash lower than the target. So, the mining node will try billions or trillions of nonce values before it gets a valid hash.


## Mining

In a picture, mining is:

<img src='https://thepracticaldev.s3.amazonaws.com/i/q63cjkwbd8ktp3l1ogvj.jpg' />

As you can see, mining is like a lottery. There is no way to predict which nonce will solve the problem.

In a [previous article](https://dev.to/damcosset/trying-to-understand-blockchain-by-making-one-ce4), I implemented a simple blockchain that demonstrates the concept of a nonce and how it changes the hash created.

In the case of Bitcoin, the hash function used is called SHA256. A hash algorithm always produces the same arbitrary length data given the same inputs. It is impossible to compute the same hash with two different inputs ( collision ). It is also impossible to predict the output of any given data in advance.

SHA256 always produces an output 256 bits long. Mining is finding the *nonce*, the only input that changes every time we run the hash function. It is very easy to prove that the nonce found indeed produces a valid hash. All the informations are available, everyone can run the hash function and confirm if the hash is valid or not. Because it is also impossible to predict what the nonce will be, it also act as a proof that the miner worked to get a valid hash ( Hence => *Proof-of-Work* ).


### Adjusting the target

In Bitcoin, a block is mined every 10 minutes or so. The difficulty is calculated so that it never deviates too much from this limit. If the difficulty stays the same in the long term, while computer power increases, it will take less and less time to mine a block. To make sure this doesn't happen, the Proof of Work target is a dynamic parameter. In the Bitcoin world, the target is adjusted every 2016 blocks. Then, we check the amount of time it took to mine those 2016 blocks. It should have taken 20160 minutes ( 2016 * 10 minutes ). The difficulty increases or decreases depending on the time it took to mine those blocks.

## Success !!

Joe's node is getting to work and starts hashing the block's header. After approximately 10 minutes, it discovers a valid hash. We can see the nonce used was 2469953656 ( *Nonce in left column* ).

Joe's node immediately transmits the block to all its peers. They need to validate the new block before propagating it to its peers. This is the part where a dishonest miner can be found out. If the data is invalid, the miner would have wasted his time and computing power. Valid data includes:

- Block header hash is less than the target
- Block size is within acceptable limits
- Block timestamp is less than two hours in the future.
- The first transaction is a coinbase transaction ( and only the first )
- The coinbase transaction has a valid reward.
- All transactions within the blocks are valid ( also have a checklist on their own )

Every node validates independently new blocks following the exact same rules. This assures that miners cannot cheat. This is a key component of the decentralized consensus.

If the block is valid, the other miners will update their own copy of the blockchain with the new block 502246. Joe's block hash is now used by all the miners to mine the block 502247.