Skip to content

Commit

Permalink
refactor(cmd-socketio-server): circular dependency fix and minor fixe…
Browse files Browse the repository at this point in the history
…s for verifier tests

Fix circular dependency between routing-interface and VerifierFactory,
add default validator config used by ValidatorAuthentication, type fixes
in Verifier, format some files according to standard.

Closes: #1596
Signed-off-by: Michal Bajer <[email protected]>
  • Loading branch information
outSH authored and takeutak committed Dec 1, 2021
1 parent 1dedc33 commit 1c800a8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* NOTE!
* This configuration file is overwritten in the validator.
* Used by ../verifier/ValidatorAuthentication.ts
*/

export const config = {
logLevel: "silent",
validatorKeyPath: "key.pem",
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const moduleName = "TransactionManagement";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class TransactionManagement {
export class TransactionManagement implements VerifierEventListener {
private blpRegistry: BLPRegistry = null; // Verifier information used in business logic
// private connectInfo: LPInfoHolder = null; // connection information
// private verifierArray: [] = []; // Verifier
Expand Down Expand Up @@ -56,7 +56,7 @@ export class TransactionManagement {
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`,
);
return;
}
Expand Down Expand Up @@ -95,15 +95,15 @@ export class TransactionManagement {
const businessLogicID = this.getBusinessLoginIDByTradeID(tradeID);
if (businessLogicID === null) {
logger.warn(
`##getOperationStatus(): not found BusinessLogicPlugin. tradeID: ${tradeID}`
`##getOperationStatus(): not found BusinessLogicPlugin. tradeID: ${tradeID}`,
);
return;
}

const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`##getOperationStatus(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`
`##getOperationStatus(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`,
);
return;
}
Expand All @@ -125,7 +125,7 @@ export class TransactionManagement {
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`,
);
return;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ export class TransactionManagement {
const eventNum = this.getEventNum(ledgerEvent);
if (eventNum === 0) {
logger.warn(
`onEvent(): invalid event, event num is zero., ledgerEvent.verifierId: ${ledgerEvent.verifierId}`
`onEvent(): invalid event, event num is zero., ledgerEvent.verifierId: ${ledgerEvent.verifierId}`,
);
return;
}
Expand All @@ -195,30 +195,30 @@ export class TransactionManagement {
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`getEventNum(): not found, businessLogicID: ${businessLogicID}`
`getEventNum(): not found, businessLogicID: ${businessLogicID}`,
);
continue;
}

const eventNum = blp.getEventDataNum(ledgerEvent);
if (eventNum !== 0) {
logger.debug(
`##getEventNum: target businessLogicID: ${businessLogicID}`
`##getEventNum: target businessLogicID: ${businessLogicID}`,
);
return eventNum;
}
}

// not found.
logger.warn(
`getEventNum(): not found(The corresponding BLP does not exist.)`
`getEventNum(): not found(The corresponding BLP does not exist.)`,
);
return 0;
}

private getBLPInstanceFromEvent(
ledgerEvent: LedgerEvent,
targetIndex: number
targetIndex: number,
): BusinessLogicPlugin | null {
const txID = this.getTxIDFromEvent(ledgerEvent, targetIndex);
if (txID === null) {
Expand All @@ -232,17 +232,17 @@ export class TransactionManagement {

private getTxIDFromEvent(
ledgerEvent: LedgerEvent,
targetIndex: number
targetIndex: number,
): string | null {
// logger.debug(`##getTxIDFromEvent: event: ${ledgerEvent}`);
for (const businessLogicID of this.blpRegistry.getBusinessLogicIDList()) {
logger.debug(
`####getTxIDFromEvent(): businessLogicID: ${businessLogicID}`
`####getTxIDFromEvent(): businessLogicID: ${businessLogicID}`,
);
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`getTxIDFromEvent(): not found, businessLogicID: ${businessLogicID}`
`getTxIDFromEvent(): not found, businessLogicID: ${businessLogicID}`,
);
continue;
}
Expand All @@ -256,7 +256,7 @@ export class TransactionManagement {

// not found.
logger.warn(
`getTxIDFromEvent(): not found(The corresponding BLP does not exist.)`
`getTxIDFromEvent(): not found(The corresponding BLP does not exist.)`,
);
return null;
}
Expand All @@ -265,27 +265,27 @@ export class TransactionManagement {
logger.debug(`##getBLPInstanceFromTxID: txID: ${txID}`);
for (const businessLogicID of this.blpRegistry.getBusinessLogicIDList()) {
logger.debug(
`####getBLPInstanceFromTxID(): businessLogicID: ${businessLogicID}`
`####getBLPInstanceFromTxID(): businessLogicID: ${businessLogicID}`,
);
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`getBLPInstanceFromTxID(): not found, businessLogicID: ${businessLogicID}`
`getBLPInstanceFromTxID(): not found, businessLogicID: ${businessLogicID}`,
);
continue;
}

// if (blp.hasTxIDInTransactions(txID)) {
logger.debug(
`####getBLPInstanceFromTxID(): found!, businessLogicID: ${businessLogicID}`
`####getBLPInstanceFromTxID(): found!, businessLogicID: ${businessLogicID}`,
);
return blp;
// }
}

// not found.
logger.warn(
`getBLPInstanceFromTxID(): not found(The corresponding BLP does not exist.)`
`getBLPInstanceFromTxID(): not found(The corresponding BLP does not exist.)`,
);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { VerifierFactory } from "../../verifier/VerifierFactory";
const router: Router = Router();
export const transactionManagement: TransactionManagement =
new TransactionManagement();
export const verifierFactory: VerifierFactory = new VerifierFactory();
export const verifierFactory: VerifierFactory = new VerifierFactory(
transactionManagement,
);

/* GET home page. */
router.get("/", (req: Request, res: Response, next: NextFunction) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,4 @@ export function json2str(jsonObj: object) {
logger.warn("invalid json format.");
return null;
}
}

// Validator test program.(socket.io client)

export function makeApiInfoList(targetApiInfo: any): ApiInfo[] {
const retApiInfoList: ApiInfo[] = [];
for (const item of targetApiInfo) {
const apiInfo: ApiInfo = new ApiInfo();
apiInfo.apiType = item.apiType;
for (const reqDataItem of item.requestedData) {
const reqData = new RequestedData();
reqData.dataName = reqDataItem.dataName;
reqData.dataType = reqDataItem.dataType;
apiInfo.requestedData.push(reqData);
}
retApiInfoList.push(apiInfo);
}
return retApiInfoList;
}

// store on socket
const socketArray: any[] = [];

// Returns the index of socketArray as a return value
export function addSocket(socket: any): number {
// TODO:
const index = socketArray.push(socket) - 1;
logger.debug(`##addSocket, index = ${index}`);
return index;
}

export function getStoredSocket(index: number): any {
logger.debug(`##getSocket, index = ${index}`);
return socketArray[index];
}

export function deleteAndDisconnectSocke(index: number) {
try {
logger.debug(`##deleteAndDisconnectSocke, index = ${index}`);
if (socketArray.length > index) {
const socket = socketArray[index];
if (socket.connected) {
logger.debug(`##call disconnect, index = ${index}`);
socket.disconnect();
} else {
logger.debug(`##already disconnected, index = ${index}`);
}
}
} catch (err) {
logger.warn(`##error:deleteAndDisconnectSocke, index = ${index}, ${err}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ import {
LedgerEvent,
VerifierEventListener,
} from "./LedgerPlugin";
import { makeApiInfoList } from "./DriverCommon";
import {
json2str,
addSocket,
getStoredSocket,
deleteAndDisconnectSocke,
} from "./DriverCommon";
import { json2str } from "./DriverCommon";
import { LedgerOperation } from "../business-logic-plugin/LedgerOperation";
import { LedgerPluginInfo } from "./validator-registry";
import { ConfigUtil } from "../routing-interface/util/ConfigUtil";
import { VerifierAuthentication } from "./VerifierAuthentication";
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
Expand All @@ -44,20 +39,12 @@ type VALIDATOR_TYPE = typeof VALIDATOR_TYPE[keyof typeof VALIDATOR_TYPE]; // 'so
// abolish template-trade
// const validatorRregistryConf: any = yaml.safeLoad(fs.readFileSync("/etc/cactus/validator-registry.yaml", 'utf8'));

type TLedgerInfo = {
validatorID: string;
validatorType: string;
validatorURL: string;
validatorKeyPath: string;
apiInfo: object;
};

export class Verifier implements IVerifier {
validatorID = "";
validatorType = "";
validatorUrl = "";
validatorKeyPath = "";
apiInfo: object = {};
apiInfo: Array<ApiInfo> = [];
counterReqID = 1;
eventListenerHash: { [key: string]: VerifierEventListener } = {}; // Listeners for events from Ledger
static mapUrlSocket: Map<string, SocketIOClient.Socket> = new Map();
Expand All @@ -66,7 +53,7 @@ export class Verifier implements IVerifier {

constructor(ledgerInfo: string) {
// TODO: Configure the Verifier based on the connection information
const ledgerInfoObj = JSON.parse(ledgerInfo) as TLedgerInfo;
const ledgerInfoObj = JSON.parse(ledgerInfo) as LedgerPluginInfo;
this.validatorID = ledgerInfoObj.validatorID;
this.validatorType = ledgerInfoObj.validatorType;
this.validatorUrl = ledgerInfoObj.validatorURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
* VerifierFactory.ts
*/

import { transactionManagement } from "../routing-interface/routes/index";
import { Verifier } from "./Verifier";
import { LPInfoHolder } from "../routing-interface/util/LPInfoHolder";
import { ConfigUtil } from "../routing-interface/util/ConfigUtil";

const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
import { VerifierEventListener } from "./LedgerPlugin";
const moduleName = "VerifierFactory";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class VerifierFactory {
private connectInfo: LPInfoHolder = null; // connection information
static verifierHash: { [key: string]: Verifier } = {}; // Verifier

constructor() {
this.connectInfo = new LPInfoHolder();
}
constructor(
private eventListener: VerifierEventListener,
private connectInfo = new LPInfoHolder(),
) {}

// Get Verifier
getVerifier(
validatorId: string,
appId = "",
monitorOptions: {} = {},
monitorMode = true
monitorMode = true,
): Verifier {
// Return Verifier
// If you have already made it, please reply. If you haven't made it yet, make it and reply.
Expand All @@ -40,7 +40,7 @@ export class VerifierFactory {
this.connectInfo.getLegerPluginInfo(validatorId);
// TODO: I want to manage an instance using the validatorId as a key instead of a dedicated member variable
VerifierFactory.verifierHash[validatorId] = new Verifier(
ledgerPluginInfo
ledgerPluginInfo,
);
logger.debug("##startMonitor");
logger.debug(`##getVerifier validatorId :${validatorId}`);
Expand All @@ -52,7 +52,7 @@ export class VerifierFactory {
VerifierFactory.verifierHash[validatorId].startMonitor(
appId,
monitorOptions,
transactionManagement
this.eventListener,
);
}
return VerifierFactory.verifierHash[validatorId];
Expand Down

0 comments on commit 1c800a8

Please sign in to comment.