An Introduction to Bitcoin Mining and the Blockchain.
How does it all work? A chapter for beginners.
I’m going to take you through a little game, and together we’re going to mine a simplified version of a bitcoin block 👇
Before we start, it’s important you have some understanding of what a Hash is.
A Hash is like a data fingerprint that’s calculated using some fancy math.
(If this is new, I suggest reading the previous beginner-friendly chapter first).
You can play around with Hashes using this helpful website (and I’ll be using it for our demonstrations here):
Let’s play!
Imagine you and I are miners in a simplified bitcoin network.
We can make some money if we mine new blocks, but we have to play according to rules, otherwise the other users in the network will reject our blocks.
Rules:
-
Max of 2 transactions per block.
-
Whoever mines the block can claim a reward of 1 bitcoin plus the fees of the transactions in that block.
-
The SHA-256 Hash of the block’s contents must start with "00000".
-
Each block must include the Hash of the previous block.
That’s a lot to remember, so it might be easier if we just take a look at the most recent block, which I mined myself.
A few things to note:
-
The Block Number 4532 means its the 4532nd block in the entire history of the network.
-
There are two transactions in the block, each paying a small fee.
-
I mined the block, so I got the reward - 1 newly created BTC, plus the 0.003 in fees.
-
It links the previous block’s hash (which starts with five 0’s).
-
And finally at the bottom, you can see the hash of this block also starts with five 0’s - so it’s valid!
But there’s one more field, the "Nonce" - what’s this?
The Nonce is actually a meaningless field (you can put any number there).
It’s sole purpose is to give you some data to tinker with until your block hashes to something valid.
Take a look what happens if we keep everything the same, but change the Nonce by 1 (we change it from 14885 to 14884).
The SHA-256 hash at the bottom is completely different. It no longer starts with five 0’s, so the block is not valid!
The above, invalid block was one of the many guess-and-check attempts that my software made before finding a valid block.
The Nonce I finally found was 14885, and it works to make the block hash to start with 00000.
Hence the final valid block is:
Alright now I’m going to help YOU mine the next block.
Let’s start by putting together the basics.
Notice:
-
We added 1 to the Block Number, so we’re at 4533 now.
-
You, the Reader, claim a reward of 1 BTC.
-
We include the previous block’s hash
But this block isn’t complete yet.
While it is legal to have 0 transactions in the block, you might as well include as many as you can so that you can claim some additional fees.
So let’s find some transactions to add.
Pending transactions get broadcast to you from other participants in the network.
Let’s pretend that there are 3 valid pending transactions to choose from:
-
Danny pays Sahil 1 BTC (+0.002 Fee).
-
Sahil pays Sam 2 BTC (+0.001 Fee).
-
Danny pays Kenny 0.5 BTC (+0.003 Fee).
Remember, the max transactions you can include in a block is 2… so which 2 will you choose?
Easy, pick the ones with the highest fees, so you can make the most money!
Let’s add them to your block:
Notice how your reward is now 1.005 instead of just 1.
Ok now that you have your block ready to go, there’s one final problem: the hash does not start with 00000!
What to do?
Well, remember the hash game from the previous chapter?
You need to start changing that Nonce and checking hashes until you find a valid one!
(Or better yet, let your friend’s software do it for you!)
Luckily I have a script that does this.
After running my script for about a second, and trying 705,719 different Nonces, look what I found for you.
It’s a valid block!
Go ahead and submit this to our fake bitcoin network, and everyone will agree that it is indeed valid, and your block will be added to the blockchain.
This block will be part of the blockchain forever!
Oh and you got a reward of 1.005 Bitcoin, congrats!
Anyway, that’s a simplified version of bitcoin mining. Hope it was helpful!