A neat overview

Now that we know about BTC addresses and transactions, there is one final piece left – the global ledger, also known as the blockchain. Every valid transaction is being added to the continuously growing blockchain. This however, introduces a new problem. If Alice owned 5BTC and created a valid transaction transferring all 5BTC to Bob, what would stop her from simultaneously creating another identical transaction with Chloe as the recipient of the 5BTC? This is known as the double spend problem. If she simultaneously sent both transactions into the Bitcoin network, due to latency differences some of the users will think that she transferred 5BTC to Bob, others will think she sent 5BTC to Chloe, and again others will see both transactions – but they need to agree who is the recipient. There needs to be a mechanism which prevents such double-spending scenarios, and which ensures that the blockchains of all Bitcoin users stay in sync and are entirely identical. The ingenious solution is what I previously described as a sort of “lottery”, which builds on something called proof of work. Recall that cryptographic hashes are entirely unpredictable, thus one can not deliberately create a predetermined hash. With this in mind consider the following rule:

If a set of transactions are to be added to the Blockchain, their hash must begin with 10 consecutive zeroes.

The process of finding such a hash turns out to be pretty hard. You can try this yourself – visit this page and type random text into the box. Then inspect the resulting hash and repeat this until you get 4 consecutive zeroes.. this will take quite a while. The set of transactions which are to be added to the blockchain is called a block. The blockchain as the name suggests is a chain of many such blocks. Only sets of transactions for which a valid hash (starting with X amount of zeroes) has been found are considered legitimate blocks. But who does this and what is their incentive?

Every Bitcoin user can join the confirmation lottery. These users are called miners and the confirmation process mining. They invest their computing power in a race against all other users in order to find the lucky hash for the next block. The race goes on and on and on for every next block. As a reward, the lucky winner gets to keep all transaction fees that were specified in the confirmed transactions. In addition to this, as a bonus, they get to create a predetermined amount of Bitcoin out of thin air and keep it. This is how new Bitcoins are created and flow into the system. The amount of BTC which is allowed to be created is halved every four years, until eventually it reaches zero. At this point no new Bitcoins will be created and the total amount in existence will be a fixed 21 million. The process of mining is actually a multibillion dollar business, with people investing incredible amounts of money in specialised mining hardware, whose only purpose is to (re-)hash sets of transactions. As the hashing power of miners increases, the Bitcoin network dynamically adjusts the difficulty of the required hashes. For example, if there is a sudden rapid increase in hashing power, the required amount of consecutive zeroes will be increased to 20, 30, 40,… zeroes. The difficulty is adjusted based on the average amount of time it took to find a block in the last 2 weeks. This needs to be done roughly every 10 minutes, and if it is faster or slower, the difficulty increases or decreases respectively.

Now back to Alice, who tries to cheat the system and sends two valid transactions into the Bitcoin network. Only one of these transactions will be confirmed – the one who makes it first through the confirmation process. This is based purely on randomness. Because a transaction output can be used as an input only once, the other transaction will simply be discarded. If the transaction with Bob as recipient made it first, he will see that he received 5BTC and that this has been confirmed once. With every consecutive block being added, the number of confirmations will increase. A higher confirmation number means exponentially smaller chances of the transaction becoming invalid. Usually for small amounts of BTC one confirmation is considered safe, however for larger amounts 6 confirmations should be considered definite. Here is the process of confirming transactions into the blockchain:

  1. Miners pick X amount of arBlockchainbitrary transactions, usually the ones with highest transaction fees.
  2. They hash the set of transactions over and over, while adding a random number or “nonce” every iteration. This is done so that the resulting hash changes.
  3. Once a valid hash is found, the set of transactions are added on top of the blockchain as the next block.
  4. The process repeats over and over with new transactions which continue being created every second.

 

The dirty details

Once again there are a few details that deserve some extra attention. As you can see in the flowchart above, every block contains the hash of its previous block. This is the glue that keeps all blocks together and prevents someone from swapping previous blocks. The important rule is to always accept the longest valid chain of blocks. To get a better idea, imagine that someone decided to invalidate all transactions made in block XXX7. Maybe this block contained a large transaction whereby someone paid 1000BTC. If the block could be replaced with another one, which did not contain this large transaction, the original owner of the 1000BTC could spend them again. He could simply try to swap the block with another one, whose hash was pre-computed in advance and which did not contain the 1000BTC transaction. However, by doing so, he would invalidate all other blocks that were built on top of XXX7, namely XXX8 and XXX9. Why is that? Because XXX9 contains the hash of XXX8 and XXX8 contains the hash of XXX7. If XXX7 was somehow modified or swapped, its new hash will differ, thus block XXX8 will become invalid, because the hash of its previous block will not match anymore. XXX9 will become invalid, too, because it builds on top of XXX8 which has been invalidated. Something like an invalidation-chain-reaction occurs. Sounds confusing? Here is a picture showing this:

  1. Block swappingTake out block XXX7 containing a 1000BTC transaction.
  2. Replace it with another precomputed block which does not contain the 1000BTC transaction.
  3. Block XXX8 becomes invalid, because the “previous block hash” value does not match anymore.

This explains why a higher confirmation number makes a transaction more secure. A transaction which has been confirmed 10 times, means that there are 9 blocks on top of it. If somebody wanted to invalidate the block containing this transaction, they would also need to compute a chain of at least 10 more consecutive blocks, due to the “follow the longest valid chain” rule. This is computationally not feasible and the costs would be so high that even the richest government departments would not be able to accomplish it. As previously mentioned, the Bitcoin network is the most powerful computer network on the world. An attacker trying to do such an invalidation would be required to possess more than 50% of the computing power of the BTC network to have a chance of success.

What if two valid blocks are found simultaneously? A kind of a draw occurs, whereby some miners begin building on top of block 1, while the rest build on top of block 2. The result is a temporary “fork“. Eventually one of the two competing chains will become longer than the other and invalidate it. People who were mining on top of the invalid one will quickly change to the longest chain and a convergence towards consensus will occur. There will simply be no point in continuing to mine on the shorter block, because chances of it taking the lead again become exponentially smaller. The invalid part of the fork is called an “orphan“, and the person who mined it will not be able to claim any rewards. It is as if the block was never created.

This article shows the ingenious solution to the double-spend problem, as well as how the Bitcoin network reaches consensus.
You can read more about the blockchain and blockchain explorers here.

 

To get an idea of how much some people invest into mining, here is a video that shows a mining farm. Do not be discouraged by this video – if you feel lucky you can join the fun with your personal computer, too. Alternatively you can team up with others in a so called mining pool and share the rewards. I will talk more about this in the future.

- Back to top -