Skip to content

Commit

Permalink
perf(api): transaction list performance enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Dec 11, 2018
1 parent a66b28b commit f2d6ec9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
2 changes: 2 additions & 0 deletions packages/bitcore-node/src/models/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Coin extends BaseModel<ICoin> {
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 }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f2d6ec9

Please sign in to comment.