Skip to content

Commit c037ec5

Browse files
awadhanapetermetz
authored andcommitted
feat(connector-besu): add getPastLogs web service
Fixes #1067 Signed-off-by: Hana Awad <[email protected]>
1 parent e3fcfa7 commit c037ec5

File tree

5 files changed

+382
-12
lines changed

5 files changed

+382
-12
lines changed

packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json

+35-1
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,6 @@
990990
}
991991
}
992992
},
993-
994993
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction": {
995994
"post": {
996995
"x-hyperledger-cactus": {
@@ -1025,6 +1024,41 @@
10251024
}
10261025
}
10271026
},
1027+
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs": {
1028+
"post": {
1029+
"x-hyperledger-cactus": {
1030+
"http": {
1031+
"verbLowerCase": "post",
1032+
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs"
1033+
}
1034+
},
1035+
"operationId": "getPastLogsV1",
1036+
"summary": "Gets past logs, matching the given options.",
1037+
"parameters": [],
1038+
"requestBody": {
1039+
"content": {
1040+
"application/json": {
1041+
"schema": {
1042+
"$ref": "#/components/schemas/GetPastLogsV1Request"
1043+
}
1044+
}
1045+
}
1046+
},
1047+
"responses": {
1048+
"200": {
1049+
"description": "OK",
1050+
"content": {
1051+
"application/json": {
1052+
"schema": {
1053+
1054+
"$ref": "#/components/schemas/GetPastLogsV1Response"
1055+
}
1056+
}
1057+
}
1058+
}
1059+
}
1060+
}
1061+
},
10281062
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract": {
10291063
"post": {
10301064
"x-hyperledger-cactus": {

packages/cactus-plugin-ledger-connector-besu/src/main/typescript/generated/openapi/typescript-axios/api.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,40 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
10561056
options: localVarRequestOptions,
10571057
};
10581058
},
1059+
/**
1060+
*
1061+
* @summary Gets past logs, matching the given options.
1062+
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
1063+
* @param {*} [options] Override http request option.
1064+
* @throws {RequiredError}
1065+
*/
1066+
getPastLogsV1: async (getPastLogsV1Request?: GetPastLogsV1Request, options: any = {}): Promise<RequestArgs> => {
1067+
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs`;
1068+
// use dummy base URL string because the URL constructor only accepts absolute URLs.
1069+
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
1070+
let baseOptions;
1071+
if (configuration) {
1072+
baseOptions = configuration.baseOptions;
1073+
}
1074+
1075+
const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
1076+
const localVarHeaderParameter = {} as any;
1077+
const localVarQueryParameter = {} as any;
1078+
1079+
1080+
1081+
localVarHeaderParameter['Content-Type'] = 'application/json';
1082+
1083+
setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
1084+
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
1085+
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
1086+
localVarRequestOptions.data = serializeDataIfNeeded(getPastLogsV1Request, localVarRequestOptions, configuration)
1087+
1088+
return {
1089+
url: toPathString(localVarUrlObj),
1090+
options: localVarRequestOptions,
1091+
};
1092+
},
10591093
/**
10601094
*
10611095
* @summary Get the Prometheus Metrics
@@ -1233,6 +1267,17 @@ export const DefaultApiFp = function(configuration?: Configuration) {
12331267
const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1BesuRunTransaction(runTransactionRequest, options);
12341268
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
12351269
},
1270+
/**
1271+
*
1272+
* @summary Gets past logs, matching the given options.
1273+
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
1274+
* @param {*} [options] Override http request option.
1275+
* @throws {RequiredError}
1276+
*/
1277+
async getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetPastLogsV1Response>> {
1278+
const localVarAxiosArgs = await localVarAxiosParamCreator.getPastLogsV1(getPastLogsV1Request, options);
1279+
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
1280+
},
12361281
/**
12371282
*
12381283
* @summary Get the Prometheus Metrics
@@ -1316,6 +1361,16 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
13161361
apiV1BesuRunTransaction(runTransactionRequest?: RunTransactionRequest, options?: any): AxiosPromise<RunTransactionResponse> {
13171362
return localVarFp.apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(axios, basePath));
13181363
},
1364+
/**
1365+
*
1366+
* @summary Gets past logs, matching the given options.
1367+
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
1368+
* @param {*} [options] Override http request option.
1369+
* @throws {RequiredError}
1370+
*/
1371+
getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any): AxiosPromise<GetPastLogsV1Response> {
1372+
return localVarFp.getPastLogsV1(getPastLogsV1Request, options).then((request) => request(axios, basePath));
1373+
},
13191374
/**
13201375
*
13211376
* @summary Get the Prometheus Metrics
@@ -1401,6 +1456,18 @@ export class DefaultApi extends BaseAPI {
14011456
return DefaultApiFp(this.configuration).apiV1BesuRunTransaction(runTransactionRequest, options).then((request) => request(this.axios, this.basePath));
14021457
}
14031458

1459+
/**
1460+
*
1461+
* @summary Gets past logs, matching the given options.
1462+
* @param {GetPastLogsV1Request} [getPastLogsV1Request]
1463+
* @param {*} [options] Override http request option.
1464+
* @throws {RequiredError}
1465+
* @memberof DefaultApi
1466+
*/
1467+
public getPastLogsV1(getPastLogsV1Request?: GetPastLogsV1Request, options?: any) {
1468+
return DefaultApiFp(this.configuration).getPastLogsV1(getPastLogsV1Request, options).then((request) => request(this.axios, this.basePath));
1469+
}
1470+
14041471
/**
14051472
*
14061473
* @summary Get the Prometheus Metrics

packages/cactus-plugin-ledger-connector-besu/src/main/typescript/plugin-ledger-connector-besu.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
import { WatchBlocksV1Endpoint } from "./web-services/watch-blocks-v1-endpoint";
8181
import { GetBalanceEndpoint } from "./web-services/get-balance-endpoint";
8282
import { GetTransactionEndpoint } from "./web-services/get-transaction-endpoint";
83+
import { GetPastLogsEndpoint } from "./web-services/get-past-logs-endpoint";
8384

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

@@ -92,17 +93,6 @@ export interface IPluginLedgerConnectorBesuOptions
9293
logLevel?: LogLevelDesc;
9394
}
9495

95-
// export interface Log{
96-
// address: string;
97-
// data: string;
98-
// blockHash: string;
99-
// transactionHash: string;
100-
// topics: Array<string>;
101-
// logIndex: number;
102-
// transactionIndex: number;
103-
// blockNumber: number;
104-
105-
// }
10696
export class PluginLedgerConnectorBesu
10797
implements
10898
IPluginLedgerConnector<
@@ -236,6 +226,13 @@ export class PluginLedgerConnectorBesu
236226
});
237227
endpoints.push(endpoint);
238228
}
229+
{
230+
const endpoint = new GetPastLogsEndpoint({
231+
connector: this,
232+
logLevel: this.options.logLevel,
233+
});
234+
endpoints.push(endpoint);
235+
}
239236
{
240237
const endpoint = new RunTransactionEndpoint({
241238
connector: this,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Express, Request, Response } from "express";
2+
3+
import {
4+
Logger,
5+
Checks,
6+
LogLevelDesc,
7+
LoggerProvider,
8+
IAsyncProvider,
9+
} from "@hyperledger/cactus-common";
10+
import {
11+
IEndpointAuthzOptions,
12+
IExpressRequestHandler,
13+
IWebServiceEndpoint,
14+
} from "@hyperledger/cactus-core-api";
15+
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
16+
17+
import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu";
18+
19+
import OAS from "../../json/openapi.json";
20+
21+
export interface IGetPastLogsEndpointOptions {
22+
logLevel?: LogLevelDesc;
23+
connector: PluginLedgerConnectorBesu;
24+
}
25+
26+
export class GetPastLogsEndpoint implements IWebServiceEndpoint {
27+
public static readonly CLASS_NAME = "GetPastLogsEndpoint";
28+
29+
private readonly log: Logger;
30+
31+
public get className(): string {
32+
return GetPastLogsEndpoint.CLASS_NAME;
33+
}
34+
35+
constructor(public readonly options: IGetPastLogsEndpointOptions) {
36+
const fnTag = `${this.className}#constructor()`;
37+
Checks.truthy(options, `${fnTag} arg options`);
38+
Checks.truthy(options.connector, `${fnTag} arg options.connector`);
39+
40+
const level = this.options.logLevel || "INFO";
41+
const label = this.className;
42+
this.log = LoggerProvider.getOrCreate({ level, label });
43+
}
44+
45+
public getOasPath() {
46+
return OAS.paths[
47+
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-past-logs"
48+
];
49+
}
50+
51+
public getPath(): string {
52+
const apiPath = this.getOasPath();
53+
return apiPath.post["x-hyperledger-cactus"].http.path;
54+
}
55+
56+
public getVerbLowerCase(): string {
57+
const apiPath = this.getOasPath();
58+
return apiPath.post["x-hyperledger-cactus"].http.verbLowerCase;
59+
}
60+
61+
public getOperationId(): string {
62+
return this.getOasPath().post.operationId;
63+
}
64+
65+
getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> {
66+
// TODO: make this an injectable dependency in the constructor
67+
return {
68+
get: async () => ({
69+
isProtected: true,
70+
requiredRoles: [],
71+
}),
72+
};
73+
}
74+
75+
public async registerExpress(
76+
expressApp: Express,
77+
): Promise<IWebServiceEndpoint> {
78+
await registerWebServiceEndpoint(expressApp, this);
79+
return this;
80+
}
81+
82+
public getExpressRequestHandler(): IExpressRequestHandler {
83+
return this.handleRequest.bind(this);
84+
}
85+
86+
public async handleRequest(req: Request, res: Response): Promise<void> {
87+
const reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`;
88+
this.log.debug(reqTag);
89+
const reqBody = req.body;
90+
try {
91+
const resBody = await this.options.connector.getPastLogs(reqBody);
92+
res.json(resBody);
93+
} catch (ex) {
94+
this.log.error(`Crash while serving ${reqTag}`, ex);
95+
res.status(500).json({
96+
message: "Internal Server Error",
97+
error: ex?.stack || ex?.message,
98+
});
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)