From 1bcdaab903ad75b5318ad981573c05071bdbf863 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 2 May 2023 14:30:15 -0300 Subject: [PATCH 1/7] Set historic tree roots in private circuit --- .../aztec3/circuits/kernel/private/c_bind.cpp | 3 ++- .../aztec-node/src/aztec-node/aztec-node.ts | 25 ++++++++++++++----- .../src/account_state/account_state.ts | 10 +++++++- .../structs/kernel/combined_constant_data.ts | 23 +++++++++++------ .../src/world-state-db/merkle_trees.ts | 9 ++++--- 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/circuits/cpp/src/aztec3/circuits/kernel/private/c_bind.cpp b/circuits/cpp/src/aztec3/circuits/kernel/private/c_bind.cpp index 2d06d0998634..987b52b180af 100644 --- a/circuits/cpp/src/aztec3/circuits/kernel/private/c_bind.cpp +++ b/circuits/cpp/src/aztec3/circuits/kernel/private/c_bind.cpp @@ -103,7 +103,8 @@ WASM_EXPORT size_t private_kernel__sim(uint8_t const* signed_tx_request_buf, previous_kernel.public_inputs.end.private_call_stack[0] = private_call_data.call_stack_item.hash(); previous_kernel.public_inputs.constants.historic_tree_roots.private_historic_tree_roots.private_data_tree_root = private_call_data.call_stack_item.public_inputs.historic_private_data_tree_root; - // previous_kernel.public_inputs.constants.historic_tree_roots.nullifier_tree_root = + previous_kernel.public_inputs.constants.historic_tree_roots.private_historic_tree_roots.nullifier_tree_root = + private_call_data.call_stack_item.public_inputs.historic_nullifier_tree_root; previous_kernel.public_inputs.constants.historic_tree_roots.private_historic_tree_roots.contract_tree_root = private_call_data.call_stack_item.public_inputs.historic_contract_tree_root; // previous_kernel.public_inputs.constants.historic_tree_roots.private_kernel_vk_tree_root = diff --git a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts index c6ae1a45f4ed..f6eae15fb0e3 100644 --- a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts +++ b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts @@ -3,7 +3,7 @@ import { PrimitivesWasm } from '@aztec/barretenberg.js/wasm'; import { CircuitsWasm } from '@aztec/circuits.js'; import { SiblingPath } from '@aztec/merkle-tree'; import { P2P, P2PClient } from '@aztec/p2p'; -import { SequencerClient, getCombinedHistoricTreeRoots } from '@aztec/sequencer-client'; +import { SequencerClient } from '@aztec/sequencer-client'; import { ContractData, ContractDataSource, @@ -130,11 +130,6 @@ export class AztecNode { * @param tx - The transaction to be submitted. */ public async sendTx(tx: Tx) { - // TODO: Patch tx to inject historic tree roots until the private kernel circuit supplies this value - if (tx.isPrivate() && tx.data.constants.historicTreeRoots.privateHistoricTreeRoots.isEmpty()) { - tx.data.constants.historicTreeRoots = await getCombinedHistoricTreeRoots(this.merkleTreeDB.asLatest()); - } - await this.p2pClient!.sendTx(tx); } @@ -189,4 +184,22 @@ export class AztecNode { const leafIndex = computePublicDataTreeLeafIndex(contract, new Fr(slot), await PrimitivesWasm.get()); return this.merkleTreeDB.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex, false); } + + /** + * Returns the current committed roots for the data trees. + * @returns the current committed roots for the data trees. + */ + public async getTreeRoots(): Promise> { + const getTreeRoot = async (id: MerkleTreeId) => + Fr.fromBuffer((await this.merkleTreeDB.getTreeInfo(id, false)).root); + console.log(`NULL TREE`, await getTreeRoot(MerkleTreeId.NULLIFIER_TREE)); + return { + [MerkleTreeId.CONTRACT_TREE]: await getTreeRoot(MerkleTreeId.CONTRACT_TREE), + [MerkleTreeId.PRIVATE_DATA_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE), + [MerkleTreeId.NULLIFIER_TREE]: await getTreeRoot(MerkleTreeId.NULLIFIER_TREE), + [MerkleTreeId.PUBLIC_DATA_TREE]: await getTreeRoot(MerkleTreeId.PUBLIC_DATA_TREE), + [MerkleTreeId.CONTRACT_TREE_ROOTS_TREE]: await getTreeRoot(MerkleTreeId.CONTRACT_TREE_ROOTS_TREE), + [MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE), + }; + } } diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index c6927f3c2113..31e1af44e2c8 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -66,7 +66,15 @@ export class AccountState { txRequest.functionData.functionSelector, ); const portalContract = await contractDataOracle.getPortalContractAddress(contractAddress); - const historicRoots = new PrivateHistoricTreeRoots(Fr.ZERO, Fr.ZERO, Fr.ZERO, Fr.ZERO); // TODO - get old roots from the database/node + + const currentRoots = await this.node.getTreeRoots(); + const historicRoots = PrivateHistoricTreeRoots.from({ + contractTreeRoot: currentRoots[MerkleTreeId.CONTRACT_TREE], + nullifierTreeRoot: currentRoots[MerkleTreeId.NULLIFIER_TREE], + privateDataTreeRoot: currentRoots[MerkleTreeId.PRIVATE_DATA_TREE], + privateKernelVkTreeRoot: Fr.ZERO, + }); + return { contractAddress, diff --git a/yarn-project/circuits.js/src/structs/kernel/combined_constant_data.ts b/yarn-project/circuits.js/src/structs/kernel/combined_constant_data.ts index 5542c941c308..dbd5373498f8 100644 --- a/yarn-project/circuits.js/src/structs/kernel/combined_constant_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/combined_constant_data.ts @@ -1,7 +1,8 @@ import { Fr } from '@aztec/foundation/fields'; +import { BufferReader } from '@aztec/foundation/serialize'; +import { FieldsOf } from '../../utils/jsUtils.js'; import { serializeToBuffer } from '../../utils/serialize.js'; import { TxContext } from '../tx_context.js'; -import { BufferReader } from '@aztec/foundation/serialize'; export class PrivateHistoricTreeRoots { constructor( @@ -11,13 +12,21 @@ export class PrivateHistoricTreeRoots { public privateKernelVkTreeRoot: Fr, // future enhancement ) {} + static from(fields: FieldsOf): PrivateHistoricTreeRoots { + return new PrivateHistoricTreeRoots(...PrivateHistoricTreeRoots.getFields(fields)); + } + + static getFields(fields: FieldsOf) { + return [ + fields.privateDataTreeRoot, + fields.nullifierTreeRoot, + fields.contractTreeRoot, + fields.privateKernelVkTreeRoot, + ] as const; + } + toBuffer() { - return serializeToBuffer( - this.privateDataTreeRoot, - this.nullifierTreeRoot, - this.contractTreeRoot, - this.privateKernelVkTreeRoot, - ); + return serializeToBuffer(...PrivateHistoricTreeRoots.getFields(this)); } isEmpty() { 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 5fd25bcc2022..8b5c3888daa7 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 @@ -13,15 +13,15 @@ import { import { WasmWrapper } from '@aztec/foundation/wasm'; import { AppendOnlyTree, - StandardIndexedTree, + IndexedTree, LeafData, Pedersen, SiblingPath, + SparseTree, + StandardIndexedTree, StandardTree, UpdateOnlyTree, - IndexedTree, newTree, - SparseTree, } from '@aztec/merkle-tree'; import { default as levelup } from 'levelup'; import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js'; @@ -122,7 +122,10 @@ export class MerkleTrees implements MerkleTreeDb { this.jobQueue.start(); + // The roots trees must contain the empty roots of their data trees await this.updateHistoricRootsTrees(true); + await contractTreeRootsTree.commit(); + await privateDataTreeRootsTree.commit(); } /** From 886f10e44c55492c7c51675787fed67ac2d26923 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 2 May 2023 15:07:52 -0300 Subject: [PATCH 2/7] Fix build issue --- .../aztec-rpc/src/account_state/account_state.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index 31e1af44e2c8..7beb72b6f3be 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -1,20 +1,22 @@ import { AcirSimulator } from '@aztec/acir-simulator'; import { AztecNode } from '@aztec/aztec-node'; import { Grumpkin } from '@aztec/barretenberg.js/crypto'; +import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; import { EcdsaSignature, KERNEL_NEW_COMMITMENTS_LENGTH, PrivateHistoricTreeRoots, TxRequest } from '@aztec/circuits.js'; +import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { Fr, Point } from '@aztec/foundation/fields'; +import { createDebugLogger } from '@aztec/foundation/log'; import { KernelProver, OutputNoteData } from '@aztec/kernel-prover'; -import { EncodedContractFunction, INITIAL_L2_BLOCK_NUM, L2BlockContext, Tx, UnverifiedData } from '@aztec/types'; +import { INITIAL_L2_BLOCK_NUM } from '@aztec/types'; +import { FunctionType } from '@aztec/noir-contracts'; +import { EncodedContractFunction, L2BlockContext, Tx, UnverifiedData } from '@aztec/types'; +import { MerkleTreeId } from '@aztec/world-state'; import { NotePreimage, TxAuxData } from '../aztec_rpc_server/tx_aux_data/index.js'; import { ContractDataOracle } from '../contract_data_oracle/index.js'; import { Database, TxAuxDataDao, TxDao } from '../database/index.js'; +import { generateFunctionSelector } from '../index.js'; import { ConstantKeyPair, KeyPair } from '../key_store/index.js'; import { SimulatorOracle } from '../simulator_oracle/index.js'; -import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; -import { FunctionType } from '@aztec/noir-contracts'; -import { generateFunctionSelector } from '../index.js'; -import { Fr, Point } from '@aztec/foundation/fields'; -import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { createDebugLogger } from '@aztec/foundation/log'; export class AccountState { public syncedToBlock = 0; From 26946d83fee328b33163de6401999970183f64f8 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Tue, 2 May 2023 15:17:28 -0300 Subject: [PATCH 3/7] Format --- yarn-project/aztec-rpc/src/account_state/account_state.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index 7beb72b6f3be..da0b780ffc2f 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -77,7 +77,6 @@ export class AccountState { privateKernelVkTreeRoot: Fr.ZERO, }); - return { contractAddress, functionAbi, From fe094f85954e6848ab65130fbea6591aa20de185 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 3 May 2023 12:58:17 -0300 Subject: [PATCH 4/7] Remove console.log --- yarn-project/aztec-node/src/aztec-node/aztec-node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts index f6eae15fb0e3..0439c47d6157 100644 --- a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts +++ b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts @@ -192,7 +192,7 @@ export class AztecNode { public async getTreeRoots(): Promise> { const getTreeRoot = async (id: MerkleTreeId) => Fr.fromBuffer((await this.merkleTreeDB.getTreeInfo(id, false)).root); - console.log(`NULL TREE`, await getTreeRoot(MerkleTreeId.NULLIFIER_TREE)); + return { [MerkleTreeId.CONTRACT_TREE]: await getTreeRoot(MerkleTreeId.CONTRACT_TREE), [MerkleTreeId.PRIVATE_DATA_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE), From 9811991315ed4eebd036f0550e1ecd61fde305e8 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 3 May 2023 13:00:20 -0300 Subject: [PATCH 5/7] Add L1-to-L2 messages trees --- yarn-project/aztec-node/src/aztec-node/aztec-node.ts | 2 ++ yarn-project/world-state/src/world-state-db/merkle_trees.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts index 0439c47d6157..ec6319b9e2b4 100644 --- a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts +++ b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts @@ -198,6 +198,8 @@ export class AztecNode { [MerkleTreeId.PRIVATE_DATA_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE), [MerkleTreeId.NULLIFIER_TREE]: await getTreeRoot(MerkleTreeId.NULLIFIER_TREE), [MerkleTreeId.PUBLIC_DATA_TREE]: await getTreeRoot(MerkleTreeId.PUBLIC_DATA_TREE), + [MerkleTreeId.L1_TO_L2_MESSAGES_TREE]: await getTreeRoot(MerkleTreeId.L1_TO_L2_MESSAGES_TREE), + [MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE]: await getTreeRoot(MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE), [MerkleTreeId.CONTRACT_TREE_ROOTS_TREE]: await getTreeRoot(MerkleTreeId.CONTRACT_TREE_ROOTS_TREE), [MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE), }; 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 8b5c3888daa7..854f0701ba85 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 @@ -124,8 +124,8 @@ export class MerkleTrees implements MerkleTreeDb { // The roots trees must contain the empty roots of their data trees await this.updateHistoricRootsTrees(true); - await contractTreeRootsTree.commit(); - await privateDataTreeRootsTree.commit(); + const historicRootsTrees = [contractTreeRootsTree, privateDataTreeRootsTree, l1Tol2MessagesRootsTree]; + await Promise.all(historicRootsTrees.map(tree => tree.commit())); } /** From 3caa368c1dafc648dba4ca40f8520fb69032bc6d Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Thu, 4 May 2023 11:12:34 -0300 Subject: [PATCH 6/7] Fetch tree roots from synchroniser --- .../src/account_state/account_state.ts | 2 +- .../src/aztec_rpc_server/aztec_rpc_server.ts | 2 +- .../aztec-rpc/src/database/database.ts | 4 + .../aztec-rpc/src/database/memory_db.ts | 13 ++ .../src/synchroniser/synchroniser.test.ts | 87 +++++++++++++- .../src/synchroniser/synchroniser.ts | 113 ++++++++++++------ 6 files changed, 175 insertions(+), 46 deletions(-) diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index da0b780ffc2f..bd72c254ccc9 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -69,7 +69,7 @@ export class AccountState { ); const portalContract = await contractDataOracle.getPortalContractAddress(contractAddress); - const currentRoots = await this.node.getTreeRoots(); + const currentRoots = await this.db.getTreeRoots(); const historicRoots = PrivateHistoricTreeRoots.from({ contractTreeRoot: currentRoots[MerkleTreeId.CONTRACT_TREE], nullifierTreeRoot: currentRoots[MerkleTreeId.NULLIFIER_TREE], diff --git a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts index f3f36de014f7..002983b84b23 100644 --- a/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts +++ b/yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts @@ -43,7 +43,7 @@ export class AztecRPCServer implements AztecRPCClient { for (const account of accounts) { await this.initAccountState(account); } - this.synchroniser.start(); + await this.synchroniser.start(); this.log(`Started. ${accounts.length} initial accounts.`); } diff --git a/yarn-project/aztec-rpc/src/database/database.ts b/yarn-project/aztec-rpc/src/database/database.ts index 7a2ca9c19afb..2bea12e6d360 100644 --- a/yarn-project/aztec-rpc/src/database/database.ts +++ b/yarn-project/aztec-rpc/src/database/database.ts @@ -4,6 +4,7 @@ import { TxAuxDataDao } from './tx_aux_data_dao.js'; import { TxDao } from './tx_dao.js'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr, Point } from '@aztec/foundation/fields'; +import { MerkleTreeId } from '@aztec/world-state'; export interface Database extends ContractDatabase { getTx(txHash: TxHash): Promise; @@ -15,4 +16,7 @@ export interface Database extends ContractDatabase { addTxAuxData(txAuxDataDao: TxAuxDataDao): Promise; addTxAuxDataBatch(txAuxDataDaos: TxAuxDataDao[]): Promise; removeNullifiedTxAuxData(nullifiers: Fr[], account: Point): Promise; + + getTreeRoots(): Promise>; + setTreeRoots(roots: Record): Promise; } diff --git a/yarn-project/aztec-rpc/src/database/memory_db.ts b/yarn-project/aztec-rpc/src/database/memory_db.ts index 90c3a3cf1425..be2508bdc56d 100644 --- a/yarn-project/aztec-rpc/src/database/memory_db.ts +++ b/yarn-project/aztec-rpc/src/database/memory_db.ts @@ -5,10 +5,12 @@ import { TxAuxDataDao } from './tx_aux_data_dao.js'; import { TxDao } from './tx_dao.js'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr, Point } from '@aztec/foundation/fields'; +import { MerkleTreeId } from '@aztec/world-state'; export class MemoryDB extends MemoryContractDatabase implements Database { private txTable: TxDao[] = []; private txAuxDataTable: TxAuxDataDao[] = []; + private treeRoots: Record | undefined; public getTx(txHash: TxHash) { return Promise.resolve(this.txTable.find(tx => tx.txHash.equals(txHash))); @@ -69,4 +71,15 @@ export class MemoryDB extends MemoryContractDatabase implements Database { return Promise.resolve(removed); } + + public getTreeRoots(): Promise> { + const roots = this.treeRoots; + if (!roots) throw new Error(`Tree roots not set in memory database`); + return Promise.resolve(roots); + } + + public setTreeRoots(roots: Record) { + this.treeRoots = roots; + return Promise.resolve(); + } } diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts index 7fb5362a8342..966b5bab99b6 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts @@ -1,28 +1,45 @@ import { AztecNode } from '@aztec/aztec-node'; import { Grumpkin } from '@aztec/barretenberg.js/crypto'; -import { mock } from 'jest-mock-extended'; +import { MockProxy, mock } from 'jest-mock-extended'; import { Database, MemoryDB } from '../database/index.js'; import { ConstantKeyPair } from '../key_store/index.js'; import { Synchroniser } from './synchroniser.js'; +import { L2Block, UnverifiedData } from '@aztec/types'; +import { MerkleTreeId } from '@aztec/world-state'; +import { Fr } from '@aztec/circuits.js'; describe('Synchroniser', () => { let grumpkin: Grumpkin; - let aztecNode: ReturnType>; + let aztecNode: MockProxy; let database: Database; - let synchroniser: Synchroniser; + let synchroniser: TestSynchroniser; + let roots: Record; beforeAll(async () => { grumpkin = await Grumpkin.new(); + }); + + beforeEach(() => { + roots = { + [MerkleTreeId.CONTRACT_TREE]: Fr.random(), + [MerkleTreeId.PRIVATE_DATA_TREE]: Fr.random(), + [MerkleTreeId.NULLIFIER_TREE]: Fr.random(), + [MerkleTreeId.PUBLIC_DATA_TREE]: Fr.random(), + [MerkleTreeId.L1_TO_L2_MESSAGES_TREE]: Fr.random(), + [MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE]: Fr.random(), + [MerkleTreeId.CONTRACT_TREE_ROOTS_TREE]: Fr.random(), + [MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE]: Fr.random(), + }; aztecNode = mock(); aztecNode.getUnverifiedData.mockResolvedValue([]); database = new MemoryDB(); - synchroniser = new Synchroniser(aztecNode, database); + synchroniser = new TestSynchroniser(aztecNode, database); }); - it('Should create account state', async () => { + it('should create account state', async () => { const account = ConstantKeyPair.random(grumpkin); const address = account.getPublicKey().toAddress(); @@ -32,4 +49,64 @@ describe('Synchroniser', () => { expect(synchroniser.getAccount(address)!.getPublicKey()).toEqual(account.getPublicKey()); }); + + it('sets tree roots from aztec node on initial sync', async () => { + aztecNode.getBlockHeight.mockResolvedValue(3); + aztecNode.getTreeRoots.mockResolvedValue(roots); + + await synchroniser.initialSync(); + + expect(await database.getTreeRoots()).toEqual(roots); + }); + + it('sets tree roots from latest block', async () => { + const block = L2Block.random(1, 4); + + aztecNode.getBlocks.mockResolvedValue([block]); + aztecNode.getUnverifiedData.mockResolvedValue([UnverifiedData.random(4)]); + + await synchroniser.work(); + + const roots = await database.getTreeRoots(); + expect(roots[MerkleTreeId.CONTRACT_TREE]).toEqual(block.endContractTreeSnapshot.root); + }); + + it('overrides tree roots from initial sync once block height is larger', async () => { + // Initial sync is done on block with height 3 + aztecNode.getBlockHeight.mockResolvedValue(3); + aztecNode.getTreeRoots.mockResolvedValue(roots); + + await synchroniser.initialSync(); + const roots0 = await database.getTreeRoots(); + expect(roots0[MerkleTreeId.CONTRACT_TREE]).toEqual(roots[MerkleTreeId.CONTRACT_TREE]); + + // We then process block with height 1, this should not change tree roots + const block1 = L2Block.random(1, 4); + aztecNode.getBlocks.mockResolvedValueOnce([block1]); + aztecNode.getUnverifiedData.mockResolvedValue([UnverifiedData.random(4)]); + + await synchroniser.work(); + const roots1 = await database.getTreeRoots(); + expect(roots1[MerkleTreeId.CONTRACT_TREE]).toEqual(roots[MerkleTreeId.CONTRACT_TREE]); + expect(roots1[MerkleTreeId.CONTRACT_TREE]).not.toEqual(block1.endContractTreeSnapshot.root); + + // But they should change when we process block with height 5 + const block5 = L2Block.random(5, 4); + aztecNode.getBlocks.mockResolvedValueOnce([block5]); + + await synchroniser.work(); + const roots5 = await database.getTreeRoots(); + expect(roots5[MerkleTreeId.CONTRACT_TREE]).not.toEqual(roots[MerkleTreeId.CONTRACT_TREE]); + expect(roots5[MerkleTreeId.CONTRACT_TREE]).toEqual(block5.endContractTreeSnapshot.root); + }); }); + +class TestSynchroniser extends Synchroniser { + public work() { + return super.work(); + } + + public initialSync(): Promise { + return super.initialSync(); + } +} diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index c302e1f479cb..840356ffe06c 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -6,12 +6,15 @@ import { Database, TxDao } from '../database/index.js'; import { InterruptableSleep } from '@aztec/foundation/sleep'; import { createDebugLogger } from '@aztec/foundation/log'; import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { MerkleTreeId } from '@aztec/world-state'; +import { Fr } from '@aztec/circuits.js'; export class Synchroniser { private runningPromise?: Promise; private accountStates: AccountState[] = []; private interruptableSleep = new InterruptableSleep(); private running = false; + private initialSyncBlockHeight = 0; constructor( private node: AztecNode, @@ -19,49 +22,15 @@ export class Synchroniser { private log = createDebugLogger('aztec:aztec_rpc_synchroniser'), ) {} - public start(from = 1, take = 1, retryInterval = 1000) { - if (this.running) { - return; - } - + public async start(from = 1, take = 1, retryInterval = 1000) { + if (this.running) return; this.running = true; + await this.initialSync(); + const run = async () => { while (this.running) { - try { - let unverifiedData = await this.node.getUnverifiedData(from, take); - if (!unverifiedData.length) { - await this.interruptableSleep.sleep(retryInterval); - continue; - } - - // Note: If less than `take` unverified data is returned, then I fetch only that number of blocks. - const blocks = await this.node.getBlocks(from, unverifiedData.length); - if (!blocks.length) { - await this.interruptableSleep.sleep(retryInterval); - continue; - } - - if (blocks.length !== unverifiedData.length) { - // "Trim" the unverified data to match the number of blocks. - unverifiedData = unverifiedData.slice(0, blocks.length); - } - - // Wrap blocks in block contexts. - const blockContexts = blocks.map(block => new L2BlockContext(block)); - - this.log( - `Forwarding ${unverifiedData.length} unverified data and blocks to ${this.accountStates.length} account states`, - ); - for (const accountState of this.accountStates) { - await accountState.process(blockContexts, unverifiedData); - } - - from += unverifiedData.length; - } catch (err) { - console.log(err); - await this.interruptableSleep.sleep(retryInterval); - } + from = await this.work(from, take, retryInterval); } }; @@ -69,6 +38,72 @@ export class Synchroniser { this.log('Started'); } + protected async initialSync() { + const [blockNumber, treeRoots] = await Promise.all([this.node.getBlockHeight(), this.node.getTreeRoots()]); + this.initialSyncBlockHeight = blockNumber; + await this.db.setTreeRoots(treeRoots); + } + + protected async work(from = 1, take = 1, retryInterval = 1000): Promise { + try { + let unverifiedData = await this.node.getUnverifiedData(from, take); + if (!unverifiedData.length) { + await this.interruptableSleep.sleep(retryInterval); + return from; + } + + // Note: If less than `take` unverified data is returned, then I fetch only that number of blocks. + const blocks = await this.node.getBlocks(from, unverifiedData.length); + if (!blocks.length) { + await this.interruptableSleep.sleep(retryInterval); + return from; + } + + if (blocks.length !== unverifiedData.length) { + // "Trim" the unverified data to match the number of blocks. + unverifiedData = unverifiedData.slice(0, blocks.length); + } + + // Wrap blocks in block contexts. + const blockContexts = blocks.map(block => new L2BlockContext(block)); + + // Update latest tree roots from the most recent block + const latestBlock = blockContexts[blockContexts.length - 1]; + await this.setTreeRootsFromBlock(latestBlock); + + this.log( + `Forwarding ${unverifiedData.length} unverified data and blocks to ${this.accountStates.length} account states`, + ); + for (const accountState of this.accountStates) { + await accountState.process(blockContexts, unverifiedData); + } + + from += unverifiedData.length; + return from; + } catch (err) { + console.log(err); + await this.interruptableSleep.sleep(retryInterval); + return from; + } + } + + private async setTreeRootsFromBlock(latestBlock: L2BlockContext) { + const { block } = latestBlock; + if (block.number < this.initialSyncBlockHeight) return; + + const roots: Record = { + [MerkleTreeId.CONTRACT_TREE]: block.endContractTreeSnapshot.root, + [MerkleTreeId.PRIVATE_DATA_TREE]: block.endPrivateDataTreeSnapshot.root, + [MerkleTreeId.NULLIFIER_TREE]: block.endNullifierTreeSnapshot.root, + [MerkleTreeId.PUBLIC_DATA_TREE]: block.endPublicDataTreeRoot, + [MerkleTreeId.L1_TO_L2_MESSAGES_TREE]: block.endL1ToL2MessageTreeSnapshot.root, + [MerkleTreeId.L1_TO_L2_MESSAGES_ROOTS_TREE]: block.endTreeOfHistoricL1ToL2MessageTreeRootsSnapshot.root, + [MerkleTreeId.CONTRACT_TREE_ROOTS_TREE]: block.endTreeOfHistoricContractTreeRootsSnapshot.root, + [MerkleTreeId.PRIVATE_DATA_TREE_ROOTS_TREE]: block.endTreeOfHistoricPrivateDataTreeRootsSnapshot.root, + }; + await this.db.setTreeRoots(roots); + } + public async stop() { this.running = false; this.interruptableSleep.interrupt(); From 2e87356f3472d1eb75dab57a8f28f53910a9e3fb Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Thu, 4 May 2023 11:21:18 -0300 Subject: [PATCH 7/7] Move MerkleTreeId to types to avoid dependency on world-state from aztec-rpc --- .../aztec-node/src/aztec-node/aztec-node.ts | 6 +++--- .../aztec-rpc/src/account_state/account_state.ts | 2 +- yarn-project/aztec-rpc/src/database/database.ts | 2 +- yarn-project/aztec-rpc/src/database/memory_db.ts | 2 +- .../src/synchroniser/synchroniser.test.ts | 2 +- .../aztec-rpc/src/synchroniser/synchroniser.ts | 2 +- .../src/block_builder/solo_block_builder.test.ts | 4 ++-- .../src/block_builder/solo_block_builder.ts | 4 ++-- .../src/sequencer/sequencer.test.ts | 4 ++-- .../sequencer-client/src/sequencer/sequencer.ts | 13 +++++++++++-- .../sequencer-client/src/sequencer/utils.ts | 3 ++- .../sequencer-client/src/simulator/fake_public.ts | 3 ++- yarn-project/types/src/index.ts | 7 ++++--- yarn-project/types/src/merkle_tree_id.ts | 13 +++++++++++++ .../merkle-tree/merkle_tree_operations_facade.ts | 3 ++- .../server_world_state_synchroniser.test.ts | 4 ++-- .../server_world_state_synchroniser.ts | 4 ++-- .../world-state/src/world-state-db/index.ts | 15 +-------------- .../src/world-state-db/merkle_trees.ts | 2 +- 19 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 yarn-project/types/src/merkle_tree_id.ts diff --git a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts index ec6319b9e2b4..c720f3dafef4 100644 --- a/yarn-project/aztec-node/src/aztec-node/aztec-node.ts +++ b/yarn-project/aztec-node/src/aztec-node/aztec-node.ts @@ -1,6 +1,8 @@ import { Archiver } from '@aztec/archiver'; import { PrimitivesWasm } from '@aztec/barretenberg.js/wasm'; import { CircuitsWasm } from '@aztec/circuits.js'; +import { AztecAddress } from '@aztec/foundation/aztec-address'; +import { Fr } from '@aztec/foundation/fields'; import { SiblingPath } from '@aztec/merkle-tree'; import { P2P, P2PClient } from '@aztec/p2p'; import { SequencerClient } from '@aztec/sequencer-client'; @@ -10,13 +12,13 @@ import { ContractPublicData, L2Block, L2BlockSource, + MerkleTreeId, Tx, TxHash, UnverifiedData, UnverifiedDataSource, } from '@aztec/types'; import { - MerkleTreeId, MerkleTrees, ServerWorldStateSynchroniser, WorldStateSynchroniser, @@ -25,8 +27,6 @@ import { import { default as levelup } from 'levelup'; import { MemDown, default as memdown } from 'memdown'; import { AztecNodeConfig } from './config.js'; -import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr } from '@aztec/foundation/fields'; export const createMemDown = () => (memdown as any)() as MemDown; diff --git a/yarn-project/aztec-rpc/src/account_state/account_state.ts b/yarn-project/aztec-rpc/src/account_state/account_state.ts index bd72c254ccc9..7ad197032999 100644 --- a/yarn-project/aztec-rpc/src/account_state/account_state.ts +++ b/yarn-project/aztec-rpc/src/account_state/account_state.ts @@ -10,7 +10,7 @@ import { KernelProver, OutputNoteData } from '@aztec/kernel-prover'; import { INITIAL_L2_BLOCK_NUM } from '@aztec/types'; import { FunctionType } from '@aztec/noir-contracts'; import { EncodedContractFunction, L2BlockContext, Tx, UnverifiedData } from '@aztec/types'; -import { MerkleTreeId } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; import { NotePreimage, TxAuxData } from '../aztec_rpc_server/tx_aux_data/index.js'; import { ContractDataOracle } from '../contract_data_oracle/index.js'; import { Database, TxAuxDataDao, TxDao } from '../database/index.js'; diff --git a/yarn-project/aztec-rpc/src/database/database.ts b/yarn-project/aztec-rpc/src/database/database.ts index 2bea12e6d360..2b48eb6f0b7a 100644 --- a/yarn-project/aztec-rpc/src/database/database.ts +++ b/yarn-project/aztec-rpc/src/database/database.ts @@ -4,7 +4,7 @@ import { TxAuxDataDao } from './tx_aux_data_dao.js'; import { TxDao } from './tx_dao.js'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr, Point } from '@aztec/foundation/fields'; -import { MerkleTreeId } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; export interface Database extends ContractDatabase { getTx(txHash: TxHash): Promise; diff --git a/yarn-project/aztec-rpc/src/database/memory_db.ts b/yarn-project/aztec-rpc/src/database/memory_db.ts index be2508bdc56d..23cd21d92d80 100644 --- a/yarn-project/aztec-rpc/src/database/memory_db.ts +++ b/yarn-project/aztec-rpc/src/database/memory_db.ts @@ -5,7 +5,7 @@ import { TxAuxDataDao } from './tx_aux_data_dao.js'; import { TxDao } from './tx_dao.js'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { Fr, Point } from '@aztec/foundation/fields'; -import { MerkleTreeId } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; export class MemoryDB extends MemoryContractDatabase implements Database { private txTable: TxDao[] = []; diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts index 966b5bab99b6..8072c8c11b6d 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts @@ -5,7 +5,7 @@ import { Database, MemoryDB } from '../database/index.js'; import { ConstantKeyPair } from '../key_store/index.js'; import { Synchroniser } from './synchroniser.js'; import { L2Block, UnverifiedData } from '@aztec/types'; -import { MerkleTreeId } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; import { Fr } from '@aztec/circuits.js'; describe('Synchroniser', () => { diff --git a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts index 840356ffe06c..b2082e7ba25c 100644 --- a/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts +++ b/yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts @@ -6,7 +6,7 @@ import { Database, TxDao } from '../database/index.js'; import { InterruptableSleep } from '@aztec/foundation/sleep'; import { createDebugLogger } from '@aztec/foundation/log'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { MerkleTreeId } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; import { Fr } from '@aztec/circuits.js'; export class Synchroniser { diff --git a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.test.ts b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.test.ts index f0697a11802f..bf00f7f5d866 100644 --- a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.test.ts +++ b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.test.ts @@ -20,8 +20,8 @@ import { makeRootRollupPublicInputs, } from '@aztec/circuits.js/factories'; import { toBufferBE } from '@aztec/foundation/bigint-buffer'; -import { ContractData, L2Block, PublicDataWrite, Tx } from '@aztec/types'; -import { MerkleTreeId, MerkleTreeOperations, MerkleTrees } from '@aztec/world-state'; +import { MerkleTreeId, ContractData, L2Block, PublicDataWrite, Tx } from '@aztec/types'; +import { MerkleTreeOperations, MerkleTrees } from '@aztec/world-state'; import { MockProxy, mock } from 'jest-mock-extended'; import { default as levelup } from 'levelup'; import flatMap from 'lodash.flatmap'; diff --git a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts index 0f59f9ae4f73..7b396f07fd69 100644 --- a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts +++ b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts @@ -24,8 +24,8 @@ import { } from '@aztec/circuits.js'; import { computeContractLeaf } from '@aztec/circuits.js/abis'; import { LeafData, SiblingPath } from '@aztec/merkle-tree'; -import { ContractData, L2Block, PublicDataWrite } from '@aztec/types'; -import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state'; +import { MerkleTreeId, ContractData, L2Block, PublicDataWrite } from '@aztec/types'; +import { MerkleTreeOperations } from '@aztec/world-state'; import chunk from 'lodash.chunk'; import flatMap from 'lodash.flatmap'; import times from 'lodash.times'; diff --git a/yarn-project/sequencer-client/src/sequencer/sequencer.test.ts b/yarn-project/sequencer-client/src/sequencer/sequencer.test.ts index e39d672b7443..68944b043444 100644 --- a/yarn-project/sequencer-client/src/sequencer/sequencer.test.ts +++ b/yarn-project/sequencer-client/src/sequencer/sequencer.test.ts @@ -1,7 +1,7 @@ import { CombinedHistoricTreeRoots, Fr, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, makeEmptyProof } from '@aztec/circuits.js'; import { P2P, P2PClientState } from '@aztec/p2p'; -import { L2Block, PrivateTx, Tx, UnverifiedData } from '@aztec/types'; -import { MerkleTreeId, MerkleTreeOperations, WorldStateRunningState, WorldStateSynchroniser } from '@aztec/world-state'; +import { L2Block, MerkleTreeId, PrivateTx, Tx, UnverifiedData } from '@aztec/types'; +import { MerkleTreeOperations, WorldStateRunningState, WorldStateSynchroniser } from '@aztec/world-state'; import { MockProxy, mock } from 'jest-mock-extended'; import times from 'lodash.times'; import { BlockBuilder } from '../block_builder/index.js'; diff --git a/yarn-project/sequencer-client/src/sequencer/sequencer.ts b/yarn-project/sequencer-client/src/sequencer/sequencer.ts index 52cc67ad3703..d43ceeb9dc14 100644 --- a/yarn-project/sequencer-client/src/sequencer/sequencer.ts +++ b/yarn-project/sequencer-client/src/sequencer/sequencer.ts @@ -1,6 +1,15 @@ import { P2P } from '@aztec/p2p'; -import { ContractData, ContractPublicData, PrivateTx, PublicTx, Tx, UnverifiedData, isPrivateTx } from '@aztec/types'; -import { MerkleTreeId, WorldStateStatus, WorldStateSynchroniser } from '@aztec/world-state'; +import { + MerkleTreeId, + ContractData, + ContractPublicData, + PrivateTx, + PublicTx, + Tx, + UnverifiedData, + isPrivateTx, +} from '@aztec/types'; +import { WorldStateStatus, WorldStateSynchroniser } from '@aztec/world-state'; import times from 'lodash.times'; import { BlockBuilder } from '../block_builder/index.js'; import { L1Publisher } from '../publisher/l1-publisher.js'; diff --git a/yarn-project/sequencer-client/src/sequencer/utils.ts b/yarn-project/sequencer-client/src/sequencer/utils.ts index 4d5833dc2a58..a831c738682a 100644 --- a/yarn-project/sequencer-client/src/sequencer/utils.ts +++ b/yarn-project/sequencer-client/src/sequencer/utils.ts @@ -1,5 +1,6 @@ import { CombinedHistoricTreeRoots, Fr, PrivateHistoricTreeRoots } from '@aztec/circuits.js'; -import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; +import { MerkleTreeOperations } from '@aztec/world-state'; /** * Fetches the private, nullifier, and contract tree roots from a given db and assembles a CombinedHistoricTreeRoots object. diff --git a/yarn-project/sequencer-client/src/simulator/fake_public.ts b/yarn-project/sequencer-client/src/simulator/fake_public.ts index c9007dadeff3..61c9bc923af6 100644 --- a/yarn-project/sequencer-client/src/simulator/fake_public.ts +++ b/yarn-project/sequencer-client/src/simulator/fake_public.ts @@ -1,7 +1,8 @@ import { PublicDB, PublicExecution } from '@aztec/acir-simulator'; import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm'; import { AztecAddress, EthAddress, Fr, PublicCircuitPublicInputs, TxRequest } from '@aztec/circuits.js'; -import { MerkleTreeId, MerkleTreeOperations, computePublicDataTreeLeafIndex } from '@aztec/world-state'; +import { MerkleTreeId } from '@aztec/types'; +import { MerkleTreeOperations, computePublicDataTreeLeafIndex } from '@aztec/world-state'; import { PublicCircuitSimulator } from './index.js'; /** diff --git a/yarn-project/types/src/index.ts b/yarn-project/types/src/index.ts index 154a5571a312..fc5a639770d6 100644 --- a/yarn-project/types/src/index.ts +++ b/yarn-project/types/src/index.ts @@ -1,12 +1,13 @@ -export * from './tx.js'; -export * from './tx_hash.js'; export * from './constants.js'; -export * from './l2_block_downloader/index.js'; export * from './contract_data.js'; export * from './l1_addresses.js'; export * from './l2_block.js'; export * from './l2_block_context.js'; +export * from './l2_block_downloader/index.js'; export * from './l2_block_source.js'; +export * from './merkle_tree_id.js'; export * from './public_data_write.js'; +export * from './tx.js'; +export * from './tx_hash.js'; export * from './unverified_data.js'; export * from './unverified_data_source.js'; diff --git a/yarn-project/types/src/merkle_tree_id.ts b/yarn-project/types/src/merkle_tree_id.ts new file mode 100644 index 000000000000..807204271c93 --- /dev/null +++ b/yarn-project/types/src/merkle_tree_id.ts @@ -0,0 +1,13 @@ +/** + * Defines the possible Merkle tree IDs. + */ +export enum MerkleTreeId { + CONTRACT_TREE = 0, + CONTRACT_TREE_ROOTS_TREE = 1, + NULLIFIER_TREE = 2, + PRIVATE_DATA_TREE = 3, + PRIVATE_DATA_TREE_ROOTS_TREE = 4, + PUBLIC_DATA_TREE = 5, + L1_TO_L2_MESSAGES_TREE = 6, + L1_TO_L2_MESSAGES_ROOTS_TREE = 7, +} diff --git a/yarn-project/world-state/src/merkle-tree/merkle_tree_operations_facade.ts b/yarn-project/world-state/src/merkle-tree/merkle_tree_operations_facade.ts index 047743daa505..5a4d131dc29d 100644 --- a/yarn-project/world-state/src/merkle-tree/merkle_tree_operations_facade.ts +++ b/yarn-project/world-state/src/merkle-tree/merkle_tree_operations_facade.ts @@ -1,5 +1,6 @@ import { SiblingPath } from '@aztec/merkle-tree'; -import { LeafData, MerkleTreeDbOperations, MerkleTreeId, MerkleTreeOperations, TreeInfo } from '../index.js'; +import { LeafData, MerkleTreeDbOperations, MerkleTreeOperations, TreeInfo } from '../index.js'; +import { MerkleTreeId } from '@aztec/types'; /** * Wraps a MerkleTreeDbOperations to call all functions with a preset includeUncommitted flag. diff --git a/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.test.ts b/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.test.ts index 7586b04f0a72..08378bf41473 100644 --- a/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.test.ts +++ b/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.test.ts @@ -3,9 +3,9 @@ import { AppendOnlyTreeSnapshot, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@az import { fr } from '@aztec/circuits.js/factories'; import { INITIAL_LEAF, Pedersen, SiblingPath } from '@aztec/merkle-tree'; -import { ContractData, L2Block, L2BlockSource, PublicDataWrite } from '@aztec/types'; +import { ContractData, L2Block, L2BlockSource, MerkleTreeId, PublicDataWrite } from '@aztec/types'; import { jest } from '@jest/globals'; -import { MerkleTreeDb, MerkleTreeId } from '../index.js'; +import { MerkleTreeDb } from '../index.js'; import { ServerWorldStateSynchroniser } from './server_world_state_synchroniser.js'; import { WorldStateRunningState } from './world_state_synchroniser.js'; import { Fr } from '@aztec/foundation/fields'; diff --git a/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.ts b/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.ts index 79c7cc15f845..08aae2329177 100644 --- a/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.ts +++ b/yarn-project/world-state/src/synchroniser/server_world_state_synchroniser.ts @@ -1,5 +1,5 @@ -import { L2Block, L2BlockDownloader, L2BlockSource } from '@aztec/types'; -import { MerkleTreeDb, MerkleTreeId, MerkleTreeOperations } from '../index.js'; +import { L2Block, L2BlockDownloader, L2BlockSource, MerkleTreeId } from '@aztec/types'; +import { MerkleTreeDb, MerkleTreeOperations } from '../index.js'; import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js'; import { WorldStateRunningState, WorldStateStatus, WorldStateSynchroniser } from './world_state_synchroniser.js'; import { getConfigEnvVars } from './config.js'; 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 36944827a887..e54e5d1131f8 100644 --- a/yarn-project/world-state/src/world-state-db/index.ts +++ b/yarn-project/world-state/src/world-state-db/index.ts @@ -1,22 +1,9 @@ import { LeafData, SiblingPath } from '@aztec/merkle-tree'; +import { MerkleTreeId } from '@aztec/types'; export * from './merkle_trees.js'; export { LeafData } from '@aztec/merkle-tree'; -/** - * Defines the possible Merkle tree IDs. - */ -export enum MerkleTreeId { - CONTRACT_TREE = 0, - CONTRACT_TREE_ROOTS_TREE = 1, - NULLIFIER_TREE = 2, - PRIVATE_DATA_TREE = 3, - PRIVATE_DATA_TREE_ROOTS_TREE = 4, - PUBLIC_DATA_TREE = 5, - L1_TO_L2_MESSAGES_TREE = 6, - L1_TO_L2_MESSAGES_ROOTS_TREE = 7, -} - /** * Type alias for the nullifier tree ID. */ 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 854f0701ba85..9a109d6d6cd0 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 @@ -29,11 +29,11 @@ import { INITIAL_NULLIFIER_TREE_SIZE, IndexedTreeId, MerkleTreeDb, - MerkleTreeId, MerkleTreeOperations, PublicTreeId, TreeInfo, } from './index.js'; +import { MerkleTreeId } from '@aztec/types'; import { SerialQueue } from '@aztec/foundation/fifo'; /**