From d8bd2aacdfe1e7a8059441d4a4ec5ca174260667 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Mon, 7 Jan 2019 17:25:04 +0000 Subject: [PATCH] FIX: better locking when importing onchain tx --- class/User.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/class/User.js b/class/User.js index d4b6060..fcb9912 100644 --- a/class/User.js +++ b/class/User.js @@ -1,3 +1,5 @@ +import { Lock } from './Lock'; + var crypto = require('crypto'); var lightningPayReq = require('bolt11'); import { BigNumber } from 'bignumber.js'; @@ -304,20 +306,18 @@ 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 + // first, locking... + let lock = new Lock(this._redis, 'importing_' + tx.txid); + if (!(await lock.obtainLock())) { + // 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); await this._redis.rpush('imported_txids_for_' + this._userid, tx.txid); + await lock.releaseLock(); } } }