DEV Community

loizenai
loizenai

Posted on

How to Synchronize Blockchain Network in JavaScript

https://grokonez.com/node-js/how-to-synchronize-blockchain-network-javascript-tutorial

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.


    app.post('/transaction/broadcast', function (req, res) {
    const transaction = bitcoin.makeNewTransaction(...);
    bitcoin.addTransactionToPendingTransactions(transaction);

    // forEach(nodeUrl in this.networkNodes)
    // POST 'nodeUrl/transaction' - body: transaction
    });

app.post('/add-block', function (req, res) {
// validate Block by req.body.NewBlock & latestBlock

if (validateBlockOk) {
    bitcoin.chain.push(block);
    bitcoin.pendingTransactions = [];
}
Enter fullscreen mode Exit fullscreen mode

});

app.get('/mine', function (req, res) {
...
const nonce = bitcoin.proofOfWork(prevBlockHash, currentBlockData);
const blockHash = bitcoin.hashBlock(prevBlockHash, currentBlockData, nonce);

// create new Block

// forEach(nodeUrl in this.networkNodes)
// POST 'nodeUrl/add-block' - body: newBlock

// reward for mining when done by
// POST '/transaction/broadcast' body: reward_transaction
Enter fullscreen mode Exit fullscreen mode

});

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.

More at:

https://grokonez.com/node-js/how-to-synchronize-blockchain-network-javascript-tutorial

How to Synchronize Blockchain Network in JavaScript

Top comments (0)