Skip to content

Commit bb40316

Browse files
committed
feat: optimizing AztecNode::getLogsByTags for new log sync algo
1 parent 1b335df commit bb40316

File tree

11 files changed

+100
-222
lines changed

11 files changed

+100
-222
lines changed

yarn-project/archiver/src/archiver/kv_archiver_store/log_store.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ export class LogStore {
5858

5959
const currentLogs = taggedLogs.get(tag.toString()) ?? [];
6060
currentLogs.push(
61-
new TxScopedL2Log(txHash, dataStartIndexForTx, logIndex, block.number, blockHash, log).toBuffer(),
61+
new TxScopedL2Log(
62+
txHash,
63+
dataStartIndexForTx,
64+
logIndex,
65+
block.number,
66+
blockHash,
67+
block.timestamp,
68+
log,
69+
).toBuffer(),
6270
);
6371
taggedLogs.set(tag.toString(), currentLogs);
6472
});
@@ -69,7 +77,15 @@ export class LogStore {
6977

7078
const currentLogs = taggedLogs.get(tag.toString()) ?? [];
7179
currentLogs.push(
72-
new TxScopedL2Log(txHash, dataStartIndexForTx, logIndex, block.number, blockHash, log).toBuffer(),
80+
new TxScopedL2Log(
81+
txHash,
82+
dataStartIndexForTx,
83+
logIndex,
84+
block.number,
85+
blockHash,
86+
block.timestamp,
87+
log,
88+
).toBuffer(),
7389
);
7490
taggedLogs.set(tag.toString(), currentLogs);
7591
});

yarn-project/pxe/src/contract_function_simulator/pxe_oracle_interface.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ describe('PXEOracleInterface', () => {
136136
0,
137137
MIN_BLOCK_NUMBER_OF_A_LOG,
138138
L2BlockHash.random(),
139+
0n,
139140
PrivateLog.random(tag.value),
140141
);
141142
logs[tag.toString()] = [log];
@@ -152,6 +153,7 @@ describe('PXEOracleInterface', () => {
152153
0,
153154
BlockNumber.ZERO,
154155
L2BlockHash.random(),
156+
0n,
155157
PrivateLog.random(tag.value),
156158
);
157159
logs[tag.toString()].push(log);
@@ -169,6 +171,7 @@ describe('PXEOracleInterface', () => {
169171
0,
170172
blockNumber,
171173
L2BlockHash.random(),
174+
0n,
172175
PrivateLog.random(tag.value),
173176
);
174177
logs[tag.toString()] = [log];
@@ -188,6 +191,7 @@ describe('PXEOracleInterface', () => {
188191
0,
189192
MAX_BLOCK_NUMBER_OF_A_LOG,
190193
L2BlockHash.random(),
194+
0n,
191195
PrivateLog.random(tag.value),
192196
);
193197
logs[tag.toString()] = [log];
@@ -899,6 +903,7 @@ describe('PXEOracleInterface', () => {
899903
randomInt(100),
900904
BlockNumber(randomInt(100)),
901905
L2BlockHash.random(),
906+
BigInt(1),
902907
log,
903908
);
904909

yarn-project/pxe/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.test.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ describe('loadPrivateLogsForSenderRecipientPair', () => {
3535
return SiloedTag.compute(tag, app);
3636
}
3737

38-
function makeLog(blockHash: Fr, blockNumber: number, tag: Fr) {
38+
// Move blockTimestamp before tag in args in makeLog
39+
function makeLog(blockHash: Fr, blockNumber: number, blockTimestamp: bigint, tag: Fr) {
3940
return new TxScopedL2Log(
4041
TxHash.random(),
4142
0,
4243
0,
4344
BlockNumber(blockNumber),
4445
L2BlockHash.fromField(blockHash),
46+
blockTimestamp,
4547
PrivateLog.random(tag),
4648
);
4749
}
@@ -54,7 +56,6 @@ describe('loadPrivateLogsForSenderRecipientPair', () => {
5456

5557
beforeEach(async () => {
5658
aztecNode.getLogsByTags.mockReset();
57-
aztecNode.getBlockHeaderByHash.mockReset();
5859
aztecNode.getL2Tips.mockReset();
5960
aztecNode.getBlockHeader.mockReset();
6061
taggingDataProvider = new NewRecipientTaggingDataProvider(await openTmpStore('test'));
@@ -103,18 +104,13 @@ describe('loadPrivateLogsForSenderRecipientPair', () => {
103104
aztecNode.getLogsByTags.mockImplementation((tags: Fr[]) => {
104105
return Promise.all(
105106
tags.map(async (t: Fr) =>
106-
t.equals(logTag.value) ? [makeLog(await logBlockHeader.hash(), finalizedBlockNumber, logTag.value)] : [],
107+
t.equals(logTag.value)
108+
? [makeLog(await logBlockHeader.hash(), finalizedBlockNumber, logBlockTimestamp, logTag.value)]
109+
: [],
107110
),
108111
);
109112
});
110113

111-
aztecNode.getBlockHeaderByHash.mockImplementation(async (hash: Fr) => {
112-
if (hash.equals(await logBlockHeader.hash())) {
113-
return logBlockHeader;
114-
}
115-
return undefined;
116-
});
117-
118114
const logs = await loadPrivateLogsForSenderRecipientPair(
119115
secret,
120116
app,
@@ -146,18 +142,13 @@ describe('loadPrivateLogsForSenderRecipientPair', () => {
146142
aztecNode.getLogsByTags.mockImplementation((tags: Fr[]) => {
147143
return Promise.all(
148144
tags.map(async (t: Fr) =>
149-
t.equals(logTag.value) ? [makeLog(await logBlockHeader.hash(), finalizedBlockNumber, logTag.value)] : [],
145+
t.equals(logTag.value)
146+
? [makeLog(await logBlockHeader.hash(), finalizedBlockNumber, logBlockTimestamp, logTag.value)]
147+
: [],
150148
),
151149
);
152150
});
153151

154-
aztecNode.getBlockHeaderByHash.mockImplementation(async (hash: Fr) => {
155-
if (hash.equals(await logBlockHeader.hash())) {
156-
return logBlockHeader;
157-
}
158-
return undefined;
159-
});
160-
161152
const logs = await loadPrivateLogsForSenderRecipientPair(
162153
secret,
163154
app,
@@ -203,24 +194,15 @@ describe('loadPrivateLogsForSenderRecipientPair', () => {
203194
return Promise.all(
204195
tags.map(async (t: Fr) => {
205196
if (t.equals(log1Tag.value)) {
206-
return [makeLog(await log1BlockHeader.hash(), finalizedBlockNumber, log1Tag.value)];
197+
return [makeLog(await log1BlockHeader.hash(), finalizedBlockNumber, log1BlockTimestamp, log1Tag.value)];
207198
} else if (t.equals(log2Tag.value)) {
208-
return [makeLog(await log2BlockHeader.hash(), finalizedBlockNumber, log2Tag.value)];
199+
return [makeLog(await log2BlockHeader.hash(), finalizedBlockNumber, log2BlockTimestamp, log2Tag.value)];
209200
}
210201
return [];
211202
}),
212203
);
213204
});
214205

215-
aztecNode.getBlockHeaderByHash.mockImplementation(async (hash: Fr) => {
216-
if (hash.equals(await log1BlockHeader.hash())) {
217-
return log1BlockHeader;
218-
} else if (hash.equals(await log2BlockHeader.hash())) {
219-
return log2BlockHeader;
220-
}
221-
return undefined;
222-
});
223-
224206
const logs = await loadPrivateLogsForSenderRecipientPair(
225207
secret,
226208
app,

yarn-project/pxe/src/tagging/recipient_sync/load_private_logs_for_sender_recipient_pair.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ export async function loadPrivateLogsForSenderRecipientPair(
8989

9090
while (true) {
9191
// Get private logs with their block timestamps and corresponding tagging indexes
92-
const logsWithTimestampsAndIndexes = await loadLogsForRange(secret, app, aztecNode, start, end, anchorBlockNumber);
92+
const privateLogsWithIndexes = await loadLogsForRange(secret, app, aztecNode, start, end, anchorBlockNumber);
9393

94-
if (logsWithTimestampsAndIndexes.length === 0) {
94+
if (privateLogsWithIndexes.length === 0) {
9595
break;
9696
}
9797

98-
logs.push(...logsWithTimestampsAndIndexes.map(({ log }) => log));
98+
logs.push(...privateLogsWithIndexes.map(({ log }) => log));
9999

100100
const { highestAgedIndex, highestFinalizedIndex } = findHighestIndexes(
101-
logsWithTimestampsAndIndexes,
101+
privateLogsWithIndexes,
102102
currentTimestamp,
103103
finalizedBlockNumber,
104104
);

yarn-project/pxe/src/tagging/recipient_sync/utils/find_highest_indexes.test.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,24 @@ import { findHighestIndexes } from './find_highest_indexes.js';
99
describe('findHighestIndexes', () => {
1010
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
1111

12-
function makeLog(blockNumber: number): TxScopedL2Log {
12+
function makeLog(blockNumber: number, blockTimestamp: bigint): TxScopedL2Log {
1313
return new TxScopedL2Log(
1414
TxHash.random(),
1515
0,
1616
0,
1717
BlockNumber(blockNumber),
1818
L2BlockHash.random(),
19+
blockTimestamp,
1920
PrivateLog.random(),
2021
);
2122
}
2223

2324
it('returns undefined for highestAgedIndex when no logs are at least 24 hours old', () => {
2425
const finalizedBlockNumber = 10;
2526
const blockTimestamp = currentTimestamp - 1n; // not aged
26-
const log = makeLog(5);
27+
const log = makeLog(5, blockTimestamp);
2728

28-
const result = findHighestIndexes(
29-
[{ log, blockTimestamp, taggingIndex: 3 }],
30-
currentTimestamp,
31-
finalizedBlockNumber,
32-
);
29+
const result = findHighestIndexes([{ log, taggingIndex: 3 }], currentTimestamp, finalizedBlockNumber);
3330

3431
expect(result.highestAgedIndex).toBeUndefined();
3532
expect(result.highestFinalizedIndex).toBe(3);
@@ -38,13 +35,9 @@ describe('findHighestIndexes', () => {
3835
it('returns undefined for highestFinalizedIndex when no logs are in finalized blocks', () => {
3936
const finalizedBlockNumber = 5;
4037
const blockTimestamp = currentTimestamp - BigInt(MAX_INCLUDE_BY_TIMESTAMP_DURATION);
41-
const log = makeLog(10); // block 10 > finalizedBlockNumber 5
38+
const log = makeLog(10, blockTimestamp); // block 10 > finalizedBlockNumber 5
4239

43-
const result = findHighestIndexes(
44-
[{ log, blockTimestamp, taggingIndex: 3 }],
45-
currentTimestamp,
46-
finalizedBlockNumber,
47-
);
40+
const result = findHighestIndexes([{ log, taggingIndex: 3 }], currentTimestamp, finalizedBlockNumber);
4841

4942
expect(result.highestAgedIndex).toBe(3);
5043
expect(result.highestFinalizedIndex).toBeUndefined();
@@ -54,14 +47,14 @@ describe('findHighestIndexes', () => {
5447
const finalizedBlockNumber = 10;
5548
const blockTimestamp1 = currentTimestamp - BigInt(MAX_INCLUDE_BY_TIMESTAMP_DURATION) - 1000n; // aged
5649
const blockTimestamp2 = currentTimestamp - BigInt(MAX_INCLUDE_BY_TIMESTAMP_DURATION) - 500n; // aged
57-
const log1 = makeLog(5);
58-
const log2 = makeLog(6);
50+
const log1 = makeLog(5, blockTimestamp1);
51+
const log2 = makeLog(6, blockTimestamp2);
5952

6053
const result = findHighestIndexes(
6154
[
62-
{ log: log1, blockTimestamp: blockTimestamp1, taggingIndex: 2 },
63-
{ log: log2, blockTimestamp: blockTimestamp2, taggingIndex: 5 },
64-
{ log: log1, blockTimestamp: blockTimestamp1, taggingIndex: 3 },
55+
{ log: log1, taggingIndex: 2 },
56+
{ log: log2, taggingIndex: 5 },
57+
{ log: log1, taggingIndex: 3 },
6558
],
6659
currentTimestamp,
6760
finalizedBlockNumber,
@@ -74,15 +67,15 @@ describe('findHighestIndexes', () => {
7467
it('selects the highest index from multiple finalized logs', () => {
7568
const finalizedBlockNumber = 10;
7669
const blockTimestamp = currentTimestamp - 500n; // 500 seconds ago - not aged
77-
const log1 = makeLog(5);
78-
const log2 = makeLog(8);
79-
const log3 = makeLog(10); // At finalized block number
70+
const log1 = makeLog(5, blockTimestamp);
71+
const log2 = makeLog(8, blockTimestamp);
72+
const log3 = makeLog(10, blockTimestamp); // At finalized block number
8073

8174
const result = findHighestIndexes(
8275
[
83-
{ log: log1, blockTimestamp, taggingIndex: 2 },
84-
{ log: log2, blockTimestamp, taggingIndex: 7 },
85-
{ log: log3, blockTimestamp, taggingIndex: 1 },
76+
{ log: log1, taggingIndex: 2 },
77+
{ log: log2, taggingIndex: 7 },
78+
{ log: log3, taggingIndex: 1 },
8679
],
8780
currentTimestamp,
8881
finalizedBlockNumber,
@@ -99,11 +92,11 @@ describe('findHighestIndexes', () => {
9992
const recentTimestamp = currentTimestamp - 5000n; // Not aged
10093

10194
const logs = [
102-
{ log: makeLog(5), blockTimestamp: veryOldTimestamp, taggingIndex: 1 }, // Aged, finalized
103-
{ log: makeLog(8), blockTimestamp: oldTimestamp, taggingIndex: 5 }, // Aged, finalized
104-
{ log: makeLog(10), blockTimestamp: recentTimestamp, taggingIndex: 8 }, // Not aged, finalized
105-
{ log: makeLog(15), blockTimestamp: oldTimestamp, taggingIndex: 12 }, // Aged, not finalized
106-
{ log: makeLog(20), blockTimestamp: recentTimestamp, taggingIndex: 15 }, // Not aged, not finalized
95+
{ log: makeLog(5, veryOldTimestamp), taggingIndex: 1 }, // Aged, finalized
96+
{ log: makeLog(8, oldTimestamp), taggingIndex: 5 }, // Aged, finalized
97+
{ log: makeLog(10, recentTimestamp), taggingIndex: 8 }, // Not aged, finalized
98+
{ log: makeLog(15, oldTimestamp), taggingIndex: 12 }, // Aged, not finalized
99+
{ log: makeLog(20, recentTimestamp), taggingIndex: 15 }, // Not aged, not finalized
107100
];
108101

109102
const result = findHighestIndexes(logs, currentTimestamp, finalizedBlockNumber);

yarn-project/pxe/src/tagging/recipient_sync/utils/find_highest_indexes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import type { TxScopedL2Log } from '@aztec/stdlib/logs';
55
* Finds the highest aged and the highest finalized tagging indexes.
66
*/
77
export function findHighestIndexes(
8-
logsWithTimestampsAndIndexes: Array<{ log: TxScopedL2Log; blockTimestamp: bigint; taggingIndex: number }>,
8+
privateLogsWithIndexes: Array<{ log: TxScopedL2Log; taggingIndex: number }>,
99
currentTimestamp: bigint,
1010
finalizedBlockNumber: number,
1111
): { highestAgedIndex: number | undefined; highestFinalizedIndex: number | undefined } {
1212
let highestAgedIndex = undefined;
1313
let highestFinalizedIndex = undefined;
1414

15-
for (const { log, blockTimestamp, taggingIndex } of logsWithTimestampsAndIndexes) {
16-
const ageInSeconds = currentTimestamp - blockTimestamp;
15+
for (const { log, taggingIndex } of privateLogsWithIndexes) {
16+
const ageInSeconds = currentTimestamp - log.blockTimestamp;
1717

1818
if (
1919
ageInSeconds >= BigInt(MAX_INCLUDE_BY_TIMESTAMP_DURATION) &&

0 commit comments

Comments
 (0)