From 9d05218a0e2f40d452a80b8411a5399c0a9d4bcf Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 20 Apr 2023 11:32:48 +0000 Subject: [PATCH] feat: exposing public tree and integrating it in sequencer --- yarn-project/merkle-tree/src/index.ts | 1 + .../src/sequencer/public_processor.ts | 3 +-- .../world-state/src/world-state-db/index.ts | 3 ++- .../src/world-state-db/merkle_trees.ts | 25 ++++++++++++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/yarn-project/merkle-tree/src/index.ts b/yarn-project/merkle-tree/src/index.ts index 3c87ce1eb76b..931cb11edfc6 100644 --- a/yarn-project/merkle-tree/src/index.ts +++ b/yarn-project/merkle-tree/src/index.ts @@ -5,6 +5,7 @@ export * from './interfaces/merkle_tree.js'; export * from './interfaces/update_only_tree.js'; export * from './pedersen.js'; export * from './sibling_path/sibling_path.js'; +export * from './sparse_tree/sparse_tree.js'; export * from './standard_indexed_tree/standard_indexed_tree.js'; export * from './standard_tree/standard_tree.js'; export { INITIAL_LEAF } from './tree_base.js'; diff --git a/yarn-project/sequencer-client/src/sequencer/public_processor.ts b/yarn-project/sequencer-client/src/sequencer/public_processor.ts index aa0c9f57f94b..0e8ed454ae5f 100644 --- a/yarn-project/sequencer-client/src/sequencer/public_processor.ts +++ b/yarn-project/sequencer-client/src/sequencer/public_processor.ts @@ -119,8 +119,7 @@ export class PublicProcessor { for (const stateTransition of stateTransitions) { const index = getLeafIndex(stateTransition.storageSlot); transitionsHashPaths.push(await this.getMembershipWitness(index)); - // TODO: Update tree once we got the interface for it - // this.db.updateLeaf(MerkleTreeId.PUBLIC_DATA_TREE, stateTransition.newValue, index); + await this.db.updateLeaf(MerkleTreeId.PUBLIC_DATA_TREE, stateTransition.newValue.toBuffer(), index); } return { readsHashPaths, transitionsHashPaths }; diff --git a/yarn-project/world-state/src/world-state-db/index.ts b/yarn-project/world-state/src/world-state-db/index.ts index 51d7dc867aea..6dddfdd80247 100644 --- a/yarn-project/world-state/src/world-state-db/index.ts +++ b/yarn-project/world-state/src/world-state-db/index.ts @@ -16,6 +16,7 @@ export enum MerkleTreeId { } export type IndexedTreeId = MerkleTreeId.NULLIFIER_TREE; +export type PublicTreeId = MerkleTreeId.PUBLIC_DATA_TREE; /** * The nullifier tree must be pre filled with the number of leaves that are added by one rollup. @@ -104,7 +105,7 @@ export interface MerkleTreeOperations { * @param leaf - The updated leaf value * @param index - The index of the leaf to be updated */ - updateLeaf(treeId: IndexedTreeId, leaf: LeafData, index: bigint): Promise; + updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise; /** * Returns the index containing a leaf value * @param treeId - The tree for which the index should be returned diff --git a/yarn-project/world-state/src/world-state-db/merkle_trees.ts b/yarn-project/world-state/src/world-state-db/merkle_trees.ts index 338c5141832e..962239519947 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_trees.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_trees.ts @@ -5,6 +5,7 @@ import { NULLIFIER_TREE_HEIGHT, PRIVATE_DATA_TREE_HEIGHT, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT, + PUBLIC_DATA_TREE_HEIGHT, } from '@aztec/circuits.js'; import { SerialQueue } from '@aztec/foundation'; import { WasmWrapper } from '@aztec/foundation/wasm'; @@ -18,6 +19,7 @@ import { UpdateOnlyTree, IndexedTree, newTree, + SparseTree, } from '@aztec/merkle-tree'; import { default as levelup } from 'levelup'; import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js'; @@ -27,6 +29,7 @@ import { MerkleTreeDb, MerkleTreeId, MerkleTreeOperations, + PublicTreeId, TreeInfo, } from './index.js'; @@ -67,21 +70,35 @@ export class MerkleTrees implements MerkleTreeDb { NULLIFIER_TREE_HEIGHT, INITIAL_NULLIFIER_TREE_SIZE, ); - const dataTree: AppendOnlyTree = await newTree( + const privateDataTree: AppendOnlyTree = await newTree( StandardTree, this.db, hasher, `${MerkleTreeId[MerkleTreeId.DATA_TREE]}`, PRIVATE_DATA_TREE_HEIGHT, ); - const dataTreeRootsTree: AppendOnlyTree = await newTree( + const privateDataTreeRootsTree: AppendOnlyTree = await newTree( StandardTree, this.db, hasher, `${MerkleTreeId[MerkleTreeId.DATA_TREE_ROOTS_TREE]}`, PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT, ); - this.trees = [contractTree, contractTreeRootsTree, nullifierTree, dataTree, dataTreeRootsTree]; + const publicDataTree: UpdateOnlyTree = await newTree( + SparseTree, + this.db, + hasher, + `${MerkleTreeId[MerkleTreeId.PUBLIC_DATA_TREE]}`, + PUBLIC_DATA_TREE_HEIGHT, + ); + this.trees = [ + contractTree, + contractTreeRootsTree, + nullifierTree, + privateDataTree, + privateDataTreeRootsTree, + publicDataTree, + ]; this.jobQueue.start(); } @@ -221,7 +238,7 @@ export class MerkleTrees implements MerkleTreeDb { * @param leaf - The new leaf value * @param index - The index to insert into */ - public async updateLeaf(treeId: IndexedTreeId, leaf: LeafData, index: bigint): Promise { + public async updateLeaf(treeId: IndexedTreeId | PublicTreeId, leaf: LeafData | Buffer, index: bigint): Promise { const tree = this.trees[treeId]; if (!('updateLeaf' in tree)) { throw new Error('Tree does not support `updateLeaf` method');