Skip to content

Commit

Permalink
feat(cactus-api-client): common verifier-factory
Browse files Browse the repository at this point in the history
Adjust SocketIOApiClient and common Verifier behavior to previous one in
cmd-socketio, fix related tests. Extract Verifier interfaces to common
location to ensure interface compatibility between old and new verifier
/ verifier factory (both should implement same interface). Create new
package cactus-verifier-client for common verifier related stuff, to
prevent circular dependencies. Add verifier-factory to create verifiers
by supplying it's ID only, based on initial configuration. Configuration
is set in ctor, but can be read from a file as in cmd-socketio
scenarious. VerifierFactory config should be compatible with existing
ledgerPluginInfo.

Closes: hyperledger-cacti#1878
Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH committed Mar 2, 2022
1 parent 0ccf1ca commit 2b57f69
Show file tree
Hide file tree
Showing 32 changed files with 1,046 additions and 322 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Verifier } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/Verifier";
import { ConfigUtil } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/util/ConfigUtil";
import { verifierFactory } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/routes/index";
import { VerifierEventListener } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/LedgerPlugin";
import { IVerifierEventListener } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/LedgerPlugin";

const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
Expand Down Expand Up @@ -32,14 +32,18 @@ export class TestEthereumVerifier {
this.validatorId,
this.appId,
this.monitorOptions,
this.monitorMode
this.monitorMode,
);
}


sendAsyncRequest(contract: object, method: { type: string; command: string; }, args: object): Promise<any> {
sendAsyncRequest(
contract: object,
method: { type: string; command: string },
args: object,
): Promise<any> {
return new Promise((resolve, reject) => {
this.verifierEthereum.sendAsyncRequest(contract, method, args)
this.verifierEthereum
.sendAsyncRequest(contract, method, args)
.then(() => {
logger.debug(`Successfully sent async request to: ${method.command}`);
resolve(true);
Expand All @@ -51,7 +55,11 @@ export class TestEthereumVerifier {
});
}

sendSyncRequest(contract: object, method: { type: string; command: string; }, args: object): Promise<any> {
sendSyncRequest(
contract: object,
method: { type: string; command: string },
args: object,
): Promise<any> {
return new Promise((resolve, reject) => {
this.verifierEthereum
.sendSyncRequest(contract, method, args)
Expand All @@ -60,12 +68,12 @@ export class TestEthereumVerifier {
if (method.command === "getBalance") {
response = {
status: result.status,
amount: parseFloat(result.data)
amount: parseFloat(result.data),
};
} else {
response = {
status: result.status,
data: result.data
data: result.data,
};
}
resolve(response);
Expand All @@ -78,7 +86,6 @@ export class TestEthereumVerifier {
}

getBalance(account: string, requestType: string): any {

this.createVerifierWithoutMonitoring();

const contract = {};
Expand All @@ -93,8 +100,12 @@ export class TestEthereumVerifier {
}
}

transferAsset(srcAccount: string, destAccount: string, amount: string, requestType: string) {

transferAsset(
srcAccount: string,
destAccount: string,
amount: string,
requestType: string,
) {
this.createVerifierWithoutMonitoring();

const contract = {};
Expand All @@ -119,28 +130,27 @@ export class TestEthereumVerifier {
}

stopMonitor() {

logger.debug(`StartingMonitor`);

this.createVerifierWithoutMonitoring();
logger.debug(`Stopping Monitor`);
const blpMonitorModuleName = "BusinessLogicCheckEthereumValidator"
const blpMonitorModuleName = "BusinessLogicCheckEthereumValidator";

this.verifierEthereum.stopMonitor(blpMonitorModuleName)
this.verifierEthereum.stopMonitor(blpMonitorModuleName);
}

startMonitor() {

logger.debug(`StartingMonitor`);

const blpMonitorModuleName = "BusinessLogicCheckEthereumValidator";
const verifier = verifierFactory.getVerifier(
this.validatorId,
blpMonitorModuleName,
this.monitorOptions,
true)
let eventListener: VerifierEventListener
true,
);
let eventListener: IVerifierEventListener;

verifier.startMonitor(blpMonitorModuleName, {}, eventListener)
verifier.startMonitor(blpMonitorModuleName, {}, eventListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export {
SocketLedgerEvent,
SocketIOApiClientOptions,
} from "./socketio-api-client";
export { Verifier, VerifierEventListener } from "./verifier";
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
const defaultMaxCounterRequestID = 100;
const defaultSyncFunctionTimeoutMillisecond = 5 * 1000; // 5 seconds

import { Logger, Checks } from "@hyperledger/cactus-common";
import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common";
import {
Logger,
Checks,
LogLevelDesc,
LoggerProvider,
} from "@hyperledger/cactus-common";
import { ISocketApiClient } from "@hyperledger/cactus-core-api";

import { Socket, SocketOptions, ManagerOptions, io } from "socket.io-client";
Expand Down Expand Up @@ -77,11 +81,10 @@ export type SocketIOApiClientOptions = {
/**
* Type of the message emitted from ledger monitoring.
*/
export class SocketLedgerEvent {
id = "";
verifierId = "";
data: Record<string, unknown> | null = null;
}
export type SocketLedgerEvent = {
status: number;
blockData: [Record<string, unknown>];
};

/**
* Client for sending requests to some socketio ledger connectors (validators) using socketio protocol.
Expand Down Expand Up @@ -330,13 +333,9 @@ export class SocketIOApiClient implements ISocketApiClient<SocketLedgerEvent> {
status: res.status,
blockData: decodedData.blockData,
};
this.log.debug("resultObj =", resultObj);
const event = new SocketLedgerEvent();
event.verifierId = this.options.validatorID;
this.log.debug(`##event.verifierId: ${event.verifierId}`);
event.data = resultObj;
this.log.debug("resultObj=", resultObj);
if (this.monitorSubject) {
this.monitorSubject.next(event);
this.monitorSubject.next(resultObj);
}
})
.catch((err) => {
Expand Down Expand Up @@ -392,4 +391,11 @@ export class SocketIOApiClient implements ISocketApiClient<SocketLedgerEvent> {
}
return `${this.options.validatorID}_${this.counterReqID++}`;
}

/**
* Closes internal socket.io connection to the validator.
*/
public close(): void {
this.socket.close();
}
}
123 changes: 0 additions & 123 deletions packages/cactus-api-client/src/main/typescript/verifier.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -533,21 +533,9 @@ describe("SocketIOApiClient Tests", function () {
// Receive events from the validator
sut.watchBlocksV1(options).subscribe({
next(ev: SocketLedgerEvent) {
expect(ev.id).toEqual("");
expect(ev.verifierId).toEqual(defaultConfigOptions.validatorID);

if (!ev.data) {
done("Event data is empty or null!");
} else {
expect((ev.data as { [key: string]: number })["status"]).toEqual(
eventStatus,
);
expect((ev.data as { [key: string]: string })["blockData"]).toEqual(
decryptedBlockData,
);

done();
}
expect(ev.status).toEqual(eventStatus);
expect(ev.blockData).toEqual(decryptedBlockData);
done();
},
error(err) {
done(err);
Expand Down Expand Up @@ -623,7 +611,7 @@ describe("SocketIOApiClient Tests", function () {
(resolve, reject) => {
blockObservable.subscribe({
next(ev: SocketLedgerEvent) {
if (!ev.data) {
if (!ev.blockData) {
reject("First event data is empty or null!"); // todo - test negative
} else {
log.info("First observer received event:", ev);
Expand All @@ -642,7 +630,7 @@ describe("SocketIOApiClient Tests", function () {
(resolve, reject) => {
blockObservable.subscribe({
next(ev: SocketLedgerEvent) {
if (!ev.data) {
if (!ev.blockData) {
reject("Second event data is empty or null!");
} else {
log.info("Second observer received event:", ev);
Expand Down Expand Up @@ -684,7 +672,7 @@ describe("SocketIOApiClient Tests", function () {
(resolve, reject) => {
const sub = sut.watchBlocksV1(options).subscribe({
next(ev: SocketLedgerEvent) {
if (!ev.data) {
if (!ev.blockData) {
sub.unsubscribe();
reject("First event data is empty or null!"); // todo - test negative
} else {
Expand All @@ -705,7 +693,7 @@ describe("SocketIOApiClient Tests", function () {
(resolve, reject) => {
const sub = sut.watchBlocksV1(options).subscribe({
next(ev: SocketLedgerEvent) {
if (!ev.data) {
if (!ev.blockData) {
sub.unsubscribe();
reject("2nd event data is empty or null!"); // todo - test negative
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-cmd-socketio-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@hyperledger/cactus-common": "1.0.0-rc.3",
"@hyperledger/cactus-core-api": "1.0.0-rc.3",
"@types/node": "^14.0.24",
"body-parser": "^1.19.0",
"cookie-parser": "1.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Request } from "express";
import { BusinessLogicPlugin } from "./BusinessLogicPlugin";
import { VerifierEventListener, LedgerEvent } from "../verifier/LedgerPlugin";
import { LedgerEvent } from "../verifier/LedgerPlugin";
import { json2str } from "../verifier/DriverCommon";
import { ConfigUtil } from "../routing-interface/util/ConfigUtil";

Expand Down
Loading

0 comments on commit 2b57f69

Please sign in to comment.