From b4a666683ec61db17f4af0377b917c4c6e0561b5 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 17 Dec 2024 19:31:13 +0000 Subject: [PATCH] naming fixes --- .../pxe/src/simulator_oracle/index.ts | 5 +- .../simulator_oracle/simulator_oracle.test.ts | 60 ++++++++++--------- .../pxe/src/simulator_oracle/tagging_utils.ts | 3 + 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/yarn-project/pxe/src/simulator_oracle/index.ts b/yarn-project/pxe/src/simulator_oracle/index.ts index 470324d96de..bb3885695a4 100644 --- a/yarn-project/pxe/src/simulator_oracle/index.ts +++ b/yarn-project/pxe/src/simulator_oracle/index.ts @@ -38,7 +38,7 @@ import { type IncomingNoteDao } from '../database/incoming_note_dao.js'; import { type PxeDatabase } from '../database/index.js'; import { produceNoteDaos } from '../note_decryption_utils/produce_note_daos.js'; import { getAcirSimulator } from '../simulator/index.js'; -import { getIndexedTaggingSecretsForTheWindow, getInitialIndexesMap } from './tagging_utils.js'; +import { WINDOW_HALF_SIZE, getIndexedTaggingSecretsForTheWindow, getInitialIndexesMap } from './tagging_utils.js'; /** * A data oracle that provides information needed for simulating a transaction. @@ -418,9 +418,6 @@ export class SimulatorOracle implements DBOracle { maxBlockNumber: number, scopes?: AztecAddress[], ): Promise> { - // Half the size of the window we slide over the tagging secret indexes. - const WINDOW_HALF_SIZE = 10; - // Ideally this algorithm would be implemented in noir, exposing its building blocks as oracles. // However it is impossible at the moment due to the language not supporting nested slices. // This nesting is necessary because for a given set of tags we don't 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 8f4f94a01f4..0db352f96a6 100644 --- a/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts +++ b/yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts @@ -37,6 +37,7 @@ import { type PxeDatabase } from '../database/index.js'; import { KVPxeDatabase } from '../database/kv_pxe_database.js'; import { ContractDataOracle } from '../index.js'; import { SimulatorOracle } from './index.js'; +import { WINDOW_HALF_SIZE } from './tagging_utils.js'; const TXS_PER_BLOCK = 4; const NUM_NOTE_HASHES_PER_BLOCK = TXS_PER_BLOCK * MAX_NOTE_HASHES_PER_TX; @@ -138,16 +139,15 @@ describe('Simulator oracle', () => { describe('sync tagged logs', () => { const NUM_SENDERS = 10; - const SENDER_OFFSET_WINDOW_SIZE = 10; let senders: { completeAddress: CompleteAddress; ivsk: Fq; secretKey: Fr }[]; - function generateMockLogs(senderOffset: number) { + function generateMockLogs(tagIndex: number) { const logs: { [k: string]: TxScopedL2Log[] } = {}; - // Add a random note from every address in the address book for our account with index senderOffset + // Add a random note from every address in the address book for our account with index tagIndex // Compute the tag as sender (knowledge of preaddress and ivsk) for (const sender of senders) { - const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, senderOffset); + const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, tagIndex); const blockNumber = 1; const randomNote = new MockNoteRequest( getRandomNoteLogPayload(tag, contractAddress), @@ -164,18 +164,18 @@ describe('Simulator oracle', () => { // Add a random note from the first sender in the address book, repeating the tag // Compute the tag as sender (knowledge of preaddress and ivsk) const firstSender = senders[0]; - const tag = computeSiloedTagForIndex(firstSender, recipient.address, contractAddress, senderOffset); + const tag = computeSiloedTagForIndex(firstSender, recipient.address, contractAddress, tagIndex); const payload = getRandomNoteLogPayload(tag, contractAddress); const logData = payload.generatePayload(GrumpkinScalar.random(), recipient.address).toBuffer(); const log = new TxScopedL2Log(TxHash.random(), 1, 0, false, logData); logs[tag.toString()].push(log); // Accumulated logs intended for recipient: NUM_SENDERS + 1 - // Add a random note from half the address book for our account with index senderOffset + 1 + // Add a random note from half the address book for our account with index tagIndex + 1 // Compute the tag as sender (knowledge of preaddress and ivsk) for (let i = NUM_SENDERS / 2; i < NUM_SENDERS; i++) { const sender = senders[i]; - const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, senderOffset + 1); + const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, tagIndex + 1); const blockNumber = 2; const randomNote = new MockNoteRequest( getRandomNoteLogPayload(tag, contractAddress), @@ -189,13 +189,13 @@ describe('Simulator oracle', () => { } // Accumulated logs intended for recipient: NUM_SENDERS + 1 + NUM_SENDERS / 2 - // Add a random note from every address in the address book for a random recipient with index senderOffset + // Add a random note from every address in the address book for a random recipient with index tagIndex // Compute the tag as sender (knowledge of preaddress and ivsk) for (const sender of senders) { const keys = deriveKeys(Fr.random()); const partialAddress = Fr.random(); const randomRecipient = computeAddress(keys.publicKeys, partialAddress); - const tag = computeSiloedTagForIndex(sender, randomRecipient, contractAddress, senderOffset); + const tag = computeSiloedTagForIndex(sender, randomRecipient, contractAddress, tagIndex); const blockNumber = 3; const randomNote = new MockNoteRequest( getRandomNoteLogPayload(tag, contractAddress), @@ -232,8 +232,8 @@ describe('Simulator oracle', () => { }); it('should sync tagged logs', async () => { - const senderOffset = 0; - generateMockLogs(senderOffset); + const tagIndex = 0; + generateMockLogs(tagIndex); const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3); // We expect to have all logs intended for the recipient, one per sender + 1 with a duplicated tag for the first // one + half of the logs for the second index @@ -266,8 +266,8 @@ describe('Simulator oracle', () => { await keyStore.addAccount(sender.secretKey, sender.completeAddress.partialAddress); } - let senderOffset = 0; - generateMockLogs(senderOffset); + let tagIndex = 0; + generateMockLogs(tagIndex); // Recompute the secrets (as recipient) to ensure indexes are updated const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address); @@ -298,8 +298,8 @@ describe('Simulator oracle', () => { // We add more logs to the second half of the window to test that a second iteration in `syncTaggedLogsAsSender` // is handled correctly. - senderOffset = 11; - generateMockLogs(senderOffset); + tagIndex = 11; + generateMockLogs(tagIndex); for (let i = 0; i < senders.length; i++) { await simulatorOracle.syncTaggedLogsAsSender( contractAddress, @@ -315,8 +315,8 @@ describe('Simulator oracle', () => { }); it('should sync tagged logs with a sender index offset', async () => { - const senderOffset = 5; - generateMockLogs(senderOffset); + const tagIndex = 5; + generateMockLogs(tagIndex); const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3); // We expect to have all logs intended for the recipient, one per sender + 1 with a duplicated tag for the first one + half of the logs for the second index expect(syncedLogs.get(recipient.address.toString())).toHaveLength(NUM_SENDERS + 1 + NUM_SENDERS / 2); @@ -342,8 +342,8 @@ describe('Simulator oracle', () => { }); it("should sync tagged logs for which indexes are not updated if they're inside the window", async () => { - const senderOffset = 1; - generateMockLogs(senderOffset); + const tagIndex = 1; + generateMockLogs(tagIndex); // Recompute the secrets (as recipient) to update indexes const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address); @@ -362,8 +362,8 @@ describe('Simulator oracle', () => { expect(syncedLogs.get(recipient.address.toString())).toHaveLength(NUM_SENDERS + 1 + NUM_SENDERS / 2); // First sender should have 2 logs, but keep index 2 since they were built using the same tag - // Next 4 senders should also have index 2 = offset + 1 - // Last 5 senders should have index 3 = offset + 2 + // Next 4 senders should also have index 2 = tagIndex + 1 + // Last 5 senders should have index 3 = tagIndex + 2 const indexes = await database.getTaggingSecretsIndexesAsRecipient(secrets); expect(indexes).toHaveLength(NUM_SENDERS); @@ -375,8 +375,8 @@ describe('Simulator oracle', () => { }); it("should not sync tagged logs for which indexes are not updated if they're outside the window", async () => { - const senderOffset = 0; - generateMockLogs(senderOffset); + const tagIndex = 0; + generateMockLogs(tagIndex); // Recompute the secrets (as recipient) to update indexes const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address); @@ -385,8 +385,10 @@ describe('Simulator oracle', () => { return poseidon2Hash([firstSenderSecretPoint.x, firstSenderSecretPoint.y, contractAddress]); }); + // We set the indexes to WINDOW_HALF_SIZE + 1 so that it's outside the window and for this reason no updates + // should be triggered. await database.setTaggingSecretsIndexesAsRecipient( - secrets.map(secret => new IndexedTaggingSecret(secret, SENDER_OFFSET_WINDOW_SIZE + 1)), + secrets.map(secret => new IndexedTaggingSecret(secret, WINDOW_HALF_SIZE + 1)), ); const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3); @@ -405,8 +407,8 @@ describe('Simulator oracle', () => { }); it('should sync tagged logs from scratch after a DB wipe', async () => { - const senderOffset = 0; - generateMockLogs(senderOffset); + const tagIndex = 0; + generateMockLogs(tagIndex); // Recompute the secrets (as recipient) to update indexes const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address); @@ -416,7 +418,7 @@ describe('Simulator oracle', () => { }); await database.setTaggingSecretsIndexesAsRecipient( - secrets.map(secret => new IndexedTaggingSecret(secret, SENDER_OFFSET_WINDOW_SIZE + 2)), + secrets.map(secret => new IndexedTaggingSecret(secret, WINDOW_HALF_SIZE + 2)), ); let syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3); @@ -448,8 +450,8 @@ describe('Simulator oracle', () => { }); it('should not sync tagged logs with a blockNumber > maxBlockNumber', async () => { - const senderOffset = 0; - generateMockLogs(senderOffset); + const tagIndex = 0; + generateMockLogs(tagIndex); const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 1); // Only NUM_SENDERS + 1 logs should be synched, since the rest have blockNumber > 1 diff --git a/yarn-project/pxe/src/simulator_oracle/tagging_utils.ts b/yarn-project/pxe/src/simulator_oracle/tagging_utils.ts index 82759eaf038..6cf121bcd7b 100644 --- a/yarn-project/pxe/src/simulator_oracle/tagging_utils.ts +++ b/yarn-project/pxe/src/simulator_oracle/tagging_utils.ts @@ -1,5 +1,8 @@ import { type Fr, IndexedTaggingSecret } from '@aztec/circuits.js'; +// Half the size of the window we slide over the tagging secret indexes. +export const WINDOW_HALF_SIZE = 10; + export function getIndexedTaggingSecretsForTheWindow( secretsAndWindows: { appTaggingSecret: Fr; leftMostIndex: number; rightMostIndex: number }[], ): IndexedTaggingSecret[] {