Skip to content

Commit

Permalink
fix: Unpick world state circulars. (AztecProtocol#3721)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
charlielye authored Dec 17, 2023
1 parent 309be4b commit 84f4671
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { L2BlockHandledStats } from '@aztec/types/stats';

import { LevelUp } from 'levelup';

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

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

/**
* Defines the possible states of the world state synchronizer.
Expand Down
1 change: 1 addition & 0 deletions yarn-project/world-state/src/world-state-db/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './merkle_trees.js';
export * from './merkle_tree_db.js';
export * from './merkle_tree_operations.js';
209 changes: 3 additions & 206 deletions yarn-project/world-state/src/world-state-db/merkle_tree_db.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import {
MAX_NEW_NULLIFIERS_PER_TX,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX,
NullifierLeafPreimage,
} from '@aztec/circuits.js';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees';
import { BatchInsertionResult, IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
import { L2Block, MerkleTreeId, SiblingPath } from '@aztec/types';
import { MAX_NEW_NULLIFIERS_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/circuits.js';
import { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';

/**
* Type alias for the nullifier tree ID.
*/
export type IndexedTreeId = MerkleTreeId.NULLIFIER_TREE | MerkleTreeId.PUBLIC_DATA_TREE;
import { MerkleTreeOperations } from './merkle_tree_operations.js';

/**
*
Expand All @@ -33,54 +22,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NEW_NULLIFIERS_PER_TX;

export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;

/**
* Defines tree information.
*/
export interface TreeInfo {
/**
* The tree ID.
*/
treeId: MerkleTreeId;
/**
* The tree root.
*/
root: Buffer;
/**
* The number of leaves in the tree.
*/
size: bigint;

/**
* The depth of the tree.
*/
depth: number;
}

/**
* Adds a last boolean flag in each function on the type.
*/
type WithIncludeUncommitted<F> = F extends (...args: [...infer Rest]) => infer Return
? (...args: [...Rest, boolean]) => Return
: F;

/**
* The current roots of the commitment trees
*/
export type CurrentTreeRoots = {
/** Note Hash Tree root. */
noteHashTreeRoot: Buffer;
/** Contract data tree root. */
contractDataTreeRoot: Buffer;
/** L1 to L2 Messages data tree root. */
l1Tol2MessagesTreeRoot: Buffer;
/** Nullifier data tree root. */
nullifierTreeRoot: Buffer;
/** Archive root. */
archiveRoot: Buffer;
/** Public data tree root */
publicDataTreeRoot: Buffer;
};

/**
* Defines the names of the setters on Merkle Trees.
*/
Expand All @@ -100,154 +48,3 @@ export type MerkleTreeDb = {
*/
getSnapshot(block: number): Promise<ReadonlyArray<TreeSnapshot | IndexedTreeSnapshot>>;
};

/**
* Defines the interface for operations on a set of Merkle Trees.
*/
export interface MerkleTreeOperations {
/**
* Appends leaves to a given tree.
* @param treeId - The tree to be updated.
* @param leaves - The set of leaves to be appended.
*/
appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise<void>;

/**
* Returns information about the given tree.
* @param treeId - The tree to be queried.
*/
getTreeInfo(treeId: MerkleTreeId): Promise<TreeInfo>;

/**
* Gets the current roots of the commitment trees.
*/
getTreeRoots(): Promise<CurrentTreeRoots>;

/**
* Gets sibling path for a leaf.
* @param treeId - The tree to be queried for a sibling path.
* @param index - The index of the leaf for which a sibling path should be returned.
*/
getSiblingPath<N extends number>(treeId: MerkleTreeId, index: bigint): Promise<SiblingPath<N>>;

/**
* Returns the previous index for a given value in an indexed tree.
* @param treeId - The tree for which the previous value index is required.
* @param value - The value to be queried.
*/
getPreviousValueIndex(
treeId: IndexedTreeId,
value: bigint,
): Promise<
| {
/**
* The index of the found leaf.
*/
index: bigint;
/**
* A flag indicating if the corresponding leaf's value is equal to `newValue`.
*/
alreadyPresent: boolean;
}
| undefined
>;

/**
* Returns the data at a specific leaf.
* @param treeId - The tree for which leaf data should be returned.
* @param index - The index of the leaf required.
*/
getLeafPreimage(treeId: IndexedTreeId, index: bigint): Promise<IndexedTreeLeafPreimage | undefined>;

/**
* Update the leaf data at the given index.
* @param treeId - The tree for which leaf data should be edited.
* @param leaf - The updated leaf value.
* @param index - The index of the leaf to be updated.
*/
updateLeaf(treeId: IndexedTreeId, leaf: NullifierLeafPreimage | Buffer, index: bigint): Promise<void>;

/**
* Returns the index containing a leaf value.
* @param treeId - The tree for which the index should be returned.
* @param value - The value to search for in the tree.
*/
findLeafIndex(treeId: MerkleTreeId, value: Buffer): Promise<bigint | undefined>;

/**
* Gets the value for a leaf in the tree.
* @param treeId - The tree for which the index should be returned.
* @param index - The index of the leaf.
*/
getLeafValue(treeId: MerkleTreeId, index: bigint): Promise<Buffer | undefined>;

/**
* Inserts the new block hash into the archive.
* This includes all of the current roots of all of the data trees and the current blocks global vars.
* @param globalVariablesHash - The global variables hash to insert into the block hash.
*/
updateArchive(globalVariablesHash: Fr): Promise<void>;

/**
* Updates the latest global variables hash
* @param globalVariablesHash - The latest global variables hash
*/
updateLatestGlobalVariablesHash(globalVariablesHash: Fr): Promise<void>;

/**
* Gets the global variables hash from the previous block
*/
getLatestGlobalVariablesHash(): Promise<Fr>;

/**
* Batch insert multiple leaves into the tree.
* @param leaves - Leaves to insert into the tree.
* @param treeId - The tree on which to insert.
* @param subtreeHeight - Height of the subtree.
* @returns The witness data for the leaves to be updated when inserting the new ones.
*/
batchInsert<TreeHeight extends number, SubtreeSiblingPathHeight extends number>(
treeId: MerkleTreeId,
leaves: Buffer[],
subtreeHeight: number,
): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>>;

/**
* Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree).
* @param block - The L2 block to handle.
*/
handleL2Block(block: L2Block): Promise<HandleL2BlockResult>;

/**
* Commits pending changes to the underlying store.
*/
commit(): Promise<void>;

/**
* Rolls back pending changes.
*/
rollback(): Promise<void>;
}

/** Return type for handleL2Block */
export type HandleL2BlockResult = {
/** Whether the block processed was emitted by our sequencer */ isBlockOurs: boolean;
};

/**
* Outputs a tree leaves using for debugging purposes.
*/
export async function inspectTree(
db: MerkleTreeOperations,
treeId: MerkleTreeId,
log = createDebugLogger('aztec:inspect-tree'),
) {
const info = await db.getTreeInfo(treeId);
const output = [`Tree id=${treeId} size=${info.size} root=0x${info.root.toString('hex')}`];
for (let i = 0; i < info.size; i++) {
output.push(
` Leaf ${i}: ${await db.getLeafValue(treeId, BigInt(i)).then(x => x?.toString('hex') ?? '[undefined]')}`,
);
}
log(output.join('\n'));
}
Loading

0 comments on commit 84f4671

Please sign in to comment.