fix postgres locking issue which could lead to rare double send

This commit is contained in:
Josh Harvey 2016-02-07 02:40:20 +02:00
parent 5cfc2ba491
commit 5b5fc8b810
2 changed files with 10 additions and 5 deletions

View file

@ -28,13 +28,21 @@ db.addOutgoingTx(session, tx, function (err, res) {
console.log(res) console.log(res)
}) })
db.addOutgoingTx(session, tx2, function (err, res) {
console.log('DEBUG2')
console.log(err)
console.log(res)
})
/*
setTimeout(function () { setTimeout(function () {
db.addOutgoingTx(session, tx2, function (err, res) { db.addOutgoingTx(session, tx2, function (err, res) {
console.log('DEBUG2') console.log('DEBUG2')
console.log(err) console.log(err)
console.log(res) console.log(res)
}) })
}, 1000) }, 0)
*/
var bills = { var bills = {
uuid: 'c630338c-cd03-11e5-a9df-dbc9be2e9fbb', uuid: 'c630338c-cd03-11e5-a9df-dbc9be2e9fbb',

View file

@ -181,7 +181,6 @@ function billsAndTxs(client, session, cb) {
} }
function computeSendAmount(tx, totals) { function computeSendAmount(tx, totals) {
console.log('DEBUG10')
var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat; var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat;
var satoshisRemaining = (tx.satoshis || totals.billsSatoshis) - var satoshisRemaining = (tx.satoshis || totals.billsSatoshis) -
totals.txSatoshis; totals.txSatoshis;
@ -304,8 +303,6 @@ function insertTx(client, session, incoming, tx, satoshis, fiat, stage,
tx.error tx.error
]; ];
console.log('DEBUG11')
query(client, getInsertQuery('transactions', fields, true), values, query(client, getInsertQuery('transactions', fields, true), values,
function(err, results) { function(err, results) {
if (err) return cb(err); if (err) return cb(err);
@ -350,7 +347,7 @@ exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) {
if (cerr) return cb(cerr); if (cerr) return cb(cerr);
async.series([ async.series([
async.apply(silentQuery, client, 'BEGIN'), async.apply(silentQuery, client, 'BEGIN'),
async.apply(silentQuery, client, 'LOCK TABLE transactions NOWAIT'), async.apply(silentQuery, client, 'LOCK TABLE transactions'),
async.apply(insertOutgoingCompleteTx, client, session, tx), async.apply(insertOutgoingCompleteTx, client, session, tx),
async.apply(removePendingTx, client, session), async.apply(removePendingTx, client, session),
async.apply(buildOutgoingTx, client, session, tx) async.apply(buildOutgoingTx, client, session, tx)