How to Synchronize Blockchain Network in JavaScript

In previous post, we built a Decentralized Blockchain Network that contained some Nodes, each Node was aware of all others. Today we will synchronize this network so that Blockchain in all Nodes are the same every time mining is done.

Previous Post: How to build a Decentralized Blockchain Network in Javascript

Next Post: How to build Consensus Algorithm for Blockchain Network in Javascript

Synchronize Blockchain Network

To synchronize our network, we need a way of broadcasting transactions to all Nodes, then we also need a way to synchronize new Block data to all Nodes.
So we will make 3 endpoints:

– POST /transaction/broadcast: create new Transaction and broadcast it to all other Nodes in the Blockchain network so that every single Node inside network will have the same exact transaction data and the whole network will effectively be synchronized.
– POST /add-block: validate Block and add this Block to Blockchain.
– GET /mine: create new Block by doing Proof Of Work, broadcast the new Block to all other Nodes using /add-block endpoint, make a mining reward transaction and broadcast it to the entire Blockchain network using /transaction/broadcast endpoint.

To understand deeply, please look at diagrams below.

Make a Transaction synchonized

synchronize-blockchain-network-javascript-broadcast-transaction

– Whenever we want to create a new Transaction, we are going to hit transaction/broadcast endpoint with Transaction data.
– New Transaction will be broadcast automatically with /transaction endpoint, and all Nodes inside of Node’s network will have the same exact Transaction data.

That’s all we have to do to synchronize Transaction data.

Mine – make all Blocks synchronized

synchronize-blockchain-network-javascript-broadcast-mine

– We choose one Node to /mine a new Block.
– After the Block is created, it will be broadcast automatically to all Nodes inside of Node’s network with /add-block endpoint. So the entire network will be synchronized and they will all be hosting the same Blockchain.

Practice

Transaction Methods

Our Transaction now contains id field.
We will split makeNewTransaction() method to 2 methods:

Endpoints
Transaction

Broadcast Transaction

Add Block

Mine

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, node4 with node1:

synchronize-blockchain-network-javascript-broadcast-register-nodes

– Open url http://localhost:3001/blockchain to check node1 network:

synchronize-blockchain-network-javascript-broadcast-node1-network

– Mine with url http://localhost:3002/mine:

synchronize-blockchain-network-javascript-broadcast-node2-mine

– Open url http://localhost:3001/blockchain / http://localhost:3002/blockchain / http://localhost:3003/blockchain / http://localhost:3004/blockchain to check:
+ new Block in chain
+ mining award in pendingTransactions

synchronize-blockchain-network-javascript-broadcast-node3-data-mine

– Broadcast new Transactions:

synchronize-blockchain-network-javascript-broadcast-transaction-node1

– Open a Blockchain to check pendingTransactions (it is the same as other Blockchain):

synchronize-blockchain-network-javascript-broadcast-node2-after-transactions

– Mine with url http://localhost:3004/mine:

synchronize-blockchain-network-javascript-broadcast-node4-mine

– Check Blockchain data:

synchronize-blockchain-network-javascript-broadcast-node1-after-mine

Source Code

Blockchain-synchronize-blockchain-network

By grokonez | June 9, 2018.



Related Posts


Got Something To Say:

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

*