From f2d6ec90591ea66b67f0fd00a31e8c277014930b Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Tue, 11 Dec 2018 12:20:30 -0500 Subject: [PATCH] perf(api): transaction list performance enhancements --- packages/bitcore-node/src/models/coin.ts | 2 + .../chain-state/internal/transforms.ts | 44 ++++++++----------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/bitcore-node/src/models/coin.ts b/packages/bitcore-node/src/models/coin.ts index 28844317122..55bd5f5af54 100644 --- a/packages/bitcore-node/src/models/coin.ts +++ b/packages/bitcore-node/src/models/coin.ts @@ -72,6 +72,8 @@ class Coin extends BaseModel { this.collection.createIndex({ spentTxid: 1 }, { background: true, sparse: true }); this.collection.createIndex({ chain: 1, network: 1, spentHeight: 1 }, { background: true }); this.collection.createIndex({ wallets: 1, spentHeight: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } }); + this.collection.createIndex({ wallets: 1, spentTxid: 1, mintIndex: 1, address: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } }); + this.collection.createIndex({ wallets: 1, mintTxid: 1, mintIndex: 1, address: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } }); } getBalance(params: { query: any }) { diff --git a/packages/bitcore-node/src/providers/chain-state/internal/transforms.ts b/packages/bitcore-node/src/providers/chain-state/internal/transforms.ts index 13accbc1429..d7289c34cc3 100644 --- a/packages/bitcore-node/src/providers/chain-state/internal/transforms.ts +++ b/packages/bitcore-node/src/providers/chain-state/internal/transforms.ts @@ -8,20 +8,16 @@ export class ListTransactionsStream extends Transform { } async _transform(transaction, _, done) { - const [ inputs, outputs ] = await Promise.all([ - CoinModel.collection - .find( - { - chain: transaction.chain, - network: transaction.network, - spentTxid: transaction.txid - }, - { batchSize: 10000 } - ) - .project({ address: 1, wallets: 1, value: 1, mintIndex: 1}) - .addCursorFlag('noCursorTimeout', true) - .toArray(), - CoinModel.collection + const sending = !! await CoinModel.collection.count({ + wallets: this.wallet._id, + 'wallets.0': { $exists: true }, + spentTxid: transaction.txid + }); + + const wallet = this.wallet._id!.toString(); + + if (sending) { + const outputs = await CoinModel.collection .find( { chain: transaction.chain, @@ -32,17 +28,7 @@ export class ListTransactionsStream extends Transform { ) .project({ address: 1, wallets: 1, value: 1, mintIndex: 1 }) .addCursorFlag('noCursorTimeout', true) - .toArray() - ]); - - const wallet = this.wallet._id!.toString(); - const sending = inputs.some((input) => { - return input.wallets.some((inputWallet) => { - return inputWallet.equals(wallet); - }); - }); - - if (sending) { + .toArray(); outputs.forEach((output) => { const sendingToOurself = output.wallets.some((outputWallet) => { return outputWallet.equals(wallet); @@ -93,6 +79,14 @@ export class ListTransactionsStream extends Transform { } return done(); } else { + const outputs = await CoinModel.collection.find({ + wallets: this.wallet._id, + 'wallets.0': { $exists: true }, + mintTxid: transaction.txid + }) + .project({ address: 1, wallets: 1, value: 1, mintIndex: 1 }) + .addCursorFlag('noCursorTimeout', true) + .toArray(); outputs.forEach((output) => { const weReceived = output.wallets.some((outputWallet) => { return outputWallet.equals(wallet);