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):

0a4d55a8d778e5022fab701977c5d840bbc486d0

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

821e5cbf98e81261a67265053c428348dc6966bf

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.

class TRBlock {

public int index; // The index of the block
public long timestamp; // Time stamp in epoch
public Object data; // The data we want to store
public String prevHash; // chain/hash of previous block
public String nonce; // String that needs to be mined
public int target; // Number of leading zeros
public String currHash; // Current hash

}

We’ll explore the attributes briefly:

1) index: indicates the index of the block ex: 0,12) timestamp: timestamp in epoch, number of seconds since 1 Jan 1970

3) data: the actual data that needs to be stored on blockchain
4) prevHash: the hash of the previous block, this is the chain/link between the blocks5) nonce: this is a magical number that is to be mined/found, we will explore about it in later posts6) target: number of leading zeros for the current hash, again we will explore about it in later posts7) currHash: The hash value of the current block, this is generated by combining all the above attributes and passing it to a hashing algorithm

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.

index : 0
timestamp: 1514528022
data : Hello World
prevHash: 000000000000000000000000000
nonce : Random nonce
target : 8
currHash : 4c04121b2bd8cc40793e98f640ad168190f58bc9

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

currHash = sha1(index+timestamp+data+prevHash+nonce)

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.

index : 1
timestamp: 1514530119
data : Second block
prevHash : 4c04121b2bd8cc40793e98f640ad168190f58bc9
nonce : Random nonce 2
target : 8
currHash : fbe680439c967840fa45a9161b27d0409e24e168

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

--

--