Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BCN] Fix long invalid chains #3683

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/bitcore-node/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function shouldFire(obj: { wallets?: Array<ObjectID> }) {
return !onlyWalletEvents || (onlyWalletEvents && obj.wallets && obj.wallets.length > 0);
}
const MAX_BATCH_SIZE = 50000;
const MAX_DESCENDANTS = 25;

export type IBtcTransaction = ITransaction & {
coinbase: boolean;
Expand Down Expand Up @@ -678,6 +679,10 @@ export class TransactionModel extends BaseTransaction<IBtcTransaction> {
invalidParentTxids?: string[] // empty at the beginning of the ancestral tree.
}) {
const { chain, network, invalidTxid, replacedByTxid, invalidParentTxids = [] } = params;
if (invalidParentTxids.length > MAX_DESCENDANTS) {
return;
}

const spentOutputsQuery = {
chain,
network,
Expand Down
6 changes: 4 additions & 2 deletions packages/bitcore-node/src/services/pruning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const args = parseArgv([], [
{ arg: 'DRY', type: 'bool' },
{ arg: 'MEMPOOL_AGE', type: 'int' },
{ arg: 'INTERVAL_HRS', type: 'float' },
{ arg: 'DESCENDANT_LIMIT', type: 'int' }
{ arg: 'DESCENDANT_LIMIT', type: 'int' },
{ arg: 'PRINT_MEMPOOL', type: 'bool' }
]);

const ONE_MIN = 1000 * 60;
Expand All @@ -32,6 +33,7 @@ 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;
const PRINT_MEMPOOL = args.PRINT_MEMPOOL || false;

// 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 @@ -140,7 +142,7 @@ export class PruningService {
try {
const nodeTx: RPCTransaction = await this.rpcs[`${chain}:${network}`].getTransaction(tx.txid);
if (nodeTx) {
logger.warn(`Tx ${tx.txid} is still in the mempool: %o`, nodeTx);
logger.warn(`Tx ${tx.txid} is still in the mempool${PRINT_MEMPOOL ? ': %o' : ''}`, nodeTx);
return cb();
}
} catch (err: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ describe('Transaction Model', function() {

const badTxs = await TransactionStorage.collection.find({ chain, network, txid: { $in: txids } }).toArray();
expect(badTxs.length).to.eq(chainLength);
expect(badTxs.map(tx => tx.blockHeight)).to.deep.eq(new Array(chainLength).fill(SpentHeightIndicators.conflicting));
expect(badTxs.slice(0, 25).every(tx => tx.blockHeight === SpentHeightIndicators.conflicting)).to.eq(true);
expect(badTxs.slice(26).every(tx => tx.blockHeight === SpentHeightIndicators.pending)).to.eq(true);

const goodTxs = await TransactionStorage.collection.find({ chain, network, txid: blockTx.txid }).toArray();
expect(goodTxs.length).to.eq(1);
Expand Down