How to Synchronize Blockchain Network in JavaScript
Tags: javascript, blockchain, synchronize, network
Published: 2021-05-19 05:27:58

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

How to Synchronize Blockchain Network in JavaScript

In <a href="https://grokonez.com/frontend/javascript/how-to-build-a-decentralized-blockchain-network-javascript">previous post</a>, 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: <a href="https://grokonez.com/frontend/javascript/how-to-build-a-decentralized-blockchain-network-javascript">How to build a Decentralized Blockchain Network in Javascript</a>

Next Post: <a href="https://grokonez.com/frontend/javascript/how-to-build-consensus-algorithm-blockchain-network-javascript-tutorial">How to build Consensus Algorithm for Blockchain Network in Javascript</a>

<!--more-->
<h2>Synchronize Blockchain Network</h2>
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 <code>/transaction/broadcast</code>: 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 <code>/add-block</code>: validate Block and add this Block to Blockchain.
- GET <code>/mine</code>: create new Block by doing Proof Of Work, broadcast the new Block to all other Nodes using <code>/add-block</code> endpoint, make a mining reward transaction and broadcast it to the entire Blockchain network using <code>/transaction/broadcast</code> endpoint.
<pre><code class="language-javascript">
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 = [];
    }
});

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
});</code></pre>

To understand deeply, please look at diagrams below.

<h3>Make a Transaction synchonized</h3>
<img src="https://grokonez.com/wp-content/uploads/2018/06/synchronize-blockchain-network-javascript-broadcast-transaction.png" alt="synchronize-blockchain-network-javascript-broadcast-transaction" width="651" height="329" class="alignnone size-full wp-image-12901" />

- Whenever we want to create a new Transaction, we are going to hit <code>transaction/broadcast</code> endpoint with Transaction data.
- New Transaction will be broadcast automatically with <code>/transaction</code> 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.
<h3>Mine - make all Blocks synchronized</h3>
<img src="https://grokonez.com/wp-content/uploads/2018/06/synchronize-blockchain-network-javascript-broadcast-mine.png" alt="synchronize-blockchain-network-javascript-broadcast-mine" width="688" height="400" class="alignnone size-full wp-image-12905" />

- We choose one Node to <code>/mine</code> a new Block.
- After the Block is created, it will be broadcast automatically to all Nodes inside of Node's network with <code>/add-block</code> 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