From 382206e0972d412b20d3a4a61b4ff9b373b669a5 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 14:42:42 -0500 Subject: [PATCH 1/7] serial resolving option for 'multiple' tracker --- packages/sessions/src/trackers/multiple.ts | 48 +++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index f6bdced6f..9fd7f199d 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -32,21 +32,44 @@ export function raceUntil(promises: Promise[], fallback: T, evalRes: (val: }) } +export function serialResolve(promises: Promise[], fallback: T, evalRes: (val: T) => boolean): Promise { + return new Promise(resolve => { + for (const p of promises) { + p.then(val => { + if (evalRes(val)) resolve(val) + }) + } + resolve(fallback) + }) +} + export async function allSafe(promises: Promise[], fallback: T): Promise { return Promise.all(promises.map(promise => promise.catch(() => fallback))) } export class MultipleTracker implements migrator.PresignedMigrationTracker, ConfigTracker { - constructor(private trackers: (migrator.PresignedMigrationTracker & ConfigTracker)[]) {} + constructor( + private trackers: (migrator.PresignedMigrationTracker & ConfigTracker)[], + private isSerial: boolean = false + ) {} async configOfImageHash(args: { imageHash: string }): Promise { const requests = this.trackers.map(async (t, i) => ({ res: await t.configOfImageHash(args), i })) - // We try to find a complete configuration, we race so that we don't wait for all trackers to respond - const result1 = await raceUntil(requests, undefined, val => { - if (val?.res === undefined) return false - return universal.genericCoderFor(val.res.version).config.isComplete(val.res) - }) + let result1: { res: commons.config.Config | undefined; i: number } | undefined + + if (this.isSerial) { + result1 = await serialResolve(requests, undefined, val => { + if (val?.res === undefined) return false + return universal.genericCoderFor(val.res.version).config.isComplete(val.res) + }) + } else { + // We try to find a complete configuration, we race so that we don't wait for all trackers to respond + result1 = await raceUntil(requests, undefined, val => { + if (val?.res === undefined) return false + return universal.genericCoderFor(val.res.version).config.isComplete(val.res) + }) + } if (result1?.res) { // Skip saving the config to the tracker that returned the result @@ -82,11 +105,14 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf async imageHashOfCounterfactualWallet(args: { wallet: string }): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> { - const imageHash = await raceUntil( - this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)), - undefined, - result => Boolean(result) - ) + let imageHash: { imageHash: string; context: commons.context.WalletContext } | undefined + const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) + + if (this.isSerial) { + imageHash = await serialResolve(requests, undefined, result => Boolean(result)) + } else { + imageHash = await raceUntil(requests, undefined, result => Boolean(result)) + } if (imageHash) { this.configOfImageHash({ imageHash: imageHash.imageHash }).then(config => { From fbdae298df3207bbedb46ede80945d5244ce6d5b Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 15:47:31 -0500 Subject: [PATCH 2/7] fixed serialResolve --- packages/sessions/src/trackers/multiple.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index 9fd7f199d..a66e2f274 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -32,15 +32,19 @@ export function raceUntil(promises: Promise[], fallback: T, evalRes: (val: }) } -export function serialResolve(promises: Promise[], fallback: T, evalRes: (val: T) => boolean): Promise { - return new Promise(resolve => { - for (const p of promises) { - p.then(val => { - if (evalRes(val)) resolve(val) - }) +export async function serialResolve(promises: Promise[], fallback: T, evalRes: (val: T) => boolean): Promise { + for (const p of promises) { + try { + const val = await p + if (evalRes(val)) { + return val + } + } catch { + // Continue to next promise if this one fails + continue } - resolve(fallback) - }) + } + return fallback } export async function allSafe(promises: Promise[], fallback: T): Promise { From f5d61173a85bae9ef635764f023ed7cd0773a821 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 17:13:02 -0500 Subject: [PATCH 3/7] switched to promise factory for isSerial --- packages/sessions/src/trackers/multiple.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index a66e2f274..f2d626fd1 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -32,10 +32,10 @@ export function raceUntil(promises: Promise[], fallback: T, evalRes: (val: }) } -export async function serialResolve(promises: Promise[], fallback: T, evalRes: (val: T) => boolean): Promise { +export async function serialResolve(promises: Array<() => Promise>, fallback: T, evalRes: (val: T) => boolean): Promise { for (const p of promises) { try { - const val = await p + const val = await p() if (evalRes(val)) { return val } @@ -63,10 +63,14 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf let result1: { res: commons.config.Config | undefined; i: number } | undefined if (this.isSerial) { - result1 = await serialResolve(requests, undefined, val => { - if (val?.res === undefined) return false - return universal.genericCoderFor(val.res.version).config.isComplete(val.res) - }) + result1 = await serialResolve( + requests.map(p => () => p), + undefined, + val => { + if (val?.res === undefined) return false + return universal.genericCoderFor(val.res.version).config.isComplete(val.res) + } + ) } else { // We try to find a complete configuration, we race so that we don't wait for all trackers to respond result1 = await raceUntil(requests, undefined, val => { @@ -113,7 +117,11 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) if (this.isSerial) { - imageHash = await serialResolve(requests, undefined, result => Boolean(result)) + imageHash = await serialResolve( + requests.map(p => () => p), + undefined, + result => Boolean(result) + ) } else { imageHash = await raceUntil(requests, undefined, result => Boolean(result)) } From 7220a3cb989b2d8245a43115c2ed1d8b0af5ef6c Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 17:29:13 -0500 Subject: [PATCH 4/7] using promiseFactory as base instead of Promise[] --- packages/sessions/src/trackers/multiple.ts | 33 ++++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index f2d626fd1..873225158 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -58,25 +58,25 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf ) {} async configOfImageHash(args: { imageHash: string }): Promise { - const requests = this.trackers.map(async (t, i) => ({ res: await t.configOfImageHash(args), i })) + const requestFactory = this.trackers.map((t, i) => async () => ({ res: await t.configOfImageHash(args), i })) let result1: { res: commons.config.Config | undefined; i: number } | undefined if (this.isSerial) { - result1 = await serialResolve( - requests.map(p => () => p), + result1 = await serialResolve(requestFactory, undefined, val => { + if (val?.res === undefined) return false + return universal.genericCoderFor(val.res.version).config.isComplete(val.res) + }) + } else { + // We try to find a complete configuration, we race so that we don't wait for all trackers to respond + result1 = await raceUntil( + requestFactory.map(p => p()), undefined, val => { if (val?.res === undefined) return false return universal.genericCoderFor(val.res.version).config.isComplete(val.res) } ) - } else { - // We try to find a complete configuration, we race so that we don't wait for all trackers to respond - result1 = await raceUntil(requests, undefined, val => { - if (val?.res === undefined) return false - return universal.genericCoderFor(val.res.version).config.isComplete(val.res) - }) } if (result1?.res) { @@ -90,7 +90,10 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf // but we try to combine all results anyway const tmptracker = new LocalConfigTracker(undefined as any) // TODO: Fix this, provider not needed anyway - const results = await allSafe(requests, undefined) + const results = await allSafe( + requestFactory.map(p => p()), + undefined + ) for (const r of results) { if (r?.res) await tmptracker.saveWalletConfig({ config: r.res }) @@ -114,16 +117,16 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf wallet: string }): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> { let imageHash: { imageHash: string; context: commons.context.WalletContext } | undefined - const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) + const requestFactory = this.trackers.map(t => () => t.imageHashOfCounterfactualWallet(args)) if (this.isSerial) { - imageHash = await serialResolve( - requests.map(p => () => p), + imageHash = await serialResolve(requestFactory, undefined, result => Boolean(result)) + } else { + imageHash = await raceUntil( + requestFactory.map(p => p()), undefined, result => Boolean(result) ) - } else { - imageHash = await raceUntil(requests, undefined, result => Boolean(result)) } if (imageHash) { From 5f6d27833f78fac05f66f64c199b55228832bbc1 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 17:48:41 -0500 Subject: [PATCH 5/7] removed factory and used simple for loop for isSerial --- packages/sessions/src/trackers/multiple.ts | 61 ++++++++-------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index 873225158..401c6a296 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -32,21 +32,6 @@ export function raceUntil(promises: Promise[], fallback: T, evalRes: (val: }) } -export async function serialResolve(promises: Array<() => Promise>, fallback: T, evalRes: (val: T) => boolean): Promise { - for (const p of promises) { - try { - const val = await p() - if (evalRes(val)) { - return val - } - } catch { - // Continue to next promise if this one fails - continue - } - } - return fallback -} - export async function allSafe(promises: Promise[], fallback: T): Promise { return Promise.all(promises.map(promise => promise.catch(() => fallback))) } @@ -58,25 +43,26 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf ) {} async configOfImageHash(args: { imageHash: string }): Promise { - const requestFactory = this.trackers.map((t, i) => async () => ({ res: await t.configOfImageHash(args), i })) + const requests = this.trackers.map(async (t, i) => ({ res: await t.configOfImageHash(args), i })) let result1: { res: commons.config.Config | undefined; i: number } | undefined if (this.isSerial) { - result1 = await serialResolve(requestFactory, undefined, val => { + for (const request of requests) { + const result = await request + if (result.res) { + if (universal.genericCoderFor(result.res.version).config.isComplete(result.res)) { + result1 = result + break + } + } + } + } else { + // We try to find a complete configuration, we race so that we don't wait for all trackers to respond + result1 = await raceUntil(requests, undefined, val => { if (val?.res === undefined) return false return universal.genericCoderFor(val.res.version).config.isComplete(val.res) }) - } else { - // We try to find a complete configuration, we race so that we don't wait for all trackers to respond - result1 = await raceUntil( - requestFactory.map(p => p()), - undefined, - val => { - if (val?.res === undefined) return false - return universal.genericCoderFor(val.res.version).config.isComplete(val.res) - } - ) } if (result1?.res) { @@ -90,10 +76,7 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf // but we try to combine all results anyway const tmptracker = new LocalConfigTracker(undefined as any) // TODO: Fix this, provider not needed anyway - const results = await allSafe( - requestFactory.map(p => p()), - undefined - ) + const results = await allSafe(requests, undefined) for (const r of results) { if (r?.res) await tmptracker.saveWalletConfig({ config: r.res }) @@ -117,16 +100,18 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf wallet: string }): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> { let imageHash: { imageHash: string; context: commons.context.WalletContext } | undefined - const requestFactory = this.trackers.map(t => () => t.imageHashOfCounterfactualWallet(args)) + const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) if (this.isSerial) { - imageHash = await serialResolve(requestFactory, undefined, result => Boolean(result)) + for (const request of requests) { + const result = await request + if (result) { + imageHash = result + break + } + } } else { - imageHash = await raceUntil( - requestFactory.map(p => p()), - undefined, - result => Boolean(result) - ) + imageHash = await raceUntil(requests, undefined, result => Boolean(result)) } if (imageHash) { From 48aa93c0c8322d2bb36be34e616f2cb124f8c903 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 17:55:55 -0500 Subject: [PATCH 6/7] changed to using this.trackers instead of local promise array --- packages/sessions/src/trackers/multiple.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index 401c6a296..5dc1d3f86 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -48,11 +48,11 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf let result1: { res: commons.config.Config | undefined; i: number } | undefined if (this.isSerial) { - for (const request of requests) { - const result = await request - if (result.res) { - if (universal.genericCoderFor(result.res.version).config.isComplete(result.res)) { - result1 = result + for (const [i, tracker] of this.trackers.entries()) { + const result = await tracker.configOfImageHash(args) + if (result) { + if (universal.genericCoderFor(result.version).config.isComplete(result)) { + result1 = { res: result, i } break } } @@ -103,10 +103,10 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) if (this.isSerial) { - for (const request of requests) { - const result = await request + for (const tracker of this.trackers) { + const result = await tracker.imageHashOfCounterfactualWallet(args) if (result) { - imageHash = result + imageHash = { imageHash: result.imageHash, context: result.context } break } } From a24f4a35aadfb71478490b6b823712fc2fcd7804 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 11 Nov 2024 18:11:35 -0500 Subject: [PATCH 7/7] moved requests to else block --- packages/sessions/src/trackers/multiple.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sessions/src/trackers/multiple.ts b/packages/sessions/src/trackers/multiple.ts index 5dc1d3f86..625f3febc 100644 --- a/packages/sessions/src/trackers/multiple.ts +++ b/packages/sessions/src/trackers/multiple.ts @@ -100,7 +100,6 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf wallet: string }): Promise<{ imageHash: string; context: commons.context.WalletContext } | undefined> { let imageHash: { imageHash: string; context: commons.context.WalletContext } | undefined - const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) if (this.isSerial) { for (const tracker of this.trackers) { @@ -111,6 +110,7 @@ export class MultipleTracker implements migrator.PresignedMigrationTracker, Conf } } } else { + const requests = this.trackers.map(t => t.imageHashOfCounterfactualWallet(args)) imageHash = await raceUntil(requests, undefined, result => Boolean(result)) }