@aztec/world-state 0.1.0-alpha11
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/.eslintrc.cjs +1 -0
- package/.tsbuildinfo +1 -0
- package/README.md +40 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +4 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts +117 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.d.ts.map +1 -0
- package/dest/merkle-tree/merkle_tree_operations_facade.js +133 -0
- package/dest/synchroniser/config.d.ts +19 -0
- package/dest/synchroniser/config.d.ts.map +1 -0
- package/dest/synchroniser/config.js +13 -0
- package/dest/synchroniser/index.d.ts +3 -0
- package/dest/synchroniser/index.d.ts.map +1 -0
- package/dest/synchroniser/index.js +3 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts +62 -0
- package/dest/synchroniser/server_world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.js +134 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts +2 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.d.ts.map +1 -0
- package/dest/synchroniser/server_world_state_synchroniser.test.js +215 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts +34 -0
- package/dest/synchroniser/world_state_synchroniser.d.ts.map +1 -0
- package/dest/synchroniser/world_state_synchroniser.js +11 -0
- package/dest/utils.d.ts +12 -0
- package/dest/utils.d.ts.map +1 -0
- package/dest/utils.js +14 -0
- package/dest/world-state-db/index.d.ts +165 -0
- package/dest/world-state-db/index.d.ts.map +1 -0
- package/dest/world-state-db/index.js +21 -0
- package/dest/world-state-db/merkle_trees.d.ts +200 -0
- package/dest/world-state-db/merkle_trees.d.ts.map +1 -0
- package/dest/world-state-db/merkle_trees.js +373 -0
- package/package.json +15 -0
- package/src/index.ts +3 -0
- package/src/merkle-tree/merkle_tree_operations_facade.ts +164 -0
- package/src/synchroniser/config.ts +27 -0
- package/src/synchroniser/index.ts +2 -0
- package/src/synchroniser/server_world_state_synchroniser.test.ts +281 -0
- package/src/synchroniser/server_world_state_synchroniser.ts +151 -0
- package/src/synchroniser/world_state_synchroniser.ts +36 -0
- package/src/utils.ts +24 -0
- package/src/world-state-db/index.ts +215 -0
- package/src/world-state-db/merkle_trees.ts +547 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { LeafData, SiblingPath, LowLeafWitnessData } from '@aztec/merkle-tree';
|
|
2
|
+
import { L2Block, MerkleTreeId } from '@aztec/types';
|
|
3
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { KERNEL_NEW_NULLIFIERS_LENGTH } from '@aztec/circuits.js';
|
|
5
|
+
|
|
6
|
+
export * from './merkle_trees.js';
|
|
7
|
+
export { LeafData } from '@aztec/merkle-tree';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Type alias for the nullifier tree ID.
|
|
11
|
+
*/
|
|
12
|
+
export type IndexedTreeId = MerkleTreeId.NULLIFIER_TREE;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type alias for the public data tree ID.
|
|
16
|
+
*/
|
|
17
|
+
export type PublicTreeId = MerkleTreeId.PUBLIC_DATA_TREE;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The nullifier tree must be pre filled with the number of leaves that are added by one rollup.
|
|
21
|
+
* The tree must be initially padded as the pre-populated 0 index prevents efficient subtree insertion.
|
|
22
|
+
* Padding with some values solves this issue.
|
|
23
|
+
*/
|
|
24
|
+
export const INITIAL_NULLIFIER_TREE_SIZE = 2 * KERNEL_NEW_NULLIFIERS_LENGTH;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Defines tree information.
|
|
28
|
+
*/
|
|
29
|
+
export interface TreeInfo {
|
|
30
|
+
/**
|
|
31
|
+
* The tree ID.
|
|
32
|
+
*/
|
|
33
|
+
treeId: MerkleTreeId;
|
|
34
|
+
/**
|
|
35
|
+
* The tree root.
|
|
36
|
+
*/
|
|
37
|
+
root: Buffer;
|
|
38
|
+
/**
|
|
39
|
+
* The number of leaves in the tree.
|
|
40
|
+
*/
|
|
41
|
+
size: bigint;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The depth of the tree.
|
|
45
|
+
*/
|
|
46
|
+
depth: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Adds a last boolean flag in each function on the type.
|
|
51
|
+
*/
|
|
52
|
+
type WithIncludeUncommitted<F> = F extends (...args: [...infer Rest]) => infer Return
|
|
53
|
+
? (...args: [...Rest, boolean]) => Return
|
|
54
|
+
: F;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The current roots of the commitment trees
|
|
58
|
+
*/
|
|
59
|
+
export type CurrentCommitmentTreeRoots = {
|
|
60
|
+
/** Private data tree root. */
|
|
61
|
+
privateDataTreeRoot: Buffer;
|
|
62
|
+
/** Contract data tree root. */
|
|
63
|
+
contractDataTreeRoot: Buffer;
|
|
64
|
+
/** L1 to L2 Messages data tree root. */
|
|
65
|
+
l1Tol2MessagesTreeRoot: Buffer;
|
|
66
|
+
/** Nullifier data tree root. */
|
|
67
|
+
nullifierTreeRoot: Buffer;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Defines the names of the setters on Merkle Trees.
|
|
72
|
+
*/
|
|
73
|
+
type MerkleTreeSetters = 'appendLeaves' | 'updateLeaf' | 'commit' | 'rollback' | 'handleL2Block' | 'batchInsert';
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Defines the interface for operations on a set of Merkle Trees configuring whether to return committed or uncommitted data.
|
|
77
|
+
*/
|
|
78
|
+
export type MerkleTreeDb = {
|
|
79
|
+
[Property in keyof MerkleTreeOperations as Exclude<Property, MerkleTreeSetters>]: WithIncludeUncommitted<
|
|
80
|
+
MerkleTreeOperations[Property]
|
|
81
|
+
>;
|
|
82
|
+
} & Pick<MerkleTreeOperations, MerkleTreeSetters>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Defines the interface for operations on a set of Merkle Trees.
|
|
86
|
+
*/
|
|
87
|
+
export interface MerkleTreeOperations {
|
|
88
|
+
/**
|
|
89
|
+
* Appends leaves to a given tree.
|
|
90
|
+
* @param treeId - The tree to be updated.
|
|
91
|
+
* @param leaves - The set of leaves to be appended.
|
|
92
|
+
*/
|
|
93
|
+
appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns information about the given tree.
|
|
97
|
+
* @param treeId - The tree to be queried.
|
|
98
|
+
*/
|
|
99
|
+
getTreeInfo(treeId: MerkleTreeId): Promise<TreeInfo>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Gets the current roots of the commitment trees.
|
|
103
|
+
*/
|
|
104
|
+
getCommitmentTreeRoots(): CurrentCommitmentTreeRoots;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Gets sibling path for a leaf.
|
|
108
|
+
* @param treeId - The tree to be queried for a sibling path.
|
|
109
|
+
* @param index - The index of the leaf for which a sibling path should be returned.
|
|
110
|
+
*/
|
|
111
|
+
getSiblingPath(treeId: MerkleTreeId, index: bigint): Promise<SiblingPath<number>>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns the previous index for a given value in an indexed tree.
|
|
115
|
+
* @param treeId - The tree for which the previous value index is required.
|
|
116
|
+
* @param value - The value to be queried.
|
|
117
|
+
*/
|
|
118
|
+
getPreviousValueIndex(
|
|
119
|
+
treeId: IndexedTreeId,
|
|
120
|
+
value: bigint,
|
|
121
|
+
): Promise<{
|
|
122
|
+
/**
|
|
123
|
+
* The index of the found leaf.
|
|
124
|
+
*/
|
|
125
|
+
index: number;
|
|
126
|
+
/**
|
|
127
|
+
* A flag indicating if the corresponding leaf's value is equal to `newValue`.
|
|
128
|
+
*/
|
|
129
|
+
alreadyPresent: boolean;
|
|
130
|
+
}>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns the data at a specific leaf.
|
|
134
|
+
* @param treeId - The tree for which leaf data should be returned.
|
|
135
|
+
* @param index - The index of the leaf required.
|
|
136
|
+
*/
|
|
137
|
+
getLeafData(treeId: IndexedTreeId, index: number): Promise<LeafData | undefined>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Update the leaf data at the given index.
|
|
141
|
+
* @param treeId - The tree for which leaf data should be edited.
|
|
142
|
+
* @param leaf - The updated leaf value.
|
|
143
|
+
* @param index - The index of the leaf to be updated.
|
|
144
|
+
*/
|
|
145
|
+
updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise<void>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Returns the index containing a leaf value.
|
|
149
|
+
* @param treeId - The tree for which the index should be returned.
|
|
150
|
+
* @param value - The value to search for in the tree.
|
|
151
|
+
*/
|
|
152
|
+
findLeafIndex(treeId: MerkleTreeId, value: Buffer): Promise<bigint | undefined>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Gets the value for a leaf in the tree.
|
|
156
|
+
* @param treeId - The tree for which the index should be returned.
|
|
157
|
+
* @param index - The index of the leaf.
|
|
158
|
+
*/
|
|
159
|
+
getLeafValue(treeId: MerkleTreeId, index: bigint): Promise<Buffer | undefined>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Inserts into the roots trees (CONTRACT_TREE_ROOTS_TREE, PRIVATE_DATA_TREE_ROOTS_TREE, L1_TO_L2_MESSAGES_TREE_ROOTS_TREE)
|
|
163
|
+
* the current roots of the corresponding trees (CONTRACT_TREE, PRIVATE_DATA_TREE, L1_TO_L2_MESSAGES_TREE).
|
|
164
|
+
*/
|
|
165
|
+
updateHistoricRootsTrees(): Promise<void>;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Batch insert multiple leaves into the tree.
|
|
169
|
+
* @param leaves - Leaves to insert into the tree.
|
|
170
|
+
* @param treeId - The tree on which to insert.
|
|
171
|
+
* @param treeHeight - Height of the tree.
|
|
172
|
+
* @param subtreeHeight - Height of the subtree.
|
|
173
|
+
* @returns The witness data for the leaves to be updated when inserting the new ones.
|
|
174
|
+
*/
|
|
175
|
+
batchInsert(
|
|
176
|
+
treeId: MerkleTreeId,
|
|
177
|
+
leaves: Buffer[],
|
|
178
|
+
treeHeight: number,
|
|
179
|
+
subtreeHeight: number,
|
|
180
|
+
): Promise<[LowLeafWitnessData<number>[], SiblingPath<number>] | [undefined, SiblingPath<number>]>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
|
|
184
|
+
* @param block - The L2 block to handle.
|
|
185
|
+
*/
|
|
186
|
+
handleL2Block(block: L2Block): Promise<void>;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Commits pending changes to the underlying store.
|
|
190
|
+
*/
|
|
191
|
+
commit(): Promise<void>;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Rolls back pending changes.
|
|
195
|
+
*/
|
|
196
|
+
rollback(): Promise<void>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Outputs a tree leaves using for debugging purposes.
|
|
201
|
+
*/
|
|
202
|
+
export async function inspectTree(
|
|
203
|
+
db: MerkleTreeOperations,
|
|
204
|
+
treeId: MerkleTreeId,
|
|
205
|
+
log = createDebugLogger('aztec:inspect-tree'),
|
|
206
|
+
) {
|
|
207
|
+
const info = await db.getTreeInfo(treeId);
|
|
208
|
+
const output = [`Tree id=${treeId} size=${info.size} root=0x${info.root.toString('hex')}`];
|
|
209
|
+
for (let i = 0; i < info.size; i++) {
|
|
210
|
+
output.push(
|
|
211
|
+
` Leaf ${i}: ${await db.getLeafValue(treeId, BigInt(i)).then(x => x?.toString('hex') ?? '[undefined]')}`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
log(output.join('\n'));
|
|
215
|
+
}
|