@aztec/archiver 0.0.1-commit.f295ac2 → 0.0.1-commit.f504929

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +9 -0
  2. package/dest/archiver.d.ts +10 -6
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +50 -111
  5. package/dest/errors.d.ts +6 -1
  6. package/dest/errors.d.ts.map +1 -1
  7. package/dest/errors.js +8 -0
  8. package/dest/factory.d.ts +5 -2
  9. package/dest/factory.d.ts.map +1 -1
  10. package/dest/factory.js +16 -13
  11. package/dest/index.d.ts +2 -1
  12. package/dest/index.d.ts.map +1 -1
  13. package/dest/index.js +1 -0
  14. package/dest/l1/bin/retrieve-calldata.js +35 -32
  15. package/dest/l1/calldata_retriever.d.ts +73 -50
  16. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  17. package/dest/l1/calldata_retriever.js +190 -259
  18. package/dest/l1/data_retrieval.d.ts +9 -9
  19. package/dest/l1/data_retrieval.d.ts.map +1 -1
  20. package/dest/l1/data_retrieval.js +24 -22
  21. package/dest/l1/spire_proposer.d.ts +5 -5
  22. package/dest/l1/spire_proposer.d.ts.map +1 -1
  23. package/dest/l1/spire_proposer.js +9 -17
  24. package/dest/l1/validate_trace.d.ts +6 -3
  25. package/dest/l1/validate_trace.d.ts.map +1 -1
  26. package/dest/l1/validate_trace.js +13 -9
  27. package/dest/modules/data_source_base.d.ts +25 -21
  28. package/dest/modules/data_source_base.d.ts.map +1 -1
  29. package/dest/modules/data_source_base.js +48 -123
  30. package/dest/modules/data_store_updater.d.ts +31 -20
  31. package/dest/modules/data_store_updater.d.ts.map +1 -1
  32. package/dest/modules/data_store_updater.js +79 -60
  33. package/dest/modules/instrumentation.d.ts +17 -4
  34. package/dest/modules/instrumentation.d.ts.map +1 -1
  35. package/dest/modules/instrumentation.js +36 -12
  36. package/dest/modules/l1_synchronizer.d.ts +4 -8
  37. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  38. package/dest/modules/l1_synchronizer.js +23 -19
  39. package/dest/store/block_store.d.ts +50 -32
  40. package/dest/store/block_store.d.ts.map +1 -1
  41. package/dest/store/block_store.js +147 -54
  42. package/dest/store/contract_class_store.d.ts +1 -1
  43. package/dest/store/contract_class_store.d.ts.map +1 -1
  44. package/dest/store/contract_class_store.js +11 -7
  45. package/dest/store/kv_archiver_store.d.ts +52 -29
  46. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  47. package/dest/store/kv_archiver_store.js +49 -23
  48. package/dest/store/l2_tips_cache.d.ts +19 -0
  49. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  50. package/dest/store/l2_tips_cache.js +89 -0
  51. package/dest/store/log_store.d.ts +17 -8
  52. package/dest/store/log_store.d.ts.map +1 -1
  53. package/dest/store/log_store.js +77 -43
  54. package/dest/test/fake_l1_state.d.ts +9 -4
  55. package/dest/test/fake_l1_state.d.ts.map +1 -1
  56. package/dest/test/fake_l1_state.js +56 -18
  57. package/dest/test/index.js +3 -1
  58. package/dest/test/mock_archiver.d.ts +1 -1
  59. package/dest/test/mock_archiver.d.ts.map +1 -1
  60. package/dest/test/mock_archiver.js +3 -2
  61. package/dest/test/mock_l2_block_source.d.ts +36 -21
  62. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  63. package/dest/test/mock_l2_block_source.js +151 -109
  64. package/dest/test/mock_structs.d.ts +3 -2
  65. package/dest/test/mock_structs.d.ts.map +1 -1
  66. package/dest/test/mock_structs.js +11 -9
  67. package/dest/test/noop_l1_archiver.d.ts +23 -0
  68. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  69. package/dest/test/noop_l1_archiver.js +68 -0
  70. package/package.json +14 -13
  71. package/src/archiver.ts +71 -136
  72. package/src/errors.ts +12 -0
  73. package/src/factory.ts +30 -14
  74. package/src/index.ts +1 -0
  75. package/src/l1/README.md +25 -68
  76. package/src/l1/bin/retrieve-calldata.ts +45 -33
  77. package/src/l1/calldata_retriever.ts +249 -379
  78. package/src/l1/data_retrieval.ts +27 -29
  79. package/src/l1/spire_proposer.ts +7 -15
  80. package/src/l1/validate_trace.ts +24 -6
  81. package/src/modules/data_source_base.ts +81 -167
  82. package/src/modules/data_store_updater.ts +92 -63
  83. package/src/modules/instrumentation.ts +46 -14
  84. package/src/modules/l1_synchronizer.ts +26 -24
  85. package/src/store/block_store.ts +188 -92
  86. package/src/store/contract_class_store.ts +11 -7
  87. package/src/store/kv_archiver_store.ts +85 -36
  88. package/src/store/l2_tips_cache.ts +89 -0
  89. package/src/store/log_store.ts +134 -49
  90. package/src/test/fake_l1_state.ts +77 -19
  91. package/src/test/index.ts +3 -0
  92. package/src/test/mock_archiver.ts +3 -2
  93. package/src/test/mock_l2_block_source.ts +196 -126
  94. package/src/test/mock_structs.ts +26 -10
  95. package/src/test/noop_l1_archiver.ts +109 -0
@@ -6,7 +6,7 @@ import { times, timesParallel } from '@aztec/foundation/collection';
6
6
  import { randomBigInt, randomInt } from '@aztec/foundation/crypto/random';
7
7
  import { Fr } from '@aztec/foundation/curves/bn254';
8
8
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
9
- import { CommitteeAttestation, L2BlockNew } from '@aztec/stdlib/block';
9
+ import { CommitteeAttestation, L2Block } from '@aztec/stdlib/block';
10
10
  import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
11
11
  import { PrivateLog, PublicLog, SiloedTag, Tag } from '@aztec/stdlib/logs';
12
12
  import { InboxLeaf } from '@aztec/stdlib/messaging';
@@ -32,16 +32,18 @@ export function makeInboxMessage(previousRollingHash = Buffer16.ZERO, overrides
32
32
  rollingHash
33
33
  };
34
34
  }
35
- export function makeInboxMessages(count, opts = {}) {
36
- const { initialHash = Buffer16.ZERO, overrideFn = (msg)=>msg, initialCheckpointNumber = 1 } = opts;
35
+ export function makeInboxMessages(totalCount, opts = {}) {
36
+ const { initialHash = Buffer16.ZERO, overrideFn = (msg)=>msg, initialCheckpointNumber = CheckpointNumber(1), messagesPerCheckpoint = 1 } = opts;
37
37
  const messages = [];
38
38
  let rollingHash = initialHash;
39
- for(let i = 0; i < count; i++){
39
+ for(let i = 0; i < totalCount; i++){
40
+ const msgIndex = i % messagesPerCheckpoint;
41
+ const checkpointNumber = CheckpointNumber.fromBigInt(BigInt(initialCheckpointNumber) + BigInt(i) / BigInt(messagesPerCheckpoint));
40
42
  const leaf = Fr.random();
41
- const checkpointNumber = CheckpointNumber(i + initialCheckpointNumber);
42
43
  const message = overrideFn(makeInboxMessage(rollingHash, {
43
44
  leaf,
44
- checkpointNumber
45
+ checkpointNumber,
46
+ index: InboxLeaf.smallestIndexForCheckpoint(checkpointNumber) + BigInt(msgIndex)
45
47
  }), i);
46
48
  rollingHash = message.rollingHash;
47
49
  messages.push(message);
@@ -147,8 +149,8 @@ export function makeInboxMessages(count, opts = {}) {
147
149
  }
148
150
  /** Creates a checkpoint with specified logs on each tx effect. */ export async function makeCheckpointWithLogs(blockNumber, options = {}) {
149
151
  const { previousArchive, numTxsPerBlock = 4, privateLogs, publicLogs } = options;
150
- const block = await L2BlockNew.random(BlockNumber(blockNumber), {
151
- checkpointNumber: CheckpointNumber(blockNumber),
152
+ const block = await L2Block.random(BlockNumber(blockNumber), {
153
+ checkpointNumber: CheckpointNumber.fromBlockNumber(BlockNumber(blockNumber)),
152
154
  indexWithinCheckpoint: IndexWithinCheckpoint(0),
153
155
  state: makeStateForBlock(blockNumber, numTxsPerBlock),
154
156
  ...previousArchive ? {
@@ -164,6 +166,6 @@ export function makeInboxMessages(count, opts = {}) {
164
166
  });
165
167
  const checkpoint = new Checkpoint(AppendOnlyTreeSnapshot.random(), CheckpointHeader.random(), [
166
168
  block
167
- ], CheckpointNumber(blockNumber));
169
+ ], CheckpointNumber.fromBlockNumber(BlockNumber(blockNumber)));
168
170
  return makePublishedCheckpoint(checkpoint, blockNumber);
169
171
  }
@@ -0,0 +1,23 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
3
+ import { type TelemetryClient } from '@aztec/telemetry-client';
4
+ import { Archiver } from '../archiver.js';
5
+ import { ArchiverInstrumentation } from '../modules/instrumentation.js';
6
+ import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
7
+ /**
8
+ * Archiver with mocked L1 connectivity for testing.
9
+ * Uses mock L1 clients and a noop synchronizer, enabling tests that
10
+ * don't require real Ethereum connectivity.
11
+ */
12
+ export declare class NoopL1Archiver extends Archiver {
13
+ constructor(dataStore: KVArchiverDataStore, l1Constants: L1RollupConstants & {
14
+ genesisArchiveRoot: Fr;
15
+ }, instrumentation: ArchiverInstrumentation);
16
+ /** Override start to skip L1 validation checks. */
17
+ start(_blockUntilSynced?: boolean): Promise<void>;
18
+ }
19
+ /** Creates an archiver with mocked L1 connectivity for testing. */
20
+ export declare function createNoopL1Archiver(dataStore: KVArchiverDataStore, l1Constants: L1RollupConstants & {
21
+ genesisArchiveRoot: Fr;
22
+ }, telemetry?: TelemetryClient): Promise<NoopL1Archiver>;
23
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9vcF9sMV9hcmNoaXZlci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3Rlc3Qvbm9vcF9sMV9hcmNoaXZlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFJQSxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFJcEQsT0FBTyxLQUFLLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUNyRSxPQUFPLEVBQUUsS0FBSyxlQUFlLEVBQW1DLE1BQU0seUJBQXlCLENBQUM7QUFLaEcsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLGdCQUFnQixDQUFDO0FBQzFDLE9BQU8sRUFBRSx1QkFBdUIsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRXhFLE9BQU8sS0FBSyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sK0JBQStCLENBQUM7QUF5QnpFOzs7O0dBSUc7QUFDSCxxQkFBYSxjQUFlLFNBQVEsUUFBUTtJQUMxQyxZQUNFLFNBQVMsRUFBRSxtQkFBbUIsRUFDOUIsV0FBVyxFQUFFLGlCQUFpQixHQUFHO1FBQUUsa0JBQWtCLEVBQUUsRUFBRSxDQUFBO0tBQUUsRUFDM0QsZUFBZSxFQUFFLHVCQUF1QixFQXVDekM7SUFFRCxtREFBbUQ7SUFDbkMsS0FBSyxDQUFDLGlCQUFpQixDQUFDLEVBQUUsT0FBTyxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FJaEU7Q0FDRjtBQUVELG1FQUFtRTtBQUNuRSx3QkFBc0Isb0JBQW9CLENBQ3hDLFNBQVMsRUFBRSxtQkFBbUIsRUFDOUIsV0FBVyxFQUFFLGlCQUFpQixHQUFHO0lBQUUsa0JBQWtCLEVBQUUsRUFBRSxDQUFBO0NBQUUsRUFDM0QsU0FBUyxHQUFFLGVBQXNDLEdBQ2hELE9BQU8sQ0FBQyxjQUFjLENBQUMsQ0FHekIifQ==
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noop_l1_archiver.d.ts","sourceRoot":"","sources":["../../src/test/noop_l1_archiver.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AAIpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,KAAK,eAAe,EAAmC,MAAM,yBAAyB,CAAC;AAKhG,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAExE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AAyBzE;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,QAAQ;IAC1C,YACE,SAAS,EAAE,mBAAmB,EAC9B,WAAW,EAAE,iBAAiB,GAAG;QAAE,kBAAkB,EAAE,EAAE,CAAA;KAAE,EAC3D,eAAe,EAAE,uBAAuB,EAuCzC;IAED,mDAAmD;IACnC,KAAK,CAAC,iBAAiB,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAIhE;CACF;AAED,mEAAmE;AACnE,wBAAsB,oBAAoB,CACxC,SAAS,EAAE,mBAAmB,EAC9B,WAAW,EAAE,iBAAiB,GAAG;IAAE,kBAAkB,EAAE,EAAE,CAAA;CAAE,EAC3D,SAAS,GAAE,eAAsC,GAChD,OAAO,CAAC,cAAc,CAAC,CAGzB"}
@@ -0,0 +1,68 @@
1
+ import { Buffer32 } from '@aztec/foundation/buffer';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { getTelemetryClient } from '@aztec/telemetry-client';
4
+ import { mock } from 'jest-mock-extended';
5
+ import { EventEmitter } from 'node:events';
6
+ import { Archiver } from '../archiver.js';
7
+ import { ArchiverInstrumentation } from '../modules/instrumentation.js';
8
+ /** Noop L1 synchronizer for testing without L1 connectivity. */ class NoopL1Synchronizer {
9
+ tracer;
10
+ constructor(tracer){
11
+ this.tracer = tracer;
12
+ }
13
+ setConfig(_config) {}
14
+ getL1BlockNumber() {
15
+ return 0n;
16
+ }
17
+ getL1Timestamp() {
18
+ return 0n;
19
+ }
20
+ testEthereumNodeSynced() {
21
+ return Promise.resolve();
22
+ }
23
+ syncFromL1(_initialSyncComplete) {
24
+ return Promise.resolve();
25
+ }
26
+ }
27
+ /**
28
+ * Archiver with mocked L1 connectivity for testing.
29
+ * Uses mock L1 clients and a noop synchronizer, enabling tests that
30
+ * don't require real Ethereum connectivity.
31
+ */ export class NoopL1Archiver extends Archiver {
32
+ constructor(dataStore, l1Constants, instrumentation){
33
+ // Create mocks for L1 clients
34
+ const publicClient = mock();
35
+ const debugClient = mock();
36
+ const rollup = mock();
37
+ const blobClient = mock();
38
+ // Mock methods called during start()
39
+ blobClient.testSources.mockResolvedValue();
40
+ publicClient.getBlockNumber.mockResolvedValue(1n);
41
+ const events = new EventEmitter();
42
+ const synchronizer = new NoopL1Synchronizer(instrumentation.tracer);
43
+ super(publicClient, debugClient, rollup, {
44
+ registryAddress: EthAddress.ZERO,
45
+ governanceProposerAddress: EthAddress.ZERO,
46
+ slashFactoryAddress: EthAddress.ZERO,
47
+ slashingProposerAddress: EthAddress.ZERO
48
+ }, dataStore, {
49
+ pollingIntervalMs: 1000,
50
+ batchSize: 100,
51
+ skipValidateCheckpointAttestations: true,
52
+ maxAllowedEthClientDriftSeconds: 300,
53
+ ethereumAllowNoDebugHosts: true
54
+ }, blobClient, instrumentation, {
55
+ ...l1Constants,
56
+ l1StartBlockHash: Buffer32.random()
57
+ }, synchronizer, events);
58
+ }
59
+ /** Override start to skip L1 validation checks. */ start(_blockUntilSynced) {
60
+ // Just start the running promise without L1 checks
61
+ this.runningPromise.start();
62
+ return Promise.resolve();
63
+ }
64
+ }
65
+ /** Creates an archiver with mocked L1 connectivity for testing. */ export async function createNoopL1Archiver(dataStore, l1Constants, telemetry = getTelemetryClient()) {
66
+ const instrumentation = await ArchiverInstrumentation.new(telemetry, ()=>dataStore.estimateSize());
67
+ return new NoopL1Archiver(dataStore, l1Constants, instrumentation);
68
+ }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@aztec/archiver",
3
- "version": "0.0.1-commit.f295ac2",
3
+ "version": "0.0.1-commit.f504929",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
7
7
  "./test": "./dest/test/index.js",
8
+ "./test/noop-l1": "./dest/test/noop_l1_archiver.js",
8
9
  "./config": "./dest/config.js"
9
10
  },
10
11
  "typedocOptions": {
@@ -64,18 +65,18 @@
64
65
  ]
65
66
  },
66
67
  "dependencies": {
67
- "@aztec/blob-client": "0.0.1-commit.f295ac2",
68
- "@aztec/blob-lib": "0.0.1-commit.f295ac2",
69
- "@aztec/constants": "0.0.1-commit.f295ac2",
70
- "@aztec/epoch-cache": "0.0.1-commit.f295ac2",
71
- "@aztec/ethereum": "0.0.1-commit.f295ac2",
72
- "@aztec/foundation": "0.0.1-commit.f295ac2",
73
- "@aztec/kv-store": "0.0.1-commit.f295ac2",
74
- "@aztec/l1-artifacts": "0.0.1-commit.f295ac2",
75
- "@aztec/noir-protocol-circuits-types": "0.0.1-commit.f295ac2",
76
- "@aztec/protocol-contracts": "0.0.1-commit.f295ac2",
77
- "@aztec/stdlib": "0.0.1-commit.f295ac2",
78
- "@aztec/telemetry-client": "0.0.1-commit.f295ac2",
68
+ "@aztec/blob-client": "0.0.1-commit.f504929",
69
+ "@aztec/blob-lib": "0.0.1-commit.f504929",
70
+ "@aztec/constants": "0.0.1-commit.f504929",
71
+ "@aztec/epoch-cache": "0.0.1-commit.f504929",
72
+ "@aztec/ethereum": "0.0.1-commit.f504929",
73
+ "@aztec/foundation": "0.0.1-commit.f504929",
74
+ "@aztec/kv-store": "0.0.1-commit.f504929",
75
+ "@aztec/l1-artifacts": "0.0.1-commit.f504929",
76
+ "@aztec/noir-protocol-circuits-types": "0.0.1-commit.f504929",
77
+ "@aztec/protocol-contracts": "0.0.1-commit.f504929",
78
+ "@aztec/stdlib": "0.0.1-commit.f504929",
79
+ "@aztec/telemetry-client": "0.0.1-commit.f504929",
79
80
  "lodash.groupby": "^4.6.0",
80
81
  "lodash.omit": "^4.5.0",
81
82
  "tslib": "^2.5.0",
package/src/archiver.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
3
2
  import { EpochCache } from '@aztec/epoch-cache';
4
3
  import { BlockTagTooOldError, RollupContract } from '@aztec/ethereum/contracts';
5
4
  import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
@@ -15,9 +14,7 @@ import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/runni
15
14
  import { DateProvider } from '@aztec/foundation/timer';
16
15
  import {
17
16
  type ArchiverEmitter,
18
- type CheckpointId,
19
- GENESIS_CHECKPOINT_HEADER_HASH,
20
- L2BlockNew,
17
+ L2Block,
21
18
  type L2BlockSink,
22
19
  type L2Tips,
23
20
  type ValidateCheckpointResult,
@@ -26,6 +23,7 @@ import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
26
23
  import {
27
24
  type L1RollupConstants,
28
25
  getEpochNumberAtTimestamp,
26
+ getSlotAtNextL1Block,
29
27
  getSlotAtTimestamp,
30
28
  getSlotRangeForEpoch,
31
29
  getTimestampRangeForEpoch,
@@ -40,13 +38,14 @@ import { ArchiverDataStoreUpdater } from './modules/data_store_updater.js';
40
38
  import type { ArchiverInstrumentation } from './modules/instrumentation.js';
41
39
  import type { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
42
40
  import type { KVArchiverDataStore } from './store/kv_archiver_store.js';
41
+ import { L2TipsCache } from './store/l2_tips_cache.js';
43
42
 
44
43
  /** Export ArchiverEmitter for use in factory and tests. */
45
44
  export type { ArchiverEmitter };
46
45
 
47
46
  /** Request to add a block to the archiver, queued for processing by the sync loop. */
48
47
  type AddBlockRequest = {
49
- block: L2BlockNew;
48
+ block: L2Block;
50
49
  resolve: () => void;
51
50
  reject: (err: Error) => void;
52
51
  };
@@ -68,7 +67,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
68
67
  public readonly events: ArchiverEmitter;
69
68
 
70
69
  /** A loop in which we will be continually fetching new checkpoints. */
71
- private runningPromise: RunningPromise;
70
+ protected runningPromise: RunningPromise;
72
71
 
73
72
  /** L1 synchronizer that handles fetching checkpoints and messages from L1. */
74
73
  private readonly synchronizer: ArchiverL1Synchronizer;
@@ -82,6 +81,9 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
82
81
  /** Helper to handle updates to the store */
83
82
  private readonly updater: ArchiverDataStoreUpdater;
84
83
 
84
+ /** In-memory cache for L2 chain tips. */
85
+ private readonly l2TipsCache: L2TipsCache;
86
+
85
87
  public readonly tracer: Tracer;
86
88
 
87
89
  /**
@@ -121,6 +123,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
121
123
  protected override readonly l1Constants: L1RollupConstants & { l1StartBlockHash: Buffer32; genesisArchiveRoot: Fr },
122
124
  synchronizer: ArchiverL1Synchronizer,
123
125
  events: ArchiverEmitter,
126
+ l2TipsCache?: L2TipsCache,
124
127
  private readonly log: Logger = createLogger('archiver'),
125
128
  ) {
126
129
  super(dataStore, l1Constants);
@@ -129,7 +132,8 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
129
132
  this.initialSyncPromise = promiseWithResolvers();
130
133
  this.synchronizer = synchronizer;
131
134
  this.events = events;
132
- this.updater = new ArchiverDataStoreUpdater(this.dataStore);
135
+ this.l2TipsCache = l2TipsCache ?? new L2TipsCache(this.dataStore.blockStore);
136
+ this.updater = new ArchiverDataStoreUpdater(this.dataStore, this.l2TipsCache);
133
137
 
134
138
  // Running promise starts with a small interval inbetween runs, so all iterations needed for the initial sync
135
139
  // are done as fast as possible. This then gets updated once the initial sync completes.
@@ -158,7 +162,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
158
162
 
159
163
  await this.blobClient.testSources();
160
164
  await this.synchronizer.testEthereumNodeSynced();
161
- await validateAndLogTraceAvailability(this.debugClient, this.config.ethereumAllowNoDebugHosts ?? false);
165
+ await validateAndLogTraceAvailability(
166
+ this.debugClient,
167
+ this.config.ethereumAllowNoDebugHosts ?? false,
168
+ this.log.getBindings(),
169
+ );
162
170
 
163
171
  // Log initial state for the archiver
164
172
  const { l1StartBlock } = this.l1Constants;
@@ -187,7 +195,7 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
187
195
  * @param block - The L2 block to add.
188
196
  * @returns A promise that resolves when the block has been added to the store, or rejects on error.
189
197
  */
190
- public addBlock(block: L2BlockNew): Promise<void> {
198
+ public addBlock(block: L2Block): Promise<void> {
191
199
  return new Promise<void>((resolve, reject) => {
192
200
  this.blockQueue.push({ block, resolve, reject });
193
201
  this.log.debug(`Queued block ${block.number} for processing`);
@@ -212,10 +220,25 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
212
220
  const queuedItems = this.blockQueue.splice(0, this.blockQueue.length);
213
221
  this.log.debug(`Processing ${queuedItems.length} queued block(s)`);
214
222
 
223
+ // Calculate slot threshold for validation
224
+ const l1Timestamp = this.synchronizer.getL1Timestamp();
225
+ const slotAtNextL1Block =
226
+ l1Timestamp === undefined ? undefined : getSlotAtNextL1Block(l1Timestamp, this.l1Constants);
227
+
215
228
  // Process each block individually to properly resolve/reject each promise
216
229
  for (const { block, resolve, reject } of queuedItems) {
230
+ const blockSlot = block.header.globalVariables.slotNumber;
231
+ if (slotAtNextL1Block !== undefined && blockSlot < slotAtNextL1Block) {
232
+ this.log.warn(
233
+ `Rejecting proposed block ${block.number} for past slot ${blockSlot} (current is ${slotAtNextL1Block})`,
234
+ { block: block.toBlockInfo(), l1Timestamp, slotAtNextL1Block },
235
+ );
236
+ reject(new Error(`Block ${block.number} is for past slot ${blockSlot} (current is ${slotAtNextL1Block})`));
237
+ continue;
238
+ }
239
+
217
240
  try {
218
- await this.updater.addBlocks([block]);
241
+ await this.updater.addProposedBlocks([block]);
219
242
  this.log.debug(`Added block ${block.number} to store`);
220
243
  resolve();
221
244
  } catch (err: any) {
@@ -323,8 +346,11 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
323
346
  }
324
347
 
325
348
  public async isEpochComplete(epochNumber: EpochNumber): Promise<boolean> {
326
- // The epoch is complete if the current L2 block is the last one in the epoch (or later)
327
- const header = await this.getBlockHeader('latest');
349
+ // The epoch is complete if the current checkpointed L2 block is the last one in the epoch (or later).
350
+ // We use the checkpointed block number (synced from L1) instead of 'latest' to avoid returning true
351
+ // prematurely when proposed blocks have been pushed to the archiver but not yet checkpointed on L1.
352
+ const checkpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
353
+ const header = checkpointedBlockNumber > 0 ? await this.getBlockHeader(checkpointedBlockNumber) : undefined;
328
354
  const slot = header ? header.globalVariables.slotNumber : undefined;
329
355
  const [_startSlot, endSlot] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
330
356
  if (slot && slot >= endSlot) {
@@ -355,8 +381,8 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
355
381
  return this.initialSyncComplete;
356
382
  }
357
383
 
358
- public unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
359
- return this.updater.unwindCheckpoints(from, checkpointsToUnwind);
384
+ public removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
385
+ return this.updater.removeCheckpointsAfter(checkpointNumber);
360
386
  }
361
387
 
362
388
  /** Used by TXE to add checkpoints directly without syncing from L1. */
@@ -364,125 +390,15 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
364
390
  checkpoints: PublishedCheckpoint[],
365
391
  pendingChainValidationStatus?: ValidateCheckpointResult,
366
392
  ): Promise<boolean> {
367
- await this.updater.setNewCheckpointData(checkpoints, pendingChainValidationStatus);
393
+ await this.updater.addCheckpoints(checkpoints, pendingChainValidationStatus);
368
394
  return true;
369
395
  }
370
396
 
371
- public async getL2Tips(): Promise<L2Tips> {
372
- const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber] = await Promise.all([
373
- this.getBlockNumber(),
374
- this.getProvenBlockNumber(),
375
- this.getCheckpointedBlockNumber(),
376
- ] as const);
377
-
378
- // TODO(#13569): Compute proper finalized block number based on L1 finalized block.
379
- // We just force it 2 epochs worth of proven data for now.
380
- // NOTE: update end-to-end/src/e2e_epochs/epochs_empty_blocks.test.ts as that uses finalized blocks in computations
381
- const finalizedBlockNumber = BlockNumber(Math.max(provenBlockNumber - this.l1Constants.epochDuration * 2, 0));
382
-
383
- const beforeInitialblockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
384
-
385
- // Get the latest block header and checkpointed blocks for proven, finalised and checkpointed blocks
386
- const [latestBlockHeader, provenCheckpointedBlock, finalizedCheckpointedBlock, checkpointedBlock] =
387
- await Promise.all([
388
- latestBlockNumber > beforeInitialblockNumber ? this.getBlockHeader(latestBlockNumber) : undefined,
389
- provenBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(provenBlockNumber) : undefined,
390
- finalizedBlockNumber > beforeInitialblockNumber ? this.getCheckpointedBlock(finalizedBlockNumber) : undefined,
391
- checkpointedBlockNumber > beforeInitialblockNumber
392
- ? this.getCheckpointedBlock(checkpointedBlockNumber)
393
- : undefined,
394
- ] as const);
395
-
396
- if (latestBlockNumber > beforeInitialblockNumber && !latestBlockHeader) {
397
- throw new Error(`Failed to retrieve latest block header for block ${latestBlockNumber}`);
398
- }
399
-
400
- // Checkpointed blocks must exist for proven, finalized and checkpointed tips if they are beyond the initial block number.
401
- if (checkpointedBlockNumber > beforeInitialblockNumber && !checkpointedBlock?.block.header) {
402
- throw new Error(
403
- `Failed to retrieve checkpointed block header for block ${checkpointedBlockNumber} (latest block is ${latestBlockNumber})`,
404
- );
405
- }
406
-
407
- if (provenBlockNumber > beforeInitialblockNumber && !provenCheckpointedBlock?.block.header) {
408
- throw new Error(
409
- `Failed to retrieve proven checkpointed for block ${provenBlockNumber} (latest block is ${latestBlockNumber})`,
410
- );
411
- }
412
-
413
- if (finalizedBlockNumber > beforeInitialblockNumber && !finalizedCheckpointedBlock?.block.header) {
414
- throw new Error(
415
- `Failed to retrieve finalized block header for block ${finalizedBlockNumber} (latest block is ${latestBlockNumber})`,
416
- );
417
- }
418
-
419
- const latestBlockHeaderHash = (await latestBlockHeader?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
420
- const provenBlockHeaderHash = (await provenCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
421
- const finalizedBlockHeaderHash =
422
- (await finalizedCheckpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
423
- const checkpointedBlockHeaderHash = (await checkpointedBlock?.block.header?.hash()) ?? GENESIS_BLOCK_HEADER_HASH;
424
-
425
- // Now attempt to retrieve checkpoints for proven, finalised and checkpointed blocks
426
- const [[provenBlockCheckpoint], [finalizedBlockCheckpoint], [checkpointedBlockCheckpoint]] = await Promise.all([
427
- provenCheckpointedBlock !== undefined
428
- ? await this.getPublishedCheckpoints(provenCheckpointedBlock?.checkpointNumber, 1)
429
- : [undefined],
430
- finalizedCheckpointedBlock !== undefined
431
- ? await this.getPublishedCheckpoints(finalizedCheckpointedBlock?.checkpointNumber, 1)
432
- : [undefined],
433
- checkpointedBlock !== undefined
434
- ? await this.getPublishedCheckpoints(checkpointedBlock?.checkpointNumber, 1)
435
- : [undefined],
436
- ]);
437
-
438
- const initialcheckpointId: CheckpointId = {
439
- number: CheckpointNumber.ZERO,
440
- hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
441
- };
442
-
443
- const makeCheckpointId = (checkpoint: PublishedCheckpoint | undefined) => {
444
- if (checkpoint === undefined) {
445
- return initialcheckpointId;
446
- }
447
- return {
448
- number: checkpoint.checkpoint.number,
449
- hash: checkpoint.checkpoint.hash().toString(),
450
- };
451
- };
452
-
453
- const l2Tips: L2Tips = {
454
- proposed: {
455
- number: latestBlockNumber,
456
- hash: latestBlockHeaderHash.toString(),
457
- },
458
- proven: {
459
- block: {
460
- number: provenBlockNumber,
461
- hash: provenBlockHeaderHash.toString(),
462
- },
463
- checkpoint: makeCheckpointId(provenBlockCheckpoint),
464
- },
465
- finalized: {
466
- block: {
467
- number: finalizedBlockNumber,
468
- hash: finalizedBlockHeaderHash.toString(),
469
- },
470
- checkpoint: makeCheckpointId(finalizedBlockCheckpoint),
471
- },
472
- checkpointed: {
473
- block: {
474
- number: checkpointedBlockNumber,
475
- hash: checkpointedBlockHeaderHash.toString(),
476
- },
477
- checkpoint: makeCheckpointId(checkpointedBlockCheckpoint),
478
- },
479
- };
480
-
481
- return l2Tips;
397
+ public getL2Tips(): Promise<L2Tips> {
398
+ return this.l2TipsCache.getL2Tips();
482
399
  }
483
400
 
484
401
  public async rollbackTo(targetL2BlockNumber: BlockNumber): Promise<void> {
485
- // TODO(pw/mbps): This still assumes 1 block per checkpoint
486
402
  const currentBlocks = await this.getL2Tips();
487
403
  const currentL2Block = currentBlocks.proposed.number;
488
404
  const currentProvenBlock = currentBlocks.proven.block.number;
@@ -490,13 +406,29 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
490
406
  if (targetL2BlockNumber >= currentL2Block) {
491
407
  throw new Error(`Target L2 block ${targetL2BlockNumber} must be less than current L2 block ${currentL2Block}`);
492
408
  }
493
- const blocksToUnwind = currentL2Block - targetL2BlockNumber;
494
409
  const targetL2Block = await this.store.getCheckpointedBlock(targetL2BlockNumber);
495
410
  if (!targetL2Block) {
496
411
  throw new Error(`Target L2 block ${targetL2BlockNumber} not found`);
497
412
  }
413
+ const targetCheckpointNumber = targetL2Block.checkpointNumber;
414
+
415
+ // Rollback operates at checkpoint granularity: the target block must be the last block of its checkpoint.
416
+ const checkpointData = await this.store.getCheckpointData(targetCheckpointNumber);
417
+ if (checkpointData) {
418
+ const lastBlockInCheckpoint = BlockNumber(checkpointData.startBlock + checkpointData.blockCount - 1);
419
+ if (targetL2BlockNumber !== lastBlockInCheckpoint) {
420
+ const previousCheckpointBoundary =
421
+ checkpointData.startBlock > 1 ? BlockNumber(checkpointData.startBlock - 1) : BlockNumber(0);
422
+ throw new Error(
423
+ `Target L2 block ${targetL2BlockNumber} is not at a checkpoint boundary. ` +
424
+ `Checkpoint ${targetCheckpointNumber} spans blocks ${checkpointData.startBlock} to ${lastBlockInCheckpoint}. ` +
425
+ `Use block ${lastBlockInCheckpoint} to roll back to this checkpoint, ` +
426
+ `or block ${previousCheckpointBoundary} to roll back to the previous one.`,
427
+ );
428
+ }
429
+ }
430
+
498
431
  const targetL1BlockNumber = targetL2Block.l1.blockNumber;
499
- const targetCheckpointNumber = CheckpointNumber.fromBlockNumber(targetL2BlockNumber);
500
432
  const targetL1Block = await this.publicClient.getBlock({
501
433
  blockNumber: targetL1BlockNumber,
502
434
  includeTransactions: false,
@@ -505,21 +437,24 @@ export class Archiver extends ArchiverDataSourceBase implements L2BlockSink, Tra
505
437
  throw new Error(`Missing L1 block ${targetL1BlockNumber}`);
506
438
  }
507
439
  const targetL1BlockHash = Buffer32.fromString(targetL1Block.hash);
508
- this.log.info(`Unwinding ${blocksToUnwind} checkpoints from L2 block ${currentL2Block}`);
509
- await this.updater.unwindCheckpoints(CheckpointNumber(currentL2Block), blocksToUnwind);
510
- this.log.info(`Unwinding L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
440
+ this.log.info(
441
+ `Removing checkpoints after checkpoint ${targetCheckpointNumber} (target block ${targetL2BlockNumber})`,
442
+ );
443
+ await this.updater.removeCheckpointsAfter(targetCheckpointNumber);
444
+ this.log.info(`Rolling back L1 to L2 messages to checkpoint ${targetCheckpointNumber}`);
511
445
  await this.store.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
512
446
  this.log.info(`Setting L1 syncpoints to ${targetL1BlockNumber}`);
513
447
  await this.store.setCheckpointSynchedL1BlockNumber(targetL1BlockNumber);
514
448
  await this.store.setMessageSynchedL1Block({ l1BlockNumber: targetL1BlockNumber, l1BlockHash: targetL1BlockHash });
515
449
  if (targetL2BlockNumber < currentProvenBlock) {
516
- this.log.info(`Clearing proven L2 block number`);
517
- await this.store.setProvenCheckpointNumber(CheckpointNumber.ZERO);
450
+ this.log.info(`Rolling back proven L2 checkpoint to ${targetCheckpointNumber}`);
451
+ await this.updater.setProvenCheckpointNumber(targetCheckpointNumber);
518
452
  }
519
453
  // TODO(palla/reorg): Set the finalized block when we add support for it.
454
+ // const currentFinalizedBlock = currentBlocks.finalized.block.number;
520
455
  // if (targetL2BlockNumber < currentFinalizedBlock) {
521
- // this.log.info(`Clearing finalized L2 block number`);
522
- // await this.store.setFinalizedL2BlockNumber(0);
456
+ // this.log.info(`Rolling back finalized L2 checkpoint to ${targetCheckpointNumber}`);
457
+ // await this.updater.setFinalizedCheckpointNumber(targetCheckpointNumber);
523
458
  // }
524
459
  }
525
460
  }
package/src/errors.ts CHANGED
@@ -88,3 +88,15 @@ export class BlockNotFoundError extends Error {
88
88
  super(`Failed to find expected block number ${blockNumber}`);
89
89
  }
90
90
  }
91
+
92
+ export class CannotOverwriteCheckpointedBlockError extends Error {
93
+ constructor(
94
+ public readonly blockNumber: number,
95
+ public readonly lastCheckpointedBlock: number,
96
+ ) {
97
+ super(
98
+ `Cannot add block ${blockNumber}: would overwrite checkpointed data (checkpointed up to block ${lastCheckpointedBlock})`,
99
+ );
100
+ this.name = 'CannotOverwriteCheckpointedBlockError';
101
+ }
102
+ }