-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: optimization of syncTaggedLogsAsSender
#10811
Merged
benesjan
merged 5 commits into
master
from
12-17-refactor_getlogsbytags_request_optimization_in_synctaggedlogsassender
Dec 18, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this file are cluttered by renaming of |
||
|
||
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); | ||
|
@@ -292,13 +292,14 @@ describe('Simulator oracle', () => { | |
let indexesAsSenderAfterSync = await database.getTaggingSecretsIndexesAsSender(secrets); | ||
expect(indexesAsSenderAfterSync).toStrictEqual([1, 1, 1, 1, 1, 2, 2, 2, 2, 2]); | ||
|
||
// Two windows are fetch for each sender | ||
expect(aztecNode.getLogsByTags.mock.calls.length).toBe(NUM_SENDERS * 2); | ||
// Only 1 window is obtained for each sender | ||
expect(aztecNode.getLogsByTags.mock.calls.length).toBe(NUM_SENDERS); | ||
aztecNode.getLogsByTags.mockReset(); | ||
|
||
// We add more logs at the end of the window to make sure we only detect them and bump the indexes if it lies within our window | ||
senderOffset = 10; | ||
generateMockLogs(senderOffset); | ||
// We add more logs to the second half of the window to test that a second iteration in `syncTaggedLogsAsSender` | ||
// is handled correctly. | ||
tagIndex = 11; | ||
generateMockLogs(tagIndex); | ||
for (let i = 0; i < senders.length; i++) { | ||
await simulatorOracle.syncTaggedLogsAsSender( | ||
contractAddress, | ||
|
@@ -308,14 +309,14 @@ describe('Simulator oracle', () => { | |
} | ||
|
||
indexesAsSenderAfterSync = await database.getTaggingSecretsIndexesAsSender(secrets); | ||
expect(indexesAsSenderAfterSync).toStrictEqual([11, 11, 11, 11, 11, 12, 12, 12, 12, 12]); | ||
expect(indexesAsSenderAfterSync).toStrictEqual([12, 12, 12, 12, 12, 13, 13, 13, 13, 13]); | ||
|
||
expect(aztecNode.getLogsByTags.mock.calls.length).toBe(NUM_SENDERS * 2); | ||
}); | ||
|
||
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); | ||
|
@@ -341,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); | ||
|
@@ -361,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); | ||
|
@@ -374,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); | ||
|
@@ -384,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); | ||
|
@@ -404,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); | ||
|
@@ -415,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); | ||
|
@@ -447,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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing
WINDOW_HALF_SIZE
from another file now because that value is needed also in the tests so it makes sense to have it exportable from "somewhere".