Blockchain explained – part 2

In Part 1 I explained the very basics of what a blockchain is with a simple example of kids trading toys. In this part I’ll explain how to fix problems which exist because of the distributed nature of the blockchain.


If Adam wants to trust Jake when Jake arrives at his house to trade, Adam needs to go through all the new transactions that are new to him (that Jake brought) and check the new transactions are valid.

If you remember from Part 1 every kid signed their transaction to prevent forgeries. In the blockchain world the way to sign a transaction is via PKI ( Public key infrastructure ). Every kid needs to have a pair of keys – a public key and a private key. The private key is used to sign a transaction. The magic of PKI is that any other kid can quickly verify the signature of another kid by simply checking the signature on the transaction against the public key of whoever signed the transaction.

Double spend

So signing your transaction prevents Jake or anyone else to change any transaction in the blockchain. However it does not prevent Jake from going around and showing other kids the old version of his ledger and spending what he has multiple times.

To prevent double spend anyone who wants to trade using a blockchain needs to verify that the latest ledger they have is actually the latest one. In our case anyone trading with Jake must check he did not go around trading with other kids.

Before Adam trades with Jake he could call up a all the other kids and have them verify the latest ledger Jake brought with him. This would obviously take time and might not even be possible if some kids are not around. Same happens in a distributed blockchain network where some participants might simply be offline.

Adam could also check with a small number of kids of his own choosing accepting the fact that the more kids he checks with the more sure he can be Jake is not trying to cheat him.

Proof of work

There is however another problem commonly known as the Sybil attack where you cheat in a distributed system by creating many fake identities. Remember that before Adam trades with Jake he needs to call a certain number of kids that participate in the blockchain to verify Jake did not trade with them and spent his balance. If it is cheap for Jake to create fake kids identities ( with fake phone numbers or email addresses ) then he could easily fool Adam. There would be a good chance Adam would be calling fake kids Jake created.

We could simply make it expensive to fake identities. But that would mean each kid who wants to join our toy trading needs to pay some fee to join.

Instead of asking for a fee to join the blockchain – we could make it so that when someone claims a transaction is valid they need to perform some work and prove they did that work. Many blockchain implementations choose to do just that and call it proof of work. Each kid not only signs their transaction but also offers a small reward – say a piece of candy – for anyone who performs a mathematical task that verifies the transaction. This math tasks needs to take some effort to do but once performed should be very cheap to verify.

If every transaction took a lot of effort to verify then other kids might not want to do it for a piece of candy each. But what we could do is allow kids to perform this math task for a whole block of transactions.


This math task is usually based on what is called hashing. This is a mathematical way of turning some data – say a sentence, or a transaction – “Adam owes Alice two toy cars” into a big number. This process of hashing makes it very easy to convert a sentence into a number but very hard to go from having a number to finding out what sentence it represented.

We can ask people to perform this math task by telling them to take our sentence and “hash” it or perform this math task of turning our transaction into a number. But there is a catch. We want that final number to have the first digit be for example the number 5. We allow people to add as many letters or words to our original sentence ( that represents our transaction – “Adam owes Alice two toy cars” ) until they get a number that starts with 5.

What you need they need to do is try many sentences like:

"Adam owes Alice two toy cars a" --> 67299
"Adam owes Alice two toy cars b" --> 87322
"Adam owes Alice two toy cars c" --> 52277 Bingo, we got 5 as first digit.

If we ask for even more first digits, say we want two fives to be first digits, the harder this task is. You see whoever does the hashing would have to try out adding a lot more letters or words until they get lucky and get a number that starts with even more first digits we want.

This is harder because if you remember the task of hashing is cheap but there is no way to know what number you will get once you are done. And you cannot start with a number and tweak it until you get the sentence you need. Commonly in the blockchain world this is called difficulty. You might have heard that for example Bitcoin which is implemented using blockchain changes difficulty automatically when there are more people participating in mining bitcoins. What they are doing is simply asking for more specific digits in their proof of work.

Attack is too expensive

Once a kid performs this math task of verifying a block they would simply add a transaction where they claim the “fee” or candy for all transactions in that block, sign it with their private key to prevent forgeries and add their transaction as the latest in the ledger. They would also broadcast to all other kids that they verified all the transactions in a block and did the work – proving it by writing what sentence they used and what number they got.

Remember, it is hard to do the work but easy to check if the work was performed. Adam or anyone else trading with Jake would simply wait for their transaction to be confirmed.

If Adam waited only on one block to be confirmed there is a chance Jake used one of his fake kids identities and performed the work. So if Adam waits for more blocks to be confirmed he would be more certain he is not getting cheated.

If Jake double spent his balance with someone else, then Adam would see that the next confirmed block does not include the transaction between him and Jake. Jake could go to great lengths and both confirm the next block and confirm more blocks after the one Adam is waiting on. That would however be even more expensive ( worth more than two toy cars he is getting from Adam) for Jake so it would not make sense for Jake to even try.

Chain split

Since blockchain is distributed there is a chance that different people confirm the same block at the same time. This would cause a “fork” in the chain and new blocks being added to multiple parts of the chain. I’ll write about how this is resolved in Part 3.

Blockchain – explain it like I’m five

It seems like everyone is writing about blockchain these days. Bitcoin – the cryptocurrency – is sometimes described as being based on blockchain technology. Blockchain is on covers of magazines (do those still exist?) and lauded as the next big thing.

I figure I’d dig a little bit deeper into what blockchain is, and write it up in a series of posts.

Blockchain is sometimes described as a distributed ledger or distributed database.

To begin with, I’ll start with a short ELI5 on blockchain.

Like they say, you don’t understand something until you can explain it to a child. So let me try to explain blockchain as if I am explaining it to a five year old.

As an example, I’ll use a couple of kids trading toys.

Say Adam is a kid who just had his birthday and got a bunch of toy cars from his grandma.

The problem is, Adam doesn’t care for toy cars and what he really wanted was a Lego Minecraft set. Julie down the street got two Minecraft Lego sets for Christmas and she’s smug about it as only a five year old can be.

Adam is keen on trading a few of his toy cars for a Minecraft Lego set. He asks Julie to trade, but she will have none of it. You see – Julie doesn’t care for toy cars one bit. What she would really like is a troll doll – of all things.

And here we have a problem. Adam wants to trade with Julie, but he doesn’t have anything that Julie wants. Adam knows Julie wants a troll doll but he doesn’t  have any.

Distributed ledger

Adam – being a very convincing person – talks to Julie and promises he will find a way to give her a couple of troll dolls if she will give him one of her Minecraft Lego sets. Julie however is no dummy. She agrees to give Adam one of her Minecraft sets only if Adam writes on a piece of paper that he promises to give her two troll dolls in return at a later date.

Adam agrees, takes a piece of paper and writes:

#1 - 26/Sep/2017 - Julie gave Adam a Minecraft Lego set - {Julie}
#2 - 26/Sep/2017 - Adam owes Julie two troll dolls - {Adam}

Both Adam and Julie sign their names at the end of each note. Adam also makes a copy of everything for himself because he doesn’t trust Julie – after all she might change what he wrote later and claim he owes here more than two troll dolls.

Now both Adam and Julie have a copy of Adam’s promise written on a piece of paper. Julie hands over a Minecraft set and Adam happily goes home.

The notes Adam and Julie wrote down are called transactions. The list of those transactions is called a ledger. This ledger is distributed between Adam and Julie, since they both have a copy of the ledger.

Blocks in a chain

After a few days, Julie learns at the playground that Jake’s sister went to college and left all her old toys to Jake. Among those is a bunch of troll dolls from the 90’s! However, Jake is only interested in toy cars and Julie has none of those.

But wait, Julie does have a promise from Adam that he will give her two troll dolls for the Minecraft set she gave him. What if she could convince Jake to give her two troll dolls instead of Adam – and have Adam owe Jake two toy cars instead?

It takes a while for Julie to explain the scheme to Jake, show him the promise note from Adam and get him to agree. After all, five year olds only trust things they can touch, and these promises on pieces of paper seem a bit high risk.

However, after some convincing Jake agrees to participate (Julie explained that Adam has a few cars that change colors based on temperature) and Julie writes down that she gives permission for Adam to owe Jake instead of her.

Julie now has a ledger with the old promise from Adam and a new one to Jake:

#1 - 26/Sep/2017 - Julie gave Adam a Minecraft Lego set
#2 - 26/Sep/2017 - Adam promises Julie two troll dolls
#3 - 28/Sep/2017 - Jake gives Julie two troll dolls
#4 - 28/Sep/2017 - Julie gives Jake the promise at #2

Julie makes a copy of her ledger and gives it to Jake – so they are both certain they won’t cheat later by changing anything.

Since everyone knows a toy car is worth one troll doll, Jake is confident that Adam will be happy to give him two toy cars instead of giving Julie two troll dolls.

Jake strolls over to Adam, shows him the ledger and asks for two toy cars instead of two troll dolls Adam originally owed Julie. Adam is more than happy to settle his debt, and since he had no troll dolls to begin with he is more than happy to give two toy cars (one of them a car that changes color when cold or hot) to Jake.

Jake also wants everything written down. Adam has an outdated ledger so he first copies the missing transactions to his ledger and then writes down the new transactions. Once he is done copying he realizes there is not enough space on his page, so he turns a new page and writes down the new transaction.

Now the ledger has multiple pages or blocks. Each page or block is numbered. These blocks are connected or chained together by having one block reference the number of the previous block.

Now both Adam and Jake have two pages or blocks in their ledgers:

#1 - 26/Sep/2017 - Julie gave Adam a Minecraft Lego set
#2 - 26/Sep/2017 - Adam promises Julie two troll dolls
#3 - 28/Sep/2017 - Jake gave Julie two troll dolls
#4 - 28/Sep/2017 - Julie gave Jake the promise at #2

Block #1
#5 - 29/Sep/2017 - Adam gave Jake two toy cars

Block: #2
Previous Block: #1

Blockchain gets its name from the chained blocks of transactions.

Just as Adam is writing down his transaction with Jake he realizes there’s a problem. What if Jake goes to someone else with his old ledger that doesn’t show Adam settled his debt with Jake? It seems Jake could double-spend or even multi-spend what he is owed by Adam, simply by running around to other kids before they all realize the trick when they go to Adam to collect their toy cars.

I’ll write about this double spending problem in the next post which will explain how blockchain protocols use consensus to ensure trust amongst participants.

Update: Here is part 2!