FIX: race condition when processing onchain topup
This commit is contained in:
parent
51b0f89fd1
commit
6a3de700f4
@ -294,8 +294,8 @@ export class User {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async accountForPosibleTxids() {
|
async accountForPosibleTxids() {
|
||||||
let imported_txids = await this._redis.lrange('imported_txids_for_' + this._userid, 0, -1);
|
|
||||||
let onchain_txs = await this.getTxs();
|
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) {
|
for (let tx of onchain_txs) {
|
||||||
if (tx.type !== 'bitcoind_tx') continue;
|
if (tx.type !== 'bitcoind_tx') continue;
|
||||||
let already_imported = false;
|
let already_imported = false;
|
||||||
@ -304,6 +304,16 @@ export class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!already_imported && tx.category === 'receive') {
|
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();
|
let userBalance = await this.getBalance();
|
||||||
userBalance += new BigNumber(tx.amount).multipliedBy(100000000).toNumber();
|
userBalance += new BigNumber(tx.amount).multipliedBy(100000000).toNumber();
|
||||||
await this.saveBalance(userBalance);
|
await this.saveBalance(userBalance);
|
||||||
|
@ -9,6 +9,7 @@ User storage schema
|
|||||||
* access_token_for_{userid} = {access_token}
|
* access_token_for_{userid} = {access_token}
|
||||||
* userid_for_{refresh_token} = {userid}
|
* userid_for_{refresh_token} = {userid}
|
||||||
* refresh_token_for_{userid} = {access_token}
|
* refresh_token_for_{userid} = {access_token}
|
||||||
|
* importing_{txid} = 1 `atomic lock when processing topup tx`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user