Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Init start support gatewayconfig #231

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 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,10 +30,11 @@ Facilitator start command will start the facilitator.

Facilitator can be started in below two ways :-

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.
1. `./facilitator start --facilitator-config <facilitator-config> --mosaic-config <mosaic-config> --gateway-config <gateway-config>`
abhayks1 marked this conversation as resolved.
Show resolved Hide resolved
* Replace `<facilitator-config>` 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 `<mosaic-config>` and `<facilitator-config>` paths respectively.
* If both `--gateway-config` is given it will read gateway and facilitator configs from `<gateway-config>` and `<facilitator-config>` paths respectively.

2. `./facilitator start <origin-chain> <aux-chain-id> `
* Replace `<origin-chain>` with name of the origin chain.
Expand All @@ -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 `<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.

* If `--gateway-config` is given then it will read the gateway config from path specified.
abhayks1 marked this conversation as resolved.
Show resolved Hide resolved
* **Note** : Both `--mosaic-config` and `--gateway-config` are together not allowed in command.
18 changes: 11 additions & 7 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 Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both params mosaicConfig and gatewayConfig are optional. It should check one of both exists.

Compiler is also complaining on same point on statement gatewayConfigPath! because of !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, added configType parameters in fromFile.

? GatewayAddresses.fromMosaicConfig(
MosaicConfig.fromFile(mosaicConfigPath),
facilitator.auxChainId,
) : GatewayAddresses.fromGatewayConfig(GatewayConfig.fromFile(gatewayConfigPath!));

return new Config(
GatewayAddresses.fromMosaicConfig(
mosaic,
facilitator.auxChainId,
),
gatewayAddresses,
facilitator,
);
}
Expand Down
68 changes: 55 additions & 13 deletions src/Config/ConfigFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}

/**
Expand All @@ -74,15 +80,17 @@ 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!);
Expand All @@ -109,6 +117,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 @@ -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);

Expand All @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should check if mosaic config exists. Because from chain with return blank object if it doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added suggested approach.
But i think, fromChain should throw error when path is not present.
It will be aligned to what we do in fromFile method.
What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created ticket to align fromChain and fromFile methods OpenST/mosaic-chains#196

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 +219,27 @@ 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