The Building Blocks of Blockchain: Distributed Ledgers & Mining

This is the 4th post in the series “The Building Blocks of Blockchain”. You can find the introductory post of the series here.

The first blockchain ever made is the blockchain behind Bitcoin. The purpose of creating Bitcoin was creating a completely digital, decentralized currency system. We’ll talk about what “decentralized” means shortly— what I want to emphasize now, is that blockchain was invented to implement a currency system. It’s a simple goal, and in this post I want to explain how blockchain achieves this goal. Once you understand that, you will understand the most important part of how blockchain works.

A Book of Transactions

Bitcoin uses a blockchain to store information about how much money everyone has, and how they’re transferring their “money” between each other. To put it differently, Bitcoin’s blockchain is a database containing information on balances and transactions. It’s basically a book of transactions, also called a ledger. I’m not even exaggerating too much when I tell you that this is what a blockchain looks like:

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin

It’s what you would expect any ledger to look like, right? Each line is a transaction with an amount, a sender, and a recipient. The first line is a bit of an exception, in that it has no sender. Instead, it declares the creation of 25 Bitcoins and awarding them to Sylvester. I’ll describe how Bitcoins are created in the next post. For now, let’s focus on balances and transactions.

The ledger is the source of truth. You only have Bitcoin if the ledger says you have Bitcoin, and if you want to give someone Bitcoin, you can only do that by adding a transaction that says so into the ledger. From our example ledger above, we can clearly tell that Sylvester has 15 Bitcoins, Porky Pig has 3 Bitcoins, Wile E. Coyote has 1 Bitcoin, and I’m sure you can figure out for yourself how many Bitcoins Yosemite Sam has.

So that’s our currency system. We can tell everyone’s current balance by summing up all the transactions in the ledger, and participants can exchange the currency by submitting new transactions into the ledger. But how does that actually happen? Where is the ledger, and who writes the new transactions into it?

The simple solution to that question would be — an almighty accountant. In other words, a single participant that safeguards the ledger and is the only one allowed to write new transactions into it.

As you can see, in our story, Sylvester is the almighty accountant. If Yosemite Sam wants to give Bugs Bunny a Bitcoin, all Sam needs to do is tell Sylvester that he is giving 1 Bitcoin to Bugs. Sylvester checks the ledger and sees that Sam has 6 Bitcoins available, meaning the new transaction is valid, so Sylvester records the new transaction in the ledger. Now the ledger looks like this:

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin

This is a completely legit solution, and I’m sure you’ve met such almighty accountants before — Visa, PayPal, your bank… They’re all parties whom you trust and who have the exclusive right to carry out transactions in their respective payment systems. E.g. when you want to send some money to your friend via PayPal, you tell PayPal you’re doing so, and PayPal records this transaction in their ledger. This is what we call a centralised payment system, because it relies on a trusted central party for managing the ledger.

But in 2008, along came this dude called Satoshi Nakamoto, who said “hold my beer” and then published a white paper called “Bitcoin: A Peer-to-Peer Electronic Cash System” that described how you can make digital payments work without a trusted central party. By the way, it turns out that Satoshi is not even a real person, and so to this day we have no idea who came up with Bitcoin 🤷

A Distributed Ledger

Bitcoin enables digital payments without a central party by using a distributed ledger. But what is a distributed ledger? The basic idea is that anyone can download a copy of the ledger, and, instead of an almighty accountant, anyone can write new transactions into the ledger.

Wait, what? How can that work?!

Well let’s have a look-see! Say Daffy Duck decided to download a copy of the ledger. At this point, both Daffy Duck and Sylvester hold identical copies of the ledger. Daffy Duck then announces that he is now also a trusted accountant, and transactions can also be submitted to him to enter into the ledger.

Meanwhile, Bugs Bunny owes both Elmer Fudd and Marvin the Martian 1 Bitcoin. He hears the news that there are now 2 trusted accountants, so he decides to be a wise guy about it. He tells Sylvester that he’s giving 1 Bitcoin to Marvin the Martian, and he tells Daffy Duck that he is giving 1 Bitcoin to Elmer Fudd. Now the ledgers look like this:

Sylvester's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin
6. Bugs Bunny gives Marvin the Martian 1 Bitcoin

Daffy Duck's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin
6. Bugs Bunny gives Elmer Fudd 1 Bitcoin

Sufferin’ succotash, Daffy! It looks like Bugs committed a double spend!

Sylvester discovers that his and Daffy’s ledger no longer match, and what’s worse, Bugs Bunny seems to have given his only Bitcoin to two different people! They quickly realise that their currency system is not going to work this way, so they decide to hire an expert.

Remember we said anyone should be able to write into the ledger? This means we can’t hope that Sylvester and Daffy are going to be the only 2 accountants for good. Soon there could be tens, maybe hundreds of accountants, and they should all be able to write into the ledger. How can Wile E. make them all maintain an identical copy of the ledger?

He suspects the key will be to make sure only one accountant at a time writes a transaction into the ledger. But how can he make sure only one accountant at a time writes a transaction into the ledger, even as new accountants can join any time? Wile E. has to ponder on this one.

Puzzles!

That’s it! Wile E. comes up with an ingenious rule:

Before an accountant can write a new transaction into the ledger, they have to solve a puzzle!

The puzzle will be so complicated, that even as all the accountants try hard to solve it, all of them at the same time, it will take about 10 minutes before one of them manages to solve it. The accountant that solves the puzzle gets to write a transaction into the ledger.

She then shares the transaction she just added, along with the solution to the puzzle, to all the other accountants. They verify that the solution is correct, and therefore accept the new transaction and add it to their copy of the ledger.

The puzzle an accountant has to solve is based on the last transaction in the ledger and the new transaction the accountant intends to add. Since the last transaction now changed, they all have to solve a new puzzle, and it takes them 10 minutes again to solve it, as the process repeats.

The purpose of the puzzle is to randomly pick one accountant as the one who writes the next transaction into the ledger. This prevents creating different versions of the ledger, without giving anyone full control over the entire ledger.

Bugs Bunny the Wise Guy, Part 2

Wile E. Coyote explains his new rules to Sylvester and Daffy, but they don’t really quite get them, so they figure they’ll probably understand them better if they just try them. As a first step though, they decide to remove Bugs Bunny’s phony double-spend transactions. Now their ledgers are identical, and they look like this:

Sylvester's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin

Daffy Duck's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin

Meanwhile, Bugs Bunny is feeling cheeky, so he decides to just pull the same stunt again: he tells Sylvester he’s giving Marvin the Martian 1 Bitcoin, and he tells Daffy Duck he’s giving Elmer Fudd 1 Bitcoin.

This time though, Daffy and Sylvester don’t add the transactions into their ledgers immediately, because they have to solve the puzzle first. So they go about doing that.

As it happens, Daffy is the one who solves the puzzle first, so he adds the transaction he has to his ledger:

Daffy's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin
6. Bugs Bunny gives Elmer Fudd 1 Bitcoin

He then shares this new transaction with Sylvester, also telling him the solution to his puzzle. Sylvester verifies the solution to Daffy’s puzzle and thus accepts the new transaction and adds it to his own ledger. Now Sylvester’s ledger looks like this:

Sylvester's Ledger

1. Sylvester creates 25 Bitcoins
2. Sylvester gives Yosemite Sam 10 Bitcoins
3. Yosemite Sam gives Porky Pig 4 Bitcoins
4. Porky Pig gives Wile E. Coyote 1 Bitcoin
5. Yosemite Sam gives Bugs Bunny 1 Bitcoin
6. Bugs Bunny gives Elmer Fudd 1 Bitcoin

Pending Transactions:
** Bugs Bunny gives Marvin the Martian 1 Bitcoin

Sylvester now sees that, according to the new state of the ledger, Bugs Bunny doesn’t have any Bitcoin left, so he just rejects the pending transaction that was supposed to give Bugs’ Bitcoin to Marvin the Martian.

But hey, at least Wile E. Coyote’s rules now have a clear way of deciding who gets to write into the ledger next, and thus make the ledger secure against double spending!

What’s All This About, Again?

You may get the impression that this analogy has become a bit too looney, but in fact, all of what you’ve just read is very very close to how Bitcoin actually works! The process of solving puzzles that I’m describing is really the process of mining, and the accountants are miners. Bitcoin miners really are competing with each other to solve hash puzzles.

What I hoped to get across from this story, is an intuitive understanding of the problem Bitcoin is trying to solve, and how it tries to solve it:

  • Bitcoin intends to implement a ledger that cannot be controlled by a single party, but is controlled by many, in a fair way.
  • Control by many is achieved by always giving control to a single participant for a limited time.
  • Fairness is achieved by each participant gaining control for a limited time based on chance (being lucky enough to be the one who solves the next puzzle).

In the next post, we’ll cover in detail how all of this actually looks in Bitcoin — including how these hash puzzles work, how transactions are organised into blocks, how blocks are chained together, and how Bitcoins are created. We can finally put all the building blocks together and build the big picture to have our well-deserved “Aha!” moment. What a time to be alive!

Next building block: Putting It All Together

Was that interesting and understandable? Did I share some horrible misinformation? Do you disagree with me? Do you have any questions? Please let me know in the comments or on LinkedIn, or on Twitter :)

If you haven’t yet, you can read the introductory post on what it is I even do here.


Image Credits

Sylvester writing, Daffy surprised, Wile E. business card, Wile E. pondering, Daffy pondering, Sylvester pondering, Marvin angry. All copyrighted material used under Fair Use for educational purposes.