Blockchain: Building a decentralized network


Last time I explained the basics of blockchain. But we are still missing some parts in order to build a working decentralized network like Bitcoin. I'll go into it in this blog.


We understand the concept of blockchain, but we don't know, where it is actually implemented in the network and how it is used. We continue with the example of building a decentralized system for money transferring and we have again person A, B and C. So in this case our blockchain has all the information about occurred transactions and it knows exactly, how much money A, B and C has.
We store a copy of the blockchain on each node. So everyone sees all transactions and the amount everyone owns. This is a fully transparent system. If A sends B 100€ now, a new block is added to the blockchain and everyone in the network updates its chain. The transaction is now confirmed, because it's stored on each blockchain and all nodes in the network see the transaction. A can't double-spend the 100€ any longer! 

Until this point you might think, why we need a blockchain, because we could also store the transactions in a simple database and the result would be the same.
But we have to consider, that humans could easily cheat in this network... Now you will see the real power of blockchain.

Remember, that in a centralized network there is an authority, which is in control of the system. So if something goes wrong or someone changes the data on the servers, the authority could still fix and reverse the errors. But in a decentralized network there is no single authority and no one is in control of the whole system. Let's say everyone is changing the blockchain in his favor and claims, that his chain is the real one. This would end up in chaos. We need to find a way to get control in a decentralized system and find consensus.

    Consensus

How can we make sure everyone in the network has the same blockchain? Let's say C manipulates its blockchain and as we know, the blockchain becomes invalid by changing data. A and B recognize the corrupt chain of C. A and B check their own chains and see both are valid and the same.



Because they are in majority, they can claim having the real chain. Now the chain of C is replaced by the valid one of A and B and our decentralized network is completely working again.
It's like in a democracy, with the majority we can actually gain some power and control. We can use this principle for blockchain as well. 


Okay, we're just missing one last thing. When there were new transactions, we've just added a new block to the chain so far. But if you think about it, we can easily adjust the last added block in our favor and the chain would still be valid, because it doesn't matter if the hash of the last block changes. Our chain stays linked together.


We can fix this problem with the so called mining.

    Mining

Our goal is, that everyone adds the same block to the existing blockchain. We need a proof, that the block with the right transactions was added to the chain. This would prevent people from adding arbitrary blocks to the chain. How can we get such a proof?

We have to look at the hash. Currently the block's hash looks like a random number. 
Let's constrain the hash and say, it has to start with 4 leading zeros. For example 0000xf1hst4d. 
But if the block's hash doesn't have 4 leading zeros, what can we do? Changing the data of the block won't help, because we would destroy necassary information. We need to add a new property to the block. We call it the nonce.



Okay now we have the nonce and it is initialized with a 0. But our hash still doesn't start with 4 leading zeros. So we increment our nonce by 1, that our nonce = 1 now. Let's check our hash again, but still no 4 zeros. We increment again the nonce by 1 and check the hash. We repeat these steps until we have a hash with 4 leading zeros.



Great we have the right hash now! The nonce is 4832 and this is the number for generating a hash with 4 leading zeros in this block. Finding this number is called mining or proof of work.
The thing is, finding the right nonce is a computational challenge. But we are in a decentralized network, so we can share the computational power. So the network works together to find the right nonce for the next block. If someone finds the nonce, he tells the network and everyone proofs it. If it is valid, the new block is added to the chain and the transactions are confirmed.  

This is, how the network is secured. No one is able to add its own block to the chain, because he would need more computational power, than the whole network together. That's impossible!

     Conclusion

We learned two mechanisms. The consensus protocol and the mining process. Adding them to the blockchain completes the decentralized network. No one can manipulate the chain. This is exactly how Bitcoin works.

In my opinion having such a network allows us being independent. But blockchain faces some problems at the moment. Until now it is not efficiently scalable. The more people come into the network, the slower it gets. Also people don't understand the technology and find it too complex. That's why they are sceptical about it. So mass adoption is missing.
But by overcoming these hurdles this technology could really shape our future.

Comments