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 = [];
}

});

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.

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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 11 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 9 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!