Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit a6481d0

Browse files
committed
Handle error for getNetworkInfo grpc api call
1 parent b7b27bf commit a6481d0

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/action/info.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ class InfoAction {
1616
this._notification = notification;
1717
}
1818

19+
/**
20+
* Fetch the network info e.g. number of node in the channel graph as a
21+
* proxy for filter header syncing being completed.
22+
* @return {Promise<undefined>}
23+
*/
24+
async getNetworkInfo() {
25+
try {
26+
const response = await this._grpc.sendCommand('getNetworkInfo');
27+
this._store.numNodes = response.num_nodes;
28+
} catch (err) {
29+
log.error('Getting network info failed', err);
30+
}
31+
}
32+
1933
/**
2034
* Fetches the current details of the lnd node and sets the corresponding
2135
* store parameters. This api is polled at the beginning of app initialization
@@ -31,8 +45,9 @@ class InfoAction {
3145
this._store.pubKey = response.identity_pubkey;
3246
this._store.syncedToChain = response.synced_to_chain;
3347
this._store.blockHeight = response.block_height;
34-
const netRes = await this._grpc.sendCommand('getNetworkInfo');
35-
this._store.isSyncing = !response.synced_to_chain || netRes.num_nodes < 2;
48+
await this.getNetworkInfo();
49+
this._store.isSyncing =
50+
!response.synced_to_chain || this._store.numNodes < 2;
3651
if (this.startingSyncTimestamp === undefined) {
3752
this.startingSyncTimestamp = response.best_header_timestamp || 0;
3853
}
@@ -41,7 +56,7 @@ class InfoAction {
4156
log.info(
4257
`Syncing to chain ...`,
4358
`block height: ${response.block_height}`,
44-
`num nodes: ${netRes.num_nodes}`
59+
`num nodes: ${this._store.numNodes}`
4560
);
4661
this._store.percentSynced = this.calcPercentSynced(response);
4762
}

src/store.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class Store {
2929
percentSynced: 0, // Expects 0-1 range
3030
route: DEFAULT_ROUTE,
3131
blockHeight: null,
32+
numNodes: null,
3233
balanceSatoshis: 0,
3334
confirmedBalanceSatoshis: 0,
3435
unconfirmedBalanceSatoshis: 0,

test/unit/action/info.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ describe('Action Info Unit Tests', () => {
2929
sandbox.restore();
3030
});
3131

32+
describe('getNetworkInfo()', () => {
33+
it('should set number of nodes', async () => {
34+
grpc.sendCommand.withArgs('getNetworkInfo').resolves({
35+
num_nodes: 2,
36+
});
37+
await info.getNetworkInfo();
38+
expect(store.numNodes, 'to equal', 2);
39+
});
40+
41+
it('should log error on failure', async () => {
42+
grpc.sendCommand.rejects();
43+
await info.getNetworkInfo();
44+
expect(logger.error, 'was called once');
45+
});
46+
});
47+
3248
describe('getInfo()', () => {
3349
it('should get public key, synced to chain, and block height', async () => {
3450
grpc.sendCommand.withArgs('getInfo').resolves({
@@ -57,6 +73,16 @@ describe('Action Info Unit Tests', () => {
5773
expect(synced, 'to be', true);
5874
});
5975

76+
it('should return false if chain is synced network info failed', async () => {
77+
grpc.sendCommand.withArgs('getInfo').resolves({
78+
synced_to_chain: true,
79+
});
80+
grpc.sendCommand.withArgs('getNetworkInfo').rejects(new Error('Boom!'));
81+
const synced = await info.getInfo();
82+
expect(synced, 'to be', false);
83+
expect(logger.error, 'was called once');
84+
});
85+
6086
it('should return false if chain is synced but not filter headers', async () => {
6187
grpc.sendCommand.withArgs('getInfo').resolves({
6288
synced_to_chain: true,

0 commit comments

Comments
 (0)