Skip to content

Commit

Permalink
Merge pull request bitpay#1998 from micahriggan/fix/verify-uses-txid-…
Browse files Browse the repository at this point in the history
…for-dupe-coins

Performance update
  • Loading branch information
micahriggan authored Feb 16, 2019
2 parents 7e5005e + 67c586f commit 96eda6f
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions packages/bitcore-node/test/verification/db-verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ export async function validateDataForBlock(blockNum: number, log = false) {

const seenTxCoins = {} as { [txid: string]: ICoin[] };
for (let tx of blockTxs) {
const coinsForTx = await CoinStorage.collection.find({ chain, network, mintTxid: tx.txid }).toArray();
for (let coin of coinsForTx) {
if (seenTxCoins[coin.mintTxid] && seenTxCoins[coin.mintTxid][coin.mintIndex]) {
success = false;
const error = { model: 'coin', err: true, type: 'DUPE_COIN', payload: { coin, blockNum } };
errors.push(error);
if (log) {
console.log(JSON.stringify(error));
}
} else {
seenTxCoins[coin.mintTxid] = seenTxCoins[coin.mintTxid] || {};
seenTxCoins[coin.mintTxid][coin.mintIndex] = coin;
}
}
if (tx.fee < 0) {
success = false;
const error = { model: 'transaction', err: true, type: 'NEG_FEE', payload: { tx, blockNum } };
Expand All @@ -50,6 +36,22 @@ export async function validateDataForBlock(blockNum: number, log = false) {
seenTxs[tx.txid] = tx;
}

const blockTxids = blockTxs.map(t => t.txid);
const coinsForTx = await CoinStorage.collection.find({ chain, network, mintTxid: { $in: blockTxids } }).toArray();
for (let coin of coinsForTx) {
if (seenTxCoins[coin.mintTxid] && seenTxCoins[coin.mintTxid][coin.mintIndex]) {
success = false;
const error = { model: 'coin', err: true, type: 'DUPE_COIN', payload: { coin, blockNum } };
errors.push(error);
if (log) {
console.log(JSON.stringify(error));
}
} else {
seenTxCoins[coin.mintTxid] = seenTxCoins[coin.mintTxid] || {};
seenTxCoins[coin.mintTxid][coin.mintIndex] = coin;
}
}

for (let txid of Object.keys(seenTxs)) {
const coins = seenTxCoins[txid];
if (!coins) {
Expand Down

0 comments on commit 96eda6f

Please sign in to comment.