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
/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./add-block
: validate Block and add this Block to Blockchain./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 = [];
}
});
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
});
To understand deeply, please look at diagrams below.
transaction/broadcast
endpoint with Transaction data./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
a new Block./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
#blockchain #javascript #synchronizez #network