Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polykey",
"version": "1.21.2",
"version": "1.21.3",
"homepage": "https://polykey.com",
"author": "Matrix AI",
"contributors": [
Expand Down
28 changes: 25 additions & 3 deletions src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,11 +1525,23 @@ class NodeConnectionManager {
}> {
// Need to get the connection details of the requester and add it to the message.
// Then send the message to the target.
// This would only function with existing connections
const existingConnection = this.getConnection(targetNodeId);
// This would only function with existing connections that are authenticated
const nodeIdString = targetNodeId.toString() as NodeIdString;
const connectionsEntry = this.connections.get(nodeIdString);
if (connectionsEntry == null) {
throw new nodesErrors.ErrorNodeConnectionManagerConnectionNotFound();
}
const existingConnection =
connectionsEntry.connections[connectionsEntry.activeConnection];
if (existingConnection == null) {
throw new nodesErrors.ErrorNodeConnectionManagerConnectionNotFound();
}
if (connectionsEntry.authenticatedForward !== AuthenticatingState.SUCCESS) {
throw new nodesErrors.ErrorNodeConnectionManagerConnectionNotFound();
}
if (connectionsEntry.authenticatedReverse !== AuthenticatingState.SUCCESS) {
throw new nodesErrors.ErrorNodeConnectionManagerConnectionNotFound();
}
const host = existingConnection.connection.host;
const port = existingConnection.connection.port;
// Do other checks.
Expand Down Expand Up @@ -1598,7 +1610,17 @@ class NodeConnectionManager {
limit: number = this.connectionGetClosestLimit,
): Array<ActiveConnectionsInfo> {
const nodeIds: Array<NodeId> = [];
for (const nodeIdString of this.connections.keys()) {
for (const [nodeIdString, connectionsEntry] of this.connections.entries()) {
if (
connectionsEntry.authenticatedForward !== AuthenticatingState.SUCCESS
) {
continue;
}
if (
connectionsEntry.authenticatedReverse !== AuthenticatingState.SUCCESS
) {
continue;
}
nodeIds.push(IdInternal.fromString<NodeId>(nodeIdString));
}
// Sort and draw limit
Expand Down
14 changes: 13 additions & 1 deletion tests/nodes/NodeConnectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe(`${NodeConnectionManager.name}`, () => {
);
const localHost = '127.0.0.1' as Host;
const dummyManifest = {} as AgentServerManifest;
const timeoutTime = 2000;
const timeoutTime = 5000;

test('NodeConnectionManager readiness', async () => {
const keyPair = keysUtils.generateKeyPair();
Expand Down Expand Up @@ -1080,11 +1080,13 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmPeer1.nodeConnectionManager.createConnection(
[ncmPeer2.nodeId],
localHost,
ncmPeer2.port,
);
await ncmPeer1.nodeConnectionManager.isAuthenticatedP(ncmPeer2.nodeId);

// Should be able to create connection from local to peer2 using peer1 as signaller
await ncmLocal.nodeConnectionManager.createConnectionPunch(
Expand Down Expand Up @@ -1113,6 +1115,7 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
// Can't signal without signaler connected
await testsUtils.expectRemoteError(
ncmLocal.nodeConnectionManager.createConnectionPunch(
Expand All @@ -1129,11 +1132,13 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmPeer1.nodeConnectionManager.createConnection(
[ncmPeer2.nodeId],
localHost,
ncmPeer2.port,
);
await ncmPeer1.nodeConnectionManager.isAuthenticatedP(ncmPeer2.nodeId);
const holePunchSpy = jest.spyOn(
ncmPeer2.nodeConnectionManager,
'holePunch',
Expand Down Expand Up @@ -1168,16 +1173,19 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmLocal.nodeConnectionManager.createConnection(
[ncmPeer1.nodeId],
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmLocal.nodeConnectionManager.createConnection(
[ncmPeer2.nodeId],
localHost,
ncmPeer2.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer2.nodeId);

const result = ncmLocal.nodeConnectionManager.getClosestConnections(
ncmPeer2.nodeId,
Expand All @@ -1193,11 +1201,13 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmPeer1.nodeConnectionManager.createConnection(
[ncmPeer2.nodeId],
localHost,
ncmPeer2.port,
);
await ncmPeer1.nodeConnectionManager.isAuthenticatedP(ncmPeer2.nodeId);

// Mock and block `handleNodesConnectionSignalFinal`
const mockedHandleNodesConnectionSignalFinal = jest.spyOn(
Expand Down Expand Up @@ -1227,11 +1237,13 @@ describe(`${NodeConnectionManager.name}`, () => {
localHost,
ncmPeer1.port,
);
await ncmLocal.nodeConnectionManager.isAuthenticatedP(ncmPeer1.nodeId);
await ncmPeer1.nodeConnectionManager.createConnection(
[ncmPeer2.nodeId],
localHost,
ncmPeer2.port,
);
await ncmPeer1.nodeConnectionManager.isAuthenticatedP(ncmPeer2.nodeId);
// Excessive connections will fail due to rate limit
const connectionsP = (async () => {
const connectionPs: Array<Promise<NodeConnection>> = [];
Expand Down
16 changes: 15 additions & 1 deletion tests/nodes/NodeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe(`${NodeManager.name}`, () => {
]);
const password = 'password';
const localHost = '127.0.0.1' as Host;
const timeoutTime = 300;
const timeoutTime = 1000;
const dummyAgentService = {
nodesAuthenticateConnection: new DummyNodesAuthenticateConnection({}),
} as AgentServerManifest;
Expand Down Expand Up @@ -1185,6 +1185,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmB.port,
);
await ncmA.nodeConnectionManager.isAuthenticatedP(ncmB.nodeId);
}
async function quickLinkConnection(structure: Array<Array<number>>) {
const linkPs: Array<Promise<void>> = [];
Expand Down Expand Up @@ -1411,6 +1412,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const result = await nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1442,6 +1444,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const result = await nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1473,6 +1476,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const result = await nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1504,6 +1508,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const resultP = nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1532,6 +1537,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const resultP = nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1573,6 +1579,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const rateLimiter = new Semaphore(3);
const resultP = nodeManager.findNodeBySignal(
Expand Down Expand Up @@ -1847,6 +1854,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const result = await nodeManager.findNode({
nodeId: ncmPeers[4].nodeId,
Expand Down Expand Up @@ -1874,6 +1882,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const result = await nodeManager.findNode({
nodeId: ncmPeers[4].nodeId,
Expand Down Expand Up @@ -1901,6 +1910,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const result = await nodeManager.findNode({
nodeId: keyRing.getNodeId(),
Expand Down Expand Up @@ -1936,6 +1946,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

const start = Date.now();
const result = await nodeManager.findNode({
Expand Down Expand Up @@ -2024,6 +2035,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

await Promise.all([
await ncmPeers[3].nodeConnectionManager.stop({ force: true }),
Expand Down Expand Up @@ -2101,6 +2113,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

await nodeManager.refreshBucket(100, 1000);
// Small networks less than 20 nodes will contact all nodes
Expand Down Expand Up @@ -2133,6 +2146,7 @@ describe(`${NodeManager.name}`, () => {
localHost,
ncmPeers[0].port,
);
await nodeConnectionManager.isAuthenticatedP(ncmPeers[0].nodeId);

expect(await nodeGraph.nodesTotal()).toBe(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as nodesUtils from '@/nodes/utils';
import * as testsUtils from '../../../utils';

describe('nodesClosestLocalNode', () => {
const logger = new Logger('nodesClosestLocalNode test', LogLevel.WARN, [
const logger = new Logger('nodesClosestLocalNode test', LogLevel.INFO, [
new StreamHandler(),
]);
const localHost = '127.0.0.1' as Host;
Expand Down Expand Up @@ -143,6 +143,8 @@ describe('nodesClosestLocalNode', () => {
const connectionId = `connectionId-${i}`;
const entry = {
activeConnection: connectionId,
authenticatedForward: 2,
authenticatedReverse: 2,
connections: {
[connectionId]: {
connection: {
Expand Down