diff --git a/src/nodes/NodeManager.ts b/src/nodes/NodeManager.ts index 740c78aea4..d8f7617b2e 100644 --- a/src/nodes/NodeManager.ts +++ b/src/nodes/NodeManager.ts @@ -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); @@ -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); } } diff --git a/tests/nodes/NodeManager.test.ts b/tests/nodes/NodeManager.test.ts index 955bf47217..5313dd9abf 100644 --- a/tests/nodes/NodeManager.test.ts +++ b/tests/nodes/NodeManager.test.ts @@ -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, { @@ -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