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: hyperledger#2309

Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH committed Mar 3, 2023
1 parent 6335787 commit ed4d5c6
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
10 changes: 8 additions & 2 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 @@ -64,14 +67,17 @@
"sanitize-html": "2.7.0",
"typescript-optional": "2.0.1",
"web3": "1.5.2",
"web3-eth-contract": "1.5.2"
"web3-eth-contract": "1.5.2",
"minimist": "1.2.8"
},
"devDependencies": {
"@hyperledger/cactus-plugin-keychain-memory": "1.1.3",
"@hyperledger/cactus-test-tooling": "1.1.3",
"@types/express": "4.17.13",
"@types/sanitize-html": "2.6.2",
"web3-eth": "1.5.2"
"@types/minimist": "1.2.2",
"web3-eth": "1.5.2",
"chalk": "4.1.2"
},
"engines": {
"node": ">=10",
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 @@ -4879,7 +4879,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 @@ -16560,7 +16560,7 @@ minimist-options@4.1.0:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"

minimist@>=1.2.6, minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5:
minimist@1.2.8, minimist@>=1.2.6, minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5:
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 ed4d5c6

Please sign in to comment.