Skip to content

Commit aa29fa9

Browse files
committed
fix: fixing tests
1 parent 748b477 commit aa29fa9

14 files changed

Lines changed: 154 additions & 165 deletions

src/PolykeyAgent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ class PolykeyAgent {
690690
this.logger.info(`Started ${this.constructor.name}`);
691691
} catch (e) {
692692
this.logger.warn(`Failed Starting ${this.constructor.name}`);
693+
this.events.removeAllListeners();
693694
await this.status?.beginStop({ pid: process.pid });
694695
await this.sessionManager?.stop();
695696
await this.notificationsManager?.stop();
@@ -706,7 +707,6 @@ class PolykeyAgent {
706707
await this.keyManager?.stop();
707708
await this.schema?.stop();
708709
await this.status?.stop({});
709-
this.events.removeAllListeners();
710710
throw e;
711711
}
712712
}
@@ -716,6 +716,7 @@ class PolykeyAgent {
716716
*/
717717
public async stop() {
718718
this.logger.info(`Stopping ${this.constructor.name}`);
719+
this.events.removeAllListeners();
719720
await this.status.beginStop({ pid: process.pid });
720721
await this.sessionManager.stop();
721722
await this.notificationsManager.stop();
@@ -736,7 +737,6 @@ class PolykeyAgent {
736737
await this.keyManager.stop();
737738
await this.schema.stop();
738739
await this.status.stop({});
739-
this.events.removeAllListeners();
740740
this.logger.info(`Stopped ${this.constructor.name}`);
741741
}
742742

src/nodes/NodeConnectionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class NodeConnectionManager {
603603
);
604604
} else {
605605
try {
606-
// FIXME: no tran neededawait this.nodeManager?.setNode(nodeId, nodeData.address);
606+
await this.nodeManager?.setNode(nodeId, nodeData.address);
607607
} catch (e) {
608608
if (!(e instanceof nodesErrors.ErrorNodeGraphSameNodeId)) throw e;
609609
}

src/nodes/NodeGraph.ts

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class NodeGraph {
198198
},
199199
this.nodeGraphBucketsDbPath,
200200
)) {
201-
const { nodeId } = nodesUtils.parseBucketsDbKey(key as unknown as Buffer);
201+
const { nodeId } = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
202202
yield [nodeId, nodeData];
203203
}
204204
}
@@ -224,45 +224,37 @@ class NodeGraph {
224224

225225
const [bucketIndex, bucketKey] = this.bucketIndex(nodeId);
226226
const lastUpdatedPath = [...this.nodeGraphLastUpdatedDbPath, bucketKey];
227-
const bucketPath = [...this.nodeGraphBucketsDbPath, bucketKey];
228-
const nodeData = await tran.get<NodeData>([
229-
...bucketPath,
230-
nodesUtils.bucketDbKey(nodeId),
231-
]);
227+
const nodeIdKey = nodesUtils.bucketDbKey(nodeId);
228+
const bucketPath = [...this.nodeGraphBucketsDbPath, bucketKey, nodeIdKey];
229+
const nodeData = await tran.get<NodeData>(bucketPath);
232230
if (nodeData != null) {
233231
this.logger.debug(
234232
`Updating node ${nodesUtils.encodeNodeId(
235233
nodeId,
236234
)} in bucket ${bucketIndex}`,
237235
);
238236
// If the node already exists we want to remove the old `lastUpdated`
239-
const lastUpdatedKey = nodesUtils.lastUpdatedBucketDbKey(
240-
nodeData.lastUpdated,
241-
nodeId,
242-
);
243-
await tran.del([...lastUpdatedPath, lastUpdatedKey]);
237+
const lastUpdatedKey = nodesUtils.lastUpdatedKey(nodeData.lastUpdated);
238+
await tran.del([...lastUpdatedPath, lastUpdatedKey, nodeIdKey]);
244239
} else {
245240
this.logger.debug(
246241
`Adding node ${nodesUtils.encodeNodeId(
247242
nodeId,
248243
)} to bucket ${bucketIndex}`,
249244
);
250-
// It didn't exist so we want to increment the bucket count
245+
// It didn't exist, so we want to increment the bucket count
251246
const count = await this.getBucketMetaProp(bucketIndex, 'count', tran);
252247
await this.setBucketMetaProp(bucketIndex, 'count', count + 1, tran);
253248
}
254249
const lastUpdated = getUnixtime();
255-
await tran.put([...bucketPath, nodesUtils.bucketDbKey(nodeId)], {
250+
await tran.put(bucketPath, {
256251
address: nodeAddress,
257252
lastUpdated,
258253
});
259-
const newLastUpdatedKey = nodesUtils.lastUpdatedBucketDbKey(
260-
lastUpdated,
261-
nodeId,
262-
);
254+
const newLastUpdatedKey = nodesUtils.lastUpdatedKey(lastUpdated);
263255
await tran.put(
264-
[...lastUpdatedPath, newLastUpdatedKey],
265-
nodesUtils.bucketDbKey(nodeId),
256+
[...lastUpdatedPath, newLastUpdatedKey, nodeIdKey],
257+
nodeIdKey,
266258
true,
267259
);
268260
}
@@ -286,7 +278,7 @@ class NodeGraph {
286278
bucketKey,
287279
])) {
288280
const { nodeId } = nodesUtils.parseLastUpdatedBucketDbKey(
289-
key as unknown as Buffer,
281+
key as Array<Buffer>,
290282
);
291283
oldestNodeIds.push(nodeId);
292284
}
@@ -304,10 +296,8 @@ class NodeGraph {
304296
const [bucketIndex, bucketKey] = this.bucketIndex(nodeId);
305297
const bucketPath = [...this.nodeGraphBucketsDbPath, bucketKey];
306298
const lastUpdatedPath = [...this.nodeGraphLastUpdatedDbPath, bucketKey];
307-
const nodeData = await tran.get<NodeData>([
308-
...bucketPath,
309-
nodesUtils.bucketDbKey(nodeId),
310-
]);
299+
const nodeIdKey = nodesUtils.bucketDbKey(nodeId);
300+
const nodeData = await tran.get<NodeData>([...bucketPath, nodeIdKey]);
311301
if (nodeData != null) {
312302
this.logger.debug(
313303
`Removing node ${nodesUtils.encodeNodeId(
@@ -316,12 +306,9 @@ class NodeGraph {
316306
);
317307
const count = await this.getBucketMetaProp(bucketIndex, 'count', tran);
318308
await this.setBucketMetaProp(bucketIndex, 'count', count - 1, tran);
319-
await tran.del([...bucketPath, nodesUtils.bucketDbKey(nodeId)]);
320-
const lastUpdatedKey = nodesUtils.lastUpdatedBucketDbKey(
321-
nodeData.lastUpdated,
322-
nodeId,
323-
);
324-
await tran.del([...lastUpdatedPath, lastUpdatedKey]);
309+
await tran.del([...bucketPath, nodeIdKey]);
310+
const lastUpdatedKey = nodesUtils.lastUpdatedKey(nodeData.lastUpdated);
311+
await tran.del([...lastUpdatedPath, lastUpdatedKey, nodeIdKey]);
325312
}
326313
}
327314

@@ -359,7 +346,7 @@ class NodeGraph {
359346
},
360347
[...this.nodeGraphBucketsDbPath, bucketKey],
361348
)) {
362-
const nodeId = nodesUtils.parseBucketDbKey(key as unknown as Buffer);
349+
const nodeId = nodesUtils.parseBucketDbKey(key[0] as Buffer);
363350
bucket.push([nodeId, nodeData]);
364351
}
365352
if (sort === 'distance') {
@@ -434,7 +421,7 @@ class NodeGraph {
434421
this.nodeGraphBucketsDbPath,
435422
)) {
436423
const { bucketIndex: bucketIndex_, nodeId } =
437-
nodesUtils.parseBucketsDbKey(key as unknown as Buffer);
424+
nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
438425
if (bucketIndex == null) {
439426
// First entry of the first bucket
440427
bucketIndex = bucketIndex_;
@@ -480,8 +467,8 @@ class NodeGraph {
480467
this.nodeGraphLastUpdatedDbPath,
481468
)) {
482469
const { bucketIndex: bucketIndex_, nodeId } =
483-
nodesUtils.parseLastUpdatedBucketsDbKey(key as unknown as Buffer);
484-
bucketsDbIterator.seek(nodesUtils.bucketsDbKey(bucketIndex_, nodeId));
470+
nodesUtils.parseLastUpdatedBucketsDbKey(key as Array<Buffer>);
471+
bucketsDbIterator.seek([key[0], key[2]]);
485472
// @ts-ignore
486473
// eslint-disable-next-line
487474
const iteratorResult = await bucketsDbIterator.next();
@@ -523,8 +510,10 @@ class NodeGraph {
523510
);
524511
}
525512

513+
const logger = this.logger.getChild('resetBuckets');
526514
// Setup new space
527515
const spaceNew = this.space === '0' ? '1' : '0';
516+
logger.debug('new space: ' + spaceNew);
528517
const nodeGraphMetaDbPathNew = [...this.nodeGraphDbPath, 'meta' + spaceNew];
529518
const nodeGraphBucketsDbPathNew = [
530519
...this.nodeGraphDbPath,
@@ -545,10 +534,16 @@ class NodeGraph {
545534
this.nodeGraphBucketsDbPath,
546535
)) {
547536
// The key is a combined bucket key and node ID
548-
const { nodeId } = nodesUtils.parseBucketsDbKey(key as unknown as Buffer);
537+
const { bucketIndex: bucketIndexOld, nodeId } =
538+
nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
539+
const nodeIdEncoded = nodesUtils.encodeNodeId(nodeId);
540+
const nodeIdKey = nodesUtils.bucketDbKey(nodeId);
549541
// If the new own node ID is one of the existing node IDs, it is just dropped
550542
// We only map to the new bucket if it isn't one of the existing node IDs
551543
if (nodeId.equals(nodeIdOwn)) {
544+
logger.debug(
545+
`nodeId ${nodeIdEncoded} from bucket ${bucketIndexOld} was identical to new NodeId and was dropped.`,
546+
);
552547
continue;
553548
}
554549
const bucketIndexNew = nodesUtils.bucketIndex(nodeIdOwn, nodeId);
@@ -560,36 +555,37 @@ class NodeGraph {
560555
if (countNew < this.nodeBucketLimit) {
561556
await tran.put([...metaPathNew, 'count'], countNew + 1);
562557
} else {
563-
let oldestIndexKey: Buffer | undefined = undefined;
558+
let oldestIndexKey: Array<Buffer> | undefined = undefined;
564559
let oldestNodeId: NodeId | undefined = undefined;
565560
for await (const [key] of tran.iterator(
566561
{
567562
limit: 1,
568563
},
569564
indexPathNew,
570565
)) {
571-
oldestIndexKey = key as unknown as Buffer;
566+
oldestIndexKey = key as Array<Buffer>;
572567
({ nodeId: oldestNodeId } = nodesUtils.parseLastUpdatedBucketDbKey(
573-
key as unknown as Buffer,
568+
key as Array<Buffer>,
574569
));
575570
}
576571
await tran.del([
577572
...bucketPathNew,
578573
nodesUtils.bucketDbKey(oldestNodeId!),
579574
]);
580-
await tran.del([...indexPathNew, oldestIndexKey!]);
575+
await tran.del([...indexPathNew, ...oldestIndexKey!]);
581576
}
577+
if (bucketIndexOld !== bucketIndexNew) {
578+
logger.debug(
579+
`nodeId ${nodeIdEncoded} moved ${bucketIndexOld}=>${bucketIndexNew}`,
580+
);
581+
} else {
582+
logger.debug(`nodeId ${nodeIdEncoded} unchanged ${bucketIndexOld}`);
583+
}
584+
await tran.put([...bucketPathNew, nodeIdKey], nodeData);
585+
const lastUpdatedKey = nodesUtils.lastUpdatedKey(nodeData.lastUpdated);
582586
await tran.put(
583-
[...bucketPathNew, nodesUtils.bucketDbKey(nodeId)],
584-
nodeData,
585-
);
586-
const lastUpdatedKey = nodesUtils.lastUpdatedBucketDbKey(
587-
nodeData.lastUpdated,
588-
nodeId,
589-
);
590-
await tran.put(
591-
[...indexPathNew, lastUpdatedKey],
592-
nodesUtils.bucketDbKey(nodeId),
587+
[...indexPathNew, lastUpdatedKey, nodeIdKey],
588+
nodeIdKey,
593589
true,
594590
);
595591
}
@@ -683,6 +679,8 @@ class NodeGraph {
683679
* current node has less than k nodes in all of its buckets, in which case it
684680
* returns all nodes it has knowledge of)
685681
*/
682+
// FIXME: this is still operating on assumptions from old code.
683+
// I can't get the gt/lt to work on the iterator.
686684
@ready(new nodesErrors.ErrorNodeGraphNotRunning())
687685
public async getClosestNodes(
688686
nodeId: NodeId,
@@ -721,55 +719,44 @@ class NodeGraph {
721719
// We can just use `!(lexpack bucketId)` to start from
722720
// Less than `!(bucketId 101)!` gets us buckets 100 and lower
723721
// greater than `!(bucketId 99)!` gets up buckets 100 and greater
724-
const prefix = Buffer.from([33]); // Code for `!` prefix
725722
if (nodeIds.length < limit) {
726723
// Just before target bucket
727-
const bucketId = Buffer.from(nodesUtils.bucketKey(startingBucket));
728-
const endKeyLower = Buffer.concat([prefix, bucketId, prefix]);
724+
const bucketIdKey = Buffer.from(nodesUtils.bucketKey(startingBucket));
729725
const remainingLimit = limit - nodeIds.length;
730726
// Iterate over lower buckets
731-
tran.iterator<NodeData>(
732-
{
733-
lt: endKeyLower,
734-
limit: remainingLimit,
735-
valueAsBuffer: false,
736-
},
737-
this.nodeGraphBucketsDbPath,
738-
);
739727
for await (const [key, nodeData] of tran.iterator<NodeData>(
740728
{
741-
lt: endKeyLower,
729+
lt: [bucketIdKey, ''],
742730
limit: remainingLimit,
743731
valueAsBuffer: false,
744732
},
745733
this.nodeGraphBucketsDbPath,
746734
)) {
747-
const info = nodesUtils.parseBucketsDbKey(key as unknown as Buffer);
735+
const info = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
748736
nodeIds.push([info.nodeId, nodeData]);
749737
}
750738
}
751739
if (nodeIds.length < limit) {
752740
// Just after target bucket
753741
const bucketId = Buffer.from(nodesUtils.bucketKey(startingBucket + 1));
754-
const startKeyUpper = Buffer.concat([prefix, bucketId, prefix]);
755742
const remainingLimit = limit - nodeIds.length;
756743
// Iterate over ids further away
757744
tran.iterator(
758745
{
759-
gt: startKeyUpper,
746+
gt: [bucketId, ''],
760747
limit: remainingLimit,
761748
},
762749
this.nodeGraphBucketsDbPath,
763750
);
764751
for await (const [key, nodeData] of tran.iterator<NodeData>(
765752
{
766-
gt: startKeyUpper,
753+
gt: [bucketId, ''],
767754
limit: remainingLimit,
768755
valueAsBuffer: false,
769756
},
770757
this.nodeGraphBucketsDbPath,
771758
)) {
772-
const info = nodesUtils.parseBucketsDbKey(key as unknown as Buffer);
759+
const info = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
773760
nodeIds.push([info.nodeId, nodeData]);
774761
}
775762
}

src/nodes/NodeManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class NodeManager {
381381
*/
382382
public async getBucket(
383383
bucketIndex: number,
384-
tran: DBTransaction,
384+
tran?: DBTransaction,
385385
): Promise<NodeBucket | undefined> {
386386
return await this.nodeGraph.getBucket(
387387
bucketIndex,
@@ -400,7 +400,7 @@ class NodeManager {
400400
* @param block - Flag for if the operation should block or utilize the async queue
401401
* @param force - Flag for if we want to add the node without authenticating or if the bucket is full.
402402
* This will drop the oldest node in favor of the new.
403-
* @param timeout Connection timeout timeout
403+
* @param timeout Connection timeout
404404
* @param tran
405405
*/
406406
@ready(new nodesErrors.ErrorNodeManagerNotRunning())

0 commit comments

Comments
 (0)