diff --git a/class/User.js b/class/User.js index 51c730d..d4b6060 100644 --- a/class/User.js +++ b/class/User.js @@ -294,8 +294,8 @@ export class User { * @returns {Promise} */ async accountForPosibleTxids() { - let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1); let onchain_txs = await this.getTxs(); + let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1); for (let tx of onchain_txs) { if (tx.type !== 'bitcoind_tx') continue; let already_imported = false; @@ -304,6 +304,16 @@ export class User { } if (!already_imported && tx.category === 'receive') { + let locked = await this._redis.get('importing_' + tx.txid); + if (locked) { + // race condition, someone's already importing this tx + return; + } + + // locking... + await this._redis.set('importing_' + tx.txid, 1); + await this._redis.expire('importing_' + tx.txid, 3600); + let userBalance = await this.getBalance(); userBalance += new BigNumber(tx.amount).multipliedBy(100000000).toNumber(); await this.saveBalance(userBalance); diff --git a/doc/schema.md b/doc/schema.md index c115e58..7c645d1 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -9,6 +9,7 @@ User storage schema * access_token_for_{userid} = {access_token} * userid_for_{refresh_token} = {userid} * refresh_token_for_{userid} = {access_token} +* importing_{txid} = 1 `atomic lock when processing topup tx`