Skip to content

Commit

Permalink
feat(connector-besu): add get past logs method
Browse files Browse the repository at this point in the history
Signed-off-by: Hana Awad <awadhana0825@gmail.com>
  • Loading branch information
awadhana authored and petermetz committed Jul 2, 2021
1 parent 61a83d7 commit c4900e9
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cactus-plugin-ledger-connector-besu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"@hyperledger/cactus-test-tooling": "0.5.0",
"@types/express": "4.17.8",
"socket.io": "4.0.1",
"web3-core": "1.2.7",
"web3-eth": "1.2.7"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,83 @@
"type": "string"
}
}

},
"GetPastLogsV1Response": {
"type": "object",
"required": [
"logs"
],
"properties": {
"logs": {
"type": "array",
"items":{
"$ref": "#/components/schemas/EvmLog"
}
}

}
},
"GetPastLogsV1Request": {
"type": "object",
"properties": {
"toBlock": {
},
"fromBlock": {

},
"address": {
},
"topics": {
"type" : "array",
"items":{

}
}

}
},
"EvmLog":{
"type" : "object",
"required": [
"address",
"data",
"blockHash",
"transactionHash",
"topics",
"logIndex",
"transactionIndex",
"blockNumber"
],
"properties": {
"address":{
"type": "string"
},
"data":{
"type": "string"
},
"blockHash":{
"type": "string"
},
"transactionHash":{
"type": "string"
},
"topics":{
"type": "array",
"items":{
"type" :"string"

}
},
"logIndex":{
"type": "number"
},
"transactionIndex":{
"type": "number"
},
"blockNumber":{
"type": "number"
}
}
},
"WatchBlocksV1": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,61 @@ export enum EthContractInvocationType {
Call = 'CALL'
}

/**
*
* @export
* @interface EvmLog
*/
export interface EvmLog {
/**
*
* @type {string}
* @memberof EvmLog
*/
address: string;
/**
*
* @type {string}
* @memberof EvmLog
*/
data: string;
/**
*
* @type {string}
* @memberof EvmLog
*/
blockHash: string;
/**
*
* @type {string}
* @memberof EvmLog
*/
transactionHash: string;
/**
*
* @type {Array<string>}
* @memberof EvmLog
*/
topics: Array<string>;
/**
*
* @type {number}
* @memberof EvmLog
*/
logIndex: number;
/**
*
* @type {number}
* @memberof EvmLog
*/
transactionIndex: number;
/**
*
* @type {number}
* @memberof EvmLog
*/
blockNumber: number;
}
/**
*
* @export
Expand Down Expand Up @@ -292,6 +347,50 @@ export interface GetBalanceV1Response {
*/
balance: string;
}
/**
*
* @export
* @interface GetPastLogsV1Request
*/
export interface GetPastLogsV1Request {
/**
*
* @type {any}
* @memberof GetPastLogsV1Request
*/
toBlock?: any | null;
/**
*
* @type {any}
* @memberof GetPastLogsV1Request
*/
fromBlock?: any | null;
/**
*
* @type {any}
* @memberof GetPastLogsV1Request
*/
address?: any | null;
/**
*
* @type {Array<any>}
* @memberof GetPastLogsV1Request
*/
topics?: Array<any>;
}
/**
*
* @export
* @interface GetPastLogsV1Response
*/
export interface GetPastLogsV1Response {
/**
*
* @type {Array<EvmLog>}
* @memberof GetPastLogsV1Response
*/
logs: Array<EvmLog>;
}
/**
*
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
GetBalanceV1Response,
} from "./generated/openapi/typescript-axios/index";

import {
GetPastLogsV1Request,
GetPastLogsV1Response,
} from "./generated/openapi/typescript-axios/index";
import {
ConsensusAlgorithmFamily,
IPluginLedgerConnector,
Expand Down Expand Up @@ -730,4 +734,11 @@ export class PluginLedgerConnectorBesu
);
return { transaction };
}

public async getPastLogs(
request: GetPastLogsV1Request,
): Promise<GetPastLogsV1Response> {
const logs = await this.web3.eth.getPastLogs(request);
return { logs };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import test, { Test } from "tape";
import { v4 as uuidv4 } from "uuid";
import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginLedgerConnectorBesu,
PluginFactoryLedgerConnector,
GetPastLogsV1Request,
} from "../../../../../main/typescript/public-api";
import { PluginKeychainMemory } from "@hyperledger/cactus-plugin-keychain-memory";
import { BesuTestLedger } from "@hyperledger/cactus-test-tooling";
import { LogLevelDesc } from "@hyperledger/cactus-common";
import HelloWorldContractJson from "../../../../solidity/hello-world-contract/HelloWorld.json";
import Web3 from "web3";
import { PluginImportType } from "@hyperledger/cactus-core-api";

test("can get past logs of an account", async (t: Test) => {
const logLevel: LogLevelDesc = "TRACE";
const besuTestLedger = new BesuTestLedger();
await besuTestLedger.start();

test.onFinish(async () => {
await besuTestLedger.stop();
await besuTestLedger.destroy();
});

const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();

/**
* Constant defining the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
const firstHighNetWorthAccount = "627306090abaB3A6e1400e9345bC60c78a8BEf57";

const web3 = new Web3(rpcApiHttpHost);
const testEthAccount = web3.eth.accounts.create(uuidv4());

const keychainEntryKey = uuidv4();
const keychainEntryValue = testEthAccount.privateKey;
const keychainPlugin = new PluginKeychainMemory({
instanceId: uuidv4(),
keychainId: uuidv4(),
// pre-provision keychain with mock backend holding the private key of the
// test account that we'll reference while sending requests with the
// signing credential pointing to this keychain entry.
backend: new Map([[keychainEntryKey, keychainEntryValue]]),
logLevel,
});
keychainPlugin.set(
HelloWorldContractJson.contractName,
HelloWorldContractJson,
);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});

const connector: PluginLedgerConnectorBesu = await factory.create({
rpcApiHttpHost,
rpcApiWsHost,
instanceId: uuidv4(),
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
});

const req: GetPastLogsV1Request = { address: firstHighNetWorthAccount };
const pastLogs = await connector.getPastLogs(req);
t.comment(JSON.stringify(pastLogs));
t.ok(pastLogs, "Past logs response is OK :-)");
t.equal(typeof pastLogs, "object", "Past logs response type is OK :-)");
t.end();
});

0 comments on commit c4900e9

Please sign in to comment.