The previous part of the series can be found here.
In the previous post, we talked about the blockchain and how using blocks bound together with hashes makes the transaction history very solid. We now have one last big problem to solve — the fact that if our banking system catches on, lots of new blocks will get added to the blockchain around the world at the same time, resulting in desynchronization, history conflicts and various alternative versions of the transaction history. At its core, it stems from the fact that adding blocks is fast and easy, and the history grows much faster than it can be synchronized.
The solution? Just make it hard and slow instead.
Spicing things up
Let’s only accept blocks into the blockchain whose hashes satisfy a rule — for example, the hashes need to begin with a certain number of zeroes. If we only accept block hashes with one leading zero, for example, only one in 16 blocks will qualify (assuming a hexadecimal representation of the hash). To get their block accepted, people creating blocks have keep trying and adding random data to their block to change the hash until the block gets a hash beginning with zero and can be accepted.
Remember that we can’t predict what data to add to the block to get the hash that we want — it’s one of fundamental characteristics of hash functions, so it’s all trial and error. If we require two leading zeroes, the difficulty jumps - now only 1 in 256 block will qualify, making the average time until someone creates a correct block longer. The difficulty will increase very quickly as we require more zeroes. This way, we can artificially slow down adding blocks, making creation of a correct block a notable event and giving everyone time to synchronise.
(Actually in Bitcoin difficulty is set a bit more precisely than just the number of leading zeroes, but that’s a good approximation.)
We should aim for a pretty-long-but-also-pretty-short time it takes to add a new block with transactions. Once we figure that out, we’ll decide on the number of leading zeroes required accordingly. The time can’t be too long, or getting your transaction into the blockchain will take forever. It can’t also be too short, because people will again start adding blocks quicker than the network can sync up.
Let’s say 10 minutes average time between each new block being added is a reasonable target to aim for. But how do we set the number of leading zeroes required? If we set it so early adopters of the network will be able to create a correct block every 10 minutes on average, we have a problem — once more people start using us, there will be more and more people creating blocks, so there’s a bigger chance someone will find a correct block much sooner than before.
The difficulty needs to be adjustable so it scales with the number of participants and the power of their computers. The Bitcoin network agrees on adjusting the difficulty every 2016 blocks created — assuming average creation time of 10 minutes, this is around two weeks. If during the previous two weeks average block creation time was less than 10 minutes, we now require more leading zeroes in the block hashes. If more than 10 minutes, we now require less. This keeps more and more powerful computers occupied with a challenge.
There is a side effect to this, though — up to now, we assumed the people creating the block will be volunteers, acting for the good of our system. However, adding blocks now requires running a lot of computations, which keep their computers occupied and eats their electricity.
Take a look at the details of this recent Bitcoin block. Its hash has a LOT of leading zeroes. Since the hashes you get are completely random, it’s pretty apparent that this can only be a result of tremendous amounts of trial and error. If it’s so hard to create a block, it means there’s enourmous interest in creating blocks. Something must be convincing all those people to keep doing this…
A gold rush, with scratch cards instead of gold
That’s right, there’s money in this. Everyone who creates a block is allowed to add a complimentary transaction of 12.5 BTC out of nowhere to his address. This is the place where new bitcoins come from. Creating blocks is somewhat misleadingly called “mining” — “miners”, people who create blocks and therefore keep the system going, receive newly created bitcoins as payment for their trouble.
The faster you can mix data, compute and check hashes, the bigger your chance to snatch the reward before anyone else. It’s important to be first, because once a miner adds a block, others have to start another round from scratch on top of his new block. That’s the reason why people keep getting monster sized Bitcoin mining machines — it’s an arms race where computing power is king.
However, if you’re a miner — it’s always you versus everyone else. Even though the reward is big, the chances that you, out of all the other miners, will get it, are always pretty minuscule. Therefore, people get together to stabilise the odds — they form mining pools like SlushPool or AntPool. Whenever someone in a mining pool manages to create a block, the reward is distributed among all participants in the pool according to their hashrate (computing power measured in hashes per second) that they contributed.
Block rewards seem a bit like free money appearing out of thin air, and as a result, the money seems a bit worthless. However, it’s nothing but. To receive it, you need to use up lots of valuable resources — time, electricity, hardware. It’s also not worthless — they are tokens enabling you to use an unmodifiable book that will never disappear or be destroyed, ever. Lastly, almost every regular currency in the world has new money appearing out of nowhere — either new notes being printed, or money created virtually. In a way, creating new money in cryptocurrency makes more sense than in what we commonly use now.
This is a good place to point out how a commonly held belief about Bitcoin is completely false. Many people believe that since bitcoin mining is called “mining”, bitcoins are like a resource you find in the ground, so with more computing power, you “mine” more of them faster. It’s a common base for the argument that Bitcoin is worthless because everyone can just “print their own money”, and when lots of computing power enters the system, horrible inflation will happen.
This is obviously false - bitcoin mining is a lot more like scratching lots of lottery scratchcards, really really fast. The faster computer you have, the more scratchcards you can scratch and the bigger your chances of getting the winning one (getting a correct hash). There’s still only one winner, and there’s always a constant amount of bitcoin created every roughly 10 minutes independently of what the total computing power of the network is.
The money also will not keep being created forever. After every 210,000 blocks added to the blockchain, the reward for adding a block is halved. Originally, the reward was 50 BTC. There have been 2 halvings so far, and currently the reward is 12.5 BTC. Eventually, it will hit zero. Since mining each block usually takes the network around 10 minutes, it means the last piece of bitcoin will be created around 2140, making the total supply 21 million bitcoins. Sounds like a long time, but due to the fact we halve the amount every time, 99% of bitcoins will actually already exist by 2036. This makes Bitcoin a scarce, valuable resource when compared to regular currency, which is always being created and is subject to inflation — decreasing value as supply increases.
There ain’t no such thing as a free lunch
There’s an important consequence to this — as creating new blocks yields less and less money, maintaining huge clusters of mining machines gets less profitable. We need them to keep going to secure the history through their immense hashing power, so there’s another way to incentivise the miners — transaction fees.
Every time you create a new transaction, you can set some money aside for the miner that will eventually add your block to the blockchain. The more you give, the more miners will choose your transaction over others for their block, making chances of quick confirmation much bigger. This is called a transaction fee.
Currently, transactions without transaction fees basically aren’t accepted at all — there’s so many containing a fee that your free transaction will practically never get confirmed. During times when the Bitcoin network is very heavily used (such as before improvements were made on August 1st) the fees needed for quick confirmation can spike pretty high as people compete for space in blocks harder — but even then they are much lower than international banking fees.
Security in numbers
As pointed out before, giving rewards for creating blocks creates an arms race in computing power. Transaction fees only magnify the effect. This is very healthy for the security of our system — anyone can add blocks, so anyone can do it according to their own agenda, but to have any kind of significant effect on the whole thing they would have to invest a tremendous amount of resources.
Remember, the only way to determine which version of the transaction history is the commonly accepted one to look for the longest one. In theory, someone possessing 51% of the computing power of the network is able to completely control the system, as they can just start their own branch of the blockchain and create new blocks faster than anyone else. The resulting branch will become the longest and therefore accepted one, invalidating any blocks mined on other branches. Performing the 51% attack is however uneconomical even for actors the size of a government or a multinational corporation — it would simply cost too much for the trouble.
The whole system roughly works as follows then:
- You create a transaction to another Bitcoin address, set aside some money for a miner and sign the transaction with your private key corresponding to your Bitcoin address.
- Transaction is published to the network.
- Some miners pick up the transaction and add it to the block they’ll be creating next.
- They then try to create a correct hash for their block quicker than others. They’ll keep putting additional random data into the block and checking whether the resulting hash has the required number of leading zeroes.
- With some luck, one of the miners that took your transaction will succeed. He’ll broadcast his victory to the network, adding his block to the blockchain. Now, as long it’s the main, longest chain, your transaction is in official history, along with the miner’s rewards from transaction fees in the block and the reward for mining the block itself.
- The miners then gather more transactions, craft more blocks and repeat the competition all over again.
Each step is secured by a combination of mathematics and economics, producing something never seen before — a single, authentic, yet distributed and decentralized source of truth.
And this is how Bitcoin works.