Skip to content

Commit 6d8c2cd

Browse files
committed
chore: added network to syncNodeGraphHandler
1 parent 307d46e commit 6d8c2cd

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/PolykeyAgent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ class PolykeyAgent {
774774
});
775775
const initialNodesShortlist = initialNodes.slice(0, 3);
776776
await this.nodeManager.syncNodeGraph(
777+
options.network,
777778
initialNodesShortlist,
778779
undefined,
779780
false,

src/nodes/NodeManager.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
253253
protected syncNodeGraphHandler = async (
254254
ctx: ContextTimed,
255255
_taskInfo: TaskInfo | undefined,
256+
network: string | undefined,
256257
initialNodes: Array<[NodeIdEncoded, NodeAddress]>,
257258
connectionConnectTimeoutTime: number | undefined,
258259
) => {
@@ -299,6 +300,14 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
299300
}
300301
if (ctx.signal.aborted) return;
301302

303+
if (network != null) {
304+
try {
305+
await this.claimNetwork(successfulConnections[0].value.nodeId, network);
306+
} catch {
307+
await this.switchNetwork(network);
308+
}
309+
}
310+
302311
// Attempt a findNode operation looking for ourselves
303312
await this.findNode(
304313
{
@@ -1877,10 +1886,10 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
18771886
);
18781887

18791888
// Error out if a network access claim already exists
1880-
if (await this.getClaimNetworkAccess(network, tran) != null) {
1881-
throw new Error('TMP network access claim already exists')
1889+
if ((await this.getClaimNetworkAccess(network, tran)) != null) {
1890+
throw new Error('TMP network access claim already exists');
18821891
}
1883-
1892+
18841893
const encodedNetworkAuthority = claimsUtils.generateSignedClaim(
18851894
claimNetworkAuthority.toSigned(),
18861895
);
@@ -2793,6 +2802,7 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
27932802
*
27942803
*/
27952804
public syncNodeGraph(
2805+
network: string | undefined,
27962806
initialNodes: Array<[NodeId, NodeAddress]>,
27972807
connectionConnectTimeoutTime?: number,
27982808
blocking?: boolean,
@@ -2801,6 +2811,7 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
28012811
@startStop.ready(new nodesErrors.ErrorNodeManagerNotRunning())
28022812
@decorators.timedCancellable(true)
28032813
public async syncNodeGraph(
2814+
network: string,
28042815
initialNodes: Array<[NodeId, NodeAddress]>,
28052816
connectionConnectTimeoutTime: number = this.connectionConnectTimeoutTime,
28062817
blocking: boolean = false,
@@ -2823,6 +2834,7 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
28232834
await this.syncNodeGraphHandler(
28242835
ctx,
28252836
undefined,
2837+
network,
28262838
initialNodesParameter,
28272839
connectionConnectTimeoutTime,
28282840
);
@@ -2832,7 +2844,11 @@ class NodeManager<Manifest extends AgentClientManifestNodeManager> {
28322844
delay: 0,
28332845
handlerId: this.syncNodeGraphHandlerId,
28342846
lazy: true,
2835-
parameters: [initialNodesParameter, connectionConnectTimeoutTime],
2847+
parameters: [
2848+
network,
2849+
initialNodesParameter,
2850+
connectionConnectTimeoutTime,
2851+
],
28362852
path: [this.tasksPath, this.syncNodeGraphHandlerId],
28372853
priority: 0,
28382854
});

tests/nodes/NodeManager.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,7 @@ describe(`${NodeManager.name}`, () => {
21442144
const mockedRefreshBucket = jest.spyOn(nodeManager, 'refreshBucket');
21452145

21462146
await nodeManager.syncNodeGraph(
2147+
undefined,
21472148
[
21482149
[
21492150
ncmPeers[0].nodeId,
@@ -2203,6 +2204,7 @@ describe(`${NodeManager.name}`, () => {
22032204
const mockedRefreshBucket = jest.spyOn(nodeManager, 'refreshBucket');
22042205

22052206
await nodeManager.syncNodeGraph(
2207+
undefined,
22062208
[
22072209
[
22082210
ncmPeers[0].nodeId,
@@ -2225,6 +2227,7 @@ describe(`${NodeManager.name}`, () => {
22252227
});
22262228
test('network entry with syncNodeGraph handles failure to resolve hostnames', async () => {
22272229
const syncP = nodeManager.syncNodeGraph(
2230+
undefined,
22282231
[
22292232
[ncmPeers[0].nodeId, ['some.random.host' as Host, 55555 as Port]],
22302233
[ncmPeers[0].nodeId, [localHost, 55555 as Port]],
@@ -2309,6 +2312,7 @@ describe(`${NodeManager.name}`, () => {
23092312
expect(await nodeGraph.nodesTotal()).toBe(0);
23102313

23112314
await nodeManager.syncNodeGraph(
2315+
undefined,
23122316
[
23132317
[
23142318
ncmPeers[0].nodeId,
@@ -2963,5 +2967,11 @@ describe(`${NodeManager.name}`, () => {
29632967
node1.nodeManager.claimNetwork(seedNodeId, network),
29642968
).rejects.toThrow(claimsErrors.ErrorEmptyStream);
29652969
});
2970+
// verify the claim exists
2971+
test.todo(
2972+
'node should automatically request NetworkAccessClaim if it does not exist',
2973+
);
2974+
// verify the claim is unmodified
2975+
test.todo('node should not request new NetworkAccessClaim if it exist');
29662976
});
29672977
});

0 commit comments

Comments
 (0)