“And what if multiple miners create different blocks at roughly the same time? Which block will count as the legit one that should count?”
Bitcoin solves the last named problem with the “longest chain rule”: Let’s say the first Block 1 you see is valid, which would be Block 1a. You then start building Block 2a, the next block on top of Block 1a. However when you see Block 1b, you keep an eye on it. If you see Block 2b later, the “longest chain rule” would say that the longer ‘b’ chain is the valid one, and the shorter ones should be ignored (‘a’). You then stop building Block 2a and start building 2b.
Figure 6 (a): Start building on the first block you see (Source: A Gentle Introduction To Blockchain)
Figure 6(b): Mine on top of the longest chain you have seen (Source: A Gentle Introduction To Blockchain)
We already covered that there are private and public blockchains, but the problem with a public network is while anyone can join, anyone can also start an attack.
But what can a dishonest miner (attacker) do? While he can refuse to relay valid transactions to other nodes, attempt to create blocks that include or exclude specific transactions of his choosing and attempt to create a “longer chain” to exclude previously accepted blocks from the main chain, he cannot create bitcoins out of thin air, steal bitcoins from other accounts or make payments on someones behalf pretending to be someone else.
Let me explain the consequences of each attack. If only one node in the network refuses to relay valid transactions, the other honest nodes will pass around the valid transactions and reject invalid transactions from the dishonest miner.
And if he tries to exclude a transaction from a block, he would be only able to delay the transaction, since the other honest nodes would pickup this “unconfirmed transaction” in their blocks.
However, an attacker can create a longer chain of blocks than the rest of the network to kick out the shorter chains. This lets the attacker unwind transactions.
What would a dishonest miner need to do to unwind transactions? He would create two payments with the same bitcoins: One to an online retailer, the other to another address he controls.
After creating both transactions he only broadcasts the payment that pays the retailer, who would send him the goods if the payment is included in an honest block.
After the retailer send you the goods, you would secretly create a longer chain of blocks which exclude the payment to the retailer and include the payment to yourself. If the other nodes are honest and apply the “longest chain rule” on your published and manipulated chain, they will ignore the honest block and continue to build on your chain. The honest block is now orphaned.
However, since you already spend the same bitcoins, the honest block is deemed invalid.
Figure 7: The “double spend” attack (Source: A Gentle Introduction To Blockchain)
This is called “double spend” and only appears for ledgers where block-makers aren’t trusted. To prevent attacks like this, you want to make it hard, expensive and next to impossible for malicious attackers to add blocks. Now the mechanism mentioned above come into play: Proof-of-Work (Bitcoin), Proof-of-Stake (Peercoin) and Delegate Proof-of-Stake (Shift).
A consensus mechanism is an algorithm, that “decides” which block is valid, and which one isn’t. Through this, blockchain and these algorithms solve the Byzantine Generals’ Problem, which is an old computer science problem that was thought to be unsolvable before Bitcoin, the combination of blockchain and Proof-of-Work.
Imagine this situation in the middle ages: There are 300 enemies in a castle surrounded by five armies of 100 men, each commanded by a general. If all five armies attacked at once, they overpower the 300 men inside the Castle. If the don’t attack simultaneously, there is the risk that the attacker will be outnumbered and lose the battle.
As you can see, the generals need to agree when to attack, let’s say at 9pm. However, the “9pm attack” message could only be passed on by a rider on horseback. He has to ride around visiting each general in turn to confirm. Besides that, any general may be a traitor and in league with the enemies in the castle.
What happened before blockchain and Proof-of-Work? General 1 decides to attack at 9pm. He sends out the message “9pm attack” to general 2 with his rider. General 2 reads the message, writes down the message and signs it to also say “9pm attack”. He sends the rider with the message to General 3.
But General 3 is a traitor, and wants the attack to fail. He destroys the message that says “9pm attack” and replaces it with a message that says “8pm attack” and sends it to General 4.
General 4 reads the message saying “8pm attack”. He then notes the time, signs the message and sends it to General 5.
How does the attack look right now? General 4 and 5, that have 200 men together, attack the enemies at 8pm, that have 300 men. Because General 1 and 2 will attack only attack at 9pm, General 4 and 5 are outnumbered and the enemies win the first fight. At 9pm, General 3 joins the enemies and General 1 and 2, having 200 men, find themselves in a fight against 400 men now.
Result: the (traitor) enemies win, since the generals could not find an agreement.
What would happen with blockchain and Proof-of-Work? General 1 sends the same message (“attack at 9pm”), but now there are two more rules: Every General must spend 10 minutes preparing any new message for it to be valid and he must include the history of every previous message in every message.
General 1 sends his message to General 2. This time General 2 know two things for certain: The message must have taken 10 minutes to prepare and that there are no previous messages – so it must be the truth. (Even if General 1 would be a traitor and puts in the wrong time, it doesn’t matter, because if the majority of Generals followed this suggestion, they will still outnumber those in the castle and win.)
Now General 2 sends his message. As required, he spends 10 minutes preparing the new message and he embeds the message from General 1 into his own. The two messages are now ‘chained’ and the rider sets off to General 3.
General 3 is once more a traitor. Last time he changed the message to “8pm attack” so that General 4 and 5 would attack early and get outnumbered. But now he can’t. Why? Because he has only 10 minutes to prepare the message for General 4. He has two options now:
He could try changing the message to “8pm attack”, but to do this, he first (a) needs to spend 10 minutes to prepare his own message, (b) 2 x 10 minutes to replace the messages of General 1 and General 3 and (c) he needs to carry out 30 minutes of work in 10 minutes, because otherwise the other Generals would know that he is traitor.
Or he could accept his defeat and prepare the “9pm attack” message during the next 10 minutes.
Result: The attackers outnumber the enemies win. This is one solution to the Byzantine Generals’ Problem. The 10 minutes of work example is also known as Proof-of-Work and is one of the most used consensus algorithm.
However, there are different kinds of consensus as explained here.