From cb11552c11c9415373ef22266de07c44a6294745 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Fri, 28 Nov 2014 00:03:12 -0500 Subject: [PATCH] fix partial_send reporting for timeout --- lib/plugins.js | 10 ++++--- lib/postgresql_interface.js | 56 +++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index fa2cab2a..969ac4c5 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -194,17 +194,18 @@ function _sendBitcoins(toAddress, satoshis, cb) { } function executeTx(session, tx, authority, cb) { - db.addOutgoingTx(session, tx, function(err, satoshisToSend) { + db.addOutgoingTx(session, tx, function(err, toSend) { if (err) return cb(err); - + var satoshisToSend = toSend.satoshis; if (satoshisToSend === 0) return cb(null, {statusCode: 204, txId: tx.txId, txHash: null}); _sendBitcoins(tx.toAddress, satoshisToSend, function(_err, txHash) { var fee = null; // Need to fill this out in plugins - db.sentCoins(session, tx, authority, satoshisToSend, fee, _err, txHash); + if (_err) toSend = {satoshis: 0, fiat: 0}; + db.sentCoins(session, tx, authority, toSend, fee, _err, txHash); - if (_err) return cb(err); + if (_err) return cb(_err); pollBalance(); cb(null, { @@ -238,6 +239,7 @@ function reapIncomingTx(session, tx) { function reapTx(row) { var session = {fingerprint: row.device_fingerprint, id: row.session_id}; var tx = { + fiat: 0, satoshis: row.satoshis, toAddress: row.to_address, currencyCode: row.currency_code, diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index f596f900..aebb27d0 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -95,8 +95,8 @@ exports.recordBill = function recordBill(session, rec, cb) { }; exports.recordDeviceEvent = function recordDeviceEvent(session, event) { - connect(function(err, client, done) { - if (err) return; + connect(function(cerr, client, done) { + if (cerr) return; var sql = 'INSERT INTO device_events (device_fingerprint, event_type, ' + 'note, device_time) VALUES ($1, $2, $3, $4)'; var values = [session.fingerprint, event.eventType, event.note, @@ -185,7 +185,8 @@ function computeSendAmount(tx, totals) { } exports.removeOldPending = function removeOldPending(timeoutMS) { - connect(function(err, client, done) { + connect(function(cerr, client, done) { + if (cerr) return; var sql = 'DELETE FROM pending_transactions ' + 'WHERE incoming AND extract(EPOCH FROM now() - created) > $1'; var timeoutS = timeoutMS / 1000; @@ -198,7 +199,8 @@ exports.removeOldPending = function removeOldPending(timeoutMS) { }; exports.pendingTxs = function pendingTxs(timeoutMS, cb) { - connect(function(err, client, done) { + connect(function(cerr, client, done) { + if (cerr) return cb(cerr); var sql = 'SELECT * ' + 'FROM pending_transactions ' + 'WHERE (incoming OR extract(EPOCH FROM now() - created) > $1) ' + @@ -228,7 +230,7 @@ function insertOutgoingTx(client, session, tx, totals, cb) { function(err) { if (err) return cb(err); - cb(null, satoshis); + cb(null, {satoshis: satoshis, fiat: fiat}); }); } @@ -317,8 +319,8 @@ function buildOutgoingTx(client, session, tx, cb) { // Calling function should only send bitcoins if result.satoshisToSend > 0 exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) { - connect(function(err, client, done) { - if (err) return cb(err); + connect(function(cerr, client, done) { + if (cerr) return cb(cerr); async.series([ async.apply(silentQuery, client, 'BEGIN'), async.apply(insertOutgoingCompleteTx, client, session, tx), @@ -331,25 +333,25 @@ exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) { } silentQuery(client, 'COMMIT', function() { done(); - var satoshisToSend = results[3]; - cb(null, satoshisToSend); + var toSend = results[3]; + cb(null, toSend); }); }); }); }; -exports.sentCoins = function sentCoins(session, tx, authority, satoshis, fee, +exports.sentCoins = function sentCoins(session, tx, authority, toSend, fee, error, txHash) { - connect(function(err, client, done) { - if (err) return logger.error(err); + connect(function(cerr, client, done) { + if (cerr) return logger.error(cerr); var newTx = _.clone(tx); newTx.txHash = txHash; newTx.error = error; - insertOutgoing(client, session, newTx, satoshis, newTx.fiat, 'partial_send', - authority, function(_err) { + insertOutgoing(client, session, newTx, toSend.satoshis, toSend.fiat, + 'partial_send', authority, function(err) { done(); - if (err) logger.error(_err); + if (err) logger.error(err); }); }); }; @@ -362,8 +364,8 @@ function maybeRemovePending(client, session, authority, cb) { exports.addIncomingTx = function addIncomingTx(session, tx, authority, satoshisReceived, cb) { - connect(function(err, client, done) { - if (err) return cb(err); + connect(function(cerr, client, done) { + if (cerr) return cb(cerr); async.waterfall([ async.apply(silentQuery, client, 'BEGIN', null), async.apply(maybeRemovePending, client, session, authority), @@ -396,8 +398,8 @@ exports.addOutgoingPending = function addOutgoingPending(session, currencyCode, }; exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) { - connect(function(err, client, done) { - if (err) return cb(err); + connect(function(cerr, client, done) { + if (cerr) return cb(cerr); async.waterfall([ async.apply(silentQuery, client, 'BEGIN', null), async.apply(addPendingTx, client, session, true, tx.currencyCode, @@ -436,15 +438,15 @@ function initialRequest(client, session, cb) { } exports.dispenseStatus = function dispenseStatus(session, cb) { - connect(function(err, client, done) { - if (err) return cb(err); + connect(function(cerr, client, done) { + if (cerr) return cb(cerr); async.parallel([ async.apply(initialRequest, client, session), async.apply(lastTxStatus, client, session) - ], function(_err, results) { + ], function(err, results) { done(); - if (_err) return cb(_err); + if (err) return cb(err); var pending = (results[0].rows.length === 1) && (results[1].rows.length === 1) && @@ -500,17 +502,17 @@ function insertDispense(client, session, tx, transactionId, counts, cb) { } exports.addDispense = function addDispense(session, tx) { - connect(function(err, client, done) { - if (err) return logger.error(err); + connect(function(cerr, client, done) { + if (cerr) return logger.error(cerr); async.waterfall([ async.apply(insertIncoming, client, session, tx, 0, tx.fiat, 'dispense', 'authorized'), async.apply(lastDispenseCount, client, session), async.apply(insertDispense, client, session, tx) - ], function(_err) { + ], function(err) { done(); - if (_err) logger.error(_err); + if (err) logger.error(err); }); }); };