From 19ebf9d2a2a92b56c6540086dd8ba03ac054b2c7 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Fri, 3 Jun 2016 03:14:22 +0300 Subject: [PATCH] fix up sweeping --- lib/plugins.js | 5 +++- lib/postgresql_interface.js | 38 ++++++++++++++---------------- migrations/013-add-last-checked.js | 14 +++++++++++ todo.txt | 2 +- 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 migrations/013-add-last-checked.js diff --git a/lib/plugins.js b/lib/plugins.js index 918b3a8f..37d7ff85 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -24,7 +24,7 @@ var MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000 var MIN_NOTIFY_AGE = 5 * 60 * 1000 var TRANSACTION_EXPIRATION = 48 * 60 * 60 * 1000 var SWEEP_LIVE_HD_INTERVAL = 60 * 1000 -var SWEEP_OLD_HD_INTERVAL = 60 * 60 * 1000 +var SWEEP_OLD_HD_INTERVAL = 2 * 60 * 1000 var TRADE_INTERVAL = 60 * 1000 var cryptoCodes = null @@ -468,6 +468,7 @@ exports.startPolling = function startPolling () { monitorIncoming() monitorUnnotified() sweepLiveHD() + sweepOldHD() } function startTrader (cryptoCode) { @@ -791,9 +792,11 @@ function sweepHD (row) { function sweepLiveHD () { return db.fetchLiveHD() .then(rows => Promise.all(rows.map(sweepHD))) + .catch(err => logger.error(err)) } function sweepOldHD () { return db.fetchOldHD() .then(rows => Promise.all(rows.map(sweepHD))) + .catch(err => logger.error(err)) } diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index e898014c..f374ff40 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -332,10 +332,7 @@ exports.updateTxStatus = function updateTxStatus (tx, status) { const values2 = [newStatus, tx.sessionId] return t.none(sql2, values2) - .then(() => { - const sql3 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' - return t.none(sql3, [tx.sessionId, newStatus]) - }) + .then(() => ({status: newStatus})) }) } @@ -344,6 +341,18 @@ exports.updateTxStatus = function updateTxStatus (tx, status) { // Note: don't worry about retrying failed transaction here // It will be tried again on the next status check return db.tx(transaction) + .then(r => { + if (!r) return + + const sql3 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' + return db.none(sql3, [tx.sessionId, r.status]) + .then(() => { + if (r.status === 'confirmed') { + const sql4 = `update cash_out_hds set confirmed=true where session_id=$1` + return db.none(sql4, [tx.sessionId]) + } + }) + }) } exports.updateRedeem = function updateRedeem (session) { @@ -449,23 +458,12 @@ exports.fetchLiveHD = function fetchLiveHD () { } exports.fetchOldHD = function fetchLiveHD () { - db.one(`select reltuples as approximate_row_count from pg_class where relname = 'cash_out_txs'`) - .then(row => { - const rowCount = row.approximate_row_count + const sql = `select * from cash_out_hds + where confirmed + order by last_checked + limit 10` - const factor = rowCount < 1000 - ? 10 - : rowCount < 10000 ? 1 : 0.1 - - const sql = `select * from cash_out_txs tablesample system $1, - cash_out_hds - where cash_out_txs.session_id=cash_out_hds.session_id - and status=$2` - - const values = [factor, 'confirmed'] - - return db.manyOrNone(sql, values) - }) + return db.manyOrNone(sql) } exports.markSwept = function markSwept (sessionId) { diff --git a/migrations/013-add-last-checked.js b/migrations/013-add-last-checked.js new file mode 100644 index 00000000..8b943c30 --- /dev/null +++ b/migrations/013-add-last-checked.js @@ -0,0 +1,14 @@ +var db = require('./db') + +exports.up = function (next) { + var sql = [ + 'alter table cash_out_hds add last_checked timestamptz not null default now()', + 'alter table cash_out_hds add confirmed boolean not null default false', + 'create index on cash_out_hds (confirmed, last_checked)' + ] + db.multi(sql, next) +} + +exports.down = function (next) { + next() +} diff --git a/todo.txt b/todo.txt index f4526ed8..a33a692f 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,4 @@ - l-m shouldn't keep polling l-s when not on pending screen (low priority) - scrutinize hkdf, maybe use own simplified version -- test geth cash-out +- test eth cash-out confirmation record in cash_out_hds