How to build a Decentralized Blockchain Network in Javascript
Tags: javascript, blockchain, decentralized
Published: 2021-05-19 05:48:52

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: <a href="https://grokonez.com/frontend/javascript/how-to-create-blockchain-api-javascript-example">How to create Blockchain API in Javascript</a>

Next Post: <a href="https://grokonez.com/frontend/javascript/how-to-synchronize-blockchain-network-javascript-tutorial">How to Synchronize Blockchain Network in JavaScript</a>

<!--more-->
<h2>Overview</h2>
To create a network, we need a way of registering these nodes.
So we will make 3 endpoints:

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

<strong>*Note:</strong> In this example, 'register' nodeA with nodeB means add nodeA's <code>nodeUrl</code> to nodeB's <code>networkNodes</code> array.
<pre><code class="language-javascript">
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 ] }
}
</code></pre>
To understand deeply, please look at diagram below:

<img src="https://grokonez.com/wp-content/uploads/2018/06/build-decentralized-blockchain-network-javascript-outline.png" alt="build-decentralized-blockchain-network-javascript-outline" width="700" height="310" class="alignnone size-full wp-image-12809" />

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

So, the process can be explained as below:
1- First, we register <strong>node5</strong> with <strong>node1</strong> using <code>/register-and-broadcast-node</code>. <strong>node5</strong> will be add to <strong>node1</strong> <code>networkNodes</code> array.
2- Next, <strong>node1</strong> automatically broadcasts <strong>node5</strong> to all nodes inside <code>networkNodes</code> array (<strong>node2</strong>, <strong>node3</strong>, <strong>node4</strong>) using <code>/register-node</code>.
3- Finally, <strong>node1</strong> calls <code>/register-bulk-nodes</code> endpoint of <strong>node5</strong> to register all nodes inside its <code>networkNodes</code> array (including itself) with <strong>node5</strong>. <strong>node5</strong> now connects to <strong>node1</strong>, <strong>node2</strong>, <strong>node3</strong>, <strong>node4</strong>.
<h2>Practice</h2>
<h3>Setup Environment</h3>
This was our project folder before:

<img src="https://grokonez.com/wp-content/uploads/2018/05/build-blockchain-api-javascript-structure.png" alt="build-blockchain-api-javascript-structure" width="204" height="213" class="alignnone size-full wp-image-12770" />
<h4>Install UUID</h4>
Because our network contains many nodes, each node should have its own address. We can generate theses address by UUID.
Run command: <code>npm install uuid</code>.

Inside <em><strong>app.js</strong></em>, import <code>uuid</code> 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