How to build a Decentralized Blockchain Network in Javascript

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.

app.post('/register-node', function (req, res) {
    const nodeUrl = req.body.nodeUrl;

    // add nodeUrl to this.networkNodes
}

app.post('/register-bulk-nodes', function (req, res) {
    const networkNodes = req.body.networkNodes;

    // forEach(nodeUrl in networkNodes)
    // add nodeUrl to this.networkNodes
}

app.post('/register-and-broadcast-node', function (req, res) {
    const nodeUrl = req.body.nodeUrl;
    // add nodeUrl to this.networkNodes

    // forEach(node in this.networkNodes)
    // POST /register-node to node
    // with body: { nodeUrl }

    // POST /register-bulk-nodes to nodeUrl
    // with body: { [ this.networkNodes, this.nodeUrl ] }
}

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:

const uuid = require('uuid/v1');
const nodeAddr = uuid();

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:

const reqPromise = require('request-promise');

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

const requestOptions = {
    uri: 'http://api.blockchain.com/register-node',
    method: 'POST',
    body: { nodeUrl: nodeUrl },
    json: true
}

reqPromise(requestOptions)
    .then(function () {
        // ...
    })
    .catch(function (err) {
        // ...
    });

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:

{
  "name": "blockchain",
  "version": "1.0.0",
  "description": "JSA blockchain demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "node1": "nodemon --watch src -e js src/api.js 3001 http://localhost:3001",
    "node2": "nodemon --watch src -e js src/api.js 3002 http://localhost:3002",
    "node3": "nodemon --watch src -e js src/api.js 3003 http://localhost:3003",
    "node4": "nodemon --watch src -e js src/api.js 3004 http://localhost:3004",
    "node5": "nodemon --watch src -e js src/api.js 3005 http://localhost:3005"
  },
  "author": "JavaSampleApproach",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "nodemon": "^1.17.5",
    "request": "^2.87.0",
    "request-promise": "^4.2.2",
    "sha256": "^0.2.0",
    "uuid": "^3.2.1"
  },
  "keywords": [
    "jsa-blockchain"
  ]
}

To access to the port and url variable:

// api.js
const port = process.argv[2];
app.listen(port, function () {
    console.log(`> listening on port ${port}...`);
});

// blockchain.js
const nodeUrl = process.argv[3];

Add Network Nodes to Blockchain

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

const nodeUrl = process.argv[3];

class Blockchain {
    constructor() {
        this.chain = [];
        this.pendingTransactions = [];

        this.nodeUrl = nodeUrl;
        this.networkNodes = [];

        this.creatNewBlock(100, '0', 'Genesis block');
    }
    ...
}

Create Endpoints

Register Node

Get nodeUrl, then put it in networkNodes:

app.post('/register-node', function (req, res) {
    const nodeUrl = req.body.nodeUrl;

    if ((bitcoin.networkNodes.indexOf(nodeUrl) == -1)
        && (bitcoin.nodeUrl !== nodeUrl)) {
        bitcoin.networkNodes.push(nodeUrl);

        res.json(
            {
                message: 'A node registers successfully!'
            }
        );
    }
    else {
        res.json(
            {
                message: 'This node cannot register!'
            }
        );
    }
});

Register Nodes in Bulk

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

app.post('/register-bulk-nodes', function (req, res) {
    const networkNodes = req.body.networkNodes;

    networkNodes.forEach(nodeUrl => {
        if ((bitcoin.networkNodes.indexOf(nodeUrl) == -1)
            && (bitcoin.nodeUrl !== nodeUrl)) {
            bitcoin.networkNodes.push(nodeUrl);
        }
    });

    res.json(
        {
            message: 'Registering bulk successfully!'
        }
    );
});

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.

app.post('/register-and-broadcast-node', function (req, res) {
    const nodeUrl = req.body.nodeUrl;

    if (bitcoin.networkNodes.indexOf(nodeUrl) == -1) {
        bitcoin.networkNodes.push(nodeUrl);
    }

    const registerNodes = [];
    bitcoin.networkNodes.forEach(networkNode => {
        const requestOptions = {
            uri: networkNode + '/register-node',
            method: 'POST',
            body: { nodeUrl: nodeUrl },
            json: true
        }

        registerNodes.push(reqPromise(requestOptions));
    });

    Promise.all(registerNodes)
        .then(data => {
            const bulkRegisterOptions = {
                uri: nodeUrl + '/register-bulk-nodes',
                method: 'POST',
                body: { networkNodes: [...bitcoin.networkNodes, bitcoin.nodeUrl] },
                json: true
            }

            return reqPromise(bulkRegisterOptions);
        }).then(data => {
            res.json(
                {
                    message: 'A node registers with network successfully!'
                }
            );
        });
});

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

0 0 votes
Article Rating
Subscribe
Notify of
guest
49 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments