db bug fix; sweep fixes

This commit is contained in:
Josh Harvey 2016-06-02 22:21:58 +03:00
parent 44188aa5ba
commit 751067bace
3 changed files with 17 additions and 24 deletions

View file

@ -7,7 +7,6 @@ var bodyParser = require('body-parser')
var LamassuConfig = require('lamassu-config')
var routes = require('./routes')
var plugins = require('./plugins')
var db = require('./postgresql_interface')
var logger = require('./logger')
module.exports = function (options) {
@ -21,8 +20,7 @@ module.exports = function (options) {
lamassuConfig = new LamassuConfig(connectionString)
db.init(connectionString)
plugins.init(db)
plugins.init(connectionString)
lamassuConfig.load(function (err, config) {
if (err) {

View file

@ -6,7 +6,7 @@ var async = require('async')
var HKDF = require('node-hkdf-sync')
var BigNumber = require('bignumber.js')
BigNumber.config({CRYPTO: true})
var db = require('./postgresql_interface')
var logger = require('./logger')
var notifier = require('./notifier')
@ -25,8 +25,7 @@ var MIN_NOTIFY_AGE = 5 * 60 * 1000
var TRANSACTION_EXPIRATION = 48 * 60 * 60 * 1000
var SWEEP_LIVE_HD_INTERVAL = 60 * 1000
var SWEEP_OLD_HD_INTERVAL = 60 * 60 * 1000
var db = null
var TRADE_INTERVAL = 60 * 1000
var cryptoCodes = null
@ -57,9 +56,11 @@ var coins = {
var alertFingerprint = null
var lastAlertTime = null
exports.init = function init () {
const masterSeed = fs.readFileSync('seeds/seed.txt').trim()
exports.init = function init (connectionString) {
const masterSeed = fs.readFileSync('seeds/seed.txt', 'utf8').trim()
hkdf = new HKDF('sha256', 'lamassu-server-salt', masterSeed)
db.init(connectionString)
}
function loadPlugin (name, config) {
@ -401,20 +402,13 @@ exports.fiatBalance = function fiatBalance (cryptoCode) {
}
function processTxStatus (tx) {
return new Promise((resolve, reject) => {
const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode]
if (!walletPlugin) throw new Error('No wallet plugins for: ' + cryptoCode)
walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) {
if (err) {
logger.error(err)
return resolve() // Resolve on error because we ignore errors
}
if (!walletPlugin) return console.error('No wallet plugins for: ' + cryptoCode)
db.updateTxStatus(tx, res.status).then(resolve).catch(reject)
})
})
return walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms)
.then(res => db.updateTxStatus(tx, res.status))
}
function notifyConfirmation (tx) {
@ -473,6 +467,7 @@ exports.startPolling = function startPolling () {
monitorLiveIncoming()
monitorIncoming()
monitorUnnotified()
sweepLiveHD()
}
function startTrader (cryptoCode) {
@ -487,7 +482,7 @@ function startTrader (cryptoCode) {
tradeIntervals[cryptoCode] = setInterval(
function () { executeTrades(cryptoCode) },
cachedConfig.exchanges.settings.tradeInterval
TRADE_INTERVAL
)
}
@ -783,14 +778,14 @@ exports.cacheResponse = function (session, path, method, body) {
function sweepHD (row) {
const cryptoCode = row.crypto_code
const walletPlugin = walletPlugins[cryptoCode]
return walletPlugin.sweep(row.hdSerial)
return walletPlugin.sweep(row.hd_serial)
.then(txHash => {
if (txHash) {
logger.debug('[%s] Swept address with tx: %s', cryptoCode, txHash)
return db.markSwept(row.session_id)
}
})
.catch(err => console.error(err))
.catch(err => logger.error(err))
}
function sweepLiveHD () {

View file

@ -424,7 +424,7 @@ exports.cacheResponse = function (session, path, method, body) {
exports.nextCashOutSerialHD = function nextCashOutSerialHD (sessionId, cryptoCode) {
const sql = `select hd_serial from cash_out_hds
where crypto_code=$1 order by hd_serial desc limit 1`
const attempt = db.oneOrNone(sql, [cryptoCode])
const attempt = () => db.oneOrNone(sql, [cryptoCode])
.then(row => {
const serialNumber = row ? row.hd_serial + 1 : 0
const fields2 = ['session_id', 'crypto_code', 'hd_serial']