Skip to content

Commit

Permalink
Merge pull request #231 from gulshanvasnani/init_start_support_gatewa…
Browse files Browse the repository at this point in the history
…yconfig

Init start support gatewayconfig
  • Loading branch information
0xsarvesh authored Oct 23, 2019
2 parents 26f404c + a89317c commit 94a95c6
Show file tree
Hide file tree
Showing 20 changed files with 964 additions and 214 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mosaic-config> --aux-chain-id <aux-chain-id> --origin-password <origin-password> --auxiliary-password <auxiliary-password> --origin-rpc <origin-rpc> --auxiliary-rpc <auxiliary-rpc> --origin-graph-ws <origin-graph-ws> --origin-graph-rpc <origin-graph-rpc> --auxiliary-graph-ws <auxiliary-graph-ws> --auxiliary-graph-rpc <auxiliary-graph-rpc> --db-path <db-path> --force
./facilitator init --gateway-config <gateway-config> --mosaic-config <mosaic-config> --aux-chain-id <aux-chain-id> --origin-password <origin-password> --auxiliary-password <auxiliary-password> --origin-rpc <origin-rpc> --auxiliary-rpc <auxiliary-rpc> --origin-graph-ws <origin-graph-ws> --origin-graph-rpc <origin-graph-rpc> --auxiliary-graph-ws <auxiliary-graph-ws> --auxiliary-graph-rpc <auxiliary-graph-rpc> --db-path <db-path> --force
```

* Replace `<gateway-config>` with location where gateway config is present.
* Replace `<mosaic-config>` with location where mosaic config is present.
* Replace `<aux-chain-id>` with auxiliary chain id.
* Replace `<origin-password>` 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.
Expand All @@ -29,17 +30,36 @@ Facilitator start command will start the facilitator.

Facilitator can be started in below two ways :-

#### Facilitator start for EIP20 gateways:
1. `./facilitator start --facilitator-config <facilitator-config> --mosaic-config <mosaic-config>`
* Replace `<facilitator-config>` 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 `<mosaic-config>` and `<facilitator-config>` paths respectively.
* Replace `<facilitator-config>` with the path to facilitator-config.json generated using `facilitator init`.
* Replace `<mosaic-config>` with the path to mosaic-config.json.
* When `--mosaic-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `<gateway-config>` and `<facilitator-config>` paths respectively and validates origin and aux chain id's.

2. `./facilitator start <origin-chain> <aux-chain-id> `
2. `./facilitator start <origin-chain> <aux-chain-id> --facilitator-config <facilitator-config> --mosaic-config <mosaic-config>`
* Replace `<origin-chain>` with name of the origin chain.
* Replace `<aux-chain-id>` 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 `<aux-chain-id>` and mosaic-config from `<mosaic-config>` path. Argument `<origin-chain>` and `<aux-chain-id>` should be present in mosaic-config.
* If `--facilitator-config` is given then it will read the mosaic config from default path for `<origin-chain>` and facilitator-config from `<facilitator-config>` path. Argument`<origin-chain>` and `<aux-chain-id>` should be present in it.
* Replace `<facilitator-config>` with the path to facilitator-config.json generated using `facilitator init`.
* Replace `<mosaic-config>` with the path to gateway-config.json.
* It validates `<origin-chain>` and `<auxiliary-chain>` id's in faciltiator and mosaic configs.

3. `./facilitator start <origin-chain> <aux-chain-id>`
* It loads mosaic config and facilitator config from default paths.

4. `./facilitator start --facilitator-config <facilitator-config>`
* It loads facilitator from `<facilitator-config>` path.

#### Facilitator start for mosaic gateways:

1. `./facilitator start --facilitator-config <facilitator-config> --gateway-config <gateway-config>`
* Replace `<facilitator-config>` with the path to facilitator-config.json generated using `facilitator init`.
* Replace `<gateway-config>` with the path to gateway-config.json.
* When `--gateway-config` and `--facilitator-config` is given then it will read gateway and facilitator configs from `<gateway-config>` and `<facilitator-config>` paths respectively and validates origin and aux chain id's.

2. `./facilitator start <origin-chain> <aux-chain-id> --facilitator-config <facilitator-config> --gateway-config <gateway-config>`
* Replace `<origin-chain>` with name of the origin chain.
* Replace `<aux-chain-id>` with id of the auxiliary chain.
* Replace `<facilitator-config>` with the path to facilitator-config.json generated using `facilitator init`.
* Replace `<gateway-config>` with the path to gateway-config.json.

* **Note** : Both `--mosaic-config` and `--gateway-config` are together not allowed in command.
36 changes: 27 additions & 9 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,6 +42,12 @@ enum DBType {
SQLITE = 'SQLITE',
}

// Database type
export enum ConfigType {
GATEWAY = 'Gateway',
MOSAIC = 'Mosaic',
}

/**
* Holds database configurations.
*/
Expand Down Expand Up @@ -314,23 +321,34 @@ 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 mosaicConfigPath Path to mosaic config file path.
* @param facilitatorConfigPath Path to facilitator config file path/
* 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 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(
mosaicConfigPath: string,
facilitatorConfigPath: string,
configPath: string,
configType: ConfigType,
): Config {
const mosaic: MosaicConfig = MosaicConfig.fromFile(mosaicConfigPath);
const facilitator: FacilitatorConfig = FacilitatorConfig.fromFile(facilitatorConfigPath);

return new Config(
GatewayAddresses.fromMosaicConfig(
mosaic,
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,
),
);
} else {
throw new Error(`Invalid config type ${configType}`);
}

return new Config(
gatewayAddresses,
facilitator,
);
}
Expand Down
89 changes: 72 additions & 17 deletions src/Config/ConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


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 */
Expand All @@ -34,27 +35,32 @@ 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;
}

/**
* 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()) {
Expand All @@ -65,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();
Expand All @@ -74,15 +80,25 @@ 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,
ConfigType.MOSAIC,
);
}

if (this.gatewayConfigPath) {
return Config.fromFile(
this.facilitatorConfigPath,
this.gatewayConfigPath,
ConfigType.GATEWAY,
);
}

const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(this.originChain!);
Expand All @@ -109,6 +125,16 @@ export default class ConfigFactory {
);
}

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(
Expand All @@ -123,16 +149,16 @@ 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;
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);

Expand All @@ -143,17 +169,31 @@ 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.
if (!MosaicConfig.exists(this.originChain)) {
throw new Error('mosaic config not found');
}
const mosaicConfig: MosaicConfig = MosaicConfig.fromChain(
facilitatorConfig.originChain,
this.originChain,
);

this.verifyChainIdInMosaicConfig(mosaicConfig);
configObj = new Config(
GatewayAddresses.fromMosaicConfig(
mosaicConfig,
facilitatorConfig.auxChainId,
this.auxChainId,
),
facilitatorConfig,
);
Expand Down Expand Up @@ -191,13 +231,28 @@ export default class ConfigFactory {
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.
*/
Expand Down
23 changes: 4 additions & 19 deletions src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
facilitatorConfigPath?: string,
config: Config,

): Promise<Facilitator> {
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,
Expand Down
Loading

0 comments on commit 94a95c6

Please sign in to comment.