Skip to content

Commit

Permalink
Sync Transaction Pool Map on Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 28, 2022
1 parent ca8a5b7 commit bf1850a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -80,6 +89,6 @@ app.listen(PORT, () => {
console.log(`listening at localhost:${PORT}`);

if (PORT !== DEFAULT_PORT) {
syncChains();
syncWithRootState();
}
});
4 changes: 4 additions & 0 deletions wallet/transaction-pool.js
Expand Up @@ -7,6 +7,10 @@ class TransactionPool {
this.transactionMap[transaction.id] = transaction;
}

setMap(transactionMap) {
this.transactionMap = transactionMap;
}

existingTransaction({ inputAddress }) {
const transactions = Object.values(this.transactionMap);

Expand Down

0 comments on commit bf1850a

Please sign in to comment.