A blockchain is a decentralized database that records all transactions in a format that is checked and authenticated by participants.
This is a definition as simple as I can think of. Let me dissect it into simpler ideas:
To enable all the blocks to check and authenticate all transactions, we need to ensure:
Let’s write some code to implement these features.
We’ll first create a bare bone block class.
We specify that each block has the hash value of the previous block, which also contributes to calculating the hash value of current block. This design ensures that any changes to a block will trigger all the ensuing blocks to change.
Then, we build a blockchain class:
The first block of a blockchain is called “Genesis Block”. It can’t be derived from previous block, so we hard coded it. To add a new block to the blockchain, we need to know the last block. And then we calculate the hash of the new block after we assign to it the previous hash.
Let’s create our own crypto currency based on this blockchain!
The data is our transaction record. Suppose it tells how much money we have. Now, what if I’m a greedy guy and change my account balance? In code, I can do this:
Then I’ll be super rich! Well, blockchain can prevent such rogue behavior. We can add a method to check if the blockchain has been tampered with:
If we change the data of a block, the hash of that block would change. And since the next block depends on the hash of current block to calculate its own hash, it will be triggered to change its hash. The change will ripple downwards, and the hash values of the entire ensuing blocks will all change! By comparing the hash values, we can find if a blockchain is valid.
Our blockchain is now working. We’ve implemented the core features of a blockchain, and we have a way to prevent invalid operations. There’s still one huge issue remains to be solved, though.
If anyone can add new blocks to a blockchain instantly, the blockchain database will soon blow up… To solve this, we need to put a restriction on how fast a block can be added.
There’s also a more important reason to put such a restriction. Imagine that someone changes a block and calculates the hash values of all ensuing blocks, then we have no way to know the change! The
isChainValid() check will still pass! So we must make calculating a valid hash difficult enough, so that no one has the computing power to calculate all the hash values after a certain block.
The process of finding a valid hash is called “mining”. And since this is the way to prove that miners have calculated new blocks legally, it’s also called “Proof Of Work”. We can specify that a hash value that begins with certain amount of 0 is valid. Since the output of the hashing function can’t be manipulated, one must go through a calculating process to find the valid hash, which, takes a great amount of computing power. The amount of 0 is called “difficulty”. More 0 means more difficult.
In code, it would look like this:
However, without changing the content of a block, the hash will never change. So our code will end up with an infinite while loop. To trigger change, we need to add something called nonce to the block.
Now, the amount of computation and time that creating a new block requires depends on the difficulty parameter. As the computer gets more and more powerful, the difficulty parameter is increased to slow down the mining process.
Check out the complete code here
Here is an excellent video you can’t miss! The interactive demo in the video is extremely helpful for me.