Skip to content

Commit 343b30b

Browse files
committed
chore: resolved lint and failing NodeManager tests
1 parent 706a4da commit 343b30b

File tree

7 files changed

+105
-140
lines changed

7 files changed

+105
-140
lines changed

src/discovery/Discovery.ts

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,11 @@ class Discovery {
153153
parent: GestaltIdEncoded | null,
154154
) => {
155155
try {
156-
this.logger.error('out here 1')
157156
await this.processVertex(
158157
vertex,
159158
lastProcessedCutoffTime ?? undefined,
160159
ctx,
161160
);
162-
this.logger.error('out here 2')
163161
this.dispatchEvent(
164162
new discoveryEvents.EventDiscoveryVertexProcessed({
165163
detail: {
@@ -171,13 +169,11 @@ class Discovery {
171169
} catch (e) {
172170
// We need to reschedule if the task was cancelled due to discovery domain stopping
173171
if (e === discoveryStoppingTaskReason) {
174-
this.logger.error('out here 3')
175172
// We need to recreate the task for the vertex
176173
const vertexId = gestaltsUtils.decodeGestaltId(vertex);
177174
if (vertexId == null) {
178175
never(`failed to decode vertex GestaltId "${vertex}"`);
179176
}
180-
this.logger.error('out here 4')
181177
await this.scheduleDiscoveryForVertex(
182178
vertexId,
183179
undefined,
@@ -187,13 +183,14 @@ class Discovery {
187183
undefined,
188184
ctx,
189185
);
190-
this.logger.error('out here 5')
191186
return;
192187
}
193188
// Aborting a duplicate task is not an error
194189
if (e === abortSingletonTaskReason) return;
195190
// Destroying tasks is not an error
196191
if (e === discoveryDestroyedTaskReason) return;
192+
// Abortion due to cancellation is not an error
193+
if (e === ctx.signal.reason) return;
197194
this.dispatchEvent(
198195
new discoveryEvents.EventDiscoveryVertexFailed({
199196
detail: {
@@ -213,7 +210,9 @@ class Discovery {
213210
/**
214211
* This handler is run periodically to check if nodes are ready to be rediscovered
215212
*/
216-
protected checkRediscoveryHandler: TaskHandler = async (ctx: ContextTimed) => {
213+
protected checkRediscoveryHandler: TaskHandler = async (
214+
ctx: ContextTimed,
215+
) => {
217216
await this.checkRediscovery(
218217
Date.now() - this.rediscoverVertexThresholdTime,
219218
undefined,
@@ -368,7 +367,7 @@ class Discovery {
368367
providerId: ProviderId,
369368
identityId: IdentityId,
370369
lastProcessedCutoffTime?: number,
371-
ctx?: Partial<ContextTimedInput>
370+
ctx?: Partial<ContextTimedInput>,
372371
): Promise<void>;
373372
@ready(new discoveryErrors.ErrorDiscoveryNotRunning())
374373
@timedCancellable(true)
@@ -439,17 +438,11 @@ class Discovery {
439438
const [type, id] = vertexId;
440439
switch (type) {
441440
case 'node':
442-
this.logger.error('processnode before')
443-
// return await this.processNode(id, lastProcessedCutoffTime, ctx);
444-
const val1 = await this.processNode(id, lastProcessedCutoffTime, ctx);
445-
this.logger.error('processnode after')
446-
return val1
441+
// Return await this.processNode(id, lastProcessedCutoffTime, ctx);
442+
return await this.processNode(id, lastProcessedCutoffTime, ctx);
447443
case 'identity':
448-
this.logger.error('processidentity before')
449-
// return await this.processIdentity(id, lastProcessedCutoffTime, ctx);
450-
const val2 = await this.processIdentity(id, lastProcessedCutoffTime, ctx);
451-
this.logger.error('processidentity after')
452-
return val2
444+
// Return await this.processIdentity(id, lastProcessedCutoffTime, ctx);
445+
return await this.processIdentity(id, lastProcessedCutoffTime, ctx);
453446
default:
454447
never(`type must be either "node" or "identity" got "${type}"`);
455448
}
@@ -466,34 +459,26 @@ class Discovery {
466459
if (nodeId.equals(this.keyRing.getNodeId())) {
467460
// Skip our own nodeId, we actively add this information when it changes,
468461
// so there is no need to scan it.
469-
this.logger.error('before processed tiem')
470462
await this.gestaltGraph.setVertexProcessedTime(
471463
gestaltNodeId,
472464
processedTime,
473465
);
474-
this.logger.error('after processed tiem')
475466
return;
476467
}
477-
this.logger.error('before claim')
478468
const newestClaimId = await this.gestaltGraph.getClaimIdNewest(nodeId);
479-
this.logger.error('after claim')
480469
// The sigChain data of the vertex (containing all cryptolinks)
481470
let vertexChainData: Record<ClaimIdEncoded, SignedClaim> = {};
482471
try {
483-
this.logger.error('before chain')
484472
vertexChainData = await this.nodeManager.requestChainData(
485473
nodeId,
486474
newestClaimId,
487475
ctx,
488476
);
489-
this.logger.error('after chain')
490477
} catch (e) {
491-
this.logger.error('before chain time error')
492478
await this.gestaltGraph.setVertexProcessedTime(
493479
gestaltNodeId,
494480
processedTime,
495481
);
496-
this.logger.error('after chain time error')
497482
// Not strictly an error in this case, we can fail to connect
498483
this.logger.info(
499484
`Failed to discover ${nodesUtils.encodeNodeId(
@@ -507,37 +492,31 @@ class Discovery {
507492
ctx.signal.throwIfAborted();
508493
switch (signedClaim.payload.typ) {
509494
case 'ClaimLinkNode':
510-
this.logger.error('claimlinknode before')
511495
await this.processClaimLinkNode(
512496
signedClaim as SignedClaim<ClaimLinkNode>,
513497
nodeId,
514498
lastProcessedCutoffTime,
515499
ctx,
516500
);
517-
this.logger.error('claimlinknode after')
518501
break;
519502
case 'ClaimLinkIdentity':
520-
this.logger.error('claimlinkidentity before')
521503
await this.processClaimLinkIdentity(
522504
signedClaim as SignedClaim<ClaimLinkIdentity>,
523505
nodeId,
524506
lastProcessedCutoffTime,
525507
ctx,
526508
);
527-
this.logger.error('claimlinkidentity after')
528509
break;
529510
default:
530511
never(
531512
`signedClaim.payload.typ must be "ClaimLinkNode" or "ClaimLinkIdentity" got "${signedClaim.payload.typ}"`,
532513
);
533514
}
534515
}
535-
this.logger.error('setvertex time before')
536516
await this.gestaltGraph.setVertexProcessedTime(
537517
gestaltNodeId,
538518
processedTime,
539519
);
540-
this.logger.error('setvertex time after')
541520
}
542521

543522
protected async processClaimLinkNode(
@@ -549,7 +528,6 @@ class Discovery {
549528
// Get the chain data of the linked node
550529
// Could be node1 or node2 in the claim so get the one that's
551530
// not equal to nodeId from above
552-
this.logger.error('processClaimLinkNode 1')
553531
const node1Id = nodesUtils.decodeNodeId(signedClaim.payload.iss);
554532
if (node1Id == null) {
555533
never(`failed to decode issuer NodeId "${signedClaim.payload.iss}"`);
@@ -558,7 +536,6 @@ class Discovery {
558536
if (node2Id == null) {
559537
never(`failed to decode subject NodeId "${signedClaim.payload.sub}"`);
560538
}
561-
this.logger.error('processClaimLinkNode 2')
562539
// Verify the claim
563540
const node1PublicKey = keysUtils.publicKeyFromNodeId(node1Id);
564541
const node2PublicKey = keysUtils.publicKeyFromNodeId(node2Id);
@@ -572,12 +549,10 @@ class Discovery {
572549
);
573550
return;
574551
}
575-
this.logger.error('processClaimLinkNode 3')
576552
const linkedNodeId = node1Id.equals(nodeId) ? node2Id : node1Id;
577553
const linkedVertexNodeInfo: GestaltNodeInfo = {
578554
nodeId: linkedNodeId,
579555
};
580-
this.logger.error('processClaimLinkNode 4')
581556
await this.gestaltGraph.linkNodeAndNode(
582557
{
583558
nodeId,
@@ -588,7 +563,6 @@ class Discovery {
588563
meta: {},
589564
},
590565
);
591-
this.logger.error('processClaimLinkNode 5')
592566
const claimId = decodeClaimId(signedClaim.payload.jti);
593567
if (claimId == null) {
594568
never(`failed to decode claimId "${signedClaim.payload.jti}"`);
@@ -602,7 +576,6 @@ class Discovery {
602576
lastProcessedCutoffTime,
603577
))
604578
) {
605-
this.logger.error('processClaimLinkNode 7')
606579
await this.scheduleDiscoveryForVertex(
607580
linkedGestaltId,
608581
undefined,
@@ -884,7 +857,6 @@ class Discovery {
884857
gestaltIdEncoded,
885858
].join(''),
886859
);
887-
this.logger.error('here1')
888860
// Check if task exists
889861
let taskExisting: Task | null = null;
890862
for await (const task of this.taskManager.getTasks(
@@ -895,14 +867,12 @@ class Discovery {
895867
ctx,
896868
)) {
897869
ctx.signal.throwIfAborted();
898-
this.logger.error('here2')
899870
// Ignore active tasks
900871
if (ignoreActive && task.status === 'active') continue;
901872
if (taskExisting == null) {
902873
taskExisting = task;
903874
continue;
904875
}
905-
this.logger.error('here3')
906876
// Any extra tasks should be cancelled, this shouldn't normally happen
907877
task.cancel(abortSingletonTaskReason);
908878
this.dispatchEvent(
@@ -914,13 +884,11 @@ class Discovery {
914884
}),
915885
);
916886
}
917-
this.logger.error('here4')
918887
// Only create if it doesn't exist
919888
if (taskExisting != null) return;
920889
this.logger.info(
921890
`Scheduling new discovery for vertex with gestaltId ${gestaltIdEncoded}`,
922891
);
923-
this.logger.error('here5')
924892
await this.taskManager.scheduleTask(
925893
{
926894
handlerId: this.discoverVertexHandlerId,
@@ -936,7 +904,6 @@ class Discovery {
936904
},
937905
tran,
938906
);
939-
this.logger.error('here6')
940907
this.dispatchEvent(
941908
new discoveryEvents.EventDiscoveryVertexQueued({
942909
detail: {
@@ -1087,12 +1054,10 @@ class Discovery {
10871054
}
10881055
// Refresh timer in preparation for request
10891056
ctx.timer.refresh();
1090-
this.logger.error('verifyIdentityClaim before getClaim')
10911057
const identitySignedClaim = await provider.getClaim(
10921058
authIdentityId,
10931059
claimId,
10941060
);
1095-
this.logger.error('verifyIdentityClaim after getClaim')
10961061
if (identitySignedClaim == null) {
10971062
continue;
10981063
}
@@ -1119,10 +1084,11 @@ class Discovery {
11191084
tran?: DBTransaction,
11201085
ctx?: Partial<ContextTimedInput>,
11211086
): Promise<void>;
1087+
@timedCancellable(true)
11221088
public async checkRediscovery(
11231089
lastProcessedCutoffTime: number,
11241090
tran: DBTransaction | undefined,
1125-
ctx: ContextTimed,
1091+
@context ctx: ContextTimed,
11261092
): Promise<void> {
11271093
if (tran == null) {
11281094
return this.db.withTransactionF((tran) =>

src/nodes/NodeConnectionManager.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ import { AbstractEvent, EventAll } from '@matrixai/events';
4646
import {
4747
context,
4848
timed,
49-
timedCancellable
50-
} from "@matrixai/contexts/dist/decorators";
49+
timedCancellable,
50+
} from '@matrixai/contexts/dist/decorators';
5151
import { Semaphore } from '@matrixai/async-locks';
5252
import { PromiseCancellable } from '@matrixai/async-cancellable';
5353
import NodeConnection from './NodeConnection';
@@ -1479,7 +1479,11 @@ class NodeConnectionManager {
14791479
port: Port;
14801480
}>;
14811481
@ready(new nodesErrors.ErrorNodeManagerNotRunning())
1482-
@timedCancellable(true)
1482+
@timedCancellable(
1483+
true,
1484+
(nodeConnectionManager: NodeConnectionManager) =>
1485+
nodeConnectionManager.connectionConnectTimeoutTime,
1486+
)
14831487
public async handleNodesConnectionSignalInitial(
14841488
sourceNodeId: NodeId,
14851489
targetNodeId: NodeId,
@@ -1518,16 +1522,20 @@ class NodeConnectionManager {
15181522
this.keyRing.keyPair,
15191523
data,
15201524
);
1521-
const connectionSignalP = this.withConnF(targetNodeId, ctx, async (conn) => {
1522-
const client = conn.getClient();
1523-
await client.methods.nodesConnectionSignalFinal({
1524-
sourceNodeIdEncoded: nodesUtils.encodeNodeId(sourceNodeId),
1525-
targetNodeIdEncoded: nodesUtils.encodeNodeId(targetNodeId),
1526-
address: address,
1527-
requestSignature: requestSignature,
1528-
relaySignature: relaySignature.toString('base64url'),
1529-
});
1530-
})
1525+
const connectionSignalP = this.withConnF(
1526+
targetNodeId,
1527+
ctx,
1528+
async (conn) => {
1529+
const client = conn.getClient();
1530+
await client.methods.nodesConnectionSignalFinal({
1531+
sourceNodeIdEncoded: nodesUtils.encodeNodeId(sourceNodeId),
1532+
targetNodeIdEncoded: nodesUtils.encodeNodeId(targetNodeId),
1533+
address: address,
1534+
requestSignature: requestSignature,
1535+
relaySignature: relaySignature.toString('base64url'),
1536+
});
1537+
},
1538+
)
15311539
// Ignore results and failures, then are expected to happen and are allowed
15321540
.then(
15331541
() => {},
@@ -1790,25 +1798,26 @@ class NodeConnectionManager {
17901798
nodeId: NodeId,
17911799
ctx?: Partial<ContextTimedInput>,
17921800
): Promise<void>;
1793-
@timedCancellable(true)
1801+
@timedCancellable(
1802+
true,
1803+
(nodeConnectionManager: NodeConnectionManager) =>
1804+
nodeConnectionManager.connectionConnectTimeoutTime,
1805+
)
17941806
public async isAuthenticatedP(
17951807
nodeId: NodeId,
17961808
@context ctx: ContextTimed,
17971809
): Promise<void> {
1810+
ctx.signal.throwIfAborted();
17981811
const targetNodeIdString = nodeId.toString() as NodeIdString;
17991812
const connectionsEntry = this.connections.get(targetNodeIdString);
18001813
if (connectionsEntry == null) {
18011814
throw new nodesErrors.ErrorNodeConnectionManagerConnectionNotFound();
18021815
}
1803-
const { p: abortP, rejectP: triggerAbort } = utils.promise<void>();
1816+
const { p: abortP, rejectP: rejectAbortP } = utils.promise<never>();
18041817
const abortHandler = () => {
1805-
triggerAbort(ctx.signal.reason);
1818+
rejectAbortP(ctx.signal.reason);
18061819
};
1807-
if (ctx.signal.aborted) {
1808-
triggerAbort(ctx.signal.reason);
1809-
} else {
1810-
ctx.signal.addEventListener('abort', abortHandler, { once: true });
1811-
}
1820+
ctx.signal.addEventListener('abort', abortHandler, { once: true });
18121821
try {
18131822
return await Promise.race([connectionsEntry.authenticatedP, abortP]);
18141823
} catch (e) {

0 commit comments

Comments
 (0)