Skip to content

Commit f2d6ec9

Browse files
perf(api): transaction list performance enhancements
1 parent a66b28b commit f2d6ec9

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

packages/bitcore-node/src/models/coin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Coin extends BaseModel<ICoin> {
7272
this.collection.createIndex({ spentTxid: 1 }, { background: true, sparse: true });
7373
this.collection.createIndex({ chain: 1, network: 1, spentHeight: 1 }, { background: true });
7474
this.collection.createIndex({ wallets: 1, spentHeight: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } });
75+
this.collection.createIndex({ wallets: 1, spentTxid: 1, mintIndex: 1, address: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } });
76+
this.collection.createIndex({ wallets: 1, mintTxid: 1, mintIndex: 1, address: 1, value: 1 }, { background: true, partialFilterExpression: { 'wallets.0': { $exists: true } } });
7577
}
7678

7779
getBalance(params: { query: any }) {

packages/bitcore-node/src/providers/chain-state/internal/transforms.ts

+19-25
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@ export class ListTransactionsStream extends Transform {
88
}
99

1010
async _transform(transaction, _, done) {
11-
const [ inputs, outputs ] = await Promise.all([
12-
CoinModel.collection
13-
.find(
14-
{
15-
chain: transaction.chain,
16-
network: transaction.network,
17-
spentTxid: transaction.txid
18-
},
19-
{ batchSize: 10000 }
20-
)
21-
.project({ address: 1, wallets: 1, value: 1, mintIndex: 1})
22-
.addCursorFlag('noCursorTimeout', true)
23-
.toArray(),
24-
CoinModel.collection
11+
const sending = !! await CoinModel.collection.count({
12+
wallets: this.wallet._id,
13+
'wallets.0': { $exists: true },
14+
spentTxid: transaction.txid
15+
});
16+
17+
const wallet = this.wallet._id!.toString();
18+
19+
if (sending) {
20+
const outputs = await CoinModel.collection
2521
.find(
2622
{
2723
chain: transaction.chain,
@@ -32,17 +28,7 @@ export class ListTransactionsStream extends Transform {
3228
)
3329
.project({ address: 1, wallets: 1, value: 1, mintIndex: 1 })
3430
.addCursorFlag('noCursorTimeout', true)
35-
.toArray()
36-
]);
37-
38-
const wallet = this.wallet._id!.toString();
39-
const sending = inputs.some((input) => {
40-
return input.wallets.some((inputWallet) => {
41-
return inputWallet.equals(wallet);
42-
});
43-
});
44-
45-
if (sending) {
31+
.toArray();
4632
outputs.forEach((output) => {
4733
const sendingToOurself = output.wallets.some((outputWallet) => {
4834
return outputWallet.equals(wallet);
@@ -93,6 +79,14 @@ export class ListTransactionsStream extends Transform {
9379
}
9480
return done();
9581
} else {
82+
const outputs = await CoinModel.collection.find({
83+
wallets: this.wallet._id,
84+
'wallets.0': { $exists: true },
85+
mintTxid: transaction.txid
86+
})
87+
.project({ address: 1, wallets: 1, value: 1, mintIndex: 1 })
88+
.addCursorFlag('noCursorTimeout', true)
89+
.toArray();
9690
outputs.forEach((output) => {
9791
const weReceived = output.wallets.some((outputWallet) => {
9892
return outputWallet.equals(wallet);

0 commit comments

Comments
 (0)