Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: adjust store schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Sep 8, 2020
1 parent 85c3124 commit 656d4b6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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}`)
Expand Down
14 changes: 9 additions & 5 deletions src/services/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}

Expand All @@ -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[])
Expand All @@ -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()
}
}
},
Expand All @@ -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)
},
Expand Down
12 changes: 8 additions & 4 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import type { Sequelize } from 'sequelize'
export function initStore (sequelize: Sequelize): Promise<void> {
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',
Expand Down
4 changes: 2 additions & 2 deletions test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('CLI', function () {
// create backups
const db = config.get<string>('db')
const backupPath = path.resolve(process.cwd(), config.get<string>('dbBackUp.path'))
mkdirSync(config.get<DbBackUpConfig>('dbBackUp').path)
mkdirSync(path.resolve(config.get<DbBackUpConfig>('dbBackUp').path))
copyFileSync(db, path.resolve(backupPath, `0x0123:10-${db}`))
copyFileSync(db, path.resolve(backupPath, `0x0123:20-${db}`))

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 656d4b6

Please sign in to comment.