Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nodejs client documentation + small fixes to JSON RPC client #610

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions clients/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ const MetricsServer = require('./modules/MetricsServer.js');
const config = require('./modules/Config.js')(argv);
const openBrowserTab = require('./modules/NodeUtils.js').openBrowserTab;

// Deprecated dumb config flag.
if (config.dumb) {
console.error(`The '--dumb' flag is deprecated, use '--protocol=dumb' instead.`);
config.protocol = 'dumb';
}

if ((config.protocol === 'wss' && !(config.host && config.port && config.tls && config.tls.cert && config.tls.key)) ||
(config.protocol === 'ws' && !(config.host && config.port)) ||
argv.help) {
Expand All @@ -39,35 +33,36 @@ if ((config.protocol === 'wss' && !(config.host && config.port && config.tls &&
' --key=SSL_KEY_FILE Private key file to use.\n' +
' --port=PORT Specifies which port to listen on for connections.\n' +
' --protocol=TYPE Set up the protocol to be used. Available protocols are\n' +
' - wss: WebSocket Secure, requires a FQDN, port,\n' +
' and SSL certificate\n' +
' - wss (default): WebSocket Secure, requires a FQDN, port,\n' +
' and SSL certificate\n' +
' - ws: WebSocket, only requires public IP/FQDN and port\n' +
' - dumb: discouraged as the number of dumb nodes might\n' +
' be limited\n' +
' be limited\n' +
'\n' +
'Options:\n' +
' --help Show this usage instructions.\n' +
' --log[=LEVEL] Configure global log level. Not specifying a log\n' +
' level will enable verbose log output.\n' +
' --log-tag=TAG[:LEVEL] Configure log level for a specific tag.\n' +
' --miner[=THREADS] Activate mining on this node. The miner will be set\n' +
' up to use THREADS parallel threads.\n' +
' --pool=SERVER:PORT Mine shares for mining pool with address SERVER:PORT\n' +
' --device-data=DATA_JSON Pass information about this device to the pool. Takes a\n' +
' valid JSON string. Only used when registering for a pool.\n' +
' valid JSON string, the format of which is defined by the\n' +
' pool operator. Only used when registering for a pool.\n' +
' --passive Do not actively connect to the network and do not\n' +
' wait for connection establishment.\n' +
' --rpc[=PORT] Start JSON-RPC server on port PORT (default: 8648).\n' +
' --metrics[=PORT] Start Prometheus-compatible metrics server on port\n' +
' [:PASSWORD] PORT (default: 8649). If PASSWORD is specified, it\n' +
' is required to be used for username "metrics" via\n' +
' Basic Authentication.\n' +
' --ui[=PORT] Serve a miner UI on port PORT (default: 8650).\n' +
' --ui[=PORT] Serve a UI on port PORT (default: 8650).\n' +
' The UI will be reachable at localhost:PORT.\n' +
' --statistics[=INTERVAL] Output statistics like mining hashrate, current\n' +
' account balance and mempool size every INTERVAL\n' +
' seconds.\n' +
' --type=TYPE Configure the consensus type to establish, one of\n' +
' full (default), light, or nano.\n' +
' full (default), light, nano or pico.\n' +
' --volatile Run in volatile mode. Consensus state is kept\n' +
' in memory only and not written to disk.\n' +
' --reverse-proxy[=PORT] This client is behind a reverse proxy running on PORT,IP\n' +
Expand Down
22 changes: 17 additions & 5 deletions clients/nodejs/modules/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const TAG = 'Config';
* @property {boolean} passive
* @property {number} statistics
* @property {{enabled: boolean, threads: string|number, throttleAfter: number, throttleWait: number, extraData: string}} miner
* @property {{enabled: boolean, host: string, port: number, mode: string}} poolMining
* @property {{enabled: boolean, port: number, corsdomain: string|Array.<string>, allowip: string|Array.<string>, username: string, password: string}} rpcServer
* @property {{enabled: boolean, host: string, port: number, mode: string, deviceData: object}} poolMining
* @property {{enabled: boolean, port: number, corsdomain: string|Array.<string>, allowip: string|Array.<string>, methods: Array.<string>, username: string, password: string}} rpcServer
* @property {{enabled: boolean, port: number}} uiServer
* @property {{enabled: boolean, port: number, password: string}} metricsServer
* @property {{seed: string, address: string}} wallet
Expand Down Expand Up @@ -106,7 +106,7 @@ const CONFIG_TYPES = {
dumb: 'boolean', // deprecated
type: {type: 'string', values: ['full', 'light', 'nano', 'pico']},
volatile: 'boolean',
network: 'string',
network: {type: 'string', values: ['main', 'test', 'dev']},
passive: 'boolean',
statistics: 'number',
miner: {
Expand Down Expand Up @@ -287,7 +287,14 @@ function readFromFile(file, oldConfig = merge({}, DEFAULT_CONFIG)) {
return false;
} else {
config = merge(oldConfig, config);
if (config.dumb) {
Log.w('The \'dumb\' flag is deprecated, use \'protocol: "dumb"\' instead.');
config.protocol = 'dumb';
}
if (config.reverseProxy.address && config.reverseProxy.addresses.length === 0) {
if (config.reverseProxy.address !== DEFAULT_CONFIG.reverseProxy.address) {
Log.w('The \'address\' option for \'reverseProxy\' is deprecated, use \'addresses\' instead.');
}
config.reverseProxy.addresses.push(config.reverseProxy.address);
}
return config;
Expand Down Expand Up @@ -315,7 +322,10 @@ function readFromArgs(argv, config = merge({}, DEFAULT_CONFIG)) {
if (typeof argv.cert === 'string') config.tls.cert = argv.cert;
if (typeof argv.key === 'string') config.tls.key = argv.key;
if (typeof argv.protocol === 'string') config.protocol = argv.protocol;
if (argv.dumb) config.dumb = argv.dumb; // deprecated
if (argv.dumb) {
Log.w('The \'--dumb\' flag is deprecated, use \'--protocol=dumb\' instead.');
config.protocol = 'dumb';
}
if (typeof argv.type === 'string') config.type = argv.type;
if (argv.volatile) config.volatile = argv.volatile;
if (typeof argv.network === 'string') config.network = argv.network;
Expand Down Expand Up @@ -378,8 +388,10 @@ function readFromArgs(argv, config = merge({}, DEFAULT_CONFIG)) {
}
if (argv.log || argv.verbose) {
config.log.level = 'verbose';
if (typeof argv.log === 'number') config.log.level = argv.log;
if (typeof argv.log === 'string') config.log.level = argv.log;
if (argv.verbose) {
Log.w('The \'--verbose\' flag is deprecated, use \'--log\' instead.');
}
}

if (!validateObjectType(config)) {
Expand Down
69 changes: 39 additions & 30 deletions clients/nodejs/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const Nimiq = require('../../dist/node.js');
const chalk = require('chalk');
const btoa = require('btoa');
const readline = require('readline');
const argv = require('minimist')(process.argv.slice(2));
const argv = require('minimist')(process.argv.slice(2), { boolean: ['silent'] });

let host = '127.0.0.1';
let port = 8648;
let user = null;
let password = null;
const silent = !!argv.silent;
if (argv.host) host = argv.host;
if (argv.port) port = parseInt(argv.port);
if (argv.user) {
Expand Down Expand Up @@ -392,7 +393,7 @@ async function action(args, rl) {
switch (args[0]) {
// Miner
case 'mining': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader();
}
const enabled = await jsonRpcFetch('mining');
Expand Down Expand Up @@ -463,7 +464,7 @@ async function action(args, rl) {
}
// Accounts
case 'accounts': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(68);
}
const accounts = await jsonRpcFetch('accounts');
Expand Down Expand Up @@ -492,7 +493,7 @@ async function action(args, rl) {
return;
}
case 'account': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(81);
}
if (args.length === 2) {
Expand All @@ -512,7 +513,7 @@ async function action(args, rl) {
}
// Blocks
case 'block': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(79);
}
if (args.length === 2) {
Expand Down Expand Up @@ -542,7 +543,7 @@ async function action(args, rl) {
}
// Transactions
case 'transaction': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(79);
}
if (args.length === 2) {
Expand Down Expand Up @@ -577,7 +578,7 @@ async function action(args, rl) {
return;
}
case 'transaction.send': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(74);
}
if (args.length < 5 || args.length > 6) {
Expand Down Expand Up @@ -608,7 +609,7 @@ async function action(args, rl) {
return;
}
case 'transaction.receipt': {
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(74);
}
if (args.length !== 2) {
Expand Down Expand Up @@ -639,7 +640,7 @@ async function action(args, rl) {
console.error('Specify account address');
return;
}
if (!rl && !argv.silent) {
if (!rl && !silent) {
await displayInfoHeader(75);
}
const transactions = (await jsonRpcFetch('getTransactionsByAddress', args[1], args[2])).sort((a, b) => a.timestamp > b.timestamp);
Expand Down Expand Up @@ -769,22 +770,22 @@ async function action(args, rl) {
return;
}
case 'log': {
if (args.length < 2) {
args.push('verbose');
}
if (args.length < 3) {
args.splice(1, 0, '*');
}
if (args.length > 3) {
console.error('Too many args');
return;
}
args[2] = Nimiq.Log.Level.toString(Nimiq.Log.Level.get(args[2]));
JSON.stringify(await jsonRpcFetch('log', args[1], args[2]));
if (args[1] === '*') {
console.log(`Global log level set to ${args[2]}`);
let level = args[2] || (args[1] && Nimiq.Log.Level.get(args[1])
? args[1] // user provided a single parameter which was the log level
: 'verbose');
const tag = args.length < 2 || args[1] === level
? '*' // no tag provided
: args[1];
level = Nimiq.Log.Level.toString(Nimiq.Log.Level.get(level)); // normalize as string
await jsonRpcFetch('log', tag, level);
if (tag === '*') {
console.log(`Global log level set to ${level}`);
} else {
console.log(`Log level for tag ${args[1]} set to ${args[2]}`);
console.log(`Log level for tag ${tag} set to ${level}`);
}
return;
}
Expand All @@ -808,24 +809,27 @@ Options:
Defaults to 8648.
--user USER Use basic authentication with username USER.
The password will be prompted for.
--silent Mute display of info headers in non-interactive
mode.

`);
}
console.log(`Actions:
help Display this help.
account ADDR Display details for account with address ADDR.
accounts List local accounts.
accounts.create Create a new Nimiq Account and store it in the
WalletStore of the Nimiq node.
block BLOCK Display details of block BLOCK.
constant CONST [VAL] Display value of constant CONST. If VAL is given,
overrides constant const with value VAL.
overrides constant CONST with value VAL.
consensus.min_fee_per_byte [FEE]
Read or change the current min fee per byte setting.
help Display this help.
log [LEVEL] [TAG] Set the log level for TAG to LEVEL. If LEVEL is
log [TAG] [LEVEL] Set the log level for TAG to LEVEL. If LEVEL is
omitted, 'verbose' is assumed. If TAG is omitted,
'*' is assumed.
mining Display information on current mining settings.
mining Display information on current mining settings
and state.
mining.enabled [VAL] Read or change enabled state of miner.
mining.threads [VAL] Read or change number of threads of miner.
mining.hashrate Read the current hashrate of miner.
Expand Down Expand Up @@ -857,15 +861,20 @@ Options:
properties. The sending account must be a local
account.
transactions ADDR [LIMIT]
Display at most LIMIT transactions involving address ADDR.

Display at most LIMIT transactions involving address
ADDR.


Most actions support output either in human-readable text form (default) or as
JSON by appending '.json' to the action name. Addresses may be given in user-
friendly address format, hex or base64 encoded. Blocks can be specified by hash
in hex or base64 format or by the height on the main chain. Transactions are
understood in hex or base64 format of their hash. Peers may be given as their
peer id in hex or peer address.`);
in hex or base64 format, by the height on the main chain, as 'latest' or as
offset X from the latest block via 'latest-X'. Transactions are understood in
hex or base64 format of their hash. Peers may be given as their peer id in hex
or peer address.

Some features might not be available, depending on the consensus type your
network node is using.`);
}
}

Expand All @@ -883,7 +892,7 @@ function main(args) {
'mining.pool', 'peer', 'peer.json', 'peers', 'peers.json', 'transaction', 'transaction.json',
'transaction.receipt', 'transaction.receipt.json', 'transaction.send', 'transactions',
'transactions.json', 'mempool', 'mempool.content', 'mempool.content.json',
'consensus.min_fee_per_byte', 'log', 'help'];
'consensus.min_fee_per_byte', 'log', 'help', 'exit'];
const hits = completions.filter((c) => c.startsWith(line));
callback(null, [hits.length ? hits : completions, line]);
} else {
Expand Down
7 changes: 4 additions & 3 deletions clients/nodejs/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//protocol: "wss",

// Specify the type of node to run.
// Possible values: "full", "light", "nano"
// Possible values: "full", "light", "nano", "pico"
// Default: "full"
//type: "full",

Expand Down Expand Up @@ -89,7 +89,7 @@
// Possible values: "smart", "nano"
// Default: "smart"
//mode: "smart",

// Optional data including stats about the device.
// The format of this JSON object is defined by the pool operator.
// Possible values: a valid JSON object
Expand Down Expand Up @@ -128,7 +128,7 @@
//password: "secret",
},

// Configure the node miner ui
// Configure the node UI
uiServer: {
// Enable the node UI.
// Possible values: "no", "yes"
Expand Down Expand Up @@ -212,6 +212,7 @@
// - port: the port that the nimiq node runs on.
// - publicKey (optional): the public part of the peer key of this seed node. Should always be set if the node
// is accessed via the internet.
// - protocol (optional): the protocol to use for connecting to the seed node. Possible values: ws or wss.
//{host: 'new-seed.nimiq.com', port: 8080, publicKey: 'e65e39616662f2c16d62dc08915e5a1d104619db8c2b9cf9b389f96c8dce9837'},
//{host: 'nimiq-seed.company-network.int', port: 8080},
],
Expand Down
Loading