This commit is contained in:
Josh Harvey 2016-12-08 23:06:48 +02:00
parent c3261bc61a
commit e24da06a1b
3 changed files with 64 additions and 38 deletions

View file

@ -19,7 +19,6 @@ const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const MAX_NOTIFY_AGE = 2 * T.days
const MIN_NOTIFY_AGE = 5 * T.minutes
const TRANSACTION_EXPIRATION = 2 * T.days
const TRADE_TTL = 2 * T.minutes
const STALE_TICKER = 3 * T.minutes
const STALE_BALANCE = 3 * T.minutes
@ -158,17 +157,6 @@ function plugins (settings) {
})
}
function stateChange (deviceId, deviceTime, rec) {
const event = {
id: rec.uuid,
deviceId: deviceId,
eventType: 'stateChange',
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}),
deviceTime: deviceTime
}
return dbm.machineEvent(event)
}
function recordPing (deviceId, deviceTime, rec) {
const event = {
id: uuid.v4(),
@ -453,23 +441,6 @@ function plugins (settings) {
.then(() => code)
}
function fetchPhoneTx (phone) {
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
.then(txs => {
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
if (confirmedTxs.length > 0) {
const maxTx = R.reduce((acc, val) => {
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
}, null, confirmedTxs)
return {tx: maxTx}
}
if (txs.length > 0) return {pending: true}
return {}
})
}
function sweepHD (row) {
const cryptoCode = row.crypto_code
@ -498,12 +469,10 @@ function plugins (settings) {
return {
pollQueries,
trade,
stateChange,
sendCoins,
cashOut,
dispenseAck,
getPhoneCode,
fetchPhoneTx,
executeTrades,
pong,
pongClear,

35
lib/route-helpers.js Normal file
View file

@ -0,0 +1,35 @@
const R = require('ramda')
const dbm = require('./postgresql_interface')
const T = require('./time')
const TRANSACTION_EXPIRATION = 2 * T.days
function stateChange (deviceId, deviceTime, rec) {
const event = {
id: rec.uuid,
deviceId: deviceId,
eventType: 'stateChange',
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}),
deviceTime: deviceTime
}
return dbm.machineEvent(event)
}
function fetchPhoneTx (phone) {
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
.then(txs => {
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
if (confirmedTxs.length > 0) {
const maxTx = R.reduce((acc, val) => {
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
}, null, confirmedTxs)
return {tx: maxTx}
}
if (txs.length > 0) return {pending: true}
return {}
})
}
module.exports = {stateChange, fetchPhoneTx}

View file

@ -14,10 +14,9 @@ const dbm = require('./postgresql_interface')
const pairing = require('./pairing')
const settingsLoader = require('./settings-loader')
const plugins = require('./plugins')
const helpers = require('./route-helpers')
module.exports = {
init
}
module.exports = {init}
const CLOCK_SKEW = 60 * 1000
const REQUEST_TTL = 3 * 60 * 1000
@ -87,8 +86,7 @@ function trade (req, res, next) {
}
function stateChange (req, res, next) {
const pi = plugins(req.settings)
pi.stateChange(req.deviceId, req.deviceTime, req.body)
helpers.stateChange(req.deviceId, req.deviceTime, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
@ -190,8 +188,7 @@ function updatePhone (req, res, next) {
}
function fetchPhoneTx (req, res, next) {
const pi = plugins(req.settings)
return pi.fetchPhoneTx(req.query.phone)
return helpers.fetchPhoneTx(req.query.phone)
.then(r => res.json(r))
.catch(next)
}
@ -337,9 +334,22 @@ function init (opts) {
? (req, res, next) => next()
: authorize
const configRequiredRoutes = [
'/poll',
'/trade',
'/send',
'/cash_out',
'/dispense_ack',
'/event',
'/verify_user',
'/verify_transaction',
'/phone_code'
]
app.use(morgan('dev', {skip}))
app.use(helmet())
app.use(populateDeviceId)
app.use(configRequiredRoutes, populateSettings)
app.use(bodyParser.json())
app.use(filterOldRequests)
app.post('*', cacheAction)
@ -409,3 +419,15 @@ function populateDeviceId (req, res, next) {
next()
}
function populateSettings (req, res, next) {
const versionId = req.headers['config-version-id']
if (!versionId) {
logger.debug('No config-version-id header')
return res.sendStatus(400)
}
settingsLoader.log(versionId)
.then(settings => { req.settings = settings })
.catch(next)
}