Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Adds Application.registerInteroperableModule (#8828)
Browse files Browse the repository at this point in the history
* Adds Application.registerInteroperableModule

* Updates Application.registerInteroperableModule to throw if Interoperability module is not registered

* ♻️ ✅  Application.registerInteroperableModule

* ♻️ ✅  Application.registerInteroperableModule
  • Loading branch information
has5aan authored Aug 10, 2023
1 parent 3a280f7 commit 9d974e7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
17 changes: 17 additions & 0 deletions framework/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ import {
MainchainInteroperabilityModule,
SidechainInteroperabilityMethod,
MainchainInteroperabilityMethod,
BaseInteroperableModule,
MODULE_NAME_INTEROPERABILITY,
} from './modules/interoperability';
import { DynamicRewardMethod, DynamicRewardModule } from './modules/dynamic_rewards';
import { Engine } from './engine';
import { BaseInteroperabilityModule } from './modules/interoperability/base_interoperability_module';

const isPidRunning = async (pid: number): Promise<boolean> =>
psList().then(list => list.some(x => x.pid === pid));
Expand Down Expand Up @@ -239,6 +242,20 @@ export class Application {
this._registerModule(Module);
}

public registerInteroperableModule(interoperableModule: BaseInteroperableModule) {
const interoperabilityModule = this._registeredModules.find(
module => module.name === MODULE_NAME_INTEROPERABILITY,
);

if (interoperabilityModule === undefined) {
throw new Error(`${MODULE_NAME_INTEROPERABILITY} module is not registered.`);
}

(interoperabilityModule as BaseInteroperabilityModule).registerInteroperableModule(
interoperableModule,
);
}

public getRegisteredModules(): BaseModule[] {
return this._registeredModules;
}
Expand Down
69 changes: 68 additions & 1 deletion framework/test/unit/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as os from 'os';
import { join } from 'path';
import { BaseEndpoint, BaseMethod, BaseModule, BasePlugin, ModuleMetadata } from '../../src';
import {
BaseCCMethod,
BaseEndpoint,
BaseInteroperableModule,
BaseMethod,
BaseModule,
BasePlugin,
MODULE_NAME_INTEROPERABILITY,
ModuleMetadata,
SidechainInteroperabilityModule,
} from '../../src';
import { Application } from '../../src/application';
import { Bus } from '../../src/controller/bus';
import { WSServer } from '../../src/controller/ws/ws_server';
Expand Down Expand Up @@ -77,6 +87,25 @@ class TestModule extends BaseModule {
}
}

class TestInteroperableModule extends BaseInteroperableModule {
public crossChainMethod = { stores: {}, events: {} } as unknown as BaseCCMethod;
public commands = [];
public endpoint: BaseEndpoint = {} as BaseEndpoint;
public method: BaseMethod = {} as BaseMethod;

public verifyAssets = jest.fn();
public beforeTransactionsExecute = jest.fn();
public afterCommandExecute = jest.fn();

public get name() {
return 'test-interoperable-module';
}

public metadata(): ModuleMetadata {
throw new Error('Method not implemented.');
}
}

describe('Application', () => {
// Arrange
const config = {
Expand Down Expand Up @@ -241,6 +270,44 @@ describe('Application', () => {
});
});

describe('#registerInteroperableModule', () => {
it('should register CCCommands and CCMethods of the registered interoperable module', () => {
const { app } = Application.defaultApplication(config);

const testInteroperableModule = new TestInteroperableModule();

app.registerInteroperableModule(testInteroperableModule);

const interoperabilityModule = app['_registeredModules'].find(
module => module.name === 'interoperability',
) as SidechainInteroperabilityModule;

expect(
interoperabilityModule['interoperableCCCommands'].has(testInteroperableModule.name),
).toBeTrue();

expect(
interoperabilityModule['interoperableCCMethods'].has(testInteroperableModule.name),
).toBeTrue();
});

it('should throw if Interoperability module is not registered', () => {
const { app } = Application.defaultApplication(config);

const index = app['_registeredModules'].findIndex(
module => module.name === MODULE_NAME_INTEROPERABILITY,
);

app['_registeredModules'][index] = new TestModule();

const testInteroperableModule = new TestInteroperableModule();

expect(() => app.registerInteroperableModule(testInteroperableModule)).toThrow(
`${MODULE_NAME_INTEROPERABILITY} module is not registered.`,
);
});
});

describe('#run', () => {
let app: Application;

Expand Down

0 comments on commit 9d974e7

Please sign in to comment.