BYOB — Build Your Own Blockchain

Kumar Anirudha
Blockchained India
Published in
5 min readSep 25, 2018

--

Image Courtesy: Google

One of the ultimate buzz words of the decade, blockchain has indefinitely altered not only the data and security industry, but across multiple domains. Often, the amazing distributed database, powered by a decentralised network, incentivised by a sophisticated cryptocurrency or token designed for the network is very much difficult to understand or build for that matter. So, here, we take a shot at Building Your Own Blockchain.

What is a Blockchain?

A blockchain, is a growing list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data (generally represented as a merkle tree root hash).
For all intend and purpose, we’ll keep it simple and focus on proof of work consensus based public blockchain.

A Proof-Of-Work based Bitcoin Blockchain example

By design, a blockchain is resistant to modification of the data, hence called immutable. It is “an open, distributed ledger that can record transactions between two parties efficiently and in a verifiable and permanent way”. For use as a distributed ledger, a blockchain is typically managed by a peer-to-peer network collectively adhering to a protocol for inter-node communication and validating new blocks. Once recorded, the data in any given block cannot be altered retroactively without alteration of all subsequent blocks, which requires consensus of the network majority. Although blockchain records are not unalterable, blockchains may be considered secure by design and exemplify a distributed computing system with high Byzantine fault tolerance. Decentralised consensus has therefore been claimed with a blockchain. Read more about blockchain introduction here and here.

Public vs Permissioned vs Private

Blockchain can be primarily categorised into three types- public, permissioned and private.

Public blockchains are completely open data blockchain networks where all the data and signatures are put up on.

Permissioned blockchains are open data public blockchain networks but the validators or miners to the network are authorised or voted on.

Private blockchains are closed data blockchain networks, where they might further be divided to a private organisational blockchain network or a consortium of private organisations.

Each type of blockchain has there own pros and cons depending on the type of the task to be done.

Properties of Blockchain

Blockchain is simply an open distributed record of immutable data. So, it’s worth saying the properties of blockchain can be defined as follows:

  1. Decentralised Network.
  2. Open transparent distributed database.
  3. Immutable Data.
  4. Consensus.
Pic Courtesy: Deloitte

Read more about the properties of blockchain here.

Consensus

One of the most important things in a decentralised network is how to agree on the data distributed amongst all to reach a consensus. To solve this, various algorithms have been introduced, most famous of which is Proof of Work, which is most widely used currently.
We won’t be covering various consensus algorithms at the moment.

Currently, there are various other consensus algorithms as well.

All we’ve to do is generate a block, it’s hash as the signature hash and attach to a previous block.
So first, let’s create a new block with these keys:

  1. index: Shows the index of the blockchain
  2. timestamp: The exact timestamp of the block writing. Very critical while resolving conflicts.
  3. transactions: The transactions in the block.
  4. signature: The Proof of Work generates a unique hash. Let’s call it signature as it showcases the unique signature of the block.
  5. previous_block_hash: The hash of the previous block. This is used to create the linked list.

Alright, now that we’ve defined a block, let’s go a step further to define a transaction data. Ideally, a lot more data is supposed to be there, but for simplicity of understanding, let’s have the following data in a transaction:

  1. sender: The address of the sender.
  2. recipient: The address of the recipient.
  3. amount: The amount to be sent. (Please note that we have not defined any unit for this amount yet. The unit will defined by the token supporting the blockchain.)

Awesome! Now, we’ve defined all the data. Let’s start our blockchain node.

Running a node

Let’s try by creating a simple node (centralised) at this point which will help other nodes to connect to later on. Refer to the blockchain.py file.

Now, we create a small Flask app to run our blockchain from web browser. It’s not required but makes the interactions easier via our blockchain enabled APIs.

Find the entire code on the github repo here.

Run the code by running:
python blockchain.py
and go to http://localhost:8000 or replace the port 8000 with your own port.

At this point, you should be seeing something like this.
So, everything seems to be working.

Open the chain url by clicking on the link given.
You should be see the genesis block generated with the data as explained above.

Open a new terminal window/tab and run another on another port:
python blockchain.py -p 8001
Repeat the same operation as above to test the exclusiveness of the nodes.

Mining

Mining, in the context of blockchain technology, is the process of adding transactions to the large distributed public ledger of existing transactions, known as the blockchain.
Below is shown how a typical mining/validation works.

So, let’s create a few new blocks and add to our blockchain.
Run the url /mine(assuming you’re running on 8000 port or replace the url with your your port) on your browser.

Now, head back to /chain and view all the new blocks that we just mined.

At this stage, we’ve successfully created a blockchain, mined new blocks and added to our blockchain. But, our different ports are not synced with each other. We’re yet to define our consensus algorithm and sync all the nodes.
We’ll be covering these on the next article series.

--

--

Kumar Anirudha
Blockchained India

Developer, Entrepreneur, Evangelist. Working on DLT, Blockchain, Tangle