-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(quorum-connector): add script for checking connection status
- 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
Showing
3 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/cactus-plugin-ledger-connector-quorum/src/scripts/get-quorum-connector-status.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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== | ||
|
@@ -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== | ||
|