From 8bb922405db4f0e84461627befe5d941273b754c Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 16 Aug 2018 01:35:34 -0400 Subject: [PATCH] replace init task with seed values This commit makes xud add seed values for pairs and currencies when a new database is created during startup. These are in addition to the seed nodes. An `initDb` config option can be set to `false` to disable initializing new databases with these seed values. This makes the `db:init` task obsolete. --- bin/xud | 5 +++++ instructions-for-devs.md | 3 +++ lib/Config.ts | 2 ++ lib/db/DB.ts | 16 +++++++++++++++- package.json | 1 - tasks/db/init.ts | 33 --------------------------------- 6 files changed, 25 insertions(+), 35 deletions(-) delete mode 100644 tasks/db/init.ts diff --git a/bin/xud b/bin/xud index cf7ccc95a..0aeb6a88a 100755 --- a/bin/xud +++ b/bin/xud @@ -4,6 +4,11 @@ const Xud = require('../dist/Xud').default; const { argv } = require('yargs') .options({ + initDb: { + describe: 'Whether to initialize the db with data', + type: 'boolean', + default: undefined, + }, xudir: { describe: 'Data directory for xud', type: 'string', diff --git a/instructions-for-devs.md b/instructions-for-devs.md index bac2212e7..7104750e7 100644 --- a/instructions-for-devs.md +++ b/instructions-for-devs.md @@ -53,6 +53,7 @@ Use `./xucli --help` to get up-to-date, optional command line arguments to overr Options: --help Show help [boolean] --version Show version number [boolean] + --initDb Whether to initialize the db with data [boolean] --xudir, -x Data directory for xud [string] --db.database SQL database name [string] --db.host Hostname for SQL database [string] @@ -134,6 +135,8 @@ Examples: This *optional* configuration file uses [TOML](https://github.com/toml-lang/toml) and by default should be saved at `~/.xud/xud.conf` on Linux or `AppData\Local\Xud\xud.conf` on Windows (run `xud` at least once for this folder to be created). All options with default values are shown below. ```toml +initDb = true # Whether to initalize a new database with default values + [rpc] port = 8886 host = "localhost" diff --git a/lib/Config.ts b/lib/Config.ts index c1a0b657c..337ed9a36 100644 --- a/lib/Config.ts +++ b/lib/Config.ts @@ -20,6 +20,7 @@ class Config { public raiden: RaidenClientConfig; public webproxy: { port: number, disable: boolean }; public instanceId = 0; + public initDb: boolean; constructor(private args?: Arguments | Object) { const platform = os.platform(); @@ -46,6 +47,7 @@ class Config { } // default configuration + this.initDb = true; this.p2p = { listen: true, port: 8885, // X = 88, U = 85 in ASCII diff --git a/lib/db/DB.ts b/lib/db/DB.ts index 1233d4766..5ddabf601 100644 --- a/lib/db/DB.ts +++ b/lib/db/DB.ts @@ -6,6 +6,7 @@ import Bluebird from 'bluebird'; import Logger from '../Logger'; import { db } from '../types'; +import { SwapProtocol } from '../types/enums'; type SequelizeConfig = { host: string; @@ -68,7 +69,8 @@ class DB { ]); if (newDb) { - // populate new databases with seed nodes + // populate new databases with default data + // TODO: make seed peers configurable await Node.bulkCreate([ { nodePubKey: '02b66438730d1fcdf4a4ae5d3d73e847a272f160fee2938e132b52cab0a0d9cfc6', @@ -83,6 +85,18 @@ class DB { addresses: [{ host: 'xud3.test.exchangeunion.com', port: 8885 }], }, ]); + + await Currency.bulkCreate([ + { id: 'BTC' }, + { id: 'LTC' }, + { id: 'ZRX' }, + { id: 'GNT' }, + ]); + + await Pair.bulkCreate([ + { baseCurrency: 'BTC', quoteCurrency: 'LTC', swapProtocol: SwapProtocol.LND }, + { baseCurrency: 'ZRX', quoteCurrency: 'GNT', swapProtocol: SwapProtocol.RAIDEN }, + ]); } } diff --git a/package.json b/package.json index 7e427b088..54b52c091 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "scripts": { "compile": "cross-os precompile && tsc && cross-os postcompile", "compile:watch": "tsc -w", - "db:init": "gulp db.init", "dev": "npm run compile && npm start", "dev:watch": "concurrently --kill-others \"npm run compile:watch\" \"npm run nodemon:watch\"", "docs": "typedoc --out typedoc --module commonjs --target es6 lib", diff --git a/tasks/db/init.ts b/tasks/db/init.ts deleted file mode 100644 index 3a8b3d1a1..000000000 --- a/tasks/db/init.ts +++ /dev/null @@ -1,33 +0,0 @@ -import Config from '../../lib/Config'; -import DB from '../../lib/db/DB'; -import Logger from '../../lib/Logger'; -import { SwapProtocol } from '../../lib/types/enums'; -import OrderBookRepository from '../../lib/orderbook/OrderBookRepository'; - -export default async (productionDb?: boolean) => { - const config = new Config(); - await config.load(); - const loggers = Logger.createLoggers(config.instanceId); - - const dbConfig = productionDb ? config.db : config.testDb; - const db = new DB(dbConfig, loggers.db); - await db.init(); - - const orderBookRepository = new OrderBookRepository(loggers.orderbook, db.models); - - await orderBookRepository.addCurrencies([ - { id: 'BTC' }, - { id: 'LTC' }, - { id: 'ZRX' }, - { id: 'GNT' }, - ]); - - await Promise.all([, - orderBookRepository.addPairs([ - { baseCurrency: 'BTC', quoteCurrency: 'LTC', swapProtocol: SwapProtocol.LND }, - { baseCurrency: 'ZRX', quoteCurrency: 'GNT', swapProtocol: SwapProtocol.RAIDEN }, - ]), - ]); - - db.close(); -};