and why it’s so difficult to rewrite the blockchain
In the last chapter, we understood that to change data in a single block, we must rewrite the whole blockchain from the point where we changed a block. You might have wondered what makes the process of rewriting the whole blockchain so difficult since there is no complex and time consuming operations involved.
See more: Blockchain c code
In this chapter, we’ll look deeply into some other requisites a block must abide that make blocks very difficult to forge.
Before we begin, let’s go back to our hash functions.
There is one other important characteristic to these functions that I haven’t yet mentioned, and that characteristic is: These functions are infeasible to compute in the reverse direction.
What this means is that while it is very simple for a computer to generate a hash from an input string, it is incredibly hard to find the ‘generator string’ just by knowing the output. In fact, there is no better way of finding the input string other than guessing a string, and checking to see if the output hash is the expected. On average, to find the input string that satisfies a desired hash, it would take 2²⁵⁶ guesses, which is just an indescribably humongous amount of guesses, even for a computer.
To prevent an imposter from modifying a block and rewriting quickly the whole blockchain, for a block to be successfully forged, its hash must obey a simple rule: It must start with a certain number of zeroes (The number of zeroes can change depending on the difficulty we want to be associated with creating a new block).
For example, let’s say we define that for a block to be valid in our blockchain it must start with 4 zeroes. Now, every time we want to add a new block to our blockchain, we must apply the hash function on it and see if the output hash starts with 4 zeroes. If it doesn’t, we must change our block a little bit and try again. This procedure repeats itself until the block is valid.
This is the fundamental concept behind Proof of Work, and why blocks are so difficult to create. When you stop to think about it, what this rule implies is that to create a new block, it will take a lot of time, energy and computational power.
Maybe you are interested: How to predict cryptocurrency price using fundamental analysis
What this also means, is that we must add another information when hashing our block: a number that changes every time we make a guess. This is necessary to ensure that the input string is slightly different at every guess, so we have a different hash every time. This number is called the Nonce.
Before we jump into the code, let’s check out the last fundamental piece of theory to piece together our blockchain: Block mining.
If you’ve ever read an in-depth article on blockchain before, this term certainly came up.
On a blockchain network, the servers that crush these computationally complex algorithms to create new blocks are called miners. Many times, in large blockchains, these miners are very large warehouses filled with servers that stay up 24/7 crushing these algorithms to make sure the blockchain evolves.
That’s it for the theory in this chapter! Now let’s go back to our code and fix our Block class so that it’s hash obeys the rules of our blockchain and starts with a certain number of zeroes.