Skip to content

Commit b7ad571

Browse files
rwat17petermetz
authored andcommitted
feat(connector-quorum): add WebsocketProvider options to quorum LP
- add WebsocketProvider options to quorum LP - add test for calling getBlock with verifier Depends on #2709 Co-authored-by: Peter Somogyvari <[email protected]> Signed-off-by: Tomasz Awramski <[email protected]> Signed-off-by: Peter Somogyvari <[email protected]>
1 parent e8170fc commit b7ad571

File tree

8 files changed

+99
-8
lines changed

8 files changed

+99
-8
lines changed

packages/cactus-plugin-ledger-connector-quorum/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"socket.io-client": "4.5.4",
7171
"typescript-optional": "2.0.1",
7272
"web3": "1.6.1",
73+
"web3-core-helpers": "1.6.1",
7374
"web3-eth-contract": "1.6.1",
7475
"web3-utils": "1.6.1",
7576
"web3js-quorum": "22.4.0"

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import Web3JsQuorum, {
1212
ISendRawTransaction,
1313
IPrivateTransactionReceipt,
1414
} from "web3js-quorum";
15+
import {
16+
WebsocketProviderOptions,
17+
HttpProviderOptions,
18+
} from "web3-core-helpers";
1519
import { AbiItem } from "web3-utils";
1620
import { Contract } from "web3-eth-contract";
1721
import { ContractSendMethod } from "web3-eth-contract";
@@ -84,6 +88,8 @@ export interface IPluginLedgerConnectorQuorumOptions
8488
prometheusExporter?: PrometheusExporter;
8589
pluginRegistry: PluginRegistry;
8690
privateUrl?: string;
91+
wsProviderOptions?: WebsocketProviderOptions;
92+
httpProviderOptions?: HttpProviderOptions;
8793
}
8894

8995
export class PluginLedgerConnectorQuorum
@@ -113,9 +119,20 @@ export class PluginLedgerConnectorQuorum
113119

114120
private getWeb3Provider() {
115121
if (!this.options.rpcApiWsHost) {
116-
return new Web3.providers.HttpProvider(this.options.rpcApiHttpHost);
117-
}
118-
return new Web3.providers.WebsocketProvider(this.options.rpcApiWsHost);
122+
return this.options.httpProviderOptions
123+
? new Web3.providers.HttpProvider(
124+
this.options.rpcApiHttpHost,
125+
this.options.httpProviderOptions,
126+
)
127+
: new Web3.providers.HttpProvider(this.options.rpcApiHttpHost);
128+
}
129+
130+
return this.options.wsProviderOptions
131+
? new Web3.providers.WebsocketProvider(
132+
this.options.rpcApiWsHost,
133+
this.options.wsProviderOptions,
134+
)
135+
: new Web3.providers.WebsocketProvider(this.options.rpcApiWsHost);
119136
}
120137

121138
constructor(public readonly options: IPluginLedgerConnectorQuorumOptions) {

packages/cactus-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/v21.4.1-invoke-web3-method-v1.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ describe("invokeRawWeb3EthMethod Tests", () => {
5151
await quorumTestLedger.start();
5252

5353
const rpcApiHttpHost = await quorumTestLedger.getRpcApiHttpHost();
54-
log.debug("rpcApiHttpHost:", rpcApiHttpHost);
54+
const getRpcApiWsHost = await quorumTestLedger.getRpcApiWsHost();
55+
log.warn("rpcApiHttpHost:", rpcApiHttpHost);
56+
log.warn("getRpcApiWsHost:", getRpcApiWsHost);
5557

5658
log.info("Create PluginLedgerConnectorQuorum...");
5759
connector = new PluginLedgerConnectorQuorum({

packages/cactus-test-plugin-ledger-connector-quorum/src/test/typescript/integration/api-client/verifier-integration-with-quorum-connector.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import "jest-extended";
1818
import lodash from "lodash";
1919
import { v4 as uuidv4 } from "uuid";
2020
import Web3 from "web3";
21+
import { WebsocketProviderOptions } from "web3-core-helpers";
2122
import { AbiItem } from "web3-utils";
2223
import { PluginRegistry } from "@hyperledger/cactus-core";
2324
import {
@@ -112,9 +113,19 @@ describe("Verifier integration with quorum connector tests", () => {
112113
plugins.push(keychainPlugin);
113114

114115
log.info("Create PluginLedgerConnectorQuorum...");
116+
117+
const wsProviderOptions: WebsocketProviderOptions = {
118+
clientConfig: {
119+
// Useful if requests are large
120+
maxReceivedFrameSize: 100000000,
121+
maxReceivedMessageSize: 100000000,
122+
},
123+
};
124+
115125
connector = new PluginLedgerConnectorQuorum({
116126
rpcApiHttpHost: connectionProfile.quorum.member1.url,
117127
rpcApiWsHost: connectionProfile.quorum.member1.wsUrl,
128+
wsProviderOptions: wsProviderOptions,
118129
logLevel: sutLogLevel,
119130
instanceId: uuidv4(),
120131
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
@@ -270,6 +281,26 @@ describe("Verifier integration with quorum connector tests", () => {
270281
};
271282
});
272283

284+
test("Using verifier for calling getBlock method works", async () => {
285+
const correctContract = {};
286+
const correctMethod = {
287+
type: "web3Eth",
288+
command: "getBlock",
289+
};
290+
291+
const correctArgs: any = { args: ["latest"] };
292+
293+
const resultCorrect = await verifier.sendSyncRequest(
294+
correctContract,
295+
correctMethod,
296+
correctArgs,
297+
);
298+
299+
log.warn("verifier", verifier);
300+
log.warn("###Results", resultCorrect);
301+
// expect(resultCorrect.status).toEqual(200);
302+
});
303+
273304
test("Invalid web3EthContract calls are rejected by QuorumApiClient", async () => {
274305
// Define correct input parameters
275306
const correctContract: Record<string, unknown> = lodash.clone(
@@ -500,6 +531,7 @@ describe("Verifier integration with quorum connector tests", () => {
500531
methodCall,
501532
argsCall,
502533
);
534+
log.error(resultsCall);
503535
expect(resultsCall.status).toEqual(200);
504536
expect(resultsCall.data).toEqual(newName);
505537
});

packages/cactus-test-tooling/src/main/typescript/quorum/quorum-test-ledger.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface IQuorumTestLedgerConstructorOptions {
2323
containerImageVersion?: string;
2424
containerImageName?: string;
2525
rpcApiHttpPort?: number;
26+
rpcApiWsPort?: number;
2627
logLevel?: LogLevelDesc;
2728
emitContainerLogs?: boolean;
2829
readonly envVars?: string[];
@@ -32,6 +33,7 @@ export const QUORUM_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({
3233
containerImageVersion: "2021-01-08-7a055c3",
3334
containerImageName: "ghcr.io/hyperledger/cactus-quorum-all-in-one",
3435
rpcApiHttpPort: 8545,
36+
rpcApiWsPort: 8546,
3537
});
3638

3739
export const QUORUM_TEST_LEDGER_OPTIONS_JOI_SCHEMA: Joi.Schema = Joi.object().keys(
@@ -51,6 +53,7 @@ export class QuorumTestLedger implements ITestLedger {
5153
public readonly containerImageVersion: string;
5254
public readonly containerImageName: string;
5355
public readonly rpcApiHttpPort: number;
56+
public readonly rpcApiWsPort: number;
5457
public readonly emitContainerLogs: boolean;
5558

5659
private readonly log: Logger;
@@ -73,6 +76,8 @@ export class QuorumTestLedger implements ITestLedger {
7376
this.rpcApiHttpPort =
7477
options.rpcApiHttpPort ||
7578
QUORUM_TEST_LEDGER_DEFAULT_OPTIONS.rpcApiHttpPort;
79+
this.rpcApiWsPort =
80+
options.rpcApiWsPort || QUORUM_TEST_LEDGER_DEFAULT_OPTIONS.rpcApiWsPort;
7681

7782
this.envVars = options.envVars || [];
7883

@@ -102,9 +107,14 @@ export class QuorumTestLedger implements ITestLedger {
102107

103108
public async getRpcApiHttpHost(): Promise<string> {
104109
const ipAddress = "127.0.0.1";
105-
const hostPort = await this.getRpcApiPublicPort();
110+
const hostPort = await this.getRpcHttpApiPublicPort();
106111
return `http://${ipAddress}:${hostPort}`;
107112
}
113+
public async getRpcApiWsHost(): Promise<string> {
114+
const ipAddress = "127.0.0.1";
115+
const hostPort = await this.getRpcWsApiPublicPort();
116+
return `ws://${ipAddress}:${hostPort}`;
117+
}
108118

109119
public async getFileContents(filePath: string): Promise<string> {
110120
const response: NodeJS.ReadableStream = await this.getContainer().getArchive(
@@ -221,7 +231,7 @@ export class QuorumTestLedger implements ITestLedger {
221231
Env: this.envVars,
222232
ExposedPorts: {
223233
[`${this.rpcApiHttpPort}/tcp`]: {}, // quorum RPC - HTTP
224-
"8546/tcp": {}, // quorum RPC - WebSocket
234+
[`${this.rpcApiWsPort}/tcp`]: {}, // quorum RPC - WebSocket
225235
"8888/tcp": {}, // orion Client Port - HTTP
226236
"8080/tcp": {}, // orion Node Port - HTTP
227237
"9001/tcp": {}, // supervisord - HTTP
@@ -339,7 +349,7 @@ export class QuorumTestLedger implements ITestLedger {
339349
}
340350
}
341351

342-
public async getRpcApiPublicPort(): Promise<number> {
352+
public async getRpcHttpApiPublicPort(): Promise<number> {
343353
const fnTag = "QuorumTestLedger#getRpcApiPublicPort()";
344354
const aContainerInfo = await this.getContainerInfo();
345355
const { rpcApiHttpPort: thePort } = this;
@@ -362,6 +372,29 @@ export class QuorumTestLedger implements ITestLedger {
362372
}
363373
}
364374

375+
public async getRpcWsApiPublicPort(): Promise<number> {
376+
const fnTag = "QuorumTestLedger#getRpcWsApiPublicPort()";
377+
const aContainerInfo = await this.getContainerInfo();
378+
const { rpcApiWsPort: thePort } = this;
379+
const { Ports: ports } = aContainerInfo;
380+
381+
if (ports.length < 1) {
382+
throw new Error(`${fnTag} no ports exposed or mapped at all`);
383+
}
384+
const mapping = ports.find((x) => x.PrivatePort === thePort);
385+
if (mapping) {
386+
if (!mapping.PublicPort) {
387+
throw new Error(`${fnTag} port ${thePort} mapped but not public`);
388+
} else if (mapping.IP !== "0.0.0.0") {
389+
throw new Error(`${fnTag} port ${thePort} mapped to localhost`);
390+
} else {
391+
return mapping.PublicPort;
392+
}
393+
} else {
394+
throw new Error(`${fnTag} no mapping found for ${thePort}`);
395+
}
396+
}
397+
365398
public async getContainerIpAddress(): Promise<string> {
366399
const fnTag = "QuorumTestLedger#getContainerIpAddress()";
367400
const aContainerInfo = await this.getContainerInfo();

packages/cactus-test-tooling/src/test/typescript/integration/quorum/quorum-test-ledger/constructor-validates-options.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tap.test("starts/stops/destroys a docker container", async (assert: any) => {
3535
assert.ok(ipAddress);
3636
assert.ok(ipAddress.length);
3737

38-
const hostPort: number = await ledger.getRpcApiPublicPort();
38+
const hostPort: number = await ledger.getRpcHttpApiPublicPort();
3939
assert.ok(hostPort, "getRpcApiPublicPort() returns truthy OK");
4040
assert.ok(isFinite(hostPort), "getRpcApiPublicPort() returns finite OK");
4141

tools/docker/quorum-all-in-one/start-quorum.sh

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ geth \
4343
--http.vhosts "*" \
4444
--http.addr 0.0.0.0 \
4545
--http.port 8545 \
46+
--ws \
47+
--wsaddr "0.0.0.0" \
48+
--wsport "8546" \
49+
--wsapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,${QUORUM_CONSENSUS:-raft} \
50+
--wsorigins "*" \
4651
--http.api admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,${QUORUM_CONSENSUS:-raft} \
4752
--port 21000 \
4853
--allow-insecure-unlock \

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -6702,6 +6702,7 @@ __metadata:
67026702
socket.io-client: 4.5.4
67036703
typescript-optional: 2.0.1
67046704
web3: 1.6.1
6705+
web3-core-helpers: 1.6.1
67056706
web3-eth: 1.6.1
67066707
web3-eth-contract: 1.6.1
67076708
web3-utils: 1.6.1

0 commit comments

Comments
 (0)