Skip to content

Commit

Permalink
Merge branch 'pruning' of https://github.com/kajoseph/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Nov 14, 2023
2 parents 96c27a1 + 38d9f3c commit 55e61b5
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/bitcore-node/src/services/pruning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import parseArgv from '../utils/parseArgv';
import '../utils/polyfills';
import { Config } from './config';

const { PRUNING_CHAIN, PRUNING_NETWORK, PRUNING_MEMPOOL_AGE, PRUNING_INTERVAL_HRS } = process.env;
const { PRUNING_CHAIN, PRUNING_NETWORK, PRUNING_MEMPOOL_AGE, PRUNING_INTERVAL_HRS, PRUNING_DESCENDANT_LIMIT } = process.env;
const args = parseArgv([], [
{ arg: 'CHAIN', type: 'string' },
{ arg: 'NETWORK', type: 'string' },
Expand All @@ -17,7 +17,8 @@ const args = parseArgv([], [
{ arg: 'EXIT', type: 'bool' },
{ arg: 'DRY', type: 'bool' },
{ arg: 'MEMPOOL_AGE', type: 'int' },
{ arg: 'INTERVAL_HRS', type: 'float' }
{ arg: 'INTERVAL_HRS', type: 'float' },
{ arg: 'DESCENDANT_LIMIT', type: 'int' }
]);

const ONE_MIN = 1000 * 60;
Expand All @@ -28,6 +29,7 @@ const CHAIN = args.CHAIN || PRUNING_CHAIN;
const NETWORK = args.NETWORK || PRUNING_NETWORK;
const INTERVAL_HRS = args.INTERVAL_HRS || Number(PRUNING_INTERVAL_HRS) || 12;
const MEMPOOL_AGE = args.MEMPOOL_AGE || Number(PRUNING_MEMPOOL_AGE) || 7;
const DESCENDANT_LIMIT = args.DESCENDANT_LIMIT || Number(PRUNING_DESCENDANT_LIMIT) || 10;

// If --DRY was given w/o a follow arg (i.e. 'true', '0', etc) assume the user wants to run a dry run (safe)
if (Object.keys(args).includes('DRY') && args.DRY === undefined) {
Expand Down Expand Up @@ -106,6 +108,7 @@ export class PruningService {
blockTimeNormalized: { $lt: oldTime }
});
logger.info(`Found ${count} outdated ${chain} ${network} mempool txs`);
let rmCount = 0;
await new Promise((resolve, reject) => {
this.transactionModel.collection
.find({ chain, network, blockHeight: -1, blockTimeNormalized: { $lt: oldTime } })
Expand All @@ -120,31 +123,31 @@ export class PruningService {
logger.info(`Finding ${tx.txid} outputs and dependent outputs`);
const outputGenerator = this.transactionModel.yieldRelatedOutputs(tx.txid);
let spentTxids = new Set<string>();
let count = 0;
for await (const coin of outputGenerator) {
if (coin.mintHeight >= 0 || coin.spentHeight >= 0) {
return cb(new Error(`Invalid coin! ${coin.mintTxid} `));
}
count++;
if (count > 50) {
throw new Error(`${tx.txid} has too many decendents`);
}
if (coin.spentTxid) {
spentTxids.add(coin.spentTxid);
if (spentTxids.size > DESCENDANT_LIMIT) {
logger.warn(`${tx.txid} has too many decendants`);
return cb();
}
}
}
spentTxids.add(tx.txid);
rmCount += spentTxids.size;
const uniqueTxids = Array.from(spentTxids);
await this.removeOldMempool(chain, network, uniqueTxids);
logger.info(`Removed ${tx.txid} transaction and ${count} dependent txs`);
cb();
logger.info(`Removed tx ${tx.txid} and ${spentTxids.size - 1} dependent txs`);
return cb();
}
})
)
.on('finish', resolve)
.on('error', reject);
});
logger.info(`Removed all old mempool txs within the last ${days} days`);
logger.info(`Removed all pending txs older than ${days} days: ${rmCount}`);
}

async processAllInvalidTxs(chain, network) {
Expand All @@ -168,23 +171,22 @@ export class PruningService {
logger.info(`Invalidating ${tx.txid} outputs and dependent outputs`);
const outputGenerator = this.transactionModel.yieldRelatedOutputs(tx.txid);
let spentTxids = new Set<string>();
let count = 0;
for await (const coin of outputGenerator) {
if (coin.mintHeight >= 0 || coin.spentHeight >= 0) {
return cb(new Error(`Invalid coin! ${coin.mintTxid} `));
}
count++;
if (count > 50) {
throw new Error(`${tx.txid} has too many decendents`);
}
if (coin.spentTxid) {
spentTxids.add(coin.spentTxid);
if (spentTxids.size > DESCENDANT_LIMIT) {
logger.warn(`${tx.txid} has too many decendants`);
return cb();
}
}
}
spentTxids.add(tx.txid);
const uniqueTxids = Array.from(spentTxids);
await this.clearInvalid(uniqueTxids);
logger.info(`Invalidated ${tx.txid} and ${count} dependent txs`);
logger.info(`Invalidated tx ${tx.txid} and ${spentTxids.size - 1} dependent txs`);
cb();
}
})
Expand Down

0 comments on commit 55e61b5

Please sign in to comment.