diff --git a/src/nodes/NodeConnectionManager.ts b/src/nodes/NodeConnectionManager.ts index c190c20d3..9d1f7302c 100644 --- a/src/nodes/NodeConnectionManager.ts +++ b/src/nodes/NodeConnectionManager.ts @@ -673,23 +673,15 @@ class NodeConnectionManager { * the addresses match if provided. Otherwise returns false. * @param nodeId - NodeId of the target * @param address - Optional address of the target - * @param connConnectTime - Optional timeout for making the connection. + * @param timer Connection timeout timer */ @ready(new nodesErrors.ErrorNodeConnectionManagerNotRunning()) - public async pingNode( - nodeId: NodeId, - address?: NodeAddress, - connConnectTime?: number, - ): Promise { + public async pingNode(nodeId: NodeId, address?: NodeAddress, timer?: Timer): Promise { // If we can create a connection then we have punched though the NAT, - // authenticated and confimed the nodeId matches + // authenticated and confirmed the nodeId matches let connAndLock: ConnectionAndLock; try { - connAndLock = await this.createConnection( - nodeId, - address, - connConnectTime, - ); + connAndLock = await this.createConnection(nodeId, address, timer); } catch (e) { if ( e instanceof nodesErrors.ErrorNodeConnectionDestroyed || diff --git a/src/nodes/NodeManager.ts b/src/nodes/NodeManager.ts index 96db367a8..9ac3eac2c 100644 --- a/src/nodes/NodeManager.ts +++ b/src/nodes/NodeManager.ts @@ -55,14 +55,10 @@ class NodeManager { * @return true if online, false if offline * @param nodeId - NodeId of the node we're pinging * @param address - Optional Host and Port we want to ping - * @param timeout - Optional timeout + * @param timer Connection timeout timer */ - public async pingNode( - nodeId: NodeId, - address?: NodeAddress, - timeout?: number, - ): Promise { - return this.nodeConnectionManager.pingNode(nodeId, address, timeout); + public async pingNode(nodeId: NodeId, address?: NodeAddress, timer?: Timer): Promise { + return this.nodeConnectionManager.pingNode(nodeId, address, timer); } /** @@ -337,14 +333,24 @@ class NodeManager { /** * Adds a node to the node graph. * Updates the node if the node already exists. - * + * @param nodeId - Id of the node we wish to add + * @param nodeAddress - Expected address of the node we want to add + * @param authenticate - Flag for if we want to authenticate the node we're adding + * @param force - Flag for if we want to add the node without authenticating or if the bucket is full. + * This will drop the oldest node in favor of the new. + * @param timer Connection timeout timer */ public async setNode( nodeId: NodeId, nodeAddress: NodeAddress, - force = false, + authenticate: boolean = true, + force: boolean = false, + timer?: Timer, tran: DBTransaction, ): Promise { + // if we fail to ping and authenticate the new node we return + // skip if force is true or authenticate is false + if (!force && authenticate && !(await this.pingNode(nodeId, nodeAddress, timer))) return // When adding a node we need to handle 3 cases // 1. The node already exists. We need to update it's last updated field // 2. The node doesn't exist and bucket has room. @@ -372,7 +378,7 @@ class NodeManager { bucketIndex, tran, ))!; - if ((await this.pingNode(oldestNodeId)) && !force) { + if ((await this.pingNode(oldestNodeId, undefined, timer)) && !force) { // The node responded, we need to update it's info and drop the new node const oldestNode = (await this.nodeGraph.getNode(oldestNodeId, tran))!; await this.nodeGraph.setNode(oldestNodeId, oldestNode.address, tran);