Skip to content

Commit

Permalink
batched approach
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Nov 21, 2024
1 parent e912bcd commit 0aa3b96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
34 changes: 20 additions & 14 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`,
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 0aa3b96

Please sign in to comment.