How to build a Decentralized Blockchain Network in Javascript

In previous post, we have a single Blockchain and the only way to access it is through API from a single server. It is centralized and not good because this API totally controls the whole Blockchain with all data inside it. Today we will build a decentralized Blockchain network that has many different instances of the API. So instead of just having our Blockchain hosted on a single centralized node, it is hosted across a decentralized network which will be very powerful because of the security: we don’t have to trust only one single entity.

Previous Post: How to create Blockchain API in Javascript

Next Post: How to Synchronize Blockchain Network in JavaScript

Overview

To create a network, we need a way of registering these nodes.
So we will make 3 endpoints:

– POST /register-node: registers a node with specific node.
– POST /register-bulk-nodes: registers a node with multiple nodes at once.
– POST /register-and-broadcast-node will register a node and broadcast that node to the entire network with node’s url on the body.

*Note: In this example, ‘register’ nodeA with nodeB means add nodeA’s nodeUrl to nodeB’s networkNodes array.

To understand deeply, please look at diagram below:

build-decentralized-blockchain-network-javascript-outline

Assume that we want to register node5 to the network. node1 has already connected to node2, node3, node4 before.
=> We just use /register-and-broadcast-node, everything will be done.

So, the process can be explained as below:
1- First, we register node5 with node1 using /register-and-broadcast-node. node5 will be add to node1 networkNodes array.
2- Next, node1 automatically broadcasts node5 to all nodes inside networkNodes array (node2, node3, node4) using /register-node.
3- Finally, node1 calls /register-bulk-nodes endpoint of node5 to register all nodes inside its networkNodes array (including itself) with node5. node5 now connects to node1, node2, node3, node4.

Practice

Setup Environment

This was our project folder before:

build-blockchain-api-javascript-structure

Install UUID

Because our network contains many nodes, each node should have its own address. We can generate theses address by UUID.
Run command: npm install uuid.

Inside app.js, import uuid and use:

Install Request-Promise

request-promise is simplified HTTP request client ‘request’ with Promise support.
Run command: npm install request-promise.

Inside app.js, import request-promise:

We will use it to POST data to a Rest API:

We also need request to work with request-promise.
Run command: npm install request.

Create multiple nodes

We have multiple instances of API and each of them acts as a network node.
>> We will run this api.js file on different ports.

Open package.json file, add these script:

To access to the port and url variable:

Add Network Nodes to Blockchain

Open blockchain.js, add nodeUrl field and networkNodes array to constructor method of Blockchain class:

Create Endpoints
Register Node

Get nodeUrl, then put it in networkNodes:

Register Nodes in Bulk

Receive list of Nodes in networkNodes, add each item in the list to networkNodes.

Register and Broadcast Node

– Register new Node with itself.
– Broadcast that new Node to all the other Nodes that are already present in current Node’s networkNodes.
– Register all of the nodes that are currently inside of networkNodes (including current Node) with the new Node.

Run and Check results

– Run the API on each Terminal with command:
npm run node1
npm run node2
npm run node3
npm run node4
npm run node5

– Register node2, node3, node4 with node1:

build-decentralized-blockchain-network-javascript-register-node

– Open url http://localhost:3001/blockchain to check node1 networkNodes:

build-decentralized-blockchain-network-javascript-register-node-result

– Register and broadcast node5:

build-decentralized-blockchain-network-javascript-register-broadcast-node

– Open url http://localhost:3001/blockchain and view node1 with node5 insides networkNodes:

build-decentralized-blockchain-network-javascript-register-broadcast-node-result-1

– To check node2, node3, node4, open url http://localhost:3002/blockchain / http://localhost:3003/blockchain / http://localhost:3004/blockchain, we can see node5 has been added:

build-decentralized-blockchain-network-javascript-register-broadcast-node-result-3

– Open url http://localhost:3005/blockchain, all Nodes in Network have registered with node5:

build-decentralized-blockchain-network-javascript-register-broadcast-node-result-5

Source Code

Blockchain-decentralizedNetwork

By grokonez | June 4, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*