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

View file

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

View file

@ -12,6 +12,7 @@ exports.up = function (next) {
'alter table transactions add dispensed 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 confirmation_time timestamp',
'alter table transactions add status status_stage NOT NULL DEFAULT \'notSeen\''
]
db.multi(sql, next)