From 87758966e54b9563e8153feb07bba808a110aa5b Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 12 Jul 2024 12:15:05 -0600 Subject: [PATCH 1/3] fix(arns): update event emitter to provide more events and logs while loading arns records Also allows clients to provide their own arns event emitter to handle loaded records --- src/utils/processes.ts | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/utils/processes.ts b/src/utils/processes.ts index e2517141..adbb993a 100644 --- a/src/utils/processes.ts +++ b/src/utils/processes.ts @@ -19,7 +19,7 @@ import { pLimit } from 'plimit-lit'; import { ANT } from '../common/ant.js'; import { IO } from '../common/io.js'; -import { ILogger } from '../common/logger.js'; +import { ILogger, Logger } from '../common/logger.js'; import { IO_TESTNET_PROCESS_ID } from '../constants.js'; import { AoANTState, @@ -94,35 +94,46 @@ export class ArNSEventEmitter extends EventEmitter { protected contract: AoIORead; private timeoutMs: number; // timeout for each request to 3 seconds private throttle; + private logger: ILogger; constructor({ contract = IO.init({ processId: IO_TESTNET_PROCESS_ID, }), timeoutMs = 60_000, concurrency = 30, + logger = Logger.default, }: { contract?: AoIORead; timeoutMs?: number; concurrency?: number; - }) { + logger?: ILogger; + } = {}) { super(); this.contract = contract; this.timeoutMs = timeoutMs; this.throttle = pLimit(concurrency); + this.logger = logger; } async fetchProcessesOwnedByWallet({ address }: { address: WalletAddress }) { const uniqueContractProcessIds: Record< string, - { state: AoANTState | undefined; names: Record } + { + state: AoANTState | undefined; + names: Record; + } > = {}; await timeout( this.timeoutMs, - fetchAllArNSRecords({ contract: this.contract }), + fetchAllArNSRecords({ contract: this.contract, emitter: this }), ) .catch((e) => { this.emit('error', `Error getting ArNS records: ${e}`); + this.logger.error(`Error getting ArNS records`, { + message: e?.message, + stack: e?.stack, + }); return {}; }) .then((records) => { @@ -142,8 +153,8 @@ export class ArNSEventEmitter extends EventEmitter { }); const idCount = Object.keys(uniqueContractProcessIds).length; - // check the contract owner and controllers this.emit('progress', 0, idCount); + // check the contract owner and controllers await Promise.all( Object.keys(uniqueContractProcessIds).map(async (processId, i) => this.throttle(async () => { @@ -188,12 +199,15 @@ export const fetchAllArNSRecords = async ({ contract = IO.init({ processId: IO_TESTNET_PROCESS_ID, }), - logger, + emitter, + logger = Logger.default, }: { contract?: AoIORead; + emitter?: EventEmitter; logger?: ILogger; }): Promise> => { let cursor: string | undefined; + const startTimestamp = Date.now(); const records: Record = {}; do { const pageResult = await contract @@ -203,6 +217,9 @@ export const fetchAllArNSRecords = async ({ message: e?.message, stack: e?.stack, }); + + emitter?.emit('error', `Error getting ArNS records: ${e}`); + return undefined; }); @@ -214,8 +231,29 @@ export const fetchAllArNSRecords = async ({ const { name, ...recordDetails } = record; records[name] = recordDetails; }); + + logger.debug('Fetched page of ArNS records', { + totalRecordCount: pageResult.totalItems, + fetchedRecordCount: Object.keys(records).length, + cursor: pageResult.nextCursor, + }); + + emitter?.emit('pageLoaded', { + totalRecordCount: pageResult.totalItems, + fetchedRecordCount: Object.keys(records).length, + records: pageResult.items, + cursor: pageResult.nextCursor, + }); + cursor = pageResult.nextCursor; } while (cursor !== undefined); + emitter?.emit('end', records); + + logger.debug('Fetched all ArNS records', { + totalRecordCount: Object.keys(records).length, + durationMs: Date.now() - startTimestamp, + }); + return records; }; From 5b919ac483778673527e0535a5aec8b06e6ce68e Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 12 Jul 2024 12:20:48 -0600 Subject: [PATCH 2/3] fix: use custom event names to avoid overlap --- src/utils/processes.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/processes.ts b/src/utils/processes.ts index adbb993a..e7113e83 100644 --- a/src/utils/processes.ts +++ b/src/utils/processes.ts @@ -218,7 +218,7 @@ export const fetchAllArNSRecords = async ({ stack: e?.stack, }); - emitter?.emit('error', `Error getting ArNS records: ${e}`); + emitter?.emit('arnsError', `Error getting ArNS records: ${e}`); return undefined; }); @@ -238,7 +238,7 @@ export const fetchAllArNSRecords = async ({ cursor: pageResult.nextCursor, }); - emitter?.emit('pageLoaded', { + emitter?.emit('arnsPage', { totalRecordCount: pageResult.totalItems, fetchedRecordCount: Object.keys(records).length, records: pageResult.items, @@ -248,7 +248,7 @@ export const fetchAllArNSRecords = async ({ cursor = pageResult.nextCursor; } while (cursor !== undefined); - emitter?.emit('end', records); + emitter?.emit('arnsEnd', records); logger.debug('Fetched all ArNS records', { totalRecordCount: Object.keys(records).length, From 1d67dfe1f7a4766bbce6cf5a55438f33423f660f Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Fri, 12 Jul 2024 15:46:42 -0600 Subject: [PATCH 3/3] fix(events): use arns name space for events --- src/utils/processes.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/processes.ts b/src/utils/processes.ts index e7113e83..dbb7ea79 100644 --- a/src/utils/processes.ts +++ b/src/utils/processes.ts @@ -218,7 +218,7 @@ export const fetchAllArNSRecords = async ({ stack: e?.stack, }); - emitter?.emit('arnsError', `Error getting ArNS records: ${e}`); + emitter?.emit('arns:error', `Error getting ArNS records: ${e}`); return undefined; }); @@ -238,7 +238,7 @@ export const fetchAllArNSRecords = async ({ cursor: pageResult.nextCursor, }); - emitter?.emit('arnsPage', { + emitter?.emit('arns:pageLoaded', { totalRecordCount: pageResult.totalItems, fetchedRecordCount: Object.keys(records).length, records: pageResult.items, @@ -248,7 +248,7 @@ export const fetchAllArNSRecords = async ({ cursor = pageResult.nextCursor; } while (cursor !== undefined); - emitter?.emit('arnsEnd', records); + emitter?.emit('arns:end', records); logger.debug('Fetched all ArNS records', { totalRecordCount: Object.keys(records).length,