FIX: race condition when processing onchain topup

This commit is contained in:
Overtorment 2019-01-05 21:27:34 +00:00
parent 51b0f89fd1
commit 6a3de700f4
2 changed files with 12 additions and 1 deletions

View File

@ -294,8 +294,8 @@ export class User {
* @returns {Promise<void>}
*/
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);

View File

@ -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`