Pramodh Rai


Understanding the Blockchain

About two weeks ago, I built a blockchain from scratch to understand it better. I did this also in preparation for a talk in Malaysia titled, The Blockchain Revolution, at Magic Academy Symposium 2017. In days prior, I’d just built on the existing Bitcoin blockchain. Terms like ‘distributed’, ‘ledger’, ‘decentralised’ , ‘cryptographic hash’, ‘proof-of-work’ simply evoked concepts in my mind. These became much clearer as I built my own version of the blockchain. I also became absolutely stoked as I realised how money and contracts are now programmable! What we are witnessing with the current trend of companies raising funds via Initial Coin Offerings is nothing less than a revolution. New ecosystems are sprouting where businesses (and even individuals) are able to issue their own digital currency. You don’t need to be a sovereign country to do this. Amazing! You can now choose where to place your trust, a topic of immense interest to me. If you do not believe in a certain currency/token, you can subscribe to another…or another..or even create your own!

Taking a step back, let’s first get to how a blockchain works. Developing our understanding from the ground will help us comprehend the larger picture being painted around us by amazing blockchain developers, forward looking entrepreneurs and innovators who are building the future of money and contracts.

Quick note: This is meant for anyone who wants to break down the jargon of blockchain and understand how it works. Whether you’re a technical or business person, simply read on, including the code snippets.

What is a blockchain?

A list of blocks (records) that are linked together using hashes, maintained as a distributed, decentralised database.

Let’s first look at what constitutes a block:

Typical constituents of a block

Each block in the blockchain consists of :

previous_block_hash : Hashing is fundamental in creation of a blockchain. It involves taking a string of characters of any length, running an algorithm (Bitcoin uses SHA-256) and giving an output of fixed length. More on this in a bit.

index : Identifies the block’s number

data : This is the portion which allows money or contracts to be programmable. It can be an object that contains information such as ‘to’ , ‘from’ addresses and ‘amount’ (for currencies) or details/logic for executing a contract.

timestamp: When the block is created

current_block_hash : As we will see in the following section, blockchain’s immutability arises from here.

nonce : A string (arbitrary) that is added to the hash of the block, meant for establishing Proof-of-Work (PoW), which is required for competing nodes to come to an agreement on whose block to accept as the next in a growing blockchain.

Role of Hashing in the Blockchain

Hashing based on block’s constituents

A hash is used to uniquely identify a block’s contents. As shown in the code above, a block’s hash is generated using SHA-256 from all of the following: previous block’s hash, index, data and timestamp.

As a result, a unique hash is generated that now identifies the block. If any of the above are changed, the corresponding hash will be different and thus one can know that the block’s contents have been changed.

Moreover, as the block is part of a longer blockchain, the next block’s previous_block_hash will be different, so that will also become incongruent. This in turn affects the block after it and so on.

Thus, hashing enhances the security of the block, and preceding ones, all the way to the genesis block (i.e. first block). Such a method of securing the blockchain makes it practically immutable. Given that a blockchain is distributed (i.e. resides on different nodes/computers), a bad actor needs to have enough hashing power to update several blocks and also control the network to propagate his version of the blockchain. This becomes much tougher when the blockchain is decentralised, as shared in the next section.

In the code repository, when you run ruby blockchain_generator.rb, the output would look something like this:

{"index":0,"data":"First block is genesis block","timestamp":"2017-10-06 11:48:48 +0800","previous_block_hash":"0","hash":"71474149f9785a5ab14df5196b6848a82c904132397750dfbd24a698fc4c801e"}
{"index":1,"data":"I've been added, my index is 1","timestamp":"2017-10-06 11:48:48 +0800","previous_block_hash":"71474149f9785a5ab14df5196b6848a82c904132397750dfbd24a698fc4c801e","hash":"c6c429f3773cf7e5d3f9ca3a4b1b59b437fbff9cdcb2d260418464abd7ab423b"}
{"index":2,"data":"I've been added, my index is 2","timestamp":"2017-10-06 11:48:48 +0800","previous_block_hash":"c6c429f3773cf7e5d3f9ca3a4b1b59b437fbff9cdcb2d260418464abd7ab423b","hash":"8bda54e3697cd4a849b6dd46041eb1c0b20fe33adc8269bd7a7897341dd5cffe"}

The output shows how blocks in the blockchain would look if generated by one node, forming a centralised blockchain. It is secure to a certain extent because of the use of hashing mechanism per block and its security would grow as the length of blockchain grows. Storing the blockchain on more nodes would make it distributed, further enhancing its security through having additional copies.

Due to the immutable, distributed nature of blockchain, Distributed Ledger Technology (DLT) improves on how data can be recorded or shared in a way that ensures consistent, tamper-proof view across multiple parties, any of whom are free to check it. Several industries can achieve a huge improvement in efficiency through DLT where verified, encrypted and timestamped transactions are recorded in a transparent manner.

Building on what we have covered so far, if the nodes were able to update one another without the need of a central authority, the blockchain would be decentralised.


If one node updates all other nodes, the blockchain is centralised. If any node can update other nodes, the blockchain is decentralised.

Any node can update all nodes in a decentralised, distributed blockchain. Gif courtesy of Sidekick Studio.

To do this securely, a way to reach consensus in a network of nodes is needed.

One of the common methods is the use of Proof-of-Work (PoW). This refers to an algorithm by which nodes in a network achieve distributed consensus. This is where the use of a nonce is required.

When a new block is being created, as described above, all contents are hashed. The output of a hash needs to meet a pre-set difficulty level, for example, “require two trailing 0s in the hash”.

Example of a hash that does not meet the difficulty level defined above, i.e. 'two trailing 0s': 

In this case, nonce serves as an arbitrary string or a counter that can be adjusted so that the overall hash meets the difficulty level.

Only the nonce can be adjusted to change the output hash in attempts to meet a preset difficulty level

If the current_block_hash does not meet the difficulty level, the nonce is adjusted, and a new hash is computed. This repeats until a hash that satisfies the condition set by difficulty level is fulfilled.

At present, only brute force works in determining PoW, i.e. nonce is adjusted and hash recalculated till the difficulty level is satisfied. This is why miners of Bitcoin employ special purpose computers to calculate PoW. Once done, this PoW is easy to verify. Thus, the first miner to determine the correct PoW propagates the block across the network. Other miners verify correctness (by having their own algorithm to check), accept the valid block and begin mining the next block.

With PoW, consensus is achieved without the need of a central trusted authority. There is no need for a bank, regulator or middleman to verify a block (PoW does this) nor the authenticity of transactions in it (hashes achieve it) in Bitcoin.

Moreover, the bad actor I wrote about above will have extra challenge controlling the blockchain. He would need to not only update the hash of several blocks, but also outpace other nodes in the network in having his block accepted in a decentralised, distributed blockchain. It can only be done if the bad actor is able to mount a majority attack, which becomes exponentially harder as the blockchain grows in a large network of nodes.

To understand implementation details, delve into the original Bitcoin white paper written by Satoshi Nakamoto. Or check out the book, Mastering Bitcoin by Andreas M. Antonopoulos.

What kind of blockchains are there?

A blockchain can be public, private, permissioned, permissionless, centralised, decentralised.

All of these are configurations that can be set up based on the network’s requirements. At this point, governments and corporations are trying out their own ways of using DLT in a centralised/decentralised manner to improve tracking goods in a supply chain, settle trades better, cut duplicate insurance claims, improve cross-border payments and so on. Variations of the blockchain are being experimented upon.

What next?

So far, through code, we have seen what a block is, how hashing is used and its significance in a blockchain. Within a block, how currency/token or a contract can be implemented is also shown briefly via the data variable. You can also see how middlemen are not required in a decentralized peer-to-peer network.

The brevity of this article, while hardly doing justice to the full power of blockchain technology, is intentional. What this does is help convert some jargon into meaningful terms that we can use to build our understanding of the revolution happening around us. From here, you may like to re-engage the news or think deeply about how businesses can benefit from using distributed ledgers. Like you, I will be building on from here too and sharing more over time.

If you would like to dive deeper, I’ve also compiled a curated list of resources on where to go next in the references section below. I hope this tutorial has been useful to you!


Source code (written in Ruby): Github.
Where it all began in 2008: Bitcoin White Paper
More on Bitcoin: Mastering Bitcoin, Programming the Open Blockchain
Next major blockchain since Bitcoin: Ethereum White Paper
More on Hashing: Blockgeeks
Truth About Blockchain: From HBR

Pramodh is the Chief Product Officer at Funding Societies | Modalku.

Topics of interest

More Related Stories