Merge pull request #402 from mempool/simon/mempool-lazy-delete

Flag transactions for lazy deletion.
This commit is contained in:
softsimon 2021-03-21 14:58:22 +07:00 committed by GitHub
commit c93adba276
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 20 deletions

View File

@ -10,6 +10,7 @@ import loadingIndicators from './loading-indicators';
class Mempool { class Mempool {
private static WEBSOCKET_REFRESH_RATE_MS = 10000; private static WEBSOCKET_REFRESH_RATE_MS = 10000;
private static LAZY_DELETE_AFTER_SECONDS = 30;
private inSync: boolean = false; private inSync: boolean = false;
private mempoolCache: { [txId: string]: TransactionExtended } = {}; private mempoolCache: { [txId: string]: TransactionExtended } = {};
private mempoolInfo: IBitcoinApi.MempoolInfo = { loaded: false, size: 0, bytes: 0, usage: 0, private mempoolInfo: IBitcoinApi.MempoolInfo = { loaded: false, size: 0, bytes: 0, usage: 0,
@ -27,6 +28,7 @@ class Mempool {
constructor() { constructor() {
setInterval(this.updateTxPerSecond.bind(this), 1000); setInterval(this.updateTxPerSecond.bind(this), 1000);
setInterval(this.deleteExpiredTransactions.bind(this), 20000);
} }
public isInSync(): boolean { public isInSync(): boolean {
@ -145,7 +147,6 @@ class Mempool {
}, 1000 * 60 * config.MEMPOOL.CLEAR_PROTECTION_MINUTES); }, 1000 * 60 * config.MEMPOOL.CLEAR_PROTECTION_MINUTES);
} }
let newMempool = {};
const deletedTransactions: TransactionExtended[] = []; const deletedTransactions: TransactionExtended[] = [];
if (this.mempoolProtection !== 1) { if (this.mempoolProtection !== 1) {
@ -154,35 +155,31 @@ class Mempool {
const transactionsObject = {}; const transactionsObject = {};
transactions.forEach((txId) => transactionsObject[txId] = true); transactions.forEach((txId) => transactionsObject[txId] = true);
// Replace mempool to separate deleted transactions // Flag transactions for lazy deletion
for (const tx in this.mempoolCache) { for (const tx in this.mempoolCache) {
if (transactionsObject[tx]) { if (!transactionsObject[tx] && !this.mempoolCache[tx].deleteAfter) {
newMempool[tx] = this.mempoolCache[tx];
} else {
deletedTransactions.push(this.mempoolCache[tx]); deletedTransactions.push(this.mempoolCache[tx]);
this.mempoolCache[tx].deleteAfter = new Date().getTime() + Mempool.LAZY_DELETE_AFTER_SECONDS * 1000;
} }
} }
} else {
newMempool = this.mempoolCache;
} }
const newTransactionsStripped = newTransactions.map((tx) => Common.stripTransaction(tx)); const newTransactionsStripped = newTransactions.map((tx) => Common.stripTransaction(tx));
this.latestTransactions = newTransactionsStripped.concat(this.latestTransactions).slice(0, 6); this.latestTransactions = newTransactionsStripped.concat(this.latestTransactions).slice(0, 6);
if (!this.inSync && transactions.length === Object.keys(newMempool).length) { if (!this.inSync && transactions.length === Object.keys(this.mempoolCache).length) {
this.inSync = true; this.inSync = true;
logger.info('The mempool is now in sync!'); logger.info('The mempool is now in sync!');
loadingIndicators.setProgress('mempool', 100); loadingIndicators.setProgress('mempool', 100);
} }
if (this.mempoolChangedCallback && (hasChange || deletedTransactions.length)) { if (this.mempoolChangedCallback && (hasChange || deletedTransactions.length)) {
this.mempoolCache = newMempool;
this.mempoolChangedCallback(this.mempoolCache, newTransactions, deletedTransactions); this.mempoolChangedCallback(this.mempoolCache, newTransactions, deletedTransactions);
} }
const end = new Date().getTime(); const end = new Date().getTime();
const time = end - start; const time = end - start;
logger.debug(`New mempool size: ${Object.keys(newMempool).length} Change: ${diff}`); logger.debug(`New mempool size: ${Object.keys(this.mempoolCache).length} Change: ${diff}`);
logger.debug('Mempool updated in ' + time / 1000 + ' seconds'); logger.debug('Mempool updated in ' + time / 1000 + ' seconds');
} }
@ -198,6 +195,16 @@ class Mempool {
); );
} }
} }
private deleteExpiredTransactions() {
const now = new Date().getTime();
for (const tx in this.mempoolCache) {
const lazyDeleteAt = this.mempoolCache[tx].deleteAfter;
if (lazyDeleteAt && lazyDeleteAt < now) {
delete this.mempoolCache[tx];
}
}
}
} }
export default new Mempool(); export default new Mempool();

View File

@ -194,10 +194,15 @@ class WebsocketHandler {
mempoolBlocks.updateMempoolBlocks(newMempool); mempoolBlocks.updateMempoolBlocks(newMempool);
const mBlocks = mempoolBlocks.getMempoolBlocks(); const mBlocks = mempoolBlocks.getMempoolBlocks();
const mempool = memPool.getMempool();
const mempoolInfo = memPool.getMempoolInfo(); const mempoolInfo = memPool.getMempoolInfo();
const vBytesPerSecond = memPool.getVBytesPerSecond(); const vBytesPerSecond = memPool.getVBytesPerSecond();
const rbfTransactions = Common.findRbfTransactions(newTransactions, deletedTransactions); const rbfTransactions = Common.findRbfTransactions(newTransactions, deletedTransactions);
for (const rbfTransaction in rbfTransactions) {
delete mempool[rbfTransaction];
}
this.wss.clients.forEach(async (client: WebSocket) => { this.wss.clients.forEach(async (client: WebSocket) => {
if (client.readyState !== WebSocket.OPEN) { if (client.readyState !== WebSocket.OPEN) {
return; return;
@ -329,28 +334,23 @@ class WebsocketHandler {
throw new Error('WebSocket.Server is not set'); throw new Error('WebSocket.Server is not set');
} }
// Check how many transactions in the new block matches the latest projected mempool block
// If it's more than 0, recalculate the mempool blocks and send to client in the same update
let mBlocks: undefined | MempoolBlock[]; let mBlocks: undefined | MempoolBlock[];
let matchRate = 0; let matchRate = 0;
const _memPool = memPool.getMempool();
const _mempoolBlocks = mempoolBlocks.getMempoolBlocksWithTransactions(); const _mempoolBlocks = mempoolBlocks.getMempoolBlocksWithTransactions();
if (_mempoolBlocks[0]) { if (_mempoolBlocks[0]) {
const matches: string[] = []; const matches: string[] = [];
for (const txId of txIds) { for (const txId of txIds) {
if (_mempoolBlocks[0].transactionIds.indexOf(txId) > -1) { if (_mempoolBlocks[0].transactionIds.indexOf(txId) > -1) {
matches.push(txId); matches.push(txId);
} }
delete _memPool[txId];
} }
matchRate = Math.round((matches.length / (txIds.length - 1)) * 100); matchRate = Math.round((matches.length / (txIds.length - 1)) * 100);
if (matchRate > 0) { mempoolBlocks.updateMempoolBlocks(_memPool);
const currentMemPool = memPool.getMempool(); mBlocks = mempoolBlocks.getMempoolBlocks();
for (const txId of matches) {
delete currentMemPool[txId];
}
mempoolBlocks.updateMempoolBlocks(currentMemPool);
mBlocks = mempoolBlocks.getMempoolBlocks();
}
} }
block.matchRate = matchRate; block.matchRate = matchRate;

View File

@ -30,6 +30,7 @@ export interface TransactionExtended extends IEsploraApi.Transaction {
ancestors?: Ancestor[]; ancestors?: Ancestor[];
bestDescendant?: BestDescendant | null; bestDescendant?: BestDescendant | null;
cpfpChecked?: boolean; cpfpChecked?: boolean;
deleteAfter?: number;
} }
interface Ancestor { interface Ancestor {