Skip to content

Commit

Permalink
refactor: pass by const reference (#9083)
Browse files Browse the repository at this point in the history
Uses const references for all complex parameters
  • Loading branch information
alexghr authored Oct 8, 2024
1 parent f9fc73a commit 764bba4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
40 changes: 22 additions & 18 deletions barretenberg/cpp/src/barretenberg/world_state/world_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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));
}
Expand All @@ -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");
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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<bb::fr>& notes,
const std::vector<bb::fr>& l1_to_l2_messages,
const std::vector<crypto::merkle_tree::NullifierLeafValue>& nullifiers,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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<index_t> leaf_index = find_leaf_index(revision, MerkleTreeId::ARCHIVE, block_header_hash);

Expand Down
36 changes: 19 additions & 17 deletions barretenberg/cpp/src/barretenberg/world_state/world_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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
*
* @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
Expand All @@ -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;

Expand All @@ -102,7 +102,7 @@ class WorldState {
* @return std::optional<T> The IndexedLeaf object or nullopt if the leaf does not exist
*/
template <typename T>
std::optional<crypto::merkle_tree::IndexedLeaf<T>> get_indexed_leaf(WorldStateRevision revision,
std::optional<crypto::merkle_tree::IndexedLeaf<T>> get_indexed_leaf(const WorldStateRevision& revision,
MerkleTreeId tree_id,
index_t leaf_index) const;

Expand All @@ -116,7 +116,7 @@ class WorldState {
* @return std::optional<T> The value of the leaf or nullopt if the leaf does not exist
*/
template <typename T>
std::optional<T> get_leaf(WorldStateRevision revision, MerkleTreeId tree_id, index_t leaf_index) const;
std::optional<T> 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
Expand All @@ -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
Expand All @@ -141,7 +141,7 @@ class WorldState {
* @return std::optional<index_t>
*/
template <typename T>
std::optional<index_t> find_leaf_index(WorldStateRevision revision,
std::optional<index_t> find_leaf_index(const WorldStateRevision& revision,
MerkleTreeId tree_id,
const T& leaf,
index_t start_index = 0) const;
Expand Down Expand Up @@ -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);

/**
Expand All @@ -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<bb::fr>& notes,
const std::vector<bb::fr>& l1_to_l2_messages,
const std::vector<crypto::merkle_tree::NullifierLeafValue>& nullifiers,
const std::vector<std::vector<crypto::merkle_tree::PublicDataLeafValue>>& 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<bb::ThreadPool> _workers;
Expand All @@ -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);

Expand All @@ -242,7 +242,7 @@ class WorldState {
};

template <typename T>
std::optional<crypto::merkle_tree::IndexedLeaf<T>> WorldState::get_indexed_leaf(const WorldStateRevision rev,
std::optional<crypto::merkle_tree::IndexedLeaf<T>> WorldState::get_indexed_leaf(const WorldStateRevision& rev,
MerkleTreeId id,
index_t leaf) const
{
Expand Down Expand Up @@ -277,7 +277,9 @@ std::optional<crypto::merkle_tree::IndexedLeaf<T>> WorldState::get_indexed_leaf(
}

template <typename T>
std::optional<T> WorldState::get_leaf(const WorldStateRevision revision, MerkleTreeId tree_id, index_t leaf_index) const
std::optional<T> WorldState::get_leaf(const WorldStateRevision& revision,
MerkleTreeId tree_id,
index_t leaf_index) const
{
using namespace crypto::merkle_tree;

Expand Down Expand Up @@ -323,7 +325,7 @@ std::optional<T> WorldState::get_leaf(const WorldStateRevision revision, MerkleT
}

template <typename T>
std::optional<index_t> WorldState::find_leaf_index(const WorldStateRevision rev,
std::optional<index_t> WorldState::find_leaf_index(const WorldStateRevision& rev,
MerkleTreeId id,
const T& leaf,
index_t start_index) const
Expand Down

0 comments on commit 764bba4

Please sign in to comment.