How to build a Decentralized Blockchain Network in Javascript » grokonez

How to build a Decentralized Blockchain Network in Javascript » grokonez

Build a decentralized Blockchain network with many different instances of API. Blockchain is hosted across a decentralized network - decentralized blockchain network javascript tutorial - decentralized blockchain network javascript example

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

decentralized blockchain network javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Blockchain Certification | Blockchain Training Course | Blockchain Council

In all the market sectors, Blockchain technology has contributed to the redesign. The improvements that were once impossible have been pushed forward. Blockchain is one of the leading innovations with the ability to influence the various sectors...

5 Blockchain Applications That Have Transformed the World of Technology

The blockchain is the decentralized database of the blocks of information, which gets recorded in the chain format and linked in a secured crypto graphical manner. This technology ensures proper safety of the data due to its secure nature, and it totally changes how people carry out transactions. It also brings about a faster and secure process of validating information needed to establish reliability.

How to Synchronize Blockchain Network in JavaScript » grokonez

Today we synchronize network so that Blockchain in all Nodes are the same every time mining is done - Synchronize Blockchain Network Javascript tutorial

Building a Decentralized Application (DApp) on the Ethereum Blockchain with JavaScript and Solidity

In this blog, I build a decentralized application, also known as a “dapp”. While there are various definitions of a dapp, in general they are applications that run on a decentralized peer-to-peer network, as opposed to a client-server network, and are governed by all the members, as opposed to a single entity that centrally owns the underlying servers and databases. In Ethereum, a dapp is backed by what is known as a “smart contract” containing the logic of the dapp. This makes a dapp running on the Ethereum blockchain potentially unstoppable as it won’t go down because the logic is replicated across many peers in the network. The smart contract backing our dapp is written in Solidity, an object-oriented, high-level language for implementing smart contracts on blockchain platforms. Our dapp will run locally on Ganache which we will interact with via a Node.js application using the web3.js library, a JavaScript library that provides an API to interact with an Ethereum blockchain. In this post, you'll see Building a Decentralized Application (DApp) on the Ethereum Blockchain with JavaScript and Solidity. Decentralized application with Node.js, web3.js, Solidity, and Ganache

How to explore Data inside of Blockchain Network in Javascript » grokonez

API to explore data Blockchain Network - search for a specific Block, Transaction, fetch data for specific Address - Blockchain network Javascript Tutorial