How does Bitcoin mining work?
August 16, 2019
One of the many pieces making Bitcoin work is mining. Some cryptocurrencies have mining, some don’t. The place miners and mining occupy in Bitcoin is very important and everyone is talking about it. Bitcoin doesn’t work like every other payment system so mining might be hard to understand at first. What is Bitcoin mining? How does it work? I will explain this as simply as possible and we will even be mining manually using the same rules as in Bitcoin!
A chain of blocks containing transactions
A blockchain is a data structure (i.e. a way of storing data on a computer). It consists of a serie of blocks, linked together in a certain way.
In Bitcoin, a block is composed of a header and a list of transactions (to keep it simple). In general, a blockchain can contain anything, it doesn’t have to be transactions! I can create a blockchain storing a list of text messages for example.
The header contains meta information about the block, like for example a timestamp, a software version and more (here is the full list of the fields of the block header).
We will look at two fields in particular: Previous Block Hash and Nonce. But first, let’s see what a hash is!
What is a hash?
In this part we will define mathematical concepts. The goal is not to define them perfectly but to intuit their meaning so that you can understand what mining is !
A hash is the result of a hash function. A function in mathematics could be described in simple terms like a black box that takes things and returns other things.
A hash function is a function that takes data of any size and transforms it into data of fixed size.
If we take for example a well-known hash function called sha256, you always get a result of the same fixed size either you enter one word or a whole sentence (like this sentence for example).
sha256("Hello!") = "334D016F755CD6DC58C53A86E183882F8EC14F52FB05345887C8A5EDD42C87B7" sha256("If we take the example of a well-known hash function called sha256, you always get a result of the same fixed size either you enter one word or a whole sentence (like this sentence for example).") = "F18F915A1F42A40BBECA7FEB4D97A47F9CF2FAAAD78721F38105492DE1329E8C"
In this example the notation means: “I apply a sha256 hash function to what is in between the parenthesis and the result is what’s after the equal sign”.
See? The result is always the same length!
Hash functions have other properties (complete list here):
- determinism: given the same input you always get the same result (you can try it on this site with the examples I gave, you will get the same result!)
- uniformity: It should return different values evenly. It shouldn’t return more often a result.
- non-invertible: given a hash, you shouldn’t be able to find the source of what created the hash.
Hashes are really different for datas that are very close. For example:
sha256("aaaaaaaaaa") = "BF2CB58A68F684D95A3B78EF8F661C9A4E5B09E82CC8F9CC88CCE90528CAEB27" sha256("aaaaaaaaab") = "4F1C5F86A4DDD78A007176779E4F4B5A1EC5171D401ED2FBD6E83615184F941F"
Both texts are one letter different, yet the second hash looks nothing like the first one!
Previous Block Hash: what does it mean?
It means that we take data of the header of the block and put it in a hash function, just like we did previously. This will result in a hash (looking exactly like we have seen before) and this hash will be placed on the header of the next block (and so on).
Here is why: if someone changes data from any block before the last block, the last block hash will look totally different (as we’ve just seen with the last example)! So it will be easy to check if someone has tampered with the data in the blockchain! This is why the header of the next block includes the previous block hash.
Bitcoin is an open network with an open blockchain. It means ANYONE can create a block.
Now imagine if it was this easy to create a block? The network would be flooded with blocks created by anyone. See how easy it is to create a hash (we did it in under a fraction of second with the generator I linked)?
Bitcoin protocol wants to make it harder to create valid blocks. In fact, it wants a block to be created around every 10 minutes. One block every 10 minutes is way less spammy than multiple blocks every second. Blocks are meant to be propagated on the network and it takes a lot of time.
It should be hard to create blocks because someone could create multiple blocks and rewrite the transactions on the blockchain. Imagine this scenario: I give 1 BTC to someone in exchange for dollars. When I receive the dollars, I start creating blocks from BEFORE I gave the BTC to the other person. Now I’ve 1 BTC and the dollars! It should be very hard to rewrite the past!
To handle this, Bitcoin has a rule for that in its code: a valid block should start with a number of heading zeros! Here is an example of a hash with seven heading zeros:
This rule might seem strange at first but we will see why it exists.
Do you remember the nonce we were talking about before? It is the key to how we get heading zeros in the hash of a block!
Now will be mining manually a fake block by using a sha256 generator website!
Our block will have a header field (a timestamp) and some data inside:
timestamp: 2019-08-16 Hello Crypto & Coin! 0
Our content is: “Hello Crypto & Coin!”, our nonce is 0 and our hash is currently:
It doesn’t start with a 0. Now we will increment the nonce until we find a number that creates a hash starting with one heading zero.
I found a result (and it was pretty easy…):
timestamp: 2019-08-16 Hello Crypto & Coin! 1
The resulting hash is:
Ok, that was easy… Now let’s try to find a hash with two heading zeros.
Let’s go… again!
Ok, after some time (just kidding, I didn’t do it manually…) I found a nonce that gives a hash starting with two zeros:
timestamp: 2019-08-16 Hello Crypto & Coin! 124
Gives the hash:
Yep, that was a bit harder… Incrementing the number of leading zeros expected in a hash increases the difficulty of finding a block!
The difficulty changes depending on the speed at which the blocks are found. If blocks are found too quickly (< 10 min) the difficulty adjusts to make blocks harder to find (more leading zeros). If blocks are found to slowly (> 10 min), the difficulty adjusts to make blocks easier to find (less leading zeros).
Here is an actual example in the Bitcoin blockchain:
The last block (at the moment of the writing of this article) has a hash starting with 19 zeros! That’s more than the genesis block (the first block ever mined on the Bitcoin blockchain) that only has 10 leading zeros.
- Bitcoin stores transactions on a blockchain: a chain of blocks where the next block contains the hash of the previous block
- Hashes have interesting properties that will be helpful for making mining possible
- Mining is the act of searching for a nonce so that a block hash starts with a number of zeros
- This prevents malevolent people from creating too many blocks and rewriting the past too easily
Having a question or suggestion about the article?
Tell me on Twitter where I'm @vincentdnl!
Help others understand Bitcoin and cryptocurrencies by sharing this post!