Skip to content

Commit 6716d66

Browse files
committed
Revert "fix(archiver): move L2 tips cache refresh out of write transactions"
This reverts commit 4735e42.
1 parent 4735e42 commit 6716d66

3 files changed

Lines changed: 11 additions & 40 deletions

File tree

yarn-project/archiver/src/modules/data_store_updater.test.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import { AztecAddress } from '@aztec/stdlib/aztec-address';
77
import { L2Block } from '@aztec/stdlib/block';
88
import { ContractClassLog, PrivateLog } from '@aztec/stdlib/logs';
99
import '@aztec/stdlib/testing/jest';
10-
import { BlockHeader } from '@aztec/stdlib/tx';
1110

12-
import { jest } from '@jest/globals';
1311
import { readFileSync } from 'fs';
1412
import { dirname, resolve } from 'path';
1513
import { fileURLToPath } from 'url';
1614

1715
import { type ArchiverDataStores, createArchiverDataStores } from '../store/data_stores.js';
18-
import { L2TipsCache } from '../store/l2_tips_cache.js';
1916
import { makeCheckpoint, makePublishedCheckpoint } from '../test/mock_structs.js';
2017
import { ArchiverDataStoreUpdater } from './data_store_updater.js';
2118

@@ -218,29 +215,4 @@ describe('ArchiverDataStoreUpdater', () => {
218215
expect(publicLogsAfter.logs.length).toBe(0);
219216
});
220217
});
221-
222-
describe('l2 tips cache refresh', () => {
223-
it('does not refresh the cache when the writer transaction aborts', async () => {
224-
const initialBlockHash = await BlockHeader.empty().hash();
225-
const tipsCache = new L2TipsCache(store.blocks, initialBlockHash);
226-
const updaterWithCache = new ArchiverDataStoreUpdater(store, tipsCache);
227-
228-
const tipsBefore = await tipsCache.getL2Tips();
229-
230-
const block = await L2Block.random(BlockNumber(1), {
231-
checkpointNumber: CheckpointNumber(1),
232-
indexWithinCheckpoint: IndexWithinCheckpoint(0),
233-
});
234-
235-
const failure = new Error('forced failure inside writer transaction');
236-
const addProposedBlockSpy = jest.spyOn(store.blocks, 'addProposedBlock').mockRejectedValueOnce(failure);
237-
238-
await expect(updaterWithCache.addProposedBlock(block)).rejects.toBe(failure);
239-
240-
const tipsAfter = await tipsCache.getL2Tips();
241-
expect(tipsAfter).toEqual(tipsBefore);
242-
243-
addProposedBlockSpy.mockRestore();
244-
});
245-
});
246218
});

yarn-project/archiver/src/modules/data_store_updater.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ export class ArchiverDataStoreUpdater {
7474
this.addContractDataToDb(block),
7575
]);
7676

77+
await this.l2TipsCache?.refresh();
7778
return opResults.every(Boolean);
7879
});
79-
await this.l2TipsCache?.refresh();
8080
return result;
8181
}
8282

@@ -144,17 +144,18 @@ export class ArchiverDataStoreUpdater {
144144
: undefined,
145145
]);
146146

147+
await this.l2TipsCache?.refresh();
147148
return { prunedBlocks, lastAlreadyInsertedBlockNumber };
148149
});
149-
await this.l2TipsCache?.refresh();
150150
return result;
151151
}
152152

153153
public async addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
154154
const result = await this.stores.db.transactionAsync(async () => {
155155
await this.stores.blocks.addProposedCheckpoint(proposedCheckpoint);
156+
await this.l2TipsCache?.refresh();
156157
});
157-
await this.l2TipsCache?.refresh();
158+
158159
return result;
159160
}
160161

@@ -255,9 +256,9 @@ export class ArchiverDataStoreUpdater {
255256
// Clear all pending proposed checkpoints since their blocks have been pruned
256257
await this.stores.blocks.deleteProposedCheckpoints();
257258

259+
await this.l2TipsCache?.refresh();
258260
return result;
259261
});
260-
await this.l2TipsCache?.refresh();
261262
return result;
262263
}
263264

@@ -288,7 +289,7 @@ export class ArchiverDataStoreUpdater {
288289
* @returns True if the operation is successful.
289290
*/
290291
public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
291-
const result = await this.stores.db.transactionAsync(async () => {
292+
return await this.stores.db.transactionAsync(async () => {
292293
const { blocksRemoved = [] } = await this.stores.blocks.removeCheckpointsAfter(checkpointNumber);
293294

294295
const opResults = await Promise.all([
@@ -299,10 +300,9 @@ export class ArchiverDataStoreUpdater {
299300
this.stores.logs.deleteLogs(blocksRemoved),
300301
]);
301302

303+
await this.l2TipsCache?.refresh();
302304
return opResults.every(Boolean);
303305
});
304-
await this.l2TipsCache?.refresh();
305-
return result;
306306
}
307307

308308
/**
@@ -312,8 +312,8 @@ export class ArchiverDataStoreUpdater {
312312
public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
313313
await this.stores.db.transactionAsync(async () => {
314314
await this.stores.blocks.setProvenCheckpointNumber(checkpointNumber);
315+
await this.l2TipsCache?.refresh();
315316
});
316-
await this.l2TipsCache?.refresh();
317317
}
318318

319319
/**
@@ -323,8 +323,8 @@ export class ArchiverDataStoreUpdater {
323323
public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
324324
await this.stores.db.transactionAsync(async () => {
325325
await this.stores.blocks.setFinalizedCheckpointNumber(checkpointNumber);
326+
await this.l2TipsCache?.refresh();
326327
});
327-
await this.l2TipsCache?.refresh();
328328
}
329329

330330
/** Extracts and stores contract data from a single block. */

yarn-project/archiver/src/store/l2_tips_cache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import type { BlockStore } from './block_store.js';
1313
/**
1414
* In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
1515
* Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
16-
* Refresh calls should happen *after* the store transaction that mutates block data has committed,
17-
* so the cache loads from committed state and is never replaced if the writer aborts.
16+
* Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
1817
*/
1918
export class L2TipsCache {
2019
#tipsPromise: Promise<L2Tips> | undefined;
@@ -35,7 +34,7 @@ export class L2TipsCache {
3534
return (this.#tipsPromise ??= this.loadFromStore());
3635
}
3736

38-
/** Reloads the L2 tips from the block store. Should be called after the writer transaction has committed. */
37+
/** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
3938
public async refresh(): Promise<void> {
4039
this.#tipsPromise = this.loadFromStore();
4140
await this.#tipsPromise;

0 commit comments

Comments
 (0)