Skip to content

Commit 7f2ad9f

Browse files
author
Micah Riggan
committed
fix(node): removing unneeded properties
using an indexer to absorb extra properties from params
1 parent ab37830 commit 7f2ad9f

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

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

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,28 @@ export class Transaction extends BaseModel<ITransaction> {
6464
logger.debug('Minting Coins', mintOps.length);
6565
if (mintOps.length) {
6666
await Promise.all(
67-
partition(mintOps, mintOps.length / config.maxPoolSize)
68-
.map(mintBatch => CoinModel.collection.bulkWrite(mintBatch, { ordered: false }))
67+
partition(mintOps, mintOps.length / config.maxPoolSize).map(mintBatch =>
68+
CoinModel.collection.bulkWrite(mintBatch, { ordered: false })
69+
)
6970
);
7071
}
71-
72+
7273
logger.debug('Spending Coins', spendOps.length);
7374
if (spendOps.length) {
7475
await Promise.all(
75-
partition(spendOps, spendOps.length / config.maxPoolSize)
76-
.map(spendBatch => CoinModel.collection.bulkWrite(spendBatch, { ordered: false }))
76+
partition(spendOps, spendOps.length / config.maxPoolSize).map(spendBatch =>
77+
CoinModel.collection.bulkWrite(spendBatch, { ordered: false })
78+
)
7779
);
7880
}
79-
81+
8082
if (mintOps) {
8183
const txOps = await this.addTransactions({ ...params, mintOps });
8284
logger.debug('Writing Transactions', txOps.length);
8385
await Promise.all(
84-
partition(txOps, txOps.length / config.maxPoolSize)
85-
.map(txBatch => this.collection.bulkWrite(txBatch, { ordered: false }))
86+
partition(txOps, txOps.length / config.maxPoolSize).map(txBatch =>
87+
this.collection.bulkWrite(txBatch, { ordered: false })
88+
)
8689
);
8790
}
8891
}
@@ -99,11 +102,13 @@ export class Transaction extends BaseModel<ITransaction> {
99102
chain: string;
100103
network: string;
101104
mintOps: Array<any>;
102-
mempoolTime?: Date
105+
mempoolTime?: Date;
103106
}) {
104107
let { blockHash, blockTime, blockTimeNormalized, chain, height, network, parentChain, forkHeight } = params;
105108
if (parentChain && forkHeight && height < forkHeight) {
106-
const parentTxs = await TransactionModel.collection.find({blockHeight: height, chain: parentChain, network}).toArray();
109+
const parentTxs = await TransactionModel.collection
110+
.find({ blockHeight: height, chain: parentChain, network })
111+
.toArray();
107112
return parentTxs.map(parentTx => {
108113
return {
109114
updateOne: {
@@ -125,13 +130,19 @@ export class Transaction extends BaseModel<ITransaction> {
125130
}
126131
},
127132
upsert: true,
128-
forceServerObjectId: true
133+
forceServerObjectId: true
129134
}
130135
};
131136
});
132137
} else {
133-
const spentQuery = height > 0 ? { spentHeight: height, chain, network } : { spentTxid: { $in: params.txs.map(tx => tx._hash) }, chain, network };
134-
const spent = await CoinModel.collection.find(spentQuery).project({ spentTxid: 1, value: 1, wallets: 1 }).toArray();
138+
const spentQuery =
139+
height > 0
140+
? { spentHeight: height, chain, network }
141+
: { spentTxid: { $in: params.txs.map(tx => tx._hash) }, chain, network };
142+
const spent = await CoinModel.collection
143+
.find(spentQuery)
144+
.project({ spentTxid: 1, value: 1, wallets: 1 })
145+
.toArray();
135146
type CoinGroup = { [txid: string]: { total: number; wallets: Array<ObjectID> } };
136147
const groupedMints = params.mintOps.reduce<CoinGroup>((agg, coinOp) => {
137148
const mintTxid = coinOp.updateOne.filter.mintTxid;
@@ -162,7 +173,7 @@ export class Transaction extends BaseModel<ITransaction> {
162173
return agg;
163174
}, {});
164175

165-
let txOps = params.txs.map((tx) => {
176+
let txOps = params.txs.map(tx => {
166177
const txid = tx._hash!;
167178
const minted = groupedMints[txid] || {};
168179
const spent = groupedSpends[txid] || {};
@@ -172,6 +183,7 @@ export class Transaction extends BaseModel<ITransaction> {
172183
const wallets = lodash.uniqBy(txWallets, wallet => wallet.toHexString());
173184
let fee = 0;
174185
if (groupedMints[txid] && groupedSpends[txid]) {
186+
// TODO: Fee is negative for mempool txs
175187
fee = groupedSpends[txid].total - groupedMints[txid].total;
176188
if (fee < 0) {
177189
console.error(txid, groupedSpends[txid], groupedMints[txid]);
@@ -225,11 +237,9 @@ export class Transaction extends BaseModel<ITransaction> {
225237
chain: parentChain,
226238
network,
227239
mintHeight: height,
228-
$or: [
229-
{ spentHeight: { $lt: SpentHeightIndicators.minimum }},
230-
{ spentHeight: { $gte: forkHeight }},
231-
]
232-
}).project({mintTxid:1, mintIndex: 1})
240+
$or: [{ spentHeight: { $lt: SpentHeightIndicators.minimum } }, { spentHeight: { $gte: forkHeight } }]
241+
})
242+
.project({ mintTxid: 1, mintIndex: 1 })
233243
.toArray();
234244
for (const parentChainCoin of parentChainCoins) {
235245
parentChainCoinsMap.set(`${parentChainCoin.mintTxid}:${parentChainCoin.mintIndex}`, true);
@@ -239,7 +249,12 @@ export class Transaction extends BaseModel<ITransaction> {
239249
tx._hash = tx.hash;
240250
let isCoinbase = tx.isCoinbase();
241251
for (let [index, output] of tx.outputs.entries()) {
242-
if ((parentChain && forkHeight && height < forkHeight) && (!parentChainCoinsMap.size || !parentChainCoinsMap.get(`${tx._hash}:${index}`))) {
252+
if (
253+
parentChain &&
254+
forkHeight &&
255+
height < forkHeight &&
256+
(!parentChainCoinsMap.size || !parentChainCoinsMap.get(`${tx._hash}:${index}`))
257+
) {
243258
continue;
244259
}
245260
let address = '';
@@ -307,11 +322,7 @@ export class Transaction extends BaseModel<ITransaction> {
307322
chain: string;
308323
network: string;
309324
mintOps?: Array<any>;
310-
mempoolTime?: Date;
311-
blockTime?: Date;
312-
blockHash?: string;
313-
blockTimeNormalized?: Date;
314-
initialSyncComplete?: boolean
325+
[rest: string]: any;
315326
}): Array<any> {
316327
let { chain, network, height, parentChain, forkHeight } = params;
317328
let spendOps: any[] = [];

0 commit comments

Comments
 (0)