WIPP
This commit is contained in:
parent
c3261bc61a
commit
e24da06a1b
3 changed files with 64 additions and 38 deletions
|
|
@ -19,7 +19,6 @@ const STALE_INCOMING_TX_AGE = T.week
|
||||||
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
|
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
|
||||||
const MAX_NOTIFY_AGE = 2 * T.days
|
const MAX_NOTIFY_AGE = 2 * T.days
|
||||||
const MIN_NOTIFY_AGE = 5 * T.minutes
|
const MIN_NOTIFY_AGE = 5 * T.minutes
|
||||||
const TRANSACTION_EXPIRATION = 2 * T.days
|
|
||||||
const TRADE_TTL = 2 * T.minutes
|
const TRADE_TTL = 2 * T.minutes
|
||||||
const STALE_TICKER = 3 * T.minutes
|
const STALE_TICKER = 3 * T.minutes
|
||||||
const STALE_BALANCE = 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) {
|
function recordPing (deviceId, deviceTime, rec) {
|
||||||
const event = {
|
const event = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
|
|
@ -453,23 +441,6 @@ function plugins (settings) {
|
||||||
.then(() => code)
|
.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) {
|
function sweepHD (row) {
|
||||||
const cryptoCode = row.crypto_code
|
const cryptoCode = row.crypto_code
|
||||||
|
|
||||||
|
|
@ -498,12 +469,10 @@ function plugins (settings) {
|
||||||
return {
|
return {
|
||||||
pollQueries,
|
pollQueries,
|
||||||
trade,
|
trade,
|
||||||
stateChange,
|
|
||||||
sendCoins,
|
sendCoins,
|
||||||
cashOut,
|
cashOut,
|
||||||
dispenseAck,
|
dispenseAck,
|
||||||
getPhoneCode,
|
getPhoneCode,
|
||||||
fetchPhoneTx,
|
|
||||||
executeTrades,
|
executeTrades,
|
||||||
pong,
|
pong,
|
||||||
pongClear,
|
pongClear,
|
||||||
|
|
|
||||||
35
lib/route-helpers.js
Normal file
35
lib/route-helpers.js
Normal 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}
|
||||||
|
|
@ -14,10 +14,9 @@ const dbm = require('./postgresql_interface')
|
||||||
const pairing = require('./pairing')
|
const pairing = require('./pairing')
|
||||||
const settingsLoader = require('./settings-loader')
|
const settingsLoader = require('./settings-loader')
|
||||||
const plugins = require('./plugins')
|
const plugins = require('./plugins')
|
||||||
|
const helpers = require('./route-helpers')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {init}
|
||||||
init
|
|
||||||
}
|
|
||||||
|
|
||||||
const CLOCK_SKEW = 60 * 1000
|
const CLOCK_SKEW = 60 * 1000
|
||||||
const REQUEST_TTL = 3 * 60 * 1000
|
const REQUEST_TTL = 3 * 60 * 1000
|
||||||
|
|
@ -87,8 +86,7 @@ function trade (req, res, next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function stateChange (req, res, next) {
|
function stateChange (req, res, next) {
|
||||||
const pi = plugins(req.settings)
|
helpers.stateChange(req.deviceId, req.deviceTime, req.body)
|
||||||
pi.stateChange(req.deviceId, req.deviceTime, req.body)
|
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
@ -190,8 +188,7 @@ function updatePhone (req, res, next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchPhoneTx (req, res, next) {
|
function fetchPhoneTx (req, res, next) {
|
||||||
const pi = plugins(req.settings)
|
return helpers.fetchPhoneTx(req.query.phone)
|
||||||
return pi.fetchPhoneTx(req.query.phone)
|
|
||||||
.then(r => res.json(r))
|
.then(r => res.json(r))
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
@ -337,9 +334,22 @@ function init (opts) {
|
||||||
? (req, res, next) => next()
|
? (req, res, next) => next()
|
||||||
: authorize
|
: authorize
|
||||||
|
|
||||||
|
const configRequiredRoutes = [
|
||||||
|
'/poll',
|
||||||
|
'/trade',
|
||||||
|
'/send',
|
||||||
|
'/cash_out',
|
||||||
|
'/dispense_ack',
|
||||||
|
'/event',
|
||||||
|
'/verify_user',
|
||||||
|
'/verify_transaction',
|
||||||
|
'/phone_code'
|
||||||
|
]
|
||||||
|
|
||||||
app.use(morgan('dev', {skip}))
|
app.use(morgan('dev', {skip}))
|
||||||
app.use(helmet())
|
app.use(helmet())
|
||||||
app.use(populateDeviceId)
|
app.use(populateDeviceId)
|
||||||
|
app.use(configRequiredRoutes, populateSettings)
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
app.use(filterOldRequests)
|
app.use(filterOldRequests)
|
||||||
app.post('*', cacheAction)
|
app.post('*', cacheAction)
|
||||||
|
|
@ -409,3 +419,15 @@ function populateDeviceId (req, res, next) {
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue