Skip to content

Commit

Permalink
test: enhancement to check connection eth validator blp
Browse files Browse the repository at this point in the history
- Add tests for sync (read and write) requests to validator.
- Add tests for async (read and write) requests to validator.
- Add handling for Monitor Mode in blp.
- Add tests for config files.
- Add automatization tools for tests and setup of blp.
- Updated readme.

Depends on hyperledger#1629

Signed-off-by: stepniowskip <piotr.stepniowski@fujitsu.com>
  • Loading branch information
stepniowskip authored and petermetz committed Feb 24, 2022
1 parent 60ee7ba commit 0ccf1ca
Show file tree
Hide file tree
Showing 11 changed files with 1,201 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { transactionManagement } from "../../packages/cactus-cmd-socketio-server
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 { Verifier } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/Verifier";
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 yaml = require("js-yaml");
Expand All @@ -29,7 +31,7 @@ export class BusinessLogicCheckEthereumValidator extends BusinessLogicBase {
}

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

const requestInfo: RequestInfo = new RequestInfo();
requestInfo.setBusinessLogicID(businessLogicID);
Expand All @@ -40,50 +42,176 @@ export class BusinessLogicCheckEthereumValidator extends BusinessLogicBase {
requestInfo.tradeID,
);

this.executeTransaction(tradeInfo);
this.startMonitor(tradeInfo);
}

executeTransaction(tradeInfo: TradeInfo) {
startMonitor(tradeInfo: TradeInfo) {
logger.debug(
`##startMonitor(): businessLogicID: ${tradeInfo.businessLogicID}`,
);

const validator = this.getValidator(tradeInfo);

if (config.monitorMode === true) {
logger.debug(`##startMonitor(): Creating validator with Monitor Mode`);
} else {
logger.debug(`##startMonitor(): Creating validator without Monitor Mode`)
}

const verifier = this.getVerifier(validator);

this.sendTestRequest(verifier);
}

private getValidator(tradeInfo: TradeInfo) {
logger.debug(`##getValidator(): Getting validator with tradeInfo: ${tradeInfo}`);
const validator = JSON.parse(
transactionManagement.getValidatorToUse(tradeInfo.businessLogicID),
);
const ethereumValidator = validator["validatorID"][0];
const ethValidator = validator["validatorID"][0];
return ethValidator;
}

private getVerifier(validator: any) {
logger.debug(`getVerifier(): Getting Verifier with validator: ${validator}`);

const verifier = verifierFactory.getVerifier(
ethereumValidator,
validator,
moduleName,
{},
false,
config.monitorMode
);

this.sendRequest(verifier);
return verifier;
}

sendRequest(verifier: Verifier) {
stopMonitor(tradeInfo: TradeInfo) {
logger.debug(`##calling stopMonitor()`);

// get validator
const validator = this.getValidator(tradeInfo);
// get verifier
const verifier = this.getVerifier(validator);
// stop monitoring
verifier.stopMonitor(moduleName);
}

sendTestRequest(verifier: Verifier) {
const contract = {};
const method = { command: "getBalance", type: "web3Eth" };
const accountEthereum = "06fc56347d91c6ad2dae0c3ba38eb12ab0d72e97";
const args = { args: [accountEthereum] };

logger.debug(`##sendRequest(): calling verifier.sendSyncRequest()`);
logger.debug(`##sendTestRequest(): calling verifier.sendSyncRequest()`);
verifier
.sendSyncRequest(contract, method, args)
.then((response) => {
logger.debug(
`got response: \n status code: ${response.status} \n Ethereum balance: ${response.data}`,
);
if (response.status == 504 && response.data === undefined) {
logger.error(`Could not establish connection to ethereum ledger`);
logger.error(`##sendTestRequest(): Could not establish connection to ethereum ledger`);
} else {
logger.info(`Successfully connected to ethereum ledger`);
logger.info(`##sendTestRequest(): Successfully connected to ethereum ledger`);
}
return { status: response.status, data: response.data };
})
.catch((err) => {
logger.error(err);
});
}

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

const tx = this.getTransactionFromEvent(ledgerEvent, targetIndex);
if (tx == null) {
logger.error(`##onEvent(): invalid event: ${json2str(ledgerEvent)}`);
return;
}

try {
const txId = tx["hash"];
const status = ledgerEvent.data["status"];
logger.debug(`##onEvent(): txId = ${txId}`);
logger.debug(`##onEvent(): status = ${status}`);

if (status !== 200) {
logger.error(
`##onEvent(): error event, status: ${status}, txId: ${txId}`
);
return;
}
} catch (err) {
logger.error(`##onEvent(): err: ${err}, event: ${json2str(ledgerEvent)}`);
}
}

getTxIDFromEvent(event: LedgerEvent, targetIndex: number): string | null {
const tx = this.getTransactionFromEvent(event, targetIndex);
console.log(tx)
if (tx == null) {
logger.warn(`##getTxIDFromEvent(): skip(not found tx)`);
return null;
}

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

logger.debug(`##getTxIDFromEvent(): txId: ${txId}`);
return txId;
} catch (err) {
logger.error(
`##getTxIDFromEvent(): err: ${err}, event: ${json2str(event)}`
);
return null;
}
}

getEventDataNum(ledgerEvent: LedgerEvent): number {
logger.debug(
`##getEventDataNum(), ledgerEvent.verifierId: ${ledgerEvent.verifierId}`
);
const event = ledgerEvent.data;
let retEventNum = 0;

try {
const validatorID = config.checkEthereumValidator.connector.validatorID;

retEventNum = event["blockData"]["transactions"].length;
logger.debug(
`##getEventDataNum(): retEventNum: ${retEventNum}, verifierId: ${ledgerEvent.verifierId}`
);
return retEventNum;
} catch (err) {
logger.error(
`##getEventDataNum(): invalid even, err: ${err}, event: ${event}`
);
return 0;
}
}

getTransactionFromEvent(
event: LedgerEvent,
targetIndex: number
): object | null {
try {
const retTransaction = event.data["blockData"]["transactions"][targetIndex];
logger.debug(
`##getTransactionFromEvent(), retTransaction: ${retTransaction}`
);
console.log(retTransaction)
return retTransaction;
} catch (err) {
logger.error(
`##getTransactionFromEvent(): invalid even, err:${err}, event:${event}`
);
}
}
}

Loading

0 comments on commit 0ccf1ca

Please sign in to comment.