Skip to content

Commit 6c5dae5

Browse files
rsercanosangaman
authored andcommitted
feat(cli): getinfo enhancements (#1170)
Closes #1059.
1 parent b483939 commit 6c5dae5

File tree

17 files changed

+1393
-622
lines changed

17 files changed

+1393
-622
lines changed

docs/api.md

Lines changed: 26 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli/commands/getinfo.ts

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,70 @@ import Table, { VerticalTable } from 'cli-table3';
44
import colors from 'colors/safe';
55
import { GetInfoRequest, GetInfoResponse, LndInfo, RaidenInfo } from '../../proto/xudrpc_pb';
66

7-
const displayChannels = (channels: any, asset: string) => {
8-
const table = new Table() as VerticalTable;
9-
Object.keys(channels).forEach((key: any) => {
10-
table.push({
11-
[colors.blue(key)] : channels[key],
12-
});
13-
});
14-
console.log(colors.underline(colors.bold(`\nLnd ${asset} channels:`)));
15-
console.log(table.toString(), '\n');
16-
};
17-
18-
const displayChainsList = (list: any[], asset: string) => {
19-
const table = new Table() as VerticalTable;
20-
list.forEach((asset, i) => {
21-
if (asset) {
22-
table.push({ [colors.blue(`${i + 1}.`)]: `${asset.chain}-${asset.network}` });
23-
}
24-
});
25-
if (table.length !== 0) {
26-
console.log(colors.underline(colors.bold(`\nLnd ${asset} chains:`)));
27-
console.log(table.toString(), '\n');
28-
}
29-
};
30-
31-
const displayUriList = (uris: string[], asset: string) => {
32-
const table = new Table() as VerticalTable;
33-
uris.forEach((uri, i) => table.push({ [`${i + 1}.`]: uri }));
34-
console.log(colors.underline(colors.bold(`\nLnd ${asset} uris:`)));
35-
console.log(table.toString(), '\n');
36-
};
37-
387
const displayLndInfo = (asset: string, info: LndInfo.AsObject) => {
398
const basicInfotable = new Table() as VerticalTable;
409
basicInfotable.push(
41-
{ [colors.blue('Error')]: info.error },
10+
{ [colors.blue('Status')]: info.status },
4211
);
43-
if (info.blockheight) {
44-
basicInfotable.push({ [colors.blue('Block Height')]: info.blockheight });
45-
}
46-
if (info.version) {
47-
basicInfotable.push({ [colors.blue('Version')]: info.version });
48-
}
49-
if (info.alias) {
50-
basicInfotable.push({ [colors.blue('Alias')] : info.alias });
51-
}
5212

53-
console.log(colors.underline(colors.bold(`\nLnd ${asset} info:`)));
54-
console.log(basicInfotable.toString(), '\n');
13+
const address = info.urisList[0] ? `${info.urisList[0].substring(0, info.urisList[0].indexOf('@'))}
14+
${info.urisList[0].substring(info.urisList[0].indexOf('@'))}` : '';
5515

56-
if (info.channels) {
57-
displayChannels(info.channels, asset);
58-
}
59-
60-
if (!info.error) {
61-
displayChainsList(info.chainsList, asset);
62-
}
16+
basicInfotable.push(
17+
{ [colors.blue('Version')]: info.version || '' },
18+
{ [colors.blue('Address')]: address },
19+
{ [colors.blue('Alias')] : info.alias || '' },
20+
{ [colors.blue('Channels')] :
21+
`Active: ${info.channels ? info.channels['active'] : 0}\
22+
| Pending: ${info.channels ? info.channels['pending'] : 0}\
23+
| Closed: ${info.channels ? info.channels['closed'] : 0}`,
24+
},
25+
{ [colors.blue('Network')] : info.chainsList && info.chainsList.length > 0 ? `${info.chainsList[0].chain} ${info.chainsList[0].network}` : '' },
26+
);
6327

64-
if (info.urisList.length > 0) {
65-
displayUriList(info.urisList, asset);
66-
}
28+
console.log(colors.underline(colors.bold(`\nLND-${asset} Info:`)));
29+
console.log(basicInfotable.toString(), '\n');
6730
};
6831

6932
const displayGeneral = (info: GetInfoResponse.AsObject) => {
7033
const table = new Table() as VerticalTable;
34+
const address = info.urisList[0] ? `${info.urisList[0].substring(0, info.urisList[0].indexOf('@'))}
35+
${info.urisList[0].substring(info.urisList[0].indexOf('@'))}` : '';
36+
7137
table.push(
38+
{ [colors.blue('Alias')]: info.alias },
39+
{ [colors.blue('Node Key')]: info.nodePubKey },
40+
{ [colors.blue('Address')]: address },
41+
{ [colors.blue('Network')]: info.network },
7242
{ [colors.blue('Version')]: info.version },
73-
{ [colors.blue('Pairs')]: info.numPairs },
7443
{ [colors.blue('Peers')]: info.numPeers },
75-
{ [colors.blue('Node key')]: info.nodePubKey },
44+
{ [colors.blue('Pairs')]: info.numPairs },
45+
{ [colors.blue('Own orders')]: info.orders ? info.orders.own : '0' },
46+
{ [colors.blue('Peer orders')]: info.orders ? info.orders.peer : '0' },
47+
{ [colors.blue('Pending swaps')]: info.pendingSwapHashesList ? JSON.stringify(info.pendingSwapHashesList, undefined, 1) : '' },
7648
);
77-
if (info.orders) {
78-
table.push(
79-
{ [colors.blue('Own orders')]: info.orders.own },
80-
{ [colors.blue('Peer orders')]: info.orders.peer },
81-
);
82-
}
83-
if (info.pendingSwapHashesList) {
84-
table.push(
85-
{ [colors.blue('Pending swaps')]: JSON.stringify(info.pendingSwapHashesList) },
86-
);
87-
}
8849
console.log(colors.underline(colors.bold('\nGeneral XUD Info')));
8950
console.log(table.toString(), '\n');
9051
};
9152

9253
const displayRaiden = (info: RaidenInfo.AsObject) => {
9354
const table = new Table() as VerticalTable;
55+
9456
table.push(
57+
{ [colors.blue('Status')]: info.status },
9558
{ [colors.blue('Version')]: info.version },
9659
{ [colors.blue('Address')]: info.address },
97-
{ [colors.blue('Channels')]: info.channels },
98-
{ [colors.blue('Error')]: info.error },
60+
{ [colors.blue('Channels')] :
61+
`Active: ${info.channels ? info.channels['active'] : 0}\
62+
| Pending: 0\
63+
| Closed: ${info.channels ? info.channels['closed'] : 0}`,
64+
},
65+
{ [colors.blue('Network')] : info.chain },
9966
);
67+
10068
console.log(colors.underline(colors.bold('\nRaiden info:')));
10169
console.log(table.toString(), '\n');
70+
10271
};
10372

10473
const displayGetInfo = (response: GetInfoResponse.AsObject) => {

lib/grpc/GrpcService.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ class GrpcService {
397397
response.setNumPairs(getInfoResponse.numPairs);
398398
response.setNumPeers(getInfoResponse.numPeers);
399399
response.setVersion(getInfoResponse.version);
400+
response.setAlias(getInfoResponse.alias);
401+
response.setNetwork(getInfoResponse.network);
400402

401403
const getLndInfo = ((lndInfo: LndInfo): xudrpc.LndInfo => {
402404
const lnd = new xudrpc.LndInfo();
@@ -411,13 +413,14 @@ class GrpcService {
411413
lnd.setChainsList(chains);
412414
}
413415
if (lndInfo.channels) {
414-
const channels = new xudrpc.LndChannels();
416+
const channels = new xudrpc.Channels();
415417
channels.setActive(lndInfo.channels.active);
416418
channels.setPending(lndInfo.channels.pending);
419+
channels.setClosed(lndInfo.channels.closed);
417420
if (lndInfo.channels.inactive) channels.setInactive(lndInfo.channels.inactive);
418421
lnd.setChannels(channels);
419422
}
420-
if (lndInfo.error) lnd.setError(lndInfo.error);
423+
lnd.setStatus(lndInfo.status);
421424
if (lndInfo.uris) lnd.setUrisList(lndInfo.uris);
422425
if (lndInfo.version) lnd.setVersion(lndInfo.version);
423426
if (lndInfo.alias) lnd.setAlias(lndInfo.alias);
@@ -430,10 +433,17 @@ class GrpcService {
430433

431434
if (getInfoResponse.raiden) {
432435
const raiden = new xudrpc.RaidenInfo();
436+
raiden.setStatus(getInfoResponse.raiden.status);
433437
if (getInfoResponse.raiden.address) raiden.setAddress(getInfoResponse.raiden.address);
434-
if (getInfoResponse.raiden.channels) raiden.setChannels(getInfoResponse.raiden.channels);
435-
if (getInfoResponse.raiden.error) raiden.setError(getInfoResponse.raiden.error);
438+
if (getInfoResponse.raiden.channels) {
439+
const channels = new xudrpc.Channels();
440+
channels.setActive(getInfoResponse.raiden.channels.active);
441+
// channels.setSettled(getInfoResponse.raiden.channels.settled);
442+
channels.setClosed(getInfoResponse.raiden.channels.closed);
443+
raiden.setChannels(channels);
444+
}
436445
if (getInfoResponse.raiden.version) raiden.setVersion(getInfoResponse.raiden.version);
446+
if (getInfoResponse.raiden.chain) raiden.setChain(getInfoResponse.raiden.chain);
437447
response.setRaiden(raiden);
438448
}
439449

lib/lndclient/LndClient.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as lndrpc from '../proto/lndrpc_pb';
99
import * as lndinvoices from '../proto/lndinvoices_pb';
1010
import assert from 'assert';
1111
import { promises as fs, watch } from 'fs';
12-
import { SwapState, SwapRole, SwapClientType } from '../constants/enums';
12+
import { SwapRole, SwapClientType, SwapState } from '../constants/enums';
1313
import { SwapDeal } from '../swaps/types';
1414
import { base64ToHex, hexToUint8Array } from '../utils/utils';
1515
import { LndClientConfig, LndInfo, ChannelCount, Chain, ClientMethods } from './types';
@@ -217,32 +217,38 @@ class LndClient extends SwapClient {
217217
let blockheight: number | undefined;
218218
let uris: string[] | undefined;
219219
let version: string | undefined;
220-
let error: string | undefined;
221220
let alias: string | undefined;
221+
let status = 'Ready';
222222
if (this.isDisabled()) {
223-
error = errors.LND_IS_DISABLED.message;
223+
status = errors.LND_IS_DISABLED.message;
224224
} else if (!this.isConnected()) {
225-
error = errors.LND_IS_UNAVAILABLE(this.status).message;
225+
status = errors.LND_IS_UNAVAILABLE(this.status).message;
226226
} else {
227227
try {
228-
const lnd = await this.getInfo();
228+
const getInfoResponse = await this.getInfo();
229+
const closedChannelsResponse = await this.getClosedChannels();
229230
channels = {
230-
active: lnd.getNumActiveChannels(),
231-
pending: lnd.getNumPendingChannels(),
231+
active: getInfoResponse.getNumActiveChannels(),
232+
inactive: getInfoResponse.getNumInactiveChannels(),
233+
pending: getInfoResponse.getNumPendingChannels(),
234+
closed: closedChannelsResponse.getChannelsList().length,
232235
};
233-
chains = lnd.getChainsList().map(value => value.toObject());
234-
blockheight = lnd.getBlockHeight(),
235-
uris = lnd.getUrisList(),
236-
version = lnd.getVersion();
237-
alias = lnd.getAlias();
236+
chains = getInfoResponse.getChainsList().map(value => value.toObject());
237+
blockheight = getInfoResponse.getBlockHeight();
238+
uris = getInfoResponse.getUrisList();
239+
version = getInfoResponse.getVersion();
240+
alias = getInfoResponse.getAlias();
241+
if (channels.active <= 0) {
242+
status = errors.LND_HAS_NO_ACTIVE_CHANNELS().message;
243+
}
238244
} catch (err) {
239245
this.logger.error('getinfo error', err);
240-
error = err.message;
246+
status = err.message;
241247
}
242248
}
243249

244250
return {
245-
error,
251+
status,
246252
channels,
247253
chains,
248254
blockheight,
@@ -407,6 +413,13 @@ class LndClient extends SwapClient {
407413
return this.unaryCall<lndrpc.GetInfoRequest, lndrpc.GetInfoResponse>('getInfo', new lndrpc.GetInfoRequest());
408414
}
409415

416+
/**
417+
* Returns closed channels that this node was a participant in.
418+
*/
419+
public getClosedChannels = (): Promise<lndrpc.ClosedChannelsResponse> => {
420+
return this.unaryCall<lndrpc.ClosedChannelsRequest, lndrpc.ClosedChannelsResponse>('closedChannels', new lndrpc.ClosedChannelsRequest());
421+
}
422+
410423
public sendSmallestAmount = async (rHash: string, destination: string): Promise<string> => {
411424
const request = this.buildSendRequest({
412425
rHash,

lib/lndclient/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const codesPrefix = errorCodesPrefix.LND;
55
const errorCodes = {
66
LND_IS_DISABLED: codesPrefix.concat('.1'),
77
LND_IS_UNAVAILABLE: codesPrefix.concat('.2'),
8+
LND_HAS_NO_ACTIVE_CHANNELS: codesPrefix.concat('.3'),
89
};
910

1011
const errors = {
@@ -16,6 +17,10 @@ const errors = {
1617
message: `lnd is ${ClientStatus[status]}`,
1718
code: errorCodes.LND_IS_UNAVAILABLE,
1819
}),
20+
LND_HAS_NO_ACTIVE_CHANNELS: () => ({
21+
message: 'lnd has no active channels',
22+
code: errorCodes.LND_HAS_NO_ACTIVE_CHANNELS,
23+
}),
1924
};
2025

2126
export { errorCodes };

lib/lndclient/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** The configurable options for the lnd client. */
2+
23
export type LndClientConfig = {
34
disable: boolean;
45
certpath: string;
@@ -11,6 +12,7 @@ export type LndClientConfig = {
1112

1213
/** General information about the state of this lnd client. */
1314
export type LndInfo = {
15+
status: string;
1416
error?: string;
1517
channels?: ChannelCount;
1618
chains?: Chain[];
@@ -24,6 +26,7 @@ export type ChannelCount = {
2426
active: number,
2527
inactive?: number,
2628
pending: number,
29+
closed: number,
2730
};
2831

2932
export type Chain = {

0 commit comments

Comments
 (0)