How does the blockchain work?

Transactions are added to the blockchain almost as soon as they come in. But we can't just add every transaction willy-nillily. We need to add them securely.

To achieve this, Bitcoin forces anyone who wants to add a block of transactions to the blockchain to solve a puzzle first.

Why the need for a puzzle?

Why not add transactions to the blockchain straight away without the need for a puzzle?

The short answer is: the puzzle prevents people from adding fraudulent transactions to the blockchain.

Quick example.

You send someone 1 bitcoin, and that transaction is added to the blockchain.

If it was possible to add to the blockchain quickly and easily, you could create a new version where you didn't acually send that bitcoin to someone else. And because your version of the blockchain is newer and longer, everyone on the bitcoin network will update their copy of the blockchain to your version, and the original bitcoin transaction will disappear.

This isn't good.

So before a block of transactions is added to the blockchain and everyone updates their copy, someone needs to solve the puzzle for that block.

This prevents dodgy versions of the blockchain from being built.

What is this puzzle?

It's a cryptographic hash function.

That means absolutely nothing to you I know, so allow me to elaborate.

Cryptography is the art of muddling up messages so that the original message is hard (or impossible) to decipher. For example, if I wanted to send a secret message to my Mum to buy more milk, I could use my cryptography skills to change the message to yub erom klim.

A hash function is the way you encrypt a message in Cryptography. In my milk example I reversed each word, so the hash function was "reverse each word". This would be an awful hash function in real life as it's easy to figure out the original message, but at least it gives you an idea of what a hash function is.

Bitcoin on the other hand uses a hash function called SHA-256, which is a tad more complex than my milk function. If I encrypt "buy more milk" using SHA-256, I get this: 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

I know, what the hell right? It's not even the same length as the original message, and how did those numbers get in there?

This just goes to show how complex (i.e. secure) SHA-256 is. There is absolutely no way you're going to be intercepting my Mum on her way to the shop now.

How is this used as a puzzle in Bitcoin?

Imagine that I told you that you could only add a block of transactions to the blockchain if you were able to find the original message from 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

Tricky, right?

The only way you could do it is by guessing the answer, over and over again until you got it right. But you're pretty methodical, so you'd start with:

// Try "a"
SHA-256("a") = xxxxxxxxxxxxxxxxxxxxxxxx

Nope.

// Try "b"
SHA-256("b") = xxxxxxxxxxxxxxxxxxxxxxxx

Nope.

6 months later…

// Try "buy more milk"
SHA-256("buy more milk") = 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

Yay!

Bitcoin asks you to solve a similar kind of puzzle for each block, but it's a variation on this "find the original message" puzzle…

Example of a Bitcoin puzzle.

First of all, imagine each block comes with it's own identification number (ID), and it's a long string of text and numbers, just like the results of the hashing we just did.

For example, lets say the current block has this ID:

blockID = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

To add this block to the blockchain (also known as "mining" it), we have to hash this ID with another number to try and get a result that's below a certain value.

When I say "get a result below a certain value", lets say that means "get a result that starts with 8 zeros".

So if we hash this ID with the number 1, we get:

SHA-256(blockID, 1) = 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

That's a good effort, but the result doesn't begin with enough zeros.

Let's try the number 2:

SHA-256(blockID, 2) = 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

Nope, still no good. Looks like we'll have to keep working our way through the numbers until we find one that works.

6 months later…

SHA-256(blockID, 2504433986) = 11cb2f8c5d60e666051894788e9d020f69c3b7aaac41ce6b6ef38b7213bdfd92

Yay!

And how does this keep the blockchain secure?

As you can imagine, iterating through all these numbers (i.e. going 1, 2, .. 2504433986) takes time and effort (i.e. computer processing power), so it's difficult to do it on your own.

Every miner on the bitcoin network is trying to find a number that works for the current block. When someone finds it, the answer is broadcast around the network and the block is added to everyone's copy of blockchain.

This is how the blockchain stays secure, because with everyone working hard to solve each block's puzzle, no one-person is going to be able to "out-work" the entire bitcoin network for long enough to be able to create a fraudulent copy of the blockchain.

Subscribe to thepokerbank

I'll send you an email if I add something new and interesting to the website.

Don't worry, it doesn't happen very often.