From 751067bace0781a4f225d573dfd1236290547ec2 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Thu, 2 Jun 2016 22:21:58 +0300 Subject: [PATCH] db bug fix; sweep fixes --- lib/app.js | 4 +--- lib/plugins.js | 35 +++++++++++++++-------------------- lib/postgresql_interface.js | 2 +- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/lib/app.js b/lib/app.js index 02c119e9..08dfac30 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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) { diff --git a/lib/plugins.js b/lib/plugins.js index 481bd74a..918b3a8f 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -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] + 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 () { diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 07f6d056..e898014c 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -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']