@aztec/archiver 0.0.1-commit.3469e52 → 0.0.1-commit.35158ae7e
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.
- package/README.md +9 -0
- package/dest/archiver.d.ts +12 -9
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +98 -124
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -1
- package/dest/errors.d.ts +26 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +34 -13
- package/dest/factory.d.ts +4 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +29 -23
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1/bin/retrieve-calldata.js +35 -32
- package/dest/l1/calldata_retriever.d.ts +73 -50
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +190 -259
- package/dest/l1/data_retrieval.d.ts +9 -9
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +24 -22
- package/dest/l1/spire_proposer.d.ts +5 -5
- package/dest/l1/spire_proposer.d.ts.map +1 -1
- package/dest/l1/spire_proposer.js +9 -17
- package/dest/l1/validate_trace.d.ts +6 -3
- package/dest/l1/validate_trace.d.ts.map +1 -1
- package/dest/l1/validate_trace.js +13 -9
- package/dest/modules/data_source_base.d.ts +27 -23
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +49 -124
- package/dest/modules/data_store_updater.d.ts +42 -26
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +149 -129
- package/dest/modules/instrumentation.d.ts +17 -4
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +36 -12
- package/dest/modules/l1_synchronizer.d.ts +5 -8
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +59 -25
- package/dest/store/block_store.d.ts +49 -32
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +188 -95
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +16 -72
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +64 -36
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +63 -29
- package/dest/store/l2_tips_cache.d.ts +19 -0
- package/dest/store/l2_tips_cache.d.ts.map +1 -0
- package/dest/store/l2_tips_cache.js +89 -0
- package/dest/store/log_store.d.ts +9 -6
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +150 -53
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +14 -1
- package/dest/test/fake_l1_state.d.ts +16 -4
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +95 -23
- package/dest/test/index.js +3 -1
- package/dest/test/mock_archiver.d.ts +1 -1
- package/dest/test/mock_archiver.d.ts.map +1 -1
- package/dest/test/mock_archiver.js +3 -2
- package/dest/test/mock_l2_block_source.d.ts +39 -23
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +157 -112
- package/dest/test/mock_structs.d.ts +6 -2
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +24 -10
- package/dest/test/noop_l1_archiver.d.ts +26 -0
- package/dest/test/noop_l1_archiver.d.ts.map +1 -0
- package/dest/test/noop_l1_archiver.js +72 -0
- package/package.json +14 -13
- package/src/archiver.ts +126 -151
- package/src/config.ts +8 -1
- package/src/errors.ts +52 -24
- package/src/factory.ts +46 -22
- package/src/index.ts +1 -0
- package/src/l1/README.md +25 -68
- package/src/l1/bin/retrieve-calldata.ts +45 -33
- package/src/l1/calldata_retriever.ts +249 -379
- package/src/l1/data_retrieval.ts +27 -29
- package/src/l1/spire_proposer.ts +7 -15
- package/src/l1/validate_trace.ts +24 -6
- package/src/modules/data_source_base.ts +84 -169
- package/src/modules/data_store_updater.ts +166 -161
- package/src/modules/instrumentation.ts +46 -14
- package/src/modules/l1_synchronizer.ts +72 -36
- package/src/store/block_store.ts +239 -140
- package/src/store/contract_class_store.ts +16 -110
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +104 -53
- package/src/store/l2_tips_cache.ts +89 -0
- package/src/store/log_store.ts +231 -70
- package/src/store/message_store.ts +20 -1
- package/src/test/fake_l1_state.ts +127 -28
- package/src/test/index.ts +3 -0
- package/src/test/mock_archiver.ts +3 -2
- package/src/test/mock_l2_block_source.ts +211 -129
- package/src/test/mock_structs.ts +45 -15
- package/src/test/noop_l1_archiver.ts +115 -0
|
@@ -2,14 +2,7 @@ import { Fr } from '@aztec/foundation/curves/bn254';
|
|
|
2
2
|
import { toArray } from '@aztec/foundation/iterable';
|
|
3
3
|
import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
4
|
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
ContractClassPublic,
|
|
8
|
-
ContractClassPublicWithBlockNumber,
|
|
9
|
-
ExecutablePrivateFunctionWithMembershipProof,
|
|
10
|
-
UtilityFunctionWithMembershipProof,
|
|
11
|
-
} from '@aztec/stdlib/contract';
|
|
12
|
-
import { Vector } from '@aztec/stdlib/types';
|
|
5
|
+
import type { ContractClassPublic, ContractClassPublicWithBlockNumber } from '@aztec/stdlib/contract';
|
|
13
6
|
|
|
14
7
|
/**
|
|
15
8
|
* LMDB-based contract class storage for the archiver.
|
|
@@ -28,18 +21,26 @@ export class ContractClassStore {
|
|
|
28
21
|
bytecodeCommitment: Fr,
|
|
29
22
|
blockNumber: number,
|
|
30
23
|
): Promise<void> {
|
|
31
|
-
await this
|
|
32
|
-
contractClass.id.toString()
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
await this.db.transactionAsync(async () => {
|
|
25
|
+
const key = contractClass.id.toString();
|
|
26
|
+
if (await this.#contractClasses.hasAsync(key)) {
|
|
27
|
+
throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
|
|
28
|
+
}
|
|
29
|
+
await this.#contractClasses.set(
|
|
30
|
+
key,
|
|
31
|
+
serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
|
|
32
|
+
);
|
|
33
|
+
await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
|
|
34
|
+
});
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
async deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
|
|
39
38
|
const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
|
|
40
39
|
if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
|
|
41
|
-
await this
|
|
42
|
-
|
|
40
|
+
await this.db.transactionAsync(async () => {
|
|
41
|
+
await this.#contractClasses.delete(contractClass.id.toString());
|
|
42
|
+
await this.#bytecodeCommitments.delete(contractClass.id.toString());
|
|
43
|
+
});
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
|
|
@@ -56,37 +57,6 @@ export class ContractClassStore {
|
|
|
56
57
|
async getContractClassIds(): Promise<Fr[]> {
|
|
57
58
|
return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
async addFunctions(
|
|
61
|
-
contractClassId: Fr,
|
|
62
|
-
newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
|
|
63
|
-
newUtilityFunctions: UtilityFunctionWithMembershipProof[],
|
|
64
|
-
): Promise<boolean> {
|
|
65
|
-
await this.db.transactionAsync(async () => {
|
|
66
|
-
const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
|
|
67
|
-
if (!existingClassBuffer) {
|
|
68
|
-
throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const existingClass = deserializeContractClassPublic(existingClassBuffer);
|
|
72
|
-
const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
|
|
73
|
-
|
|
74
|
-
const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
|
|
75
|
-
...existingClass,
|
|
76
|
-
privateFunctions: [
|
|
77
|
-
...existingPrivateFns,
|
|
78
|
-
...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
|
|
79
|
-
],
|
|
80
|
-
utilityFunctions: [
|
|
81
|
-
...existingUtilityFns,
|
|
82
|
-
...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
|
|
83
|
-
],
|
|
84
|
-
};
|
|
85
|
-
await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
60
|
}
|
|
91
61
|
|
|
92
62
|
function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
|
|
@@ -94,83 +64,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
|
|
|
94
64
|
contractClass.l2BlockNumber,
|
|
95
65
|
numToUInt8(contractClass.version),
|
|
96
66
|
contractClass.artifactHash,
|
|
97
|
-
contractClass.privateFunctions.length,
|
|
98
|
-
contractClass.privateFunctions.map(serializePrivateFunction),
|
|
99
|
-
contractClass.utilityFunctions.length,
|
|
100
|
-
contractClass.utilityFunctions.map(serializeUtilityFunction),
|
|
101
67
|
contractClass.packedBytecode.length,
|
|
102
68
|
contractClass.packedBytecode,
|
|
103
69
|
contractClass.privateFunctionsRoot,
|
|
104
70
|
);
|
|
105
71
|
}
|
|
106
72
|
|
|
107
|
-
function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
|
|
108
|
-
return serializeToBuffer(
|
|
109
|
-
fn.selector,
|
|
110
|
-
fn.vkHash,
|
|
111
|
-
fn.bytecode.length,
|
|
112
|
-
fn.bytecode,
|
|
113
|
-
fn.functionMetadataHash,
|
|
114
|
-
fn.artifactMetadataHash,
|
|
115
|
-
fn.utilityFunctionsTreeRoot,
|
|
116
|
-
new Vector(fn.privateFunctionTreeSiblingPath),
|
|
117
|
-
fn.privateFunctionTreeLeafIndex,
|
|
118
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
119
|
-
fn.artifactTreeLeafIndex,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
|
|
124
|
-
return serializeToBuffer(
|
|
125
|
-
fn.selector,
|
|
126
|
-
fn.bytecode.length,
|
|
127
|
-
fn.bytecode,
|
|
128
|
-
fn.functionMetadataHash,
|
|
129
|
-
fn.artifactMetadataHash,
|
|
130
|
-
fn.privateFunctionsArtifactTreeRoot,
|
|
131
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
132
|
-
fn.artifactTreeLeafIndex,
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
73
|
function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
|
|
137
74
|
const reader = BufferReader.asReader(buffer);
|
|
138
75
|
return {
|
|
139
76
|
l2BlockNumber: reader.readNumber(),
|
|
140
77
|
version: reader.readUInt8() as 1,
|
|
141
78
|
artifactHash: reader.readObject(Fr),
|
|
142
|
-
privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
|
|
143
|
-
utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
|
|
144
79
|
packedBytecode: reader.readBuffer(),
|
|
145
80
|
privateFunctionsRoot: reader.readObject(Fr),
|
|
146
81
|
};
|
|
147
82
|
}
|
|
148
|
-
|
|
149
|
-
function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
|
|
150
|
-
const reader = BufferReader.asReader(buffer);
|
|
151
|
-
return {
|
|
152
|
-
selector: reader.readObject(FunctionSelector),
|
|
153
|
-
vkHash: reader.readObject(Fr),
|
|
154
|
-
bytecode: reader.readBuffer(),
|
|
155
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
156
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
157
|
-
utilityFunctionsTreeRoot: reader.readObject(Fr),
|
|
158
|
-
privateFunctionTreeSiblingPath: reader.readVector(Fr),
|
|
159
|
-
privateFunctionTreeLeafIndex: reader.readNumber(),
|
|
160
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
161
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
|
|
166
|
-
const reader = BufferReader.asReader(buffer);
|
|
167
|
-
return {
|
|
168
|
-
selector: reader.readObject(FunctionSelector),
|
|
169
|
-
bytecode: reader.readBuffer(),
|
|
170
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
171
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
172
|
-
privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
|
|
173
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
174
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
175
|
-
};
|
|
176
|
-
}
|
|
@@ -27,11 +27,14 @@ export class ContractInstanceStore {
|
|
|
27
27
|
|
|
28
28
|
addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
|
|
29
29
|
return this.db.transactionAsync(async () => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
new
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
const key = contractInstance.address.toString();
|
|
31
|
+
if (await this.#contractInstances.hasAsync(key)) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
|
|
37
|
+
await this.#contractInstancePublishedAt.set(key, blockNumber);
|
|
35
38
|
});
|
|
36
39
|
}
|
|
37
40
|
|
|
@@ -6,16 +6,22 @@ import { createLogger } from '@aztec/foundation/log';
|
|
|
6
6
|
import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
|
|
7
7
|
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
8
8
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
type BlockData,
|
|
11
|
+
BlockHash,
|
|
12
|
+
CheckpointedL2Block,
|
|
13
|
+
L2Block,
|
|
14
|
+
type ValidateCheckpointResult,
|
|
15
|
+
} from '@aztec/stdlib/block';
|
|
16
|
+
import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
|
|
11
17
|
import type {
|
|
12
18
|
ContractClassPublic,
|
|
19
|
+
ContractClassPublicWithCommitment,
|
|
13
20
|
ContractDataSource,
|
|
14
21
|
ContractInstanceUpdateWithAddress,
|
|
15
22
|
ContractInstanceWithAddress,
|
|
16
|
-
ExecutablePrivateFunctionWithMembershipProof,
|
|
17
|
-
UtilityFunctionWithMembershipProof,
|
|
18
23
|
} from '@aztec/stdlib/contract';
|
|
24
|
+
import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
|
|
19
25
|
import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
|
|
20
26
|
import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
|
|
21
27
|
import type { BlockHeader, TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
@@ -24,13 +30,13 @@ import type { UInt64 } from '@aztec/stdlib/types';
|
|
|
24
30
|
import { join } from 'path';
|
|
25
31
|
|
|
26
32
|
import type { InboxMessage } from '../structs/inbox_message.js';
|
|
27
|
-
import { BlockStore, type
|
|
33
|
+
import { BlockStore, type RemoveCheckpointsResult } from './block_store.js';
|
|
28
34
|
import { ContractClassStore } from './contract_class_store.js';
|
|
29
35
|
import { ContractInstanceStore } from './contract_instance_store.js';
|
|
30
36
|
import { LogStore } from './log_store.js';
|
|
31
37
|
import { MessageStore } from './message_store.js';
|
|
32
38
|
|
|
33
|
-
export const ARCHIVER_DB_VERSION =
|
|
39
|
+
export const ARCHIVER_DB_VERSION = 6;
|
|
34
40
|
export const MAX_FUNCTION_SIGNATURES = 1000;
|
|
35
41
|
export const MAX_FUNCTION_NAME_LEN = 256;
|
|
36
42
|
|
|
@@ -72,6 +78,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
72
78
|
this.#contractInstanceStore = new ContractInstanceStore(db);
|
|
73
79
|
}
|
|
74
80
|
|
|
81
|
+
/** Returns the underlying block store. Used by L2TipsCache. */
|
|
82
|
+
get blockStore(): BlockStore {
|
|
83
|
+
return this.#blockStore;
|
|
84
|
+
}
|
|
85
|
+
|
|
75
86
|
/** Opens a new transaction to the underlying store and runs all operations within it. */
|
|
76
87
|
public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
77
88
|
return this.db.transactionAsync(callback);
|
|
@@ -101,6 +112,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
101
112
|
return this.db.close();
|
|
102
113
|
}
|
|
103
114
|
|
|
115
|
+
/** Computes the finalized block number based on the proven block number. */
|
|
116
|
+
getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
117
|
+
return this.#blockStore.getFinalizedL2BlockNumber();
|
|
118
|
+
}
|
|
119
|
+
|
|
104
120
|
/** Looks up a public function name given a selector. */
|
|
105
121
|
getDebugFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
|
|
106
122
|
return Promise.resolve(this.functionNames.get(selector.toString()));
|
|
@@ -150,19 +166,14 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
150
166
|
|
|
151
167
|
/**
|
|
152
168
|
* Add new contract classes from an L2 block to the store's list.
|
|
153
|
-
* @param data - List of contract classes to be added.
|
|
154
|
-
* @param bytecodeCommitments - Bytecode commitments for the contract classes.
|
|
169
|
+
* @param data - List of contract classes (with bytecode commitments) to be added.
|
|
155
170
|
* @param blockNumber - Number of the L2 block the contracts were registered in.
|
|
156
171
|
* @returns True if the operation is successful.
|
|
157
172
|
*/
|
|
158
|
-
async addContractClasses(
|
|
159
|
-
data: ContractClassPublic[],
|
|
160
|
-
bytecodeCommitments: Fr[],
|
|
161
|
-
blockNumber: BlockNumber,
|
|
162
|
-
): Promise<boolean> {
|
|
173
|
+
async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: BlockNumber): Promise<boolean> {
|
|
163
174
|
return (
|
|
164
175
|
await Promise.all(
|
|
165
|
-
data.map(
|
|
176
|
+
data.map(c => this.#contractClassStore.addContractClass(c, c.publicBytecodeCommitment, blockNumber)),
|
|
166
177
|
)
|
|
167
178
|
).every(Boolean);
|
|
168
179
|
}
|
|
@@ -177,15 +188,6 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
177
188
|
return this.#contractClassStore.getBytecodeCommitment(contractClassId);
|
|
178
189
|
}
|
|
179
190
|
|
|
180
|
-
/** Adds private functions to a contract class. */
|
|
181
|
-
addFunctions(
|
|
182
|
-
contractClassId: Fr,
|
|
183
|
-
privateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
|
|
184
|
-
utilityFunctions: UtilityFunctionWithMembershipProof[],
|
|
185
|
-
): Promise<boolean> {
|
|
186
|
-
return this.#contractClassStore.addFunctions(contractClassId, privateFunctions, utilityFunctions);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
191
|
/**
|
|
190
192
|
* Add new contract instances from an L2 block to the store's list.
|
|
191
193
|
* @param data - List of contract instances to be added.
|
|
@@ -228,12 +230,14 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
/**
|
|
231
|
-
* Append new
|
|
232
|
-
*
|
|
233
|
+
* Append a new proposed block to the store.
|
|
234
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
235
|
+
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
236
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
233
237
|
* @returns True if the operation is successful.
|
|
234
238
|
*/
|
|
235
|
-
|
|
236
|
-
return this.#blockStore.
|
|
239
|
+
addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
|
|
240
|
+
return this.#blockStore.addProposedBlock(block, opts);
|
|
237
241
|
}
|
|
238
242
|
|
|
239
243
|
/**
|
|
@@ -254,14 +258,12 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
254
258
|
}
|
|
255
259
|
|
|
256
260
|
/**
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* @param checkpointsToUnwind - The number of checkpoints we are to unwind
|
|
261
|
-
* @returns True if the operation is successful
|
|
261
|
+
* Removes all checkpoints with checkpoint number > checkpointNumber.
|
|
262
|
+
* Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
|
|
263
|
+
* @param checkpointNumber - Remove all checkpoints strictly after this one.
|
|
262
264
|
*/
|
|
263
|
-
|
|
264
|
-
return this.#blockStore.
|
|
265
|
+
removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<RemoveCheckpointsResult> {
|
|
266
|
+
return this.#blockStore.removeCheckpointsAfter(checkpointNumber);
|
|
265
267
|
}
|
|
266
268
|
|
|
267
269
|
/**
|
|
@@ -284,7 +286,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
284
286
|
* Returns the block for the given hash, or undefined if not exists.
|
|
285
287
|
* @param blockHash - The block hash to return.
|
|
286
288
|
*/
|
|
287
|
-
getCheckpointedBlockByHash(blockHash:
|
|
289
|
+
getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
|
|
288
290
|
return this.#blockStore.getCheckpointedBlockByHash(blockHash);
|
|
289
291
|
}
|
|
290
292
|
/**
|
|
@@ -298,21 +300,21 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
298
300
|
* Returns the block for the given number, or undefined if not exists.
|
|
299
301
|
* @param number - The block number to return.
|
|
300
302
|
*/
|
|
301
|
-
getBlock(number: BlockNumber): Promise<
|
|
303
|
+
getBlock(number: BlockNumber): Promise<L2Block | undefined> {
|
|
302
304
|
return this.#blockStore.getBlock(number);
|
|
303
305
|
}
|
|
304
306
|
/**
|
|
305
307
|
* Returns the block for the given hash, or undefined if not exists.
|
|
306
308
|
* @param blockHash - The block hash to return.
|
|
307
309
|
*/
|
|
308
|
-
getBlockByHash(blockHash:
|
|
309
|
-
return this.#blockStore.getBlockByHash(
|
|
310
|
+
getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
|
|
311
|
+
return this.#blockStore.getBlockByHash(blockHash);
|
|
310
312
|
}
|
|
311
313
|
/**
|
|
312
314
|
* Returns the block for the given archive root, or undefined if not exists.
|
|
313
315
|
* @param archive - The archive root to return.
|
|
314
316
|
*/
|
|
315
|
-
getBlockByArchive(archive: Fr): Promise<
|
|
317
|
+
getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
|
|
316
318
|
return this.#blockStore.getBlockByArchive(archive);
|
|
317
319
|
}
|
|
318
320
|
|
|
@@ -322,7 +324,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
322
324
|
* @param limit - The number of blocks to return.
|
|
323
325
|
* @returns The requested L2 blocks.
|
|
324
326
|
*/
|
|
325
|
-
getBlocks(from: BlockNumber, limit: number): Promise<
|
|
327
|
+
getBlocks(from: BlockNumber, limit: number): Promise<L2Block[]> {
|
|
326
328
|
return toArray(this.#blockStore.getBlocks(from, limit));
|
|
327
329
|
}
|
|
328
330
|
|
|
@@ -350,8 +352,8 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
350
352
|
* Returns the block header for the given hash, or undefined if not exists.
|
|
351
353
|
* @param blockHash - The block hash to return.
|
|
352
354
|
*/
|
|
353
|
-
getBlockHeaderByHash(blockHash:
|
|
354
|
-
return this.#blockStore.getBlockHeaderByHash(
|
|
355
|
+
getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
|
|
356
|
+
return this.#blockStore.getBlockHeaderByHash(blockHash);
|
|
355
357
|
}
|
|
356
358
|
|
|
357
359
|
/**
|
|
@@ -362,6 +364,22 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
362
364
|
return this.#blockStore.getBlockHeaderByArchive(archive);
|
|
363
365
|
}
|
|
364
366
|
|
|
367
|
+
/**
|
|
368
|
+
* Gets block metadata (without tx data) by block number.
|
|
369
|
+
* @param blockNumber - The block number to return.
|
|
370
|
+
*/
|
|
371
|
+
getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
|
|
372
|
+
return this.#blockStore.getBlockData(blockNumber);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Gets block metadata (without tx data) by archive root.
|
|
377
|
+
* @param archive - The archive root to return.
|
|
378
|
+
*/
|
|
379
|
+
getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
|
|
380
|
+
return this.#blockStore.getBlockDataByArchive(archive);
|
|
381
|
+
}
|
|
382
|
+
|
|
365
383
|
/**
|
|
366
384
|
* Gets a tx effect.
|
|
367
385
|
* @param txHash - The hash of the tx corresponding to the tx effect.
|
|
@@ -376,8 +394,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
376
394
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
377
395
|
* @returns The requested tx receipt (or undefined if not found).
|
|
378
396
|
*/
|
|
379
|
-
getSettledTxReceipt(
|
|
380
|
-
|
|
397
|
+
getSettledTxReceipt(
|
|
398
|
+
txHash: TxHash,
|
|
399
|
+
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
400
|
+
): Promise<TxReceipt | undefined> {
|
|
401
|
+
return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
|
|
381
402
|
}
|
|
382
403
|
|
|
383
404
|
/**
|
|
@@ -385,11 +406,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
385
406
|
* @param blocks - The blocks for which to add the logs.
|
|
386
407
|
* @returns True if the operation is successful.
|
|
387
408
|
*/
|
|
388
|
-
addLogs(blocks:
|
|
409
|
+
addLogs(blocks: L2Block[]): Promise<boolean> {
|
|
389
410
|
return this.#logStore.addLogs(blocks);
|
|
390
411
|
}
|
|
391
412
|
|
|
392
|
-
deleteLogs(blocks:
|
|
413
|
+
deleteLogs(blocks: L2Block[]): Promise<boolean> {
|
|
393
414
|
return this.#logStore.deleteLogs(blocks);
|
|
394
415
|
}
|
|
395
416
|
|
|
@@ -438,10 +459,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
438
459
|
* array implies no logs match that tag.
|
|
439
460
|
* @param tags - The tags to search for.
|
|
440
461
|
* @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
|
|
462
|
+
* @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
|
|
441
463
|
*/
|
|
442
|
-
getPrivateLogsByTags(tags: SiloedTag[], page?: number): Promise<TxScopedL2Log[][]> {
|
|
464
|
+
getPrivateLogsByTags(tags: SiloedTag[], page?: number, upToBlockNumber?: BlockNumber): Promise<TxScopedL2Log[][]> {
|
|
443
465
|
try {
|
|
444
|
-
return this.#logStore.getPrivateLogsByTags(tags, page);
|
|
466
|
+
return this.#logStore.getPrivateLogsByTags(tags, page, upToBlockNumber);
|
|
445
467
|
} catch (err) {
|
|
446
468
|
return Promise.reject(err);
|
|
447
469
|
}
|
|
@@ -453,14 +475,16 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
453
475
|
* @param contractAddress - The contract address to search logs for.
|
|
454
476
|
* @param tags - The tags to search for.
|
|
455
477
|
* @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
|
|
478
|
+
* @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
|
|
456
479
|
*/
|
|
457
480
|
getPublicLogsByTagsFromContract(
|
|
458
481
|
contractAddress: AztecAddress,
|
|
459
482
|
tags: Tag[],
|
|
460
483
|
page?: number,
|
|
484
|
+
upToBlockNumber?: BlockNumber,
|
|
461
485
|
): Promise<TxScopedL2Log[][]> {
|
|
462
486
|
try {
|
|
463
|
-
return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags, page);
|
|
487
|
+
return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
|
|
464
488
|
} catch (err) {
|
|
465
489
|
return Promise.reject(err);
|
|
466
490
|
}
|
|
@@ -508,6 +532,22 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
508
532
|
await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
|
|
509
533
|
}
|
|
510
534
|
|
|
535
|
+
/**
|
|
536
|
+
* Gets the number of the latest finalized checkpoint processed.
|
|
537
|
+
* @returns The number of the latest finalized checkpoint processed.
|
|
538
|
+
*/
|
|
539
|
+
getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
540
|
+
return this.#blockStore.getFinalizedCheckpointNumber();
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Stores the number of the latest finalized checkpoint processed.
|
|
545
|
+
* @param checkpointNumber - The number of the latest finalized checkpoint processed.
|
|
546
|
+
*/
|
|
547
|
+
async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
548
|
+
await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
|
|
549
|
+
}
|
|
550
|
+
|
|
511
551
|
async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
|
|
512
552
|
await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
|
|
513
553
|
}
|
|
@@ -551,6 +591,11 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
551
591
|
return this.#messageStore.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
|
|
552
592
|
}
|
|
553
593
|
|
|
594
|
+
/** Persists the inbox tree-in-progress checkpoint number from L1 state. */
|
|
595
|
+
public setInboxTreeInProgress(value: bigint): Promise<void> {
|
|
596
|
+
return this.#messageStore.setInboxTreeInProgress(value);
|
|
597
|
+
}
|
|
598
|
+
|
|
554
599
|
/** Returns an async iterator to all L1 to L2 messages on the range. */
|
|
555
600
|
public iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
|
|
556
601
|
return this.#messageStore.iterateL1ToL2Messages(range);
|
|
@@ -598,7 +643,7 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
598
643
|
* @param checkpointNumber Retrieves all blocks for the given checkpoint
|
|
599
644
|
* @returns The collection of blocks for the requested checkpoint if available (undefined otherwise)
|
|
600
645
|
*/
|
|
601
|
-
getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<
|
|
646
|
+
getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
|
|
602
647
|
return this.#blockStore.getBlocksForCheckpoint(checkpointNumber);
|
|
603
648
|
}
|
|
604
649
|
|
|
@@ -611,21 +656,27 @@ export class KVArchiverDataStore implements ContractDataSource {
|
|
|
611
656
|
return this.#blockStore.getCheckpointData(checkpointNumber);
|
|
612
657
|
}
|
|
613
658
|
|
|
659
|
+
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
|
|
660
|
+
getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
|
|
661
|
+
return this.#blockStore.getCheckpointDataForSlotRange(startSlot, endSlot);
|
|
662
|
+
}
|
|
663
|
+
|
|
614
664
|
/**
|
|
615
665
|
* Gets all blocks that have the given slot number.
|
|
616
666
|
* @param slotNumber - The slot number to search for.
|
|
617
667
|
* @returns All blocks with the given slot number.
|
|
618
668
|
*/
|
|
619
|
-
getBlocksForSlot(slotNumber: SlotNumber): Promise<
|
|
669
|
+
getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
|
|
620
670
|
return this.#blockStore.getBlocksForSlot(slotNumber);
|
|
621
671
|
}
|
|
622
672
|
|
|
623
673
|
/**
|
|
624
674
|
* Removes all blocks with block number > blockNumber.
|
|
675
|
+
* Does not remove any associated checkpoints.
|
|
625
676
|
* @param blockNumber - The block number to remove after.
|
|
626
677
|
* @returns The removed blocks (for event emission).
|
|
627
678
|
*/
|
|
628
|
-
removeBlocksAfter(blockNumber: BlockNumber): Promise<
|
|
629
|
-
return this.#blockStore.
|
|
679
|
+
removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
|
|
680
|
+
return this.#blockStore.removeBlocksAfter(blockNumber);
|
|
630
681
|
}
|
|
631
682
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { type BlockData, type CheckpointId, GENESIS_CHECKPOINT_HEADER_HASH, type L2Tips } from '@aztec/stdlib/block';
|
|
4
|
+
|
|
5
|
+
import type { BlockStore } from './block_store.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
|
|
9
|
+
* Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
|
|
10
|
+
* Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
|
|
11
|
+
*/
|
|
12
|
+
export class L2TipsCache {
|
|
13
|
+
#tipsPromise: Promise<L2Tips> | undefined;
|
|
14
|
+
|
|
15
|
+
constructor(private blockStore: BlockStore) {}
|
|
16
|
+
|
|
17
|
+
/** Returns the cached L2 tips. Loads from the block store on first call. */
|
|
18
|
+
public getL2Tips(): Promise<L2Tips> {
|
|
19
|
+
return (this.#tipsPromise ??= this.loadFromStore());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
|
|
23
|
+
public async refresh(): Promise<void> {
|
|
24
|
+
this.#tipsPromise = this.loadFromStore();
|
|
25
|
+
await this.#tipsPromise;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private async loadFromStore(): Promise<L2Tips> {
|
|
29
|
+
const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
30
|
+
this.blockStore.getLatestBlockNumber(),
|
|
31
|
+
this.blockStore.getProvenBlockNumber(),
|
|
32
|
+
this.blockStore.getCheckpointedL2BlockNumber(),
|
|
33
|
+
this.blockStore.getFinalizedL2BlockNumber(),
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
const genesisBlockHeader = {
|
|
37
|
+
blockHash: GENESIS_BLOCK_HEADER_HASH,
|
|
38
|
+
checkpointNumber: CheckpointNumber.ZERO,
|
|
39
|
+
} as const;
|
|
40
|
+
const beforeInitialBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
41
|
+
|
|
42
|
+
const getBlockData = (blockNumber: BlockNumber) =>
|
|
43
|
+
blockNumber > beforeInitialBlockNumber ? this.blockStore.getBlockData(blockNumber) : genesisBlockHeader;
|
|
44
|
+
|
|
45
|
+
const [latestBlockData, provenBlockData, checkpointedBlockData, finalizedBlockData] = await Promise.all(
|
|
46
|
+
[latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber].map(getBlockData),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
if (!latestBlockData || !provenBlockData || !finalizedBlockData || !checkpointedBlockData) {
|
|
50
|
+
throw new Error('Failed to load block data for L2 tips');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const [provenCheckpointId, finalizedCheckpointId, checkpointedCheckpointId] = await Promise.all([
|
|
54
|
+
this.getCheckpointIdForBlock(provenBlockData),
|
|
55
|
+
this.getCheckpointIdForBlock(finalizedBlockData),
|
|
56
|
+
this.getCheckpointIdForBlock(checkpointedBlockData),
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
proposed: { number: latestBlockNumber, hash: latestBlockData.blockHash.toString() },
|
|
61
|
+
proven: {
|
|
62
|
+
block: { number: provenBlockNumber, hash: provenBlockData.blockHash.toString() },
|
|
63
|
+
checkpoint: provenCheckpointId,
|
|
64
|
+
},
|
|
65
|
+
finalized: {
|
|
66
|
+
block: { number: finalizedBlockNumber, hash: finalizedBlockData.blockHash.toString() },
|
|
67
|
+
checkpoint: finalizedCheckpointId,
|
|
68
|
+
},
|
|
69
|
+
checkpointed: {
|
|
70
|
+
block: { number: checkpointedBlockNumber, hash: checkpointedBlockData.blockHash.toString() },
|
|
71
|
+
checkpoint: checkpointedCheckpointId,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async getCheckpointIdForBlock(blockData: Pick<BlockData, 'checkpointNumber'>): Promise<CheckpointId> {
|
|
77
|
+
const checkpointData = await this.blockStore.getCheckpointData(blockData.checkpointNumber);
|
|
78
|
+
if (!checkpointData) {
|
|
79
|
+
return {
|
|
80
|
+
number: CheckpointNumber.ZERO,
|
|
81
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
number: checkpointData.checkpointNumber,
|
|
86
|
+
hash: checkpointData.header.hash().toString(),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|