Skip to content

Commit

Permalink
fix: squash into ping node changes.
Browse files Browse the repository at this point in the history
Updated `NodeConnectionManager.pingNode` to just use the proxy connection.

#322
  • Loading branch information
tegefaulkes authored and emmacasolin committed Jun 14, 2022
1 parent 7815b2e commit 9505e20
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
72 changes: 35 additions & 37 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,9 @@ class NodeConnectionManager {
public async acquireConnection(
targetNodeId: NodeId,
timer?: Timer,
address?: NodeAddress,
): Promise<ResourceAcquire<NodeConnection<GRPCClientAgent>>> {
return async () => {
const { connection, timer } = await this.getConnection(
targetNodeId,
address,
timer,
);
const { connection, timer } = await this.getConnection(targetNodeId, timer);
// Acquire the read lock and the release function
const [release] = await this.connectionLocks.lock([
targetNodeId.toString(),
Expand Down Expand Up @@ -185,7 +180,7 @@ class NodeConnectionManager {
timer?: Timer,
): Promise<T> {
return await withF(
[await this.acquireConnection(targetNodeId, timer, undefined)],
[await this.acquireConnection(targetNodeId, timer)],
async ([conn]) => {
this.logger.info(
`withConnF calling function with connection to ${nodesUtils.encodeNodeId(
Expand Down Expand Up @@ -214,11 +209,7 @@ class NodeConnectionManager {
) => AsyncGenerator<T, TReturn, TNext>,
timer?: Timer,
): AsyncGenerator<T, TReturn, TNext> {
const acquire = await this.acquireConnection(
targetNodeId,
timer,
undefined,
);
const acquire = await this.acquireConnection(targetNodeId, timer);
const [release, conn] = await acquire();
let caughtError;
try {
Expand All @@ -236,13 +227,11 @@ class NodeConnectionManager {
* Create a connection to another node (without performing any function).
* This is a NOOP if a connection already exists.
* @param targetNodeId Id of node we are creating connection to
* @param address Optional address to connect to
* @param timer Connection timeout timer
* @returns ConnectionAndLock that was created or exists in the connection map
*/
protected async getConnection(
targetNodeId: NodeId,
address?: NodeAddress,
timer?: Timer,
): Promise<ConnectionAndTimer> {
this.logger.info(
Expand Down Expand Up @@ -467,7 +456,7 @@ class NodeConnectionManager {
// call to getConnectionToNode
// FIXME: no tran
await this.nodeGraph.setNode(nextNodeId, nextNodeAddress.address);
await this.getConnection(nextNodeId, undefined, timer);
await this.getConnection(nextNodeId, timer);
} catch (e) {
// If we can't connect to the node, then skip it
continue;
Expand Down Expand Up @@ -581,7 +570,7 @@ class NodeConnectionManager {
for (const seedNodeId of this.getSeedNodes()) {
// Check if the connection is viable
try {
await this.getConnection(seedNodeId, undefined, timer);
await this.getConnection(seedNodeId, timer);
} catch (e) {
if (e instanceof nodesErrors.ErrorNodeConnectionTimeout) continue;
throw e;
Expand Down Expand Up @@ -675,39 +664,48 @@ class NodeConnectionManager {
* connection can be authenticated, it's certificate matches the nodeId and
* the addresses match if provided. Otherwise returns false.
* @param nodeId - NodeId of the target
* @param address - Optional address of the target
* @param host - Host of the target node
* @param port - Port of the target node
* @param timer Connection timeout timer
*/
@ready(new nodesErrors.ErrorNodeConnectionManagerNotRunning())
public async pingNode(
nodeId: NodeId,
address?: NodeAddress,
host: Host,
port: Port,
timer?: Timer,
): Promise<boolean> {
// If we can create a connection then we have punched though the NAT,
// authenticated and confirmed the nodeId matches
let connAndLock: ConnectionAndLock;
const proxyAddress = networkUtils.buildAddress(
this.proxy.getProxyHost(),
this.proxy.getProxyPort(),
);
const signature = await this.keyManager.signWithRootKeyPair(
Buffer.from(proxyAddress),
);
const holePunchPromises = Array.from(this.getSeedNodes(), (seedNodeId) => {
return this.sendHolePunchMessage(
seedNodeId,
this.keyManager.getNodeId(),
nodeId,
proxyAddress,
signature,
);
});
const forwardPunchPromise = this.holePunchForward(
nodeId,
host,
port,
timer,
);

try {
connAndLock = await this.createConnection(nodeId, address, timer);
await Promise.all([forwardPunchPromise, ...holePunchPromises]);
} catch (e) {
if (
e instanceof nodesErrors.ErrorNodeConnectionDestroyed ||
e instanceof nodesErrors.ErrorNodeConnectionTimeout ||
e instanceof grpcErrors.ErrorGRPC ||
e instanceof agentErrors.ErrorAgentClientDestroyed
) {
// Failed to connect, returning false
return false;
}
throw e;
return false;
}
const remoteHost = connAndLock.connection?.host;
const remotePort = connAndLock.connection?.port;
// If address wasn't set then nothing to check
if (address == null) return true;
// Check if the address information match in case there was an
// existing connection
return address.host === remoteHost && address.port === remotePort;
return true;
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Timer } from '../types';
import Logger from '@matrixai/logger';
import * as nodesErrors from './errors';
import * as nodesUtils from './utils';
import * as networkUtils from '../network/utils';
import * as validationUtils from '../validation/utils';
import * as utilsPB from '../proto/js/polykey/v1/utils/utils_pb';
import * as claimsErrors from '../claims/errors';
Expand Down Expand Up @@ -60,7 +61,17 @@ class NodeManager {
address?: NodeAddress,
timer?: Timer,
): Promise<boolean> {
return this.nodeConnectionManager.pingNode(nodeId, address, timer);
// We need to attempt a connection using the proxies
// For now we will just do a forward connect + relay message
const targetAddress =
address ?? (await this.nodeConnectionManager.findNode(nodeId));
const targetHost = await networkUtils.resolveHost(targetAddress.host);
return await this.nodeConnectionManager.pingNode(
nodeId,
targetHost,
targetAddress.port,
timer,
);
}

/**
Expand Down

0 comments on commit 9505e20

Please sign in to comment.