How To Create BlockChain From Scratch

Part -1: Understanding the basics of BlockChain

Daniel Bharath
Technology at Nineleaps
4 min readJan 15, 2018

--

In this series we will explore the internals of blockchain by building a coin called TRCoin (T-Rex Coin) from scratch. We will simplify most of the things like complexity, algorithm choices etc. (Bear with me if you encounter new terminologies).

Credits : http://prehistoricearth.wikia.com/wiki/File:Tyrannosaurus-rex-skeleton-model_635.jpg

Basically, a blockchain is a series of blocks with some data, linked with a chain, the chain being the hash of the previous block. The entire blockchain would exist on each one of the node that wants to interact with it, meaning it is copied on each one of the nodes in the network. So, no single server hosts it, which makes it decentralized. Yes, you can download the entire Bitcoin blockchain close to 150Gb* (as on December 2017)

Let’s build the chain first

So, what is this hash? Think of hash being a magical box! When we put in some object/text into the box, it would return a unique finger print. The magical box is so perfect that even a minuscule change in the input object /text would change the finger print. There are different hashing algorithms, for simplicity we will be using “SHA-1”. To generate a sample hash check here.

If we input a text like “Hello World”, it generates the below hash (hexadecimal string):

Now let’s add a comma to “Hello World,

The hash changes drastically by just adding a simple comma.

Therefore, in a blockchain, the chain is built by passing the block data into a hashing algorithm that would generate a hash, which is linked to the next block, henceforth, forming a series of blocks linked with the hashes of the previous blocks.

If we try to tamper data in one of the blocks, it will change the hash value, which is equivalent to breaking the chain, this then propagates to the subsequent blocks breaking all the links. To rebuild the entire chain with the new tampered data is an expensive task. Moreover, the change has to be propagated to the entire network, i.e., to all the copies of blockchain in every node. This brings mutation, an expensive operation to blockchain. We will also see in the next posts that consensus algorithms like proof of work will further add even more difficulty to re-chain a blockchain, making it almost immutable (51% attack).

Let’s build the Block now!

Every blockchain has a Genesis block which is the first block in the chain. We can take a look at the Genesis block in Bitcoin network here. For simplicity, our TRCoin Block will contain only few attributes and we will add more as we further improve our chain in the upcoming posts.

We’ll explore the attributes briefly:

Now, with the block and the chain, let’s create a simple blockchain.

Let’s create the Genesis block for TRCoin.

The Genesis block will contain these attributes. Some of the them are default values; for simplicity currHash is calculated by combining all the attributes.

so currHash is defined as SHA-1 of all the other attributes

Now let’s create the second block. For this post let’s ignore consensus/proof of work for which the nonce and target attributes will be used.

The blockchain looks something like this

We have built our basic blockchain, now let’s go ahead and build another block and add it to the chain. In the next post will we add Gossip to the blockchain so that it will start talking and synchronizing

Source code for building this block chain can be found below.
java, python, javascript

References

[1] Andreas M. Antonopoulos, “Mastering Bitcoin” https://github.com/bitcoinbook/bitcoinbook

[2] Satoshi Nakamoto, “Bitcoin: A Peer-to-Peer Electronic Cash System”, https://bitcoin.org/bitcoin.pdf

[3] Let’s build a blockchain! — A mini-cryptocurrency in Ruby https://www.youtube.com/watch?v=3aJI1ABdjQk

--

--