What is Bitcoin Mining? [2020 Edition]
Bitcoin mining and the Consensus algorithm are the core principles of Bitcoin technologies. They are what makes Bitcoin unique and revolutionary. Let’s see what it’s all about.
As we already know, Bitcoin is a P2P decentralized network in which we transfer value through a series of nodes that hold a decentralized database, and we don’t have any central authority to rely on. That’s all good, but we already had this before Bitcoin came along, the real benefits of this technology are with the Consensus algorithm.
The process of mining is not only a way of discovering new Bitcoins but is, in fact, the main function for securing the network by finding consensus of what is the real ‘truth’ without any central point of authority — this is the Consensus algorithm.
Also Read: How Bitcoin Mining Work? [Technical]
The Reward System
The reward system is the incentive for miners to secure the network. The miners who add new valid blocks to the blockchain receive new coins created and the collection of the transaction fees in that block. Nowadays, the main incentive they have is the new coins created, the transaction fees are only 0.5% of their income, but it is slowly increasing as the limited coin supply decreases over time.
The miners pay themselves, when they successfully add a new block to the blockchain, by creating a special transaction within the block, called the ‘Coinbase’ transaction, which includes the total fees of the transactions plus a number of coins that decreases over time.
Every time the new block is added to the blockchain a new bitcoin is created out of thin air. There’s a limited supply of 21 millions bitcoins. For maintaining a finite and diminishing issuance of money, the maximum amount of a new bitcoin created decreases every four years or every 210.000 blocks. It started in 2009 with 50 Bitcoin per block and in 2012 halved to 25 Bitcoins. In July 2016 halved again to 12.5 Bitcoins. It is estimated that around the year 2020 (block 630.000) it will half again to 6.25 Bitcoins. The last block with currency creation associated will be the 6.720.000th block, to be mined approximately in the year 2137. From that point, the only reward that the miners will have will be the transaction fees. We have to take into account that this fixed monetary supply is a way to resist inflation and appreciate the coin value over time.
Quick Question: What stops miners rewarding themselves a lot more than the current set reward? The answer is that an incorrect reward would result in the block being rejected by the other nodes of the network, wasting a lot of electricity and resources.
Block Header Creation and Mining
The main task of the miner is to create a block, with its block header and transactions and find the solution of the Proof of Work algorithm faster than the other miners in order to spread the new block to the network and win the competition for the reward and fees. For creating the block header, the miner must follow these steps:
- Pick a previous block hash header
- Pick the transactions from the mempool
- Create the Merkle Tree from the picked transactions
- Add a timestamp to the block header
- Add the target of the Proof of Work algorithm (later explained)
- Add the nonce which is initialized to zero (later explained)
By choosing a previous block hash header the miner is committing his mining power to extend the chain that ends in that specific block, in other words, it’s ‘voting’ for that chain ‘buring’ the last block deeper, therefore adding more certainty to the transactions on the block.
Mining is a ‘friendly’ word used to describe the computational work that machines produce in order to solve the Proof of Work (aka PoW) algorithm.
To understand the PoW algorithm we have to see first what is a hash algorithm.
A hash algorithm is a function that takes an arbitrary-length data input and produces a fixed-length deterministic output. It will produce the same output for the same input every time. The main feature of these algorithms is that they are ‘collision-free’ meaning that it’s computationally infeasible to find two different inputs that produce the same output. In Bitcoin, PoW uses SHA256 as it’s core hashing function, which always returns a 256 bits length output. There’s one more particularity, and it’s that there’s no way of knowing nor calculating which input will produce a specific output, the only way is by trying as many times as needed until the desired output is reached.
Let’s see an example:
SHA256(‘This post is awesome’) = 0cea0123da2b2638c9f5743947d6caf313c674e651377c3c373115b3b555aaeb
In this case “0cea0123da2b2638c9f5743947d6caf313c674e651377c3c373115b3b555aaeb” is the resulting output of hashing with SHA256 ‘This post is awesome’. Every time we apply SHA256 to ‘This post is awesome’ it will produce the same output.
Now if we add only one more character:
SHA256(‘This post is awesome1’) = f68930a4da1c474e0073919df822464909180f45370dc2d95e97b7e3c2e3f31d
You see now that a slightly different input produces a totally different output with the same length. In the same fashion, if we increase the variable number, each time we get a new different hash:
SHA256(‘This post is awesome2’) = e04a4eaf043434447d8fadf423c5776bd218461bb4cceb6775129df868e4c5ca
SHA256(‘This post is awesome3’) = c1a5f3f9d98c0758db3a81f74a7e1383b26c44123c13f40bf9c2faf1c14660bd
SHA256(‘This post is awesome4’) = 414d63aa2178b594947c3c4ec4f922940ead9c188648466332626931e9924823
SHA256(‘This post is awesome5’) = 5a6530944bb1ba20972f593925ef8b2f4c64e40a65b5ec129a61886c5c7cfeb6
SHA256(‘This post is awesome6’) = 061c33a89bafc0fad304befa219d508cdf134b93bf44dbc0df516178b26764fc
The variable component number of the input (the number on the examples above), in Bitcoin is called ‘nonce’, it’s used to vary the output and it’s what miners need to find in order to produce an output smaller than the specific target. Wow! What target are we talking about?? Keep reading my dear friend!
Target and Difficulty
A target number is a hexadecimal number used as threshold for the hashed output of the nonce and block header. The result of the hash between the nonce and the block header must be less than the target number. Sometimes we hear that the resulting hash of the nonce and block header must have a certain amount of zeros at the beginning, which is another way of explaining the same thing, the more zeros a number has at the beginning the smaller it is, so the smaller the target.
Smaller targets mean more difficulty, while bigger targets mean less difficulty.
So, in order for a miner to find the specific solution to PoW, it calculates the hash of the block header and the nonce, if the hash is no less than the target, the miner will modify the nonce (usually just incrementing it by one) and try again, until it can find a number less than the target. This process takes a lot of computational power — work. Once the nonce is found, we can say that the PoW for the block is solved, and it’s proof that the miner spent the computational power trying to find the specific output, hence the name, Proof of Work.
As we’ve seen, the target determines the difficulty of the mining and therefore affects the time that it takes to solve the PoW algorithm. Bitcoin blocks are generated every 10 minutes on average, this means that Bitcoin has a fixed frequency currency issuance and it must remain constant over a period of many decades. Over this time it’s expected that the computational power will grow to a much faster pace, so Bitcoin difficulty will have to adjust in order to keep the block generation time at 10 minutes. This process is called ‘retargeting’ and occurs automatically in every node. Every 2016 blocks, all nodes retarget the difficulty by following a preset formula:
New Target = Old Target * (Actual time of last 2016 blocks / 20160 minutes)
This equation takes the time it took to find the last 2016 blocks and divide it with the desired amount of time (20160 minutes, 10 minutes every block) and then multiplies the ratio with the old target. In other words, if the network is finding blocks faster than every 10 minutes the difficulty increases, and if not, it decreases.
Winning the Mining Round
So what happens when a miner finds the solution of PoW and creates the winning block? The first thing it does after creating the block with the PoW solution is transmit it to all its peers. As every other node in the network receives the new block they validate it and if it’s okay, they add it to their local copy of the blockchain, therefore extending the selected chain. After adding the block they abandon the work for that round, and start competing for the next one.
When a node receives a new block, it must validate it by checking a certain set of rules:
- The data structure is syntactically valid
- The block header hash must be less than the target
- The timestamp of the header block must be less than two hours in the future
- The block size is between the average.
- There’s only one Coinbase transaction in the block and it’s the first one
- All transactions in the block are valid.
If the block doesn’t comply with at least one of this rules, the block becomes invalid and won’t be added to the blockchain. The mining process expend a lot of electricity and resources, so if a miner creates a malicious or invalid block that doesn’t comply with this rules it will be rejected and would have wasted a lot of resources for nothing, this is why independent validation is a key component of decentralized consensus.
The final step for the receiving nodes is to add the new block to their local copy of the blockchain. Before getting to that, we have to understand that nodes maintain three different types of blocks:
- Blocks in the main blockchain
- Blocks that conform branches of the main chain (secondary chains)
- Blocks that don’t have a parent yet (orphan blocks)
The “main chain” is the blockchain that has the most blocks on it, or in the case of two chains with the same amount of blocks, it’s the one that has more proof of work in it. By always selecting the longest chain, nodes can ‘vote’ for the consensuated ‘truth’ (everyone has the same version of what is valid). However, there’s situations in which the nodes will maintain branches of the main chain, the conforming blocks of these branches are valid, but they aren’t part of the main chain, and are kept for future reference in case one of those branches is extended to exceed the main chain in work (solving temporary discrepancies between nodes). The orphan blocks are blocks that have an unknown ‘previous block header hash’. They are saved in a orphan block pool until the parent block is received. Once the parent is received they will be linked to it and be removed from the orphan pool.
So now that we know the main components of the process it’s very easy to describe:
After a node validates the new block, it will look at its previous hash block, and see if it is on its local copy of the blockchain. If it isn’t, it will put it on the orphan pool saving it for later. If it is, it will link the new block to its father. If the father of the block is the last block of the main chain, the new block is considered for now as part of the main chain, if the parents height is less than the maximum height (not the last block), it will link it anyway to its parent creating a temporary branch which will be solved later, as more blocks are added on top of one of each of the chains.
And that’s how Bitcoin secures the network through the mining process — by finding consensus through independent nodes. I hope you learn something new today, I’m very grateful that you reached to this point. You can follow me on Medium or on twitter if you want to read and learn more about Blockchain technology.
See you next time!