1
- import {
2
- MAX_NEW_NULLIFIERS_PER_TX ,
3
- MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX ,
4
- NullifierLeafPreimage ,
5
- } from '@aztec/circuits.js' ;
6
- import { Fr } from '@aztec/foundation/fields' ;
7
- import { createDebugLogger } from '@aztec/foundation/log' ;
8
- import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees' ;
9
- import { BatchInsertionResult , IndexedTreeSnapshot , TreeSnapshot } from '@aztec/merkle-tree' ;
10
- import { L2Block , MerkleTreeId , SiblingPath } from '@aztec/types' ;
1
+ import { MAX_NEW_NULLIFIERS_PER_TX , MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/circuits.js' ;
2
+ import { IndexedTreeSnapshot , TreeSnapshot } from '@aztec/merkle-tree' ;
11
3
12
- /**
13
- * Type alias for the nullifier tree ID.
14
- */
15
- export type IndexedTreeId = MerkleTreeId . NULLIFIER_TREE | MerkleTreeId . PUBLIC_DATA_TREE ;
4
+ import { MerkleTreeOperations } from './merkle_tree_operations.js' ;
16
5
17
6
/**
18
7
*
@@ -33,54 +22,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NEW_NULLIFIERS_PER_TX;
33
22
34
23
export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX ;
35
24
36
- /**
37
- * Defines tree information.
38
- */
39
- export interface TreeInfo {
40
- /**
41
- * The tree ID.
42
- */
43
- treeId : MerkleTreeId ;
44
- /**
45
- * The tree root.
46
- */
47
- root : Buffer ;
48
- /**
49
- * The number of leaves in the tree.
50
- */
51
- size : bigint ;
52
-
53
- /**
54
- * The depth of the tree.
55
- */
56
- depth : number ;
57
- }
58
-
59
25
/**
60
26
* Adds a last boolean flag in each function on the type.
61
27
*/
62
28
type WithIncludeUncommitted < F > = F extends ( ...args : [ ...infer Rest ] ) => infer Return
63
29
? ( ...args : [ ...Rest , boolean ] ) => Return
64
30
: F ;
65
31
66
- /**
67
- * The current roots of the commitment trees
68
- */
69
- export type CurrentTreeRoots = {
70
- /** Note Hash Tree root. */
71
- noteHashTreeRoot : Buffer ;
72
- /** Contract data tree root. */
73
- contractDataTreeRoot : Buffer ;
74
- /** L1 to L2 Messages data tree root. */
75
- l1Tol2MessagesTreeRoot : Buffer ;
76
- /** Nullifier data tree root. */
77
- nullifierTreeRoot : Buffer ;
78
- /** Archive root. */
79
- archiveRoot : Buffer ;
80
- /** Public data tree root */
81
- publicDataTreeRoot : Buffer ;
82
- } ;
83
-
84
32
/**
85
33
* Defines the names of the setters on Merkle Trees.
86
34
*/
@@ -100,154 +48,3 @@ export type MerkleTreeDb = {
100
48
*/
101
49
getSnapshot ( block : number ) : Promise < ReadonlyArray < TreeSnapshot | IndexedTreeSnapshot > > ;
102
50
} ;
103
-
104
- /**
105
- * Defines the interface for operations on a set of Merkle Trees.
106
- */
107
- export interface MerkleTreeOperations {
108
- /**
109
- * Appends leaves to a given tree.
110
- * @param treeId - The tree to be updated.
111
- * @param leaves - The set of leaves to be appended.
112
- */
113
- appendLeaves ( treeId : MerkleTreeId , leaves : Buffer [ ] ) : Promise < void > ;
114
-
115
- /**
116
- * Returns information about the given tree.
117
- * @param treeId - The tree to be queried.
118
- */
119
- getTreeInfo ( treeId : MerkleTreeId ) : Promise < TreeInfo > ;
120
-
121
- /**
122
- * Gets the current roots of the commitment trees.
123
- */
124
- getTreeRoots ( ) : Promise < CurrentTreeRoots > ;
125
-
126
- /**
127
- * Gets sibling path for a leaf.
128
- * @param treeId - The tree to be queried for a sibling path.
129
- * @param index - The index of the leaf for which a sibling path should be returned.
130
- */
131
- getSiblingPath < N extends number > ( treeId : MerkleTreeId , index : bigint ) : Promise < SiblingPath < N > > ;
132
-
133
- /**
134
- * Returns the previous index for a given value in an indexed tree.
135
- * @param treeId - The tree for which the previous value index is required.
136
- * @param value - The value to be queried.
137
- */
138
- getPreviousValueIndex (
139
- treeId : IndexedTreeId ,
140
- value : bigint ,
141
- ) : Promise <
142
- | {
143
- /**
144
- * The index of the found leaf.
145
- */
146
- index : bigint ;
147
- /**
148
- * A flag indicating if the corresponding leaf's value is equal to `newValue`.
149
- */
150
- alreadyPresent : boolean ;
151
- }
152
- | undefined
153
- > ;
154
-
155
- /**
156
- * Returns the data at a specific leaf.
157
- * @param treeId - The tree for which leaf data should be returned.
158
- * @param index - The index of the leaf required.
159
- */
160
- getLeafPreimage ( treeId : IndexedTreeId , index : bigint ) : Promise < IndexedTreeLeafPreimage | undefined > ;
161
-
162
- /**
163
- * Update the leaf data at the given index.
164
- * @param treeId - The tree for which leaf data should be edited.
165
- * @param leaf - The updated leaf value.
166
- * @param index - The index of the leaf to be updated.
167
- */
168
- updateLeaf ( treeId : IndexedTreeId , leaf : NullifierLeafPreimage | Buffer , index : bigint ) : Promise < void > ;
169
-
170
- /**
171
- * Returns the index containing a leaf value.
172
- * @param treeId - The tree for which the index should be returned.
173
- * @param value - The value to search for in the tree.
174
- */
175
- findLeafIndex ( treeId : MerkleTreeId , value : Buffer ) : Promise < bigint | undefined > ;
176
-
177
- /**
178
- * Gets the value for a leaf in the tree.
179
- * @param treeId - The tree for which the index should be returned.
180
- * @param index - The index of the leaf.
181
- */
182
- getLeafValue ( treeId : MerkleTreeId , index : bigint ) : Promise < Buffer | undefined > ;
183
-
184
- /**
185
- * Inserts the new block hash into the archive.
186
- * This includes all of the current roots of all of the data trees and the current blocks global vars.
187
- * @param globalVariablesHash - The global variables hash to insert into the block hash.
188
- */
189
- updateArchive ( globalVariablesHash : Fr ) : Promise < void > ;
190
-
191
- /**
192
- * Updates the latest global variables hash
193
- * @param globalVariablesHash - The latest global variables hash
194
- */
195
- updateLatestGlobalVariablesHash ( globalVariablesHash : Fr ) : Promise < void > ;
196
-
197
- /**
198
- * Gets the global variables hash from the previous block
199
- */
200
- getLatestGlobalVariablesHash ( ) : Promise < Fr > ;
201
-
202
- /**
203
- * Batch insert multiple leaves into the tree.
204
- * @param leaves - Leaves to insert into the tree.
205
- * @param treeId - The tree on which to insert.
206
- * @param subtreeHeight - Height of the subtree.
207
- * @returns The witness data for the leaves to be updated when inserting the new ones.
208
- */
209
- batchInsert < TreeHeight extends number , SubtreeSiblingPathHeight extends number > (
210
- treeId : MerkleTreeId ,
211
- leaves : Buffer [ ] ,
212
- subtreeHeight : number ,
213
- ) : Promise < BatchInsertionResult < TreeHeight , SubtreeSiblingPathHeight > > ;
214
-
215
- /**
216
- * Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
217
- * @param block - The L2 block to handle.
218
- */
219
- handleL2Block ( block : L2Block ) : Promise < HandleL2BlockResult > ;
220
-
221
- /**
222
- * Commits pending changes to the underlying store.
223
- */
224
- commit ( ) : Promise < void > ;
225
-
226
- /**
227
- * Rolls back pending changes.
228
- */
229
- rollback ( ) : Promise < void > ;
230
- }
231
-
232
- /** Return type for handleL2Block */
233
- export type HandleL2BlockResult = {
234
- /** Whether the block processed was emitted by our sequencer */ isBlockOurs : boolean ;
235
- } ;
236
-
237
- /**
238
- * Outputs a tree leaves using for debugging purposes.
239
- */
240
- export async function inspectTree (
241
- db : MerkleTreeOperations ,
242
- treeId : MerkleTreeId ,
243
- log = createDebugLogger ( 'aztec:inspect-tree' ) ,
244
- ) {
245
- const info = await db . getTreeInfo ( treeId ) ;
246
- const output = [ `Tree id=${ treeId } size=${ info . size } root=0x${ info . root . toString ( 'hex' ) } ` ] ;
247
- for ( let i = 0 ; i < info . size ; i ++ ) {
248
- output . push (
249
- ` Leaf ${ i } : ${ await db . getLeafValue ( treeId , BigInt ( i ) ) . then ( x => x ?. toString ( 'hex' ) ?? '[undefined]' ) } ` ,
250
- ) ;
251
- }
252
- log ( output . join ( '\n' ) ) ;
253
- }
0 commit comments