Skip to content

Commit

Permalink
feat(connector-besu): add getPastLogs web service
Browse files Browse the repository at this point in the history
Fixes #1067

Signed-off-by: Hana Awad <awadhana0825@gmail.com>
  • Loading branch information
awadhana authored and petermetz committed Jul 8, 2021
1 parent e3fcfa7 commit c037ec5
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@
}
}
},

"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction": {
"post": {
"x-hyperledger-cactus": {
Expand Down Expand Up @@ -1025,6 +1024,41 @@
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs": {
"post": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "post",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs"
}
},
"operationId": "getPastLogsV1",
"summary": "Gets past logs, matching the given options.",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetPastLogsV1Request"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {

"$ref": "#/components/schemas/GetPastLogsV1Response"
}
}
}
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract": {
"post": {
"x-hyperledger-cactus": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,40 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
options: localVarRequestOptions,
};
},
/**
*
* @summary Gets past logs, matching the given options.
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getPastLogsV1: async (getPastLogsV1Request?: GetPastLogsV1Request, options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;



localVarHeaderParameter['Content-Type'] = 'application/json';

setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
localVarRequestOptions.data = serializeDataIfNeeded(getPastLogsV1Request, localVarRequestOptions, configuration)

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
*
* @summary Get the Prometheus Metrics
Expand Down Expand Up @@ -1233,6 +1267,17 @@ export const DefaultApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1BesuRunTransaction(runTransactionRequest, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary Gets past logs, matching the given options.
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetPastLogsV1Response>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getPastLogsV1(getPastLogsV1Request, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary Get the Prometheus Metrics
Expand Down Expand Up @@ -1316,6 +1361,16 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
apiV1BesuRunTransaction(runTransactionRequest?: RunTransactionRequest, options?: any): AxiosPromise<RunTransactionResponse> {
return localVarFp.apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(axios, basePath));
},
/**
*
* @summary Gets past logs, matching the given options.
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any): AxiosPromise<GetPastLogsV1Response> {
return localVarFp.getPastLogsV1(getPastLogsV1Request, options).then((request) => request(axios, basePath));
},
/**
*
* @summary Get the Prometheus Metrics
Expand Down Expand Up @@ -1401,6 +1456,18 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary Gets past logs, matching the given options.
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof DefaultApi
*/
public getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any) {
return DefaultApiFp(this.configuration).getPastLogsV1(getPastLogsV1Request, options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary Get the Prometheus Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
import { WatchBlocksV1Endpoint } from "./web-services/watch-blocks-v1-endpoint";
import { GetBalanceEndpoint } from "./web-services/get-balance-endpoint";
import { GetTransactionEndpoint } from "./web-services/get-transaction-endpoint";
import { GetPastLogsEndpoint } from "./web-services/get-past-logs-endpoint";

export const E_KEYCHAIN_NOT_FOUND = "cactus.connector.besu.keychain_not_found";

Expand All @@ -92,17 +93,6 @@ export interface IPluginLedgerConnectorBesuOptions
logLevel?: LogLevelDesc;
}

// export interface Log{
// address: string;
// data: string;
// blockHash: string;
// transactionHash: string;
// topics: Array<string>;
// logIndex: number;
// transactionIndex: number;
// blockNumber: number;

// }
export class PluginLedgerConnectorBesu
implements
IPluginLedgerConnector<
Expand Down Expand Up @@ -236,6 +226,13 @@ export class PluginLedgerConnectorBesu
});
endpoints.push(endpoint);
}
{
const endpoint = new GetPastLogsEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new RunTransactionEndpoint({
connector: this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Express, Request, Response } from "express";

import {
Logger,
Checks,
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
} from "@hyperledger/cactus-common";
import {
IEndpointAuthzOptions,
IExpressRequestHandler,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu";

import OAS from "../../json/openapi.json";

export interface IGetPastLogsEndpointOptions {
logLevel?: LogLevelDesc;
connector: PluginLedgerConnectorBesu;
}

export class GetPastLogsEndpoint implements IWebServiceEndpoint {
public static readonly CLASS_NAME = "GetPastLogsEndpoint";

private readonly log: Logger;

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

constructor(public readonly options: IGetPastLogsEndpointOptions) {
const fnTag = `${this.className}#constructor()`;
Checks.truthy(options, `${fnTag} arg options`);
Checks.truthy(options.connector, `${fnTag} arg options.connector`);

const level = this.options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
}

public getOasPath() {
return OAS.paths[
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs"
];
}

public getPath(): string {
const apiPath = this.getOasPath();
return apiPath.post["x-hyperledger-cactus"].http.path;
}

public getVerbLowerCase(): string {
const apiPath = this.getOasPath();
return apiPath.post["x-hyperledger-cactus"].http.verbLowerCase;
}

public getOperationId(): string {
return this.getOasPath().post.operationId;
}

getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> {
// TODO: make this an injectable dependency in the constructor
return {
get: async () => ({
isProtected: true,
requiredRoles: [],
}),
};
}

public async registerExpress(
expressApp: Express,
): Promise<IWebServiceEndpoint> {
await registerWebServiceEndpoint(expressApp, this);
return this;
}

public getExpressRequestHandler(): IExpressRequestHandler {
return this.handleRequest.bind(this);
}

public async handleRequest(req: Request, res: Response): Promise<void> {
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`;
this.log.debug(reqTag);
const reqBody = req.body;
try {
const resBody = await this.options.connector.getPastLogs(reqBody);
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
});
}
}
}
Loading

0 comments on commit c037ec5

Please sign in to comment.