diff --git a/src/discovery/Discovery.ts b/src/discovery/Discovery.ts index c17f86a3e6..091bc58726 100644 --- a/src/discovery/Discovery.ts +++ b/src/discovery/Discovery.ts @@ -80,7 +80,6 @@ class Discovery { protected discoveryQueueDb: DBLevel; protected lock: Mutex = new Mutex(); protected discoveryQueueIdGenerator: DiscoveryQueueIdGenerator; - // Used for unattended discovery protected visitedVertices = new Set(); protected discoveryQueue: AsyncGenerator; protected discoveryProcess: Promise; @@ -161,12 +160,19 @@ class Discovery { return [async () => release(), this]; }; + /** + * Queues a node for discovery. Internally calls `pushKeyToDiscoveryQueue`. + */ @ready(new discoveryErrors.ErrorDiscoveryNotRunning()) public async queueDiscoveryByNode(nodeId: NodeId) { const nodeKey = gestaltsUtils.keyFromNode(nodeId); await this.pushKeyToDiscoveryQueue(nodeKey); } + /** + * Queues an identity for discovery. Internally calls + * `pushKeyToDiscoveryQueue`. + */ @ready(new discoveryErrors.ErrorDiscoveryNotRunning()) public async queueDiscoveryByIdentity( providerId: ProviderId, @@ -176,6 +182,9 @@ class Discovery { await this.pushKeyToDiscoveryQueue(identityKey); } + /** + * Generator for the logic of iterating through the Discovery Queue. + */ public async *setupDiscoveryQueue(): AsyncGenerator { while (true) { if (!(await this.queueIsEmpty())) { @@ -336,12 +345,21 @@ class Discovery { } } + /** + * Used for iterating over the discovery queue. This method should run + * continuously whenever the Discovery module is started and should be exited + * only during stopping. + */ protected async runDiscoveryQueue() { for await (const _ of this.discoveryQueue) { // Empty } } + /** + * Simple check for whether the Discovery Queue is empty. Uses a + * transaction lock to ensure consistency. + */ protected async queueIsEmpty(): Promise { return await utils.withF([this.transaction], async () => { let nextDiscoveryQueueId: DiscoveryQueueId | undefined; @@ -358,6 +376,10 @@ class Discovery { }); } + /** + * Push a Gestalt Key to the Discovery Queue. This process also unlocks + * the queue if it was previously locked (due to being empty). + */ protected async pushKeyToDiscoveryQueue(gk: GestaltKey) { await utils.withF([this.transaction], async () => { const discoveryQueueId = this.discoveryQueueIdGenerator(); @@ -373,6 +395,11 @@ class Discovery { } } + /** + * Remove a Gestalt Key from the Discovery Queue by its QueueId. This should + * only be done after a Key has been discovered in order to remove it from + * the beginning of the queue. + */ protected async removeKeyFromDiscoveryQueue(keyId: DiscoveryQueueId) { await utils.withF([this.transaction], async () => { await this.db.del(this.discoveryQueueDbDomain, idUtils.toBuffer(keyId)); diff --git a/src/discovery/types.ts b/src/discovery/types.ts index ae13bddd4b..9c32ed947a 100644 --- a/src/discovery/types.ts +++ b/src/discovery/types.ts @@ -1,6 +1,9 @@ import type { Opaque } from '../types'; import type { Id } from '../GenericIdTypes'; +/** + * Used to preserve order in the Discovery Queue. + */ type DiscoveryQueueId = Opaque<'DiscoveryQueueId', Id>; type DiscoveryQueueIdGenerator = () => DiscoveryQueueId; diff --git a/src/identities/utils.ts b/src/identities/utils.ts index 2e5f36be10..dfdc119065 100644 --- a/src/identities/utils.ts +++ b/src/identities/utils.ts @@ -49,6 +49,9 @@ function browser(url: string): void { browserProcess.unref(); } +/** + * Check whether a given identity matches at least one search term from a list. + */ function matchIdentityData( identityData: IdentityData, searchTerms: Array,