How a blockchain really works
When Bitcoin first came to life back in 2009 the word blockchain was new to everyone. These days it has become a commonly used word but what actually is a blockchain and how does one work? That’s exactly what we will be explaining in this post.
To understand what a blockchain is we first need to talk about hash algorithms and more specifically the SHA256 algorithm. A hashing algorithm is usually used inside a function when programming. The function usually takes one parameter which could be anything, but in this example it is just some text (a string).
x = “This is some text that we are going to hash….”
K = SHA256(x)
At this point X is a variable and it is holding our text that we want to pass to the SHA256 function. We pass the text to the function and it will perform the algorithm and return the hashed value of that text back to us. The value looks like below.
The value that the hash function returned is an unique fingerprint that represents the text “This is some text that we are going to hash….”. Now, if we remove one puncture at the end of the text and pass that text to the same hash function we will receive this instead
As you can clearly see the second hash is completely different from the first one, even though we only removed one puncture at the end of the text. Also notice that the hashes are always the same in length. The length is different for different hashing algorithms but SHA256 always returns a hexadecimal string that is exactly 64 characters in length. This is a very important concept and without it we would not be able to have a blockchain.
Since the returned value is a hexadecimal string the hash is actually a number, usually really big. You might be confused that the hash actually is a number right? Because it contains letters and really doesn’t look like a number that we are used to. That’s because the numbers we commonly use are those written in base 10.
The above numbers in base 10 are actually 6.455778312978373e+76 and 1.0265237402344713e+77 respectively. In both our cases really really big numbers.
So what is a block?
A block in a blockchain is an entity that holds a lot of valuable information, for example all the transactions that have occured during a certain period of time and has made it inside the block.
Above we see a very simplified overview of a block in a blockchain. We have included the most important parts of a block in the image, however please keep in mind that this is a big abstraction to the real thing.
The nonce in a block has a very specific purpose. It is a number that the miners in the network are collectively trying to find (More about this later when we talk about mining).
The body of a block usually holds transactions between different addresses on the network. For example when Bob sends Alice 1 Bitcoin that transaction will eventually end up in the body of a block in Bitcoins blockchain.
The block hash is a unique hash string that is stored in each block. Right before the next block is about to be mined the complete block is passed to the SHA256 function as a parameter to the function and the returned hash is permanently stored in the block.
Previous block hash
The previous block hash is rather self explanatory. Each block in a blockchain stores the previous blocks hash, this is what eventually makes it a chain.
In Image 2 we see three blocks. Notice that the first block (the genesis block) has no previous block hash. Instead the previous block hash is set to 0. All other blocks after the first have a hash corresponding to the previous blocks hash. In this way each node on the network can verify that nothing has ever been tampered with in any previous block. Because if anything gets tampered with in any block the subsequent blocks hashes would also change. The subsequent blocks hashes would change because when hashing each block we also include the previous blocks hash in the hashing function as it’s also a part of the block. And because each node in the network verifies each block that gets mined they would immediately identify any foul play.
Miners and the Nonce
In a distributed peer to peer blockchain network like Bitcoin there are many nodes running on many different computers in different locations. One vital feature that each Bitcoin node has, is the ability to mine. When a node is mining it is actually repeatedly guessing for a number (the nonce).
This nonce together with the transactions and everything else that the current block consists of should – when hashed with the SHA256 algorithm return a number that is smaller than a previously decided value by the network. This value changes depending on how many nodes are currently mining and how big their collective effort is to find the nonce. This is commonly referred to the network difficulty and the process of finding it is commonly referred to as “Proof of work”.
As soon as one miner has found a correct nonce it has successfully mined the block and that miner will then notify the rest of the network of its achievement. The rest of the network can easily verify if a correct nonce has been found by simply checking if the resulting block hash is smaller than that set by the network as the target.
Once the network has accepted the newly minted block the successful miner gets rewarded with tokens. In the case of the Bitcoin network these tokens are Bitcoins.