Skip to content

Commit

Permalink
refactor: fixes for iroha pr
Browse files Browse the repository at this point in the history
fixes for iroha pr hyperledger-cacti#1966

Signed-off-by: stepniowskip <[email protected]>
  • Loading branch information
stepniowskip committed May 9, 2022
1 parent 76edfcc commit ce975bb
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import type { Observable } from "rxjs";
export interface ISocketApiClient<BlockType> {
sendAsyncRequest?(
args: any,
method?: Record<string, unknown>,
methodName?: string,
method: Record<string, unknown>,
baseConfig?: any,
contract?: Record<string, unknown>,
): void;

sendSyncRequest?(
args: any,
method?: Record<string, unknown>,
methodName?: string,
method: Record<string, unknown>,
baseConfig?: any,
contract?: Record<string, unknown>,
): Promise<any>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@
"description": "Can only be set to false for an insecure grpc connection."
},
"monitorMode": {
"type": "boolean"
"type": "boolean",
"nullable": true,
"description": "Flag used for monitoring. It changes default beahviour of transaction wrapper so it return error to caller instead of throwing RuntimeError straight away."
}
}
},
Expand All @@ -235,7 +237,7 @@
"type": "string",
"nullable": false
},
"IrohaSocketSession": {
"IrohaSocketSessionEvent": {
"type": "string",
"enum": [
"org.hyperledger.cactus.api.async.iroha.SocketSession.Subscribe",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common";
import { Constants, ISocketApiClient } from "@hyperledger/cactus-core-api";
import {
DefaultApi,
IrohaSocketSession,
IrohaSocketSessionEvent,
IrohaBlockProgress,
IrohaBaseConfig,
} from "../generated/openapi/typescript-axios";
Expand Down Expand Up @@ -67,13 +67,13 @@ export class IrohaApiClient
const socket: Socket = io(this.wsApiHost, { path: this.wsApiPath });
const subject = new ReplaySubject<IrohaBlockProgress>(0);
this.log.debug(monitorOptions);
socket.on(IrohaSocketSession.Next, (data: IrohaBlockProgress) => {
socket.on(IrohaSocketSessionEvent.Next, (data: IrohaBlockProgress) => {
subject.next(data);
});

socket.on("connect", () => {
this.log.debug("connected OK...");
socket.emit(IrohaSocketSession.Subscribe, monitorOptions);
socket.emit(IrohaSocketSessionEvent.Subscribe, monitorOptions);
});

socket.connect();
Expand All @@ -93,7 +93,7 @@ export class IrohaApiClient
return subject.pipe(
finalize(() => {
this.log.info("FINALIZE - unsubscribing from the stream...");
socket.emit(IrohaSocketSession.Unsubscribe);
socket.emit(IrohaSocketSessionEvent.Unsubscribe);
socket.disconnect();
}),
share(),
Expand All @@ -103,17 +103,18 @@ export class IrohaApiClient
/**
* Immediately sends request to the validator, doesn't report any error or responses.
* @param args - arguments.
* @param method - function / method to be executed by validator.
* @param baseConfig - baseConfig needed to properly connect to ledger
* @param methodName - function / method to be executed by validator.
*/
public sendAsyncRequest(
args: any,
method: Record<string, unknown>,
baseConfig?: IrohaBaseConfig,
methodName?: string,
): void {
this.log.debug(`inside: sendAsyncRequest()`);
this.log.debug(`baseConfig=${baseConfig}`);
this.log.debug(`methodName=${methodName}`);
this.log.debug(`methodName=${method.methodName}`);
this.log.debug(`args=${args}`);

if (baseConfig === undefined || baseConfig === {}) {
Expand All @@ -131,21 +132,21 @@ export class IrohaApiClient
throw new RuntimeError("Some fields in baseConfig are undefined");
}

if (methodName === undefined || methodName === "") {
if (method.methodName === undefined || method.methodName === "") {
throw new RuntimeError("methodName parameter must be specified");
}

const socket: Socket = io(this.wsApiHost, { path: this.wsApiPath });
const asyncRequestData = {
baseConfig: baseConfig,
methodName: methodName,
methodName: method.methodName,
args: args,
};

this.log.debug("requestData:", asyncRequestData);

try {
socket.emit(IrohaSocketSession.SendAsyncRequest, asyncRequestData);
socket.emit(IrohaSocketSessionEvent.SendAsyncRequest, asyncRequestData);
} catch (err) {
this.log.error("Exception in: sendAsyncRequest(): ", err);
throw err;
Expand All @@ -155,18 +156,18 @@ export class IrohaApiClient
/**
* Sends request to be executed on the ledger, watches and reports any error and the response from a ledger.
* @param args - arguments.
* @param method - function / method to be executed by validator.
* @param baseConfig - baseConfig needed to properly connect to ledger
* @param methodName - function / method to be executed by validator.
* @returns Promise that will resolve with response from the ledger, or reject when error occurred.
*/
public sendSyncRequest(
args: any,
method: Record<string, unknown>,
baseConfig?: IrohaBaseConfig,
methodName?: string,
): Promise<any> {
this.log.debug(`inside: sendSyncRequest()`);
this.log.debug(`baseConfig=${baseConfig}`);
this.log.debug(`method=${methodName}`);
this.log.debug(`method=${method}`);
this.log.debug(`args=${args}`);

if (baseConfig === undefined || baseConfig === {}) {
Expand All @@ -184,7 +185,7 @@ export class IrohaApiClient
throw new RuntimeError("Some fields in baseConfig are undefined");
}

if (methodName === undefined || methodName === "") {
if (method.methodName === undefined || method.methodName === "") {
throw new RuntimeError("methodName parameter must be specified");
}

Expand Down Expand Up @@ -219,14 +220,14 @@ export class IrohaApiClient

const syncRequestData = {
baseConfig: baseConfig,
methodName: methodName,
methodName: method.methodName,
args: args,
};

this.log.debug("requestData:", syncRequestData);

try {
socket.emit(IrohaSocketSession.SendSyncRequest, syncRequestData);
socket.emit(IrohaSocketSessionEvent.SendSyncRequest, syncRequestData);
} catch (err) {
this.log.error("Exception in: sendAsyncRequest(): ", err);
throw err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,87 @@ export interface IrohaBaseConfig {
* @memberof IrohaBaseConfig
*/
tls?: boolean;
/**
* Flag used for monitoring. It changes default beahviour of transaction wrapper so it return error to caller instead of throwing RuntimeError straight away.
* @type {boolean}
* @memberof IrohaBaseConfig
*/
monitorMode?: boolean | null;
}
/**
*
* @export
* @interface IrohaBlockProgress
*/
export interface IrohaBlockProgress {
/**
*
* @type {IrohaBlockResponse}
* @memberof IrohaBlockProgress
*/
transactionReceipt: IrohaBlockResponse;
}
/**
*
* @export
* @interface IrohaBlockResponse
*/
export interface IrohaBlockResponse {
/**
*
* @type {IrohaBlockResponsePayload}
* @memberof IrohaBlockResponse
*/
payload: IrohaBlockResponsePayload;
/**
*
* @type {Array<any>}
* @memberof IrohaBlockResponse
*/
signaturesList: Array<any>;
}
/**
*
* @export
* @interface IrohaBlockResponsePayload
*/
export interface IrohaBlockResponsePayload {
/**
*
* @type {Array<any>}
* @memberof IrohaBlockResponsePayload
*/
transactionsList: Array<any>;
/**
*
* @type {number}
* @memberof IrohaBlockResponsePayload
*/
txNumber: number;
/**
*
* @type {number}
* @memberof IrohaBlockResponsePayload
*/
height: number;
/**
*
* @type {string}
* @memberof IrohaBlockResponsePayload
*/
prevBlockHash: string;
/**
*
* @type {number}
* @memberof IrohaBlockResponsePayload
*/
createdTime: number;
/**
*
* @type {Array<any>}
* @memberof IrohaBlockResponsePayload
*/
rejectedTransactionsHashesList: Array<any>;
}
/**
*
Expand Down Expand Up @@ -256,6 +337,22 @@ export enum IrohaQuery {
GetPeers = 'getPeers'
}

/**
*
* @export
* @enum {string}
*/

export enum IrohaSocketSessionEvent {
Subscribe = 'org.hyperledger.cactus.api.async.iroha.SocketSession.Subscribe',
Next = 'org.hyperledger.cactus.api.async.iroha.SocketSession.Next',
Unsubscribe = 'org.hyperledger.cactus.api.async.iroha.SocketSession.Unsubscribe',
Error = 'org.hyperledger.cactus.api.async.iroha.SocketSession.Error',
Complete = 'org.hyperledger.cactus.api.async.iroha.SocketSession.Complete',
SendAsyncRequest = 'org.hyperledger.cactus.api.async.iroha.SocketSession.SendAsyncRequest',
SendSyncRequest = 'org.hyperledger.cactus.api.async.iroha.SocketSession.SendSyncRequest'
}

/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ import { QueryService_v1Client as QueryService } from "iroha-helpers-ts/lib/prot
import commands from "iroha-helpers-ts/lib/commands/index";
import queries from "iroha-helpers-ts/lib/queries";

export class Transaction {
export interface IIrohaTransactionWrapperOptions {
logLevel?: LogLevelDesc;
}

export class IrohaTransactionWrapper {
private readonly log: Logger;
public static readonly CLASS_NAME = "Transaction";
public static readonly CLASS_NAME = "IrohaTransactionWrapper";

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

constructor(logLevel?: LogLevelDesc) {
const level = logLevel || "INFO";
constructor(options: IIrohaTransactionWrapperOptions) {
const level = options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
}
Expand Down Expand Up @@ -413,7 +417,10 @@ export class Transaction {
});
return { transactionReceipt: response };
} catch (err: any) {
if ("monitorMode" in baseConfig && baseConfig.monitorMode === true) {
if (
"monitorModeEnabled" in baseConfig &&
baseConfig.monitorModeEnabled === true
) {
return { transactionReceipt: err };
} else {
this.log.error(err);
Expand Down
Loading

0 comments on commit ce975bb

Please sign in to comment.