From 755112efd979d1e334ee7d767f8ca58e877224b2 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 16 Oct 2019 13:25:08 +0530 Subject: [PATCH 01/12] Changes done to use token config in facilitator init --- src/Config/Config.ts | 20 ++++---- src/Config/ConfigFactory.ts | 27 +++++++++-- src/SeedData.ts | 33 ++++--------- src/bin/facilitator-init.ts | 82 +++++++++++++++++++++++++++----- src/bin/facilitator.ts | 2 +- test/Config/constructor.test.ts | 10 ++-- test/Config/fromChain.test.ts | 15 ++++-- test/Config/fromFile.test.ts | 19 ++++++-- test/SeedData/populateDb.test.ts | 38 +++++++-------- 9 files changed, 163 insertions(+), 83 deletions(-) diff --git a/src/Config/Config.ts b/src/Config/Config.ts index e73df7bf..8612595e 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -30,6 +30,7 @@ import { import Logger from '../Logger'; import Utils from '../Utils'; import schema from './FacilitatorConfig.schema.json'; +import GatewayAddresses from "./GatewayAddresses"; // Database password key to read from env. const ENV_DB_PASSWORD = 'MOSAIC_FACILITATOR_DB_PASSWORD'; @@ -254,7 +255,7 @@ export class FacilitatorConfig { export class Config { public facilitator: FacilitatorConfig; - public mosaic: MosaicConfig; + public gatewayAddresses: GatewayAddresses; private _originWeb3?: Web3; @@ -262,15 +263,15 @@ export class Config { /** * It would set mosaic config and facilitator config object. - * @param mosaicConfig Mosaic config object. + * @param gatewayAddresses GatewayAddresses object. * @param facilitatorConfig Facilitator config object. */ public constructor( - mosaicConfig: MosaicConfig, + gatewayAddresses: GatewayAddresses, facilitatorConfig: FacilitatorConfig, ) { + this.gatewayAddresses = gatewayAddresses; this.facilitator = facilitatorConfig; - this.mosaic = mosaicConfig; } /** @@ -316,7 +317,7 @@ export class Config { * mosaic config path or facilitator config path doesn't exists. * @param mosaicConfigPath Path to mosaic config file path. * @param facilitatorConfigPath Path to facilitator config file path/ - * @returns Config object consisting of mosaic and facilitator configurations. + * @returns Config object consisting of gatewayAddresses and facilitator configurations. */ public static fromFile( mosaicConfigPath: string, @@ -324,8 +325,8 @@ export class Config { ): Config { const mosaic: MosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); const facilitator: FacilitatorConfig = FacilitatorConfig.fromFile(facilitatorConfigPath); - - return new Config(mosaic, facilitator); + const gatewayAddresses = GatewayAddresses.fromMosaicConfig(mosaic, facilitator.auxChainId); + return new Config(gatewayAddresses, facilitator); } /** @@ -333,7 +334,7 @@ export class Config { * default location, it will initialize new config objects. * @param originChain Origin chain id. * @param auxiliaryChain Auxiliary chain id. - * @returns Config object consisting of mosaic and facilitator configurations. + * @returns Config object consisting of gatewayAddresses and facilitator configurations. */ public static fromChain( originChain: string, @@ -341,6 +342,7 @@ export class Config { ): Config { const mosaic: MosaicConfig = MosaicConfig.fromChain(originChain); const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(auxiliaryChain); - return new Config(mosaic, facilitator); + const gatewayAddresses = GatewayAddresses.fromMosaicConfig(mosaic, auxiliaryChain); + return new Config(gatewayAddresses, facilitator); } } diff --git a/src/Config/ConfigFactory.ts b/src/Config/ConfigFactory.ts index 73de13eb..9b41ed4d 100644 --- a/src/Config/ConfigFactory.ts +++ b/src/Config/ConfigFactory.ts @@ -18,6 +18,7 @@ import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import { FacilitatorStartException } from '../Exception'; import { Config, FacilitatorConfig } from './Config'; +import GatewayAddresses from "./GatewayAddresses"; /* eslint-disable @typescript-eslint/no-non-null-assertion */ @@ -85,19 +86,32 @@ export default class ConfigFactory { } const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain!); - return new Config(mosaicConfig, facilitatorConfig); + // todo: convert to gatewayaddresses object from mosaic config. + const gatewayAddresses = GatewayAddresses.fromMosaicConfig( + mosaicConfig, + facilitatorConfig.auxChainId + ); + return new Config(gatewayAddresses, facilitatorConfig); } if (this.mosaicConfigPath) { const mosaic: MosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); this.verifyChainIdInMosaicConfig(mosaic); const facilitator = FacilitatorConfig.fromChain(this.auxChainId!); - return new Config(mosaic, facilitator); + const gatewayAddresses = GatewayAddresses.fromMosaicConfig( + mosaic, + facilitator.auxChainId, + ); + return new Config(gatewayAddresses, facilitator); } const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId!); const mosaic: MosaicConfig = MosaicConfig.fromChain(this.originChain!); - return new Config(mosaic, facilitator); + const gatewayAddresses = GatewayAddresses.fromMosaicConfig( + mosaic, + facilitator.auxChainId, + ); + return new Config(gatewayAddresses, facilitator); } /** @@ -112,6 +126,7 @@ export default class ConfigFactory { configObj = Config.fromFile(this.mosaicConfigPath, this.facilitatorConfigPath!); this.originChain = configObj.facilitator.originChain; this.auxChainId = configObj.facilitator.auxChainId; + // todo: may be think about chain id in mosaic config in gateway config. this.verifyChainIdInMosaicConfig(configObj.mosaic); } else { const facilitatorConfig: FacilitatorConfig = FacilitatorConfig.fromFile( @@ -120,7 +135,11 @@ export default class ConfigFactory { const mosaicConfig: MosaicConfig = MosaicConfig.fromChain( facilitatorConfig.originChain, ); - configObj = new Config(mosaicConfig, facilitatorConfig); + const gatewayAddresses = GatewayAddresses.fromMosaicConfig( + mosaicConfig, + facilitatorConfig.auxChainId, + ); + configObj = new Config(gatewayAddresses, facilitatorConfig); } return configObj; } diff --git a/src/SeedData.ts b/src/SeedData.ts index 2f910c3f..f651551f 100644 --- a/src/SeedData.ts +++ b/src/SeedData.ts @@ -22,9 +22,6 @@ import { interacts } from '@openst/mosaic-contracts'; import { EIP20CoGateway } from '@openst/mosaic-contracts/dist/interacts/EIP20CoGateway'; import { EIP20Gateway } from '@openst/mosaic-contracts/dist/interacts/EIP20Gateway'; -import { - AuxiliaryChain as AuxiliaryChainMosaicConfig, OriginChain as OriginChainMosaicConfig, -} from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import { Config } from './Config/Config'; import Utils from './Utils'; import AuxiliaryChain from './models/AuxiliaryChain'; @@ -196,26 +193,12 @@ export default class SeedData { await Promise.all(promises); } - /** - * @return Returns auxiliary chain mosaic config. - */ - private get auxiliaryChainMosaicConfig(): AuxiliaryChainMosaicConfig { - return this.config.mosaic.auxiliaryChains[this.config.facilitator.auxChainId]; - } - - /** - * @return Returns origin chain mosaic config. - */ - private get originChainMosaicConfig(): OriginChainMosaicConfig { - return this.config.mosaic.originChain; - } - /** * @return Returns Gateway address. */ private get gatewayAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.origin.eip20GatewayAddress, + this.config.gatewayAddresses.originGatewayAddress, ); } @@ -224,7 +207,7 @@ export default class SeedData { */ private get coGatewayAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.auxiliary.eip20CoGatewayAddress, + this.config.gatewayAddresses.auxiliaryGatewayAddress, ); } @@ -233,7 +216,7 @@ export default class SeedData { */ private get anchorAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.origin.anchorAddress, + this.config.gatewayAddresses.originAnchorAddress, ); } @@ -242,7 +225,7 @@ export default class SeedData { */ private get coAnchorAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.auxiliary.anchorAddress, + this.config.gatewayAddresses.auxiliaryAnchorAddress, ); } @@ -251,7 +234,7 @@ export default class SeedData { */ private get stakePoolAddress(): string { return Utils.toChecksumAddress( - this.originChainMosaicConfig.contractAddresses.stakePoolAddress, + this.config.gatewayAddresses.stakePoolAddress, ); } @@ -260,7 +243,7 @@ export default class SeedData { */ private get redeemPoolAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.auxiliary.redeemPoolAddress, + this.config.gatewayAddresses.redeemPoolAddress, ); } @@ -269,7 +252,7 @@ export default class SeedData { */ private get utilityTokenAddress(): string { return Utils.toChecksumAddress( - this.auxiliaryChainMosaicConfig.contractAddresses.auxiliary.utilityTokenAddress, + this.config.gatewayAddresses.utilityTokenAddress, ); } @@ -278,7 +261,7 @@ export default class SeedData { */ private get valueTokenAddress(): string { return Utils.toChecksumAddress( - this.originChainMosaicConfig.contractAddresses.valueTokenAddress, + this.config.gatewayAddresses.valueTokenAddress, ); } diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 5a8d5393..bb4b1231 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -28,10 +28,66 @@ import Logger from '../Logger'; import Repositories from '../repositories/Repositories'; import SeedData from '../SeedData'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; +import GatewayAddresses from '../Config/GatewayAddresses'; + +/** + * It provides gateway addresses and origin chain id. It is to be used when mosaic + * config is provided. + * @param auxChainId Auxiliary chain id. + * @param mosaicConfigPath Path to mosaic config. + * @returns originchain id and gatewayaddresses object. + */ +function getFromMosaicConfig( + auxChainId: number, + mosaicConfigPath: string +): { + originChainId: string; gatewayAddresses: GatewayAddresses +} { + const mosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); + const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; + if (auxChain === null || auxChain === undefined) { + Logger.error(`auxchain id ${auxChainId} is not present in the mosaic config`); + process.exit(1); + } + + return { + originChainId: mosaicConfig.originChain.chain, + gatewayAddresses: GatewayAddresses.fromMosaicConfig(mosaicConfig, auxChainId) + }; +} + +/** + * It provides gateway addresses and origin chain id. It is to be used when gateway + * config is provided. + * @param auxChainId Auxiliary chain id. + * @param gatewayConfigPath Path to gateway config. + * @returns originchain id and gatewayaddresses object. + */ +function getFromGatewayConfig( + auxChainId: number, + gatewayConfigPath: string +): { + originChainId: string; gatewayAddresses: GatewayAddresses +} { + const gatewayConfig = GatewayConfig.fromFile(gatewayConfigPath); + + if (auxChainId === gatewayConfig.auxChainId) { + Logger.error(`aux chain id present in gateway config is ${gatewayConfig.auxChainId}`+ + `but ${auxChainId} is specified`); + process.exit(1); + } + const gatewayAddresses = GatewayAddresses.fromGatewayConfig(gatewayConfig); + return { + originChainId: gatewayConfig.mosaicConfig.originChain.chain, + gatewayAddresses + } +} commander .option('-m, --mosaic-config ', 'path to mosaic configuration') + .option('-t, --gateway-config ', 'path to gateway configuration') .option('-c, --aux-chain-id ', 'auxiliary chain id') .option('-o, --origin-password ', 'origin chain account password') .option('-a, --auxiliary-password ', 'auxiliary chain account password') @@ -47,9 +103,10 @@ commander // Validating mandatory parameters let mandatoryOptionMissing = false; - if (options.mosaicConfig === undefined) { - Logger.error('required --mosaic-config '); - mandatoryOptionMissing = true; + if(options.mosaicConfig && options.gatewayConfig) { + Logger.error('both mosaic and gateway config is provided. ' + + 'only one of the option is expected. refer readme for more details'); + process.exit(1); } const { auxChainId } = options; @@ -118,14 +175,14 @@ commander const facilitatorConfig = FacilitatorConfig.fromChain(auxChainId); // Get origin chain id. - const mosaicConfig = MosaicConfig.fromFile(options.mosaicConfig); - const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; - if (auxChain === null || auxChain === undefined) { - Logger.error('aux chain id is not present in the mosaic config'); - process.exit(1); - } + const { + originChainId, + gatewayAddresses, + } = options.mosaicConfig !== undefined ? getFromMosaicConfig(auxChainId, options.mosaicConfig) : + getFromGatewayConfig(auxChainId, options.tokenConfig); - const originChainId = mosaicConfig.originChain.chain; + facilitatorConfig.originChain = originChainId; + facilitatorConfig.auxChainId = auxChainId; let { dbPath } = options; if (dbPath === undefined || dbPath === null) { @@ -139,8 +196,7 @@ commander } facilitatorConfig.database.path = dbPath; - facilitatorConfig.originChain = originChainId; - facilitatorConfig.auxChainId = auxChainId; + const setFacilitator = ( chainId: string, rpc: string, @@ -173,7 +229,7 @@ commander options.auxiliaryPassword, ); - const config = new Config(mosaicConfig, facilitatorConfig); + const config = new Config(gatewayAddresses, facilitatorConfig); const repositories = await Repositories.create(config.facilitator.database.path); const seedData = new SeedData( config, diff --git a/src/bin/facilitator.ts b/src/bin/facilitator.ts index 6cabef30..241849a7 100644 --- a/src/bin/facilitator.ts +++ b/src/bin/facilitator.ts @@ -20,6 +20,6 @@ import facilitator from 'commander'; facilitator - .command('init ', 'initializes the facilitator config') + .command('init ', 'initializes the facilitator config') .command('start ', 'starts the facilitator') .parse(process.argv); diff --git a/test/Config/constructor.test.ts b/test/Config/constructor.test.ts index a58a5295..84367c76 100644 --- a/test/Config/constructor.test.ts +++ b/test/Config/constructor.test.ts @@ -17,15 +17,15 @@ import sinon from 'sinon'; -import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import { Config } from '../../src/Config/Config'; import assert from '../test_utils/assert'; +import GatewayAddresses from '../../src/Config/GatewayAddresses'; describe('Config.constructor()', () => { it('should pass with valid arguments', () => { - const mosaic = sinon.createStubInstance(MosaicConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); const facilitator = sinon.fake() as any; - const config = new Config(mosaic, facilitator); + const config = new Config(gatewayAddresses, facilitator); assert.strictEqual( config.facilitator, @@ -33,8 +33,8 @@ describe('Config.constructor()', () => { 'Facilitator object is different', ); assert.strictEqual( - config.mosaic, - mosaic, + config.gatewayAddresses, + gatewayAddresses, 'Mosaic object is different', ); diff --git a/test/Config/fromChain.test.ts b/test/Config/fromChain.test.ts index 340d96b5..0c75603f 100644 --- a/test/Config/fromChain.test.ts +++ b/test/Config/fromChain.test.ts @@ -21,6 +21,7 @@ import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import { Config, FacilitatorConfig } from '../../src/Config/Config'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; +import GatewayAddresses from '../../src/Config/GatewayAddresses'; describe('Config.fromChain()', () => { const originChain = '2'; @@ -28,6 +29,7 @@ describe('Config.fromChain()', () => { it('should pass with valid arguments', () => { const mosaic = sinon.createStubInstance(MosaicConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); const facilitator = sinon.fake(FacilitatorConfig); const mosaicConfigSpy = sinon.replace( @@ -36,6 +38,12 @@ describe('Config.fromChain()', () => { sinon.fake.returns(mosaic), ); + const gatewayAddressesSpy = sinon.replace( + GatewayAddresses, + 'fromMosaicConfig', + sinon.fake.returns(gatewayAddresses), + ); + const facilitatorConfigSpy = sinon.replace( FacilitatorConfig, 'fromChain', @@ -45,15 +53,16 @@ describe('Config.fromChain()', () => { const config = Config.fromChain(originChain, auxChain); SpyAssert.assert(mosaicConfigSpy, 1, [[originChain]]); SpyAssert.assert(facilitatorConfigSpy, 1, [[auxChain]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); assert.strictEqual( config.facilitator, facilitator as any, 'Facilitator object is different', ); assert.strictEqual( - config.mosaic, - mosaic, - 'Mosaic object is different', + config.gatewayAddresses, + gatewayAddresses, + 'GatewayAddresses object is different', ); sinon.restore(); diff --git a/test/Config/fromFile.test.ts b/test/Config/fromFile.test.ts index a9144b1e..5fb1304a 100644 --- a/test/Config/fromFile.test.ts +++ b/test/Config/fromFile.test.ts @@ -21,14 +21,18 @@ import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import { Config, FacilitatorConfig } from '../../src/Config/Config'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; +import GatewayAddresses from "../../src/Config/GatewayAddresses"; describe('Config.fromFile()', () => { const mosaicConfigPath = 'test/Config/mosaic-config.json'; const facilitatorConfigPath = 'test/Config/facilitator-config.json'; + const auxChain = 3; it('should pass with valid arguments', () => { const mosaic = sinon.createStubInstance(MosaicConfig); - const facilitator = FacilitatorConfig.fromChain(3); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const facilitator = FacilitatorConfig.fromChain(auxChain); + facilitator.auxChainId = auxChain; const mosaicConfigSpy = sinon.replace( MosaicConfig, @@ -36,6 +40,12 @@ describe('Config.fromFile()', () => { sinon.fake.returns(mosaic), ); + const gatewayAddressesSpy = sinon.replace( + GatewayAddresses, + 'fromMosaicConfig', + sinon.fake.returns(gatewayAddresses), + ); + const facilitatorConfigSpy = sinon.replace( FacilitatorConfig, 'fromFile', @@ -46,15 +56,16 @@ describe('Config.fromFile()', () => { SpyAssert.assert(mosaicConfigSpy, 1, [[mosaicConfigPath]]); SpyAssert.assert(facilitatorConfigSpy, 1, [[facilitatorConfigPath]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); assert.strictEqual( config.facilitator, facilitator, 'Facilitator object is different', ); assert.strictEqual( - config.mosaic, - mosaic, - 'Mosaic object is different', + config.gatewayAddresses, + gatewayAddresses, + 'GatewayAddresses object is different', ); sinon.restore(); diff --git a/test/SeedData/populateDb.test.ts b/test/SeedData/populateDb.test.ts index 68df318d..2b93d4ae 100644 --- a/test/SeedData/populateDb.test.ts +++ b/test/SeedData/populateDb.test.ts @@ -42,14 +42,14 @@ describe('SeedData.populateDb()', (): void => { const originChain = '12346'; const auxiliaryChainId = 301; const zeroBn = new BigNumber('0'); - const ostComposerAddress = Web3Utils.toChecksumAddress('0x3c8ba8caecb60c67d69605a772ae1bb9a732fb38'); + const stakePoolAddress = Web3Utils.toChecksumAddress('0x3c8ba8caecb60c67d69605a772ae1bb9a732fb38'); const redeemPoolAddress = Web3Utils.toChecksumAddress('0x8bA9C19BeacBB3eF85E1Df57ceef1Df922F2D87F'); - const ostGatewayAddress = '0x97BA58DBE58898F2B669C56496f46F638DC322d4'; - const ostCoGatewayAddress = '0x40ce8B8EDEb678ea3aD1c9628924C903f8d04227'; + const eip20GatewayAddress = '0x97BA58DBE58898F2B669C56496f46F638DC322d4'; + const eip20CoGatewayAddress = '0x40ce8B8EDEb678ea3aD1c9628924C903f8d04227'; const anchorAddress = '0xaC80704c80AB83512b48314bDfa82f79923C2Fbe'; const coAnchorAddress = '0xBe26124167E8a350eE806B3ba11Ddb6c8E6dc689'; const valueTokenAddress = '0x325f05a75999347b7d8461BaEf274afAE0B8AE1c'; - const ostPrimeAddress = '0x0d3E57044B1B96a257fB2ba3958c1130219A2d55'; + const utilityTokenAddress = '0x0d3E57044B1B96a257fB2ba3958c1130219A2d55'; const currentTimestamp = Utils.getCurrentTimestamp(); /** @@ -59,8 +59,8 @@ describe('SeedData.populateDb()', (): void => { const auxiliaryChain = new AuxiliaryChain( auxiliaryChainId, originChain, - ostGatewayAddress, - ostCoGatewayAddress, + eip20GatewayAddress, + eip20CoGatewayAddress, anchorAddress, coAnchorAddress, zeroBn, @@ -78,17 +78,17 @@ describe('SeedData.populateDb()', (): void => { */ async function verifyGatewayData(): Promise { const gateway = new Gateway( - ostGatewayAddress, + eip20GatewayAddress, originChain, GatewayType.Origin, - ostCoGatewayAddress, + eip20CoGatewayAddress, valueTokenAddress, anchorAddress, zeroBn, zeroBn, true, ); - const gatewayFromDb = await repositories.gatewayRepository.get(ostGatewayAddress); + const gatewayFromDb = await repositories.gatewayRepository.get(eip20GatewayAddress); GatewayRepositoryUtil.assertGatewayAttributes(gateway, gatewayFromDb as Gateway); } @@ -97,16 +97,16 @@ describe('SeedData.populateDb()', (): void => { */ async function verifyCoGatewayData(): Promise { const gateway = new Gateway( - ostCoGatewayAddress, + eip20CoGatewayAddress, auxiliaryChainId.toString(), GatewayType.Auxiliary, - ostGatewayAddress, - ostPrimeAddress, + eip20GatewayAddress, + utilityTokenAddress, coAnchorAddress, zeroBn, zeroBn, ); - const gatewayFromDb = await repositories.gatewayRepository.get(ostCoGatewayAddress); + const gatewayFromDb = await repositories.gatewayRepository.get(eip20CoGatewayAddress); GatewayRepositoryUtil.assertGatewayAttributes(gateway, gatewayFromDb as Gateway); } @@ -123,12 +123,12 @@ describe('SeedData.populateDb()', (): void => { */ async function verifyOstComposerRelatedContractEntities(): Promise { const contractEntity = new ContractEntity( - ostComposerAddress, + stakePoolAddress, EntityType.StakeRequesteds, currentTimestamp, ); const contractEntityFromDb = await repositories.contractEntityRepository.get( - ostComposerAddress, + stakePoolAddress, EntityType.StakeRequesteds, ); ContractEntityRepositoryUtil.assertion(contractEntity, contractEntityFromDb as ContractEntity); @@ -164,12 +164,12 @@ describe('SeedData.populateDb()', (): void => { const promises = []; for (let i = 0; i < eventTypes.length; i += 1) { const contractEntity = new ContractEntity( - ostGatewayAddress, + eip20GatewayAddress, eventTypes[i], currentTimestamp, ); const promise = repositories.contractEntityRepository.get( - ostGatewayAddress, + eip20GatewayAddress, eventTypes[i], ).then((contractEntityFromDb) => { ContractEntityRepositoryUtil.assertion( @@ -229,12 +229,12 @@ describe('SeedData.populateDb()', (): void => { const promises = []; for (let i = 0; i < eventTypes.length; i += 1) { const contractEntity = new ContractEntity( - ostCoGatewayAddress, + eip20CoGatewayAddress, eventTypes[i], currentTimestamp, ); const promise = repositories.contractEntityRepository.get( - ostCoGatewayAddress, + eip20CoGatewayAddress, eventTypes[i], ).then((contractEntityFromDb) => { ContractEntityRepositoryUtil.assertion( From 0e0ee780db70805ad44c0fad20f8ebd61af0025a Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Thu, 17 Oct 2019 14:23:41 +0530 Subject: [PATCH 02/12] Added gatewayconfig option in facilitator init --- src/Container.ts | 2 +- src/bin/facilitator-init.ts | 18 +++++++++++------- src/bin/facilitator-start.ts | 2 ++ ...f056b1320fE619571849f138Cd1Ae2f2e64179.json | 15 +++++++++++++++ test_integration/mosaic.json | 2 +- 5 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 test_integration/0xA7f056b1320fE619571849f138Cd1Ae2f2e64179.json diff --git a/src/Container.ts b/src/Container.ts index 83fe594e..a38327cf 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -36,7 +36,7 @@ export default class Container { public static async create( originChain?: string, auxChainId?: string, - mosaicConfigPath?: string, + mosaicConfigPath?: string, // todo: may be only config. facilitatorConfigPath?: string, ): Promise { Logger.debug('Reading config file'); diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index bb4b1231..36fe4524 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -74,7 +74,7 @@ function getFromGatewayConfig( if (auxChainId === gatewayConfig.auxChainId) { Logger.error(`aux chain id present in gateway config is ${gatewayConfig.auxChainId}`+ - `but ${auxChainId} is specified`); + `but ${auxChainId} is specified in the command`); process.exit(1); } @@ -87,7 +87,7 @@ function getFromGatewayConfig( commander .option('-m, --mosaic-config ', 'path to mosaic configuration') - .option('-t, --gateway-config ', 'path to gateway configuration') + .option('-q, --gateway-config ', 'path to gateway configuration') .option('-c, --aux-chain-id ', 'auxiliary chain id') .option('-o, --origin-password ', 'origin chain account password') .option('-a, --auxiliary-password ', 'auxiliary chain account password') @@ -102,10 +102,14 @@ commander .action(async (options) => { // Validating mandatory parameters let mandatoryOptionMissing = false; - - if(options.mosaicConfig && options.gatewayConfig) { - Logger.error('both mosaic and gateway config is provided. ' + - 'only one of the option is expected. refer readme for more details'); + console.log('options :- ',options); + + if( + (options.mosaicConfig && options.gatewayConfig) || + (options.gatewayConfig === undefined && options.mosaicConfig === undefined) + ) { + Logger.error('only one option out of gateway config and mosaic config is required. ' + + 'refer readme for more details'); process.exit(1); } @@ -179,7 +183,7 @@ commander originChainId, gatewayAddresses, } = options.mosaicConfig !== undefined ? getFromMosaicConfig(auxChainId, options.mosaicConfig) : - getFromGatewayConfig(auxChainId, options.tokenConfig); + getFromGatewayConfig(auxChainId, options.gatewayConfig); facilitatorConfig.originChain = originChainId; facilitatorConfig.auxChainId = auxChainId; diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index 254430bc..a35447b3 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -39,9 +39,11 @@ process.on('SIGTERM', terminationHandler); facilitatorCmd .option('-m, --mosaic-config ', 'path to mosaic configuration') + .option('-q, --gateway-config ', 'path to gateway configuration') .option('-t, --facilitator-config ', 'path to facilitator configuration') .action(async (origin_chain, aux_chain_id, options) => { try { + // todo: call to config factory to get config facilitator = await Container.create( origin_chain, aux_chain_id, diff --git a/test_integration/0xA7f056b1320fE619571849f138Cd1Ae2f2e64179.json b/test_integration/0xA7f056b1320fE619571849f138Cd1Ae2f2e64179.json new file mode 100644 index 00000000..92d010df --- /dev/null +++ b/test_integration/0xA7f056b1320fE619571849f138Cd1Ae2f2e64179.json @@ -0,0 +1,15 @@ +{ + "mosaicConfigFilePath":"./test_integration/mosaic.json", + "auxChainId":1000, + "originContracts":{ + "baseTokenAddress":"0x8e183Fd2cd55C7C05bBf4FAC989740f69e559A6d", + "valueTokenAddress":"0x8e183Fd2cd55C7C05bBf4FAC989740f69e559A6d", + "gatewayOrganizationAddress":"0x3f99f42d226A0CD1C1Fcae1e8dC11b2f7a9DcE4B", + "eip20GatewayAddress":"0xaE02C7b1C324A8D94A564bC8d713Df89eae441fe" + }, + "auxiliaryContracts":{ + "coGatewayOrganizationAddress":"0x2D586C7E220839a9284888B10aDF4823AcD6EdF3", + "utilityTokenAddress":"0x62F8729C1C282C231a22252e90CE9735533D2518", + "eip20CoGatewayAddress":"0xc6fF898ceBf631eFb58eEc7187E4c1f70AE8d943" + } +} diff --git a/test_integration/mosaic.json b/test_integration/mosaic.json index 5b734412..b1c12edb 100644 --- a/test_integration/mosaic.json +++ b/test_integration/mosaic.json @@ -16,7 +16,7 @@ ], "contractAddresses": { "origin": { - "baseTokenAddress":"0x7de07272E4c6cb822503B46C8a41517935C3f0c0", + "baseTokenAddress":"0x9AC77F4c0ca4D0F2142D7a77175cf4F1295fb2d8", "anchorOrganizationAddress": "0x7de07272E4c6cb822503B46C8a41517935C3f0c0", "anchorAddress": "0xEa8D41fc6C6C0Ee155E5F6FbF9Cc1167Fa17927E", "gatewayOrganizationAddress": "0xaB5335771fBA0270634e649882229c7e8CaA6158", From f4978421860a449dce06e2770feaaed8ad88bc71 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 18 Oct 2019 18:51:06 +0530 Subject: [PATCH 03/12] Added gateway config option in facilitator-start command; Unit test cases for getConfig method --- README.md | 15 +- src/Config/Config.ts | 20 +- src/Config/ConfigFactory.ts | 82 ++++-- src/Container.ts | 23 +- src/bin/facilitator-init.ts | 62 ++-- src/bin/facilitator-start.ts | 13 +- .../fromGatewayConfig.test.ts | 6 +- test/Config/createWeb3Instance.test.ts | 2 +- test/Config/fromFile.test.ts | 2 +- test/Config/getAuxiliaryWeb3.test.ts | 2 +- test/Config/getOriginWeb3.test.ts | 2 +- test/ConfigFactory/getConfig.test.ts | 268 ++++++++++++++++-- test/SeedData/populateDb.test.ts | 2 +- test_integration/Utils.ts | 7 +- tsconfig.json | 24 +- 15 files changed, 397 insertions(+), 133 deletions(-) diff --git a/README.md b/README.md index be4f5487..320407f9 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,10 @@ Facilitator init command will create facilitator config for an auxiliary chain. A simple run would be the following: ``` -./facilitator init --mosaic-config --aux-chain-id --origin-password --auxiliary-password --origin-rpc --auxiliary-rpc --origin-graph-ws --origin-graph-rpc --auxiliary-graph-ws --auxiliary-graph-rpc --db-path --force +./facilitator init --gateway-config --mosaic-config --aux-chain-id --origin-password --auxiliary-password --origin-rpc --auxiliary-rpc --origin-graph-ws --origin-graph-rpc --auxiliary-graph-ws --auxiliary-graph-rpc --db-path --force ``` +* Replace `` with location where gateway config is present. * Replace `` with location where mosaic config is present. * Replace `` with auxiliary chain id. * Replace `` with the password required to encrypt the worker account of origin chain created with this command. It will be required to unlock worker account while starting facilitator. @@ -29,10 +30,11 @@ Facilitator start command will start the facilitator. Facilitator can be started in below two ways :- -1. `./facilitator start --facilitator-config --mosaic-config ` - * Replace `` with the path to facilitator-config.json using `facilitator init`. - * `--mosaic-config` is optional argument. - * If both `--mosaic-config` is given it will read mosaic and facilitator configs from `` and `` paths respectively. +1. `./facilitator start --facilitator-config --mosaic-config --gateway-config ` + * Replace `` with the path to facilitator-config.json using generated using `facilitator init`. + * `--mosaic-config` and `--gateway-config` is optional argument. + * If both `--mosaic-config` is given it will read mosaic and facilitator configs from `` and `` paths respectively. + * If both `--gateway-config` is given it will read gateway and facilitator configs from `` and `` paths respectively. 2. `./facilitator start ` * Replace `` with name of the origin chain. @@ -42,4 +44,5 @@ Facilitator can be started in below two ways :- * `--mosaic-config` and `--facilitator-config` refers to file path of mosaic config and facilitator config respectively. They are optional fields. * If `--mosaic-config` is given then it will read the facilitator config from default path for `` and mosaic-config from `` path. Argument `` and `` should be present in mosaic-config. * If `--facilitator-config` is given then it will read the mosaic config from default path for `` and facilitator-config from `` path. Argument`` and `` should be present in it. - + * If `--gateway-config` is given then it will read the gateway config from path specified. + * **Note** : Both `--mosaic-config` and `--gateway-config` are together not allowed in command. diff --git a/src/Config/Config.ts b/src/Config/Config.ts index eb68a35d..60cdbac0 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -21,6 +21,7 @@ import path from 'path'; import Web3 from 'web3'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import Account from '../Account'; import Directory from '../Directory'; import { @@ -128,7 +129,7 @@ export class FacilitatorConfig { */ private constructor(config: any) { this.originChain = config.originChain || ''; - this.auxChainId = config.auxChainId || ''; + this.auxChainId = config.auxChainId || 0; this.database = config.database || new DBConfig(); this.chains = {}; this.encryptedAccounts = config.encryptedAccounts || {}; @@ -315,22 +316,25 @@ export class Config { /** * It provides config object from the path specified. This will throw if * mosaic config path or facilitator config path doesn't exists. + * @param facilitatorConfigPath Path to facilitator config file path. * @param mosaicConfigPath Path to mosaic config file path. - * @param facilitatorConfigPath Path to facilitator config file path/ + * @param gatewayConfigPath Path to gateway config file path. * @returns Config object consisting of gateway addresses and facilitator configurations. */ public static fromFile( - mosaicConfigPath: string, facilitatorConfigPath: string, + mosaicConfigPath?: string, + gatewayConfigPath?: string, ): Config { - const mosaic: MosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); const facilitator: FacilitatorConfig = FacilitatorConfig.fromFile(facilitatorConfigPath); + const gatewayAddresses = mosaicConfigPath + ? GatewayAddresses.fromMosaicConfig( + MosaicConfig.fromFile(mosaicConfigPath), + facilitator.auxChainId, + ) : GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(gatewayConfigPath)); return new Config( - GatewayAddresses.fromMosaicConfig( - mosaic, - facilitator.auxChainId, - ), + gatewayAddresses, facilitator, ); } diff --git a/src/Config/ConfigFactory.ts b/src/Config/ConfigFactory.ts index 12c857ce..a65b37dd 100644 --- a/src/Config/ConfigFactory.ts +++ b/src/Config/ConfigFactory.ts @@ -16,6 +16,7 @@ import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import { FacilitatorStartException } from '../Exception'; import { Config, FacilitatorConfig } from './Config'; import GatewayAddresses from './GatewayAddresses'; @@ -34,22 +35,27 @@ export default class ConfigFactory { public facilitatorConfigPath?: string; + public gatewayConfigPath?: string; + /** * @param originChain Name of the origin chain. * @param auxChainId Identifier of the aux chain. * @param mosaicConfigPath Path to mosaic config file. - * @param facilitatorConfigPath Path to facilitator config path. + * @param facilitatorConfigPath Path to facilitator config. + * @param gatewayConfigPath Path to gateway config. */ public constructor( originChain?: string, auxChainId?: number, mosaicConfigPath?: string, facilitatorConfigPath?: string, + gatewayConfigPath?: string, ) { this.originChain = originChain; this.auxChainId = auxChainId; this.mosaicConfigPath = mosaicConfigPath; this.facilitatorConfigPath = facilitatorConfigPath; + this.gatewayConfigPath = gatewayConfigPath; } /** @@ -74,18 +80,20 @@ export default class ConfigFactory { const facilitatorConfig: FacilitatorConfig = FacilitatorConfig.fromFile( this.facilitatorConfigPath, ); - this.verifyChainIdInFacilitatorConfig(facilitatorConfig); - // when mosaic config path is given. if (this.mosaicConfigPath) { const mosaicConfig: MosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); // verify origin chain and aux chain is present in mosaic config. this.verifyChainIdInMosaicConfig(mosaicConfig); - return Config.fromFile(this.mosaicConfigPath, this.facilitatorConfigPath); + return Config.fromFile(this.facilitatorConfigPath, this.mosaicConfigPath, ''); + } + + if (this.gatewayConfigPath) { + return Config.fromFile(this.facilitatorConfigPath, '', this.gatewayConfigPath); } - const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain!); + const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain); return new Config( GatewayAddresses.fromMosaicConfig( @@ -99,7 +107,7 @@ export default class ConfigFactory { if (this.mosaicConfigPath) { const mosaic: MosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); this.verifyChainIdInMosaicConfig(mosaic); - const facilitator = FacilitatorConfig.fromChain(this.auxChainId!); + const facilitator = FacilitatorConfig.fromChain(this.auxChainId); return new Config( GatewayAddresses.fromMosaicConfig( mosaic, @@ -109,8 +117,18 @@ export default class ConfigFactory { ); } - const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId!); - const mosaic: MosaicConfig = MosaicConfig.fromChain(this.originChain!); + if (this.gatewayConfigPath) { + const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId); + const gatewayConfig = GatewayConfig.fromFile(this.gatewayConfigPath); + this.verifyChainIdInGatewayConfig(gatewayConfig); + return new Config( + GatewayAddresses.fromGatewayConfig(gatewayConfig), + facilitator, + ); + } + + const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId); + const mosaic: MosaicConfig = MosaicConfig.fromChain(this.originChain); return new Config( GatewayAddresses.fromMosaicConfig( mosaic, @@ -127,12 +145,12 @@ export default class ConfigFactory { */ private handleFacilitatorConfigOption(): Config { let configObj; + const facilitatorConfig = FacilitatorConfig.fromFile(this.facilitatorConfigPath); + this.auxChainId = facilitatorConfig.auxChainId; + this.originChain = facilitatorConfig.originChain; // When no origin and aux chain provided. if (this.mosaicConfigPath) { const mosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); - const facilitatorConfig = FacilitatorConfig.fromFile(this.facilitatorConfigPath!); - this.auxChainId = facilitatorConfig.auxChainId; - this.originChain = facilitatorConfig.originChain; this.verifyChainIdInMosaicConfig(mosaicConfig); @@ -143,17 +161,27 @@ export default class ConfigFactory { ), facilitatorConfig, ); - } else { - const facilitatorConfig: FacilitatorConfig = FacilitatorConfig.fromFile( - this.facilitatorConfigPath!, + } else if (this.gatewayConfigPath) { + const gatewayConfig = GatewayConfig.fromFile(this.gatewayConfigPath); + + this.verifyChainIdInGatewayConfig(gatewayConfig); + + configObj = new Config( + GatewayAddresses.fromGatewayConfig( + gatewayConfig, + ), + facilitatorConfig, ); + } else { + // only facilitator config is given. const mosaicConfig: MosaicConfig = MosaicConfig.fromChain( - facilitatorConfig.originChain, + this.originChain, ); + this.verifyChainIdInMosaicConfig(mosaicConfig); configObj = new Config( GatewayAddresses.fromMosaicConfig( mosaicConfig, - facilitatorConfig.auxChainId, + this.auxChainId, ), facilitatorConfig, ); @@ -168,13 +196,13 @@ export default class ConfigFactory { private verifyChainIdInFacilitatorConfig( facilitatorConfig: FacilitatorConfig, ): void { - if (facilitatorConfig.chains[this.auxChainId!] === undefined) { + if (facilitatorConfig.chains[this.auxChainId] === undefined) { throw new FacilitatorStartException( `facilitator config is invalid as provided auxchain ${this.auxChainId} is not present`, ); } - if (facilitatorConfig.chains[this.originChain!] === undefined) { + if (facilitatorConfig.chains[this.originChain] === undefined) { throw new FacilitatorStartException( `facilitator config is invalid as provided origin chain ${this.originChain} is not present`, ); @@ -188,16 +216,30 @@ export default class ConfigFactory { private verifyChainIdInMosaicConfig( mosaicConfig: MosaicConfig, ): void { - if (mosaicConfig.auxiliaryChains[this.auxChainId!] === undefined) { + if (mosaicConfig.auxiliaryChains[this.auxChainId] === undefined) { throw new FacilitatorStartException('aux chain is not present in mosaic config'); } - if (mosaicConfig.originChain.chain !== this.originChain) { throw new FacilitatorStartException('origin chain id in mosaic config is different ' + 'than the one provided'); } } + /** + * It verifies chain id's in gateway config. + * @param gatewayConfig GatewayConfig object. + */ + private verifyChainIdInGatewayConfig( + gatewayConfig: GatewayConfig, + ): void { + this.verifyChainIdInMosaicConfig(gatewayConfig.mosaicConfig); + if (gatewayConfig.auxChainId !== this.auxChainId) { + throw new FacilitatorStartException( + `Aux chain id ${gatewayConfig.auxChainId} in gatewayconfig and provided auxchain id ${this.auxChainId} are not same`, + ); + } + } + /** * It verifies whether both origin and aux chain ids are defined. */ diff --git a/src/Container.ts b/src/Container.ts index a38327cf..c8168218 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -15,39 +15,24 @@ // ---------------------------------------------------------------------------- -import ConfigFactory from './Config/ConfigFactory'; import Facilitator from './Facilitator'; import Handlers from './handlers/Handlers'; -import Logger from './Logger'; import Repositories from './repositories/Repositories'; import Services from './services/Services'; import Subscriptions from './subscriptions/Subscriptions'; import TransactionHandler from './TransactionHandler'; +import { Config } from './Config/Config'; export default class Container { /** * This instantiate all the dependencies. - * @param originChain Origin chain Identifier - * @param auxChainId Auxiliary chain ID. - * @param mosaicConfigPath Mosaic Config path. - * @param facilitatorConfigPath Facilitator config path. + * @param config Config object/ * @return Promise that resolves to facilitator instance. */ public static async create( - originChain?: string, - auxChainId?: string, - mosaicConfigPath?: string, // todo: may be only config. - facilitatorConfigPath?: string, + config: Config, + ): Promise { - Logger.debug('Reading config file'); - const configFactory: ConfigFactory = new ConfigFactory( - originChain, - auxChainId ? Number.parseInt(auxChainId, 10) : undefined, - mosaicConfigPath, - facilitatorConfigPath, - ); - const config = configFactory.getConfig(); - Logger.debug('Config loaded successfully.'); const repositories = await Repositories.create(config.facilitator.database.path); const handler = Handlers.create( repositories, diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 36fe4524..874bf8c6 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -19,6 +19,7 @@ import commander from 'commander'; import Web3 from 'web3'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import Account from '../Account'; import { Chain, FacilitatorConfig, Config, ENV_WORKER_PASSWORD_PREFIX, @@ -28,7 +29,6 @@ import Logger from '../Logger'; import Repositories from '../repositories/Repositories'; import SeedData from '../SeedData'; -import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import GatewayAddresses from '../Config/GatewayAddresses'; /** @@ -40,20 +40,20 @@ import GatewayAddresses from '../Config/GatewayAddresses'; */ function getFromMosaicConfig( auxChainId: number, - mosaicConfigPath: string + mosaicConfigPath: string, ): { - originChainId: string; gatewayAddresses: GatewayAddresses -} { - const mosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); - const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; - if (auxChain === null || auxChain === undefined) { - Logger.error(`auxchain id ${auxChainId} is not present in the mosaic config`); - process.exit(1); - } + originChainId: string; gatewayAddresses: GatewayAddresses; + } { + const mosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); + const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; + if (auxChain === null || auxChain === undefined) { + Logger.error(`auxchain id ${auxChainId} is not present in the mosaic config`); + process.exit(1); + } return { originChainId: mosaicConfig.originChain.chain, - gatewayAddresses: GatewayAddresses.fromMosaicConfig(mosaicConfig, auxChainId) + gatewayAddresses: GatewayAddresses.fromMosaicConfig(mosaicConfig, auxChainId), }; } @@ -66,23 +66,23 @@ function getFromMosaicConfig( */ function getFromGatewayConfig( auxChainId: number, - gatewayConfigPath: string + gatewayConfigPath: string, ): { - originChainId: string; gatewayAddresses: GatewayAddresses -} { - const gatewayConfig = GatewayConfig.fromFile(gatewayConfigPath); - - if (auxChainId === gatewayConfig.auxChainId) { - Logger.error(`aux chain id present in gateway config is ${gatewayConfig.auxChainId}`+ - `but ${auxChainId} is specified in the command`); - process.exit(1); - } + originChainId: string; gatewayAddresses: GatewayAddresses; + } { + const gatewayConfig = GatewayConfig.fromFile(gatewayConfigPath); + + if (auxChainId === gatewayConfig.auxChainId) { + Logger.error(`aux chain id present in gateway config is ${gatewayConfig.auxChainId}` + + `but ${auxChainId} is specified in the command`); + process.exit(1); + } const gatewayAddresses = GatewayAddresses.fromGatewayConfig(gatewayConfig); return { originChainId: gatewayConfig.mosaicConfig.originChain.chain, - gatewayAddresses - } + gatewayAddresses, + }; } commander @@ -102,14 +102,14 @@ commander .action(async (options) => { // Validating mandatory parameters let mandatoryOptionMissing = false; - console.log('options :- ',options); + console.log('options :- ', options); - if( - (options.mosaicConfig && options.gatewayConfig) || - (options.gatewayConfig === undefined && options.mosaicConfig === undefined) + if ( + (options.mosaicConfig && options.gatewayConfig) + || (options.gatewayConfig === undefined && options.mosaicConfig === undefined) ) { - Logger.error('only one option out of gateway config and mosaic config is required. ' + - 'refer readme for more details'); + Logger.error('only one option out of gateway config and mosaic config is required. ' + + 'refer readme for more details'); process.exit(1); } @@ -182,8 +182,8 @@ commander const { originChainId, gatewayAddresses, - } = options.mosaicConfig !== undefined ? getFromMosaicConfig(auxChainId, options.mosaicConfig) : - getFromGatewayConfig(auxChainId, options.gatewayConfig); + } = options.mosaicConfig !== undefined ? getFromMosaicConfig(auxChainId, options.mosaicConfig) + : getFromGatewayConfig(auxChainId, options.gatewayConfig); facilitatorConfig.originChain = originChainId; facilitatorConfig.auxChainId = auxChainId; diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index a35447b3..770d9e3f 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -20,6 +20,7 @@ import commander from 'commander'; import Container from '../Container'; import Facilitator from '../Facilitator'; import Logger from '../Logger'; +import ConfigFactory from '../Config/ConfigFactory'; const facilitatorCmd = commander .arguments('[origin_chain] [aux_chain_id]'); @@ -43,12 +44,18 @@ facilitatorCmd .option('-t, --facilitator-config ', 'path to facilitator configuration') .action(async (origin_chain, aux_chain_id, options) => { try { - // todo: call to config factory to get config - facilitator = await Container.create( + Logger.debug('Reading config file'); + const configFactory: ConfigFactory = new ConfigFactory( origin_chain, - aux_chain_id, + aux_chain_id ? Number.parseInt(aux_chain_id, 10) : undefined, options.mosaicConfig, options.facilitatorConfig, + options.gatewayConfig, + ); + const config = configFactory.getConfig(); + Logger.debug('Config loaded successfully.'); + facilitator = await Container.create( + config, ); Logger.info('facilitator starting...'); await facilitator.start(); diff --git a/test/Config/GatewayAddresses/fromGatewayConfig.test.ts b/test/Config/GatewayAddresses/fromGatewayConfig.test.ts index b1e20310..752b14da 100644 --- a/test/Config/GatewayAddresses/fromGatewayConfig.test.ts +++ b/test/Config/GatewayAddresses/fromGatewayConfig.test.ts @@ -18,7 +18,7 @@ import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import GatewayAddresses from '../../../src/Config/GatewayAddresses'; import assert from '../../test_utils/assert'; -import Utils from "../../../src/Utils"; +import Utils from '../../../src/Utils'; describe('GatewayAddresses.fromGatewayConfig()', () => { /** @@ -133,10 +133,10 @@ describe('GatewayAddresses.fromGatewayConfig()', () => { it('should pass when stakepool and redeempool addresses are present in gateway config', () => { gatewayConfig.originContracts.stakePoolAddress = Utils.toChecksumAddress( - '0xc6fF898ceBf631eFb58eEc7187E4c1f70AE8d943' + '0xc6fF898ceBf631eFb58eEc7187E4c1f70AE8d943', ); gatewayConfig.auxiliaryContracts.redeemPoolAddress = Utils.toChecksumAddress( - '0xdab6898ceBf631eFb58eEc7187E4c1f70AE8d943' + '0xdab6898ceBf631eFb58eEc7187E4c1f70AE8d943', ); const gatewayAddresses = GatewayAddresses.fromGatewayConfig(gatewayConfig); diff --git a/test/Config/createWeb3Instance.test.ts b/test/Config/createWeb3Instance.test.ts index 8e2f7d87..5b77e11c 100644 --- a/test/Config/createWeb3Instance.test.ts +++ b/test/Config/createWeb3Instance.test.ts @@ -29,7 +29,7 @@ describe('Config.createWeb3Instance', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(mosaicConfigPath, facilitatorConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); chain = config.facilitator.chains[config.facilitator.originChain]; }); diff --git a/test/Config/fromFile.test.ts b/test/Config/fromFile.test.ts index ddec7e6d..f85deeb8 100644 --- a/test/Config/fromFile.test.ts +++ b/test/Config/fromFile.test.ts @@ -52,7 +52,7 @@ describe('Config.fromFile()', () => { sinon.fake.returns(facilitator), ); - const config = Config.fromFile(mosaicConfigPath, facilitatorConfigPath); + const config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); SpyAssert.assert(mosaicConfigSpy, 1, [[mosaicConfigPath]]); SpyAssert.assert(facilitatorConfigSpy, 1, [[facilitatorConfigPath]]); diff --git a/test/Config/getAuxiliaryWeb3.test.ts b/test/Config/getAuxiliaryWeb3.test.ts index 07514229..60f43d63 100644 --- a/test/Config/getAuxiliaryWeb3.test.ts +++ b/test/Config/getAuxiliaryWeb3.test.ts @@ -28,7 +28,7 @@ describe('Config.auxiliaryWeb3', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(mosaicConfigPath, facilitatorConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); chain = config.facilitator.chains[config.facilitator.auxChainId]; }); diff --git a/test/Config/getOriginWeb3.test.ts b/test/Config/getOriginWeb3.test.ts index 499c362e..197096e0 100644 --- a/test/Config/getOriginWeb3.test.ts +++ b/test/Config/getOriginWeb3.test.ts @@ -28,7 +28,7 @@ describe('Config.originWeb3', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(mosaicConfigPath, facilitatorConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); chain = config.facilitator.chains[config.facilitator.originChain]; }); diff --git a/test/ConfigFactory/getConfig.test.ts b/test/ConfigFactory/getConfig.test.ts index 383232d0..6e47a4a9 100644 --- a/test/ConfigFactory/getConfig.test.ts +++ b/test/ConfigFactory/getConfig.test.ts @@ -18,17 +18,33 @@ import sinon from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import { Config, FacilitatorConfig } from '../../src/Config/Config'; import ConfigFactory from '../../src/Config/ConfigFactory'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; import GatewayAddresses from '../../src/Config/GatewayAddresses'; -describe('FacilitatorOptionParser.getConfig()', () => { +describe('ConfigFactory.getConfig()', () => { const originChain = '2'; const auxChain = 3; const facilitatorConfigPath = './facilitator-config.json'; const mosaicConfigPath = './test/mosaic-config.json'; + const gatewayConfigPath = './gateway-config.json'; + const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + const mosaic = JSON.parse(mosaicJson) as MosaicConfig; + const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; + const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; + + function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { + const spy = sinon.stub( + GatewayAddresses, + 'fromGatewayConfig', + ).callsFake( + sinon.fake.returns(gatewayAddresses), + ); + return spy; + } function spyFacilitatorFromFile(fcConfig: any): any { const spy = sinon.stub( @@ -40,6 +56,16 @@ describe('FacilitatorOptionParser.getConfig()', () => { return spy; } + function spyGatewayConfigFromFile(gatewayConfig: any): any { + const spy = sinon.stub( + GatewayConfig, + 'fromFile', + ).callsFake( + sinon.fake.returns(gatewayConfig), + ); + return spy; + } + function spyFacilitatorFromChain(fcConfig: any): any { const spy = sinon.stub( FacilitatorConfig, @@ -80,6 +106,23 @@ describe('FacilitatorOptionParser.getConfig()', () => { return spy; } + function spyConfigfromFile(config: Config): any { + const spy = sinon.stub( + Config, + 'fromFile', + ).callsFake( + sinon.fake.returns(config), + ); + return spy; + } + + function getGatewayConfigStub(): any { + const stubGatewayConfig = sinon.createStubInstance(GatewayConfig); + stubGatewayConfig.mosaicConfig = mosaic; + stubGatewayConfig.auxChainId = auxChain; + return stubGatewayConfig; + } + it('should fail when origin chain is provided but aux chain id is undefined', () => { const fs: ConfigFactory = new ConfigFactory( originChain, @@ -94,7 +137,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { ); }); - it('should fail when origin chain is provided but aux chain id is blank', () => { + it('should fail when origin chain is provided but aux chain id is undefined', () => { const fs: ConfigFactory = new ConfigFactory( originChain, undefined, @@ -221,7 +264,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { it('should pass when origin chain, aux chain id is provided and facilitator config path is ' + 'provided', () => { - const config = `{"originChain":"7","auxChainId":"${auxChain}","chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; + const config = `{"originChain":"7","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; const mosaicConfig = sinon.createStubInstance(MosaicConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); const facilitatorConfig = JSON.parse(config) as FacilitatorConfig; @@ -239,7 +282,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { const configObj: Config = fs.getConfig(); SpyAssert.assert(facilitatorSpy, 1, [[facilitatorConfigPath]]); SpyAssert.assert(mosaicSpy, 1, [[originChain]]); - SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaicConfig, auxChain.toString()]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaicConfig, auxChain]]); assert.strictEqual( configObj.gatewayAddresses, @@ -253,12 +296,8 @@ describe('FacilitatorOptionParser.getConfig()', () => { }); it('should pass when origin chain, aux chain and mosaic config path is provided', () => { - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const facilitatorJson = `{"originChain":"7","auxChainId":"${auxChain}","chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; const mosaicSpy = spyMosaicfromFile(mosaic); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); - const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; const facilitatorSpy = spyFacilitatorFromChain(facilitator); const gatewayAddressesSpy = spyGatewayAddressesFromMosaicConfig(gatewayAddresses); @@ -267,13 +306,14 @@ describe('FacilitatorOptionParser.getConfig()', () => { auxChain, mosaicConfigPath, '', + '', ); const configObj = fs.getConfig(); SpyAssert.assert(mosaicSpy, 1, [[mosaicConfigPath]]); SpyAssert.assert(facilitatorSpy, 1, [[auxChain]]); - SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain.toString()]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); assert.strictEqual( configObj.facilitator, facilitator as any, @@ -290,10 +330,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { }); it('should pass when origin chain and aux chain is provided', () => { - const facilitatorJson = `{"originChain":"7","auxChainId":"${auxChain}","chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; - const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); - const mosaic = sinon.createStubInstance(MosaicConfig); const facilitatorSpy = spyFacilitatorFromChain(facilitator); const mosaicSpy = spyMosaicFromChain(mosaic); @@ -304,7 +341,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { SpyAssert.assert(mosaicSpy, 1, [[originChain]]); SpyAssert.assert(facilitatorSpy, 1, [[auxChain]]); - SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain.toString()]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); assert.strictEqual( config.gatewayAddresses, gatewayAddresses, @@ -322,12 +359,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { }); it('should pass when facilitator config and mosaic config is provided', () => { - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; const mosaicFromFileSpy = spyMosaicfromFile(mosaic); - const facilitator = sinon.createStubInstance(FacilitatorConfig); - facilitator.originChain = originChain; - facilitator.auxChainId = auxChain; const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); const facilitatorFromFileSpy = spyFacilitatorFromFile(facilitator); const gatewayAddressesSpy = spyGatewayAddressesFromMosaicConfig(gatewayAddresses); @@ -384,8 +416,8 @@ describe('FacilitatorOptionParser.getConfig()', () => { it('should fail when facilitator config and mosaic config is provided but auxiliary ' + 'id doesn\'t match', () => { - const configJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(configJson) as MosaicConfig; + const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + const mosaic = JSON.parse(mosaicJson) as MosaicConfig; const facilitator = sinon.createStubInstance(FacilitatorConfig); facilitator.originChain = '5'; @@ -411,8 +443,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { }); it('should pass when only facilitator config is provided', () => { - const facilitatorJson = `{"originChain":"${originChain}","auxChainId":"${auxChain}","chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; - const mosaic = sinon.createStubInstance(MosaicConfig); + const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); const facilitatorSpy = spyFacilitatorFromFile(JSON.parse(facilitatorJson)); const mosaicSpy = spyMosaicFromChain(mosaic); @@ -428,7 +459,7 @@ describe('FacilitatorOptionParser.getConfig()', () => { SpyAssert.assert(facilitatorSpy, 1, [[facilitatorConfigPath]]); SpyAssert.assert(mosaicSpy, 1, [[originChain]]); - SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain.toString()]]); + SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); assert.strictEqual( config.gatewayAddresses, gatewayAddresses, @@ -445,4 +476,195 @@ describe('FacilitatorOptionParser.getConfig()', () => { facilitatorSpy.restore(); mosaicSpy.restore(); }); + + it('should pass when origin chain, aux chain id, facilitator config and mosaic config is provided', () => { + const mosaicFromFileSpy = spyMosaicfromFile(mosaic); + const fakeConfig = sinon.createStubInstance(Config); + const configFromFileSpy = spyConfigfromFile(fakeConfig); + const facilitatorFromFileSpy = spyFacilitatorFromFile(facilitator); + const fs: ConfigFactory = new ConfigFactory( + originChain, + auxChain, + mosaicConfigPath, + facilitatorConfigPath, + ); + + const config: Config = fs.getConfig(); + + SpyAssert.assert(facilitatorFromFileSpy, 1, [[facilitatorConfigPath]]); + SpyAssert.assert(mosaicFromFileSpy, 1, [[mosaicConfigPath]]); + SpyAssert.assert(configFromFileSpy, 1, [[facilitatorConfigPath, mosaicConfigPath, '']]); + assert.strictEqual( + config, + fakeConfig, + 'Invalid gateway addresses object', + ); + + facilitatorFromFileSpy.restore(); + configFromFileSpy.restore(); + mosaicFromFileSpy.restore(); + sinon.restore(); + }); + + it('should pass when origin chain, aux chain id and gateway config is provided', () => { + const stubGatewayConfig = getGatewayConfigStub(); + const facilitatorFromChainSpy = spyFacilitatorFromChain(facilitator); + const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const fromGatewayConfigSpy = spyFromGatewayConfig(gatewayAddresses); + + const fs: ConfigFactory = new ConfigFactory( + originChain, + auxChain, + undefined, + undefined, + gatewayConfigPath, + ); + + const config: Config = fs.getConfig(); + + SpyAssert.assert(facilitatorFromChainSpy, 1, [[auxChain]]); + SpyAssert.assert(gatewayConfigFromFileSpy, 1, [[gatewayConfigPath]]); + SpyAssert.assert(fromGatewayConfigSpy, 1, [[stubGatewayConfig]]); + + assert.strictEqual( + config.gatewayAddresses, + gatewayAddresses, + 'Invalid gateway addresses object', + ); + + assert.strictEqual( + config.facilitator, + facilitator, + 'Invalid facilitator object', + ); + + sinon.restore(); + }); + + it('should fail when origin chain, aux chain id and gateway config is provided but aux chain id is incorrect in mosaic config', () => { + const stubGatewayConfig = getGatewayConfigStub(); + const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; + spyFacilitatorFromChain(facilitator); + spyGatewayConfigFromFile(stubGatewayConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + spyFromGatewayConfig(gatewayAddresses); + const auxChainId = 200; + + const fs: ConfigFactory = new ConfigFactory( + originChain, + auxChainId, + undefined, + undefined, + gatewayConfigPath, + ); + + assert.throws( + () => fs.getConfig(), + 'aux chain is not present in mosaic config', + ); + + sinon.restore(); + }); + + it('should pass when origin chain, aux chain id and gateway config is provided', () => { + const stubGatewayConfig = getGatewayConfigStub(); + const facilitatorFromChainSpy = spyFacilitatorFromChain(facilitator); + const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const fromGatewayConfigSpy = spyFromGatewayConfig(gatewayAddresses); + + const fs: ConfigFactory = new ConfigFactory( + originChain, + auxChain, + undefined, + undefined, + gatewayConfigPath, + ); + + const config: Config = fs.getConfig(); + + SpyAssert.assert(facilitatorFromChainSpy, 1, [[auxChain]]); + SpyAssert.assert(gatewayConfigFromFileSpy, 1, [[gatewayConfigPath]]); + SpyAssert.assert(fromGatewayConfigSpy, 1, [[stubGatewayConfig]]); + + assert.strictEqual( + config.gatewayAddresses, + gatewayAddresses, + 'Invalid gateway addresses object', + ); + + assert.strictEqual( + config.facilitator, + facilitator, + 'Invalid facilitator object', + ); + + facilitatorFromChainSpy.restore(); + gatewayConfigFromFileSpy.restore(); + fromGatewayConfigSpy.restore(); + sinon.restore(); + }); + + it('should pass when facilitator and gateway config path is provided', () => { + const stubGatewayConfig = getGatewayConfigStub(); + const facilitatorFromFileSpy = spyFacilitatorFromFile(facilitator); + const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const fromGatewayConfigSpy = spyFromGatewayConfig(gatewayAddresses); + + const fs: ConfigFactory = new ConfigFactory( + undefined, + undefined, + undefined, + facilitatorConfigPath, + gatewayConfigPath, + ); + + const config: Config = fs.getConfig(); + + SpyAssert.assert(facilitatorFromFileSpy, 1, [[facilitatorConfigPath]]); + SpyAssert.assert(gatewayConfigFromFileSpy, 1, [[gatewayConfigPath]]); + SpyAssert.assert(fromGatewayConfigSpy, 1, [[stubGatewayConfig]]); + + assert.strictEqual( + config.gatewayAddresses, + gatewayAddresses, + 'Invalid gateway addresses object', + ); + + assert.strictEqual( + config.facilitator, + facilitator, + 'Invalid facilitator object', + ); + + sinon.restore(); + }); + + it('should fail when facilitator and gateway config path is provided but aux chain id is different', () => { + const stubGatewayConfig = getGatewayConfigStub(); + const auxChainIdInGatewayConfig = '200'; + stubGatewayConfig.auxChainId = auxChainIdInGatewayConfig; + spyFacilitatorFromFile(facilitator); + spyGatewayConfigFromFile(stubGatewayConfig); + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + spyFromGatewayConfig(gatewayAddresses); + + const fs: ConfigFactory = new ConfigFactory( + undefined, + undefined, + undefined, + facilitatorConfigPath, + gatewayConfigPath, + ); + + assert.throws( + () => fs.getConfig(), + `Aux chain id ${auxChainIdInGatewayConfig} in gatewayconfig and provided auxchain id ${auxChain} are not same`, + ); + + + sinon.restore(); + }); }); diff --git a/test/SeedData/populateDb.test.ts b/test/SeedData/populateDb.test.ts index 05048398..b8eb6d2d 100644 --- a/test/SeedData/populateDb.test.ts +++ b/test/SeedData/populateDb.test.ts @@ -289,7 +289,7 @@ describe('SeedData.populateDb()', (): void => { ); const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(mosaicConfigPath, facilitatorConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); sinon.replaceGetter( config, 'originWeb3', diff --git a/test_integration/Utils.ts b/test_integration/Utils.ts index a1a323c6..1f38dd64 100644 --- a/test_integration/Utils.ts +++ b/test_integration/Utils.ts @@ -42,7 +42,7 @@ export default class Utils { private redeemPool: string; - private ostPrime: string; + private utilityTokenAddresses: string; public gatewayAddresses: GatewayAddresses; @@ -70,7 +70,7 @@ export default class Utils { this.auxiliaryWeb3.transactionConfirmationBlocks = 1; this.stakePoolAddress = this.gatewayAddresses.stakePoolAddress; this.redeemPool = this.gatewayAddresses.redeemPoolAddress; - this.ostPrime = this.gatewayAddresses.utilityTokenAddress; + this.utilityTokenAddresses = this.gatewayAddresses.utilityTokenAddress; } /** @@ -815,6 +815,7 @@ export default class Utils { */ public getSimpleTokenInstance(): EIP20Token { const { valueTokenAddress } = this.gatewayAddresses; + console.log('valueTokenAddress ;- ',valueTokenAddress); const simpletokenInstance: EIP20Token = interacts.getEIP20Token( this.originWeb3, valueTokenAddress, @@ -829,7 +830,7 @@ export default class Utils { public getSimpleTokenPrimeInstance(): OSTPrime { const simpletokenPrimeInstance: OSTPrime = interacts.getOSTPrime( this.auxiliaryWeb3, - this.ostPrime, + this.utilityTokenAddresses, ); return simpletokenPrimeInstance; } diff --git a/tsconfig.json b/tsconfig.json index 576a4398..ca6868bb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,18 +4,18 @@ "module": "commonjs", "allowSyntheticDefaultImports": true, "esModuleInterop": true, - "alwaysStrict": true, - "strictNullChecks": true, - "strictBindCallApply": true, - "strictFunctionTypes": true, - "strictPropertyInitialization": true, - "noImplicitThis": true, - "noImplicitAny": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noErrorTruncation": true, +// "alwaysStrict": true, +// "strictNullChecks": true, +// "strictBindCallApply": true, +// "strictFunctionTypes": true, +// "strictPropertyInitialization": true, +// "noImplicitThis": true, +// "noImplicitAny": true, +// "noImplicitReturns": true, +// "noFallthroughCasesInSwitch": true, +// "noUnusedLocals": true, +// "noUnusedParameters": true, +// "noErrorTruncation": true, "resolveJsonModule": true, "inlineSourceMap": true, "declaration": true, From c69e5556220f206cef092e25e5e90645184e5dba Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 18 Oct 2019 19:08:47 +0530 Subject: [PATCH 04/12] Reverted changes --- src/Config/Config.ts | 4 ++-- src/Config/ConfigFactory.ts | 18 +++++++++--------- test_integration/Utils.ts | 1 - tsconfig.json | 24 ++++++++++++------------ 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Config/Config.ts b/src/Config/Config.ts index 60cdbac0..217c5994 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -129,7 +129,7 @@ export class FacilitatorConfig { */ private constructor(config: any) { this.originChain = config.originChain || ''; - this.auxChainId = config.auxChainId || 0; + this.auxChainId = config.auxChainId || ''; this.database = config.database || new DBConfig(); this.chains = {}; this.encryptedAccounts = config.encryptedAccounts || {}; @@ -331,7 +331,7 @@ export class Config { ? GatewayAddresses.fromMosaicConfig( MosaicConfig.fromFile(mosaicConfigPath), facilitator.auxChainId, - ) : GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(gatewayConfigPath)); + ) : GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(gatewayConfigPath!)); return new Config( gatewayAddresses, diff --git a/src/Config/ConfigFactory.ts b/src/Config/ConfigFactory.ts index a65b37dd..ab7fddfb 100644 --- a/src/Config/ConfigFactory.ts +++ b/src/Config/ConfigFactory.ts @@ -93,7 +93,7 @@ export default class ConfigFactory { return Config.fromFile(this.facilitatorConfigPath, '', this.gatewayConfigPath); } - const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain); + const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain!); return new Config( GatewayAddresses.fromMosaicConfig( @@ -107,7 +107,7 @@ export default class ConfigFactory { if (this.mosaicConfigPath) { const mosaic: MosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); this.verifyChainIdInMosaicConfig(mosaic); - const facilitator = FacilitatorConfig.fromChain(this.auxChainId); + const facilitator = FacilitatorConfig.fromChain(this.auxChainId!); return new Config( GatewayAddresses.fromMosaicConfig( mosaic, @@ -118,7 +118,7 @@ export default class ConfigFactory { } if (this.gatewayConfigPath) { - const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId); + const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId!); const gatewayConfig = GatewayConfig.fromFile(this.gatewayConfigPath); this.verifyChainIdInGatewayConfig(gatewayConfig); return new Config( @@ -127,8 +127,8 @@ export default class ConfigFactory { ); } - const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId); - const mosaic: MosaicConfig = MosaicConfig.fromChain(this.originChain); + const facilitator: FacilitatorConfig = FacilitatorConfig.fromChain(this.auxChainId!); + const mosaic: MosaicConfig = MosaicConfig.fromChain(this.originChain!); return new Config( GatewayAddresses.fromMosaicConfig( mosaic, @@ -145,7 +145,7 @@ export default class ConfigFactory { */ private handleFacilitatorConfigOption(): Config { let configObj; - const facilitatorConfig = FacilitatorConfig.fromFile(this.facilitatorConfigPath); + const facilitatorConfig = FacilitatorConfig.fromFile(this.facilitatorConfigPath!); this.auxChainId = facilitatorConfig.auxChainId; this.originChain = facilitatorConfig.originChain; // When no origin and aux chain provided. @@ -196,13 +196,13 @@ export default class ConfigFactory { private verifyChainIdInFacilitatorConfig( facilitatorConfig: FacilitatorConfig, ): void { - if (facilitatorConfig.chains[this.auxChainId] === undefined) { + if (facilitatorConfig.chains[this.auxChainId!] === undefined) { throw new FacilitatorStartException( `facilitator config is invalid as provided auxchain ${this.auxChainId} is not present`, ); } - if (facilitatorConfig.chains[this.originChain] === undefined) { + if (facilitatorConfig.chains[this.originChain!] === undefined) { throw new FacilitatorStartException( `facilitator config is invalid as provided origin chain ${this.originChain} is not present`, ); @@ -216,7 +216,7 @@ export default class ConfigFactory { private verifyChainIdInMosaicConfig( mosaicConfig: MosaicConfig, ): void { - if (mosaicConfig.auxiliaryChains[this.auxChainId] === undefined) { + if (mosaicConfig.auxiliaryChains[this.auxChainId!] === undefined) { throw new FacilitatorStartException('aux chain is not present in mosaic config'); } if (mosaicConfig.originChain.chain !== this.originChain) { diff --git a/test_integration/Utils.ts b/test_integration/Utils.ts index 1f38dd64..90ff99db 100644 --- a/test_integration/Utils.ts +++ b/test_integration/Utils.ts @@ -815,7 +815,6 @@ export default class Utils { */ public getSimpleTokenInstance(): EIP20Token { const { valueTokenAddress } = this.gatewayAddresses; - console.log('valueTokenAddress ;- ',valueTokenAddress); const simpletokenInstance: EIP20Token = interacts.getEIP20Token( this.originWeb3, valueTokenAddress, diff --git a/tsconfig.json b/tsconfig.json index ca6868bb..576a4398 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,18 +4,18 @@ "module": "commonjs", "allowSyntheticDefaultImports": true, "esModuleInterop": true, -// "alwaysStrict": true, -// "strictNullChecks": true, -// "strictBindCallApply": true, -// "strictFunctionTypes": true, -// "strictPropertyInitialization": true, -// "noImplicitThis": true, -// "noImplicitAny": true, -// "noImplicitReturns": true, -// "noFallthroughCasesInSwitch": true, -// "noUnusedLocals": true, -// "noUnusedParameters": true, -// "noErrorTruncation": true, + "alwaysStrict": true, + "strictNullChecks": true, + "strictBindCallApply": true, + "strictFunctionTypes": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "noImplicitAny": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noErrorTruncation": true, "resolveJsonModule": true, "inlineSourceMap": true, "declaration": true, From af25fd9778afd6936ea424b1ec98191242fbe092 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Fri, 18 Oct 2019 19:17:13 +0530 Subject: [PATCH 05/12] Removed console --- src/bin/facilitator-init.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 874bf8c6..32f669cf 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -102,7 +102,6 @@ commander .action(async (options) => { // Validating mandatory parameters let mandatoryOptionMissing = false; - console.log('options :- ', options); if ( (options.mosaicConfig && options.gatewayConfig) From d57c7b8d577c5c839b5bfad2ab0724e4e345c9e5 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Mon, 21 Oct 2019 13:20:11 +0530 Subject: [PATCH 06/12] Added validation so that together gateway config and mosaic-config is not allowed --- src/bin/facilitator-start.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index 770d9e3f..3b33d61a 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -44,6 +44,14 @@ facilitatorCmd .option('-t, --facilitator-config ', 'path to facilitator configuration') .action(async (origin_chain, aux_chain_id, options) => { try { + if ( + (options.mosaicConfig && options.gatewayConfig) + ) { + Logger.error('only one option out of gateway config and mosaic config is required. ' + + 'refer readme for more details'); + process.exit(1); + } + Logger.debug('Reading config file'); const configFactory: ConfigFactory = new ConfigFactory( origin_chain, From 939b7c4cbc8583b17e5d4bb2e20a333b0f2ecefd Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Mon, 21 Oct 2019 13:26:33 +0530 Subject: [PATCH 07/12] Documentation changes --- src/Config/ConfigFactory.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Config/ConfigFactory.ts b/src/Config/ConfigFactory.ts index ab7fddfb..ccbbde6b 100644 --- a/src/Config/ConfigFactory.ts +++ b/src/Config/ConfigFactory.ts @@ -60,7 +60,7 @@ export default class ConfigFactory { /** * It would evaluate the parameters and return config object. - * @returns Config object that contains mosaic and facilitator configs. + * @returns Config object that contains gateway and facilitator configs. */ public getConfig(): Config { if (this.isFacilitatorConfigPathAvailable()) { @@ -71,7 +71,7 @@ export default class ConfigFactory { /** * This method returns Config object when origin chain and aux chain is defined. - * @returns Config object encapsulating facilitator and mosaic configs. + * @returns Config object encapsulating facilitator and gateway configs. */ private handleOriginAuxChainOption(): Config { this.verifyOriginAuxChainDefined(); @@ -141,7 +141,7 @@ export default class ConfigFactory { /** * This method returns config object when facilitator config is provided and * origin chain and aux chain is not provided. - * @returns Config object encapsulating facilitator and mosaic configs. + * @returns Config object encapsulating facilitator and gateway configs. */ private handleFacilitatorConfigOption(): Config { let configObj; From e29cef412e70fba178f8f53500eeb335345e2cfc Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Mon, 21 Oct 2019 20:13:23 +0530 Subject: [PATCH 08/12] Added validation and updated readme --- README.md | 6 +++--- src/bin/facilitator-init.ts | 6 +++--- src/bin/facilitator-start.ts | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 320407f9..a0ea8786 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,10 @@ Facilitator start command will start the facilitator. Facilitator can be started in below two ways :- 1. `./facilitator start --facilitator-config --mosaic-config --gateway-config ` - * Replace `` with the path to facilitator-config.json using generated using `facilitator init`. + * Replace `` with the path to facilitator-config.json generated using `facilitator init`. * `--mosaic-config` and `--gateway-config` is optional argument. - * If both `--mosaic-config` is given it will read mosaic and facilitator configs from `` and `` paths respectively. - * If both `--gateway-config` is given it will read gateway and facilitator configs from `` and `` paths respectively. + * If both `--mosaic-config` and `--facilitator-config` is given then it will read mosaic and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. + * If both `--gateway-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. 2. `./facilitator start ` * Replace `` with name of the origin chain. diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 32f669cf..0e99c8c4 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -107,9 +107,8 @@ commander (options.mosaicConfig && options.gatewayConfig) || (options.gatewayConfig === undefined && options.mosaicConfig === undefined) ) { - Logger.error('only one option out of gateway config and mosaic config is required. ' - + 'refer readme for more details'); - process.exit(1); + Logger.error('one option out of gateway config and mosaic config is required.'); + mandatoryOptionMissing = true; } const { auxChainId } = options; @@ -159,6 +158,7 @@ commander } if (mandatoryOptionMissing) { + Logger.info('refer readme for more details'); process.exit(1); } diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index 3b33d61a..88b7ef7b 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -43,12 +43,22 @@ facilitatorCmd .option('-q, --gateway-config ', 'path to gateway configuration') .option('-t, --facilitator-config ', 'path to facilitator configuration') .action(async (origin_chain, aux_chain_id, options) => { + let mandatoryOptionMissing = false; try { if ( (options.mosaicConfig && options.gatewayConfig) ) { - Logger.error('only one option out of gateway config and mosaic config is required. ' - + 'refer readme for more details'); + Logger.error('one option out of gateway config and mosaic config is required.'); + mandatoryOptionMissing = true; + } + + if(options.facilitatorConfig === undefined) { + Logger.error('required --facilitator-config '); + mandatoryOptionMissing = true; + } + + if (mandatoryOptionMissing) { + Logger.info('refer readme for more details'); process.exit(1); } From dcd07f9821b267e53cb17a3e1512001ce3349589 Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 22 Oct 2019 11:59:18 +0530 Subject: [PATCH 09/12] Fixed failing test case --- src/bin/facilitator-start.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index 88b7ef7b..6bb33d4e 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -52,11 +52,6 @@ facilitatorCmd mandatoryOptionMissing = true; } - if(options.facilitatorConfig === undefined) { - Logger.error('required --facilitator-config '); - mandatoryOptionMissing = true; - } - if (mandatoryOptionMissing) { Logger.info('refer readme for more details'); process.exit(1); From fbeacbf797fc425185d8b3d48a2c2f1a7b60e93a Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 22 Oct 2019 18:53:52 +0530 Subject: [PATCH 10/12] Added FacilitatorInit class, ConfiyType enum, unit test cases --- src/Config/Config.ts | 32 ++- src/Config/ConfigFactory.ts | 21 +- src/bin/facilitator-init.ts | 241 ++++++++---------- src/bin/facilitator.ts | 2 +- src/lib/FacilitatorInit.ts | 58 +++++ test/Config/createWeb3Instance.test.ts | 6 +- test/Config/fromFile.test.ts | 23 +- test/Config/getAuxiliaryWeb3.test.ts | 4 +- test/Config/getOriginWeb3.test.ts | 4 +- test/ConfigFactory/getConfig.test.ts | 90 +++++-- test/SeedData/populateDb.test.ts | 4 +- .../persist.test.ts | 64 ++--- .../getFromGatewayConfig.test.ts | 108 ++++++++ .../getFromMosaicConfig.test.ts | 109 ++++++++ 14 files changed, 547 insertions(+), 219 deletions(-) create mode 100644 src/lib/FacilitatorInit.ts create mode 100644 test/lib/FacilitatorInit/getFromGatewayConfig.test.ts create mode 100644 test/lib/FacilitatorInit/getFromMosaicConfig.test.ts diff --git a/src/Config/Config.ts b/src/Config/Config.ts index 217c5994..36f4f382 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -42,6 +42,12 @@ enum DBType { SQLITE = 'SQLITE', } +// Database type +export enum ConfigType { + GATEWAY = 'Gateway', + MOSAIC = 'Mosaic', +} + /** * Holds database configurations. */ @@ -315,23 +321,31 @@ export class Config { /** * It provides config object from the path specified. This will throw if - * mosaic config path or facilitator config path doesn't exists. + * mosaic config path or facilitator config path doesn't exists and invalid config type + * is provided. * @param facilitatorConfigPath Path to facilitator config file path. - * @param mosaicConfigPath Path to mosaic config file path. - * @param gatewayConfigPath Path to gateway config file path. + * @param configPath Path to mosaic or gateway config. + * @param configType Type of config. * @returns Config object consisting of gateway addresses and facilitator configurations. */ public static fromFile( facilitatorConfigPath: string, - mosaicConfigPath?: string, - gatewayConfigPath?: string, + configPath: string, + configType: ConfigType, ): Config { const facilitator: FacilitatorConfig = FacilitatorConfig.fromFile(facilitatorConfigPath); - const gatewayAddresses = mosaicConfigPath - ? GatewayAddresses.fromMosaicConfig( - MosaicConfig.fromFile(mosaicConfigPath), + + let gatewayAddresses: GatewayAddresses; + if (ConfigType.GATEWAY === configType) { + gatewayAddresses = GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(configPath)); + } else if (ConfigType.MOSAIC === configType) { + gatewayAddresses = GatewayAddresses.fromMosaicConfig( + MosaicConfig.fromFile(configPath), facilitator.auxChainId, - ) : GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(gatewayConfigPath!)); + ); + } else { + throw new Error(`Invalid config type ${configType}`); + } return new Config( gatewayAddresses, diff --git a/src/Config/ConfigFactory.ts b/src/Config/ConfigFactory.ts index ccbbde6b..b13dec1e 100644 --- a/src/Config/ConfigFactory.ts +++ b/src/Config/ConfigFactory.ts @@ -18,7 +18,7 @@ import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import { FacilitatorStartException } from '../Exception'; -import { Config, FacilitatorConfig } from './Config'; +import { Config, FacilitatorConfig, ConfigType } from './Config'; import GatewayAddresses from './GatewayAddresses'; /* eslint-disable @typescript-eslint/no-non-null-assertion */ @@ -86,11 +86,19 @@ export default class ConfigFactory { const mosaicConfig: MosaicConfig = MosaicConfig.fromFile(this.mosaicConfigPath); // verify origin chain and aux chain is present in mosaic config. this.verifyChainIdInMosaicConfig(mosaicConfig); - return Config.fromFile(this.facilitatorConfigPath, this.mosaicConfigPath, ''); + return Config.fromFile( + this.facilitatorConfigPath, + this.mosaicConfigPath, + ConfigType.MOSAIC, + ); } if (this.gatewayConfigPath) { - return Config.fromFile(this.facilitatorConfigPath, '', this.gatewayConfigPath); + return Config.fromFile( + this.facilitatorConfigPath, + this.gatewayConfigPath, + ConfigType.GATEWAY, + ); } const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain!); @@ -174,9 +182,13 @@ export default class ConfigFactory { ); } else { // only facilitator config is given. + if (!MosaicConfig.exists(this.originChain)) { + throw new Error('mosaic config not found'); + } const mosaicConfig: MosaicConfig = MosaicConfig.fromChain( this.originChain, ); + this.verifyChainIdInMosaicConfig(mosaicConfig); configObj = new Config( GatewayAddresses.fromMosaicConfig( @@ -235,7 +247,8 @@ export default class ConfigFactory { this.verifyChainIdInMosaicConfig(gatewayConfig.mosaicConfig); if (gatewayConfig.auxChainId !== this.auxChainId) { throw new FacilitatorStartException( - `Aux chain id ${gatewayConfig.auxChainId} in gatewayconfig and provided auxchain id ${this.auxChainId} are not same`, + `Aux chain id ${gatewayConfig.auxChainId} in gatewayconfig and provided auxchain id ` + + `${this.auxChainId} are not same`, ); } } diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 0e99c8c4..44dbf4f0 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -18,8 +18,6 @@ import commander from 'commander'; import Web3 from 'web3'; -import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; -import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; import Account from '../Account'; import { Chain, FacilitatorConfig, Config, ENV_WORKER_PASSWORD_PREFIX, @@ -30,60 +28,7 @@ import Logger from '../Logger'; import Repositories from '../repositories/Repositories'; import SeedData from '../SeedData'; import GatewayAddresses from '../Config/GatewayAddresses'; - -/** - * It provides gateway addresses and origin chain id. It is to be used when mosaic - * config is provided. - * @param auxChainId Auxiliary chain id. - * @param mosaicConfigPath Path to mosaic config. - * @returns originchain id and gatewayaddresses object. - */ -function getFromMosaicConfig( - auxChainId: number, - mosaicConfigPath: string, -): { - originChainId: string; gatewayAddresses: GatewayAddresses; - } { - const mosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); - const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; - if (auxChain === null || auxChain === undefined) { - Logger.error(`auxchain id ${auxChainId} is not present in the mosaic config`); - process.exit(1); - } - - return { - originChainId: mosaicConfig.originChain.chain, - gatewayAddresses: GatewayAddresses.fromMosaicConfig(mosaicConfig, auxChainId), - }; -} - -/** - * It provides gateway addresses and origin chain id. It is to be used when gateway - * config is provided. - * @param auxChainId Auxiliary chain id. - * @param gatewayConfigPath Path to gateway config. - * @returns originchain id and gatewayaddresses object. - */ -function getFromGatewayConfig( - auxChainId: number, - gatewayConfigPath: string, -): { - originChainId: string; gatewayAddresses: GatewayAddresses; - } { - const gatewayConfig = GatewayConfig.fromFile(gatewayConfigPath); - - if (auxChainId === gatewayConfig.auxChainId) { - Logger.error(`aux chain id present in gateway config is ${gatewayConfig.auxChainId}` - + `but ${auxChainId} is specified in the command`); - process.exit(1); - } - - const gatewayAddresses = GatewayAddresses.fromGatewayConfig(gatewayConfig); - return { - originChainId: gatewayConfig.mosaicConfig.originChain.chain, - gatewayAddresses, - }; -} +import FacilitatorInit from '../lib/FacilitatorInit'; commander .option('-m, --mosaic-config ', 'path to mosaic configuration') @@ -162,93 +107,109 @@ commander process.exit(1); } - if (options.force) { - FacilitatorConfig.remove(auxChainId); - } else { - try { - if (FacilitatorConfig.isFacilitatorConfigPresent(auxChainId)) { - Logger.error('facilitator config already present. use -f option to override the existing facilitator config.'); - process.exit(1); - } - } catch (e) { - Logger.info('creating facilitator config as it is not present'); + try { + if (options.force) { + FacilitatorConfig.remove(auxChainId); + } else if (FacilitatorConfig.isFacilitatorConfigPresent(auxChainId)) { + throw new Error('facilitator config already present. use -f option to override the existing facilitator config.'); + } + Logger.info('creating facilitator config as it is not present'); + const facilitatorConfig = FacilitatorConfig.fromChain(auxChainId); + + // Get origin chain id. + let originChainId: string | undefined; + let gatewayAddresses: GatewayAddresses | undefined; + if (options.mosaicConfig !== undefined) { + ( + { + originChainId, + gatewayAddresses, + } = FacilitatorInit.getFromMosaicConfig(auxChainId, options.mosaicConfig) + ); } - } - const facilitatorConfig = FacilitatorConfig.fromChain(auxChainId); - - // Get origin chain id. - const { - originChainId, - gatewayAddresses, - } = options.mosaicConfig !== undefined ? getFromMosaicConfig(auxChainId, options.mosaicConfig) - : getFromGatewayConfig(auxChainId, options.gatewayConfig); - - facilitatorConfig.originChain = originChainId; - facilitatorConfig.auxChainId = auxChainId; - - let { dbPath } = options; - if (dbPath === undefined || dbPath === null) { - Logger.info('database path is not provided'); - dbPath = DatabaseFileHelper.create(auxChainId); - } else if (DatabaseFileHelper.verify(dbPath)) { - Logger.info('DB file verified'); - } else { - Logger.error('DB file doesn\'t exists or file extension is incorrect'); - process.exit(1); - } + if (options.gatewayConfig !== undefined) { + ( + { + originChainId, + gatewayAddresses, + } = FacilitatorInit.getFromMosaicConfig(auxChainId, options.mosaicConfig) + ); + } + + facilitatorConfig.originChain = originChainId!; + facilitatorConfig.auxChainId = auxChainId; + + let { dbPath } = options; + if (dbPath === undefined || dbPath === null) { + Logger.info('database path is not provided'); + dbPath = DatabaseFileHelper.create(auxChainId); + } else if (DatabaseFileHelper.verify(dbPath)) { + Logger.info('DB file verified'); + } else { + throw new Error('DB file doesn\'t exists or file extension is incorrect'); + } - facilitatorConfig.database.path = dbPath; - - const setFacilitator = ( - chainId: string, - rpc: string, - subGraphWs: string, - subGraphRpc: string, - password: string, - ): void => { - const account: Account = Account.create(new Web3(''), password); - - facilitatorConfig.chains[chainId] = new Chain(rpc, account.address, subGraphWs, subGraphRpc); - const envVariableNameForWorkerPassword = `${ENV_WORKER_PASSWORD_PREFIX}${account.address}`; - process.env[envVariableNameForWorkerPassword] = password; - - facilitatorConfig.encryptedAccounts[account.address] = account.encryptedKeyStore; - }; - - setFacilitator( - originChainId, - options.originRpc, - options.originGraphWs, - options.originGraphRpc, - options.originPassword, - ); - - setFacilitator( - auxChainId, - options.auxiliaryRpc, - options.auxiliaryGraphWs, - options.auxiliaryGraphRpc, - options.auxiliaryPassword, - ); - - const config = new Config(gatewayAddresses, facilitatorConfig); - const repositories = await Repositories.create(config.facilitator.database.path); - const seedData = new SeedData( - config, - repositories.gatewayRepository, - repositories.auxiliaryChainRepository, - repositories.contractEntityRepository, - ); - await seedData.populateDb(); - - facilitatorConfig.writeToFacilitatorConfig(auxChainId); - Logger.info('facilitator config file is generated'); - - Logger.info(`👉 worker address for ${originChainId} chain is ` - + `${facilitatorConfig.chains[originChainId].worker}`); - - Logger.info(`👉 worker address for ${auxChainId} chain is ` + facilitatorConfig.database.path = dbPath; + + const setFacilitator = ( + chainId: string, + rpc: string, + subGraphWs: string, + subGraphRpc: string, + password: string, + ): void => { + const account: Account = Account.create(new Web3(''), password); + + facilitatorConfig.chains[chainId] = new Chain( + rpc, + account.address, + subGraphWs, + subGraphRpc, + ); + const envVariableNameForWorkerPassword = `${ENV_WORKER_PASSWORD_PREFIX}${account.address}`; + process.env[envVariableNameForWorkerPassword] = password; + + facilitatorConfig.encryptedAccounts[account.address] = account.encryptedKeyStore; + }; + + setFacilitator( + originChainId!, + options.originRpc, + options.originGraphWs, + options.originGraphRpc, + options.originPassword, + ); + + setFacilitator( + auxChainId, + options.auxiliaryRpc, + options.auxiliaryGraphWs, + options.auxiliaryGraphRpc, + options.auxiliaryPassword, + ); + + const config = new Config(gatewayAddresses!, facilitatorConfig); + const repositories = await Repositories.create(config.facilitator.database.path); + const seedData = new SeedData( + config, + repositories.gatewayRepository, + repositories.auxiliaryChainRepository, + repositories.contractEntityRepository, + ); + await seedData.populateDb(); + + facilitatorConfig.writeToFacilitatorConfig(auxChainId); + Logger.info('facilitator config file is generated'); + + Logger.info(`👉 worker address for ${originChainId} chain is ` + + `${facilitatorConfig.chains[originChainId!].worker}`); + + Logger.info(`👉 worker address for ${auxChainId} chain is ` + `${facilitatorConfig.chains[auxChainId].worker}`); + } catch (e) { + Logger.error(e); + process.exit(1); + } }) .parse(process.argv); diff --git a/src/bin/facilitator.ts b/src/bin/facilitator.ts index 241849a7..87f99c39 100644 --- a/src/bin/facilitator.ts +++ b/src/bin/facilitator.ts @@ -20,6 +20,6 @@ import facilitator from 'commander'; facilitator - .command('init ', 'initializes the facilitator config') + .command('init ', 'initializes the facilitator config') .command('start ', 'starts the facilitator') .parse(process.argv); diff --git a/src/lib/FacilitatorInit.ts b/src/lib/FacilitatorInit.ts new file mode 100644 index 00000000..a88297dd --- /dev/null +++ b/src/lib/FacilitatorInit.ts @@ -0,0 +1,58 @@ +import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; +import GatewayAddresses from '../Config/GatewayAddresses'; + +/** + * It contains helper methods for facilitator init command. + */ +export default class FacilitatorInit { +/** + * It provides gateway addresses and origin chain id. It is to be used when mosaic + * config is provided. + * @param auxChainId Auxiliary chain id. + * @param mosaicConfigPath Path to mosaic config. + * @returns originchain id and gatewayaddresses object. + */ + public static getFromMosaicConfig( + auxChainId: number, + mosaicConfigPath: string, + ): { + originChainId?: string; gatewayAddresses?: GatewayAddresses; + } { + const mosaicConfig = MosaicConfig.fromFile(mosaicConfigPath); + const auxChain = mosaicConfig.auxiliaryChains[auxChainId]; + if (!auxChain) { + return {}; + } + + return { + originChainId: mosaicConfig.originChain.chain, + gatewayAddresses: GatewayAddresses.fromMosaicConfig(mosaicConfig, auxChainId), + }; + } + + /** + * It provides gateway addresses and origin chain id. It is to be used when gateway + * config is provided. + * @param auxChainId Auxiliary chain id. + * @param gatewayConfigPath Path to gateway config. + * @returns originchain id and gatewayaddresses object. + */ + public static getFromGatewayConfig( + auxChainId: number, + gatewayConfigPath: string, + ): { + originChainId?: string; gatewayAddresses?: GatewayAddresses; + } { + const gatewayConfig = GatewayConfig.fromFile(gatewayConfigPath); + + if (!(auxChainId === gatewayConfig.auxChainId)) { + return {}; + } + + return { + originChainId: gatewayConfig.mosaicConfig.originChain.chain, + gatewayAddresses: GatewayAddresses.fromGatewayConfig(gatewayConfig), + }; + } +} diff --git a/test/Config/createWeb3Instance.test.ts b/test/Config/createWeb3Instance.test.ts index 5b77e11c..bcd94ba9 100644 --- a/test/Config/createWeb3Instance.test.ts +++ b/test/Config/createWeb3Instance.test.ts @@ -19,7 +19,9 @@ import sinon from 'sinon'; import Web3 from 'web3'; import Account from '../../src/Account'; -import { Chain, Config, ENV_WORKER_PASSWORD_PREFIX } from '../../src/Config/Config'; +import { + Chain, Config, ConfigType, ENV_WORKER_PASSWORD_PREFIX, +} from '../../src/Config/Config'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; @@ -29,7 +31,7 @@ describe('Config.createWeb3Instance', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC); chain = config.facilitator.chains[config.facilitator.originChain]; }); diff --git a/test/Config/fromFile.test.ts b/test/Config/fromFile.test.ts index f85deeb8..ff1d3b28 100644 --- a/test/Config/fromFile.test.ts +++ b/test/Config/fromFile.test.ts @@ -18,7 +18,7 @@ import sinon from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; -import { Config, FacilitatorConfig } from '../../src/Config/Config'; +import { Config, FacilitatorConfig, ConfigType } from '../../src/Config/Config'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; import GatewayAddresses from '../../src/Config/GatewayAddresses'; @@ -52,7 +52,7 @@ describe('Config.fromFile()', () => { sinon.fake.returns(facilitator), ); - const config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); + const config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC); SpyAssert.assert(mosaicConfigSpy, 1, [[mosaicConfigPath]]); SpyAssert.assert(facilitatorConfigSpy, 1, [[facilitatorConfigPath]]); @@ -70,4 +70,23 @@ describe('Config.fromFile()', () => { sinon.restore(); }); + + it('should fail when invalid config type is provided', () => { + const mosaic = sinon.createStubInstance(MosaicConfig); + const facilitator = FacilitatorConfig.fromChain(auxChain); + facilitator.auxChainId = auxChain; + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const invalidConfigType = 'invalidconfigtype' as ConfigType; + + sinon.replace(MosaicConfig, 'fromFile', sinon.fake.returns(mosaic)); + + sinon.replace(GatewayAddresses, 'fromMosaicConfig', sinon.fake.returns(gatewayAddresses)); + + sinon.replace(FacilitatorConfig, 'fromFile', sinon.fake.returns(facilitator)); + + assert.throws( + () => Config.fromFile(facilitatorConfigPath, mosaicConfigPath, invalidConfigType), + `Invalid config type ${invalidConfigType}`, + ); + }); }); diff --git a/test/Config/getAuxiliaryWeb3.test.ts b/test/Config/getAuxiliaryWeb3.test.ts index 60f43d63..bb795dcf 100644 --- a/test/Config/getAuxiliaryWeb3.test.ts +++ b/test/Config/getAuxiliaryWeb3.test.ts @@ -19,7 +19,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import Web3 from 'web3'; -import { Chain, Config } from '../../src/Config/Config'; +import { Chain, Config, ConfigType } from '../../src/Config/Config'; import SpyAssert from '../test_utils/SpyAssert'; describe('Config.auxiliaryWeb3', () => { @@ -28,7 +28,7 @@ describe('Config.auxiliaryWeb3', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC); chain = config.facilitator.chains[config.facilitator.auxChainId]; }); diff --git a/test/Config/getOriginWeb3.test.ts b/test/Config/getOriginWeb3.test.ts index 197096e0..48769656 100644 --- a/test/Config/getOriginWeb3.test.ts +++ b/test/Config/getOriginWeb3.test.ts @@ -19,7 +19,7 @@ import { assert } from 'chai'; import sinon from 'sinon'; import Web3 from 'web3'; -import { Chain, Config } from '../../src/Config/Config'; +import { Chain, Config, ConfigType } from '../../src/Config/Config'; import SpyAssert from '../test_utils/SpyAssert'; describe('Config.originWeb3', () => { @@ -28,7 +28,7 @@ describe('Config.originWeb3', () => { beforeEach(() => { const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC); chain = config.facilitator.chains[config.facilitator.originChain]; }); diff --git a/test/ConfigFactory/getConfig.test.ts b/test/ConfigFactory/getConfig.test.ts index 6e47a4a9..a1c5bb9a 100644 --- a/test/ConfigFactory/getConfig.test.ts +++ b/test/ConfigFactory/getConfig.test.ts @@ -19,7 +19,7 @@ import sinon from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; -import { Config, FacilitatorConfig } from '../../src/Config/Config'; +import { Config, ConfigType, FacilitatorConfig } from '../../src/Config/Config'; import ConfigFactory from '../../src/Config/ConfigFactory'; import assert from '../test_utils/assert'; import SpyAssert from '../test_utils/SpyAssert'; @@ -28,13 +28,21 @@ import GatewayAddresses from '../../src/Config/GatewayAddresses'; describe('ConfigFactory.getConfig()', () => { const originChain = '2'; const auxChain = 3; + + function getMosaic(): string { + return `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + } + + function getFacilitator(): string { + return `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; + } + const facilitatorConfigPath = './facilitator-config.json'; const mosaicConfigPath = './test/mosaic-config.json'; const gatewayConfigPath = './gateway-config.json'; - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; - const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; - const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; + const mosaic = JSON.parse(getMosaic()) as MosaicConfig; + + const facilitator = JSON.parse(getFacilitator()) as FacilitatorConfig; function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { const spy = sinon.stub( @@ -56,7 +64,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyGatewayConfigFromFile(gatewayConfig: any): any { + function spyGatewayConfigFromFile(gatewayConfig: GatewayConfig): any { const spy = sinon.stub( GatewayConfig, 'fromFile', @@ -116,6 +124,16 @@ describe('ConfigFactory.getConfig()', () => { return spy; } + function spyMosaicExists(status: boolean): any { + const spy = sinon.stub( + MosaicConfig, + 'exists', + ).callsFake( + sinon.fake.returns(status), + ); + return spy; + } + function getGatewayConfigStub(): any { const stubGatewayConfig = sinon.createStubInstance(GatewayConfig); stubGatewayConfig.mosaicConfig = mosaic; @@ -391,13 +409,13 @@ describe('ConfigFactory.getConfig()', () => { it('should fail when facilitator config and mosaic config is provided but origin chain doesn\'t match', () => { const configJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(configJson) as MosaicConfig; - const facilitator = sinon.createStubInstance(FacilitatorConfig); - facilitator.originChain = '5'; + const mosaicConfig = JSON.parse(configJson) as MosaicConfig; + const facilitatorStub = sinon.createStubInstance(FacilitatorConfig); + facilitatorStub.originChain = '5'; - facilitator.auxChainId = 3; - spyMosaicfromFile(mosaic); - spyFacilitatorFromFile(facilitator); + facilitatorStub.auxChainId = 3; + spyMosaicfromFile(mosaicConfig); + spyFacilitatorFromFile(facilitatorStub); const fs: ConfigFactory = new ConfigFactory( undefined as any, @@ -416,16 +434,13 @@ describe('ConfigFactory.getConfig()', () => { it('should fail when facilitator config and mosaic config is provided but auxiliary ' + 'id doesn\'t match', () => { - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; + const facilitatorStub = sinon.createStubInstance(FacilitatorConfig); + facilitatorStub.originChain = '5'; - const facilitator = sinon.createStubInstance(FacilitatorConfig); - facilitator.originChain = '5'; - - facilitator.auxChainId = 3; + facilitatorStub.auxChainId = 3; spyMosaicfromFile(mosaic); - spyFacilitatorFromFile(facilitator); + spyFacilitatorFromFile(facilitatorStub); const fs: ConfigFactory = new ConfigFactory( undefined as any, @@ -448,7 +463,7 @@ describe('ConfigFactory.getConfig()', () => { const facilitatorSpy = spyFacilitatorFromFile(JSON.parse(facilitatorJson)); const mosaicSpy = spyMosaicFromChain(mosaic); const gatewayAddressesSpy = spyGatewayAddressesFromMosaicConfig(gatewayAddresses); - + const mosaicExistsSpy = spyMosaicExists(true); const fs: ConfigFactory = new ConfigFactory( undefined, undefined, @@ -460,6 +475,7 @@ describe('ConfigFactory.getConfig()', () => { SpyAssert.assert(facilitatorSpy, 1, [[facilitatorConfigPath]]); SpyAssert.assert(mosaicSpy, 1, [[originChain]]); SpyAssert.assert(gatewayAddressesSpy, 1, [[mosaic, auxChain]]); + SpyAssert.assert(mosaicExistsSpy, 1, [[originChain]]); assert.strictEqual( config.gatewayAddresses, gatewayAddresses, @@ -472,9 +488,9 @@ describe('ConfigFactory.getConfig()', () => { `Expected chain id is ${originChain} but got ${config.facilitator.originChain}`, ); - sinon.restore(); facilitatorSpy.restore(); mosaicSpy.restore(); + sinon.restore(); }); it('should pass when origin chain, aux chain id, facilitator config and mosaic config is provided', () => { @@ -493,7 +509,11 @@ describe('ConfigFactory.getConfig()', () => { SpyAssert.assert(facilitatorFromFileSpy, 1, [[facilitatorConfigPath]]); SpyAssert.assert(mosaicFromFileSpy, 1, [[mosaicConfigPath]]); - SpyAssert.assert(configFromFileSpy, 1, [[facilitatorConfigPath, mosaicConfigPath, '']]); + SpyAssert.assert( + configFromFileSpy, + 1, + [[facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC]], + ); assert.strictEqual( config, fakeConfig, @@ -544,7 +564,6 @@ describe('ConfigFactory.getConfig()', () => { it('should fail when origin chain, aux chain id and gateway config is provided but aux chain id is incorrect in mosaic config', () => { const stubGatewayConfig = getGatewayConfigStub(); - const facilitator = JSON.parse(facilitatorJson) as FacilitatorConfig; spyFacilitatorFromChain(facilitator); spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -667,4 +686,29 @@ describe('ConfigFactory.getConfig()', () => { sinon.restore(); }); + + it('should fail when only facilitator config is provided but mosaic config is not found', () => { + const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; + const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const facilitatorSpy = spyFacilitatorFromFile(JSON.parse(facilitatorJson)); + const mosaicConfig = MosaicConfig.fromChain('200'); + spyMosaicExists(false); + spyMosaicFromChain(mosaicConfig); + spyGatewayAddressesFromMosaicConfig(gatewayAddresses); + + const fs: ConfigFactory = new ConfigFactory( + undefined, + undefined, + undefined, + facilitatorConfigPath, + ); + + assert.throws( + () => fs.getConfig(), + 'mosaic config not found', + ); + + facilitatorSpy.restore(); + sinon.restore(); + }); }); diff --git a/test/SeedData/populateDb.test.ts b/test/SeedData/populateDb.test.ts index b8eb6d2d..4980315d 100644 --- a/test/SeedData/populateDb.test.ts +++ b/test/SeedData/populateDb.test.ts @@ -22,7 +22,7 @@ import * as Web3Utils from 'web3-utils'; import { interacts } from '@openst/mosaic-contracts'; -import { Config } from '../../src/Config/Config'; +import { Config, ConfigType } from '../../src/Config/Config'; import AuxiliaryChain from '../../src/models/AuxiliaryChain'; import ContractEntity, { EntityType } from '../../src/models/ContractEntity'; import Gateway from '../../src/models/Gateway'; @@ -289,7 +289,7 @@ describe('SeedData.populateDb()', (): void => { ); const mosaicConfigPath = 'test/Facilitator/testdata/mosaic.json'; const facilitatorConfigPath = 'test/FacilitatorConfig/testdata/facilitator-config.json'; - config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath); + config = Config.fromFile(facilitatorConfigPath, mosaicConfigPath, ConfigType.MOSAIC); sinon.replaceGetter( config, 'originWeb3', diff --git a/test/handlers/stake_and_mint/StakeIntentDeclaredHandler/persist.test.ts b/test/handlers/stake_and_mint/StakeIntentDeclaredHandler/persist.test.ts index 56c068a1..9a605632 100644 --- a/test/handlers/stake_and_mint/StakeIntentDeclaredHandler/persist.test.ts +++ b/test/handlers/stake_and_mint/StakeIntentDeclaredHandler/persist.test.ts @@ -288,36 +288,36 @@ describe('StakeIntentDeclaredHandler.persist()', (): void => { SpyAssert.assert(stakeRequestSave, 1, [[stakeRequest]]); }); - it('should not update messageHash in messageTransferRequestRepository when stakeRequest is' + - ' undefined', - async (): Promise => { - const messageSave = sinon.stub(); - const mockedMessageRepository = sinon.createStubInstance(MessageRepository, - { - save: messageSave as any, - get: Promise.resolve(null), - }); - - const stakeRequestSave = sinon.stub(); - mockedMessageTransferRequestRepository = sinon.createStubInstance( - MessageTransferRequestRepository, - { - getBySenderProxyNonce: Promise.resolve(null), - save: stakeRequestSave as any, - }, - ); - - const handler = new StakeIntentDeclaredHandler( - mockedMessageRepository as any, - mockedMessageTransferRequestRepository as any, - ); - await handler.persist(transactions); - - SpyAssert.assert( - mockedMessageTransferRequestRepository.getBySenderProxyNonce, - 1, - [[transactions[0]._staker, new BigNumber(transactions[0]._stakerNonce)]], - ); - SpyAssert.assert(stakeRequestSave, 0, [[]]); - }); + it('should not update messageHash in messageTransferRequestRepository when stakeRequest is' + + ' undefined', + async (): Promise => { + const messageSave = sinon.stub(); + const mockedMessageRepository = sinon.createStubInstance(MessageRepository, + { + save: messageSave as any, + get: Promise.resolve(null), + }); + + const stakeRequestSave = sinon.stub(); + mockedMessageTransferRequestRepository = sinon.createStubInstance( + MessageTransferRequestRepository, + { + getBySenderProxyNonce: Promise.resolve(null), + save: stakeRequestSave as any, + }, + ); + + const handler = new StakeIntentDeclaredHandler( + mockedMessageRepository as any, + mockedMessageTransferRequestRepository as any, + ); + await handler.persist(transactions); + + SpyAssert.assert( + mockedMessageTransferRequestRepository.getBySenderProxyNonce, + 1, + [[transactions[0]._staker, new BigNumber(transactions[0]._stakerNonce)]], + ); + SpyAssert.assert(stakeRequestSave, 0, [[]]); + }); }); diff --git a/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts b/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts new file mode 100644 index 00000000..02748842 --- /dev/null +++ b/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts @@ -0,0 +1,108 @@ +// Copyright 2019 OpenST Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ---------------------------------------------------------------------------- + + +import sinon from 'sinon'; + +import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; +import assert from '../../test_utils/assert'; +import SpyAssert from '../../test_utils/SpyAssert'; +import FacilitatorInit from '../../../src/lib/FacilitatorInit'; +import GatewayAddresses from '../../../src/Config/GatewayAddresses'; + + +describe('FacilitatorInit.getFromGatewayConfig()', () => { + const gatewayConfigPath = 'test/Config/gateway-config.json'; + const auxChain = 3; + const originChain = '2'; + const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + const mosaic = JSON.parse(mosaicJson) as MosaicConfig; + + function getGatewayConfigStub(): any { + const stubGatewayConfig = sinon.createStubInstance(GatewayConfig); + stubGatewayConfig.mosaicConfig = mosaic; + stubGatewayConfig.auxChainId = auxChain; + return stubGatewayConfig; + } + + function spyGatewayConfigfromFile(gatewayConfig: MosaicConfig): any { + const spy = sinon.stub( + GatewayConfig, + 'fromFile', + ).callsFake( + sinon.fake.returns(gatewayConfig), + ); + return spy; + } + + function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { + const spy = sinon.stub( + GatewayAddresses, + 'fromGatewayConfig', + ).callsFake( + sinon.fake.returns(gatewayAddresses), + ); + return spy; + } + + it('should pass with valid arguments', () => { + const gatewayConfig = getGatewayConfigStub(); + + const gatewayConfigFromFileSpy = spyGatewayConfigfromFile(gatewayConfig); + const dummyGatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const fromGatewayConfigSpy = spyFromGatewayConfig(dummyGatewayAddresses); + + const { + originChainId, + gatewayAddresses, + } = FacilitatorInit.getFromGatewayConfig(auxChain, gatewayConfigPath); + + SpyAssert.assert(gatewayConfigFromFileSpy, 1, [[gatewayConfigPath]]); + SpyAssert.assert(fromGatewayConfigSpy, 1, [[gatewayConfig]]); + + assert.strictEqual( + originChainId, + originChain, + 'Facilitator object is different', + ); + assert.strictEqual( + gatewayAddresses, + dummyGatewayAddresses, + 'GatewayAddresses object is different', + ); + + sinon.restore(); + }); + + it('should fail when aux chain is not present in gateway config', () => { + const gatewayConfig = getGatewayConfigStub(); + + const gatewayConfigFromFileSpy = spyGatewayConfigfromFile(gatewayConfig); + const invalidAuxChain = 200; + const emptyObject = FacilitatorInit.getFromGatewayConfig(invalidAuxChain, gatewayConfigPath); + + + SpyAssert.assert(gatewayConfigFromFileSpy, 1, [[gatewayConfigPath]]); + assert.strictEqual( + Object.keys(emptyObject).length === 0, + true, + 'Object must be empty', + ); + + sinon.restore(); + }); +}); diff --git a/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts b/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts new file mode 100644 index 00000000..f778329c --- /dev/null +++ b/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts @@ -0,0 +1,109 @@ +// Copyright 2019 OpenST Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ---------------------------------------------------------------------------- + + +import sinon from 'sinon'; + +import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; +import assert from '../../test_utils/assert'; +import SpyAssert from '../../test_utils/SpyAssert'; +import FacilitatorInit from '../../../src/lib/FacilitatorInit'; +import GatewayAddresses from '../../../src/Config/GatewayAddresses'; + + +describe('FacilitatorInit.getFromMosaicConfig()', () => { + const mosaicConfigPath = 'test/Config/mosaic-config.json'; + const auxChain = 3; + const originChain = '2'; + + function spyMosaicfromFile(mosaicConfig: MosaicConfig): any { + const spy = sinon.stub( + MosaicConfig, + 'fromFile', + ).callsFake( + sinon.fake.returns(mosaicConfig), + ); + return spy; + } + + // function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { + // const spy = sinon.stub( + // GatewayAddresses, + // 'fromGatewayConfig', + // ).callsFake( + // sinon.fake.returns(gatewayAddresses), + // ); + // return spy; + // } + + function spyFromMosaicConfig(gatewayAddresses: GatewayAddresses): any { + const spy = sinon.stub( + GatewayAddresses, + 'fromMosaicConfig', + ).callsFake( + sinon.fake.returns(gatewayAddresses), + ); + return spy; + } + + it('should pass with valid arguments', () => { + const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + const mosaic = JSON.parse(mosaicJson) as MosaicConfig; + + const mosaicFromFileSpy = spyMosaicfromFile(mosaic); + const dummyGatewayAddresses = sinon.createStubInstance(GatewayAddresses); + const fromMosaicConfigSpy = spyFromMosaicConfig(dummyGatewayAddresses); + + const { + originChainId, + gatewayAddresses, + } = FacilitatorInit.getFromMosaicConfig(auxChain, mosaicConfigPath); + + SpyAssert.assert(fromMosaicConfigSpy, 1, [[mosaic, auxChain]]); + SpyAssert.assert(mosaicFromFileSpy, 1, [[mosaicConfigPath]]); + + assert.strictEqual( + originChainId, + originChain, + `Expected origin chain id is ${originChain} but got ${originChainId}`, + ); + assert.strictEqual( + gatewayAddresses, + dummyGatewayAddresses, + 'GatewayAddresses object is different', + ); + + sinon.restore(); + }); + + it('should fail when aux chain is not present in mosaic config', () => { + const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + const mosaic = JSON.parse(mosaicJson) as MosaicConfig; + + const mosaicFromFileSpy = spyMosaicfromFile(mosaic); + + const emptyObject = FacilitatorInit.getFromMosaicConfig(100, mosaicConfigPath); + + SpyAssert.assert(mosaicFromFileSpy, 1, [[mosaicConfigPath]]); + assert.strictEqual( + Object.keys(emptyObject).length === 0, + true, + 'Object must be empty', + ); + + sinon.restore(); + }); +}); From e9b733607ca3b4bbf595ab35165b7afe1bb6b5cd Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Tue, 22 Oct 2019 20:18:53 +0530 Subject: [PATCH 11/12] Updated readme; Fixed test cases --- README.md | 41 +++++++++++++++++++++++++----------- src/bin/facilitator.ts | 4 ++-- test/Config/fromFile.test.ts | 1 + 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a0ea8786..1d5fa390 100644 --- a/README.md +++ b/README.md @@ -30,19 +30,36 @@ Facilitator start command will start the facilitator. Facilitator can be started in below two ways :- -1. `./facilitator start --facilitator-config --mosaic-config --gateway-config ` +#### Facilitator start for EIP20 gateways: +1. `./facilitator start --facilitator-config --mosaic-config ` * Replace `` with the path to facilitator-config.json generated using `facilitator init`. - * `--mosaic-config` and `--gateway-config` is optional argument. - * If both `--mosaic-config` and `--facilitator-config` is given then it will read mosaic and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. - * If both `--gateway-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. + * Replace `` with the path to mosaic-config.json. + * When `--mosaic-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. -2. `./facilitator start ` +2. `./facilitator start --facilitator-config --mosaic-config ` * Replace `` with name of the origin chain. * Replace `` with id of the auxiliary chain. - - ##### Options - * `--mosaic-config` and `--facilitator-config` refers to file path of mosaic config and facilitator config respectively. They are optional fields. - * If `--mosaic-config` is given then it will read the facilitator config from default path for `` and mosaic-config from `` path. Argument `` and `` should be present in mosaic-config. - * If `--facilitator-config` is given then it will read the mosaic config from default path for `` and facilitator-config from `` path. Argument`` and `` should be present in it. - * If `--gateway-config` is given then it will read the gateway config from path specified. - * **Note** : Both `--mosaic-config` and `--gateway-config` are together not allowed in command. + * Replace `` with the path to facilitator-config.json generated using `facilitator init`. + * Replace `` with the path to gateway-config.json. + * It validates `` and `` id's in faciltiator and mosaic configs. + +3. `./facilitator start ` + * It loads mosaic config and facilitator config from default paths. + +4. `./facilitator start --facilitator-config ` + * It loads facilitator from `` path. + +#### Facilitator start for mosaic gateways: + +1. `./facilitator start --facilitator-config --gateway-config ` + * Replace `` with the path to facilitator-config.json generated using `facilitator init`. + * Replace `` with the path to gateway-config.json. + * When `--gateway-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `` and `` paths respectively and validates origin and aux chain id's. + +2. `./facilitator start --facilitator-config --gateway-config ` + * Replace `` with name of the origin chain. + * Replace `` with id of the auxiliary chain. + * Replace `` with the path to facilitator-config.json generated using `facilitator init`. + * Replace `` with the path to gateway-config.json. + + * **Note** : Both `--mosaic-config` and `--gateway-config` are together not allowed in command. diff --git a/src/bin/facilitator.ts b/src/bin/facilitator.ts index 87f99c39..3097ef11 100644 --- a/src/bin/facilitator.ts +++ b/src/bin/facilitator.ts @@ -20,6 +20,6 @@ import facilitator from 'commander'; facilitator - .command('init ', 'initializes the facilitator config') - .command('start ', 'starts the facilitator') + .command('init ', 'initializes the facilitator config') + .command('start ', 'starts the facilitator') .parse(process.argv); diff --git a/test/Config/fromFile.test.ts b/test/Config/fromFile.test.ts index ff1d3b28..42015d09 100644 --- a/test/Config/fromFile.test.ts +++ b/test/Config/fromFile.test.ts @@ -88,5 +88,6 @@ describe('Config.fromFile()', () => { () => Config.fromFile(facilitatorConfigPath, mosaicConfigPath, invalidConfigType), `Invalid config type ${invalidConfigType}`, ); + sinon.restore(); }); }); From f681282f206726079b9e5699eb728aed6f8d5cdb Mon Sep 17 00:00:00 2001 From: Gulshan Vasnani Date: Wed, 23 Oct 2019 16:07:01 +0530 Subject: [PATCH 12/12] Updated gateway-config short option; Added types of stub methods --- src/bin/facilitator-init.ts | 4 +- src/bin/facilitator-start.ts | 2 +- test/ConfigFactory/getConfig.test.ts | 99 ++++++++++++------- .../getFromGatewayConfig.test.ts | 32 ++++-- .../getFromMosaicConfig.test.ts | 42 ++++---- 5 files changed, 116 insertions(+), 63 deletions(-) diff --git a/src/bin/facilitator-init.ts b/src/bin/facilitator-init.ts index 44dbf4f0..959be08f 100644 --- a/src/bin/facilitator-init.ts +++ b/src/bin/facilitator-init.ts @@ -32,14 +32,14 @@ import FacilitatorInit from '../lib/FacilitatorInit'; commander .option('-m, --mosaic-config ', 'path to mosaic configuration') - .option('-q, --gateway-config ', 'path to gateway configuration') + .option('-g, --gateway-config ', 'path to gateway configuration') .option('-c, --aux-chain-id ', 'auxiliary chain id') .option('-o, --origin-password ', 'origin chain account password') .option('-a, --auxiliary-password ', 'auxiliary chain account password') .option('-r, --origin-rpc ', 'origin chain rpc') .option('-h, --auxiliary-rpc ', 'auxiliary chain rpc') .option('-e, --origin-graph-ws ', 'origin ws subgraph endpoint ') - .option('-g, --origin-graph-rpc ', 'origin rpc subgraph endpoint') + .option('-n, --origin-graph-rpc ', 'origin rpc subgraph endpoint') .option('-s, --auxiliary-graph-ws ', 'auxiliary ws subgraph endpoint') .option('-i, --auxiliary-graph-rpc ', 'auxiliary rpc subgraph endpoint') .option('-d, --db-path ', 'path where db path is present') diff --git a/src/bin/facilitator-start.ts b/src/bin/facilitator-start.ts index 6bb33d4e..9ff55aa4 100644 --- a/src/bin/facilitator-start.ts +++ b/src/bin/facilitator-start.ts @@ -40,7 +40,7 @@ process.on('SIGTERM', terminationHandler); facilitatorCmd .option('-m, --mosaic-config ', 'path to mosaic configuration') - .option('-q, --gateway-config ', 'path to gateway configuration') + .option('-g, --gateway-config ', 'path to gateway configuration') .option('-t, --facilitator-config ', 'path to facilitator configuration') .action(async (origin_chain, aux_chain_id, options) => { let mandatoryOptionMissing = false; diff --git a/test/ConfigFactory/getConfig.test.ts b/test/ConfigFactory/getConfig.test.ts index a1c5bb9a..0834e4d9 100644 --- a/test/ConfigFactory/getConfig.test.ts +++ b/test/ConfigFactory/getConfig.test.ts @@ -15,7 +15,7 @@ // ---------------------------------------------------------------------------- -import sinon from 'sinon'; +import sinon, { SinonStub, SinonStubbedInstance } from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; @@ -29,22 +29,50 @@ describe('ConfigFactory.getConfig()', () => { const originChain = '2'; const auxChain = 3; - function getMosaic(): string { - return `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; + function getMosaic(): object { + return { + originChain: + { + chain: originChain, + }, + auxiliaryChains: + { + [auxChain]: + { + chainId: auxChain, + }, + }, + }; } - function getFacilitator(): string { - return `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; + function getFacilitator(): object { + return { + originChain, + auxChainId: auxChain, + chains: + { + [originChain]: + { + worker: '0x123', + }, + [auxChain]: + { + worker: '0x123', + }, + }, + }; } const facilitatorConfigPath = './facilitator-config.json'; const mosaicConfigPath = './test/mosaic-config.json'; const gatewayConfigPath = './gateway-config.json'; - const mosaic = JSON.parse(getMosaic()) as MosaicConfig; + const mosaic = getMosaic() as MosaicConfig; - const facilitator = JSON.parse(getFacilitator()) as FacilitatorConfig; + const facilitator = getFacilitator() as FacilitatorConfig; - function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { + function spyFromGatewayConfig( + gatewayAddresses: GatewayAddresses, + ): SinonStub<[GatewayConfig], GatewayAddresses> { const spy = sinon.stub( GatewayAddresses, 'fromGatewayConfig', @@ -54,7 +82,9 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyFacilitatorFromFile(fcConfig: any): any { + function spyFacilitatorFromFile( + fcConfig: FacilitatorConfig | SinonStubbedInstance, + ): SinonStub<[string], FacilitatorConfig> { const spy = sinon.stub( FacilitatorConfig, 'fromFile', @@ -64,7 +94,9 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyGatewayConfigFromFile(gatewayConfig: GatewayConfig): any { + function spyGatewayConfigFromFile( + gatewayConfig: GatewayConfig, + ): SinonStub<[string], GatewayConfig> { const spy = sinon.stub( GatewayConfig, 'fromFile', @@ -74,7 +106,9 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyFacilitatorFromChain(fcConfig: any): any { + function spyFacilitatorFromChain( + fcConfig: FacilitatorConfig, + ): SinonStub<[number], FacilitatorConfig> { const spy = sinon.stub( FacilitatorConfig, 'fromChain', @@ -84,7 +118,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyMosaicFromChain(mosaicConfig: MosaicConfig): any { + function spyMosaicFromChain(mosaicConfig: MosaicConfig): SinonStub<[string], MosaicConfig> { const spy = sinon.stub( MosaicConfig, 'fromChain', @@ -94,7 +128,9 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyGatewayAddressesFromMosaicConfig(gatewayAddresses: GatewayAddresses): any { + function spyGatewayAddressesFromMosaicConfig( + gatewayAddresses: GatewayAddresses, + ): SinonStub<[MosaicConfig, number], GatewayAddresses> { const spy = sinon.stub( GatewayAddresses, 'fromMosaicConfig', @@ -104,7 +140,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyMosaicfromFile(mosaicConfig: MosaicConfig): any { + function spyMosaicfromFile(mosaicConfig: MosaicConfig): SinonStub<[string], MosaicConfig> { const spy = sinon.stub( MosaicConfig, 'fromFile', @@ -114,7 +150,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyConfigfromFile(config: Config): any { + function spyConfigfromFile(config: Config): SinonStub<[string, string, ConfigType], Config> { const spy = sinon.stub( Config, 'fromFile', @@ -124,7 +160,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function spyMosaicExists(status: boolean): any { + function spyMosaicExists(status: boolean): SinonStub<[string], boolean> { const spy = sinon.stub( MosaicConfig, 'exists', @@ -134,7 +170,7 @@ describe('ConfigFactory.getConfig()', () => { return spy; } - function getGatewayConfigStub(): any { + function getGatewayConfigStub(auxChain: number): SinonStubbedInstance { const stubGatewayConfig = sinon.createStubInstance(GatewayConfig); stubGatewayConfig.mosaicConfig = mosaic; stubGatewayConfig.auxChainId = auxChain; @@ -240,9 +276,8 @@ describe('ConfigFactory.getConfig()', () => { }); it('should fail when mosaic config path is provided and input origin chain doesn\'t match in it', () => { - const config = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; const dummyoriginChain = '9'; - const spy = spyMosaicfromFile(JSON.parse(config)); + const spy = spyMosaicfromFile(getMosaic() as MosaicConfig); const fs: ConfigFactory = new ConfigFactory( dummyoriginChain, @@ -261,9 +296,8 @@ describe('ConfigFactory.getConfig()', () => { }); it('should fail when mosaic config path is provided and aux chain id is not present in it', () => { - const config = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; const dummyAuxChainId = 9; - const spy = spyMosaicfromFile(JSON.parse(config)); + const spy = spyMosaicfromFile(getMosaic() as MosaicConfig); const fs: ConfigFactory = new ConfigFactory( originChain, dummyAuxChainId, @@ -282,10 +316,9 @@ describe('ConfigFactory.getConfig()', () => { it('should pass when origin chain, aux chain id is provided and facilitator config path is ' + 'provided', () => { - const config = `{"originChain":"7","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; const mosaicConfig = sinon.createStubInstance(MosaicConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); - const facilitatorConfig = JSON.parse(config) as FacilitatorConfig; + const facilitatorConfig = getFacilitator() as FacilitatorConfig; const facilitatorSpy = spyFacilitatorFromFile(facilitatorConfig); const mosaicSpy = spyMosaicFromChain(mosaicConfig); const gatewayAddressesSpy = spyGatewayAddressesFromMosaicConfig(gatewayAddresses); @@ -428,7 +461,6 @@ describe('ConfigFactory.getConfig()', () => { () => fs.getConfig(), 'origin chain id in mosaic config is different than the one provided', ); - sinon.restore(); }); @@ -458,9 +490,8 @@ describe('ConfigFactory.getConfig()', () => { }); it('should pass when only facilitator config is provided', () => { - const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); - const facilitatorSpy = spyFacilitatorFromFile(JSON.parse(facilitatorJson)); + const facilitatorSpy = spyFacilitatorFromFile(getFacilitator() as FacilitatorConfig); const mosaicSpy = spyMosaicFromChain(mosaic); const gatewayAddressesSpy = spyGatewayAddressesFromMosaicConfig(gatewayAddresses); const mosaicExistsSpy = spyMosaicExists(true); @@ -527,7 +558,7 @@ describe('ConfigFactory.getConfig()', () => { }); it('should pass when origin chain, aux chain id and gateway config is provided', () => { - const stubGatewayConfig = getGatewayConfigStub(); + const stubGatewayConfig = getGatewayConfigStub(auxChain); const facilitatorFromChainSpy = spyFacilitatorFromChain(facilitator); const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -563,7 +594,7 @@ describe('ConfigFactory.getConfig()', () => { }); it('should fail when origin chain, aux chain id and gateway config is provided but aux chain id is incorrect in mosaic config', () => { - const stubGatewayConfig = getGatewayConfigStub(); + const stubGatewayConfig = getGatewayConfigStub(auxChain); spyFacilitatorFromChain(facilitator); spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -587,7 +618,7 @@ describe('ConfigFactory.getConfig()', () => { }); it('should pass when origin chain, aux chain id and gateway config is provided', () => { - const stubGatewayConfig = getGatewayConfigStub(); + const stubGatewayConfig = getGatewayConfigStub(auxChain); const facilitatorFromChainSpy = spyFacilitatorFromChain(facilitator); const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -626,7 +657,7 @@ describe('ConfigFactory.getConfig()', () => { }); it('should pass when facilitator and gateway config path is provided', () => { - const stubGatewayConfig = getGatewayConfigStub(); + const stubGatewayConfig = getGatewayConfigStub(auxChain); const facilitatorFromFileSpy = spyFacilitatorFromFile(facilitator); const gatewayConfigFromFileSpy = spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -662,9 +693,8 @@ describe('ConfigFactory.getConfig()', () => { }); it('should fail when facilitator and gateway config path is provided but aux chain id is different', () => { - const stubGatewayConfig = getGatewayConfigStub(); - const auxChainIdInGatewayConfig = '200'; - stubGatewayConfig.auxChainId = auxChainIdInGatewayConfig; + const auxChainIdInGatewayConfig = 200; + const stubGatewayConfig = getGatewayConfigStub(auxChainIdInGatewayConfig); spyFacilitatorFromFile(facilitator); spyGatewayConfigFromFile(stubGatewayConfig); const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); @@ -688,9 +718,8 @@ describe('ConfigFactory.getConfig()', () => { }); it('should fail when only facilitator config is provided but mosaic config is not found', () => { - const facilitatorJson = `{"originChain":"${originChain}","auxChainId":${auxChain},"chains":{"${originChain}":{"worker": "0x123"},"${auxChain}":{"worker": "0x123"}}}`; const gatewayAddresses = sinon.createStubInstance(GatewayAddresses); - const facilitatorSpy = spyFacilitatorFromFile(JSON.parse(facilitatorJson)); + const facilitatorSpy = spyFacilitatorFromFile(getFacilitator() as FacilitatorConfig); const mosaicConfig = MosaicConfig.fromChain('200'); spyMosaicExists(false); spyMosaicFromChain(mosaicConfig); diff --git a/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts b/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts index 02748842..68966f9f 100644 --- a/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts +++ b/test/lib/FacilitatorInit/getFromGatewayConfig.test.ts @@ -15,7 +15,7 @@ // ---------------------------------------------------------------------------- -import sinon from 'sinon'; +import sinon, { SinonStub, SinonStubbedInstance } from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import GatewayConfig from '@openst/mosaic-chains/lib/src/Config/GatewayConfig'; @@ -29,17 +29,35 @@ describe('FacilitatorInit.getFromGatewayConfig()', () => { const gatewayConfigPath = 'test/Config/gateway-config.json'; const auxChain = 3; const originChain = '2'; - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; - function getGatewayConfigStub(): any { + function getMosaic(): object { + return { + originChain: + { + chain: originChain, + }, + auxiliaryChains: + { + [auxChain]: + { + chainId: auxChain, + }, + }, + }; + } + + const mosaic = getMosaic() as MosaicConfig; + + function getGatewayConfigStub(): SinonStubbedInstance { const stubGatewayConfig = sinon.createStubInstance(GatewayConfig); stubGatewayConfig.mosaicConfig = mosaic; stubGatewayConfig.auxChainId = auxChain; return stubGatewayConfig; } - function spyGatewayConfigfromFile(gatewayConfig: MosaicConfig): any { + function spyGatewayConfigfromFile( + gatewayConfig: GatewayConfig, + ): SinonStub<[string], GatewayConfig> { const spy = sinon.stub( GatewayConfig, 'fromFile', @@ -49,7 +67,9 @@ describe('FacilitatorInit.getFromGatewayConfig()', () => { return spy; } - function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { + function spyFromGatewayConfig( + gatewayAddresses: GatewayAddresses, + ): SinonStub<[GatewayConfig], GatewayAddresses> { const spy = sinon.stub( GatewayAddresses, 'fromGatewayConfig', diff --git a/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts b/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts index f778329c..a07925f5 100644 --- a/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts +++ b/test/lib/FacilitatorInit/getFromMosaicConfig.test.ts @@ -15,7 +15,7 @@ // ---------------------------------------------------------------------------- -import sinon from 'sinon'; +import sinon, { SinonStub } from 'sinon'; import MosaicConfig from '@openst/mosaic-chains/lib/src/Config/MosaicConfig'; import assert from '../../test_utils/assert'; @@ -29,7 +29,25 @@ describe('FacilitatorInit.getFromMosaicConfig()', () => { const auxChain = 3; const originChain = '2'; - function spyMosaicfromFile(mosaicConfig: MosaicConfig): any { + function getMosaic(): object { + return { + originChain: + { + chain: originChain, + }, + auxiliaryChains: + { + [auxChain]: + { + chainId: auxChain, + }, + }, + }; + } + + const mosaic = getMosaic() as MosaicConfig; + + function spyMosaicfromFile(mosaicConfig: MosaicConfig): SinonStub<[string], MosaicConfig> { const spy = sinon.stub( MosaicConfig, 'fromFile', @@ -39,17 +57,9 @@ describe('FacilitatorInit.getFromMosaicConfig()', () => { return spy; } - // function spyFromGatewayConfig(gatewayAddresses: GatewayAddresses): any { - // const spy = sinon.stub( - // GatewayAddresses, - // 'fromGatewayConfig', - // ).callsFake( - // sinon.fake.returns(gatewayAddresses), - // ); - // return spy; - // } - - function spyFromMosaicConfig(gatewayAddresses: GatewayAddresses): any { + function spyFromMosaicConfig( + gatewayAddresses: GatewayAddresses, + ): SinonStub<[MosaicConfig, number], GatewayAddresses> { const spy = sinon.stub( GatewayAddresses, 'fromMosaicConfig', @@ -60,9 +70,6 @@ describe('FacilitatorInit.getFromMosaicConfig()', () => { } it('should pass with valid arguments', () => { - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; - const mosaicFromFileSpy = spyMosaicfromFile(mosaic); const dummyGatewayAddresses = sinon.createStubInstance(GatewayAddresses); const fromMosaicConfigSpy = spyFromMosaicConfig(dummyGatewayAddresses); @@ -90,9 +97,6 @@ describe('FacilitatorInit.getFromMosaicConfig()', () => { }); it('should fail when aux chain is not present in mosaic config', () => { - const mosaicJson = `{"originChain":{"chain":"${originChain}"},"auxiliaryChains":{"${auxChain}":{"chainId": ${auxChain}}}}`; - const mosaic = JSON.parse(mosaicJson) as MosaicConfig; - const mosaicFromFileSpy = spyMosaicfromFile(mosaic); const emptyObject = FacilitatorInit.getFromMosaicConfig(100, mosaicConfigPath);