Skip to content

Commit bbfabac

Browse files
author
Micah Riggan
committed
Fixing import script and adding a catch to the loop
1 parent 71b6c08 commit bbfabac

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

packages/bitcore-client/bin/wallet-import

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const getFileKeys = file => {
3131
const parseKeys = keys => {
3232
let parsed = JSON.parse(keys);
3333
if (typeof parsed === 'object') {
34-
if (!parsed instanceof Array ) {
34+
if ((parsed instanceof Array) === false ) {
3535
return [parsed];
3636
} else {
3737
return parsed;

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

+26-11
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,27 @@ export class WalletAddressModel extends BaseModel<IWalletAddress> {
4141
const unprocessedAddresses: Array<string> = [];
4242
return new Promise(async resolve => {
4343
for (let address of addresses) {
44-
const updatedAddress = await this.collection.findOneAndUpdate({
45-
wallet: wallet._id, address: address, chain, network
46-
}, { $setOnInsert: { wallet: wallet._id, address: address, chain, network }}, { returnOriginal: false, upsert: true });
47-
if (!updatedAddress.value!.processed) {
48-
unprocessedAddresses.push(address);
49-
await CoinStorage.collection.updateMany(
50-
{ chain, network, address },
51-
{ $addToSet: { wallets: wallet._id } }
44+
try {
45+
const updatedAddress = await this.collection.findOneAndUpdate(
46+
{
47+
wallet: wallet._id,
48+
address: address,
49+
chain,
50+
network
51+
},
52+
{ $setOnInsert: { wallet: wallet._id, address: address, chain, network } },
53+
{ returnOriginal: false, upsert: true }
5254
);
55+
if (!updatedAddress.value!.processed) {
56+
unprocessedAddresses.push(address);
57+
await CoinStorage.collection.updateMany(
58+
{ chain, network, address },
59+
{ $addToSet: { wallets: wallet._id } }
60+
);
61+
}
62+
} catch (err) {
63+
// Perhaps a race condition from multiple calls around the same time
64+
console.error('Likely an upsert race condition in walletAddress updates');
5365
}
5466
}
5567

@@ -60,7 +72,7 @@ export class WalletAddressModel extends BaseModel<IWalletAddress> {
6072
let txids = {};
6173
coinStream.on('data', (coin: ICoin) => {
6274
coinStream.pause();
63-
if (!unprocessedAddresses.includes(coin.address)){
75+
if (!unprocessedAddresses.includes(coin.address)) {
6476
return coinStream.resume();
6577
}
6678
if (!txids[coin.mintTxid]) {
@@ -80,8 +92,11 @@ export class WalletAddressModel extends BaseModel<IWalletAddress> {
8092
return coinStream.resume();
8193
});
8294
coinStream.on('end', async () => {
83-
for (const address of unprocessedAddresses){
84-
await this.collection.updateOne({ chain, network, address, wallet: wallet._id }, { $set: {processed: true }});
95+
for (const address of unprocessedAddresses) {
96+
await this.collection.updateOne(
97+
{ chain, network, address, wallet: wallet._id },
98+
{ $set: { processed: true } }
99+
);
85100
}
86101
resolve();
87102
});

0 commit comments

Comments
 (0)