Skip to content

Commit

Permalink
feat(ocean-api): integrate playground with ocean-api (#928)
Browse files Browse the repository at this point in the history
* feat(ocean-api): integrate playground with ocean-api

* automatically error if playground is configured on non regtest setup

* Apply suggestions from code review
  • Loading branch information
fuxingloh authored Jan 4, 2022
1 parent 74dd172 commit a0a93ca
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
48 changes: 48 additions & 0 deletions apps/ocean-api/__tests__/modules/PlaygroundModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { OceanApiTesting } from '../../testing/OceanApiTesting'
import waitForExpect from 'wait-for-expect'

describe('regtest', () => {
const apiTesting = OceanApiTesting.create()

beforeEach(async () => {
apiTesting.playgroundEnable(false)
await apiTesting.start()
})

afterEach(async () => {
await apiTesting.stop()
})

it('should not increment block count', async () => {
const initial = await apiTesting.rpc.blockchain.getBlockCount()

await new Promise((resolve) => {
setTimeout(_ => resolve(0), 5000)
})

const next = await apiTesting.rpc.blockchain.getBlockCount()
expect(next).toStrictEqual(initial)
})
})

describe('playground', () => {
const apiTesting = OceanApiTesting.create()

beforeEach(async () => {
apiTesting.playgroundEnable(true)
await apiTesting.start()
})

afterEach(async () => {
await apiTesting.stop()
})

it('should increment block count', async () => {
const initial = await apiTesting.rpc.blockchain.getBlockCount()

await waitForExpect(async () => {
const next = await apiTesting.rpc.blockchain.getBlockCount()
expect(next).toBeGreaterThan(initial)
})
})
})
65 changes: 65 additions & 0 deletions apps/ocean-api/src/modules/PlaygroundModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Injectable, Logger, Module } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Interval, ScheduleModule } from '@nestjs/schedule'
import { BotLogger, Playground } from '@defichain/playground'
import { ApiClient } from '@defichain/jellyfish-api-core'

/**
* Provide a PlaygroundRunner when PLAYGROUND_ENABLE is enabled.
*/
@Module({
imports: [
ScheduleModule.forRoot()
],
providers: [
{
provide: 'PLAYGROUND_RUNNER',
useFactory: (configService: ConfigService, apiClient: ApiClient): PlaygroundRunner | undefined => {
if (configService.get<boolean>('PLAYGROUND_ENABLE') === false) {
return undefined
}

if (configService.get<string>('API_NETWORK') !== 'regtest') {
throw new Error('PLAYGROUND_ENABLE:true is only allowed on API_NETWORK:regtest')
}

return new PlaygroundRunner(apiClient)
},
inject: [ConfigService, ApiClient]
}
]
})
export class PlaygroundModule {
}

/**
* Universal logger for playground using NestJS logger.
*/
class PlaygroundLogger implements BotLogger {
private readonly logger = new Logger(PlaygroundLogger.name)

info (action: string, message: string): void {
this.logger.log(`${action} ${message}`)
}
}

@Injectable()
class PlaygroundRunner {
constructor (
private readonly apiClient: ApiClient,
private readonly logger: PlaygroundLogger = new PlaygroundLogger(),
private readonly playground: Playground = new Playground(apiClient, logger)
) {
}

async onApplicationBootstrap (): Promise<void> {
this.logger.info('onApplicationBootstrap', 'Bootstrapping')
await this.playground.bootstrap()
this.logger.info('onApplicationBootstrap', 'Bootstrapped')
}

@Interval(3000)
async cycle (): Promise<void> {
await this.playground.cycle()
}
}
6 changes: 4 additions & 2 deletions apps/ocean-api/src/modules/RootModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConfigModule } from '@nestjs/config'
import { ControllerModule } from './ControllerModule'
import { BlockchainCppModule } from './BlockchainCppModule'
import { ActuatorModule } from './ActuatorModule'
import { PlaygroundModule } from './PlaygroundModule'

@Module({
imports: [
Expand All @@ -14,7 +15,8 @@ import { ActuatorModule } from './ActuatorModule'
}),
ActuatorModule,
BlockchainCppModule,
ControllerModule
ControllerModule,
PlaygroundModule
]
})
export class RootModule {
Expand All @@ -25,7 +27,7 @@ function ENV_VALIDATION_SCHEMA (): any {
NODE_ENV: Joi.string().valid('production', 'test').default('test'),
PORT: Joi.number().default(3000),
API_VERSION: Joi.string().regex(/^v[0-9]+(\.[0-9]+)?$/).default('v1'),
API_NETWORK: Joi.string().valid('regtest', 'testnet', 'mainnet', 'playground').default('regtest'),
API_NETWORK: Joi.string().valid('regtest', 'testnet', 'mainnet').default('regtest'),
PLAYGROUND_ENABLE: Joi.boolean()
})
}

0 comments on commit a0a93ca

Please sign in to comment.