From bf1850a6eab1508c64333bebe9ee83427f5f5abd Mon Sep 17 00:00:00 2001 From: David Katz <15Dkatz@shcp.edu> Date: Wed, 28 Sep 2022 12:11:51 -0700 Subject: [PATCH] Sync Transaction Pool Map on Connect --- index.js | 13 +++++++++++-- wallet/transaction-pool.js | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 348bf49..5be0e30 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,7 @@ app.get('/api/transaction-pool-map', (req, res) => { res.json(transactionPool.transactionMap); }); -const syncChains = () => { +const syncWithRootState = () => { request({ url: `${ROOT_NODE_ADDRESS}/api/blocks` }, (error, response, body) => { if (!error && response.statusCode === 200) { const rootChain = JSON.parse(body); @@ -67,6 +67,15 @@ const syncChains = () => { blockchain.replaceChain(rootChain); } }); + + request({ url: `${ROOT_NODE_ADDRESS}/api/transaction-pool-map` }, (error, response, body) => { + if (!error && response.statusCode === 200) { + const rootTransactionPoolMap = JSON.parse(body); + + console.log('replace transaction pool map on a sync with', rootTransactionPoolMap); + transactionPool.setMap(rootTransactionPoolMap); + } + }); }; let PEER_PORT; @@ -80,6 +89,6 @@ app.listen(PORT, () => { console.log(`listening at localhost:${PORT}`); if (PORT !== DEFAULT_PORT) { - syncChains(); + syncWithRootState(); } }); diff --git a/wallet/transaction-pool.js b/wallet/transaction-pool.js index 39db4b8..7ee3def 100644 --- a/wallet/transaction-pool.js +++ b/wallet/transaction-pool.js @@ -7,6 +7,10 @@ class TransactionPool { this.transactionMap[transaction.id] = transaction; } + setMap(transactionMap) { + this.transactionMap = transactionMap; + } + existingTransaction({ inputAddress }) { const transactions = Object.values(this.transactionMap);