Skip to content

Commit

Permalink
fix: fix into 7cc74d0
Browse files Browse the repository at this point in the history
Relates #359
  • Loading branch information
tegefaulkes authored and CMCDragonkai committed Mar 26, 2022
1 parent b3dd668 commit 97077e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,15 @@ class NodeManager {
// If this is a new entry, check the bucket limit
const [bucketIndex, ] = this.nodeGraph.bucketIndex(nodeId)
const count = await this.nodeGraph.getBucketMetaProp(bucketIndex, 'count');
if (nodeData == null && count >= this.nodeGraph.nodeBucketLimit) {
if (nodeData != null || count < this.nodeGraph.nodeBucketLimit) {
// Either already exists or has room in the bucket
// We want to add or update the node
await this.nodeGraph.setNode(nodeId, nodeAddress);
} else {
// We want to add a node but the bucket is full
// We need to ping the oldest node
const oldestNodeId = (await this.nodeGraph.getOldestNode(bucketIndex))!;
if (await this.pingNode(oldestNodeId) && !force){
if (await this.pingNode(oldestNodeId) && !force) {
// The node responded, we need to update it's info and drop the new node
const oldestNode = (await this.nodeGraph.getNode(oldestNodeId))!;
await this.nodeGraph.setNode(oldestNodeId, oldestNode.address);
Expand All @@ -367,10 +371,6 @@ class NodeManager {
await this.nodeGraph.unsetNode(oldestNodeId);
await this.nodeGraph.setNode(nodeId, nodeAddress);
}
} else {
// Either already exists or has room in the bucket
// We want to add or update the node
await this.nodeGraph.setNode(nodeId, nodeAddress);
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/nodes/NodeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe(`${NodeManager.name} test`, () => {
});

const nodeData = (await nodeGraph.getNode(nodeId))!;
await sleep(100);
await sleep(1100);

// should update the node
await nodeManager.setNode(nodeId, {
Expand Down Expand Up @@ -481,6 +481,8 @@ describe(`${NodeManager.name} test`, () => {
nodeManagerPingMock.mockResolvedValue(true);
const oldestNodeId = await nodeGraph.getOldestNode(bucketIndex);
const oldestNode = await nodeGraph.getNode(oldestNodeId!);
// waiting for a second to tick over
await sleep(1100);
// adding a new node with bucket full
await nodeManager.setNode(nodeId, {port: 55555} as NodeAddress);
// bucket still contains max nodes
Expand Down

0 comments on commit 97077e1

Please sign in to comment.