Blockchain Ep05: Deep in the Mines

Alabhya Mishra
Coinmonks
7 min readDec 11, 2021

--

In my previous post, we looked at a slight modification to the block structure from the one we had built in the second post of this series. Modifier was introduced as a way to prevent Collisions.

The modified block with a Modifier

However, towards the end of the post I also mentioned that Hashing Functions used in Blockchains are smart enough to prevent collisions. So we don’t really need Modifier, but we still have it. And its called Nonce and it is extremely important to the way Blockchain works!

Sorry but not sorry — A modifier called Nonce really exists in every Block of the Blockchain!

But, if not to prevent Collisions, why does Nonce exist?

The Truth about Nonce

Ever since we constructed the primitive version of blockchain, we were faced with one big problem — the chain could be modified. Sure, making changes to the middle of the chain a bit more work, because Hashes in subsequent block need updating, but it wasn’t difficult enough to prevent a malicious individual from actually doing it. Hashing is a fairly quick process, it takes less than microseconds to compute Hash for any input, so updating an entire chain would be a few seconds work. What can we do to deter this?

Let’s create a rule — only Hash Outputs that start with 000000 (6 leading 0s) will be accepted as a valid Block ID.

Its not an outrageous rule really, we see such rules in many areas of life. You have to be 18+ years to vote, you have to be of a certain age to buy a drink, you need a valid visa to enter a country. So let’s have a rule that acts like a valid visa for a block.

This is an invalid block, because the Block ID doesn’t start with 000..

This block doesn’t have a valid Current Block ID because the starting 3 characters are e042b3 and not 000000 as the rule requires (the previous block isn’t valid either). The rule says, unless the Current Block ID starts with 000000, block cannot be added to the chain. But we can’t change the Data ID, it depends on the Data stored in the block. We can’t change the Previous Block ID either. So what can we do? Nonce comes to the rescue!

Nonce Probing

Just like we did in Probing Modifier to prevent Collision, we can Probe Nonce to find a valid Hash Output. Just keep increasing Nonce by 1 until we are able to find a Hash Output that starts with 000000. And once we find the Nonce that gives us a valid Current Block ID, we can add this block to the chain. Phew

How is this helpful? Well, for one it makes the task of modifying chain randomly difficult. We know that Hash Outputs are random, the input to Hashing Functions has no likeness to the Output. We also know that Hashing is a One Way function, we do not know what input is required to generate a specific output. The moment we impose that rule of leading zeros on our chain, we have tried to curb the randomness of Hashing, and also created a task for ourselves to identify a specific input required to generate a format of the output. And there is no way to do this, except trial and error!

We have to keep trying over and over again, until we find the right combination of Data ID, Previous Block ID and Nonce that create a valid Current Block ID. This makes the work far more difficult than finding a simple Hash. More difficult, means more time consuming.

How long does Nonce Probing take?

To see how difficult it really becomes by imposing the 6 leading 0s rule, I actually wrote a python program. The program computes how long it takes to find a Nonce required to follow the rule. Images below show how the chain evolves with variation in leading 0s rule, starting with 0 leading 0s rule…

Time taken to create the chain with no leading 0s rule; lzc = 0 in the code

It takes almost no time to create the chain when the leading 0s rule is not imposed. Let’s see what happens if we have 3 leading 0s rule.

Time taken to create the chain with 3 leading 0s rule; lzc = 3 in the code

Hmm, there is a slight increase in time , but not enough to deter someone from trying to modify the chain. However, look Nonce values. It took nearly 8000 attempts to find the right Nonce for Second Block! Maybe we should keep going. What happens if we impose the 6 leading 0s rule?

Time taken to create the chain with 6 leading 0s rule; lzc = 6 in the code

Woah! Just look at those Nonce values, already in millions. And it took over 1 minute to find the right combination for Third Block. Now we are getting to a point where someone might reconsider modifying blocks, unless they have a good reason to. But is it still enough? What happens if we go with 8 leading 0s rule?

I have no idea! I didn’t have the patience for the code to stop running. So I can safely assume Nonce is in trillions, if not more. If you think you have the patience, you can try it for yourself by downloading the code from here — GitHub Python Notebook link.

Mining, this is Mining! Finding the right Nonce value which creates a valid Hash Output is called Mining. Adding a new block to the end of chain requires mining just 1 block — and that takes a decent amount of time. Modifying a block in the middle of chain would require re-mining all the subsequent blocks — practically impossible!

Author’s Note: Bitcoin Blockchain uses the SHA-256 Hashing Function and imposes huge leading 0s rule. For example, Hash Output of the Block Number — 712449 on Bitcoin Blockchain is 0000000000000000000adf409db0fcc82d0838ae3a1bc34f87ab48ac7b88bb7e that is 19 leading 0s! Click on the link to see for yourself.

In Conclusion

In this post, we introduced a new component in the block — Nonce. We then saw how imposing simple rules on what the acceptable formats of Current Block ID can increase complexity of finding the right Nonce value for a valid Hash Output. And finally we learnt what Mining is!

Mining is an iterative process to find the right combination of Nonce, Data ID and Previous Block ID that creates a valid Hash Output. Someone needs to do a lot of work (iterations) to find the right combination, only then can a block be linked to the chain. And tampering with the chain requires re-doing the whole process. You have to go deep into the mines again!

In my upcoming posts, I want to talk about why Blockchains are extremely useful. Specifically, in the world of Banking. But before we can get to how Blockchain can disrupt Banking, we have to understand what Banking really is. See you then…

Cliffhanger

Proof-Of-Work makes the Blockchain robust. But why do we need it to be robust? Why all this noise about immutable chains, why is it so important that the data within the chain and the chain itself not be modified, even slightly?

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--

Alabhya Mishra
Coinmonks

Working in finance, data science and analytics. Interested in learning Blockchain