Skip to content

Commit

Permalink
feat(quorum-connector): add script for checking connection status
Browse files Browse the repository at this point in the history
- Simple script will check ledger connection by getting block through the connector.

Closes: #2309

Co-authored-by: Peter Somogyvari <[email protected]>

Signed-off-by: Michal Bajer <[email protected]>
Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
outSH authored and petermetz committed Jun 13, 2023
1 parent 47a64ee commit d306d21
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/cactus-plugin-ledger-connector-quorum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"module": "dist/lib/main/typescript/index.js",
"browser": "dist/cactus-plugin-ledger-connector-quorum.web.umd.js",
"types": "dist/lib/main/typescript/index.d.ts",
"bin": {
"cacti-quorum-connector-status": "dist/lib/scripts/get-quorum-connector-status.js"
},
"files": [
"dist/*"
],
Expand All @@ -59,6 +62,7 @@
"@hyperledger/cactus-core-api": "2.0.0-alpha.1",
"axios": "0.21.4",
"express": "4.17.3",
"minimist": "1.2.8",
"prom-client": "13.2.0",
"run-time-error": "1.4.0",
"rxjs": "7.3.0",
Expand All @@ -72,7 +76,9 @@
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0-alpha.1",
"@hyperledger/cactus-test-tooling": "2.0.0-alpha.1",
"@types/express": "4.17.13",
"@types/minimist": "1.2.2",
"@types/sanitize-html": "2.6.2",
"chalk": "4.1.2",
"web3-eth": "1.5.2"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env node

/**
* Simple command line tool to check ledger connection of running quorum connector.
* Will try to get latest block from the ledger.
*
* Usage:
* After installing the connector package...
* `npm install -g` in it's dir or directly from NPM - `npm install @hyperledger/cactus-plugin-ledger-connector-quorum`
* ...you can start the command line tool with npx:
* `npx cacti-quorum-connector-status <URL>:<PORT>`
*
* TODO:
* - Add healthcheck endpoint to quorum connector and query it instead of reading the latest block.
*/

import {
QuorumApiClient,
QuorumApiClientOptions,
} from "../main/typescript/public-api";

import minimist from "minimist";
import path from "path";
import axios from "axios";
import chalk from "chalk";

type AuthOptions = {
apiKey?: string;
accessToken?: string;
username?: string;
password?: string;
};

async function main(url: string, auth: AuthOptions = {}) {
try {
console.log(`Check Quorum connector ${url}...`);
const config = new QuorumApiClientOptions({
basePath: url,
...auth,
});
const quorumApiClient = new QuorumApiClient(config);

// Get latest block
const connectorResponse = await quorumApiClient.invokeWeb3EthMethodV1({
methodName: "getBlock",
params: ["latest"],
});

// Check response
if (
!connectorResponse ||
!connectorResponse.data ||
!connectorResponse.data.data ||
connectorResponse.data.status !== 200
) {
console.log(connectorResponse.data);
throw new Error("Invalid response from the connector");
}

const blockData = connectorResponse.data.data;
console.log(
chalk.green(`OK - Latest block #${blockData.number} ${blockData.hash}`),
);
} catch (error: unknown) {
let errorMessage = `Error: ${error}`;
if (axios.isAxiosError(error)) {
errorMessage = `${error.name}: ${error.message}`;
}

console.error(chalk.red(errorMessage));
process.exit(2);
}
}

function showHelp() {
const scriptName = path.basename(__filename);
console.log(
chalk.yellow(
`Usage: ${scriptName} <CONNECTOR_URL>:<PORT> [-h|--help] [--apiKey <KEY>] [--accessToken <KEY>] [--username <USERNAME> --password <PASSWORD>]`,
),
);
process.exit(1);
}

if (require.main === module) {
const argv = minimist(process.argv.slice(2));
if (
argv["h"] ||
argv["help"] ||
argv["_"].length !== 1 ||
(argv["username"] && !argv["password"]) ||
(argv["password"] && !argv["username"])
) {
showHelp();
}
const connectorUrl = argv["_"][0];

// Ensure valid URL was provided.
// Without it the connector request will hang indefinitely.
try {
new URL(connectorUrl);
} catch (err) {
console.error(chalk.red(err));
showHelp();
}

main(connectorUrl, {
apiKey: argv["apiKey"],
accessToken: argv["accessToken"],
username: argv["username"],
password: argv["password"],
});
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7019,7 +7019,7 @@
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==

"@types/minimist@^1.2.0", "@types/minimist@^1.2.2":
"@types/minimist@1.2.2", "@types/minimist@^1.2.0", "@types/minimist@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==
Expand Down Expand Up @@ -22437,7 +22437,7 @@ [email protected]:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"

[email protected], minimist@>=1.2.6, minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5, minimist@^1.2.6:
[email protected], minimist@1.2.8, minimist@>=1.2.6, minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5, minimist@^1.2.6:
version "1.2.7"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18"
integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==
Expand Down

0 comments on commit d306d21

Please sign in to comment.