Bitcoin, how does it work?
What exactly is the blockchain?
You've heard it countless times; blockchain! Not only is the term ubiquitous so is the general misunderstanding behind it. And that leaves the door open towards spurious imagination.
Blockchain is a technology that, as the name says, "blocks" the "chain" of fake news.
It most definitely is not that! But this was actually said on national television by a prominent Portuguese intellectual.
Blockchain is a unique kind of database. It's a continuously growing list of records that is open, distributed and impervious to the change of data.
Let's let that sink in.
- Open - anyone in the world can query it.
- Distributed - approximately 100.000 nodes participate in its quorum.
- Impervious - once data is written, no single party can change it.
So we've established that it's a growing list of records. And it's generally at this stage that you'll hear that cryptography glues it together.
How are the blocks tied together?
For that we must explore the concept of hashing. A hashing function maps data of variable size to a fixed-size value. The one Bitcoin uses is called sha256
. You can explore how it works by playing with the following text boxes.
Just change the string "abc" to whatever you want, and you'll see that given tiny variations, the hash changes dramatically. Bitcoin established, by design, that we would be trying to find hashes with a pre-determined number of zeros in the beginning.
Use the above and try with abc26
, you'll get a hash that starts with a 0
. Now try it with abc93803
you'll have 0000
zeros at start. You can browse the first block ever mined and see that the initial difficulty was set to 8 zeros.
As of now, the current difficulty is set to 19(!) zeros, but that changes depending on the amount of hashing power currently available. Bitcoin sets the difficulty (starting number of zeros) to have blocks being mined approximately every 10 minutes. But it's a game of chance, a block may take from just a few seconds to much longer. From the image below you can see that it took 21 minutes, to mine block 673088.
On the early stages of the network, when things were starting and hashing power was scarce, it took almost a full day to mine a single block (block 168 to 169). But since Bitcoin took off, the longest it took, was just a bit over 1 hour and 39 minutes from block 152218 to 152219. Mind you the more hashing power there is, the less likely it is for events like these to spur.
Exactly what is hashed and what's in a block?
A block header carries the following information:
- miner software version
- timestamp of the block
- sha256 of the merkle tree root
- sha256 of the previous block
- a nounce (random data to try and get x number of zeros)
- a target (number of zeros to find)
We'll get to the merkle tree root in an instance but, as you can see, given these 6 items the miner starts iterating the nounce to try and find a hash that is less (has more zeros) than the target. As soon as it finds one, the nouce that solves the problem is saved to the block and the client publishes it. The miner that publishes the block first is rewarded with both the transaction fees and new Bitcoin that's minted out of thin air.
What is a Merkle Tree?
It's a way to verify the integrity of large data structures. In the case of Bitcoin just get all the transactions you are trying to validate and hash them two by two. Then get those hashes and also hash them two by two. Proceed like this until you have a single final hash. That is called the root hash and uniquely represents all the transactions that are below it.
Say we have 4 transactions represented by letters. We join them two by two and hash them in sequential order. Then we hash the hashes.
How do you get BTCs from mining?
Before starting to try find a solution to the block, the miner simply adds a new transaction to their block that gives them X amount of BTC. This is called a generation transaction and its value depends on the block being mined.
The block reward started at 50 BTC and halves every 210.000 blocks. As blocks are mined more or less every 10 minutes, you can pretty much guess when the next halving will happen. Though it has happened to be offset by months!
On 2021 we're on halving3
which gives 6.25 BTC per block.
Halving | Date | Block | Block Reward | Mined in period | % mined |
---|---|---|---|---|---|
launch | 3-Jan-2009 | 0 | 50 | 10,500,000 | 50 |
halving 1 | 28-Nov-2012 | 210,000 | 25 | 5,250,000 | 75 |
halving 2 | 9-Jul-2016 | 420,000 | 12.5 | 2,625,000 | 87.5 |
halving 3 | 11-May-2020 | 630,000 | 6.25 | 1,312,500 | 93.75 |
halving 4 | On 2024 ? | 840,000 | 3.125 | 656,250 | 96.88 |
halving 5 | On 2028 ? | 1,050,000 | 1.5625 | 328,125 | 98.44 |
halving 6 | On 2032 ? | 1,260,000 | 0.78125 | 164,062 | 99.22 |
halving 7 | On 2036 ? | 1,470,000 | 0.390625 | 82,031 | 99.61 |
As you can grasp from this post, the network works on the basis of Proof of Work (PoS) done by the miners. But, being de-centralized and open, anyone can implement a miner.
Remember above when we said that the Block Reward paid to the miner is added by the miner itself? Well, there is at least one documented case where the miner software wasn't working as expected and successfully mined a block without transactions (no fees) and without adding it's own address(!) no block reward!
You can check this half a million bug right here, block 501727.