Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ocean-api): integrate playground with ocean-api #928

Merged
merged 3 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved

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') {
fuxingloh marked this conversation as resolved.
Show resolved Hide resolved
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()
})
}