fix pairing bug; remove cachedResponses

This commit is contained in:
Josh Harvey 2016-11-19 15:51:26 +02:00
parent 5a8e5627ef
commit c9af2fe5b3
4 changed files with 3 additions and 48 deletions

View file

@ -861,8 +861,6 @@ exports.requestDispense = function requestDispense (tx) {
} }
exports.fetchTx = db.fetchTx exports.fetchTx = db.fetchTx
exports.cachedResponse = db.cachedResponse
exports.cacheResponse = db.cacheResponse
function sweepHD (row) { function sweepHD (row) {
const cryptoCode = row.crypto_code const cryptoCode = row.crypto_code

View file

@ -7,7 +7,6 @@ const pgp = require('pg-promise')()
const logger = require('./logger') const logger = require('./logger')
const CACHED_SESSION_TTL = 60 * 60 * 1000
const LIVE_SWEEP_TTL = 48 * 60 * 60 * 1000 const LIVE_SWEEP_TTL = 48 * 60 * 60 * 1000
function isUniqueViolation (err) { 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) { exports.cacheResponse = function (deviceId, txId, path, method, body) {
const sql = `update cached_responses const sql = `update cached_responses
set body=$1 set body=$1
@ -479,5 +437,3 @@ exports.markSwept = function markSwept (txId) {
return db.none(sql, [true, txId]) return db.none(sql, [true, txId])
} }
setInterval(pruneCachedResponses, CACHED_SESSION_TTL)

View file

@ -6,6 +6,7 @@ const BigNumber = require('bignumber.js')
const logger = require('./logger') const logger = require('./logger')
const configManager = require('./config-manager') const configManager = require('./config-manager')
const db = require('./db') const db = require('./db')
const pairing = require('./pairing')
let plugins let plugins
@ -181,7 +182,7 @@ function verifyTx (req, res) {
function ca (req, res) { function ca (req, res) {
const token = req.query.token const token = req.query.token
return pair.authorizeCaDownload(token) return pairing.authorizeCaDownload(token)
.then(valid => { .then(valid => {
if (valid) return res.json({ca: pair.ca()}) if (valid) return res.json({ca: pair.ca()})
@ -193,7 +194,7 @@ function pair (req, res) {
const token = req.query.token const token = req.query.token
const deviceId = getDeviceId(req) const deviceId = getDeviceId(req)
return pair.pair(token, deviceId) return pairing.pair(token, deviceId)
.then(valid => { .then(valid => {
if (valid) return cacheAndRespond(req, res) if (valid) return cacheAndRespond(req, res)
throw httpError('Pairing failed') throw httpError('Pairing failed')