How are blocks chained together in the blockchain? What makes it safe?
Visual impression of the blockchain
This is a simplified representation of the blockchain. When a new block is added, it will retrieve the hash from the currently last block and store it. This way the blockchain can be browsed backwards. The former block has no knowledge of the next block.
What is hash?
Hash is the outcome of a hash function. Maybe you have used some kind of code when you were in school, to send 'secret' messages to your friends. This encrypt function has the same function but has some security rules.
Hash functions only work one way. This means when you want to hash a phrase you enter the phrase and the hash function will show you the result. Unlike your 'secret' code, it is not possible to convert the result back into the original phrase. You may wonder what is the use of the hash. The hash is used to check for valid values.
Rule 1
It should be impossible to convert the result of a hash function back into the original phrase
I know what you are thinking, you want to try a phrase that is only slightly different from the first one and compare the results of the hash function. You expect it to reveal some of the mystery.
Rule 2
Two slightly different phrases should result in completely different hashes.
The third rule is pretty easy to imagine.
Rule 3
The hash function should always return the same hash for the same phrase.
If this rule wasn't present there would be no use in using this hash function. The whole point is being able to check whether a value is valid.
Rule 4
For the blockchain to work correctly, the result of the hash function needs to be unique.
Every day use of a hash function
A simple example of every day hash function use is the way your password is (or should be) stored on websites where you create an account.
When you create your account, you are often asked to provide a password. In the database of that website your password is not stored as plain text. Before saving your password, the software uses a hash function to create a hash from it. Then the hash is stored in the database.
Next time you try to login, the system doesn't know what your password is but uses the same hash function on whatever password you supply. Then it checks whether or not the outcome results in the hash attached to your username. If it doesn't match then you supplied the wrong password (or username), if it does then the system will log you in.
This is the reason why 'Lost password' procedures result in a password reset and not in an email that tells you what your password is. If you ever come across a system that does supply you with your password then you know the password is saved as plain text. This makes the system very vulnerable. Make sure you don't use a password you also use for other accounts or stay away from this website alltogether.
Back to the blockchain.
Because each block holds its own hash and the hash of the former block, it is not easy to manipulate the blockchain unnoticed.
Let's say you would manipulate the data of one block, by changing the amount of a transaction, or the receiving address. This would result in an invalid chain, the hash doesn't match the content of the block anymore.
No problem, use the hash function to create a new hash and we are done. Still an invalid chain. Remember the hash of a former block is stored in the next block. Oh brother, so this means all the blocks in the chain have to be hashed again to adapt to the change you made.
And that, is easier said than done. Hashes are not that easily generated due to the difficulty level set.
Rule 5
With current difficulty level 18, all hashes have to start with 18 leading zeros.
Example hashes
The current difficulty level results in one valid hash being generated every 10 minutes. When people are mining, they are searching for these hashes. Can you imagine how much energy is wasted around the world by all computers systems that are mining, but only one resulting in finding a valid result every 10 minutes. But that is another story.
Mining hashes, a lot of calculations
For each block the hash is generating using the hash function. This hardly ever results in a hash that meets all the mentioned rules, especially the 18 leading zeros rule. So how do you get a valid hash, you can't start changing the data of this block and feeding the same data to the hash function will always result in the same hash.
For that reason there is sort of a counter added to the data of the block, it is called nonce. This value has no meaning to the data of the block. By changing the nonce, the hash of the block changes.
In a simplified way this is what happens:
- take in account all the necessary data and use the hash function on it
- if the hash is not valid
- increase the nonce and try again
This delay in finding valid hashes makes the system difficult to temper with.