improve transaction expiration

This commit is contained in:
Josh Harvey 2016-05-10 14:03:13 +03:00
parent 7f33e87c21
commit 996ebd395b
3 changed files with 27 additions and 14 deletions

View file

@ -25,6 +25,7 @@ var STALE_INCOMING_TX_AGE = 7 * 24 * 60 * 60 * 1000
var UNNOTIFIED_INTERVAL = 60 * 1000 var UNNOTIFIED_INTERVAL = 60 * 1000
var MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000 var MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000
var MIN_NOTIFY_AGE = 5 * 60 * 1000 var MIN_NOTIFY_AGE = 5 * 60 * 1000
var TRANSACTION_EXPIRATION = 48 * 60 * 60 * 1000
if (argv.timeout) PENDING_TIMEOUT = argv.timeout / 1000 if (argv.timeout) PENDING_TIMEOUT = argv.timeout / 1000
@ -457,8 +458,9 @@ function processTxStatus (tx) {
var status = res.status var status = res.status
if (tx.status === status) return resolve() if (tx.status === status) return resolve()
const confirm = (status === 'instant' && tx.status !== 'confirmed') ||
db.updateTxStatus(tx, status, function (_err) { (status === 'confirmed' && tx.status !== 'instant')
db.updateTxStatus(tx, status, confirm, function (_err) {
if (_err) logger.error(err) if (_err) logger.error(err)
resolve() resolve()
}) })
@ -804,7 +806,7 @@ exports.registerRedeem = function registerRedeem (session) {
} }
exports.fetchPhoneTx = function fetchPhoneTx (phone) { exports.fetchPhoneTx = function fetchPhoneTx (phone) {
db.fetchPhoneTxs(phone) db.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
.then(txs => { .then(txs => {
const authorizedTxs = txs.filter(tx => tx.authorized) const authorizedTxs = txs.filter(tx => tx.authorized)
if (authorizedTxs.length > 0) { if (authorizedTxs.length > 0) {

View file

@ -527,8 +527,8 @@ function normalizeTxs (txs) {
exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) { exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) {
var sql = 'SELECT * FROM transactions ' + var sql = 'SELECT * FROM transactions ' +
'WHERE phone=$1 AND dispensed=$2 ' + 'WHERE phone=$1 AND dispensed=$2 ' +
'AND (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1 ' + 'AND (EXTRACT(EPOCH FROM (COALESCE(confirmation_time, now()) - created))) * 1000 < $3 ' +
'AND stage=$3 AND authority=$4 AND incoming=$5' 'AND stage=$4 AND authority=$5 AND incoming=$6'
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
connect(function (cerr, client, done) { connect(function (cerr, client, done) {
@ -713,17 +713,27 @@ exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod, cb) {
}) })
} }
exports.updateTxStatus = function updateTxStatus (tx, status, cb) { function pquery (sql, values) {
var sql = 'UPDATE transactions SET status=$1 WHERE id=$2' return new Promise((resolve, reject) => {
connect(function (cerr, client, done) { connect(function (cerr, client, done) {
if (cerr) return cb(cerr) if (cerr) return reject(cerr)
var values = [status, tx.id] query(client, sql, values, function (err, results) {
query(client, sql, values, function (err) {
done(err) done(err)
cb(err) if (err) return reject(err)
resolve(results)
}) })
}) })
})
}
exports.updateTxStatus = function updateTxStatus (tx, status, confirm) {
var sql = confirm
? 'UPDATE transactions SET status=$1, confirmation_time=now() WHERE id=$2'
: 'UPDATE transactions SET status=$1 WHERE id=$2'
var values = [status, tx.id]
return pquery(sql, values)
} }
exports.updateRedeem = function updateRedeem (session, cb) { exports.updateRedeem = function updateRedeem (session, cb) {

View file

@ -12,6 +12,7 @@ exports.up = function (next) {
'alter table transactions add dispensed boolean NOT NULL DEFAULT false', 'alter table transactions add dispensed boolean NOT NULL DEFAULT false',
'alter table transactions add notified boolean NOT NULL DEFAULT false', 'alter table transactions add notified boolean NOT NULL DEFAULT false',
'alter table transactions add redeem boolean NOT NULL DEFAULT false', 'alter table transactions add redeem boolean NOT NULL DEFAULT false',
'alter table transactions add confirmation_time timestamp',
'alter table transactions add status status_stage NOT NULL DEFAULT \'notSeen\'' 'alter table transactions add status status_stage NOT NULL DEFAULT \'notSeen\''
] ]
db.multi(sql, next) db.multi(sql, next)