diff --git a/lib/plugins.js b/lib/plugins.js index d6daca11..7f9a589b 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -12,7 +12,7 @@ var REAP_RATE = 2 * 1000; var PENDING_TIMEOUT = 70 * 1000; // TODO: might have to update this if user is allowed to extend monitoring time -var DEPOSIT_TIMEOUT = 120 * 1000; +var DEPOSIT_TIMEOUT = 130 * 1000; var db = null; @@ -325,8 +325,7 @@ exports.trade = function trade(session, rawTrade, cb) { }; async.parallel([ - async.apply(db.addOutgoingPending, session, tx.currencyCode, tx.toAddress, - tx.satoshis), + async.apply(db.addOutgoingPending, session, tx.currencyCode, tx.toAddress), async.apply(db.recordBill, session, rawTrade) ], cb); }; diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index b2a582a6..a3544991 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -5,14 +5,13 @@ var pg = require('pg'); var async = require('async'); -var util = require('util'); var _ = require('lodash'); var logger = require('./logger'); /* function inspect(rec) { - console.log(util.inspect(rec, {depth: null, colors: true})); + console.log(require('util').inspect(rec, {depth: null, colors: true})); } */ @@ -190,7 +189,7 @@ exports.removeOldPending = function removeOldPending(timeoutMS) { connect(function(cerr, client, done) { if (cerr) return; var sql = 'DELETE FROM pending_transactions ' + - 'WHERE incoming AND extract(EPOCH FROM now() - created) > $1'; + 'WHERE incoming AND extract(EPOCH FROM now() - updated) > $1'; var timeoutS = timeoutMS / 1000; var values = [timeoutS]; query(client, sql, values, function(err) { @@ -205,8 +204,8 @@ exports.pendingTxs = function pendingTxs(timeoutMS, cb) { if (cerr) return cb(cerr); var sql = 'SELECT * ' + 'FROM pending_transactions ' + - 'WHERE (incoming OR extract(EPOCH FROM now() - created) > $1) ' + - 'ORDER BY created ASC'; + 'WHERE (incoming OR extract(EPOCH FROM now() - updated) > $1) ' + + 'ORDER BY updated ASC'; var timeoutS = timeoutMS / 1000; var values = [timeoutS]; query(client, sql, values, function(err, results) { @@ -228,6 +227,8 @@ function insertOutgoingTx(client, session, tx, totals, cb) { var authority = tx.fiat ? 'machine' : 'timeout'; var satoshis = sendAmount.satoshis; var fiat = sendAmount.fiat; + if (satoshis === 0) return cb(null, {satoshis: 0, fiat: 0}); + insertOutgoing(client, session, tx, satoshis, fiat, stage, authority, function(err) { @@ -296,6 +297,12 @@ function insertTx(client, session, incoming, tx, satoshis, fiat, stage, }); } +function refreshPendingTx(client, session, cb) { + var sql = 'UPDATE pending_transactions SET updated=now() ' + + 'WHERE device_fingerprint=$1 AND session_id=$2'; + query(client, sql, [session.fingerprint, session.id], cb); +} + function addPendingTx(client, session, incoming, currencyCode, toAddress, satoshis, cb) { var fields = ['device_fingerprint', 'session_id', 'incoming', @@ -306,6 +313,9 @@ function addPendingTx(client, session, incoming, currencyCode, toAddress, query(client, sql, values, function(err) { // If pending tx already exists, do nothing + if (err && isUniqueViolation(err)) + return refreshPendingTx(client, session, cb); + if (err && !isUniqueViolation(err)) return cb(err); cb(); @@ -373,7 +383,7 @@ exports.addIncomingTx = function addIncomingTx(session, tx, authority, if (cerr) return cb(cerr); var satoshisRequested = tx.satoshis; var insufficientFunds = satoshisReceived < satoshisRequested; - async.waterfall([ + async.series([ async.apply(silentQuery, client, 'BEGIN', null), async.apply(maybeRemovePending, client, session, insufficientFunds, authority), @@ -393,11 +403,11 @@ exports.addIncomingTx = function addIncomingTx(session, tx, authority, }; exports.addOutgoingPending = function addOutgoingPending(session, currencyCode, - toAddress, satoshis, cb) { + toAddress, cb) { connect(function(cerr, client, done) { if (cerr) return cb(cerr); - addPendingTx(client, session, false, currencyCode, toAddress, satoshis, + addPendingTx(client, session, false, currencyCode, toAddress, 0, function(err) { done(); cb(err); @@ -408,7 +418,7 @@ exports.addOutgoingPending = function addOutgoingPending(session, currencyCode, exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) { connect(function(cerr, client, done) { if (cerr) return cb(cerr); - async.waterfall([ + async.series([ async.apply(silentQuery, client, 'BEGIN', null), async.apply(addPendingTx, client, session, true, tx.currencyCode, tx.toAddress, tx.satoshis), diff --git a/migrations/004-transactions-reload.js b/migrations/004-transactions-reload.js index 2b608cf3..c0660e14 100644 --- a/migrations/004-transactions-reload.js +++ b/migrations/004-transactions-reload.js @@ -41,7 +41,7 @@ exports.up = function(next) { 'currency_code text NOT NULL, ' + 'to_address text NOT NULL, ' + 'satoshis integer NOT NULL, ' + - 'created timestamp NOT NULL DEFAULT now() ' + + 'updated timestamp NOT NULL DEFAULT now() ' + ')', 'CREATE TABLE dispenses ( ' +