diff --git a/packages/modules/packages/archivist/packages/abstract/src/AbstractIndirectArchivist.ts b/packages/modules/packages/archivist/packages/abstract/src/AbstractIndirectArchivist.ts index 210b8c5fb9b..d6db904ea97 100644 --- a/packages/modules/packages/archivist/packages/abstract/src/AbstractIndirectArchivist.ts +++ b/packages/modules/packages/archivist/packages/abstract/src/AbstractIndirectArchivist.ts @@ -105,15 +105,13 @@ export abstract class AbstractIndirectArchivist< } protected async getHandler(hashes: string[]): Promise { - return await this.busy(async () => { - return compact( - await Promise.all( - hashes.map(async (hash) => { - return (await this.getFromParents(hash)) ?? null - }), - ), - ) - }) + return compact( + await Promise.all( + hashes.map(async (hash) => { + return (await this.getFromParents(hash)) ?? null + }), + ), + ) } protected head(): Promisable { diff --git a/packages/modules/packages/archivist/packages/indexeddb/src/IndexedDbArchivist.ts b/packages/modules/packages/archivist/packages/indexeddb/src/IndexedDbArchivist.ts index dd1cd57a5f7..ff716611376 100644 --- a/packages/modules/packages/archivist/packages/indexeddb/src/IndexedDbArchivist.ts +++ b/packages/modules/packages/archivist/packages/indexeddb/src/IndexedDbArchivist.ts @@ -83,43 +83,33 @@ export class IndexedDbArchivist< } protected override async allHandler(): Promise { - return await this.busy(async () => { - const result = await entries(this.db) - return result.map(([_hash, payload]) => payload) - }) + const result = await entries(this.db) + return result.map(([_hash, payload]) => payload) } protected override async clearHandler(): Promise { - return await this.busy(async () => { - await clear(this.db) - }) + await clear(this.db) } protected override async deleteHandler(hashes: string[]): Promise { - return await this.busy(async () => { - await delMany(hashes, this.db) - return hashes.map((_) => true) - }) + await delMany(hashes, this.db) + return hashes.map((_) => true) } protected override async getHandler(hashes: string[]): Promise { - return await this.busy(async () => { - const result = await getMany(hashes, this.db) - return result - }) + const result = await getMany(hashes, this.db) + return result } protected async insertHandler(payloads: Payload[]): Promise { - return await this.busy(async () => { - const entries = await Promise.all( - payloads.map>(async (payload) => { - const hash = await PayloadHasher.hashAsync(payload) - return [hash, payload] - }), - ) - await setMany(entries, this.db) - const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads) - return [result[0]] - }) + const entries = await Promise.all( + payloads.map>(async (payload) => { + const hash = await PayloadHasher.hashAsync(payload) + return [hash, payload] + }), + ) + await setMany(entries, this.db) + const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads) + return [result[0]] } } diff --git a/packages/modules/packages/archivist/packages/memory/src/MemoryArchivist.ts b/packages/modules/packages/archivist/packages/memory/src/MemoryArchivist.ts index 32502167ed4..d489ea1488d 100644 --- a/packages/modules/packages/archivist/packages/memory/src/MemoryArchivist.ts +++ b/packages/modules/packages/archivist/packages/memory/src/MemoryArchivist.ts @@ -69,23 +69,21 @@ export class MemoryArchivist< } protected override async commitHandler(): Promise { - return await this.busy(async () => { - const payloads = assertEx(await this.allHandler(), 'Nothing to commit') - const settled = await Promise.allSettled( - compact( - Object.values((await this.parents()).commit ?? [])?.map(async (parent) => { - const queryPayload = PayloadWrapper.wrap({ - payloads: await Promise.all(payloads.map((payload) => PayloadWrapper.hashAsync(payload))), - schema: ArchivistInsertQuerySchema, - }) - const query = await this.bindQuery(queryPayload, payloads) - return (await parent?.query(query[0], query[1]))?.[0] - }), - ), - ) - await this.clearHandler() - return compact(settled.filter(fulfilled).map((result) => result.value)) - }) + const payloads = assertEx(await this.allHandler(), 'Nothing to commit') + const settled = await Promise.allSettled( + compact( + Object.values((await this.parents()).commit ?? [])?.map(async (parent) => { + const queryPayload = PayloadWrapper.wrap({ + payloads: await Promise.all(payloads.map((payload) => PayloadWrapper.hashAsync(payload))), + schema: ArchivistInsertQuerySchema, + }) + const query = await this.bindQuery(queryPayload, payloads) + return (await parent?.query(query[0], query[1]))?.[0] + }), + ), + ) + await this.clearHandler() + return compact(settled.filter(fulfilled).map((result) => result.value)) } protected override async deleteHandler(hashes: string[]): Promise { @@ -97,43 +95,39 @@ export class MemoryArchivist< } protected override async getHandler(hashes: string[]): Promise { - return await this.busy(async () => { - return compact( - await Promise.all( - hashes.map(async (hash) => { - const payload = this.cache.get(hash) ?? (await super.get([hash]))[0] ?? null - if (this.storeParentReads) { - // NOTE: `payload` can actually be `null` here but TS doesn't seem - // to recognize it. LRUCache claims not to support `null`s via their - // types but seems to under the hood just fine. - this.cache.set(hash, payload) - } - return payload - }), - ), - ) - }) + return compact( + await Promise.all( + hashes.map(async (hash) => { + const payload = this.cache.get(hash) ?? (await super.get([hash]))[0] ?? null + if (this.storeParentReads) { + // NOTE: `payload` can actually be `null` here but TS doesn't seem + // to recognize it. LRUCache claims not to support `null`s via their + // types but seems to under the hood just fine. + this.cache.set(hash, payload) + } + return payload + }), + ), + ) } protected async insertHandler(payloads: Payload[]): Promise { - return await this.busy(async () => { - await Promise.all( - payloads.map((payload) => { - return this.insertPayloadIntoCache(payload) - }), - ) + await Promise.all( + payloads.map((payload) => { + return this.insertPayloadIntoCache(payload) + }), + ) - const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads) - const parentBoundWitnesses: BoundWitness[] = [] - const parents = await this.parents() - if (Object.entries(parents.write ?? {}).length) { - // We store the child bw also - parentBoundWitnesses.push(...(await this.writeToParents([result[0], ...payloads]))) - } - const boundWitnesses = [result[0], ...parentBoundWitnesses] - await this.emit('inserted', { boundWitnesses, module: this }) - return boundWitnesses - }) + const [result] = await this.bindQueryResult({ payloads, schema: ArchivistInsertQuerySchema }, payloads) + const parentBoundWitnesses: BoundWitness[] = [] + const parents = await this.parents() + if (Object.entries(parents.write ?? {}).length) { + // We store the child bw also + parentBoundWitnesses.push(...(await this.writeToParents([result[0], ...payloads]))) + } + const boundWitnesses = [result[0], ...parentBoundWitnesses] + await this.emit('inserted', { boundWitnesses, module: this }) + return boundWitnesses } private async insertPayloadIntoCache(payload: Payload): Promise { diff --git a/packages/modules/packages/bridge/packages/abstract/src/AbstractBridge.ts b/packages/modules/packages/bridge/packages/abstract/src/AbstractBridge.ts index a7a2a82050e..4a9775cf498 100644 --- a/packages/modules/packages/bridge/packages/abstract/src/AbstractBridge.ts +++ b/packages/modules/packages/bridge/packages/abstract/src/AbstractBridge.ts @@ -51,25 +51,23 @@ export abstract class AbstractBridge< override async resolve(filter?: ModuleFilter): Promise override async resolve(nameOrAddress: string): Promise override async resolve(nameOrAddressOrFilter?: ModuleFilter | string): Promise { - return await this.busy(async () => { - switch (typeof nameOrAddressOrFilter) { - case 'string': { - const byAddress = Account.isAddress(nameOrAddressOrFilter) - ? (await super.resolve({ address: [nameOrAddressOrFilter] })).pop() ?? - (await this.targetDownResolver().resolve({ address: [nameOrAddressOrFilter] })).pop() - : undefined - return ( - byAddress ?? - (await super.resolve({ name: [nameOrAddressOrFilter] })).pop() ?? - (await this.targetDownResolver().resolve({ name: [nameOrAddressOrFilter] })).pop() - ) - } - default: { - const filter: ModuleFilter | undefined = nameOrAddressOrFilter - return [...(await this.targetDownResolver().resolve(filter)), ...(await super.resolve(filter))].filter(duplicateModules) - } + switch (typeof nameOrAddressOrFilter) { + case 'string': { + const byAddress = Account.isAddress(nameOrAddressOrFilter) + ? (await super.resolve({ address: [nameOrAddressOrFilter] })).pop() ?? + (await this.targetDownResolver().resolve({ address: [nameOrAddressOrFilter] })).pop() + : undefined + return ( + byAddress ?? + (await super.resolve({ name: [nameOrAddressOrFilter] })).pop() ?? + (await this.targetDownResolver().resolve({ name: [nameOrAddressOrFilter] })).pop() + ) } - }) + default: { + const filter: ModuleFilter | undefined = nameOrAddressOrFilter + return [...(await this.targetDownResolver().resolve(filter)), ...(await super.resolve(filter))].filter(duplicateModules) + } + } } targetDownResolver(address?: string): BridgeModuleResolver { @@ -77,11 +75,7 @@ export abstract class AbstractBridge< return this._targetDownResolvers[address ?? 'root'] as BridgeModuleResolver } - async targetResolve(address: string, filter?: ModuleFilter) { - //TODO: Honor address so that the resolve only is done through that remote module - //right now, we check the entire remote hive - return (await this.targetDownResolver(address).resolve(filter)) as TModule[] - } + async targetResolve(address: string, filter?: ModuleFilter) {} protected override async queryHandler(query: T, payloads?: Payload[]): Promise { const wrapper = QueryBoundWitnessWrapper.parseQuery(query, payloads) diff --git a/packages/modules/packages/bridge/packages/http/src/HttpBridge.ts b/packages/modules/packages/bridge/packages/http/src/HttpBridge.ts index bdfd2b7aaa6..f0f532cf504 100644 --- a/packages/modules/packages/bridge/packages/http/src/HttpBridge.ts +++ b/packages/modules/packages/bridge/packages/http/src/HttpBridge.ts @@ -118,70 +118,48 @@ export class HttpBridge< } async targetDiscover(address?: string): Promise { - return await this.busy(async () => { - //if caching, return cached result if exists - const cachedResult = this.discoverCache?.get(address ?? 'root ') - if (cachedResult) { - return cachedResult - } - const addressToDiscover = address ?? this.rootAddress - const queryPayload = PayloadWrapper.wrap({ schema: ModuleDiscoverQuerySchema }) - const boundQuery = await this.bindQuery(queryPayload) - const discover = assertEx(await this.targetQuery(addressToDiscover, boundQuery[0], boundQuery[1]), `Unable to resolve [${address}]`)[1] - - this._targetQueries[addressToDiscover] = compact( - discover?.map((payload) => { - if (payload.schema === QuerySchema) { - const schemaPayload = payload as QueryPayload - return schemaPayload.query - } else { - return null - } - }) ?? [], - ) - - const targetConfigSchema = assertEx( - discover.find((payload) => payload.schema === ConfigSchema) as ConfigPayload, - `Discover did not return a [${ConfigSchema}] payload`, - ).config - - this._targetConfigs[addressToDiscover] = assertEx( - discover?.find((payload) => payload.schema === targetConfigSchema) as ModuleConfig, - `Discover did not return a [${targetConfigSchema}] payload`, - ) - - //if caching, set entry - this.discoverCache?.set(address ?? 'root', discover) - - return discover - }) + //if caching, return cached result if exists + const cachedResult = this.discoverCache?.get(address ?? 'root ') + if (cachedResult) { + return cachedResult + } + const addressToDiscover = address ?? this.rootAddress + const queryPayload = PayloadWrapper.wrap({ schema: ModuleDiscoverQuerySchema }) + const boundQuery = await this.bindQuery(queryPayload) + const discover = assertEx(await this.targetQuery(addressToDiscover, boundQuery[0], boundQuery[1]), `Unable to resolve [${address}]`)[1] + + this._targetQueries[addressToDiscover] = compact( + discover?.map((payload) => { + if (payload.schema === QuerySchema) { + const schemaPayload = payload as QueryPayload + return schemaPayload.query + } else { + return null + } + }) ?? [], + ) + + const targetConfigSchema = assertEx( + discover.find((payload) => payload.schema === ConfigSchema) as ConfigPayload, + `Discover did not return a [${ConfigSchema}] payload`, + ).config + + this._targetConfigs[addressToDiscover] = assertEx( + discover?.find((payload) => payload.schema === targetConfigSchema) as ModuleConfig, + `Discover did not return a [${targetConfigSchema}] payload`, + ) + + //if caching, set entry + this.discoverCache?.set(address ?? 'root', discover) + + return discover } targetQueries(address: string): string[] { return assertEx(this._targetQueries[address], `targetQueries not set [${address}]`) } - async targetQuery(address: string, query: QueryBoundWitness, payloads: Payload[] = []): Promise { - return await this.busy(async () => { - try { - const moduleUrlString = this.moduleUrl(address).toString() - const result = await this.axios.post>(moduleUrlString, [query, payloads]) - if (result.status === 404) { - throw `target module not found [${moduleUrlString}] [${result.status}]` - } - if (result.status >= 400) { - this.logger?.error(`targetQuery failed [${moduleUrlString}]`) - throw `targetQuery failed [${moduleUrlString}] [${result.status}]` - } - return result.data.data - } catch (ex) { - const error = ex as AxiosError - this.logger?.error(`Error Status: ${error.status}`) - this.logger?.error(`Error Cause: ${JSON.stringify(error.cause, null, 2)}`) - throw error - } - }) - } + async targetQuery(address: string, query: QueryBoundWitness, payloads: Payload[] = []): Promise {} targetQueryable(_address: string, _query: QueryBoundWitness, _payloads?: Payload[], _queryConfig?: ModuleConfig): boolean { return true diff --git a/packages/modules/packages/module/packages/abstract/src/AbstractIndirectModule.ts b/packages/modules/packages/module/packages/abstract/src/AbstractIndirectModule.ts index a406c45dc5d..14dfb4cc0c9 100644 --- a/packages/modules/packages/module/packages/abstract/src/AbstractIndirectModule.ts +++ b/packages/modules/packages/module/packages/abstract/src/AbstractIndirectModule.ts @@ -239,14 +239,12 @@ export abstract class AbstractIndirectModule { this.started('throw') - return await this.busy(async () => { - const result = await this.queryHandler(assertEx(QueryBoundWitnessWrapper.unwrap(query)), payloads, queryConfig) + const result = await this.queryHandler(assertEx(QueryBoundWitnessWrapper.unwrap(query)), payloads, queryConfig) - const args: ModuleQueriedEventArgs = { module: this, payloads, query, result } - await this.emit('moduleQueried', args) + const args: ModuleQueriedEventArgs = { module: this, payloads, query, result } + await this.emit('moduleQueried', args) - return result - }) + return result } queryable( diff --git a/packages/modules/packages/module/packages/abstract/src/AbstractModule.ts b/packages/modules/packages/module/packages/abstract/src/AbstractModule.ts index c6bb8c1e25d..41c6157cc56 100644 --- a/packages/modules/packages/module/packages/abstract/src/AbstractModule.ts +++ b/packages/modules/packages/module/packages/abstract/src/AbstractModule.ts @@ -19,30 +19,22 @@ export abstract class AbstractModule { - return await this.busy(async () => { - return await super.addressPreviousHash() - }) + return await super.addressPreviousHash() } /* make it public */ override async describe(): Promise { - return await this.busy(async () => { - return await super.describe() - }) + return await super.describe() } /* make it public */ override async discover(): Promise { - return await this.busy(async () => { - return await super.discover() - }) + return await super.discover() } /* make it public */ override async manifest(): Promise { - return await this.busy(async () => { - return await super.manifest() - }) + return await super.manifest() } /* make it public */ diff --git a/packages/modules/packages/witness/packages/abstract/src/AbstractWitness.ts b/packages/modules/packages/witness/packages/abstract/src/AbstractWitness.ts index bbd6fa4cc58..1b34046d109 100644 --- a/packages/modules/packages/witness/packages/abstract/src/AbstractWitness.ts +++ b/packages/modules/packages/witness/packages/abstract/src/AbstractWitness.ts @@ -41,13 +41,11 @@ export abstract class AbstractWitness< } async observe(payloads?: Payload[]): Promise { - return await this.busy(async () => { - this.started('throw') - const payloadList = assertEx(await this.observeHandler(payloads), 'Trying to witness nothing') - assertEx(payloadList.length > 0, 'Trying to witness empty list') - payloadList?.forEach((payload) => assertEx(payload.schema, 'observe: Missing Schema')) - return payloadList - }) + this.started('throw') + const payloadList = assertEx(await this.observeHandler(payloads), 'Trying to witness nothing') + assertEx(payloadList.length > 0, 'Trying to witness empty list') + payloadList?.forEach((payload) => assertEx(payload.schema, 'observe: Missing Schema')) + return payloadList } protected override async queryHandler(