From 276a6602dd23f58feb5be897fd437980e8eae1cf Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 30 Jan 2024 14:26:42 +0000 Subject: [PATCH 01/25] refactor: updating block hash to be header.hash() --- .../aztec-nr/aztec/src/oracle/header.nr | 2 +- .../circuits.js/src/abis/abis.test.ts | 39 +-------- yarn-project/circuits.js/src/abis/abis.ts | 84 ------------------- .../circuits.js/src/structs/header.test.ts | 7 ++ .../circuits.js/src/structs/header.ts | 12 ++- .../rollup-lib/src/base/base_rollup_inputs.nr | 4 +- .../src/crates/rollup-lib/src/hash.nr | 13 --- .../src/crates/rollup-lib/src/lib.nr | 2 - .../src/crates/rollup-lib/src/root.nr | 22 ++--- .../crates/types/src/abis/global_variables.nr | 14 ---- .../src/crates/types/src/header.nr | 24 +++--- .../src/world-state-db/merkle_trees.ts | 1 - 12 files changed, 41 insertions(+), 183 deletions(-) delete mode 100644 yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/hash.nr diff --git a/yarn-project/aztec-nr/aztec/src/oracle/header.nr b/yarn-project/aztec-nr/aztec/src/oracle/header.nr index 33718bf206bb..bcb7027d2d63 100644 --- a/yarn-project/aztec-nr/aztec/src/oracle/header.nr +++ b/yarn-project/aztec-nr/aztec/src/oracle/header.nr @@ -40,7 +40,7 @@ pub fn get_header_at(block_number: u32, context: PrivateContext) -> Header { let header = get_header_at_internal(block_number); // 4) Compute the block hash from the block header - let block_hash = header.block_hash(); + let block_hash = header.hash(); // 5) Get the membership witness of the block in the archive let witness = get_archive_membership_witness(last_archive_block_number, block_hash); diff --git a/yarn-project/circuits.js/src/abis/abis.test.ts b/yarn-project/circuits.js/src/abis/abis.test.ts index 30ed11ab2629..88a28a0dcf0a 100644 --- a/yarn-project/circuits.js/src/abis/abis.test.ts +++ b/yarn-project/circuits.js/src/abis/abis.test.ts @@ -6,7 +6,6 @@ import { FunctionData, FunctionLeafPreimage, FunctionSelector, - GlobalVariables, NewContractData, PublicCallStackItem, PublicCircuitPublicInputs, @@ -22,14 +21,12 @@ import { makeVerificationKey, } from '../tests/factories.js'; import { - computeBlockHashWithGlobals, computeCommitmentNonce, computeCommitmentsHash, computeContractLeaf, computeFunctionLeaf, computeFunctionSelector, computeFunctionTreeRoot, - computeGlobalsHash, computeNullifierHash, computePrivateCallStackItemHash, computePublicCallStackItemHash, @@ -44,7 +41,7 @@ import { hashTxRequest, hashVK, siloCommitment, - siloNullifier, + siloNullifier } from './abis.js'; describe('abis', () => { @@ -113,40 +110,6 @@ describe('abis', () => { expect(res).toMatchSnapshot(); }); - it('computes block hash with globals', () => { - const globals = GlobalVariables.from({ - chainId: new Fr(1n), - version: new Fr(2n), - blockNumber: new Fr(3n), - timestamp: new Fr(4n), - }); - const noteHashTreeRoot = new Fr(5n); - const nullifierTreeRoot = new Fr(6n); - const contractTreeRoot = new Fr(7n); - const l1ToL2DataTreeRoot = new Fr(8n); - const publicDataTreeRoot = new Fr(9n); - const res = computeBlockHashWithGlobals( - globals, - noteHashTreeRoot, - nullifierTreeRoot, - contractTreeRoot, - l1ToL2DataTreeRoot, - publicDataTreeRoot, - ); - expect(res).toMatchSnapshot(); - }); - - it('compute globals hash', () => { - const globals = GlobalVariables.from({ - chainId: new Fr(1n), - version: new Fr(2n), - blockNumber: new Fr(3n), - timestamp: new Fr(4n), - }); - const res = computeGlobalsHash(globals); - expect(res).toMatchSnapshot(); - }); - it('computes public data tree value', () => { const value = new Fr(3n); const res = computePublicDataTreeValue(value); diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index 1b479589ac48..791e12e77465 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -212,90 +212,6 @@ export function siloNullifier(contract: AztecAddress, innerNullifier: Fr): Fr { return Fr.fromBuffer(pedersenHash([contract.toBuffer(), innerNullifier.toBuffer()], GeneratorIndex.OUTER_NULLIFIER)); } -/** - * Computes the block hash given the blocks globals and roots. - * @param globals - The global variables to put into the block hash. - * @param noteHashTree - The root of the note hash tree. - * @param nullifierTreeRoot - The root of the nullifier tree. - * @param contractTreeRoot - The root of the contract tree. - * @param l1ToL2DataTreeRoot - The root of the l1 to l2 data tree. - * @param publicDataTreeRoot - The root of the public data tree. - * @returns The block hash. - */ -// TODO(#3941) -export function computeBlockHashWithGlobals( - globals: GlobalVariables, - noteHashTreeRoot: Fr, - nullifierTreeRoot: Fr, - contractTreeRoot: Fr, - l1ToL2DataTreeRoot: Fr, - publicDataTreeRoot: Fr, -): Fr { - return computeBlockHash( - computeGlobalsHash(globals), - noteHashTreeRoot, - nullifierTreeRoot, - contractTreeRoot, - l1ToL2DataTreeRoot, - publicDataTreeRoot, - ); -} - -/** - * Computes the block hash given the blocks globals and roots. - * @param globalsHash - The global variables hash to put into the block hash. - * @param noteHashTree - The root of the note hash tree. - * @param nullifierTreeRoot - The root of the nullifier tree. - * @param contractTreeRoot - The root of the contract tree. - * @param l1ToL2DataTreeRoot - The root of the l1 to l2 data tree. - * @param publicDataTreeRoot - The root of the public data tree. - * @returns The block hash. - */ -// TODO(#3941): nuke this and replace with `Header.hash()` -export function computeBlockHash( - globalsHash: Fr, - noteHashTreeRoot: Fr, - nullifierTreeRoot: Fr, - contractTreeRoot: Fr, - l1ToL2DataTreeRoot: Fr, - publicDataTreeRoot: Fr, -): Fr { - return Fr.fromBuffer( - pedersenHash( - [ - globalsHash.toBuffer(), - noteHashTreeRoot.toBuffer(), - nullifierTreeRoot.toBuffer(), - contractTreeRoot.toBuffer(), - l1ToL2DataTreeRoot.toBuffer(), - publicDataTreeRoot.toBuffer(), - ], - GeneratorIndex.BLOCK_HASH, - ), - ); -} - -/** - * Computes the globals hash given the globals. - * @param globals - The global variables to put into the block hash. - * @returns The globals hash. - * TODO: move this to GlobalVariables? - * TODO(#3941) Investigate whether to nuke this once #3941 is done. - */ -export function computeGlobalsHash(globals: GlobalVariables): Fr { - return Fr.fromBuffer( - pedersenHash( - [ - globals.chainId.toBuffer(), - globals.version.toBuffer(), - globals.blockNumber.toBuffer(), - globals.timestamp.toBuffer(), - ], - GeneratorIndex.GLOBAL_VARIABLES, - ), - ); -} - /** * Computes a public data tree value ready for insertion. * @param value - Raw public data tree value to hash into a tree-insertion-ready value. diff --git a/yarn-project/circuits.js/src/structs/header.test.ts b/yarn-project/circuits.js/src/structs/header.test.ts index 5581a1b482a3..72d49ae9bf27 100644 --- a/yarn-project/circuits.js/src/structs/header.test.ts +++ b/yarn-project/circuits.js/src/structs/header.test.ts @@ -18,4 +18,11 @@ describe('Header', () => { const res = Header.fromFieldArray(fieldArray); expect(res).toEqual(expected); }); + + it('computes hash', () => { + const seed = 9870243; + const header = makeHeader(seed, undefined); + const hash = header.hash(); + expect(hash).toMatchSnapshot(); + }); }); diff --git a/yarn-project/circuits.js/src/structs/header.ts b/yarn-project/circuits.js/src/structs/header.ts index 9f78ac3a1a02..e770d2864301 100644 --- a/yarn-project/circuits.js/src/structs/header.ts +++ b/yarn-project/circuits.js/src/structs/header.ts @@ -1,7 +1,8 @@ +import { pedersenHash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { BufferReader, from2Fields, serializeToBuffer, to2Fields } from '@aztec/foundation/serialize'; -import { HEADER_LENGTH } from '../constants.gen.js'; +import { GeneratorIndex, HEADER_LENGTH } from '../constants.gen.js'; import { GlobalVariables } from './global_variables.js'; import { PartialStateReference } from './partial_state_reference.js'; import { AppendOnlyTreeSnapshot } from './rollup/append_only_tree_snapshot.js'; @@ -107,4 +108,13 @@ export class Header { const buffer = Buffer.from(str.replace(/^0x/i, ''), 'hex'); return Header.fromBuffer(buffer); } + + hash(): Fr { + return Fr.fromBuffer( + pedersenHash( + this.toFieldArray().map(f => f.toBuffer()), + GeneratorIndex.BLOCK_HASH, + ), + ); + } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr index c6ac3c04df33..1d394206a837 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr @@ -339,7 +339,7 @@ impl BaseRollupInputs { // Rebuild the block hash let header = self.kernel_data.public_inputs.constants.historical_header; - let previous_block_hash = header.block_hash(); + let previous_block_hash = header.hash(); let previous_block_hash_witness = self.archive_root_membership_witness; @@ -738,7 +738,7 @@ mod tests { let _nullifier = builder.end.new_nullifiers.pop(); inputs.kernel_data = builder.is_public(); - inputs.pre_existing_blocks[0] = inputs.kernel_data.historical_header.block_hash(); + inputs.pre_existing_blocks[0] = inputs.kernel_data.historical_header.hash(); inputs } diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/hash.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/hash.nr deleted file mode 100644 index 6f314de2f249..000000000000 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/hash.nr +++ /dev/null @@ -1,13 +0,0 @@ -use dep::types::{ - abis::global_variables::GlobalVariables, - constants::GENERATOR_INDEX__BLOCK_HASH, - state_reference::StateReference, -}; - -pub fn compute_block_hash_with_globals(globals: GlobalVariables, state: StateReference) -> Field { - let inputs = [ - globals.hash(), state.partial.note_hash_tree.root, state.partial.nullifier_tree.root, state.partial.contract_tree.root, state.l1_to_l2_message_tree.root, state.partial.public_data_tree.root - ]; - - dep::std::hash::pedersen_hash_with_separator(inputs, GENERATOR_INDEX__BLOCK_HASH) -} diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/lib.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/lib.nr index 1d0df93cc9df..08534d6cb250 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/lib.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/lib.nr @@ -11,8 +11,6 @@ mod root; mod components; -mod hash; - mod merkle_tree; mod tests; diff --git a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr index 1fca5912f628..cfd8ad62d4e9 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/root.nr @@ -13,7 +13,7 @@ use dep::types::{ state_reference::StateReference, utils::uint256::U256, }; -use crate::{components, hash::compute_block_hash_with_globals}; +use crate::components; use crate::merkle_tree::{calculate_subtree, calculate_empty_tree_root}; impl RootRollupInputs { @@ -49,12 +49,15 @@ impl RootRollupInputs { partial: right.end, }; - // Build the block hash for this iteration from the tree roots and global variables - // Then insert the block into the archive tree - let block_hash = compute_block_hash_with_globals( - left.constants.global_variables, + let header = Header { + last_archive: left.constants.last_archive, + body_hash: components::compute_calldata_hash(self.previous_rollup_data), state, - ); + global_variables : left.constants.global_variables + }; + + // Build the block hash for this by hashing the header and then insert the new leaf to archive tree. + let block_hash = header.hash(); // Update the archive let archive = components::insert_subtree_to_snapshot_tree( @@ -65,13 +68,6 @@ impl RootRollupInputs { 0 ); - let header = Header { - last_archive: left.constants.last_archive, - body_hash: components::compute_calldata_hash(self.previous_rollup_data), - state, - global_variables : left.constants.global_variables - }; - RootRollupPublicInputs{ aggregation_object, archive, diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr index 23a43a027034..d5f2857151b9 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/global_variables.nr @@ -45,20 +45,6 @@ impl Eq for GlobalVariables { } } -// TODO(#3941) This is only used in Header.block_hash() --> Nuke it once that func is updated -impl Hash for GlobalVariables { - fn hash(self) -> Field { - dep::std::hash::pedersen_hash_with_separator([ - self.chain_id, - self.version, - self.block_number, - self.timestamp - ], - GENERATOR_INDEX__GLOBAL_VARIABLES, - ) - } -} - impl Empty for GlobalVariables { fn empty() -> Self { Self { diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr index b838c4534808..d81ba17993de 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr @@ -19,7 +19,10 @@ use crate::{ StateReference, STATE_REFERENCE_LENGTH, }, - traits::Empty, + traits::{ + Empty, + Hash, + }, utils::{ arr_copy_slice, bounded_vec::BoundedVec, @@ -69,19 +72,6 @@ impl Header { global_variables: GlobalVariables::deserialize(global_variables_fields), } } - - // TODO(#3941) Rename this as hash() and make it hash the whole header - pub fn block_hash(self) -> Field { - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595) - pedersen_hash([ - self.global_variables.hash(), - self.state.partial.note_hash_tree.root, - self.state.partial.nullifier_tree.root, - self.state.partial.contract_tree.root, - self.state.l1_to_l2_message_tree.root, - self.state.partial.public_data_tree.root, - ], GENERATOR_INDEX__BLOCK_HASH) - } } impl Empty for Header { @@ -94,3 +84,9 @@ impl Empty for Header { } } } + +impl Hash for Header { + fn hash(self) -> Field { + pedersen_hash(self.serialize(), GENERATOR_INDEX__BLOCK_HASH) + } +} 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 21d4e4532365..35177e00a743 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 @@ -19,7 +19,6 @@ import { PublicDataTreeLeafPreimage, StateReference, } from '@aztec/circuits.js'; -import { computeBlockHash, computeGlobalsHash } from '@aztec/circuits.js/abis'; import { Committable } from '@aztec/foundation/committable'; import { SerialQueue } from '@aztec/foundation/fifo'; import { createDebugLogger } from '@aztec/foundation/log'; From 753ce381b2ae2a9ca8afd3f745858c6879a8c45d Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 30 Jan 2024 16:22:38 +0000 Subject: [PATCH 02/25] WIP --- yarn-project/circuit-types/src/l2_block.ts | 1 + .../src/block_builder/solo_block_builder.ts | 35 ------------------- .../sequencer-client/src/sequencer/utils.ts | 1 + .../world-state-db/merkle_tree_operations.ts | 5 --- .../merkle_tree_operations_facade.ts | 7 ---- .../merkle_tree_snapshot_operations_facade.ts | 4 --- .../src/world-state-db/merkle_trees.ts | 28 ++------------- 7 files changed, 5 insertions(+), 76 deletions(-) diff --git a/yarn-project/circuit-types/src/l2_block.ts b/yarn-project/circuit-types/src/l2_block.ts index 0f7dc8844e84..ccc3f7bdcea3 100644 --- a/yarn-project/circuit-types/src/l2_block.ts +++ b/yarn-project/circuit-types/src/l2_block.ts @@ -443,6 +443,7 @@ export class L2Block { /** * Returns the block's hash. * @returns The block's hash. + * TODO: rename this as hash and make it return this.header.hash() */ public getBlockHash(): Buffer { if (!this.blockHash) { 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 49318dcaa672..9ddff6a8b521 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 @@ -43,10 +43,7 @@ import { VerificationKey, } from '@aztec/circuits.js'; import { - computeBlockHash, - computeBlockHashWithGlobals, computeContractLeaf, - computeGlobalsHash, } from '@aztec/circuits.js/abis'; import { makeTuple } from '@aztec/foundation/array'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; @@ -279,8 +276,6 @@ export class SoloBlockBuilder implements BlockBuilder { // Update the root trees with the latest data and contract tree roots, // and validate them against the output of the root circuit simulation this.debug(`Updating and validating root trees`); - const globalVariablesHash = computeGlobalsHash(left[0].constants.globalVariables); - await this.db.updateLatestGlobalVariablesHash(globalVariablesHash); await this.db.updateArchive(globalVariablesHash); await this.validateRootOutput(rootOutput); @@ -288,36 +283,6 @@ export class SoloBlockBuilder implements BlockBuilder { return [rootOutput, rootProof]; } - async updateArchive(globalVariables: GlobalVariables) { - // Calculate the block hash and add it to the historical block hashes tree - const blockHash = await this.calculateBlockHash(globalVariables); - await this.db.appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); - } - - protected async calculateBlockHash(globals: GlobalVariables) { - const [noteHashTreeRoot, nullifierTreeRoot, contractTreeRoot, publicDataTreeRoot, l1ToL2MessageTreeRoot] = ( - await Promise.all( - [ - MerkleTreeId.NOTE_HASH_TREE, - MerkleTreeId.NULLIFIER_TREE, - MerkleTreeId.CONTRACT_TREE, - MerkleTreeId.PUBLIC_DATA_TREE, - MerkleTreeId.L1_TO_L2_MESSAGE_TREE, - ].map(tree => this.getTreeSnapshot(tree)), - ) - ).map(r => r.root); - - const blockHash = computeBlockHashWithGlobals( - globals, - noteHashTreeRoot, - nullifierTreeRoot, - contractTreeRoot, - l1ToL2MessageTreeRoot, - publicDataTreeRoot, - ); - return blockHash; - } - protected async validatePartialState(partialState: PartialStateReference) { await Promise.all([ this.validateSimulatedTree( diff --git a/yarn-project/sequencer-client/src/sequencer/utils.ts b/yarn-project/sequencer-client/src/sequencer/utils.ts index eebff558fea8..7214dedacff9 100644 --- a/yarn-project/sequencer-client/src/sequencer/utils.ts +++ b/yarn-project/sequencer-client/src/sequencer/utils.ts @@ -6,6 +6,7 @@ import { MerkleTreeOperations } from '@aztec/world-state'; * * TODO(#4148) Proper genesis state. If the state is empty, we allow anything for now. */ +// TODO: replaced this with the function implemented on MerkleTrees class export async function buildInitialHeader(db: MerkleTreeOperations) { const state = await db.getStateReference(); return new Header(AppendOnlyTreeSnapshot.empty(), Buffer.alloc(32, 0), state, GlobalVariables.empty()); diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts index 3d4d5db15092..f0220ace6061 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts @@ -127,11 +127,6 @@ export interface MerkleTreeOperations { */ updateLatestGlobalVariablesHash(globalVariablesHash: Fr): Promise; - /** - * Gets the global variables hash from the previous block - */ - getLatestGlobalVariablesHash(): Promise; - /** * Batch insert multiple leaves into the tree. * @param leaves - Leaves to insert into the tree. diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts index 2669172c8133..66fd092c37bf 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts @@ -142,13 +142,6 @@ export class MerkleTreeOperationsFacade implements MerkleTreeOperations { return this.trees.updateLatestGlobalVariablesHash(globalVariablesHash, this.includeUncommitted); } - /** - * Gets the global variables hash from the previous block - */ - public getLatestGlobalVariablesHash(): Promise { - return this.trees.getLatestGlobalVariablesHash(this.includeUncommitted); - } - /** * Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree). * @param block - The L2 block to handle. diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts index a0f94f8a14d0..7394b3796913 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts @@ -34,10 +34,6 @@ export class MerkleTreeSnapshotOperationsFacade implements MerkleTreeOperations return tree.findLeafIndex(value); } - getLatestGlobalVariablesHash(): Promise { - return Promise.reject(new Error('not implemented')); - } - async getLeafPreimage( treeId: MerkleTreeId.NULLIFIER_TREE, index: bigint, 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 35177e00a743..c2bf1f58c17e 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 @@ -211,14 +211,6 @@ export class MerkleTrees implements MerkleTreeDb { return await this.synchronize(() => this._updateLatestGlobalVariablesHash(globalVariablesHash)); } - /** - * Gets the global variables hash from the previous block - * @param includeUncommitted - Indicates whether to include uncommitted data. - */ - public async getLatestGlobalVariablesHash(includeUncommitted: boolean): Promise { - return await this.synchronize(() => this._getGlobalVariablesHash(includeUncommitted)); - } - /** * Gets the tree info for the specified tree. * @param treeId - Id of the tree to get information from. @@ -255,19 +247,6 @@ export class MerkleTrees implements MerkleTreeDb { return Promise.resolve(state); } - // TODO(#3941) - private async _getCurrentBlockHash(globalsHash: Fr, includeUncommitted: boolean): Promise { - const state = await this.getStateReference(includeUncommitted); - return computeBlockHash( - globalsHash, - state.partial.noteHashTree.root, - state.partial.nullifierTree.root, - state.partial.contractTree.root, - state.l1ToL2MessageTree.root, - state.partial.publicDataTree.root, - ); - } - /** * Gets the value at the given index. * @param treeId - The ID of the tree to get the leaf value from. @@ -604,12 +583,11 @@ export class MerkleTrees implements MerkleTreeDb { ); } - // Sync and add the block to the blocks tree - const globalVariablesHash = computeGlobalsHash(l2Block.header.globalVariables); + // Sync and add header hash to archive tree await this._updateLatestGlobalVariablesHash(globalVariablesHash); - this.log(`Synced global variables with hash ${globalVariablesHash}`); + this.log(`Synced global variables ${l2Block.header.globalVariables.toJSON()}`); - const blockHash = await this._getCurrentBlockHash(globalVariablesHash, true); + const blockHash = l2Block.header.hash(); await this._appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); await this._commit(); From 0e88c219f86820afd91df02a884b27ac7289afe6 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 30 Jan 2024 16:54:06 +0000 Subject: [PATCH 03/25] nuked global vars hash from merkletrees --- .../src/block_builder/solo_block_builder.ts | 4 +- .../world-state-db/merkle_tree_operations.ts | 14 +-- .../merkle_tree_operations_facade.ts | 17 +--- .../merkle_tree_snapshot_operations_facade.ts | 4 - .../src/world-state-db/merkle_trees.ts | 97 +++++++------------ 5 files changed, 44 insertions(+), 92 deletions(-) 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 9ddff6a8b521..4ccf982b6b73 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 @@ -42,9 +42,7 @@ import { VK_TREE_HEIGHT, VerificationKey, } from '@aztec/circuits.js'; -import { - computeContractLeaf, -} from '@aztec/circuits.js/abis'; +import { computeContractLeaf } from '@aztec/circuits.js/abis'; import { makeTuple } from '@aztec/foundation/array'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { padArrayEnd } from '@aztec/foundation/collection'; diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts index f0220ace6061..68f73d50f88f 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts @@ -1,5 +1,5 @@ import { L2Block, MerkleTreeId } from '@aztec/circuit-types'; -import { NullifierLeafPreimage, StateReference } from '@aztec/circuits.js'; +import { Header, NullifierLeafPreimage, StateReference } from '@aztec/circuits.js'; import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; @@ -115,17 +115,11 @@ export interface MerkleTreeOperations { getLeafValue(treeId: MerkleTreeId, index: bigint): Promise; /** - * Inserts the new block hash into the archive. + * Inserts the 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. + * @param header - The header to insert into the archive. */ - updateArchive(globalVariablesHash: Fr): Promise; - - /** - * Updates the latest global variables hash - * @param globalVariablesHash - The latest global variables hash - */ - updateLatestGlobalVariablesHash(globalVariablesHash: Fr): Promise; + updateArchive(header: Header): Promise; /** * Batch insert multiple leaves into the tree. diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts index 66fd092c37bf..3e0f865e4579 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts @@ -1,6 +1,5 @@ import { L2Block, MerkleTreeId } from '@aztec/circuit-types'; -import { NullifierLeafPreimage, StateReference } from '@aztec/circuits.js'; -import { Fr } from '@aztec/foundation/fields'; +import { Header, NullifierLeafPreimage, StateReference } from '@aztec/circuits.js'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; import { BatchInsertionResult } from '@aztec/merkle-tree'; import { SiblingPath } from '@aztec/types/membership'; @@ -128,18 +127,10 @@ export class MerkleTreeOperationsFacade implements MerkleTreeOperations { /** * 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. + * @param header - The header to insert into the archive. */ - public updateArchive(globalVariablesHash: Fr): Promise { - return this.trees.updateArchive(globalVariablesHash, this.includeUncommitted); - } - - /** - * Updates the latest global variables hash - * @param globalVariablesHash - The latest global variables hash - */ - public updateLatestGlobalVariablesHash(globalVariablesHash: Fr): Promise { - return this.trees.updateLatestGlobalVariablesHash(globalVariablesHash, this.includeUncommitted); + public updateArchive(header: Header): Promise { + return this.trees.updateArchive(header, this.includeUncommitted); } /** diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts index 7394b3796913..1e6676f5754e 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts @@ -148,10 +148,6 @@ export class MerkleTreeSnapshotOperationsFacade implements MerkleTreeOperations return Promise.reject(new Error('Tree snapshot operations are read-only')); } - updateLatestGlobalVariablesHash(): Promise { - return Promise.reject(new Error('Tree snapshot operations are read-only')); - } - updateLeaf(): Promise { return Promise.reject(new Error('Tree snapshot operations are read-only')); } 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 c2bf1f58c17e..b4a64ad3f5c7 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 { CONTRACT_TREE_HEIGHT, Fr, GlobalVariables, + Header, L1_TO_L2_MSG_TREE_HEIGHT, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, NOTE_HASH_TREE_HEIGHT, @@ -19,7 +20,6 @@ import { PublicDataTreeLeafPreimage, StateReference, } from '@aztec/circuits.js'; -import { Committable } from '@aztec/foundation/committable'; import { SerialQueue } from '@aztec/foundation/fifo'; import { createDebugLogger } from '@aztec/foundation/log'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; @@ -42,18 +42,6 @@ import { INITIAL_NULLIFIER_TREE_SIZE, INITIAL_PUBLIC_DATA_TREE_SIZE, MerkleTreeD import { HandleL2BlockResult, IndexedTreeId, MerkleTreeOperations, TreeInfo } from './merkle_tree_operations.js'; import { MerkleTreeOperationsFacade } from './merkle_tree_operations_facade.js'; -/** - * Data necessary to reinitialize the merkle trees from Db. - */ -interface FromDbOptions { - /** - * The global variables hash from the last block. - */ - globalVariablesHash: Fr; -} - -const LAST_GLOBAL_VARS_HASH = 'lastGlobalVarsHash'; - /** * The nullifier tree is an indexed tree. */ @@ -72,27 +60,26 @@ class PublicDataTree extends StandardIndexedTree { } } +const FROM_DB_FLAG = 'FROM_DB'; + /** * A convenience class for managing multiple merkle trees. */ export class MerkleTrees implements MerkleTreeDb { private trees: (AppendOnlyTree | UpdateOnlyTree)[] = []; - private latestGlobalVariablesHash: Committable; private jobQueue = new SerialQueue(); - #globalVariablesHash: AztecSingleton; + fromDb: AztecSingleton; constructor(private store: AztecKVStore, private log = createDebugLogger('aztec:merkle_trees')) { - this.latestGlobalVariablesHash = new Committable(Fr.ZERO); - this.#globalVariablesHash = store.openSingleton(LAST_GLOBAL_VARS_HASH); + this.fromDb = store.openSingleton(FROM_DB_FLAG); } /** * initializes the collection of Merkle Trees. - * @param fromDbOptions - Options to initialize the trees from the database. + * @param fromDb - Whether to initialize the trees from the db. */ - public async init(fromDbOptions?: FromDbOptions) { - const fromDb = fromDbOptions !== undefined; + public async init(fromDb = false) { const initializeTree = fromDb ? loadTree : newTree; const hasher = new Pedersen(); @@ -144,17 +131,14 @@ export class MerkleTrees implements MerkleTreeDb { this.jobQueue.start(); - // The first leaf in the blocks tree contains the empty roots of the other trees and empty global variables. if (!fromDb) { - const initialGlobalVariablesHash = computeGlobalsHash(GlobalVariables.empty()); - await this._updateLatestGlobalVariablesHash(initialGlobalVariablesHash); - await this._updateArchive(initialGlobalVariablesHash, true); - await this._commit(); - } else { - await this._updateLatestGlobalVariablesHash(fromDbOptions.globalVariablesHash); - // make the restored global variables hash and tree roots current - await this._commit(); + // We are not initializing from db so we need to populate the first leaf of the archive tree which is a hash of + // the initial header. + const initialHeder = await this.buildInitialHeader(true); + await this.#updateArchive(initialHeder, true); } + + await this._commit(); } /** @@ -164,12 +148,17 @@ export class MerkleTrees implements MerkleTreeDb { */ public static async new(store: AztecKVStore) { const merkleTrees = new MerkleTrees(store); - const globalVariablesHash = store.openSingleton(LAST_GLOBAL_VARS_HASH); - const val = globalVariablesHash.get(); - await merkleTrees.init(val ? { globalVariablesHash: Fr.fromBuffer(val) } : undefined); + const fromDbFlag = store.openSingleton(FROM_DB_FLAG); + const val = fromDbFlag.get(); + await merkleTrees.init(val); return merkleTrees; } + public async buildInitialHeader(includeUncommitted: boolean): Promise
{ + const state = await this.getStateReference(includeUncommitted); + return new Header(AppendOnlyTreeSnapshot.empty(), Buffer.alloc(32, 0), state, GlobalVariables.empty()); + } + /** * Stops the job queue (waits for all jobs to finish). */ @@ -196,19 +185,11 @@ export class MerkleTrees implements MerkleTreeDb { /** * Inserts into the roots trees (CONTRACT_TREE_ROOTS_TREE, NOTE_HASH_TREE_ROOTS_TREE, L1_TO_L2_MESSAGE_TREE_ROOTS_TREE) * the current roots of the corresponding trees (CONTRACT_TREE, NOTE_HASH_TREE, L1_TO_L2_MESSAGE_TREE). - * @param globalsHash - The current global variables hash. + * @param header - The header to insert into the archive. * @param includeUncommitted - Indicates whether to include uncommitted data. */ - public async updateArchive(globalsHash: Fr, includeUncommitted: boolean) { - await this.synchronize(() => this._updateArchive(globalsHash, includeUncommitted)); - } - - /** - * Updates the latest global variables hash - * @param globalVariablesHash - The latest global variables hash - */ - public async updateLatestGlobalVariablesHash(globalVariablesHash: Fr) { - return await this.synchronize(() => this._updateLatestGlobalVariablesHash(globalVariablesHash)); + public async updateArchive(header: Header, includeUncommitted: boolean) { + await this.synchronize(() => this.#updateArchive(header, includeUncommitted)); } /** @@ -419,17 +400,15 @@ export class MerkleTrees implements MerkleTreeDb { return await this.jobQueue.put(fn); } - private _updateLatestGlobalVariablesHash(globalVariablesHash: Fr): Promise { - this.latestGlobalVariablesHash.set(globalVariablesHash); - return Promise.resolve(); - } - - private _getGlobalVariablesHash(includeUncommitted: boolean): Promise { - return Promise.resolve(this.latestGlobalVariablesHash.get(includeUncommitted)); - } + async #updateArchive(header: Header, includeUncommitted: boolean) { + const state = await this.getStateReference(includeUncommitted); + // TODO: StateReference.equals + // TODO: Is this even needed here? + if (state.toBuffer().equals(header.state.toBuffer())) { + throw new Error('State in header does not match current state'); + } - private async _updateArchive(globalsHash: Fr, includeUncommitted: boolean) { - const blockHash = await this._getCurrentBlockHash(globalsHash, includeUncommitted); + const blockHash = header.hash(); await this._appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); } @@ -503,8 +482,7 @@ export class MerkleTrees implements MerkleTreeDb { for (const tree of this.trees) { await tree.commit(); } - this.latestGlobalVariablesHash.commit(); - await this.#globalVariablesHash.set(this.latestGlobalVariablesHash.get().toBuffer()); + await this.fromDb.set(true); } /** @@ -515,7 +493,6 @@ export class MerkleTrees implements MerkleTreeDb { for (const tree of this.trees) { await tree.rollback(); } - this.latestGlobalVariablesHash.rollback(); } public getSnapshot(blockNumber: number) { @@ -583,12 +560,8 @@ export class MerkleTrees implements MerkleTreeDb { ); } - // Sync and add header hash to archive tree - await this._updateLatestGlobalVariablesHash(globalVariablesHash); - this.log(`Synced global variables ${l2Block.header.globalVariables.toJSON()}`); - - const blockHash = l2Block.header.hash(); - await this._appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); + // The last thing remaining is to update the archive + await this.#updateArchive(l2Block.header, true); await this._commit(); } From a985f504b37914e82a51e464ce57fb5a79662f63 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 30 Jan 2024 16:59:30 +0000 Subject: [PATCH 04/25] WIP --- yarn-project/aztec-node/src/aztec-node/server.ts | 8 ++------ .../src/block_builder/solo_block_builder.test.ts | 11 ++--------- .../src/block_builder/solo_block_builder.ts | 13 ++----------- 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index e770dbcf8402..deb436a90826 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -35,7 +35,7 @@ import { PUBLIC_DATA_TREE_HEIGHT, PublicDataTreeLeafPreimage, } from '@aztec/circuits.js'; -import { computeGlobalsHash, computePublicDataTreeLeafSlot } from '@aztec/circuits.js/abis'; +import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/abis'; import { L1ContractAddresses, createEthereumChain } from '@aztec/ethereum'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { createDebugLogger } from '@aztec/foundation/log'; @@ -549,16 +549,12 @@ export class AztecNodeService implements AztecNode { const blockNumber = (await this.blockSource.getBlockNumber()) + 1; const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(new Fr(blockNumber)); const prevHeader = (await this.blockSource.getBlock(-1))?.header; - const prevGlobalVariables = prevHeader?.globalVariables ?? GlobalVariables.empty(); // Instantiate merkle trees so uncommitted updates by this simulation are local to it. // TODO we should be able to remove this after https://github.com/AztecProtocol/aztec-packages/issues/1869 // So simulation of public functions doesn't affect the merkle trees. const merkleTrees = new MerkleTrees(this.merkleTreesDb, this.log); - const globalVariablesHash = computeGlobalsHash(prevGlobalVariables); - await merkleTrees.init({ - globalVariablesHash, - }); + await merkleTrees.init(true); // TODO(benesjan): : not sure if correct const publicProcessorFactory = new PublicProcessorFactory( merkleTrees.asLatest(), 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 3a005017e91c..76ff3c88e5eb 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 @@ -33,7 +33,7 @@ import { SideEffectLinkedToNoteHash, StateReference, } from '@aztec/circuits.js'; -import { computeBlockHashWithGlobals, computeContractLeaf } from '@aztec/circuits.js/abis'; +import { computeContractLeaf } from '@aztec/circuits.js/abis'; import { fr, makeBaseOrMergeRollupPublicInputs, @@ -157,14 +157,7 @@ describe('sequencer/solo_block_builder', () => { }; const updateArchive = async () => { - const blockHash = computeBlockHashWithGlobals( - globalVariables, - rootRollupOutput.header.state.partial.noteHashTree.root, - rootRollupOutput.header.state.partial.nullifierTree.root, - rootRollupOutput.header.state.partial.contractTree.root, - rootRollupOutput.header.state.l1ToL2MessageTree.root, - rootRollupOutput.header.state.partial.publicDataTree.root, - ); + const blockHash = rootRollupOutput.header.hash(); await expectsDb.appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); }; 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 4ccf982b6b73..503c1c98bf42 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 @@ -274,7 +274,7 @@ export class SoloBlockBuilder implements BlockBuilder { // Update the root trees with the latest data and contract tree roots, // and validate them against the output of the root circuit simulation this.debug(`Updating and validating root trees`); - await this.db.updateArchive(globalVariablesHash); + await this.db.updateArchive(rootOutput.header); await this.validateRootOutput(rootOutput); @@ -455,16 +455,7 @@ export class SoloBlockBuilder implements BlockBuilder { } protected getHistoricalTreesMembershipWitnessFor(tx: ProcessedTx) { - const header = tx.data.constants.historicalHeader; - // TODO(#3941) - const blockHash = computeBlockHash( - computeGlobalsHash(header.globalVariables), - header.state.partial.noteHashTree.root, - header.state.partial.nullifierTree.root, - header.state.partial.contractTree.root, - header.state.l1ToL2MessageTree.root, - header.state.partial.publicDataTree.root, - ); + const blockHash = tx.data.constants.historicalHeader.hash() return this.getMembershipWitnessFor(blockHash, MerkleTreeId.ARCHIVE, ARCHIVE_HEIGHT); } From 2e38eb77d1ab2f8e74e68b7c8e2ebfec1fceb44d Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 08:20:34 +0000 Subject: [PATCH 05/25] updated snaps --- .../src/abis/__snapshots__/abis.test.ts.snap | 86 ------------------- .../structs/__snapshots__/header.test.ts.snap | 44 ++++++++++ 2 files changed, 44 insertions(+), 86 deletions(-) create mode 100644 yarn-project/circuits.js/src/structs/__snapshots__/header.test.ts.snap diff --git a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap index 8491e4c1e370..a04642f359b3 100644 --- a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap +++ b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap @@ -10,49 +10,6 @@ exports[`abis Computes an empty public inputs hash 1`] = `"0x2e2b79cee62cb99e91 exports[`abis Computes an empty sideeffect hash 1`] = `"0x27b1d0839a5b23baf12a8d195b18ac288fcf401afb2f70b8a4b529ede5fa9fed"`; -exports[`abis compute globals hash 1`] = ` -Fr { - "asBigInt": 19996198784166720428914107076917074510032365849254400404611644441094528984289n, - "asBuffer": { - "data": [ - 44, - 53, - 114, - 139, - 52, - 188, - 28, - 197, - 40, - 157, - 117, - 130, - 246, - 163, - 68, - 239, - 11, - 227, - 228, - 184, - 250, - 189, - 231, - 11, - 41, - 32, - 78, - 168, - 174, - 60, - 100, - 225, - ], - "type": "Buffer", - }, -} -`; - exports[`abis compute private call stack item hash 1`] = ` Fr { "asBigInt": 12187345511405217717040217531423286257305914329376428594135414078733109256018n, @@ -280,49 +237,6 @@ exports[`abis computes a function selector 1`] = ` } `; -exports[`abis computes block hash with globals 1`] = ` -Fr { - "asBigInt": 7177915431153102916601456081755280584460785548870192083974540199919775120827n, - "asBuffer": { - "data": [ - 15, - 222, - 142, - 96, - 169, - 217, - 84, - 90, - 55, - 110, - 52, - 172, - 167, - 236, - 97, - 56, - 185, - 98, - 133, - 50, - 113, - 88, - 184, - 54, - 59, - 75, - 252, - 181, - 66, - 59, - 253, - 187, - ], - "type": "Buffer", - }, -} -`; - exports[`abis computes commitment nonce 1`] = ` Fr { "asBigInt": 7653394882992289714855533169019502055399179742531912347686813547951736946253n, diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/header.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/header.test.ts.snap new file mode 100644 index 000000000000..8a15a59ee847 --- /dev/null +++ b/yarn-project/circuits.js/src/structs/__snapshots__/header.test.ts.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Header computes hash 1`] = ` +Fr { + "asBigInt": 17991638921121681345555824161757346486368776085978982802127357651656088857262n, + "asBuffer": { + "data": [ + 39, + 198, + 232, + 33, + 120, + 194, + 121, + 42, + 23, + 66, + 111, + 251, + 166, + 131, + 251, + 128, + 16, + 46, + 122, + 209, + 193, + 24, + 177, + 67, + 172, + 91, + 198, + 153, + 236, + 93, + 170, + 174, + ], + "type": "Buffer", + }, +} +`; From 1519745fab67566dc1b3e2161cbf1048a66dee13 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 08:36:08 +0000 Subject: [PATCH 06/25] fix --- yarn-project/world-state/src/world-state-db/merkle_trees.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b4a64ad3f5c7..bdf3d70091d7 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 @@ -404,7 +404,7 @@ export class MerkleTrees implements MerkleTreeDb { const state = await this.getStateReference(includeUncommitted); // TODO: StateReference.equals // TODO: Is this even needed here? - if (state.toBuffer().equals(header.state.toBuffer())) { + if (!state.toBuffer().equals(header.state.toBuffer())) { throw new Error('State in header does not match current state'); } From 04dbef122e4ba6f5629e7907aa8621529ad64462 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 08:40:15 +0000 Subject: [PATCH 07/25] simplification --- .../src/block_builder/solo_block_builder.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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 503c1c98bf42..9730959f7421 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 @@ -454,11 +454,6 @@ export class SoloBlockBuilder implements BlockBuilder { return new MembershipWitness(height, index, assertLength(path.toFieldArray(), height)); } - protected getHistoricalTreesMembershipWitnessFor(tx: ProcessedTx) { - const blockHash = tx.data.constants.historicalHeader.hash() - return this.getMembershipWitnessFor(blockHash, MerkleTreeId.ARCHIVE, ARCHIVE_HEIGHT); - } - protected async getConstantRollupData(globalVariables: GlobalVariables): Promise { return ConstantRollupData.from({ baseRollupVkHash: DELETE_FR, @@ -688,6 +683,13 @@ export class SoloBlockBuilder implements BlockBuilder { publicDataSiblingPath, }); + const blockHash = tx.data.constants.historicalHeader.hash(); + const archiveRootMembershipWitness = await this.getMembershipWitnessFor( + blockHash, + MerkleTreeId.ARCHIVE, + ARCHIVE_HEIGHT, + ); + return BaseRollupInputs.from({ kernelData: this.getKernelDataFor(tx), start, @@ -700,7 +702,7 @@ export class SoloBlockBuilder implements BlockBuilder { publicDataReadsPreimages: txPublicDataReadsInfo.newPublicDataReadsPreimages, publicDataReadsMembershipWitnesses: txPublicDataReadsInfo.newPublicDataReadsWitnesses, - archiveRootMembershipWitness: await this.getHistoricalTreesMembershipWitnessFor(tx), + archiveRootMembershipWitness, constants, }); From 3fc00c4e325b9c241151d99c1abf489bc678aedf Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 08:49:43 +0000 Subject: [PATCH 08/25] disabling 1 broken test to see where CI progresses --- .../src/block_builder/solo_block_builder.test.ts | 2 +- .../sequencer-client/src/block_builder/solo_block_builder.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 76ff3c88e5eb..5754f5f3c810 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 @@ -255,7 +255,7 @@ describe('sequencer/solo_block_builder', () => { builder = new SoloBlockBuilder(builderDb, vks, simulator, prover); }); - it('builds an L2 block using mock simulator', async () => { + it.skip('builds an L2 block using mock simulator', async () => { // Assemble a fake transaction const txs = await buildMockSimulatorInputs(); 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 9730959f7421..76f3b9a1968e 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 @@ -702,7 +702,7 @@ export class SoloBlockBuilder implements BlockBuilder { publicDataReadsPreimages: txPublicDataReadsInfo.newPublicDataReadsPreimages, publicDataReadsMembershipWitnesses: txPublicDataReadsInfo.newPublicDataReadsWitnesses, - archiveRootMembershipWitness, + archiveRootMembershipWitness, // TODO(benesjan): This is a bad name. Rename it to kernelHeaderMembershipWitness constants, }); From 1a82a8be23bfbce0e90eca3d2b79984d86804005 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 09:26:20 +0000 Subject: [PATCH 09/25] fixed block builder test --- .../rollup/append_only_tree_snapshot.ts | 2 ++ .../block_builder/solo_block_builder.test.ts | 20 ++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts b/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts index 4d8914029c1a..9526cc012715 100644 --- a/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts +++ b/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts @@ -47,10 +47,12 @@ export class AppendOnlyTreeSnapshot { return AppendOnlyTreeSnapshot.fromBuffer(Buffer.from(str, STRING_ENCODING)); } + // TODO(benesjan): rename to 0 static empty() { return new AppendOnlyTreeSnapshot(Fr.ZERO, 0); } + // TODO(benesjan): rename to isZero isEmpty(): boolean { return this.root.isZero() && this.nextAvailableLeafIndex === 0; } 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 5754f5f3c810..8cb1b106708e 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 @@ -15,6 +15,7 @@ import { BaseOrMergeRollupPublicInputs, Fr, GlobalVariables, + Header, KernelCircuitPublicInputs, MAX_NEW_COMMITMENTS_PER_TX, MAX_NEW_L2_TO_L1_MSGS_PER_TX, @@ -210,12 +211,6 @@ describe('sequencer/solo_block_builder', () => { // Update l1 to l2 data tree // And update the root trees now to create proper output to the root rollup circuit await updateL1ToL2MessageTree(mockL1ToL2Messages); - rootRollupOutput.header.state = await getStateReference(); - - // Calculate block hash - rootRollupOutput.header.globalVariables = globalVariables; - await updateArchive(); - rootRollupOutput.archive = await getTreeSnapshot(MerkleTreeId.ARCHIVE); const newNullifiers = txs.flatMap(tx => tx.data.end.newNullifiers); const newCommitments = txs.flatMap(tx => tx.data.end.newCommitments); @@ -230,9 +225,11 @@ describe('sequencer/solo_block_builder', () => { const newEncryptedLogs = new L2BlockL2Logs(txs.map(tx => tx.encryptedLogs || new TxL2Logs([]))); const newUnencryptedLogs = new L2BlockL2Logs(txs.map(tx => tx.unencryptedLogs || new TxL2Logs([]))); + // We are constructing the block here just to get body hash/calldata hash so we can pass in an empty archive and header const l2Block = L2Block.fromFields({ - archive: rootRollupOutput.archive, - header: rootRollupOutput.header, + archive: AppendOnlyTreeSnapshot.empty(), + header: Header.empty(), + // Only the values bellow go to body hash/calldata hash newCommitments: newCommitments.map((sideEffect: SideEffect) => sideEffect.value), newNullifiers: newNullifiers.map((sideEffect: SideEffectLinkedToNoteHash) => sideEffect.value), newContracts, @@ -244,7 +241,12 @@ describe('sequencer/solo_block_builder', () => { newUnencryptedLogs, }); + rootRollupOutput.header.globalVariables = globalVariables; rootRollupOutput.header.bodyHash = l2Block.getCalldataHash(); + rootRollupOutput.header.state = await getStateReference(); + + await updateArchive(); + rootRollupOutput.archive = await getTreeSnapshot(MerkleTreeId.ARCHIVE); return txs; }; @@ -255,7 +257,7 @@ describe('sequencer/solo_block_builder', () => { builder = new SoloBlockBuilder(builderDb, vks, simulator, prover); }); - it.skip('builds an L2 block using mock simulator', async () => { + it('builds an L2 block using mock simulator', async () => { // Assemble a fake transaction const txs = await buildMockSimulatorInputs(); From 18dfa59c39a558586cee9f62a5684f7e2a610c48 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 09:26:42 +0000 Subject: [PATCH 10/25] fmt --- yarn-project/circuits.js/src/abis/abis.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/circuits.js/src/abis/abis.test.ts b/yarn-project/circuits.js/src/abis/abis.test.ts index 88a28a0dcf0a..99b718e52a93 100644 --- a/yarn-project/circuits.js/src/abis/abis.test.ts +++ b/yarn-project/circuits.js/src/abis/abis.test.ts @@ -41,7 +41,7 @@ import { hashTxRequest, hashVK, siloCommitment, - siloNullifier + siloNullifier, } from './abis.js'; describe('abis', () => { From 82ce4ab816aa40b610aedf8e3d214e639b5d7b4e Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 09:36:15 +0000 Subject: [PATCH 11/25] updated comments --- yarn-project/circuits.js/src/structs/header.ts | 1 + .../src/block_builder/solo_block_builder.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/yarn-project/circuits.js/src/structs/header.ts b/yarn-project/circuits.js/src/structs/header.ts index e770d2864301..7cae2fbebd21 100644 --- a/yarn-project/circuits.js/src/structs/header.ts +++ b/yarn-project/circuits.js/src/structs/header.ts @@ -109,6 +109,7 @@ export class Header { return Header.fromBuffer(buffer); } + // TODO(benesjan): cache this? would require making all properties readonly hash(): Fr { return Fr.fromBuffer( pedersenHash( 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 8cb1b106708e..8c863c9e49c8 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 @@ -208,8 +208,7 @@ describe('sequencer/solo_block_builder', () => { await updateExpectedTreesFromTxs([txs[1]]); baseRollupOutputRight.end = await getPartialStateReference(); - // Update l1 to l2 data tree - // And update the root trees now to create proper output to the root rollup circuit + // Update l1 to l2 message tree await updateL1ToL2MessageTree(mockL1ToL2Messages); const newNullifiers = txs.flatMap(tx => tx.data.end.newNullifiers); @@ -241,6 +240,7 @@ describe('sequencer/solo_block_builder', () => { newUnencryptedLogs, }); + // Now we update can make the final header, compute the block hash and update archive rootRollupOutput.header.globalVariables = globalVariables; rootRollupOutput.header.bodyHash = l2Block.getCalldataHash(); rootRollupOutput.header.state = await getStateReference(); From fccd917e2af57cdf32158cb7a6e306b33d5d74e5 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 09:49:47 +0000 Subject: [PATCH 12/25] update L2Block.hash --- .../archiver/src/archiver/archiver.ts | 6 +-- .../archiver/kv_archiver_store/block_store.ts | 2 +- yarn-project/circuit-types/src/l2_block.ts | 45 +++++++------------ yarn-project/circuit-types/src/l2_tx.ts | 8 ++-- .../world-state-db/merkle_tree_operations.ts | 1 - 5 files changed, 23 insertions(+), 39 deletions(-) diff --git a/yarn-project/archiver/src/archiver/archiver.ts b/yarn-project/archiver/src/archiver/archiver.ts index e350e2168236..4856a608bdff 100644 --- a/yarn-project/archiver/src/archiver/archiver.ts +++ b/yarn-project/archiver/src/archiver/archiver.ts @@ -297,11 +297,7 @@ export class Archiver implements ArchiveSource { retrievedBlocks.retrievedData.map(block => { // Ensure we pad the L1 to L2 message array to the full size before storing. block.newL1ToL2Messages = padArrayEnd(block.newL1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP); - return L2Block.fromFields( - omit(block, ['newEncryptedLogs', 'newUnencryptedLogs']), - block.getBlockHash(), - block.getL1BlockNumber(), - ); + return L2Block.fromFields(omit(block, ['newEncryptedLogs', 'newUnencryptedLogs']), block.getL1BlockNumber()); }), ); } diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts index db38f09866a0..e3ebb5fdf3d4 100644 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts +++ b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts @@ -46,7 +46,7 @@ export class BlockStore { blockNumber: block.number, block: block.toBuffer(), l1BlockNumber: block.getL1BlockNumber(), - blockHash: block.getBlockHash(), + blockHash: block.hash().toBuffer(), }); for (const [i, tx] of block.getTxs().entries()) { diff --git a/yarn-project/circuit-types/src/l2_block.ts b/yarn-project/circuit-types/src/l2_block.ts index ccc3f7bdcea3..181a1090a764 100644 --- a/yarn-project/circuit-types/src/l2_block.ts +++ b/yarn-project/circuit-types/src/l2_block.ts @@ -11,7 +11,7 @@ import { } from '@aztec/circuits.js'; import { makeAppendOnlyTreeSnapshot, makeHeader } from '@aztec/circuits.js/factories'; import { times } from '@aztec/foundation/collection'; -import { keccak, sha256 } from '@aztec/foundation/crypto'; +import { sha256 } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize'; @@ -91,7 +91,6 @@ export class L2Block { public newL1ToL2Messages: Fr[] = [], newEncryptedLogs?: L2BlockL2Logs, newUnencryptedLogs?: L2BlockL2Logs, - private blockHash?: Buffer, l1BlockNumber?: bigint, ) { if (newCommitments.length % MAX_NEW_COMMITMENTS_PER_TX !== 0) { @@ -173,7 +172,6 @@ export class L2Block { newEncryptedLogs, newUnencryptedLogs, }, - undefined, // just for testing purposes, each random L2 block got emitted in the equivalent L1 block BigInt(l2BlockNum), ); @@ -229,7 +227,6 @@ export class L2Block { */ newUnencryptedLogs?: L2BlockL2Logs; }, - blockHash?: Buffer, l1BlockNumber?: bigint, ) { return new this( @@ -244,7 +241,6 @@ export class L2Block { fields.newL1ToL2Messages, fields.newEncryptedLogs, fields.newUnencryptedLogs, - blockHash, l1BlockNumber, ); } @@ -332,7 +328,7 @@ export class L2Block { * @param blockHash - The hash of the block. * @returns Deserialized L2 block. */ - static fromBuffer(buf: Buffer | BufferReader, blockHash?: Buffer) { + static fromBuffer(buf: Buffer | BufferReader) { const reader = BufferReader.asReader(buf); const header = reader.readObject(Header); const archive = reader.readObject(AppendOnlyTreeSnapshot); @@ -345,20 +341,17 @@ export class L2Block { // TODO(sean): could an optimization of this be that it is encoded such that zeros are assumed const newL1ToL2Messages = reader.readVector(Fr); - return L2Block.fromFields( - { - archive, - header, - newCommitments, - newNullifiers, - newPublicDataWrites, - newL2ToL1Msgs, - newContracts, - newContractData, - newL1ToL2Messages, - }, - blockHash, - ); + return L2Block.fromFields({ + archive, + header, + newCommitments, + newNullifiers, + newPublicDataWrites, + newL2ToL1Msgs, + newContracts, + newContractData, + newL1ToL2Messages, + }); } /** @@ -441,15 +434,11 @@ export class L2Block { } /** - * Returns the block's hash. + * Returns the block's hash (hash of block header). * @returns The block's hash. - * TODO: rename this as hash and make it return this.header.hash() */ - public getBlockHash(): Buffer { - if (!this.blockHash) { - this.blockHash = keccak(this.toBufferWithLogs()); - } - return this.blockHash; + public hash(): Fr { + return this.header.hash(); } /** @@ -640,7 +629,7 @@ export class L2Block { newL2ToL1Msgs, newContracts, newContractData, - this.getBlockHash(), + this.hash(), Number(this.header.globalVariables.blockNumber.toBigInt()), ); } diff --git a/yarn-project/circuit-types/src/l2_tx.ts b/yarn-project/circuit-types/src/l2_tx.ts index 65588476418d..593f85d7b816 100644 --- a/yarn-project/circuit-types/src/l2_tx.ts +++ b/yarn-project/circuit-types/src/l2_tx.ts @@ -57,7 +57,7 @@ export class L2Tx { /** * The unique identifier of the block containing the transaction. */ - public blockHash: Buffer, + public blockHash: Fr, /** * The block number in which the transaction was included. */ @@ -80,7 +80,7 @@ export class L2Tx { reader.readVector(Fr), reader.readVector(Fr), reader.readVector(ContractData), - reader.readBytes(Fr.SIZE_IN_BYTES), + Fr.fromBuffer(reader), reader.readNumber(), ); } @@ -106,7 +106,7 @@ export class L2Tx { new Vector(this.newL2ToL1Msgs).toBuffer(), new Vector(this.newContracts).toBuffer(), new Vector(this.newContractData).toBuffer(), - this.blockHash, + this.blockHash.toBuffer(), numToUInt32BE(this.blockNumber), ]); } @@ -127,7 +127,7 @@ export class L2Tx { times(rand(0, MAX_NEW_L2_TO_L1_MSGS_PER_TX), Fr.random), times(rand(0, MAX_NEW_CONTRACTS_PER_TX), Fr.random), times(rand(0, MAX_NEW_CONTRACTS_PER_TX), ContractData.random), - Fr.random().toBuffer(), + Fr.random(), 123, ); } diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts index 68f73d50f88f..b960e7d22f13 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts @@ -1,6 +1,5 @@ import { L2Block, MerkleTreeId } from '@aztec/circuit-types'; import { Header, NullifierLeafPreimage, StateReference } from '@aztec/circuits.js'; -import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; import { BatchInsertionResult } from '@aztec/merkle-tree'; From ab4140fb4ba29b36da22a625d844bffbb08a607c Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 10:01:31 +0000 Subject: [PATCH 13/25] WIP --- .../archiver/src/archiver/kv_archiver_store/block_store.ts | 6 ++---- yarn-project/pxe/src/pxe_service/pxe_service.ts | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts index e3ebb5fdf3d4..c4722bf7df31 100644 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts +++ b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts @@ -77,7 +77,7 @@ export class BlockStore { */ *getBlocks(start: number, limit: number): IterableIterator { for (const blockCtx of this.#blocks.values(this.#computeBlockRange(start, limit))) { - yield L2Block.fromBuffer(blockCtx.block, blockCtx.blockHash); + yield L2Block.fromBuffer(blockCtx.block); } } @@ -92,9 +92,7 @@ export class BlockStore { return undefined; } - const block = L2Block.fromBuffer(blockCtx.block, blockCtx.blockHash); - - return block; + return L2Block.fromBuffer(blockCtx.block); } /** diff --git a/yarn-project/pxe/src/pxe_service/pxe_service.ts b/yarn-project/pxe/src/pxe_service/pxe_service.ts index faab1796d97f..7cdcccef7152 100644 --- a/yarn-project/pxe/src/pxe_service/pxe_service.ts +++ b/yarn-project/pxe/src/pxe_service/pxe_service.ts @@ -445,7 +445,7 @@ export class PXEService implements PXE { txHash, TxStatus.MINED, '', - settledTx.blockHash, + settledTx.blockHash.toBuffer(), settledTx.blockNumber, deployedContractAddress, ); From d8d2b10b4ed5ad5b7188d98f9b6f1a65d82b74aa Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 10:40:47 +0000 Subject: [PATCH 14/25] nuked block hash from block store --- .../archiver/src/archiver/kv_archiver_store/block_store.ts | 2 -- yarn-project/world-state/src/world-state-db/merkle_trees.ts | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts index c4722bf7df31..ef637162c4e0 100644 --- a/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts +++ b/yarn-project/archiver/src/archiver/kv_archiver_store/block_store.ts @@ -9,7 +9,6 @@ type BlockContext = { blockNumber: number; l1BlockNumber: bigint; block: Buffer; - blockHash: Buffer; }; /** @@ -46,7 +45,6 @@ export class BlockStore { blockNumber: block.number, block: block.toBuffer(), l1BlockNumber: block.getL1BlockNumber(), - blockHash: block.hash().toBuffer(), }); for (const [i, tx] of block.getTxs().entries()) { 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 bdf3d70091d7..f2b04f2abbf7 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 @@ -402,8 +402,7 @@ export class MerkleTrees implements MerkleTreeDb { async #updateArchive(header: Header, includeUncommitted: boolean) { const state = await this.getStateReference(includeUncommitted); - // TODO: StateReference.equals - // TODO: Is this even needed here? + // TODO(benesjan): Is this even needed here? if (!state.toBuffer().equals(header.state.toBuffer())) { throw new Error('State in header does not match current state'); } From b53ab9a848c8552eab36a4831e7711843182418f Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 10:59:41 +0000 Subject: [PATCH 15/25] simplified MerkleTrees --- .../aztec-node/src/aztec-node/server.ts | 3 +- yarn-project/merkle-tree/src/index.ts | 2 +- .../src/world-state-db/merkle_trees.ts | 33 ++++++++++--------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index deb436a90826..0b99e4c8bf5f 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -26,7 +26,6 @@ import { ARCHIVE_HEIGHT, CONTRACT_TREE_HEIGHT, Fr, - GlobalVariables, Header, L1_TO_L2_MSG_TREE_HEIGHT, NOTE_HASH_TREE_HEIGHT, @@ -554,7 +553,7 @@ export class AztecNodeService implements AztecNode { // TODO we should be able to remove this after https://github.com/AztecProtocol/aztec-packages/issues/1869 // So simulation of public functions doesn't affect the merkle trees. const merkleTrees = new MerkleTrees(this.merkleTreesDb, this.log); - await merkleTrees.init(true); // TODO(benesjan): : not sure if correct + await merkleTrees.init(); const publicProcessorFactory = new PublicProcessorFactory( merkleTrees.asLatest(), diff --git a/yarn-project/merkle-tree/src/index.ts b/yarn-project/merkle-tree/src/index.ts index 68826f44e42c..45ae1771bbf8 100644 --- a/yarn-project/merkle-tree/src/index.ts +++ b/yarn-project/merkle-tree/src/index.ts @@ -6,7 +6,7 @@ export * from './pedersen.js'; export * from './sparse_tree/sparse_tree.js'; export { StandardIndexedTree } from './standard_indexed_tree/standard_indexed_tree.js'; export * from './standard_tree/standard_tree.js'; -export { INITIAL_LEAF } from './tree_base.js'; +export { INITIAL_LEAF, getTreeMeta } from './tree_base.js'; export { newTree } from './new_tree.js'; export { loadTree } from './load_tree.js'; export * from './snapshots/snapshot_builder.js'; 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 f2b04f2abbf7..f72c249a53a7 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 @@ -23,7 +23,7 @@ import { import { SerialQueue } from '@aztec/foundation/fifo'; import { createDebugLogger } from '@aztec/foundation/log'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; -import { AztecKVStore, AztecSingleton } from '@aztec/kv-store'; +import { AztecKVStore } from '@aztec/kv-store'; import { AppendOnlyTree, BatchInsertionResult, @@ -32,6 +32,7 @@ import { StandardIndexedTree, StandardTree, UpdateOnlyTree, + getTreeMeta, loadTree, newTree, } from '@aztec/merkle-tree'; @@ -60,8 +61,6 @@ class PublicDataTree extends StandardIndexedTree { } } -const FROM_DB_FLAG = 'FROM_DB'; - /** * A convenience class for managing multiple merkle trees. */ @@ -69,17 +68,13 @@ export class MerkleTrees implements MerkleTreeDb { private trees: (AppendOnlyTree | UpdateOnlyTree)[] = []; private jobQueue = new SerialQueue(); - fromDb: AztecSingleton; - - constructor(private store: AztecKVStore, private log = createDebugLogger('aztec:merkle_trees')) { - this.fromDb = store.openSingleton(FROM_DB_FLAG); - } + constructor(private store: AztecKVStore, private log = createDebugLogger('aztec:merkle_trees')) {} /** - * initializes the collection of Merkle Trees. - * @param fromDb - Whether to initialize the trees from the db. + * Initializes the collection of Merkle Trees. */ - public async init(fromDb = false) { + public async init() { + const fromDb = this.#isDbPopulated(); const initializeTree = fromDb ? loadTree : newTree; const hasher = new Pedersen(); @@ -148,9 +143,7 @@ export class MerkleTrees implements MerkleTreeDb { */ public static async new(store: AztecKVStore) { const merkleTrees = new MerkleTrees(store); - const fromDbFlag = store.openSingleton(FROM_DB_FLAG); - const val = fromDbFlag.get(); - await merkleTrees.init(val); + await merkleTrees.init(); return merkleTrees; } @@ -481,7 +474,6 @@ export class MerkleTrees implements MerkleTreeDb { for (const tree of this.trees) { await tree.commit(); } - await this.fromDb.set(true); } /** @@ -583,4 +575,15 @@ export class MerkleTrees implements MerkleTreeDb { return { isBlockOurs: ourBlock }; } + + #isDbPopulated(): boolean { + try { + getTreeMeta(this.store, MerkleTreeId[MerkleTreeId.NULLIFIER_TREE]); + // Tree meta was found --> db is populated + return true; + } catch (e) { + // Tree meta was not found --> db is not populated + return false; + } + } } From 4b61229b20a138879d1af92742f74b7de380b118 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:03:38 +0000 Subject: [PATCH 16/25] simplifying how merkle trees are initialized --- yarn-project/aztec-node/src/aztec-node/server.ts | 3 +-- .../world-state/src/world-state-db/merkle_trees.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index 0b99e4c8bf5f..4fc825cb12bf 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -552,8 +552,7 @@ export class AztecNodeService implements AztecNode { // Instantiate merkle trees so uncommitted updates by this simulation are local to it. // TODO we should be able to remove this after https://github.com/AztecProtocol/aztec-packages/issues/1869 // So simulation of public functions doesn't affect the merkle trees. - const merkleTrees = new MerkleTrees(this.merkleTreesDb, this.log); - await merkleTrees.init(); + const merkleTrees = await MerkleTrees.new(this.merkleTreesDb, this.log); const publicProcessorFactory = new PublicProcessorFactory( merkleTrees.asLatest(), 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 f72c249a53a7..88536c34d202 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 @@ -21,7 +21,7 @@ import { StateReference, } from '@aztec/circuits.js'; import { SerialQueue } from '@aztec/foundation/fifo'; -import { createDebugLogger } from '@aztec/foundation/log'; +import { DebugLogger, createDebugLogger } from '@aztec/foundation/log'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; import { AztecKVStore } from '@aztec/kv-store'; import { @@ -68,12 +68,12 @@ export class MerkleTrees implements MerkleTreeDb { private trees: (AppendOnlyTree | UpdateOnlyTree)[] = []; private jobQueue = new SerialQueue(); - constructor(private store: AztecKVStore, private log = createDebugLogger('aztec:merkle_trees')) {} + private constructor(private store: AztecKVStore, private log: DebugLogger) {} /** * Initializes the collection of Merkle Trees. */ - public async init() { + async #init() { const fromDb = this.#isDbPopulated(); const initializeTree = fromDb ? loadTree : newTree; @@ -141,9 +141,9 @@ export class MerkleTrees implements MerkleTreeDb { * @param store - The db instance to use for data persistance. * @returns - A fully initialized MerkleTrees instance. */ - public static async new(store: AztecKVStore) { - const merkleTrees = new MerkleTrees(store); - await merkleTrees.init(); + public static async new(store: AztecKVStore, log = createDebugLogger('aztec:merkle_trees')) { + const merkleTrees = new MerkleTrees(store, log); + await merkleTrees.#init(); return merkleTrees; } From 155d8cbd808b20a57b6eb440f234548c6e2b7e8a Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:21:12 +0000 Subject: [PATCH 17/25] cleanup --- yarn-project/aztec-node/src/aztec-node/server.ts | 3 +-- yarn-project/circuits.js/src/abis/abis.ts | 1 - .../end-to-end/src/integration_l1_publisher.test.ts | 1 - .../src/block_builder/solo_block_builder.test.ts | 7 +++---- .../sequencer-client/src/sequencer/index.ts | 1 - .../src/sequencer/public_processor.ts | 3 +-- .../sequencer-client/src/sequencer/utils.ts | 13 ------------- .../src/world-state-db/merkle_tree_operations.ts | 5 +++++ .../world-state-db/merkle_tree_operations_facade.ts | 8 ++++++++ .../merkle_tree_snapshot_operations_facade.ts | 6 +++++- 10 files changed, 23 insertions(+), 25 deletions(-) delete mode 100644 yarn-project/sequencer-client/src/sequencer/utils.ts diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index 4fc825cb12bf..2ccd40baa009 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -44,7 +44,6 @@ import { GlobalVariableBuilder, PublicProcessorFactory, SequencerClient, - buildInitialHeader, getGlobalVariableBuilder, } from '@aztec/sequencer-client'; import { SiblingPath } from '@aztec/types/membership'; @@ -536,7 +535,7 @@ export class AztecNodeService implements AztecNode { // No block was not found so we build the initial header. const committedDb = await this.#getWorldState('latest'); - return await buildInitialHeader(committedDb); + return await committedDb.buildInitialHeader(); } /** diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index 791e12e77465..c6650f41e42d 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -21,7 +21,6 @@ import { ContractStorageUpdateRequest, FunctionData, FunctionLeafPreimage, - GlobalVariables, NewContractData, PrivateCallStackItem, PrivateCircuitPublicInputs, diff --git a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts index 4b6c4da2429e..a5a081b72d51 100644 --- a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts +++ b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts @@ -36,7 +36,6 @@ import { L1Publisher, RealRollupCircuitSimulator, SoloBlockBuilder, - buildInitialHeader, getL1Publisher, getVerificationKeys, makeEmptyProcessedTx as makeEmptyProcessedTxFromHistoricalTreeRoots, 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 8c863c9e49c8..ec3747352b94 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 @@ -64,7 +64,6 @@ import { makeEmptyProcessedTx as makeEmptyProcessedTxFromHistoricalTreeRoots, makeProcessedTx, } from '../sequencer/processed_tx.js'; -import { buildInitialHeader } from '../sequencer/utils.js'; import { RollupSimulator } from '../simulator/index.js'; import { RealRollupCircuitSimulator } from '../simulator/rollup.js'; import { SoloBlockBuilder } from './solo_block_builder.js'; @@ -123,7 +122,7 @@ describe('sequencer/solo_block_builder', () => { }, 20_000); const makeEmptyProcessedTx = async () => { - const header = await buildInitialHeader(builderDb); + const header = await builderDb.buildInitialHeader(); return makeEmptyProcessedTxFromHistoricalTreeRoots(header, chainId, version); }; @@ -185,7 +184,7 @@ describe('sequencer/solo_block_builder', () => { const buildMockSimulatorInputs = async () => { const kernelOutput = makePrivateKernelPublicInputsFinal(); - kernelOutput.constants.historicalHeader = await buildInitialHeader(expectsDb); + kernelOutput.constants.historicalHeader = await expectsDb.buildInitialHeader(); const tx = await makeProcessedTx( new Tx( @@ -292,7 +291,7 @@ describe('sequencer/solo_block_builder', () => { const makeBloatedProcessedTx = async (seed = 0x1) => { const tx = mockTx(seed); const kernelOutput = KernelCircuitPublicInputs.empty(); - kernelOutput.constants.historicalHeader = await buildInitialHeader(builderDb); + kernelOutput.constants.historicalHeader = await builderDb.buildInitialHeader(); kernelOutput.end.publicDataUpdateRequests = makeTuple( MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, i => new PublicDataUpdateRequest(fr(i), fr(0), fr(i + 10)), diff --git a/yarn-project/sequencer-client/src/sequencer/index.ts b/yarn-project/sequencer-client/src/sequencer/index.ts index 43a83d121a16..4e1a69cbc9b2 100644 --- a/yarn-project/sequencer-client/src/sequencer/index.ts +++ b/yarn-project/sequencer-client/src/sequencer/index.ts @@ -1,3 +1,2 @@ export * from './sequencer.js'; export * from './config.js'; -export * from './utils.js'; diff --git a/yarn-project/sequencer-client/src/sequencer/public_processor.ts b/yarn-project/sequencer-client/src/sequencer/public_processor.ts index 51e1ad011d82..ebc2c87aa938 100644 --- a/yarn-project/sequencer-client/src/sequencer/public_processor.ts +++ b/yarn-project/sequencer-client/src/sequencer/public_processor.ts @@ -56,7 +56,6 @@ import { PublicKernelCircuitSimulator } from '../simulator/index.js'; import { ContractsDataSourcePublicDB, WorldStateDB, WorldStatePublicDB } from '../simulator/public_executor.js'; import { RealPublicKernelCircuitSimulator } from '../simulator/public_kernel.js'; import { FailedTx, ProcessedTx, makeEmptyProcessedTx, makeProcessedTx } from './processed_tx.js'; -import { buildInitialHeader } from './utils.js'; /** * Creates new instances of PublicProcessor given the provided merkle tree db and contract data source. @@ -79,7 +78,7 @@ export class PublicProcessorFactory { historicalHeader: Header | undefined, globalVariables: GlobalVariables, ): Promise { - historicalHeader = historicalHeader ?? (await buildInitialHeader(this.merkleTree)); + historicalHeader = historicalHeader ?? (await this.merkleTree.buildInitialHeader()); const publicContractsDB = new ContractsDataSourcePublicDB(this.contractDataSource); const worldStatePublicDB = new WorldStatePublicDB(this.merkleTree); diff --git a/yarn-project/sequencer-client/src/sequencer/utils.ts b/yarn-project/sequencer-client/src/sequencer/utils.ts deleted file mode 100644 index 7214dedacff9..000000000000 --- a/yarn-project/sequencer-client/src/sequencer/utils.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { AppendOnlyTreeSnapshot, GlobalVariables, Header } from '@aztec/circuits.js'; -import { MerkleTreeOperations } from '@aztec/world-state'; - -/** - * Builds the initial header by reading the roots from the database. - * - * TODO(#4148) Proper genesis state. If the state is empty, we allow anything for now. - */ -// TODO: replaced this with the function implemented on MerkleTrees class -export async function buildInitialHeader(db: MerkleTreeOperations) { - const state = await db.getStateReference(); - return new Header(AppendOnlyTreeSnapshot.empty(), Buffer.alloc(32, 0), state, GlobalVariables.empty()); -} diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts index b960e7d22f13..8a59832253af 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations.ts @@ -55,6 +55,11 @@ export interface MerkleTreeOperations { */ getStateReference(): Promise; + /** + * Builds the initial header. + */ + buildInitialHeader(): Promise
; + /** * Gets sibling path for a leaf. * @param treeId - The tree to be queried for a sibling path. diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts index 3e0f865e4579..f36513f6e784 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_operations_facade.ts @@ -31,6 +31,14 @@ export class MerkleTreeOperationsFacade implements MerkleTreeOperations { return this.trees.getStateReference(this.includeUncommitted); } + /** + * Builds the initial header. + * @returns The initial header. + */ + buildInitialHeader(): Promise
{ + return this.trees.buildInitialHeader(this.includeUncommitted); + } + /** * Appends a set of leaf values to the tree. * @param treeId - Id of the tree to append leaves to. diff --git a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts index 1e6676f5754e..79e7a01d16fa 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_tree_snapshot_operations_facade.ts @@ -1,5 +1,5 @@ import { MerkleTreeId } from '@aztec/circuit-types'; -import { AppendOnlyTreeSnapshot, Fr, PartialStateReference, StateReference } from '@aztec/circuits.js'; +import { AppendOnlyTreeSnapshot, Fr, Header, PartialStateReference, StateReference } from '@aztec/circuits.js'; import { IndexedTreeLeafPreimage } from '@aztec/foundation/trees'; import { BatchInsertionResult, IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree'; import { SiblingPath } from '@aztec/types/membership'; @@ -151,4 +151,8 @@ export class MerkleTreeSnapshotOperationsFacade implements MerkleTreeOperations updateLeaf(): Promise { return Promise.reject(new Error('Tree snapshot operations are read-only')); } + + buildInitialHeader(): Promise
{ + throw new Error('Building initial header not supported on snapshot.'); + } } From 7e4ebcd8ab33f72c1caab2d18eb1cae391b8169f Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:24:43 +0000 Subject: [PATCH 18/25] unified naming of zero/empty method between noir/TS --- yarn-project/circuit-types/src/l2_block.ts | 20 +++++++++---------- .../circuits.js/src/structs/header.ts | 4 ++-- .../src/structs/partial_state_reference.ts | 16 +++++++-------- .../rollup/append_only_tree_snapshot.ts | 6 ++---- .../src/structs/state_reference.ts | 4 ++-- .../block_builder/solo_block_builder.test.ts | 2 +- .../src/block_builder/solo_block_builder.ts | 10 +++++----- .../src/world-state-db/merkle_trees.ts | 2 +- 8 files changed, 31 insertions(+), 33 deletions(-) diff --git a/yarn-project/circuit-types/src/l2_block.ts b/yarn-project/circuit-types/src/l2_block.ts index 181a1090a764..5dd80826f924 100644 --- a/yarn-project/circuit-types/src/l2_block.ts +++ b/yarn-project/circuit-types/src/l2_block.ts @@ -450,11 +450,11 @@ export class L2Block { const buf = serializeToBuffer( this.header.globalVariables, // TODO(#3868) - AppendOnlyTreeSnapshot.empty(), // this.startNoteHashTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startNullifierTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startContractTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startPublicDataTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startL1ToL2MessageTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startNoteHashTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startNullifierTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startContractTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startPublicDataTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startL1ToL2MessageTreeSnapshot, this.header.lastArchive, this.header.state.partial.noteHashTree, this.header.state.partial.nullifierTree, @@ -477,11 +477,11 @@ export class L2Block { const inputValue = serializeToBuffer( new Fr(Number(this.header.globalVariables.blockNumber.toBigInt()) - 1), // TODO(#3868) - AppendOnlyTreeSnapshot.empty(), // this.startNoteHashTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startNullifierTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startContractTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startPublicDataTreeSnapshot, - AppendOnlyTreeSnapshot.empty(), // this.startL1ToL2MessageTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startNoteHashTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startNullifierTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startContractTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startPublicDataTreeSnapshot, + AppendOnlyTreeSnapshot.zero(), // this.startL1ToL2MessageTreeSnapshot, this.header.lastArchive, ); return sha256(inputValue); diff --git a/yarn-project/circuits.js/src/structs/header.ts b/yarn-project/circuits.js/src/structs/header.ts index 7cae2fbebd21..8b122fe928bf 100644 --- a/yarn-project/circuits.js/src/structs/header.ts +++ b/yarn-project/circuits.js/src/structs/header.ts @@ -80,7 +80,7 @@ export class Header { static empty(): Header { return new Header( - AppendOnlyTreeSnapshot.empty(), + AppendOnlyTreeSnapshot.zero(), Buffer.alloc(NUM_BYTES_PER_SHA256), StateReference.empty(), GlobalVariables.empty(), @@ -89,7 +89,7 @@ export class Header { isEmpty(): boolean { return ( - this.lastArchive.isEmpty() && + this.lastArchive.isZero() && this.bodyHash.equals(Buffer.alloc(NUM_BYTES_PER_SHA256)) && this.state.isEmpty() && this.globalVariables.isEmpty() diff --git a/yarn-project/circuits.js/src/structs/partial_state_reference.ts b/yarn-project/circuits.js/src/structs/partial_state_reference.ts index ed2ce8857921..7e375eeb12a8 100644 --- a/yarn-project/circuits.js/src/structs/partial_state_reference.ts +++ b/yarn-project/circuits.js/src/structs/partial_state_reference.ts @@ -29,10 +29,10 @@ export class PartialStateReference { static empty(): PartialStateReference { return new PartialStateReference( - AppendOnlyTreeSnapshot.empty(), - AppendOnlyTreeSnapshot.empty(), - AppendOnlyTreeSnapshot.empty(), - AppendOnlyTreeSnapshot.empty(), + AppendOnlyTreeSnapshot.zero(), + AppendOnlyTreeSnapshot.zero(), + AppendOnlyTreeSnapshot.zero(), + AppendOnlyTreeSnapshot.zero(), ); } @@ -51,10 +51,10 @@ export class PartialStateReference { isEmpty(): boolean { return ( - this.noteHashTree.isEmpty() && - this.nullifierTree.isEmpty() && - this.contractTree.isEmpty() && - this.publicDataTree.isEmpty() + this.noteHashTree.isZero() && + this.nullifierTree.isZero() && + this.contractTree.isZero() && + this.publicDataTree.isZero() ); } } diff --git a/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts b/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts index 9526cc012715..09216a8a0a17 100644 --- a/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts +++ b/yarn-project/circuits.js/src/structs/rollup/append_only_tree_snapshot.ts @@ -47,13 +47,11 @@ export class AppendOnlyTreeSnapshot { return AppendOnlyTreeSnapshot.fromBuffer(Buffer.from(str, STRING_ENCODING)); } - // TODO(benesjan): rename to 0 - static empty() { + static zero() { return new AppendOnlyTreeSnapshot(Fr.ZERO, 0); } - // TODO(benesjan): rename to isZero - isEmpty(): boolean { + isZero(): boolean { return this.root.isZero() && this.nextAvailableLeafIndex === 0; } } diff --git a/yarn-project/circuits.js/src/structs/state_reference.ts b/yarn-project/circuits.js/src/structs/state_reference.ts index 7079c4511f48..284d988e07fb 100644 --- a/yarn-project/circuits.js/src/structs/state_reference.ts +++ b/yarn-project/circuits.js/src/structs/state_reference.ts @@ -30,10 +30,10 @@ export class StateReference { } static empty(): StateReference { - return new StateReference(AppendOnlyTreeSnapshot.empty(), PartialStateReference.empty()); + return new StateReference(AppendOnlyTreeSnapshot.zero(), PartialStateReference.empty()); } isEmpty(): boolean { - return this.l1ToL2MessageTree.isEmpty() && this.partial.isEmpty(); + return this.l1ToL2MessageTree.isZero() && this.partial.isEmpty(); } } 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 ec3747352b94..1686dac939dc 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 @@ -225,7 +225,7 @@ describe('sequencer/solo_block_builder', () => { // We are constructing the block here just to get body hash/calldata hash so we can pass in an empty archive and header const l2Block = L2Block.fromFields({ - archive: AppendOnlyTreeSnapshot.empty(), + archive: AppendOnlyTreeSnapshot.zero(), header: Header.empty(), // Only the values bellow go to body hash/calldata hash newCommitments: newCommitments.map((sideEffect: SideEffect) => sideEffect.value), 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 76f3b9a1968e..5a098846b069 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 @@ -153,19 +153,19 @@ export class SoloBlockBuilder implements BlockBuilder { protected validateTxs(txs: ProcessedTx[]) { for (const tx of txs) { const txHeader = tx.data.constants.historicalHeader; - if (txHeader.state.l1ToL2MessageTree.isEmpty()) { + if (txHeader.state.l1ToL2MessageTree.isZero()) { throw new Error(`Empty L1 to L2 messages tree in tx: ${toFriendlyJSON(tx)}`); } - if (txHeader.state.partial.noteHashTree.isEmpty()) { + if (txHeader.state.partial.noteHashTree.isZero()) { throw new Error(`Empty note hash tree in tx: ${toFriendlyJSON(tx)}`); } - if (txHeader.state.partial.nullifierTree.isEmpty()) { + if (txHeader.state.partial.nullifierTree.isZero()) { throw new Error(`Empty nullifier tree in tx: ${toFriendlyJSON(tx)}`); } - if (txHeader.state.partial.contractTree.isEmpty()) { + if (txHeader.state.partial.contractTree.isZero()) { throw new Error(`Empty contract tree in tx: ${toFriendlyJSON(tx)}`); } - if (txHeader.state.partial.publicDataTree.isEmpty()) { + if (txHeader.state.partial.publicDataTree.isZero()) { throw new Error(`Empty public data tree in tx: ${toFriendlyJSON(tx)}`); } } 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 88536c34d202..c6405d1d2cda 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 @@ -149,7 +149,7 @@ export class MerkleTrees implements MerkleTreeDb { public async buildInitialHeader(includeUncommitted: boolean): Promise
{ const state = await this.getStateReference(includeUncommitted); - return new Header(AppendOnlyTreeSnapshot.empty(), Buffer.alloc(32, 0), state, GlobalVariables.empty()); + return new Header(AppendOnlyTreeSnapshot.zero(), Buffer.alloc(32, 0), state, GlobalVariables.empty()); } /** From 295d48eec731b86f7abb5c18cdecb92b5d5a4cbb Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:36:43 +0000 Subject: [PATCH 19/25] syntax sugar + fix --- .../src/integration_l1_publisher.test.ts | 2 +- .../src/block_builder/solo_block_builder.ts | 5 +- .../src/world-state-db/merkle_trees.ts | 65 +++++++------------ 3 files changed, 28 insertions(+), 44 deletions(-) diff --git a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts index a5a081b72d51..9ce0bf7768e1 100644 --- a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts +++ b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts @@ -149,7 +149,7 @@ describe('L1Publisher integration', () => { l1BlockPublishRetryIntervalMS: 100, }); - prevHeader = await buildInitialHeader(builderDb); + prevHeader = await builderDb.buildInitialHeader(); }, 100_000); const makeEmptyProcessedTx = async () => { 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 5a098846b069..488b71914564 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 @@ -271,8 +271,7 @@ export class SoloBlockBuilder implements BlockBuilder { const rootProof = await this.prover.getRootRollupProof(rootInput, rootOutput); - // Update the root trees with the latest data and contract tree roots, - // and validate them against the output of the root circuit simulation + // Update the archive with the latest block header this.debug(`Updating and validating root trees`); await this.db.updateArchive(rootOutput.header); @@ -702,7 +701,7 @@ export class SoloBlockBuilder implements BlockBuilder { publicDataReadsPreimages: txPublicDataReadsInfo.newPublicDataReadsPreimages, publicDataReadsMembershipWitnesses: txPublicDataReadsInfo.newPublicDataReadsWitnesses, - archiveRootMembershipWitness, // TODO(benesjan): This is a bad name. Rename it to kernelHeaderMembershipWitness + archiveRootMembershipWitness, constants, }); 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 c6405d1d2cda..e0a308812ea7 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 @@ -133,7 +133,7 @@ export class MerkleTrees implements MerkleTreeDb { await this.#updateArchive(initialHeder, true); } - await this._commit(); + await this.#commit(); } /** @@ -192,7 +192,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns The tree info for the specified tree. */ public async getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise { - return await this.synchronize(() => this._getTreeInfo(treeId, includeUncommitted)); + return await this.synchronize(() => this.#getTreeInfo(treeId, includeUncommitted)); } /** @@ -248,7 +248,7 @@ export class MerkleTrees implements MerkleTreeDb { index: bigint, includeUncommitted: boolean, ): Promise> { - return await this.synchronize(() => this._getSiblingPath(treeId, index, includeUncommitted)); + return await this.synchronize(() => this.trees[treeId].getSiblingPath(index, includeUncommitted)); } /** @@ -258,7 +258,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns Empty promise. */ public async appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise { - return await this.synchronize(() => this._appendLeaves(treeId, leaves)); + return await this.synchronize(() => this.#appendLeaves(treeId, leaves)); } /** @@ -266,7 +266,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns Empty promise. */ public async commit(): Promise { - return await this.synchronize(() => this._commit()); + return await this.synchronize(() => this.#commit()); } /** @@ -274,7 +274,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns Empty promise. */ public async rollback(): Promise { - return await this.synchronize(() => this._rollback()); + return await this.synchronize(() => this.#rollback()); } /** @@ -302,7 +302,7 @@ export class MerkleTrees implements MerkleTreeDb { | undefined > { return await this.synchronize(() => - Promise.resolve(this._getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted)), + Promise.resolve(this.#getIndexedTree(treeId).findIndexOfPreviousKey(value, includeUncommitted)), ); } @@ -319,7 +319,7 @@ export class MerkleTrees implements MerkleTreeDb { includeUncommitted: boolean, ): Promise { return await this.synchronize(() => - Promise.resolve(this._getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted)), + Promise.resolve(this.#getIndexedTree(treeId).getLatestLeafPreimageCopy(index, includeUncommitted)), ); } @@ -349,7 +349,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns Empty promise. */ public async updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise { - return await this.synchronize(() => this._updateLeaf(treeId, leaf, index)); + return await this.synchronize(() => this.#updateLeaf(treeId, leaf, index)); } /** @@ -358,7 +358,7 @@ export class MerkleTrees implements MerkleTreeDb { * @returns Whether the block handled was produced by this same node. */ public async handleL2Block(block: L2Block): Promise { - return await this.synchronize(() => this._handleL2Block(block)); + return await this.synchronize(() => this.#handleL2Block(block)); } /** @@ -401,7 +401,7 @@ export class MerkleTrees implements MerkleTreeDb { } const blockHash = header.hash(); - await this._appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); + await this.#appendLeaves(MerkleTreeId.ARCHIVE, [blockHash.toBuffer()]); } /** @@ -410,7 +410,7 @@ export class MerkleTrees implements MerkleTreeDb { * @param includeUncommitted - Indicates whether to include uncommitted data. * @returns The tree info for the specified tree. */ - private _getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise { + #getTreeInfo(treeId: MerkleTreeId, includeUncommitted: boolean): Promise { const treeInfo = { treeId, root: this.trees[treeId].getRoot(includeUncommitted), @@ -425,32 +425,17 @@ export class MerkleTrees implements MerkleTreeDb { * @param treeId - Id of the tree to get an instance of. * @returns The indexed tree for the specified tree id. */ - private _getIndexedTree(treeId: IndexedTreeId): IndexedTree { + #getIndexedTree(treeId: IndexedTreeId): IndexedTree { return this.trees[treeId] as IndexedTree; } - /** - * Returns the sibling path for a leaf in a tree. - * @param treeId - Id of the tree to get the sibling path from. - * @param index - Index of the leaf to get the sibling path for. - * @param includeUncommitted - Indicates whether to include uncommitted updates in the sibling path. - * @returns Promise containing the sibling path for the leaf. - */ - private _getSiblingPath( - treeId: MerkleTreeId, - index: bigint, - includeUncommitted: boolean, - ): Promise> { - return Promise.resolve(this.trees[treeId].getSiblingPath(index, includeUncommitted)); - } - /** * Appends leaves to a tree. * @param treeId - Id of the tree to append leaves to. * @param leaves - Leaves to append. * @returns Empty promise. */ - private async _appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise { + async #appendLeaves(treeId: MerkleTreeId, leaves: Buffer[]): Promise { const tree = this.trees[treeId]; if (!('appendLeaves' in tree)) { throw new Error('Tree does not support `appendLeaves` method'); @@ -458,7 +443,7 @@ export class MerkleTrees implements MerkleTreeDb { return await tree.appendLeaves(leaves); } - private async _updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise { + async #updateLeaf(treeId: IndexedTreeId, leaf: Buffer, index: bigint): Promise { const tree = this.trees[treeId]; if (!('updateLeaf' in tree)) { throw new Error('Tree does not support `updateLeaf` method'); @@ -470,7 +455,7 @@ export class MerkleTrees implements MerkleTreeDb { * Commits all pending updates. * @returns Empty promise. */ - private async _commit(): Promise { + async #commit(): Promise { for (const tree of this.trees) { await tree.commit(); } @@ -480,7 +465,7 @@ export class MerkleTrees implements MerkleTreeDb { * Rolls back all pending updates. * @returns Empty promise. */ - private async _rollback(): Promise { + async #rollback(): Promise { for (const tree of this.trees) { await tree.rollback(); } @@ -490,7 +475,7 @@ export class MerkleTrees implements MerkleTreeDb { return Promise.all(this.trees.map(tree => tree.getSnapshot(blockNumber))); } - private async _snapshot(blockNumber: number): Promise { + async #snapshot(blockNumber: number): Promise { for (const tree of this.trees) { await tree.snapshot(blockNumber); } @@ -500,7 +485,7 @@ export class MerkleTrees implements MerkleTreeDb { * Handles a single L2 block (i.e. Inserts the new commitments into the merkle tree). * @param l2Block - The L2 block to handle. */ - private async _handleL2Block(l2Block: L2Block): Promise { + async #handleL2Block(l2Block: L2Block): Promise { const treeRootWithIdPairs = [ [l2Block.header.state.partial.contractTree.root, MerkleTreeId.CONTRACT_TREE], [l2Block.header.state.partial.nullifierTree.root, MerkleTreeId.NULLIFIER_TREE], @@ -516,10 +501,10 @@ export class MerkleTrees implements MerkleTreeDb { const ourBlock = treeRootWithIdPairs.every(([root, id]) => compareRoot(root, id)); if (ourBlock) { this.log(`Block ${l2Block.number} is ours, committing world state`); - await this._commit(); + await this.#commit(); } else { this.log(`Block ${l2Block.number} is not ours, rolling back world state and committing state from chain`); - await this._rollback(); + await this.#rollback(); // Sync the append only trees for (const [tree, leaves] of [ @@ -527,7 +512,7 @@ export class MerkleTrees implements MerkleTreeDb { [MerkleTreeId.NOTE_HASH_TREE, l2Block.newCommitments], [MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l2Block.newL1ToL2Messages], ] as const) { - await this._appendLeaves( + await this.#appendLeaves( tree, leaves.map(fr => fr.toBuffer()), ); @@ -554,12 +539,12 @@ export class MerkleTrees implements MerkleTreeDb { // The last thing remaining is to update the archive await this.#updateArchive(l2Block.header, true); - await this._commit(); + await this.#commit(); } for (const [root, treeId] of treeRootWithIdPairs) { const treeName = MerkleTreeId[treeId]; - const info = await this._getTreeInfo(treeId, false); + const info = await this.#getTreeInfo(treeId, false); const syncedStr = '0x' + info.root.toString('hex'); const rootStr = root.toString(); // Sanity check that the rebuilt trees match the roots published by the L2 block @@ -571,7 +556,7 @@ export class MerkleTrees implements MerkleTreeDb { this.log(`Tree ${treeName} synched with size ${info.size} root ${rootStr}`); } } - await this._snapshot(l2Block.number); + await this.#snapshot(l2Block.number); return { isBlockOurs: ourBlock }; } From 1530eff0b47f97393bcc1eed22b8ca7bc579afa4 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:40:24 +0000 Subject: [PATCH 20/25] comment --- yarn-project/world-state/src/world-state-db/merkle_trees.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 e0a308812ea7..12250c1cb8b8 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 @@ -395,7 +395,9 @@ export class MerkleTrees implements MerkleTreeDb { async #updateArchive(header: Header, includeUncommitted: boolean) { const state = await this.getStateReference(includeUncommitted); - // TODO(benesjan): Is this even needed here? + + // This method should be called only when the block builder already updated the state so we sanity check that it's + // the case here. if (!state.toBuffer().equals(header.state.toBuffer())) { throw new Error('State in header does not match current state'); } From c0046ea17c9e000c55fa63fcba1eb6e14fae1a9b Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 11:42:31 +0000 Subject: [PATCH 21/25] removed unnecessary comment --- yarn-project/circuits.js/src/structs/header.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn-project/circuits.js/src/structs/header.ts b/yarn-project/circuits.js/src/structs/header.ts index 8b122fe928bf..016f9e9f98e7 100644 --- a/yarn-project/circuits.js/src/structs/header.ts +++ b/yarn-project/circuits.js/src/structs/header.ts @@ -109,7 +109,6 @@ export class Header { return Header.fromBuffer(buffer); } - // TODO(benesjan): cache this? would require making all properties readonly hash(): Fr { return Fr.fromBuffer( pedersenHash( From 5cc909b9209d09f90af688403ed98741290d8e15 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 12:46:59 +0000 Subject: [PATCH 22/25] updated stale comment --- yarn-project/world-state/src/world-state-db/merkle_trees.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 12250c1cb8b8..c11960c7eb65 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 @@ -176,9 +176,8 @@ export class MerkleTrees implements MerkleTreeDb { } /** - * Inserts into the roots trees (CONTRACT_TREE_ROOTS_TREE, NOTE_HASH_TREE_ROOTS_TREE, L1_TO_L2_MESSAGE_TREE_ROOTS_TREE) - * the current roots of the corresponding trees (CONTRACT_TREE, NOTE_HASH_TREE, L1_TO_L2_MESSAGE_TREE). - * @param header - The header to insert into the archive. + * Updates the archive with the new block/header hash. + * @param header - The header whose hash to insert into the archive. * @param includeUncommitted - Indicates whether to include uncommitted data. */ public async updateArchive(header: Header, includeUncommitted: boolean) { From 29dfb8d024502df6a38d4dafb46de8b3890097ae Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 12:52:31 +0000 Subject: [PATCH 23/25] below instead of bellow --- l1-contracts/src/periphery/ContractDeploymentEmitter.sol | 2 +- .../src/periphery/interfaces/IContractDeploymentEmitter.sol | 2 +- yarn-project/circuits.js/src/structs/complete_address.ts | 2 +- yarn-project/circuits.js/src/types/partial_address.ts | 2 +- yarn-project/end-to-end/src/integration_l1_publisher.test.ts | 2 +- yarn-project/foundation/src/abi/encoder.ts | 2 +- .../contracts/inclusion_proofs_contract/src/main.nr | 4 ++-- .../noir-protocol-circuits/src/crates/types/src/constants.nr | 2 +- .../src/block_builder/solo_block_builder.test.ts | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/l1-contracts/src/periphery/ContractDeploymentEmitter.sol b/l1-contracts/src/periphery/ContractDeploymentEmitter.sol index 15a72458d406..efa55fc0f357 100644 --- a/l1-contracts/src/periphery/ContractDeploymentEmitter.sol +++ b/l1-contracts/src/periphery/ContractDeploymentEmitter.sol @@ -23,7 +23,7 @@ contract ContractDeploymentEmitter is IContractDeploymentEmitter { * @param _saltedInitializationHash - Salted init hash * @param _publicKeyHash - Public key hash * @param _acir - The acir bytecode of the L2 contract - * @dev See the link bellow for more info on partial address and public key: + * @dev See the link below for more info on partial address and public key: * https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys * TODO: replace the link above with the link to deployed docs */ diff --git a/l1-contracts/src/periphery/interfaces/IContractDeploymentEmitter.sol b/l1-contracts/src/periphery/interfaces/IContractDeploymentEmitter.sol index bd45c93cd68c..9debce8240fd 100644 --- a/l1-contracts/src/periphery/interfaces/IContractDeploymentEmitter.sol +++ b/l1-contracts/src/periphery/interfaces/IContractDeploymentEmitter.sol @@ -19,7 +19,7 @@ interface IContractDeploymentEmitter { * @param saltedInitializationHash - Salted init hash * @param publicKeyHash - Public key hash * @param acir - The acir bytecode of the L2 contract - * @dev See the link bellow for more info on partial address and public key: + * @dev See the link below for more info on partial address and public key: * https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys * TODO: replace the link above with the link to deployed docs */ diff --git a/yarn-project/circuits.js/src/structs/complete_address.ts b/yarn-project/circuits.js/src/structs/complete_address.ts index 7708ee176078..cf34dfa24265 100644 --- a/yarn-project/circuits.js/src/structs/complete_address.ts +++ b/yarn-project/circuits.js/src/structs/complete_address.ts @@ -16,7 +16,7 @@ import { * * @remarks We have introduced this type because it is common that these 3 values are used together. They are commonly * used together because it is the information needed to send user a note. - * @remarks See the link bellow for details about how address is computed: + * @remarks See the link below for details about how address is computed: * https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys */ export class CompleteAddress { diff --git a/yarn-project/circuits.js/src/types/partial_address.ts b/yarn-project/circuits.js/src/types/partial_address.ts index e99d3786536a..21877f72e889 100644 --- a/yarn-project/circuits.js/src/types/partial_address.ts +++ b/yarn-project/circuits.js/src/types/partial_address.ts @@ -1,7 +1,7 @@ import { Fr } from '@aztec/foundation/fields'; /** - * A type which along with public key forms a preimage of a contract address. See the link bellow for more details + * A type which along with public key forms a preimage of a contract address. See the link below for more details * https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys */ export type PartialAddress = Fr; diff --git a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts index 9ce0bf7768e1..f147fccfd4b2 100644 --- a/yarn-project/end-to-end/src/integration_l1_publisher.test.ts +++ b/yarn-project/end-to-end/src/integration_l1_publisher.test.ts @@ -255,7 +255,7 @@ describe('L1Publisher integration', () => { l2ToL1Messages: block.newL2ToL1Msgs.map(m => `0x${m.toBuffer().toString('hex').padStart(64, '0')}`), }, block: { - // The json formatting in forge is a bit brittle, so we convert Fr to a number in the few values bellow. + // The json formatting in forge is a bit brittle, so we convert Fr to a number in the few values below. // This should not be a problem for testing as long as the values are not larger than u32. archive: `0x${block.archive.root.toBuffer().toString('hex').padStart(64, '0')}`, body: `0x${block.bodyToBuffer().toString('hex')}`, diff --git a/yarn-project/foundation/src/abi/encoder.ts b/yarn-project/foundation/src/abi/encoder.ts index cf25db56f546..ad515013769d 100644 --- a/yarn-project/foundation/src/abi/encoder.ts +++ b/yarn-project/foundation/src/abi/encoder.ts @@ -92,7 +92,7 @@ class ArgumentEncoder { break; } for (const field of abiType.fields) { - // The ugly check bellow is here because of a `CompleteAddress`. Since it has `address` property but in ABI + // The ugly check below is here because of a `CompleteAddress`. Since it has `address` property but in ABI // it's called inner we set `field.name` here to `address` instead of using `field.name`. I know it's hacky // but using address.address in Noir looks stupid and renaming `address` param of `CompleteAddress` // to `inner` doesn't make sense. diff --git a/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr b/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr index 58cb7b377a78..12a3a7970296 100644 --- a/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr @@ -97,7 +97,7 @@ contract InclusionProofs { fn test_note_inclusion_proof( owner: AztecAddress, block_number: u32, // The block at which we'll prove that the note exists - // Value bellow is only used when the note is not found --> used to test the note inclusion failure case (it + // Value below is only used when the note is not found --> used to test the note inclusion failure case (it // allows me to pass in random value of note nullifier - I cannot add and fetch a random note from PXE because // PXE performs note commitment inclusion check when you add a new note). spare_commitment: Field @@ -126,7 +126,7 @@ contract InclusionProofs { fn test_nullifier_non_inclusion_proof( owner: AztecAddress, block_number: u32, // The block at which we'll prove that the nullifier does not exists - // Value bellow is only used when the note is not found --> used to test the nullifier non-inclusion failure + // Value below is only used when the note is not found --> used to test the nullifier non-inclusion failure // case (it allows me to pass in random value of note nullifier - I cannot add and fetch a random note from PXE // because PXE performs note commitment inclusion check when you add a new note). spare_nullifier: Field diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr index 8a65c1d284c4..baeddd68ea5b 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr @@ -85,7 +85,7 @@ global ARGS_HASH_CHUNK_COUNT: u32 = 16; // NOIR CONSTANTS - constants used only in yarn-packages/noir-contracts // Some are defined here because Noir doesn't yet support globals referencing other globals yet. -// Move these constants to a noir file once the issue bellow is resolved: +// Move these constants to a noir file once the issue below is resolved: // https://github.com/noir-lang/noir/issues/1734 global L1_TO_L2_MESSAGE_LENGTH: Field = 8; global L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH: Field = 25; 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 1686dac939dc..470ce36f9cc3 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 @@ -227,7 +227,7 @@ describe('sequencer/solo_block_builder', () => { const l2Block = L2Block.fromFields({ archive: AppendOnlyTreeSnapshot.zero(), header: Header.empty(), - // Only the values bellow go to body hash/calldata hash + // Only the values below go to body hash/calldata hash newCommitments: newCommitments.map((sideEffect: SideEffect) => sideEffect.value), newNullifiers: newNullifiers.map((sideEffect: SideEffectLinkedToNoteHash) => sideEffect.value), newContracts, From 474b01aba8386076b6679fcc1dea95f63582c93a Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 12:53:56 +0000 Subject: [PATCH 24/25] stale arg description --- yarn-project/circuit-types/src/l2_block.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/yarn-project/circuit-types/src/l2_block.ts b/yarn-project/circuit-types/src/l2_block.ts index 5dd80826f924..4210e720eaf1 100644 --- a/yarn-project/circuit-types/src/l2_block.ts +++ b/yarn-project/circuit-types/src/l2_block.ts @@ -325,7 +325,6 @@ export class L2Block { /** * Deserializes L2 block without logs from a buffer. * @param buf - A serialized L2 block. - * @param blockHash - The hash of the block. * @returns Deserialized L2 block. */ static fromBuffer(buf: Buffer | BufferReader) { From 55250483c1b3266ec718b8d88fc4668f80d1639d Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 31 Jan 2024 12:54:58 +0000 Subject: [PATCH 25/25] improved readability --- .../src/world-state-db/merkle_trees.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 c11960c7eb65..3a55636c2730 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 @@ -70,6 +70,17 @@ export class MerkleTrees implements MerkleTreeDb { private constructor(private store: AztecKVStore, private log: DebugLogger) {} + /** + * Method to asynchronously create and initialize a MerkleTrees instance. + * @param store - The db instance to use for data persistance. + * @returns - A fully initialized MerkleTrees instance. + */ + public static async new(store: AztecKVStore, log = createDebugLogger('aztec:merkle_trees')) { + const merkleTrees = new MerkleTrees(store, log); + await merkleTrees.#init(); + return merkleTrees; + } + /** * Initializes the collection of Merkle Trees. */ @@ -136,17 +147,6 @@ export class MerkleTrees implements MerkleTreeDb { await this.#commit(); } - /** - * Method to asynchronously create and initialize a MerkleTrees instance. - * @param store - The db instance to use for data persistance. - * @returns - A fully initialized MerkleTrees instance. - */ - public static async new(store: AztecKVStore, log = createDebugLogger('aztec:merkle_trees')) { - const merkleTrees = new MerkleTrees(store, log); - await merkleTrees.#init(); - return merkleTrees; - } - public async buildInitialHeader(includeUncommitted: boolean): Promise
{ const state = await this.getStateReference(includeUncommitted); return new Header(AppendOnlyTreeSnapshot.zero(), Buffer.alloc(32, 0), state, GlobalVariables.empty());