I won’t say you have to understand advance concepts in programming to understand Block-Chain, but your life would be a lot easier if you do. And if you understand object oriented programming, I have a time saver for you: Block-Chain is Link-list. If you are new to object-oriented programming, read on. I will go through the components that makes up Block-Chain.
Every one of my blogs utilizes a top down approach except for this one, because Block-Chain, beneath all the hype, is really just a technical cleverness that can only be appreciated if you already understand codes. It isn’t any grandeur idea that can be transferred into other endeavor, such as the Fundamental Theorem of Calculus. It works because of the way programming is designed. At least I do not see any extension of this idea to other systems, unless we artificially alter a system to behave like programming. This is about as a top down view as I can get with Block-Chain, but I will try my best to make it less dry and more entertaining.
There are 3 classes that are combined together to form a Block-Chain: Block, Blockchain, and Transaction. Notice I capitalized the first alphabet of each terms because they are classes. This blog will discuss the Block class, and part 2 will dive deep into Blockchain and Transaction.
You need to know 3 class methods in Block: constructor, calculateHash, and mineBlock.
Every block has a hash. And every hash is encrypted with the object variables time stamp, previous hash, transaction, and nonce — I will explain nonce later. That is why, in the above screenshot, you see that all variables are initiated with the class constructor input except for nonce and hash, which is assigned to the output of “calculatedHash()”. So what is “calculateHash()”?
“calculateHash()” is a class method to keep the code clean. Inside, I imported SHA256 to encrypt hash, but it’s not the only way. Inside SHA256, we pass the object variables — timeStamp, previousHash, nonce, and transactions — to generate an encrypted code.
So back to nonce. Its soul purpose is to change the encrypted code, that’s it. The next question is why do we need to change the encrypted code? The reason is that if encrypted code doesn’t change, the while loop, in the above screenshot, will run forever. But then why do we need to run this while loop in the “mineBlock” method, and why do we even need this method in the first place. The reason is security. With the increase in difficulty, the computing power, hence the time that requires to mine a block, increases significantly. To understand how does that help security, you first need to understand Block-Chain in the context of nodes.
To run Block-Chain in the real world, it’s not reasonable to have one person or one entity to run a whole block chain for the whole world! It also defeats the purpose of Block-Chain, which is to decentralize. The solution is to give everyone the opportunity to run a Block-Chain network, this individual network is called a node. But how do we know which individual node has authentic blocks and which has fabricated blocks. There, that’s the security issue I mentioned previously. So how is it raising difficulty to mine a block helps security exactly? You were looking at it in the above screenshot: the while loop. I will explain the parameter in the mineBlock function in part 2, right now think of them as how long it takes to form a new block. This concept is called Proof-of-Work and it helps security in 2 ways. First, since it takes intense resources in the form of computing power and time to mine 1 block, miners risk of losing all of the previously formed blocks if they get caught violating the rules. And 2nd, since it’s resource-intensive to form a new block, naturally those who has the longest block is the most trustworthy.
Of course like all things, Proof-of-Work is not a bullet proof solution. For example, centralization is an issue where the largest chain will continue to get larger, which defeats the purpose of decentralization. Therefore, it is still a working process. For now, all you have to do is to understand how this Block class works, and I will tie everything together in part 2 so you will see the magic of Block-Chain.