Skip to content

Commit

Permalink
feat(cmd-server-socket): add a communication test to open-api validators
Browse files Browse the repository at this point in the history
Signed-off-by: Takuma TAKEUCHI <takeuchi.takuma@fujitsu.com>
  • Loading branch information
takeutak authored and petermetz committed Oct 7, 2021
1 parent 6bad29b commit c1fe6a0
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 196 deletions.
238 changes: 129 additions & 109 deletions examples/test-run-transaction/BusinessLogicRunTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,123 +5,143 @@
* BusinessLogicRunTransaction.ts
*/

import { Request } from 'express';
import { RequestInfo } from './RequestInfo';
import { Request } from "express";
import { RequestInfo } from "./RequestInfo";
//import { MeterManagement } from './MeterManagement';
//import { MeterInfo } from './MeterInfo';
import { TradeInfo } from '../../packages/routing-interface/TradeInfo';
import { transactionManagement } from '../../packages/routing-interface/routes/index';
import { verifierFactory } from '../../packages/routing-interface/routes/index';
import { BusinessLogicBase } from '../../packages/business-logic-plugin/BusinessLogicBase';
import { TradeInfo } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/TradeInfo";
import { transactionManagement } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/routes/index";
import { verifierFactory } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/routes/index";
import { BusinessLogicBase } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/business-logic-plugin/BusinessLogicBase";
//import { makeRawTransaction } from './TransactionEthereum'
import { LedgerEvent } from '../../packages/ledger-plugin/LedgerPlugin';
import { json2str } from '../../packages/ledger-plugin/DriverCommon'

const fs = require('fs');
const path = require('path');
const config: any = JSON.parse(fs.readFileSync(path.resolve(__dirname, "./config/default.json"), 'utf8'));
import { LedgerEvent } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/LedgerPlugin";
import { json2str } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/DriverCommon";

const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
//const config: any = JSON.parse(fs.readFileSync("/etc/cactus/default.json", 'utf8'));
const config: any = yaml.safeLoad(
fs.readFileSync("/etc/cactus/default.yaml", "utf8")
);
import { getLogger } from "log4js";
const moduleName = 'BusinessLogicRunTransaction';
const moduleName = "BusinessLogicRunTransaction";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class BusinessLogicRunTransaction extends BusinessLogicBase {
businessLogicID: string;

constructor(businessLogicID: string) {
super();
this.businessLogicID = businessLogicID;
}

startTransaction(req: Request, businessLogicID: string, tradeID: string) {

logger.debug("called startTransaction()");

// set RequestInfo
const requestInfo: RequestInfo = new RequestInfo();
requestInfo.setBusinessLogicID(businessLogicID);
requestInfo.keychainId = req.body.tradeParams[0];
requestInfo.keychainRef = req.body.tradeParams[1];
requestInfo.channelName = req.body.tradeParams[2];
requestInfo.invocationType = req.body.tradeParams[3];
requestInfo.functionName = req.body.tradeParams[4];
requestInfo.functionArgs = req.body.tradeParams[5];
logger.debug(`tradeParams: ${req.body.tradeParams}`);

// set TradeID
requestInfo.setTradeID(tradeID);

// Create trade information
const tradeInfo: TradeInfo = new TradeInfo(requestInfo.businessLogicID, requestInfo.tradeID);

// Call Verifier to perform the function
this.execTransaction(requestInfo, tradeInfo);

}


execTransaction(requestInfo: RequestInfo, tradeInfo: TradeInfo) {

logger.debug("called execTransaction()");

const useValidator = JSON.parse(transactionManagement.getValidatorToUse(tradeInfo.businessLogicID));

// TODO: Temporarily specify no monitoring required (# 3rd parameter = false)
const verifier = verifierFactory.getVerifier(useValidator['validatorID'][0], {}, false);
logger.debug("getVerifier");

// TODO: for Temporarily specify no monitoring required (# 3rd parameter = false)
verifier.setEventListener(transactionManagement);

const contract = {};
const method = {command: "test-run-transaction"};
const args = {"args": {
"keychainId": requestInfo.keychainId,
"keychainRef": requestInfo.keychainRef,
"channelName": requestInfo.channelName,
"invocationType": requestInfo.invocationType,
"functionName": requestInfo.functionName,
"functionArgs": requestInfo.functionArgs
}};

logger.debug(`##before call verifier.requestLedgerOperationHttp()`);
verifier.requestLedgerOperationHttp(contract, method, args);
logger.debug(`##after call verifier.requestLedgerOperationHttp()`);

}


onEvent(ledgerEvent: LedgerEvent, targetIndex: number): void {
logger.debug(`##in BLP:onEvent()`);
logger.debug(`##onEvent(): ${json2str(ledgerEvent)}`);
businessLogicID: string;

constructor(businessLogicID: string) {
super();
this.businessLogicID = businessLogicID;
}

startTransaction(req: Request, businessLogicID: string, tradeID: string) {
logger.debug("called startTransaction()");

// set RequestInfo
const requestInfo: RequestInfo = new RequestInfo();
requestInfo.setBusinessLogicID(businessLogicID);
requestInfo.keychainId = req.body.tradeParams[0];
requestInfo.keychainRef = req.body.tradeParams[1];
requestInfo.channelName = req.body.tradeParams[2];
requestInfo.invocationType = req.body.tradeParams[3];
requestInfo.functionName = req.body.tradeParams[4];
requestInfo.functionArgs = req.body.tradeParams[5];
logger.debug(`tradeParams: ${req.body.tradeParams}`);

// set TradeID
requestInfo.setTradeID(tradeID);

// Create trade information
const tradeInfo: TradeInfo = new TradeInfo(
requestInfo.businessLogicID,
requestInfo.tradeID
);

// Call Verifier to perform the function
this.execTransaction(requestInfo, tradeInfo);
}

execTransaction(requestInfo: RequestInfo, tradeInfo: TradeInfo) {
logger.debug("called execTransaction()");

const useValidator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID)
);

// TODO: Temporarily specify no monitoring required (# 4rd parameter = false)
const verifier = verifierFactory.getVerifier(
useValidator["validatorID"][0],
"BusinessLogicRunTransaction",
{},
false
);
logger.debug("getVerifier");

const contract = {};
const method = { command: "test-run-transaction" };
const args = {
args: {
keychainId: requestInfo.keychainId,
keychainRef: requestInfo.keychainRef,
channelName: requestInfo.channelName,
invocationType: requestInfo.invocationType,
functionName: requestInfo.functionName,
functionArgs: requestInfo.functionArgs,
},
};

logger.debug(`##execTransaction call verifier.sendAsyncRequest()`);
verifier
.sendAsyncRequest(contract, method, args)
.then(() => {
logger.debug(`##execTransaction sendAsyncRequest finish`);
})
.catch((err) => {
logger.error(err);
});
}

onEvent(ledgerEvent: LedgerEvent, targetIndex: number): void {
logger.debug(`##in BLP:onEvent()`);
logger.debug(`##onEvent(): ${json2str(ledgerEvent)}`);
}

getEventDataNum(ledgerEvent: LedgerEvent): number {
// NOTE: This method implements the BisinessLogcPlugin operation(* Override by subclass)
// TODO:
logger.debug(
`##in getEventDataNum(), ledgerEvent: ${JSON.stringify(ledgerEvent)}`
);
const retEventNum = ledgerEvent.data["blockData"].length;
logger.debug(`##retEventNum: ${retEventNum}`);
return retEventNum;
}

getTxIDFromEvent(
ledgerEvent: LedgerEvent,
targetIndex: number
): string | null {
// NOTE: This method implements the BisinessLogcPlugin operation(* Override by subclass)
// TODO:
logger.debug(
`##in getTxIDFromEvent(), ledgerEvent: ${JSON.stringify(ledgerEvent)}`
);

const txId = ledgerEvent.data["txId"];

if (typeof txId !== "string") {
logger.warn(
`#getTxIDFromEvent(): skip(invalid block, not found txId.), event: ${json2str(
ledgerEvent
)}`
);
return null;
}


getEventDataNum(ledgerEvent: LedgerEvent): number {
// NOTE: This method implements the BisinessLogcPlugin operation(* Override by subclass)
// TODO:
logger.debug(`##in getEventDataNum(), ledgerEvent: ${JSON.stringify(ledgerEvent)}`);
const retEventNum = ledgerEvent.data['blockData'].length;
logger.debug(`##retEventNum: ${retEventNum}`);
return retEventNum;
}


getTxIDFromEvent(ledgerEvent: LedgerEvent, targetIndex: number): string | null {
// NOTE: This method implements the BisinessLogcPlugin operation(* Override by subclass)
// TODO:
logger.debug(`##in getTxIDFromEvent(), ledgerEvent: ${JSON.stringify(ledgerEvent)}`);

const txId = ledgerEvent.data['txId'];

if (typeof txId !== 'string') {
logger.warn(`#getTxIDFromEvent(): skip(invalid block, not found txId.), event: ${json2str(ledgerEvent)}`);
return null;
}

logger.debug(`###getTxIDFromEvent(): txId: ${txId}`);
return txId;
}


logger.debug(`###getTxIDFromEvent(): txId: ${txId}`);
return txId;
}
}
29 changes: 14 additions & 15 deletions examples/test-run-transaction/RequestInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@

// request information
export class RequestInfo {
businessLogicID: string;
tradeID: string;
businessLogicID: string;
tradeID: string;

keychainId: string;
keychainRef: string;
channelName: string;
invocationType: string;
functionName: string;
functionArgs: Array<any>;
keychainId: string;
keychainRef: string;
channelName: string;
invocationType: string;
functionName: string;
functionArgs: Array<any>;

setBusinessLogicID(businessLogicID: string) {
this.businessLogicID = businessLogicID;
}
setBusinessLogicID(businessLogicID: string) {
this.businessLogicID = businessLogicID;
}

setTradeID(tradeID: string) {
this.tradeID = tradeID;
}
setTradeID(tradeID: string) {
this.tradeID = tradeID;
}
}

26 changes: 14 additions & 12 deletions examples/test-run-transaction/config/BLP_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
* BLP_config.ts
*/

import { BusinessLogicPlugin } from '../../../packages/business-logic-plugin/BusinessLogicPlugin';
import { BusinessLogicRunTransaction } from '../BusinessLogicRunTransaction';
// import { BusinessLogicCartrade } from '../examples/cartrade/BusinessLogicXxxxTrade';
import { BusinessLogicPlugin } from "../../../packages/cactus-cmd-socketio-server/src/main/typescript/business-logic-plugin/BusinessLogicPlugin";
import { BusinessLogicRunTransaction } from "../BusinessLogicRunTransaction";
// import { BusinessLogicCartrade } from '../examples/cartrade/BusinessLogic***Trade';

export function getTargetBLPInstance(businessLogicID: string): BusinessLogicPlugin | null {
switch (businessLogicID) {
case "j71S9gLN":
return new BusinessLogicRunTransaction(businessLogicID);
// case "xxxxxxxx":
// return new BusinessLogicXxxxTrade();
default:
return null;
}
export function getTargetBLPInstance(
businessLogicID: string
): BusinessLogicPlugin | null {
switch (businessLogicID) {
case "j71S9gLN":
return new BusinessLogicRunTransaction(businessLogicID);
// case "*******":
// return new BusinessLogicX***Trade();
default:
return null;
}
}
5 changes: 5 additions & 0 deletions examples/test-run-transaction/config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
runTransactionInfo:
connector:
validatorID: vIdO32CF

logLevel: debug
2 changes: 1 addition & 1 deletion examples/test-run-transaction/config/usersetting.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"logLevel": "debug",
"applicationHostInfo": {
"hostName": "http://xxx.xxxxx.xxx:xxxx",
"hostName": "http://aaa.bbb.ccc.ddd",
"hostPort": 5034
},
"appRouters": [
Expand Down
15 changes: 15 additions & 0 deletions examples/test-run-transaction/config/usersetting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
blpRegistry:
-
businessLogicID: j71S9gLN
validatorID: [vIdO32CF]

logLevel: debug

applicationHostInfo:
hostName: http://aaa.bbb.ccc.ddd
hostPort: 5034

appRouters:
-
path: /api/v1/bl/test-run-transaction/
routerJs: ../../../../../../examples/test-run-transaction/test-run-transaction.js
7 changes: 5 additions & 2 deletions examples/test-run-transaction/copyBLPConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* copyBLPConfig.ts
*/

import * as shell from 'shelljs';
import * as shell from "shelljs";

// NOTE: Copy the static assets to the dist folder.
// Example:
// shell.cp('-R', 'src/routing-interface/views', 'dist/routing-interface/views/');
shell.cp('../../dist/examples/test-run-transaction/config/BLP_config.js', '../../dist/packages/config/');
shell.cp(
"../../dist/examples/test-run-transaction/config/BLP_config.js",
"../../dist/packages/cactus-cmd-socketio-server/src/main/typescript/business-logic-plugin/"
);
6 changes: 3 additions & 3 deletions examples/test-run-transaction/copyStaticAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* copyStaticAssets.ts
*/

import * as shell from 'shelljs';
import * as shell from "shelljs";

// NOTE: Copy the static assets to the dist folder.
// Example:
// shell.cp('-R', 'src/routing-interface/views', 'dist/routing-interface/views/');
shell.cp('-R', 'config/default.json', '../../dist/examples/test-run-transaction/config/');
shell.cp('-R', 'config/usersetting.json', '../../dist/packages/config/');
shell.cp("-R", "config/default.yaml", "/etc/cactus/");
shell.cp("-R", "config/usersetting.yaml", "/etc/cactus/");

0 comments on commit c1fe6a0

Please sign in to comment.