Skip to content

Commit

Permalink
fix: findNode and getClosestGlobalNodes no longer throws `ErrorNo…
Browse files Browse the repository at this point in the history
…deGraphEmptyDatabase` with empty network

`getClosestGlobalNodes` was throwing `ErrorNodeGraphEmptyDatabase` when it failed to get new nodes during the search process. Now it just returns undefined as expected.

Related #398
  • Loading branch information
tegefaulkes committed Jul 12, 2022
1 parent 048e5d9 commit 8990017
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,10 @@ class NodeConnectionManager {
this.initialClosestNodes,
);
// If we have no nodes at all in our database (even after synchronising),
// then we should throw an eor. We aren't going to find any others
// then we should return nothing. We aren't going to find any others
if (shortlist.length === 0) {
throw new nodesErrors.ErrorNodeGraphEmptyDatabase();
this.logger.warn('Node graph was empty, No nodes to query');
return;
}
// Need to keep track of the nodes that have been contacted
// Not sufficient to simply check if there's already a pre-existing connection
Expand Down
6 changes: 0 additions & 6 deletions src/nodes/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class ErrorNodeGraphNodeIdNotFound<T> extends ErrorNodes<T> {
exitCode = sysexits.NOUSER;
}

class ErrorNodeGraphEmptyDatabase<T> extends ErrorNodes<T> {
static description = 'NodeGraph database was empty';
exitCode = sysexits.USAGE;
}

class ErrorNodeGraphOversizedBucket<T> extends ErrorNodes<T> {
static description: 'Bucket invalidly contains more nodes than capacity';
exitCode = sysexits.USAGE;
Expand Down Expand Up @@ -101,7 +96,6 @@ export {
ErrorNodeGraphNotRunning,
ErrorNodeGraphDestroyed,
ErrorNodeGraphNodeIdNotFound,
ErrorNodeGraphEmptyDatabase,
ErrorNodeGraphOversizedBucket,
ErrorNodeGraphSameNodeId,
ErrorNodeGraphBucketIndex,
Expand Down
4 changes: 2 additions & 2 deletions tests/bin/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ describe('bootstrap', () => {
});
});
await new Promise((res) => {
bootstrapProcess1.once('exit', () => res(null))
})
bootstrapProcess1.once('exit', () => res(null));
});
// Attempting to bootstrap should fail with existing state
const bootstrapProcess2 = await testBinUtils.pkStdio(
[
Expand Down
20 changes: 19 additions & 1 deletion tests/nodes/NodeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ describe(`${NodeManager.name} test`, () => {
'refreshBucket',
);
try {
logger.setLevel(LogLevel.DEBUG);
logger.setLevel(LogLevel.WARN);
await queue.start();
await nodeManager.start();
await nodeConnectionManager.start({ nodeManager });
Expand Down Expand Up @@ -1145,4 +1145,22 @@ describe(`${NodeManager.name} test`, () => {
await queue.stop();
}
});
test('refreshBucket should not throw errors when network is empty', async () => {
const nodeManager = new NodeManager({
db,
sigchain: {} as Sigchain,
keyManager,
nodeGraph,
nodeConnectionManager,
queue,
refreshBucketTimerDefault: 10000000,
logger,
});
await nodeConnectionManager.start({ nodeManager });
try {
await expect(nodeManager.refreshBucket(100)).resolves.not.toThrow();
} finally {
await nodeManager.stop();
}
});
});

0 comments on commit 8990017

Please sign in to comment.