From 0aa3b96c18542bd6042856e303217201731fbffc Mon Sep 17 00:00:00 2001 From: sklppy88 Date: Thu, 21 Nov 2024 09:22:11 +0000 Subject: [PATCH] batched approach --- .../pxe/src/simulator_oracle/index.ts | 34 +++++++++++-------- .../simulator_oracle/simulator_oracle.test.ts | 3 +- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index a80cc5764f4..59f750e605f 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -351,23 +351,29 @@ export class SimulatorOracle implements DBOracle { const appTaggingSecret = await this.#calculateTaggingSecret(contractAddress, sender, recipient); let [currentIndex] = await this.db.getTaggingSecretsIndexesAsSender([appTaggingSecret]); - while (true) { - const currentIndexedAppTaggingSecret = new IndexedTaggingSecret(appTaggingSecret, currentIndex); - const currentTag = currentIndexedAppTaggingSecret.computeTag(recipient); + const WINDOW = 5; - const [[possibleLog]] = await this.aztecNode.getLogsByTags([currentTag]); + let possibleLogs: TxScopedL2Log[][]; - if (possibleLog !== undefined) { - currentIndex++; - } else { - this.log.debug( - `Syncing logs for sender ${sender}, secret ${appTaggingSecret}:${currentIndex} at contract: ${contractName}(${contractAddress})`, - ); + do { + const currentTags = [...new Array(WINDOW)].map((_, i) => { + const indexedAppTaggingSecret = new IndexedTaggingSecret(appTaggingSecret, currentIndex + i); + return indexedAppTaggingSecret.computeTag(recipient); + }); - await this.db.setTaggingSecretsIndexesAsSender([new IndexedTaggingSecret(appTaggingSecret, currentIndex)]); - break; - } - } + possibleLogs = await this.aztecNode.getLogsByTags(currentTags); + currentIndex += WINDOW; + } while (possibleLogs.every(log => log.length !== 0)); + + // We are getting the first empty index, and subtracting it from our WINDOW. So if our first empty index is 1, + // and our current index is 5, which means that we went through the loop one time, we take our current index, + // and subtract WINDOW - first empty index (= 5-1) from it. This means that new index will be 1. + const newIndex = currentIndex - (WINDOW - possibleLogs.findIndex(log => log.length === 0)); + + await this.db.setTaggingSecretsIndexesAsSender([new IndexedTaggingSecret(appTaggingSecret, newIndex)]); + this.log.debug( + `Syncing logs for sender ${sender}, secret ${appTaggingSecret}:${currentIndex} at contract: ${contractName}(${contractAddress})`, + ); } /** diff --git a/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts b/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts index 4a21acb33dd..f77a53d6221 100644 --- a/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts +++ b/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts @@ -300,8 +300,7 @@ describe('Simulator oracle', () => { const indexesAsSenderAfterSync = await database.getTaggingSecretsIndexesAsSender(secrets); expect(indexesAsSenderAfterSync).toStrictEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]); - // We expect getLogsByTags to be called N + 1 times, where N is the index. - expect(aztecNode.getLogsByTags.mock.calls.length).toBe(2 * 5 + 3 * 5); + expect(aztecNode.getLogsByTags.mock.calls.length).toBe(NUM_SENDERS); }); it('should sync tagged logs with a sender index offset', async () => {