Skip to content

Commit

Permalink
feat: NodeManager.setNode authenticates the added node
Browse files Browse the repository at this point in the history
`setNode` now authenticates the node you are trying to add. Added a flag for skipping this authentication as well as a timeout timer for the authentication. this is shared between authentication new node and the old node if the bucket is full.

Related #322
  • Loading branch information
tegefaulkes authored and emmacasolin committed Jun 14, 2022
1 parent fa39cd5 commit e925034
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
16 changes: 4 additions & 12 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
public async pingNode(nodeId: NodeId, address?: NodeAddress, timer?: Timer): Promise<boolean> {
// 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 ||
Expand Down
26 changes: 16 additions & 10 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
return this.nodeConnectionManager.pingNode(nodeId, address, timeout);
public async pingNode(nodeId: NodeId, address?: NodeAddress, timer?: Timer): Promise<boolean> {
return this.nodeConnectionManager.pingNode(nodeId, address, timer);
}

/**
Expand Down Expand Up @@ -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<void> {
// 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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e925034

Please sign in to comment.