Mining is the process of adding new blocks to the blockchain by validating transactions and incorporating proof of work. New blocks are chained together through their history in a way that prevents attackers from manipulating history or disrupting transactions.
The actual process of mining is like a computational race to find the correct answer to a puzzle. The node that finds the correct answer first gets to add their block to the blockchain and receive payment for the work preformed.
Currently, that reward is 12.5 BTC.
How Mining Works
A Bitcoin miner is a node that is racing against other nodes to find the answer to a question posed by the blockchain. The node that finds the answer first gets the reward of 12.5 BTC and can add their block of transactions to the blockchain. This starts the mining process over again.
The question the blockchain asks builds off a foundational part of the blockchain: hashes. Hashing data plays a large role in data integrity and is integral to the strength of Bitcoin. Essentially, hashes are alphanumeric numbers used to represent a set of data (such as the transactions in a block). The blockchain asks the nodes to find a hash value that is below a set target.
Given a set of transactions, the hash of the block is a seemingly random number. Changing any part of the transaction will cause a major change in the hash value. For instance:
Craft Crypto → b161ec24741ce3b912ed09d0a714714a6846df987204518b5318b12c03f2ecbf
Craft-Crypto → 377f0a261dd94ff21f97949572414eb988c3ab20a32eeb741fcbc45581aa3a90
(For reference, a Bitcoin hash is a number that is 256 bits long, which has a max value of about 12 followed by 76 zeroes. Writing this value in hexadecimal [alphanumeric] greatly reduces the space needed for that number.)
The process of mining is the process of finding a hash value that is below a target set by the blockchain. Nodes are able to change the value of hash by incrementing a special variable called the ‘nonce.’ This is where mining get’s its name: incrementing the nonce to check if the hash value works is a bit like picking at rocks to find the gold.
Heigh Ho, Heigh Ho, It's Off To Work We Go
The mining process follows this outline:
- A node collects transactions to put into a block
- The node confirms that all transactions are valid
- The node hashes together all the transactions to create a single hash for the block.
- The block is only valid if the hash value is below the blockchain’s target.
- If the hash value is above the target, nonce is incremented and a new hash is calculated. This repeats until the hash value is below the target.
After a block is accepted by the blockchain, it is then sent off to the other nodes to be cross-checked by the network.
Let’s take a look at an example
Adding ‘Craft-Crypto’ to the Blockchain
Hashes are represented by 64 letters and numbers. As an easy example, the blockchain sets a target by dictating the number of 0s at the beginning of a hash. For instance, the hash of ‘Craft-Crypto’ is:
To impose a proof of work requirement, the blockchain dictates the hash must have one 0 at the beginning. It wouldn’t make sense to change the data (Craft-Crypto), so we instead add the nonce on the end. Nonce starts off at 0, so we hash ‘Craft-Crypto0’ and check the answer:
This still does not meet the requirement, so we keep incrementing nonce until we find one that works:
Craft-Crypto1 → feff4f32546432d7844576f405f6f6afed270ff8aa09810b5dcc81c04c91e856
Craft-Crypto2 → 6c1fae5f11fa0e73e7b18206771207a0e9c538ed4f3fbba8b621bc5b94563bf1
Craft-Crypto3 → 03411ddff7be182e7dd72bf72ad8abc2f1df4037ddc0c2e74315f8e16777073f
There we go! The nonce value we need to make an acceptable block is 3. This took 4 tries (starting at a nonce of 0), which means this had a proof of work requirement of 4 hashes to get added.
To exponentially increase the amount of work needed, the blockchain increases the number of 0s required. For instance, finding a hash that has two 0s at the front needs 92 hashes:
Craft-Crypto4 → be30ce7d0af942b417e4c4a4a1d58ee6b5c9d28ddb185bc066a1cad2f13955be
Craft-Crypto5 → 58d2d8e8b906a42d957d039df0a4687e59fa033952479553a9e3536d19debb66
Craft-Crypto90 → 93ea35d13e997cdd47754b33437420d2155d1a6ea723e0be892c390d1633cebc
Craft-Crypto91 → 00d422ec13827967cc2fc671ffbeced813245467bfcc28550566bdb3e76b6f07
For a real world example, Block #567366 has a hash of:
The blockchain changes target value, or the difficulty, to keep block generation at a steady pace. The idea is that a block will be created every 10 minutes on average.
So if mining is just guessing at a number until some hash gets below a certain value, what’s the point?
Why Bitcoin Needs Mining and Proof of Work
Proof of work helps create order in a distributed network such as the blockchain. With no trusted central authority to guide the blockchain, some system is needed to determine which computer can add a block to the blockchain. For Bitcoin to work, there needs to be one and only one prevailing history of transactions.
Consider a global network with no proof of work. All nodes start off with the same history of transactions, but can then add to that history whenever they receive a new transaction. Considering the distance between nodes, it’s fair to say that some nodes receive some transactions before others.
Let’s say that Bob has 5 Bitcoins, Alice has none, and neither does Chuck. Alice writes a great article for Bob and Chuck helped too. Bob sends Alice 2 BTC as payment (it was a freaking fantastic article). Alice then sends Chuck a Bitcoin for his contributions.
Node A and Node B are on opposite sides of the country. Node A hears about Bob’s payment first, and then Alice’s. Node A’s transaction history looks like this:
Whereas Node B sees Alice’s transaction first and then Bob’s:
Node B rejects Alice’s transfer, as she does not have any Bitcoin to send to Chuck. Then she receives 2 from Bob. Now Node B’s history is radically different than Node A’s. Which history is valid?
Instead, proof of work is a system that selects one node to add their block of transactions to the blockchain. After the block is added, the other nodes can verify it or reject it, but only one block is added at a time. The node is chosen through ‘random chance’ based on the hashing power of each node. The more hashing power a node can bring, the better chances it has of finding the correct nonce and adding a block (and getting the 12.5 Bitcoin!).
In addition, adding proof of work to the blockchain gives Bitcoin its inherent resistance to attackers.
How Proof of Work Makes Bitcoin Secure
Imposing proof of work to add blocks to the blockchain prevents attackers from tampering with the blockchain. For example, let’s say an attacker went back into history and tried to reverse a payment he made.
Changing the transfer changes the hash of that block. Because the hash changed, there is an incredibly high chance that the nonce value needs to be recalculated. Doing this requires the attacker to put about the same amount of work into finding the correct nonce value as the entire network put into finding the original.
Further, each block is tied to each other through their hashes. If a block’s hash changes, all the hashes of all the blocks after it will also change. This means that new nonce values will need to be found for each block as well. This is compounded with the fact that the blockchain continues to add blocks while the attacker is crafting the fraudulent chain (which results in even more reworking for the attacker).
Basically, unless an attacker has a majority of the hashing power of the blockchain (the 51% attack), then the history of the blockchain is considered immutable, unchanging, and permanent. Currently, the cost of a 51% attack is about 8.7 billion dollars. This does not include energy costs or the ‘cost’ of reducing the value of Bitcoin from an attack.
Hashing It All Together
Mining a Bitcoin involves finding the correct nonce to satisfy hash requirements on the blockchain. Due to the nature of hashing, finding the correct nonce is entirely a ‘guess and check’ operation. The first node to find the correct nonce adds their block to the blockchain and receives 12.5 BTC for their efforts. This mining activity also helps to create order on the network and makes Bitcoin transactions immutable.
Guessing and checking nonces is how mining got it’s name. It’s just like cracking open random rocks until you find the gold.