From 656d4b6ea8a947c75059a6d5417100e00d8a84aa Mon Sep 17 00:00:00 2001 From: Nazar Duchak Date: Tue, 8 Sep 2020 14:50:34 +0300 Subject: [PATCH] feat: adjust store schema --- src/cli/start.ts | 8 ++++---- src/services/storage/index.ts | 14 +++++++++----- src/store.ts | 12 ++++++++---- test/cli.spec.ts | 4 ++-- test/utils.ts | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/cli/start.ts b/src/cli/start.ts index 003c4ce8..818a33dc 100644 --- a/src/cli/start.ts +++ b/src/cli/start.ts @@ -3,7 +3,7 @@ import { flags } from '@oclif/command' import { appFactory, services } from '../app' import { loggingFactory } from '../logger' -import { Flags, Config, SupportedServices, isSupportedServices } from '../definitions' +import { Flags, Config, SupportedServices, isSupportedServices, Application } from '../definitions' import { BaseCLICommand } from '../utils' import DbBackUpJob from '../db-backup' import { ethFactory } from '../blockchain' @@ -108,13 +108,13 @@ ${formattedServices}` const resetPromise = new Promise(resolve => { appFactory({ appResetCallBack: () => resolve() - }).then(({ app, stop }) => { + }).then((application: { app: Application, stop: () => void }) => { // Lets save the function that stops the app - stopCallback = stop + stopCallback = application.stop // Start server const port = config.get('port') - const server = app.listen(port) + const server = application.app.listen(port) server.on('listening', () => logger.info(`Server started on port ${port}`) diff --git a/src/services/storage/index.ts b/src/services/storage/index.ts index e42d1124..3ca709be 100644 --- a/src/services/storage/index.ts +++ b/src/services/storage/index.ts @@ -125,7 +125,7 @@ const storage: CachedService = { const reorgEmitterService = app.service(ServiceAddresses.REORG_EMITTER) // We require services to be precached before running server - if (!isServiceInitialized(`${SERVICE_NAME}.storageManager`)) { + if (!isServiceInitialized(`${SERVICE_NAME}.${STORAGE_MANAGER}`)) { return logger.critical('Storage service is not initialized! Run precache command.') } @@ -140,6 +140,7 @@ const storage: CachedService = { }) storageManagerEventsEmitter.on('newConfirmation', (data) => confirmationService.emit('newConfirmation', data)) storageManagerEventsEmitter.on('invalidConfirmation', (data) => confirmationService.emit('invalidConfirmation', data)) + storageManagerEventsEmitter.on(REORG_OUT_OF_RANGE_EVENT_NAME, (blockNumber: number) => reorgEmitterService.emitReorg(blockNumber, 'storage')) // Staking watcher const stakingEventsEmitter = getEventsEmitterForService(`${SERVICE_NAME}.${STAKING}`, eth, stakingContract.abi as AbiItem[]) @@ -149,12 +150,13 @@ const storage: CachedService = { }) stakingEventsEmitter.on('newConfirmation', (data) => confirmationService.emit('newConfirmation', data)) stakingEventsEmitter.on('invalidConfirmation', (data) => confirmationService.emit('invalidConfirmation', data)) - eventsEmitter.on(REORG_OUT_OF_RANGE_EVENT_NAME, (blockNumber: number) => reorgEmitterService.emitReorg(blockNumber, 'storage')) + stakingEventsEmitter.on(REORG_OUT_OF_RANGE_EVENT_NAME, (blockNumber: number) => reorgEmitterService.emitReorg(blockNumber, 'storage')) return { stop: () => { confirmationService.removeAllListeners() - eventsEmitter.stop() + stakingEventsEmitter.stop() + storageManagerEventsEmitter.stop() } } }, @@ -167,8 +169,10 @@ const storage: CachedService = { logger.info(`Removed ${priceCount} billing plans entries, ${stakeCount} stakes, ${offersCount} offers and ${agreementsCount} agreements`) const store = getObject() - delete store['storage.lastFetchedBlockNumber'] - delete store['storage.lastFetchedBlockHash'] + delete store['storage.storageManager.lastFetchedBlockNumber'] + delete store['storage.staking.lastFetchedBlockNumber'] + delete store['storage.storageManager.lastFetchedBlockHash'] + delete store['storage.staking.lastFetchedBlockHash'] await sleep(1000) }, diff --git a/src/store.ts b/src/store.ts index d200006e..abf8e505 100644 --- a/src/store.ts +++ b/src/store.ts @@ -5,10 +5,14 @@ import type { Sequelize } from 'sequelize' export function initStore (sequelize: Sequelize): Promise { return init(sequelize, { 'blockchain.lastFetchedBlock': 'json', - 'storage.lastFetchedBlockNumber': 'int', - 'storage.lastFetchedBlockHash': 'string', - 'storage.lastProcessedBlockNumber': 'int', - 'storage.lastProcessedBlockHash': 'string', + 'storage.storageManager.lastFetchedBlockNumber': 'int', + 'storage.storageManager.lastFetchedBlockHash': 'string', + 'storage.storageManager.lastProcessedBlockNumber': 'int', + 'storage.storageManager.lastProcessedBlockHash': 'string', + 'storage.staking.lastFetchedBlockNumber': 'int', + 'storage.staking.lastFetchedBlockHash': 'string', + 'storage.staking.lastProcessedBlockNumber': 'int', + 'storage.staking.lastProcessedBlockHash': 'string', 'rates.lastUpdate': 'int', 'rns.owner.lastFetchedBlockNumber': 'int', 'rns.owner.lastFetchedBlockHash': 'string', diff --git a/test/cli.spec.ts b/test/cli.spec.ts index fc6057a0..e099a715 100644 --- a/test/cli.spec.ts +++ b/test/cli.spec.ts @@ -52,7 +52,7 @@ describe('CLI', function () { // create backups const db = config.get('db') const backupPath = path.resolve(process.cwd(), config.get('dbBackUp.path')) - mkdirSync(config.get('dbBackUp').path) + mkdirSync(path.resolve(config.get('dbBackUp').path)) copyFileSync(db, path.resolve(backupPath, `0x0123:10-${db}`)) copyFileSync(db, path.resolve(backupPath, `0x0123:20-${db}`)) @@ -79,7 +79,7 @@ describe('CLI', function () { initAppStub.callsFake((options: AppModule.AppOptions): Promise<{ app: Application, stop: () => void }> => { appResetCallback = options.appResetCallBack - return Promise.resolve({ app: {} as Application, stop: stopSpy }) + return Promise.resolve({ app: { listen: () => ({ on: () => undefined }) } as unknown as Application, stop: stopSpy }) }) sinon.stub(blockchainUtils, 'ethFactory').returns({ getBlock: sinon.stub().resolves(true) diff --git a/test/utils.ts b/test/utils.ts index 842d2cee..468ff2b4 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -76,7 +76,7 @@ export function blockMock (blockNumber: number, blockHash = '0x123', options: Pa return block } -export function rmDir (folder: string) { +export function rmDir (folder: string): void { if (fs.existsSync(folder)) { for (const file of fs.readdirSync(folder)) { fs.unlinkSync(path.join(folder, file))