Principle of Blockchain Technology the naive way!

Pankaj
366Pi Consulting
Published in
5 min readAug 19, 2018

Hola to all the Blockchain enthusiasts out there.

“The blockchain is an incorruptible digital ledger of economic transactions that can be programmed to record not just financial transactions but virtually everything of value.”
Don & Alex Tapscott, authors Blockchain Revolution (2016)

This article is all about “Being able to understand the basic principle of Blockchain Technology and build a simple javascript based model of blockchain called ‘Naivechain’. we shall also see a simulation of how to create nodes and mine blocks.

For ease of learning we shall see the simulation of the project https://github.com/lhartikk/naivechain

In order to simulate and run the above project one should either work under Linux or windows.

Also you will need ‘node.js’,’npm’ and ‘curl’ running in your machine.

This simulation involves few basic steps:-

1. Fork the project

> go to https://github.com/lhartikk/naivechain

and click on the ‘fork’ button on the top right corner to get a branch to work on!

https://github.com/lhartikk/naivechain
fork count increased by 1

2.Check and make sure that git , npm ,curl,node are installed.

> you can run the following commands on your terminal or command prompt to check if all the above components are present in your machine.

node -v

npm -v

curl -V // v with caps on.

if you get an error you could refer to this for steps to install node.

you should select unix command tools while installing git.

you could use git bash if you are under windows.

3. get a clone of the “Naivechain” from github.

>go to your github profile and select “Naivechain”.

> click the green button “clone or download” and copy the address as per the mode (you could use either https or ssh).

4.Building the project on local machine.

> create a directory for the project .Let's name it ‘SimpleMiner’ or any of your choice.

> Right click and open terminal or git bash.

> clone the github repository using the command git clone followed by the link copied in the step 3.

> cd naivechain.

>Type ‘npm install’ to get the npm packages.

>Open the project ‘naivechain’ using a standard editor like sublime or atom.

>Open the file “main.js” and go through the code.

> for better understanding of the code, you could see this

It should be noted that the node actually exposes two web servers. one for the user to control the node(HTTP server) and one for the Peer to Peer communication between the nodes(websocket HTTP server).

In this model, the user is able to interact with the node in the following ways:

>List all the blocks.

>Create a new block with a given content.

>List or add peers.

The easiest way to control node is using Curl.

you can read more about Curl here.

5.Setup Connected Nodes and Mine a Block

>we shall establish the FIRST Node.

>Get back to terminal or command prompt or git bash and type.

npm install
HTTP_PORT=3001 P2P_PORT=6001 npm start

>The node established will listen for signals from another nodes by websocket interface on port 6001 and will listen for commands via HTTP interface on port 3001.

>we can see the node’s information on the address

http://localhost:3001/blocks

5.we can see the “list of all blocks” on the above link.

6.Now we shall set the “Second peer” in chain.

>Open a new terminal window and type.

HTTP_PORT=3002 P2P_PORT=6002 PEERS=ws://localhost:6001 npm start

The second peer will listen for signals from another nodes on port 6002 and will listen for commands via HTTP interface on port 3002.

This node will receive information by first peer via P2P communication by port 6001.

6.Let us mine a new block

Open terminal and type

curl -H "Content-type:application/json" --data '{"data" : "Some data to the first block"}' http://localhost:3001/mineBlock

using the above, we command first node to mine a new block.

its index will be index of previous block+1.

We could add some content to the block.(“some data to the first block”).

status after a new block is mined by first node.

we could possibly add more blocks.

7.Querying the Blockchain

we could use

curl http://localhost:3001/blocks

to get information on the blockchain.

we could add as many blocks as we want and add peers and also through the above simulation , it can be seen that the change is reflected across all nodes currently on the network.

That’s it guys you are now ready to rock!!

Thanks to all who came to read this post. I hope this was a one-stop guide for all the people who are starting to explore the exciting journey to the blockchain world.

If you found this post useful, then ‘give me some claps :P’ and share your knowledge by sharing this post.

And I may update this post in future, so suggestions are most welcomed even if we have never met. I am quite friendly to people. :D

(If you found any mistake in this post, please inform me. Be it a grammatical, or a spelling mistake, or a technical mistake)

--

--