Skip to content

Commit 84f4671

Browse files
authored
fix: Unpick world state circulars. (AztecProtocol#3721)
Please provide a paragraph or two giving a summary of the change, including relevant motivation and context. # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist).
1 parent 309be4b commit 84f4671

8 files changed

+218
-217
lines changed

yarn-project/world-state/src/synchronizer/server_world_state_synchronizer.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { L2BlockHandledStats } from '@aztec/types/stats';
66

77
import { LevelUp } from 'levelup';
88

9-
import { HandleL2BlockResult, MerkleTreeOperations, MerkleTrees } from '../index.js';
10-
import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js';
11-
import { MerkleTreeSnapshotOperationsFacade } from '../merkle-tree/merkle_tree_snapshot_operations_facade.js';
9+
import { HandleL2BlockResult, MerkleTreeOperations, MerkleTrees } from '../world-state-db/index.js';
10+
import { MerkleTreeOperationsFacade } from '../world-state-db/merkle_tree_operations_facade.js';
11+
import { MerkleTreeSnapshotOperationsFacade } from '../world-state-db/merkle_tree_snapshot_operations_facade.js';
1212
import { WorldStateConfig } from './config.js';
1313
import { WorldStateRunningState, WorldStateStatus, WorldStateSynchronizer } from './world_state_synchronizer.js';
1414

yarn-project/world-state/src/synchronizer/world_state_synchronizer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MerkleTreeOperations } from '../index.js';
1+
import { MerkleTreeOperations } from '../world-state-db/index.js';
22

33
/**
44
* Defines the possible states of the world state synchronizer.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './merkle_trees.js';
22
export * from './merkle_tree_db.js';
3+
export * from './merkle_tree_operations.js';
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
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';
113

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';
165

176
/**
187
*
@@ -33,54 +22,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NEW_NULLIFIERS_PER_TX;
3322

3423
export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
3524

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-
5925
/**
6026
* Adds a last boolean flag in each function on the type.
6127
*/
6228
type WithIncludeUncommitted<F> = F extends (...args: [...infer Rest]) => infer Return
6329
? (...args: [...Rest, boolean]) => Return
6430
: F;
6531

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-
8432
/**
8533
* Defines the names of the setters on Merkle Trees.
8634
*/
@@ -100,154 +48,3 @@ export type MerkleTreeDb = {
10048
*/
10149
getSnapshot(block: number): Promise<ReadonlyArray<TreeSnapshot | IndexedTreeSnapshot>>;
10250
};
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

Comments
 (0)