DEV Community

loizenai
loizenai

Posted on

How to build a Decentralized Blockchain Network in Javascript

https://grokonez.com/node-js/how-to-build-a-decentralized-blockchain-network-javascript-example

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:

More at:

https://grokonez.com/node-js/how-to-build-a-decentralized-blockchain-network-javascript-example

How to build a Decentralized Blockchain Network in Javascript

Top comments (0)