Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Discovery Support with MDNS #53

Merged
merged 2 commits into from
Nov 9, 2023
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
120 changes: 81 additions & 39 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
Expand Up @@ -109,7 +109,7 @@
"@matrixai/errors": "^1.2.0",
"@matrixai/logger": "^3.1.0",
"commander": "^8.3.0",
"polykey": "^1.2.1-alpha.19",
"polykey": "^1.2.1-alpha.25",
"threads": "^1.6.5",
"@swc/core": "1.3.82",
"@swc/jest": "^0.2.29",
Expand Down
37 changes: 21 additions & 16 deletions src/nodes/CommandFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class CommandFind extends CommandPolykey {
success: false,
message: '',
id: '',
host: '',
port: 0,
addresses: [] as Array<{ host: string; port: number }>,
};
const builtAddresses: Array<string> = [];
try {
const response = await binUtils.retryAuthentication(
(auth) =>
Expand All @@ -69,12 +69,13 @@ class CommandFind extends CommandPolykey {
);
result.success = true;
result.id = nodesUtils.encodeNodeId(nodeId);
result.host = response.host;
result.port = response.port;
result.message = `Found node at ${networkUtils.buildAddress(
result.host as Host,
result.port as Port,
)}`;
for (const { host, port } of response.addresses) {
result.addresses.push({ host, port });
builtAddresses.push(
networkUtils.buildAddress(host as Host, port as Port),
);
}
result.message = `Found node at ${builtAddresses.join(', ')}`;
} catch (err) {
if (
!(err.cause instanceof nodesErrors.ErrorNodeGraphNodeIdNotFound)
Expand All @@ -84,16 +85,20 @@ class CommandFind extends CommandPolykey {
// Else failed to find the node.
result.success = false;
result.id = nodesUtils.encodeNodeId(nodeId);
result.host = '';
result.port = 0;
result.message = `Failed to find node ${result.id}`;
}
let output: any = result;
if (options.format === 'human') output = [result.message];
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
let outputFormatted: string | Uint8Array;
if (options.format === 'json') {
outputFormatted = binUtils.outputFormatter({
type: 'json',
data: result,
});
} else {
outputFormatted = binUtils.outputFormatter({
type: 'list',
data: ['Found node at', ...builtAddresses],
});
}
process.stdout.write(outputFormatted);
// Like ping it should error when failing to find node for automation reasons.
if (!result.success) {
Expand Down
2 changes: 2 additions & 0 deletions tests/agent/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ describe('start', () => {
[seedNodeId2]: {
host: seedNodeHost2 as Host,
port: seedNodePort2 as Port,
scopes: ['global'],
},
},
testnet: {},
Expand Down Expand Up @@ -998,6 +999,7 @@ describe('start', () => {
[seedNodeId2]: {
host: seedNodeHost2 as Host,
port: seedNodePort2 as Port,
scopes: ['global'],
},
},
});
Expand Down
12 changes: 10 additions & 2 deletions tests/nodes/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ describe('add', () => {
expect(exitCode).toBe(0);
// Checking if node was added.
const node = await pkAgent.nodeGraph.getNode(validNodeId);
expect(node?.address).toEqual({ host: validHost, port: port });
expect(node?.address).toEqual({
host: validHost,
port: port,
scopes: ['global'],
});
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand Down Expand Up @@ -197,7 +201,11 @@ describe('add', () => {
expect(exitCode).toBe(0);
// Checking if node was added.
const node = await pkAgent.nodeGraph.getNode(validNodeId);
expect(node?.address).toEqual({ host: validHost, port: port });
expect(node?.address).toEqual({
host: validHost,
port: port,
scopes: ['global'],
});
},
);
});
41 changes: 31 additions & 10 deletions tests/nodes/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,24 @@ describe('find', () => {
},
);
expect(exitCode).toBe(0);
expect(JSON.parse(stdout)).toEqual({
const output = JSON.parse(stdout);
expect(output).toMatchObject({
success: true,
message: `Found node at ${remoteOnlineHost}:${remoteOnlinePort}`,
id: nodesUtils.encodeNodeId(remoteOnlineNodeId),
host: remoteOnlineHost,
port: remoteOnlinePort,
});
expect(output.addresses).toEqual(
expect.arrayContaining([
{
host: remoteOnlineHost,
port: remoteOnlinePort,
},
]),
);
expect(output.message).toMatch(
new RegExp(
`Found node at .*?${remoteOnlineHost}:${remoteOnlinePort}.*?`,
),
);
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand All @@ -145,13 +156,24 @@ describe('find', () => {
},
);
expect(exitCode).toBe(0);
expect(JSON.parse(stdout)).toEqual({
const output = JSON.parse(stdout);
expect(output).toMatchObject({
success: true,
message: `Found node at ${remoteOfflineHost}:${remoteOfflinePort}`,
id: nodesUtils.encodeNodeId(remoteOfflineNodeId),
host: remoteOfflineHost,
port: remoteOfflinePort,
});
expect(output.addresses).toEqual(
expect.arrayContaining([
{
host: remoteOfflineHost,
port: remoteOfflinePort,
},
]),
);
expect(output.message).toMatch(
new RegExp(
`Found node at .*?${remoteOfflineHost}:${remoteOfflinePort}.*?`,
),
);
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand Down Expand Up @@ -183,8 +205,7 @@ describe('find', () => {
unknownNodeId!,
)}`,
id: nodesUtils.encodeNodeId(unknownNodeId!),
host: '',
port: 0,
addresses: [],
});
},
globalThis.failedConnectionTimeout,
Expand Down
Loading