fix up zero-conf

This commit is contained in:
Josh Harvey 2017-05-21 19:01:38 +01:00
parent 23458b6b56
commit c78519072b
6 changed files with 4205 additions and 180 deletions

View file

@ -231,7 +231,7 @@ function plugins (settings, deviceId) {
}
function getStatus (tx) {
return wallet.getStatus(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode, tx.fiat, deviceId)
return wallet.getStatus(settings, tx, deviceId)
}
function newAddress (tx) {

View file

@ -4,9 +4,9 @@ const E = require('../../../error')
const NAME = 'FakeWallet'
const SECONDS = 1000
const PUBLISH_TIME = 3 * SECONDS
const PUBLISH_TIME = 2 * SECONDS
const AUTHORIZE_TIME = 8 * SECONDS
const CONFIRM_TIME = 60 * SECONDS
const CONFIRM_TIME = 180 * SECONDS
let t0

View file

@ -5,7 +5,8 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode) {
.then(() => {
if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode)
const authorizedValue = 1e5 * 10
const authorizedValue = 1e5 * 2
console.log('DEBUG300: %j', cryptoAtoms.lte(authorizedValue))
return cryptoAtoms.lte(authorizedValue)
})
}

View file

@ -7,10 +7,12 @@ const pify = require('pify')
const fs = pify(require('fs'))
const options = require('./options')
const ph = require('./plugin-helper')
const db = require('./db')
const FETCH_INTERVAL = 5000
const INSUFFICIENT_FUNDS_CODE = 570
const INSUFFICIENT_FUNDS_NAME = 'InsufficientFunds'
const ZERO_CONF_EXPIRATION = 60000
function httpError (msg, code) {
const err = new Error(msg)
@ -67,36 +69,56 @@ function newAddress (settings, info) {
.then(r => r.wallet.newAddress(r.account, info))
}
function getWalletStatus (settings, toAddress, cryptoAtoms, cryptoCode) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.getStatus(r.account, toAddress, cryptoAtoms, cryptoCode))
function getWalletStatus (settings, tx) {
return fetchWallet(settings, tx.cryptoCode)
.then(r => r.wallet.getStatus(r.account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
}
function authorizeZeroConf (settings, toAddress, cryptoAtoms, cryptoCode, fiat, machineId) {
const cryptoConfig = configManager.cryptoScoped(cryptoCode, settings.config)
function authorizeZeroConf (settings, tx, machineId) {
console.log('DEBUG330: %j', machineId)
const cryptoConfig = configManager.cryptoScoped(tx.cryptoCode, settings.config)
const machineConfig = configManager.machineScoped(machineId, settings.config)
const plugin = cryptoConfig.zeroConf
const zeroConfLimit = machineConfig.zeroConfLimit
if (fiat.gt(zeroConfLimit)) return Promise.resolve(false)
if (cryptoCode !== 'BTC' || plugin === 'all-zero-conf') return Promise.resolve(true)
if (tx.fiat.gt(zeroConfLimit)) return Promise.resolve(false)
if (tx.cryptoCode !== 'BTC' || plugin === 'all-zero-conf') return Promise.resolve(true)
const zeroConf = ph.load(ph.ZERO_CONF, plugin)
const account = settings.accounts[plugin]
return zeroConf.authorize(account, toAddress, cryptoAtoms, cryptoCode)
return zeroConf.authorize(account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
}
function getStatus (settings, toAddress, cryptoAtoms, cryptoCode, fiat, machineId) {
function getPublishAge (txId) {
const sql = `select extract(epoch from (now() - created)) * 1000 as age
from cash_out_actions
where tx_id=$1
and action=$2`
return db.oneOrNone(sql, [txId, 'published'])
.then(row => row && row.age)
}
function getStatus (settings, tx, machineId) {
const promises = [
getWalletStatus(settings, toAddress, cryptoAtoms, cryptoCode),
authorizeZeroConf(settings, toAddress, cryptoAtoms, cryptoCode, fiat, machineId)
getWalletStatus(settings, tx),
authorizeZeroConf(settings, tx, machineId),
getPublishAge(tx.id)
]
return Promise.all(promises)
.then(([status, isAuthorized]) => {
if (status === 'authorized') return isAuthorized ? 'authorized' : 'published'
return status
.then(([statusRec, isAuthorized, publishAge]) => {
console.log('DEBUG301: %j', [statusRec, isAuthorized])
if (statusRec.status === 'authorized') {
const unauthorizedStatus = publishAge < ZERO_CONF_EXPIRATION
? 'published'
: 'rejected'
return {status: (isAuthorized ? 'authorized' : unauthorizedStatus)}
}
return statusRec
})
}

File diff suppressed because one or more lines are too long

View file

@ -12,7 +12,7 @@
{
"code": "confidenceFactor",
"display": "Confidence Factor",
"fieldType": "string",
"fieldType": "integer",
"required": true,
"value": ""
}