How to build Consensus Algorithm for Blockchain Network in Javascript

During broadcasting Transactions over our Blockchain network, a certain node may not receive a piece of information, or even there is a bad actor who sent out false information or created fraudulent transactions and broadcasts them to the whole network, convinces everybody that they are legitimate transactions. So, the Consensus Algorithm provides us a way to compare one Node to all the other Nodes inside of the network to confirm that it has the correct data. In this tutorial, we’re gonna create a Consensus Algorithm that implements the Longest Chain rule.

Previous Post: How to Synchronize Blockchain Network in JavaScript

Next Post: How to explore Data inside of Blockchain Network in Javascript

Consensus Algorithm

Longest Chain rule

This rule simply compares the chain of the chosen Node with all the other chains inside of our Blockchain network.
If one of the other chains has longer length than the current chain, we simply replace the chain of the Node we are on with the longest chain in the network.

Why we use Longest Chain rule?
>> The longest chain has the most Blocks in it and each of those Blocks was mined by using Proof of Work. We can assume that the whole network contributed to the longest chain because of how much work went into that chain.

Implement Longest Chain rule
Validate chain

This method validates a Blockchain by comparing all of the hashes of all of the Blocks inside of the chain.

Consensus Endpoint

We add an endpoint GET /consensus to our API. It will:
– make a request to every Node inside of our Blockchain network to get their copies of the Blockchain
– find the longest chain by comparing current chain with these chains
– use isChainValid() function to check if the longest chain found in previous step is valid
– if all things are validated, assign chain & transactions from the longest chain to this Blockchain

Practice

Validate Chain Method

Inside blockchain.js, add new method named isChainValid() to Blockchain class:

Consensus Endpoint

Inside api.js, add new endpoint GET /consensus:

Run & Check results

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

– Register and broadcast node2, node3 (but NOT node4) with node1:

consensus-algorithm-blockchain-network-javascript-broadcast-register-nodes

– Broadcast some Transactions and mine, then check a Node:

consensus-algorithm-blockchain-network-javascript-broadcast-check-nodes

– Show node4 data (remember that we haven’t register this Node with our Blockchain network):

consensus-algorithm-blockchain-network-javascript-broadcast-show-node-opening

– Register node4 with our Blockchain network using /register-and-broadcast-node:

consensus-algorithm-blockchain-network-javascript-broadcast-node4-register

– Run Consensus Algorithm on node4 with url http://localhost:3004/consensus:

consensus-algorithm-blockchain-network-javascript-broadcast-show-node-consensus

– Show node4 data after running Consensus Algorithm:

consensus-algorithm-blockchain-network-javascript-broadcast-check-node-after-consensus

Source Code

Blockchain-consensus-algorithm

By grokonez | June 13, 2018.



Related Posts


Got Something To Say:

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

*