How Block Hashes Work in Blockchain
A blockchain is a concept of storing data digitally. This data comes in blocks. These blocks are chained together and make the data immutable. When a block of data is chained with the other blocks, its data can never be changed again. It will be publicly available to anyone who wants to see it ever again and will be available in the same sequence in which it was added to the blockchain. Nobody can change the information once it is added in the blockchain.
In the previous section, we have learned how to build the block and the cryptographic hashing ties into the whole process. Here you can see how blockchain uses all these concepts together to preserve the integrity of a full blockchain.
Imagine a bunch of blocks of transaction data as of the following image.
In the above images, you can see that block assembles in chronological order. Block number one is followed by block number two, then block number three, then block number four, and you can continue it as long as you want. Here, you will find that there is a block number field, data field, nonce field, hash value field, and previous field. The previous field is corresponding to the hash value field of the previous block.
We know that every block in a blockchain is cryptographically tied to the next block. In the above example, the previous field in block one is zero because the block one doesn't have a previous hash value, so its value is zero. In block number two, you will find that there is a hash value in the previous field, which references to the previous block hash value. This process is continuing until the last block.
Now imagine if the data in any block is altered. Let us say that we have altered in block number 2, the data in block 2 is now different, and mining the block also gets a new signature. The signature that corresponds with this new set of data is no longer chained to other blocks. It just breaks block number 2 because the hash is no longer valid and it also invalidates every single block that comes after it to the end of the chain. It indicates to other users of this blockchain that some data in block 2 has been altered, and because the blockchain should be immutable, they reject this change by shifting back to a previous record of the blockchain where all the blocks are still chained together. This is the main advantage of the blockchain.
Now, if you try to fix it, the only way you can do it by figuring out a nonce which is explained in the previous lesson. So we take simply one block and try that. If it doesn't give a valid hash, try with two, three, four, and if none of them will work, then simply mine it. When you mine a block, the system figures out that there is a valid hash as it has got four leading zeroes. But as you notice that when you try to hash block number two, this hash doesn't have four leading zeros. Therefore it's still an invalid block. So you would have to mine this block as well. Also, you have to do that with every single block all the way to the front of the chain to fix the problem.
Understanding the importance of four leading zeroes in the hash
Now let's talk about how important it is to have four leading zeroes. These four leading zeros are tied to something called difficulty level. The difficulty level is something that's tied and built into the blockchain network which determines how difficult it is for you to get the equivalent cryptographic hash for a block. In this case, that difficulty level requires us to have a hash that is smaller than what we have in the target.
For this specific purpose, we will need a target that has at least four leading zeroes. For example, if the hash target is 0000a1b2c3d4e5f6, any hash less than or equal to this number is a valid block hash. Many hashes would satisfy this requirement, and anyone of those would be valid. However, it is a tough task to find such a hash. Lesser the hash target, the more difficult it is to find a valid hash.
These difficulty levels keep increasing over time as new computers are added in more cryptographic hashing to the bitcoin network. So, more hashing power means the difficulty level needs to go up. This difficulty level is adjusted in every two weeks to make sure that the computer which is actually competing to solve these cryptographic problems take approximately a total of 10 minutes to mine a new block.
Note: One block every 10 mins = 6 blocks per hour, 6 x 24 in a day, and 6 x 24 x 14 = 2016 blocks in two weeks.
For example: If we were to find a hash less than or equal to 0FFFF, we have 65,536 choices. However, if we were to find a hash less than or equal to 000FF, we have only 256 choices. A lower target number means fewer choices. Usually, more leading zeros we require in our hash, much harder it is to find a valid hash.