diff --git a/lib/pair.js b/lib/pairing.js similarity index 100% rename from lib/pair.js rename to lib/pairing.js diff --git a/lib/plugins.js b/lib/plugins.js index e9a6aeff..7bd5413d 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -861,8 +861,6 @@ exports.requestDispense = function requestDispense (tx) { } exports.fetchTx = db.fetchTx -exports.cachedResponse = db.cachedResponse -exports.cacheResponse = db.cacheResponse function sweepHD (row) { const cryptoCode = row.crypto_code diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 1d44d0dc..da84b42a 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -7,7 +7,6 @@ const pgp = require('pg-promise')() const logger = require('./logger') -const CACHED_SESSION_TTL = 60 * 60 * 1000 const LIVE_SWEEP_TTL = 48 * 60 * 60 * 1000 function isUniqueViolation (err) { @@ -382,47 +381,6 @@ exports.updateNotify = function updateNotify (tx) { }) } -function insertCachedRequest (deviceId, txId, path, method, body) { - const fields = [ - 'device_id', - 'tx_id', - 'path', - 'method', - 'body' - ] - - const sql = getInsertQuery('cached_responses', fields) - return db.none(sql, [deviceId, txId, path, method, body]) -} - -exports.cachedResponse = function (deviceId, txId, path, method) { - const sql = `select body from cached_responses - where device_id=$1 - and tx_id=$2 - and path=$3 - and method=$4` - - const values = [deviceId, txId, path, method] - - return insertCachedRequest(deviceId, txId, path, method, {pendingRequest: true}) - .then(() => ({})) - .catch(err => { - if (!isUniqueViolation(err)) throw err - console.log('DEBUG22: %j', err) - return db.one(sql, values) - .then(row => ({body: row.body})) - }) -} - -function pruneCachedResponses () { - const sql = `delete from cached_responses - where (EXTRACT(EPOCH FROM (now() - created))) * 1000 < $1` - - const values = [CACHED_SESSION_TTL] - - return db.none(sql, values) -} - exports.cacheResponse = function (deviceId, txId, path, method, body) { const sql = `update cached_responses set body=$1 @@ -479,5 +437,3 @@ exports.markSwept = function markSwept (txId) { return db.none(sql, [true, txId]) } - -setInterval(pruneCachedResponses, CACHED_SESSION_TTL) diff --git a/lib/routes.js b/lib/routes.js index 43e503ac..a056343a 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -6,6 +6,7 @@ const BigNumber = require('bignumber.js') const logger = require('./logger') const configManager = require('./config-manager') const db = require('./db') +const pairing = require('./pairing') let plugins @@ -181,7 +182,7 @@ function verifyTx (req, res) { function ca (req, res) { const token = req.query.token - return pair.authorizeCaDownload(token) + return pairing.authorizeCaDownload(token) .then(valid => { if (valid) return res.json({ca: pair.ca()}) @@ -193,7 +194,7 @@ function pair (req, res) { const token = req.query.token const deviceId = getDeviceId(req) - return pair.pair(token, deviceId) + return pairing.pair(token, deviceId) .then(valid => { if (valid) return cacheAndRespond(req, res) throw httpError('Pairing failed')