The Building Blocks of Blockchain: Putting It All Together
This is the 5th post in the series “The Building Blocks of Blockchain.” You can find the index and the introductory post of the series here.
Alright! We’ve learned how hashes work, how digital signatures work, and how distributed ledgers and mining works. We’ve learned how each of them works conceptually and in isolation. They are the basic building blocks we need to understand well — in order to understand blockchain well. Now we’re ready to put the blocks together and build the big picture so you can finally experience that “Aha!” moment I owe you.
So let’s get to it!
In the last episode, the relationship between Bugs Bunny and Marvin the Martian was somewhere along these lines:
And guess what? Bugs still owes Marvin that Bitcoin! Unlike last time though, now Bugs Bunny has one to spare, so all he has to do is submit a transaction to Marvin’s address. Let’s look at how he does that.
Submitting a Transaction
The usual way for users to exchange Bitcoins is by using a wallet. A wallet is an app on your phone or computer which holds your private key, and which you use to submit transactions. In the earlier post on digital signatures, we mentioned how a wallet is used to generate a key pair which we need to produce digital signatures.
In much the same way, Bugs Bunny uses the wallet on his phone to submit the following transaction:
Amount: 1 Bitcoin
Recipient: (Marvin) bc1q428wf72dmujnz58l57tm3n76gyhkxad7y5rrqh
Signature: MEUCIQDnFAMFHCJjkxGu+dvFZqxzmvN7uMVf5Y1wSinebiTJ6wIgCFoN2Ak04YgGnRVM12uy7wXsoo45xM8MChNwAov46TY=
That strange string of characters in the recipient field is Marvin’s Bitcoin address. This is an identifier in blockchain which works just like a bank account number. The Signature
field contains a digital signature produced with Bugs Bunny’s private key. This proves that the transaction came from Bugs Bunny, as described in the digital signatures building block.
In the rest of this post, we’ll keep using the more friendly-looking transaction notation from our previous post:
Bugs Bunny gives Marvin the Martian 1 Bitcoin
I just thought it’s worthwhile reminding ourselves that each of these transactions contains a digital signature.
Once created, the wallet submits this transaction to one of the miners.
The Anatomy of a Block
As we described in the previous post, miners are the ones that write transactions into a blockchain. They don’t write them into the blockchain one by one, but in batches. Each batch of transactions is organised into a block, so miners write blocks of transactions into the blockchain. Let’s see what these blocks look like and how they get added to the blockchain.
Daffy Duck is still a miner in our blockchain network, and he just received Bugs’ transaction. He now has 5 verified transactions submitted to him, which he wants to put in a block and add that block to the blockchain. Here’s what this block might look like:
1. Daffy Duck receives 12.5 Bitcoins
2. Yosemite Sam gives Wile E. Coyote 4 Bitcoins
3. Porky Pig gives Pepé Le Pew 3 Bitcoins
4. Bugs Bunny gives Marvin the Martian 1 Bitcoin
5. Wile E. Coyote gives Elmer Fudd 2 Bitcoins
6. Elmer Fudd gives Porky Pig 7 Bitcoins
---
previous_block: 00bc30f8fae8f8b3ca113390fb4cfa16fd01084189fe285805a43c22bd81c739
Where Do Bitcoins Come From?
That first transaction in that block above looks weird.
“Daffy Duck receives 12.5 Bitcoins.”
Receives… from whom? From no one. The first transaction in any block is the so-called coinbase transaction, and it’s a transaction that creates Bitcoins out of thin air. In fact, this is the one and only way how Bitcoins are created. Apart from creating Bitcoins, the other purpose of the coinbase transaction is to provide an incentive for miners to mine, i.e. an incentive to verify transactions they receive and write them into the ledger. It’s essentially the miners’ paycheck.
The amount that miners receive in the coinbase transaction is called the block reward, and it is defined in Bitcoin’s protocol. It started with 50 Bitcoins in 2008, and it halves every 4 years. At the time of writing this post, the block reward is 12.5 Bitcoins, and it’s poised to halve to 6.25 Bitcoins this year (in 2020).
There’s another thing we haven’t seen before, but appeared in our sample block above: the previous_block
field. This field contains the hash of the last block in the blockchain. Each block contains the hash of the block that came before it, which ensures the blocks are sequential, and prevents changes to the contents of the blocks in the blockchain. In other words, this field is what makes the blockchain a chain.
Something’s still missing, though. We learned in the post on distributed ledgers that Daffy needs to solve a puzzle first in order to add the block to the blockchain. Where does that fit in to the story?
Where’s the Puzzle?
The puzzle that miners have to solve is actually ingeniously simple. Check it out:
The hash of the block has to start with a certain number of zeroes.
Let’s see what that means. First, let’s compute the hash of our block as it is now. For this, I just used this online tool for computing hashes.
It uses Keccak-256, the same hash function that both Bitcoin and Ethereum use. The resulting hash is
cea391ea414e9d02dece0db721c0c35de3e50153f13b30160fd7b9260599b771
This hash starts with a c
, which is definitely not a certain number of zeroes. There’s a separate rule that determines what that certain number of zeroes is, i.e. how many zeroes the hash has to start with, but for our example, let’s assume that our current puzzle requires the hash to start with 2 zeroes. How can we make the hash of our block be different, though? The hash is the hash — there’s no changing it.
For exactly this purpose, Bitcoin’s blocks contain another field, the nonce
. That means a block actually looks more like this:
1. Daffy Duck receives 12.5 Bitcoins
2. Yosemite Sam gives Wile E. Coyote 4 Bitcoins
3. Porky Pig gives Pepé Le Pew 3 Bitcoins
4. Bugs Bunny gives Marvin the Martian 1 Bitcoin
5. Wile E. Coyote gives Elmer Fudd 2 Bitcoins
6. Elmer Fudd gives Porky Pig 7 Bitcoins
---
previous_block:
00bc30f8fae8f8b3ca113390fb4cfa16fd01084189fe285805a43c22bd81c739
nonce: 0
The nonce is a number which the miner can set freely in order to change the resulting hash, i.e. to make the hash of the block start with the required number of zeroes. It is essentially the solution of the puzzle.
Now that we’ve introduced the nonce, we can express the puzzle more accurately:
Find a nonce, such that the hash of the block starts with a certain number of zeroes.
Again, for our example, we’ll assume that the target is 2 zeroes. So let’s have at it! We add the new field, set the nonce to 0 (the lowest possible value) and use that online tool again to compute the hash:
89b3e49dc09b0028d3d0ce48ab30314438fd5a08a759302c3d12a6fecd781094
Still no cigar. Daffy has to keep trying different numbers for the nonce, until the resulting hash starts with 2 zeroes. Here are some of his attempts:
1: e464f509fed70efa387ef32dd4172e1205900377094780d2ba6fe9f7c7523afc
2: 938c428a450b550c16e686ebf097e9811978b921beb4b84da7a90f5095d4fb13
3: 2d57d47654c2da7d0f37234c4e88bc7f4f0ec0e01624e7c91df533238a83973c
27: b8ef539375af495738e0bccc6f96f02ddfd34a409b8631ca5abae7e870bf1a02
After trying for quite a while, Daffy found that the nonce 117
results in the following hash:
00639a07198476c9436cbbe1836ca8ca23c6a4792f8ebcba68732b56e9b81fae
Bingo! The hash starts with 2 zeroes, so he’s solved the puzzle!
As you may remember from the post on hashes, we said that cryptographic hash functions have an avalanche effect, i.e. that changing even a single digit in the input data results in a completely different hash. In Daffy’s attempts with different nonces above, you can see the avalanche effect in action.
Time to Gossip
Having solved the puzzle, Daffy Duck now has a valid block that he can tack onto the blockchain. He’s said to have mined a new block. This is what his new block looks like:
1. Daffy Duck receives 12.5 Bitcoins
2. Yosemite Sam gives Wile E. Coyote 4 Bitcoins
3. Porky Pig gives Pepé Le Pew 3 Bitcoins
4. Bugs Bunny gives Marvin the Martian 1 Bitcoin
5. Wile E. Coyote gives Elmer Fudd 2 Bitcoins
6. Elmer Fudd gives Porky Pig 7 Bitcoins
---
previous_block:
00bc30f8fae8f8b3ca113390fb4cfa16fd01084189fe285805a43c22bd81c739
nonce: 117
hash: 00639a07198476c9436cbbe1836ca8ca23c6a4792f8ebcba68732b56e9b81fae
Now he needs to let the world know about it. He sends his new block to all the other miners he knows about. They calculate the hash of the new block, see that it starts with 2 zeroes, and therefore accept it as a valid block. These miners then forward it to all the miners they know about, and soon the block becomes part of the blockchain of all the miners. This process of announcing the new block is called the gossip protocol. I’m sure you can see why 🤭
Of course, Marvin has also been following the blockchain closely, so it’s not long before he finds out Bugs’ transaction came through.
Why Are We Solving Puzzles, Again?
At any time, there could be thousands of miners online, all competing to add the next block to the blockchain to get their reward. We want to make sure that they don’t add new blocks at the same time, because that would lead to different versions of the ledger. Instead, we’re looking for a fair way to always pick one random miner of all the miners who are online, and only allow that one miner to write the next block. Making them all solve the puzzle is the fair way to always pick one miner, because the puzzle is engineered so that statistically, every 10 minutes only one miner can find a solution.
The puzzle is the magic ingredient that replaces a central party that would decide who gets to write into the blockchain. The puzzle is what makes blockchain decentralised.
One Solution Every 10 Minutes, No Matter How Many Miners?
That sounds suspicious, doesn’t it? How can it be that there’s always only 1 solution every 10 minutes, even if the number of miners increases? Shouldn’t it take less time to find a solution if the number of miners rises? You’re absolutely right. It should, and it does. But there’s a simple way Bitcoin solves this.
In our earlier example, we said that Daffy had to find a hash which starts with 2 zeroes. The number of zeroes the hash has to start with is called the difficulty, and it’s updated every 2 weeks. It’s called the difficulty, because the more zeroes the hash is required to start with, the more difficult it is to find the right nonce, i.e. to solve the puzzle.
Every 2 weeks, the average time between blocks is calculated. If it’s less than 10 minutes, the difficulty is increased accordingly. If it’s more than 10 minutes, the difficulty is decreased. As I’m writing this, the hash of the most recent block starts with 18 zeroes, and its nonce is 1,284,630,554
.
How Did Bugs Bunny Know Which Miner to Submit the Transaction To?
In our example, Bugs Bunny sent his transaction to Daffy Duck, and Daffy conveniently mined the next block, including Bugs’ transaction. In reality, there could be thousands of miners online, and only one of them will mine a block every 10 minutes. This means it can take quite a while before Daffy actually mines the next block. So how is Bugs supposed to pick the right miner to send his transaction to, i.e. guess who is going to mine the next block?
Luckily, he doesn’t need to. Just as miners use the gossip protocol to announce their newly mined block, they also use it to share newly received transactions. They check if the transaction is valid according to the current state of the ledger, and then they share the transactions with all other miners they know about.
What If There’s Too Many Transactions?
As of February 2020, there are around 2000 transactions in a Bitcoin block on average. If there’s 1 new block every 10 minutes, that means Bitcoin can execute about 3.6 transactions per second. That gives Bugs Bunny a mischievous idea: he’ll create another Bitcoin address for himself, and submit millions of transactions just passing 1 Bitcoin between his 2 addresses. This will make the miners dance to Bugs’ tune,
while all other transactions will be stuck in line waiting for Bugs’ transactions to clear. What a rascal.
As you may have guessed, Bitcoin has a remedy for such mischievous behaviour:
Transactions are not free.
Transactions actually include a fee, so that transaction that Bugs Bunny sent to Marvin the Martian looked more like this:
Withdraw 1.01 Bitcoins from Bugs Bunny, deposit 1 Bitcoin to Marvin
That missing 0.01 Bitcoin is awarded to the miner who eventually mines this transaction. If the block reward is a miner’s paycheck, the transaction fee is the miner’s tip. Without a high enough tip, miners will just ignore the transaction and move on with their lives. The amount of the fee is not part of the Bitcoin protocol — it’s at the discretion of each miner to decide what they will accept as a sufficient fee to include a transaction in their next block. This basically means transaction fees create a market of their own.
Having learned this, Bugs Bunny now has 2 options: either he submits his transactions without a fee, resulting in the miners ignoring them; or he includes a fee, which makes his plan look more like this:
Congratulations!
There we have it! You should now have a fairly deep understanding of how blockchain works, all based on the practical details of how Bitcoin — the first blockchain — works.
One often-repeated feature of blockchain is its immutability, i.e. that its contents cannot be changed. This is a key feature that makes blockchain attractive for the energy industry. In case you’re interested in learning how what we learned here makes blockchain immutable, you can find out in this 3-minute post I wrote.
If you found any of this confusing or unclear, please let me know in the comments below, or on LinkedIn, or on Twitter.
Even though we still haven’t talked about the arguably most exciting part of blockchain — smart contracts, I thought this would be an appropriate moment for me to thank you, and for you to thank yourself, since you’ve just learned the complete story of blockchain as Bitcoin defined it. Smart contracts were only introduced 6 years after Bitcoin’s release — with Ethereum.
In the next and final episode of the Building Blocks series, we will talk about how smart contracts tie into what we learned about blockchain so far, and what the energy industry had in mind with them.
I would like to take this opportunity to praise the outstanding Coursera course “Bitcoin and Cryptocurrency Technologies” given by Princeton University. I completed this course in 2017, and this was the one resource that gave me my first in-depth understanding of blockchain.
All copyrighted material used under Fair Use for educational purposes.