From 764bba4dd8a016d45b201562ec82f9a12de65c2d Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Tue, 8 Oct 2024 19:58:01 +0300 Subject: [PATCH] refactor: pass by const reference (#9083) Uses const references for all complex parameters --- .../barretenberg/world_state/world_state.cpp | 40 ++++++++++--------- .../barretenberg/world_state/world_state.hpp | 36 +++++++++-------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp b/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp index 283bd3067fc..e45faaa87e9 100644 --- a/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp +++ b/barretenberg/cpp/src/barretenberg/world_state/world_state.cpp @@ -188,7 +188,7 @@ Fork::SharedPtr WorldState::create_new_fork(index_t blockNumber) return fork; } -TreeMetaResponse WorldState::get_tree_info(WorldStateRevision revision, MerkleTreeId tree_id) const +TreeMetaResponse WorldState::get_tree_info(const WorldStateRevision& revision, MerkleTreeId tree_id) const { Fork::SharedPtr fork = retrieve_fork(revision.forkId); return std::visit( @@ -213,7 +213,7 @@ TreeMetaResponse WorldState::get_tree_info(WorldStateRevision revision, MerkleTr fork->_trees.at(tree_id)); } -StateReference WorldState::get_state_reference(WorldStateRevision revision) const +StateReference WorldState::get_state_reference(const WorldStateRevision& revision) const { return get_state_reference(revision, retrieve_fork(revision.forkId)); } @@ -225,7 +225,9 @@ StateReference WorldState::get_initial_state_reference() const true); } -StateReference WorldState::get_state_reference(WorldStateRevision revision, Fork::SharedPtr fork, bool initial_state) +StateReference WorldState::get_state_reference(const WorldStateRevision& revision, + Fork::SharedPtr fork, + bool initial_state) { if (fork->_forkId != revision.forkId) { throw std::runtime_error("Fork does not match revision"); @@ -271,7 +273,7 @@ StateReference WorldState::get_state_reference(WorldStateRevision revision, Fork return state_reference; } -fr_sibling_path WorldState::get_sibling_path(WorldStateRevision revision, +fr_sibling_path WorldState::get_sibling_path(const WorldStateRevision& revision, MerkleTreeId tree_id, index_t leaf_index) const { @@ -314,7 +316,9 @@ void WorldState::update_public_data(const PublicDataLeafValue& new_value, Fork:: } } -void WorldState::update_archive(const StateReference& block_state_ref, fr block_header_hash, Fork::Id fork_id) +void WorldState::update_archive(const StateReference& block_state_ref, + const bb::fr& block_header_hash, + Fork::Id fork_id) { auto world_state_ref = get_state_reference(WorldStateRevision{ .forkId = fork_id, .includeUncommitted = true }); if (block_state_matches_world_state(block_state_ref, world_state_ref)) { @@ -354,7 +358,7 @@ void WorldState::rollback() } bool WorldState::sync_block(const StateReference& block_state_ref, - fr block_header_hash, + const bb::fr& block_header_hash, const std::vector& notes, const std::vector& l1_to_l2_messages, const std::vector& nullifiers, @@ -445,9 +449,9 @@ bool WorldState::sync_block(const StateReference& block_state_ref, throw std::runtime_error("Can't synch block: block state does not match world state"); } -GetLowIndexedLeafResponse WorldState::find_low_leaf_index(const WorldStateRevision revision, +GetLowIndexedLeafResponse WorldState::find_low_leaf_index(const WorldStateRevision& revision, MerkleTreeId tree_id, - fr leaf_key) const + const bb::fr& leaf_key) const { Fork::SharedPtr fork = retrieve_fork(revision.forkId); Signal signal; @@ -479,7 +483,7 @@ GetLowIndexedLeafResponse WorldState::find_low_leaf_index(const WorldStateRevisi return low_leaf_info; } -bb::fr WorldState::compute_initial_archive(StateReference initial_state_ref) +bb::fr WorldState::compute_initial_archive(const StateReference& initial_state_ref) { // NOTE: this hash operations needs to match the one in yarn-project/circuits.js/src/structs/header.ts return HashPolicy::hash({ GENERATOR_INDEX__BLOCK_HASH, // separator @@ -492,14 +496,14 @@ bb::fr WorldState::compute_initial_archive(StateReference initial_state_ref) 0, 0, // state reference - the initial state for all the trees (accept the archive tree) - initial_state_ref[MerkleTreeId::L1_TO_L2_MESSAGE_TREE].first, - initial_state_ref[MerkleTreeId::L1_TO_L2_MESSAGE_TREE].second, - initial_state_ref[MerkleTreeId::NOTE_HASH_TREE].first, - initial_state_ref[MerkleTreeId::NOTE_HASH_TREE].second, - initial_state_ref[MerkleTreeId::NULLIFIER_TREE].first, - initial_state_ref[MerkleTreeId::NULLIFIER_TREE].second, - initial_state_ref[MerkleTreeId::PUBLIC_DATA_TREE].first, - initial_state_ref[MerkleTreeId::PUBLIC_DATA_TREE].second, + initial_state_ref.at(MerkleTreeId::L1_TO_L2_MESSAGE_TREE).first, + initial_state_ref.at(MerkleTreeId::L1_TO_L2_MESSAGE_TREE).second, + initial_state_ref.at(MerkleTreeId::NOTE_HASH_TREE).first, + initial_state_ref.at(MerkleTreeId::NOTE_HASH_TREE).second, + initial_state_ref.at(MerkleTreeId::NULLIFIER_TREE).first, + initial_state_ref.at(MerkleTreeId::NULLIFIER_TREE).second, + initial_state_ref.at(MerkleTreeId::PUBLIC_DATA_TREE).first, + initial_state_ref.at(MerkleTreeId::PUBLIC_DATA_TREE).second, // global variables 0, 0, @@ -528,7 +532,7 @@ bool WorldState::block_state_matches_world_state(const StateReference& block_sta tree_ids.begin(), tree_ids.end(), [=](auto id) { return block_state_ref.at(id) == tree_state_ref.at(id); }); } -bool WorldState::is_archive_tip(WorldStateRevision revision, bb::fr block_header_hash) const +bool WorldState::is_archive_tip(const WorldStateRevision& revision, const bb::fr& block_header_hash) const { std::optional leaf_index = find_leaf_index(revision, MerkleTreeId::ARCHIVE, block_header_hash); diff --git a/barretenberg/cpp/src/barretenberg/world_state/world_state.hpp b/barretenberg/cpp/src/barretenberg/world_state/world_state.hpp index b65ed1cc013..d01a19f40bb 100644 --- a/barretenberg/cpp/src/barretenberg/world_state/world_state.hpp +++ b/barretenberg/cpp/src/barretenberg/world_state/world_state.hpp @@ -63,7 +63,7 @@ class WorldState { * @param tree_id The ID of the tree * @return TreeInfo */ - crypto::merkle_tree::TreeMetaResponse get_tree_info(WorldStateRevision revision, MerkleTreeId tree_id) const; + crypto::merkle_tree::TreeMetaResponse get_tree_info(const WorldStateRevision& revision, MerkleTreeId tree_id) const; /** * @brief Gets the state reference for all the trees in the world state @@ -71,7 +71,7 @@ class WorldState { * @param revision The revision to query * @return StateReference */ - StateReference get_state_reference(WorldStateRevision revision) const; + StateReference get_state_reference(const WorldStateRevision& revision) const; /** * @brief Gets the initial state reference for all the trees in the world state @@ -88,7 +88,7 @@ class WorldState { * @param leaf_index The index of the leaf * @return crypto::merkle_tree::fr_sibling_path */ - crypto::merkle_tree::fr_sibling_path get_sibling_path(WorldStateRevision revision, + crypto::merkle_tree::fr_sibling_path get_sibling_path(const WorldStateRevision& revision, MerkleTreeId tree_id, index_t leaf_index) const; @@ -102,7 +102,7 @@ class WorldState { * @return std::optional The IndexedLeaf object or nullopt if the leaf does not exist */ template - std::optional> get_indexed_leaf(WorldStateRevision revision, + std::optional> get_indexed_leaf(const WorldStateRevision& revision, MerkleTreeId tree_id, index_t leaf_index) const; @@ -116,7 +116,7 @@ class WorldState { * @return std::optional The value of the leaf or nullopt if the leaf does not exist */ template - std::optional get_leaf(WorldStateRevision revision, MerkleTreeId tree_id, index_t leaf_index) const; + std::optional get_leaf(const WorldStateRevision& revision, MerkleTreeId tree_id, index_t leaf_index) const; /** * @brief Finds the leaf that would have its nextIdx/nextValue fields modified if the target leaf were to be @@ -127,9 +127,9 @@ class WorldState { * @param leaf_key The leaf to find the predecessor of * @return PredecessorInfo */ - crypto::merkle_tree::GetLowIndexedLeafResponse find_low_leaf_index(WorldStateRevision revision, + crypto::merkle_tree::GetLowIndexedLeafResponse find_low_leaf_index(const WorldStateRevision& revision, MerkleTreeId tree_id, - fr leaf_key) const; + const bb::fr& leaf_key) const; /** * @brief Finds the index of a leaf in a tree @@ -141,7 +141,7 @@ class WorldState { * @return std::optional */ template - std::optional find_leaf_index(WorldStateRevision revision, + std::optional find_leaf_index(const WorldStateRevision& revision, MerkleTreeId tree_id, const T& leaf, index_t start_index = 0) const; @@ -186,7 +186,7 @@ class WorldState { * @param fork_id The fork ID to update. */ void update_archive(const StateReference& block_state_ref, - fr block_header_hash, + const bb::fr& block_header_hash, Fork::Id fork_id = CANONICAL_FORK_ID); /** @@ -205,14 +205,14 @@ class WorldState { * @param block The block to synchronize with. */ bool sync_block(const StateReference& block_state_ref, - fr block_header_hash, + const bb::fr& block_header_hash, const std::vector& notes, const std::vector& l1_to_l2_messages, const std::vector& nullifiers, const std::vector>& public_writes); uint64_t create_fork(index_t blockNumber); - void delete_fork(uint64_t forkId); + void delete_fork(Fork::Id forkId); private: std::shared_ptr _workers; @@ -229,11 +229,11 @@ class WorldState { Fork::SharedPtr retrieve_fork(uint64_t forkId) const; Fork::SharedPtr create_new_fork(index_t blockNumber); - bool is_archive_tip(WorldStateRevision revision, bb::fr block_header_hash) const; + bool is_archive_tip(const WorldStateRevision& revision, const bb::fr& block_header_hash) const; - static bb::fr compute_initial_archive(StateReference initial_state_ref); + static bb::fr compute_initial_archive(const StateReference& initial_state_ref); - static StateReference get_state_reference(WorldStateRevision revision, + static StateReference get_state_reference(const WorldStateRevision& revision, Fork::SharedPtr fork, bool initial_state = false); @@ -242,7 +242,7 @@ class WorldState { }; template -std::optional> WorldState::get_indexed_leaf(const WorldStateRevision rev, +std::optional> WorldState::get_indexed_leaf(const WorldStateRevision& rev, MerkleTreeId id, index_t leaf) const { @@ -277,7 +277,9 @@ std::optional> WorldState::get_indexed_leaf( } template -std::optional WorldState::get_leaf(const WorldStateRevision revision, MerkleTreeId tree_id, index_t leaf_index) const +std::optional WorldState::get_leaf(const WorldStateRevision& revision, + MerkleTreeId tree_id, + index_t leaf_index) const { using namespace crypto::merkle_tree; @@ -323,7 +325,7 @@ std::optional WorldState::get_leaf(const WorldStateRevision revision, MerkleT } template -std::optional WorldState::find_leaf_index(const WorldStateRevision rev, +std::optional WorldState::find_leaf_index(const WorldStateRevision& rev, MerkleTreeId id, const T& leaf, index_t start_index) const