fix some db bugs; clean up debugging
This commit is contained in:
parent
52a5bbd3de
commit
22f332aa7b
1 changed files with 39 additions and 22 deletions
|
|
@ -21,10 +21,13 @@ function isUniqueViolation(err) {
|
||||||
return err.code === '23505';
|
return err.code === '23505';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isLowSeverity(err) {
|
||||||
|
return isUniqueViolation(err) || err.severity === 'low';
|
||||||
|
}
|
||||||
|
|
||||||
var conString = null;
|
var conString = null;
|
||||||
|
|
||||||
function rollback(client, done) {
|
function rollback(client, done) {
|
||||||
logger.warn('Rolling back transaction.');
|
|
||||||
client.query('ROLLBACK', function(err) {
|
client.query('ROLLBACK', function(err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
});
|
});
|
||||||
|
|
@ -89,9 +92,10 @@ exports.recordBill = function recordBill(session, rec, cb) {
|
||||||
if (cerr) return cb(cerr);
|
if (cerr) return cb(cerr);
|
||||||
query(client, getInsertQuery('bills', fields, false), values, function(err) {
|
query(client, getInsertQuery('bills', fields, false), values, function(err) {
|
||||||
done();
|
done();
|
||||||
// TODO: Handle unique violations more cleanly for idempotency
|
if (err && isUniqueViolation(err)) {
|
||||||
// Now, it just returns an error, which should be fine, but a 204 status
|
logger.warn('Attempt to report bill twice');
|
||||||
// would be nicer.
|
return cb();
|
||||||
|
}
|
||||||
cb(err);
|
cb(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -116,8 +120,10 @@ function query(client, queryStr, values, cb) {
|
||||||
|
|
||||||
client.query(queryStr, values, function(err, results) {
|
client.query(queryStr, values, function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(queryStr);
|
if (!isLowSeverity(err)) {
|
||||||
console.log(values);
|
console.log(queryStr);
|
||||||
|
console.log(values);
|
||||||
|
}
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
cb(null, results);
|
cb(null, results);
|
||||||
|
|
@ -132,8 +138,10 @@ function silentQuery(client, queryStr, values, cb) {
|
||||||
|
|
||||||
client.query(queryStr, values, function(err) {
|
client.query(queryStr, values, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(queryStr);
|
if (!isLowSeverity(err)) {
|
||||||
console.log(values);
|
console.log(queryStr);
|
||||||
|
console.log(values);
|
||||||
|
}
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
cb();
|
cb();
|
||||||
|
|
@ -328,7 +336,13 @@ function insertTx(client, session, incoming, tx, satoshis, fiat, stage,
|
||||||
function refreshPendingTx(client, session, cb) {
|
function refreshPendingTx(client, session, cb) {
|
||||||
var sql = 'UPDATE pending_transactions SET updated=now() ' +
|
var sql = 'UPDATE pending_transactions SET updated=now() ' +
|
||||||
'WHERE device_fingerprint=$1 AND session_id=$2';
|
'WHERE device_fingerprint=$1 AND session_id=$2';
|
||||||
query(client, sql, [session.fingerprint, session.id], cb);
|
connect(function(cerr, client, done) {
|
||||||
|
if (cerr) return cb(cerr);
|
||||||
|
query(client, sql, [session.fingerprint, session.id], function(err) {
|
||||||
|
done(err);
|
||||||
|
cb(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
||||||
|
|
@ -339,12 +353,7 @@ function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
||||||
var values = [session.fingerprint, session.id, incoming, currencyCode,
|
var values = [session.fingerprint, session.id, incoming, currencyCode,
|
||||||
toAddress, satoshis];
|
toAddress, satoshis];
|
||||||
query(client, sql, values, function(err) {
|
query(client, sql, values, function(err) {
|
||||||
|
cb(err);
|
||||||
if (err && isUniqueViolation(err)) {
|
|
||||||
return refreshPendingTx(client, session, cb);
|
|
||||||
}
|
|
||||||
if (err) return cb(err);
|
|
||||||
cb();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -441,6 +450,7 @@ function ensureNotFinal(client, session, cb) {
|
||||||
if (results.rows.length > 0) {
|
if (results.rows.length > 0) {
|
||||||
error = new Error('Final request already exists');
|
error = new Error('Final request already exists');
|
||||||
error.name = 'staleBill';
|
error.name = 'staleBill';
|
||||||
|
error.severity = 'low';
|
||||||
return cb(error);
|
return cb(error);
|
||||||
}
|
}
|
||||||
cb();
|
cb();
|
||||||
|
|
@ -459,13 +469,20 @@ exports.addOutgoingPending = function addOutgoingPending(session, currencyCode,
|
||||||
0)
|
0)
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
rollback(client, done);
|
return rollback(client, function(rberr) {
|
||||||
if (err.name === 'staleBill') {
|
done(rberr);
|
||||||
logger.info('Received a bill insert after send coins request');
|
|
||||||
return cb();
|
if (isUniqueViolation(err)) {
|
||||||
}
|
// Pending tx exists, refresh it.
|
||||||
logger.error(err);
|
return refreshPendingTx(client, session, cb);
|
||||||
return cb(err);
|
}
|
||||||
|
if (err.name === 'staleBill') {
|
||||||
|
logger.info('Received a bill insert after send coins request');
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
logger.error(err);
|
||||||
|
return cb(err);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
silentQuery(client, 'COMMIT', null, function() {
|
silentQuery(client, 'COMMIT', null, function() {
|
||||||
done();
|
done();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue