Skip to content

Commit a437e73

Browse files
authored
feat: Leaf index requests to the native world state can now be performed as a batch query (#10649)
Previously, every request to retrieve the index for a given leaf had to made using a seperate request. Given the number of requests that need to be handled, this has now been changed to offer a batch request instead.
1 parent 19a500f commit a437e73

File tree

30 files changed

+551
-586
lines changed

30 files changed

+551
-586
lines changed

barretenberg/cpp/src/barretenberg/crypto/merkle_tree/append_only_tree/content_addressed_append_only_tree.hpp

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,34 @@ template <typename Store, typename HashingPolicy> class ContentAddressedAppendOn
159159
/**
160160
* @brief Returns the index of the provided leaf in the tree
161161
*/
162-
void find_leaf_index(const fr& leaf, bool includeUncommitted, const FindLeafCallback& on_completion) const;
162+
void find_leaf_indices(const std::vector<typename Store::LeafType>& leaves,
163+
bool includeUncommitted,
164+
const FindLeafCallback& on_completion) const;
163165

164166
/**
165167
* @brief Returns the index of the provided leaf in the tree
166168
*/
167-
void find_leaf_index(const fr& leaf,
168-
const block_number_t& blockNumber,
169-
bool includeUncommitted,
170-
const FindLeafCallback& on_completion) const;
169+
void find_leaf_indices(const std::vector<typename Store::LeafType>& leaves,
170+
const block_number_t& blockNumber,
171+
bool includeUncommitted,
172+
const FindLeafCallback& on_completion) const;
171173

172174
/**
173175
* @brief Returns the index of the provided leaf in the tree only if it exists after the index value provided
174176
*/
175-
void find_leaf_index_from(const fr& leaf,
176-
const index_t& start_index,
177-
bool includeUncommitted,
178-
const FindLeafCallback& on_completion) const;
177+
void find_leaf_indices_from(const std::vector<typename Store::LeafType>& leaves,
178+
const index_t& start_index,
179+
bool includeUncommitted,
180+
const FindLeafCallback& on_completion) const;
179181

180182
/**
181183
* @brief Returns the index of the provided leaf in the tree only if it exists after the index value provided
182184
*/
183-
void find_leaf_index_from(const fr& leaf,
184-
const index_t& start_index,
185-
const block_number_t& blockNumber,
186-
bool includeUncommitted,
187-
const FindLeafCallback& on_completion) const;
185+
void find_leaf_indices_from(const std::vector<typename Store::LeafType>& leaves,
186+
const index_t& start_index,
187+
const block_number_t& blockNumber,
188+
bool includeUncommitted,
189+
const FindLeafCallback& on_completion) const;
188190

189191
/**
190192
* @brief Returns the block numbers that correspond to the given indices values
@@ -415,14 +417,9 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_block_numbers(
415417
execute_and_report<BlockForIndexResponse>(
416418
[=, this](TypedResponse<BlockForIndexResponse>& response) {
417419
response.inner.blockNumbers.reserve(indices.size());
418-
TreeMeta meta;
419420
ReadTransactionPtr tx = store_->create_read_transaction();
420-
store_->get_meta(meta, *tx, true);
421-
index_t maxIndex = meta.committedSize;
422421
for (index_t index : indices) {
423-
bool outOfRange = index >= maxIndex;
424-
std::optional<block_number_t> block =
425-
outOfRange ? std::nullopt : store_->find_block_for_index(index, *tx);
422+
std::optional<block_number_t> block = store_->find_block_for_index(index, *tx);
426423
response.inner.blockNumbers.emplace_back(block);
427424
}
428425
},
@@ -441,16 +438,14 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_block_numbers(
441438
execute_and_report<BlockForIndexResponse>(
442439
[=, this](TypedResponse<BlockForIndexResponse>& response) {
443440
response.inner.blockNumbers.reserve(indices.size());
444-
TreeMeta meta;
445441
BlockPayload blockPayload;
446442
ReadTransactionPtr tx = store_->create_read_transaction();
447-
store_->get_meta(meta, *tx, true);
448443
if (!store_->get_block_data(blockNumber, blockPayload, *tx)) {
449444
throw std::runtime_error(format("Unable to find block numbers for indices for block ",
450445
blockNumber,
451446
", failed to get block data."));
452447
}
453-
index_t maxIndex = std::min(meta.committedSize, blockPayload.size);
448+
index_t maxIndex = blockPayload.size;
454449
for (index_t index : indices) {
455450
bool outOfRange = index >= maxIndex;
456451
std::optional<block_number_t> block =
@@ -718,43 +713,45 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::get_leaf(const index_
718713
}
719714

720715
template <typename Store, typename HashingPolicy>
721-
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index(const fr& leaf,
722-
bool includeUncommitted,
723-
const FindLeafCallback& on_completion) const
716+
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_indices(
717+
const std::vector<typename Store::LeafType>& leaves,
718+
bool includeUncommitted,
719+
const FindLeafCallback& on_completion) const
724720
{
725-
find_leaf_index_from(leaf, 0, includeUncommitted, on_completion);
721+
find_leaf_indices_from(leaves, 0, includeUncommitted, on_completion);
726722
}
727723

728724
template <typename Store, typename HashingPolicy>
729-
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index(const fr& leaf,
730-
const block_number_t& blockNumber,
731-
bool includeUncommitted,
732-
const FindLeafCallback& on_completion) const
725+
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_indices(
726+
const std::vector<typename Store::LeafType>& leaves,
727+
const block_number_t& blockNumber,
728+
bool includeUncommitted,
729+
const FindLeafCallback& on_completion) const
733730
{
734-
find_leaf_index_from(leaf, 0, blockNumber, includeUncommitted, on_completion);
731+
find_leaf_indices_from(leaves, 0, blockNumber, includeUncommitted, on_completion);
735732
}
736733

737734
template <typename Store, typename HashingPolicy>
738-
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index_from(
739-
const fr& leaf, const index_t& start_index, bool includeUncommitted, const FindLeafCallback& on_completion) const
735+
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_indices_from(
736+
const std::vector<typename Store::LeafType>& leaves,
737+
const index_t& start_index,
738+
bool includeUncommitted,
739+
const FindLeafCallback& on_completion) const
740740
{
741741
auto job = [=, this]() -> void {
742742
execute_and_report<FindLeafIndexResponse>(
743743
[=, this](TypedResponse<FindLeafIndexResponse>& response) {
744-
if (leaf == fr::zero()) {
745-
throw std::runtime_error("Requesting indices for zero leaves is prohibited");
746-
}
744+
response.inner.leaf_indices.reserve(leaves.size());
747745
ReadTransactionPtr tx = store_->create_read_transaction();
746+
748747
RequestContext requestContext;
749748
requestContext.includeUncommitted = includeUncommitted;
750749
requestContext.root = store_->get_current_root(*tx, includeUncommitted);
751-
std::optional<index_t> leaf_index =
752-
store_->find_leaf_index_from(leaf, start_index, requestContext, *tx, includeUncommitted);
753-
response.success = leaf_index.has_value();
754-
if (response.success) {
755-
response.inner.leaf_index = leaf_index.value();
756-
} else {
757-
response.message = format("Failed to find index from ", start_index, " for leaf ", leaf);
750+
751+
for (const auto& leaf : leaves) {
752+
std::optional<index_t> leaf_index =
753+
store_->find_leaf_index_from(leaf, start_index, requestContext, *tx);
754+
response.inner.leaf_indices.emplace_back(leaf_index);
758755
}
759756
},
760757
on_completion);
@@ -763,8 +760,8 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index_from(
763760
}
764761

765762
template <typename Store, typename HashingPolicy>
766-
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index_from(
767-
const fr& leaf,
763+
void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_indices_from(
764+
const std::vector<typename Store::LeafType>& leaves,
768765
const index_t& start_index,
769766
const block_number_t& blockNumber,
770767
bool includeUncommitted,
@@ -773,12 +770,10 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index_from(
773770
auto job = [=, this]() -> void {
774771
execute_and_report<FindLeafIndexResponse>(
775772
[=, this](TypedResponse<FindLeafIndexResponse>& response) {
773+
response.inner.leaf_indices.reserve(leaves.size());
776774
if (blockNumber == 0) {
777775
throw std::runtime_error("Unable to find leaf index for block number 0");
778776
}
779-
if (leaf == fr::zero()) {
780-
throw std::runtime_error("Requesting indices for zero leaves is prohibited");
781-
}
782777
ReadTransactionPtr tx = store_->create_read_transaction();
783778
BlockPayload blockData;
784779
if (!store_->get_block_data(blockNumber, blockData, *tx)) {
@@ -788,18 +783,17 @@ void ContentAddressedAppendOnlyTree<Store, HashingPolicy>::find_leaf_index_from(
788783
blockNumber,
789784
", failed to get block data."));
790785
}
786+
791787
RequestContext requestContext;
792788
requestContext.blockNumber = blockNumber;
793789
requestContext.includeUncommitted = includeUncommitted;
794790
requestContext.root = blockData.root;
795-
std::optional<index_t> leaf_index =
796-
store_->find_leaf_index_from(leaf, start_index, requestContext, *tx, includeUncommitted);
797-
response.success = leaf_index.has_value();
798-
if (response.success) {
799-
response.inner.leaf_index = leaf_index.value();
800-
} else {
801-
response.message = format(
802-
"Failed to find index from ", start_index, " for leaf ", leaf, " at block ", blockNumber);
791+
requestContext.maxIndex = blockData.size;
792+
793+
for (const auto& leaf : leaves) {
794+
std::optional<index_t> leaf_index =
795+
store_->find_leaf_index_from(leaf, start_index, requestContext, *tx);
796+
response.inner.leaf_indices.emplace_back(leaf_index);
803797
}
804798
},
805799
on_completion);

0 commit comments

Comments
 (0)