Skip to content

Commit

Permalink
fix(plugin-consortium-manual): drop repo constructor arg hyperledger#…
Browse files Browse the repository at this point in the history
…1199

Removes the non-serializable consortiumRepo argument from the
constructor of the consortium plugin manual class's options.

Refactors the constructor and the internals of the plugin  to initialize
the consortium repo from the consortium database at runtime
instead of expecting it passed in via the constructor.
Refactors the internal code previously using the options.consoritumRepo
object to use this.repo instead which is what gets initalized in
the constructor as explained above.

All this leads to equivalent functionality but less boilerplate and now
thanks to this the plugin can be (should be - more tests needed)
initialized by the API server purely based on the static configuration
file when necessary.

Fixes hyperledger#1199

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz authored and Leeyoungone committed Aug 26, 2021
1 parent 0e79dd0 commit 180253c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
LedgerType,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry, ConsortiumRepository } from "@hyperledger/cactus-core";
import { PluginRegistry } from "@hyperledger/cactus-core";

import {
LogLevelDesc,
Expand Down Expand Up @@ -210,16 +210,13 @@ export class SupplyChainApp {
const connectionProfile = await this.ledgers.fabric.getConnectionProfileOrg1();
const sshConfig = await this.ledgers.fabric.getSshConfig();

const consortiumRepo = new ConsortiumRepository({ db: consortiumDatabase });

const registryA = new PluginRegistry({
plugins: [
new PluginConsortiumManual({
instanceId: "PluginConsortiumManual_A",
consortiumDatabase,
keyPairPem: keyPairPemA,
logLevel: this.options.logLevel,
consortiumRepo,
}),
new SupplyChainCactusPlugin({
logLevel: this.options.logLevel,
Expand Down Expand Up @@ -259,7 +256,6 @@ export class SupplyChainApp {
consortiumDatabase,
keyPairPem: keyPairPemB,
logLevel: this.options.logLevel,
consortiumRepo,
}),
new SupplyChainCactusPlugin({
logLevel: this.options.logLevel,
Expand Down Expand Up @@ -298,7 +294,6 @@ export class SupplyChainApp {
consortiumDatabase,
keyPairPem: keyPairPemC,
logLevel: "INFO",
consortiumRepo,
}),
new SupplyChainCactusPlugin({
logLevel: "INFO",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export interface IWebAppOptions {
export interface IPluginConsortiumManualOptions extends ICactusPluginOptions {
keyPairPem: string;
consortiumDatabase: ConsortiumDatabase;
consortiumRepo: ConsortiumRepository;
prometheusExporter?: PrometheusExporter;
pluginRegistry?: PluginRegistry;
logLevel?: LogLevelDesc;
Expand All @@ -57,12 +56,18 @@ export interface IPluginConsortiumManualOptions extends ICactusPluginOptions {

export class PluginConsortiumManual
implements ICactusPlugin, IPluginWebService {
public static readonly CLASS_NAME = "PluginConsortiumManual";
public prometheusExporter: PrometheusExporter;
private readonly log: Logger;
private readonly instanceId: string;
private readonly repo: ConsortiumRepository;
private endpoints: IWebServiceEndpoint[] | undefined;
private httpServer: Server | SecureServer | null = null;

public get className(): string {
return PluginConsortiumManual.CLASS_NAME;
}

constructor(public readonly options: IPluginConsortiumManualOptions) {
const fnTag = `PluginConsortiumManual#constructor()`;
if (!options) {
Expand All @@ -73,13 +78,18 @@ export class PluginConsortiumManual
options.consortiumDatabase,
`${fnTag} options.consortiumDatabase`,
);

this.log = LoggerProvider.getOrCreate({
label: "plugin-consortium-manual",
});

this.instanceId = this.options.instanceId;
this.repo = new ConsortiumRepository({ db: options.consortiumDatabase });

this.prometheusExporter =
options.prometheusExporter ||
new PrometheusExporter({ pollingIntervalInMin: 1 });

Checks.truthy(
this.prometheusExporter,
`${fnTag} options.prometheusExporter`,
Expand Down Expand Up @@ -107,12 +117,8 @@ export class PluginConsortiumManual
}

public getNodeCount(): number {
const consortiumDatabase: ConsortiumDatabase = this.options
.consortiumDatabase;
const consortiumRepo: ConsortiumRepository = new ConsortiumRepository({
db: consortiumDatabase,
});
return consortiumRepo.allNodes.length;
Checks.truthy(this.repo, `${this.className}.this.repo`);
return this.repo.allNodes.length;
}

/**
Expand All @@ -121,12 +127,8 @@ export class PluginConsortiumManual
* only affects **the metrics**.
*/
public updateMetricNodeCount(): void {
const consortiumDatabase: ConsortiumDatabase = this.options
.consortiumDatabase;
const consortiumRepo: ConsortiumRepository = new ConsortiumRepository({
db: consortiumDatabase,
});
this.prometheusExporter.setNodeCount(consortiumRepo.allNodes.length);
const nodeCount = this.getNodeCount();
this.prometheusExporter.setNodeCount(nodeCount);
}

public async shutdown(): Promise<void> {
Expand Down Expand Up @@ -184,10 +186,8 @@ export class PluginConsortiumManual
// presence of webAppOptions implies that caller wants the plugin to configure it's own express instance on a custom
// host/port to listen on

const { consortiumDatabase, keyPairPem } = this.options;
const consortiumRepo = new ConsortiumRepository({
db: consortiumDatabase,
});
const { keyPairPem } = this.options;
const consortiumRepo = this.repo;

const endpoints: IWebServiceEndpoint[] = [];
{
Expand Down Expand Up @@ -229,11 +229,12 @@ export class PluginConsortiumManual
}

public async getNodeJws(): Promise<JWSGeneral> {
const { keyPairPem, consortiumRepo: repo } = this.options;
Checks.truthy(this.repo, `${this.className}.this.repo`);
const { keyPairPem } = this.options;

this.updateMetricNodeCount();
const keyPair = JWK.asKey(keyPairPem);
const payloadObject = { consortiumDatabase: repo.consortiumDatabase };
const payloadObject = { consortiumDatabase: this.repo.consortiumDatabase };
const payloadJson = jsonStableStringify(payloadObject);
const _protected = {
iat: Date.now(),
Expand All @@ -245,7 +246,7 @@ export class PluginConsortiumManual
}

public async getConsortiumJws(): Promise<JWSGeneral> {
const nodes = this.options.consortiumRepo.allNodes;
const nodes = this.repo.allNodes;

const requests = nodes
.map((cnm) => cnm.nodeApiHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ import {
Configuration,
} from "@hyperledger/cactus-core-api";

import { ConsortiumRepository } from "@hyperledger/cactus-core";

import {
PluginConsortiumManual,
IPluginConsortiumManualOptions,
} from "../../../../main/typescript/plugin-consortium-manual";

import {
GetNodeJwsEndpoint,
// IGetNodeJwsEndpointOptions,
} from "../../../../main/typescript/public-api";

import { GetNodeJwsEndpoint } from "../../../../main/typescript/public-api";

import { v4 as uuidv4 } from "uuid";

import { DefaultApi as ConsortiumManualApi } from "../../../../main/typescript/public-api";
Expand All @@ -44,14 +39,12 @@ test("Can provide JWS", async (t: Test) => {
ledger: [],
pluginInstance: [],
};
const consortiumRepo = new ConsortiumRepository({ db });

// Creating the PluginConsortiumManual object to observe the prometheus metrics.
const options: IPluginConsortiumManualOptions = {
instanceId: uuidv4(),
keyPairPem: keyPairPem,
consortiumDatabase: db,
consortiumRepo,
};

const pluginConsortiumManual: PluginConsortiumManual = new PluginConsortiumManual(
Expand Down Expand Up @@ -138,7 +131,7 @@ test("Can provide JWS", async (t: Test) => {
publicKeyPem: "",
};

consortiumRepo.consortiumDatabase.cactusNode.push(dummyCactusNode);
db.cactusNode.push(dummyCactusNode);
// The invocation of the node JWS endpoint internally triggers the update
// of the metrics so after it has executed we can expect the metrics to
// show the new values for our assertions below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Ledger,
LedgerType,
} from "@hyperledger/cactus-core-api";
import { PluginRegistry, ConsortiumRepository } from "@hyperledger/cactus-core";
import { PluginRegistry } from "@hyperledger/cactus-core";
import {
DefaultApi as QuorumApi,
PluginLedgerConnectorQuorum,
Expand Down Expand Up @@ -128,8 +128,6 @@ test(testCase, async (t: Test) => {
pluginInstance: [],
};

const consortiumRepo = new ConsortiumRepository({ db: consortiumDatabase });

const config = new Configuration({ basePath: consortium.mainApiHost });
const mainApiClient = new ApiClient(config);

Expand Down Expand Up @@ -176,7 +174,6 @@ test(testCase, async (t: Test) => {
keyPairPem: keyPair1.toPEM(true),
consortiumDatabase,
logLevel,
consortiumRepo,
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down Expand Up @@ -219,7 +216,6 @@ test(testCase, async (t: Test) => {
keyPairPem: keyPair2.toPEM(true),
consortiumDatabase,
logLevel,
consortiumRepo,
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ConsortiumDatabase,
ConsortiumMember,
} from "@hyperledger/cactus-core-api";
import { PluginRegistry, ConsortiumRepository } from "@hyperledger/cactus-core";
import { PluginRegistry } from "@hyperledger/cactus-core";

test("member node public keys and hosts are pre-shared", async (t: Test) => {
const consortiumId = uuidV4();
Expand Down Expand Up @@ -138,7 +138,6 @@ test("member node public keys and hosts are pre-shared", async (t: Test) => {
pluginInstance: [],
};

const consortiumRepo = new ConsortiumRepository({ db: consortiumDatabase });
t.comment(`Setting up first node...`);
{
// 2. Instantiate plugin registry which will provide the web service plugin with the key value storage plugin
Expand All @@ -151,7 +150,6 @@ test("member node public keys and hosts are pre-shared", async (t: Test) => {
keyPairPem: keyPair1.toPEM(true),
consortiumDatabase,
logLevel: "trace",
consortiumRepo,
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down Expand Up @@ -205,8 +203,7 @@ test("member node public keys and hosts are pre-shared", async (t: Test) => {
pluginRegistry,
keyPairPem: keyPair2.toPEM(true),
consortiumDatabase,
logLevel: "trace",
consortiumRepo,
logLevel: "TRACE",
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down Expand Up @@ -261,8 +258,7 @@ test("member node public keys and hosts are pre-shared", async (t: Test) => {
pluginRegistry,
keyPairPem: keyPair3.toPEM(true),
consortiumDatabase,
logLevel: "trace",
consortiumRepo,
logLevel: "TRACE",
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Consortium,
ConsortiumDatabase,
} from "@hyperledger/cactus-core-api";
import { PluginRegistry, ConsortiumRepository } from "@hyperledger/cactus-core";
import { PluginRegistry } from "@hyperledger/cactus-core";

import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import {
Expand Down Expand Up @@ -83,8 +83,6 @@ tap.test(
consortium: [consortium],
};

const consortiumRepo = new ConsortiumRepository({ db: consortiumDatabase });

// 3. Instantiate the web service consortium plugin which will host itself on a new TCP port for isolation/security
// Note that if we omitted the `webAppOptions` object that the web service plugin would default to installing itself
// on the default port of the API server. This allows for flexibility in deployments.
Expand All @@ -98,7 +96,6 @@ tap.test(
hostname: "127.0.0.1",
port: 0,
},
consortiumRepo,
};
const pluginConsortiumManual = new PluginConsortiumManual(options);

Expand Down

0 comments on commit 180253c

Please sign in to comment.