From 272405b4b1d2c2a7e887ebfb8ad249a76b1e20d7 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Tue, 18 Jul 2017 08:12:01 +0300 Subject: [PATCH 1/4] stress testing; add mock-ticker --- .migrate-stress | 1 + bin/lamassu-migrate | 1 + lib/admin/config.js | 1 + lib/options.js | 24 ++++++++++++------- lib/plugins/ticker/mock-ticker/mock-ticker.js | 12 ++++++++++ 5 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 .migrate-stress create mode 100644 lib/plugins/ticker/mock-ticker/mock-ticker.js diff --git a/.migrate-stress b/.migrate-stress new file mode 100644 index 00000000..fa39ddf4 --- /dev/null +++ b/.migrate-stress @@ -0,0 +1 @@ +{"migrations":[{"title":"001-initial.js"},{"title":"002-bills.js"},{"title":"003-device-events.js"},{"title":"004-transactions-reload.js"},{"title":"005-addCrypto.js"},{"title":"006-add-machine-config.js"},{"title":"007-add-phone.js"},{"title":"008-add-two-way.js"},{"title":"009-update-timestamps.js"},{"title":"010-cached-requests.js"},{"title":"011-transactions-reload-2.js"},{"title":"012-add-hd-path-serial.js"},{"title":"013-add-last-checked.js"},{"title":"014-session-to-tx-id.js"},{"title":"015-paired_devices.js"},{"title":"016-new_cached_requests_table.js"},{"title":"017-user_tokens.js"},{"title":"018-alter_devices.js"},{"title":"019-remove-dispense-counts.js"},{"title":"020-add-server-events.js"},{"title":"021-config-version-id.js"},{"title":"022-add_cash_in_sent.js"},{"title":"023-add-dispenses-to-cash-out.js"},{"title":"024-consolidate-hd.js"},{"title":"025-create_trades.js"},{"title":"026-add_send_confirmed.js"},{"title":"027-tx_errors.js"},{"title":"028-cash_out_actions.js"},{"title":"029-add_valid_to_user_config.js"},{"title":"030-cash-out-provision.js"},{"title":"031-remove_name_from_devices.js"},{"title":"032-create_machine_pings_table.js"},{"title":"033-add_cash_in_fee.js"},{"title":"034-add_cash_out_error_code.js"},{"title":"035-log_bank_notes.js"}],"path":"/Users/josh/projects/lamassu-server/.migrate-stress","pos":35} \ No newline at end of file diff --git a/bin/lamassu-migrate b/bin/lamassu-migrate index 590d3121..932f63a3 100755 --- a/bin/lamassu-migrate +++ b/bin/lamassu-migrate @@ -7,6 +7,7 @@ const options = require('../lib/options') const migrateDir = path.resolve(__dirname, '..', 'migrations') const migration = migrate.load(options.migrateStatePath, migrateDir) +console.log(options.migrateStatePath) migration.up(err => { if (err) { console.error('DB Migration failed: %s', err) diff --git a/lib/admin/config.js b/lib/admin/config.js index 0ce77b09..6b2259a5 100644 --- a/lib/admin/config.js +++ b/lib/admin/config.js @@ -170,6 +170,7 @@ function fetchData () { {code: 'kraken', display: 'Kraken', class: 'ticker', cryptos: ['BTC', 'ETH', 'LTC', 'DASH', 'ZEC']}, {code: 'bitstamp', display: 'Bitstamp', class: 'ticker', cryptos: ['BTC', 'LTC']}, {code: 'coinbase', display: 'Coinbase', class: 'ticker', cryptos: ['BTC', 'ETH', 'LTC']}, + {code: 'mock-ticker', display: 'Mock ticker', class: 'ticker', cryptos: ALL_CRYPTOS}, {code: 'bitcoind', display: 'bitcoind', class: 'wallet', cryptos: ['BTC']}, {code: 'geth', display: 'geth', class: 'wallet', cryptos: ['ETH']}, {code: 'zcashd', display: 'zcashd', class: 'wallet', cryptos: ['ZEC']}, diff --git a/lib/options.js b/lib/options.js index 291ed278..b2c549b1 100644 --- a/lib/options.js +++ b/lib/options.js @@ -4,21 +4,27 @@ const os = require('os') const _ = require('lodash/fp') const argv = require('minimist')(process.argv.slice(2)) -let serverConfig +function load () { + if (argv.f) { + const configPath = argv.f + return JSON.parse(fs.readFileSync(configPath)) + } -try { - const globalConfigPath = path.resolve('/etc', 'lamassu', 'lamassu.json') - serverConfig = JSON.parse(fs.readFileSync(globalConfigPath)) -} catch (_) { try { - const homeConfigPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json') - serverConfig = JSON.parse(fs.readFileSync(homeConfigPath)) + const globalConfigPath = path.resolve('/etc', 'lamassu', 'lamassu.json') + return JSON.parse(fs.readFileSync(globalConfigPath)) } catch (_) { - console.error("Couldn't open lamassu.json config file.") - process.exit(1) + try { + const homeConfigPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json') + return JSON.parse(fs.readFileSync(homeConfigPath)) + } catch (_) { + console.error("Couldn't open lamassu.json config file.") + process.exit(1) + } } } +const serverConfig = load() const defaults = {logLevel: 'info'} const commandLine = {logLevel: argv.logLevel} diff --git a/lib/plugins/ticker/mock-ticker/mock-ticker.js b/lib/plugins/ticker/mock-ticker/mock-ticker.js new file mode 100644 index 00000000..49470e90 --- /dev/null +++ b/lib/plugins/ticker/mock-ticker/mock-ticker.js @@ -0,0 +1,12 @@ +const BN = require('../../../bn') + +function ticker (account, fiatCode, cryptoCode) { + return Promise.resolve({ + rates: { + ask: BN(105), + bid: BN(100) + } + }) +} + +module.exports = {ticker} From 916a3629a89d7c21668d4201afe7b9ed64cb79c3 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Wed, 19 Jul 2017 16:14:45 +0200 Subject: [PATCH 2/4] refactor migration --- bin/lamassu-apply-defaults | 46 ++---------------------------------- bin/lamassu-migrate | 20 ++++++---------- lib/apply-defaults.js | 48 ++++++++++++++++++++++++++++++++++++++ lib/blockchain/ethereum.js | 2 +- lib/migrate.js | 19 +++++++++++++++ lib/options.js | 5 ++++ lib/settings-loader.js | 2 ++ 7 files changed, 84 insertions(+), 58 deletions(-) create mode 100644 lib/apply-defaults.js create mode 100644 lib/migrate.js diff --git a/bin/lamassu-apply-defaults b/bin/lamassu-apply-defaults index 355dfe93..6ef998c4 100755 --- a/bin/lamassu-apply-defaults +++ b/bin/lamassu-apply-defaults @@ -1,50 +1,8 @@ #!/usr/bin/env node -const assert = require('assert') -const _ = require('lodash/fp') +const applyDefaults = require('../lib/apply-defaults') -// const db = require('../lib/db') -const settingsLoader = require('../lib/settings-loader') -const schema = require('../lamassu-schema.json') -const newFields = [] - -const DEFAULT_CRYPTO = _.first(_.find(['code', 'cryptoCurrencies'], schema.fields).default) - -assert(DEFAULT_CRYPTO) - -Promise.resolve() -.then(() => { - schema.groups.forEach(group => { - return group.fields.forEach(fieldCode => { - const field = schema.fields.find(r => r.code === fieldCode) - if (!field) throw new Error('No such field: ' + fieldCode) - if (_.isNil(field.default)) return - if (group.machineScope === 'specific') return - - const crypto = group.cryptoScope === 'specific' - ? DEFAULT_CRYPTO - : 'global' - - return newFields.push({ - fieldLocator: { - fieldScope: { - crypto, - machine: 'global' - }, - code: fieldCode, - fieldType: field.fieldType, - fieldClass: field.fieldClass - }, - fieldValue: { - fieldType: field.fieldType, - value: field.default - } - }) - }) - }) - - return settingsLoader.save(newFields) -}) +applyDefaults.run() .then(() => { console.log('Success.') process.exit(0) diff --git a/bin/lamassu-migrate b/bin/lamassu-migrate index 932f63a3..ee212caa 100755 --- a/bin/lamassu-migrate +++ b/bin/lamassu-migrate @@ -1,19 +1,13 @@ #!/usr/bin/env node -const path = require('path') -const migrate = require('migrate') -const options = require('../lib/options') - -const migrateDir = path.resolve(__dirname, '..', 'migrations') -const migration = migrate.load(options.migrateStatePath, migrateDir) - -console.log(options.migrateStatePath) -migration.up(err => { - if (err) { - console.error('DB Migration failed: %s', err) - process.exit(1) - } +const migrate = require('../lib/migrate') +migrate.run() +.then(() => { console.log('DB Migration succeeded.') process.exit(0) }) +.catch(err => { + console.error('DB Migration failed: %s', err) + process.exit(1) +}) diff --git a/lib/apply-defaults.js b/lib/apply-defaults.js new file mode 100644 index 00000000..5834b0f3 --- /dev/null +++ b/lib/apply-defaults.js @@ -0,0 +1,48 @@ +const assert = require('assert') +const _ = require('lodash/fp') + +const settingsLoader = require('../lib/settings-loader') +const schema = require('../lamassu-schema.json') +const newFields = [] + +const DEFAULT_CRYPTO = _.first(_.find(['code', 'cryptoCurrencies'], schema.fields).default) + +assert(DEFAULT_CRYPTO) + +module.exports = {run} + +function run () { + return Promise.resolve() + .then(() => { + schema.groups.forEach(group => { + return group.fields.forEach(fieldCode => { + const field = schema.fields.find(r => r.code === fieldCode) + if (!field) throw new Error('No such field: ' + fieldCode) + if (_.isNil(field.default)) return + if (group.machineScope === 'specific') return + + const crypto = group.cryptoScope === 'specific' + ? DEFAULT_CRYPTO + : 'global' + + return newFields.push({ + fieldLocator: { + fieldScope: { + crypto, + machine: 'global' + }, + code: fieldCode, + fieldType: field.fieldType, + fieldClass: field.fieldClass + }, + fieldValue: { + fieldType: field.fieldType, + value: field.default + } + }) + }) + }) + + return settingsLoader.save(newFields) + }) +} diff --git a/lib/blockchain/ethereum.js b/lib/blockchain/ethereum.js index dfc025b9..0f629766 100644 --- a/lib/blockchain/ethereum.js +++ b/lib/blockchain/ethereum.js @@ -7,6 +7,6 @@ module.exports = {setup} function setup (dataDir) { const coinRec = coinUtils.getCryptoCurrency('ETH') common.firewall([coinRec.defaultPort]) - const cmd = `/usr/local/bin/${coinRec.daemon} --datadir "${dataDir}" --cache 500` + const cmd = `/usr/local/bin/${coinRec.daemon} --datadir "${dataDir}" --cache 500 --rpc` common.writeSupervisorConfig(coinRec, cmd) } diff --git a/lib/migrate.js b/lib/migrate.js new file mode 100644 index 00000000..d5f6462f --- /dev/null +++ b/lib/migrate.js @@ -0,0 +1,19 @@ +const path = require('path') +const migrate = require('migrate') + +const options = require('./options') + +const migrateDir = path.resolve(__dirname, '..', 'migrations') +const migration = migrate.load(options.migrateStatePath, migrateDir) + +module.exports = {run} + +function run () { + return new Promise((resolve, reject) => { + migration.up(err => { + if (err) return reject(err) + return resolve(0) + }) + }) +} + diff --git a/lib/options.js b/lib/options.js index b2c549b1..4ba090de 100644 --- a/lib/options.js +++ b/lib/options.js @@ -5,6 +5,11 @@ const _ = require('lodash/fp') const argv = require('minimist')(process.argv.slice(2)) function load () { + if (process.env.LAMASSU_CONFIG) { + const configPath = process.env.LAMASSU_CONFIG + return JSON.parse(fs.readFileSync(configPath)) + } + if (argv.f) { const configPath = argv.f return JSON.parse(fs.readFileSync(configPath)) diff --git a/lib/settings-loader.js b/lib/settings-loader.js index b525f91e..375265f2 100644 --- a/lib/settings-loader.js +++ b/lib/settings-loader.js @@ -135,6 +135,8 @@ function settings () { function save (config) { const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)' + console.log('DEBUG800: %s', sql) + return configValidate.validate(config) .then(() => db.none(sql, ['config', {config}, true])) .catch(() => db.none(sql, ['config', {config}, false])) From 37d12291a0f234f8e25eac8f3be89fef29a2f00f Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Tue, 25 Jul 2017 19:59:47 +0300 Subject: [PATCH 3/4] make mock-wallet times relative --- lib/plugins/wallet/mock-wallet/mock-wallet.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index 85dc291a..445ae11b 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -6,8 +6,8 @@ const NAME = 'FakeWallet' const SECONDS = 1000 const PUBLISH_TIME = 2 * SECONDS -const AUTHORIZE_TIME = 8 * SECONDS -const CONFIRM_TIME = 180 * SECONDS +const AUTHORIZE_TIME = PUBLISH_TIME + 6 * SECONDS +const CONFIRM_TIME = AUTHORIZE_TIME + 180 * SECONDS let t0 From 305b3f150d2da9099c308a547c1aea42054f4e7a Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Wed, 26 Jul 2017 01:10:16 +0300 Subject: [PATCH 4/4] remove debug --- lib/settings-loader.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/settings-loader.js b/lib/settings-loader.js index 375265f2..b525f91e 100644 --- a/lib/settings-loader.js +++ b/lib/settings-loader.js @@ -135,8 +135,6 @@ function settings () { function save (config) { const sql = 'insert into user_config (type, data, valid) values ($1, $2, $3)' - console.log('DEBUG800: %s', sql) - return configValidate.validate(config) .then(() => db.none(sql, ['config', {config}, true])) .catch(() => db.none(sql, ['config', {config}, false]))