From 17eb0d9c15d70090d8190efe3483fb06150c1ced Mon Sep 17 00:00:00 2001 From: Justin Langston Date: Mon, 24 Sep 2018 16:41:28 -0400 Subject: [PATCH] make update wallet more robust --- .../bitcore-node/src/models/walletAddress.ts | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/packages/bitcore-node/src/models/walletAddress.ts b/packages/bitcore-node/src/models/walletAddress.ts index e8c50975088..97ad7b26d0d 100644 --- a/packages/bitcore-node/src/models/walletAddress.ts +++ b/packages/bitcore-node/src/models/walletAddress.ts @@ -69,21 +69,18 @@ export class WalletAddress extends BaseModel { const { walletUpdates, coinUpdates } = updates; const { chain, network } = wallet; - let walletUpdateBatches = partition(walletUpdates, walletUpdates.length/8); - let coinUpdateBatches = partition(coinUpdates, coinUpdates.length/8); + let walletUpdateBatches = partition(walletUpdates, 1000); + let coinUpdateBatches = partition(coinUpdates, 1000); return new Promise(async resolve => { - await Promise.all( - walletUpdateBatches.map(walletUpdateBatch => { - return WalletAddressModel.collection.bulkWrite(walletUpdateBatch, { ordered: false }); - }) - ); + for (const walletUpdateBatch of walletUpdateBatches) { + await WalletAddressModel.collection.bulkWrite(walletUpdateBatch, { ordered: false }); + } + + for (const coinUpdateBatch of coinUpdateBatches) { + await CoinModel.collection.bulkWrite(coinUpdateBatch, { ordered: false }); + } - await Promise.all( - coinUpdateBatches.map(coinUpdateBatch => { - return CoinModel.collection.bulkWrite(coinUpdateBatch, { ordered: false }); - }) - ); let coins = await CoinModel.collection.find({ wallets: wallet._id }, { batchSize: 100 }).project({ spentTxid: 1, mintTxid: 1 }).toArray(); let txids = {}; for (let coin of coins) { @@ -98,11 +95,10 @@ export class WalletAddress extends BaseModel { } } }); - await Promise.all( - partition(txUpdates, txUpdates.length/16).map(txUpdate => { - return TransactionModel.collection.bulkWrite(txUpdate, { ordered: false }); - }) - ) + const txUpdateBatches = partition(txUpdates, 1000); + for (const txUpdate of txUpdateBatches) { + await TransactionModel.collection.bulkWrite(txUpdate, { ordered: false }); + } resolve(); }); }