29#include <unordered_map>
62 const std::string& data_dir,
64 const std::unordered_map<MerkleTreeId, uint32_t>& tree_heights,
65 const std::unordered_map<MerkleTreeId, index_t>& tree_prefill,
66 uint32_t initial_header_generator_point,
67 uint64_t genesis_timestamp = 0);
70 const std::string& data_dir,
72 const std::unordered_map<MerkleTreeId, uint32_t>& tree_heights,
73 const std::unordered_map<MerkleTreeId, index_t>& tree_prefill,
74 uint32_t initial_header_generator_point,
75 uint64_t genesis_timestamp = 0);
78 const std::string& data_dir,
80 const std::unordered_map<MerkleTreeId, uint32_t>& tree_heights,
81 const std::unordered_map<MerkleTreeId, index_t>& tree_prefill,
83 uint32_t initial_header_generator_point,
84 uint64_t genesis_timestamp = 0);
87 const std::string& data_dir,
89 const std::unordered_map<MerkleTreeId, uint32_t>& tree_heights,
90 const std::unordered_map<MerkleTreeId, index_t>& tree_prefill,
92 uint32_t initial_header_generator_point,
93 uint64_t genesis_timestamp = 0);
101 void copy_stores(
const std::string& dstPath,
bool compact)
const;
141 const std::vector<index_t>& leafIndices,
153 template <
typename T>
167 template <
typename T>
181 const bb::fr& leaf_key)
const;
192 template <
typename T>
197 index_t start_index = 0)
const;
207 template <
typename T>
220 template <
typename T>
231 template <
typename T>
234 uint32_t subtree_depth,
245 template <
typename T>
266 const bb::fr& block_header_hash,
288 const bb::fr& block_header_hash,
289 const std::vector<bb::fr>& notes,
290 const std::vector<bb::fr>& l1_to_l2_messages,
307 std::unordered_map<uint64_t, Fork::SharedPtr>
_forks;
316 uint64_t maxReaders);
339 uint32_t generator_point,
340 uint64_t genesis_timestamp = 0);
344 bool initial_state =
false);
353 template <
typename TreeType>
358 std::string& message,
361 template <
typename TreeType>
366 std::string& message,
370 template <
typename TreeType>
375 std::string& message,
380template <
typename TreeType>
385 std::string& message,
389 bool expected =
true;
390 if (!response.
success && success.compare_exchange_strong(expected,
false)) {
391 message = response.message;
399template <
typename TreeType>
404 std::string& message,
409 bool expected =
true;
410 if (!response.
success && success.compare_exchange_strong(expected,
false)) {
411 message = response.message;
419template <
typename TreeType>
424 std::string& message,
429 bool expected =
true;
430 if (!response.
success && success.compare_exchange_strong(expected,
false)) {
431 message = response.message;
466 throw std::runtime_error(
"Failed to find indexed leaf: " + local.
message);
469 return local.
inner.indexed_leaf;
472 throw std::runtime_error(
"Invalid tree type");
480 using namespace crypto::merkle_tree;
486 std::string error_msg;
491 if (!response.success || !response.inner.leaf.has_value()) {
495 error_msg = response.message;
497 leaf = response.inner.leaf;
514 if (!response.success || !response.inner.indexed_leaf.has_value()) {
516 error_msg = response.message;
518 leaf = response.inner.indexed_leaf.value().leaf;
542 using namespace crypto::merkle_tree;
555 wrapper.tree->find_leaf_indices_from(
558 wrapper.tree->find_leaf_indices_from(leaves, start_index, rev.
includeUncommitted, callback);
570 wrapper.tree->find_leaf_indices_from(leaves, start_index, rev.
includeUncommitted, callback);
576 if (!local.
success || local.
inner.leaf_indices.size() != leaves.size()) {
577 throw std::runtime_error(local.
message);
589 using namespace crypto::merkle_tree;
621 if (!local.
success || local.
inner.leaf_paths.size() != leaves.size()) {
622 throw std::runtime_error(local.
message);
630 using namespace crypto::merkle_tree;
637 std::string error_msg;
641 auto callback = [&](
const auto& resp) {
644 error_msg = resp.message;
648 wrapper.tree->add_values(leaves, callback);
653 typename Tree::AddCompletionCallback callback = [&](
const auto& resp) {
656 error_msg = resp.message;
660 wrapper.tree->add_or_update_values(leaves, 0, callback);
666 throw std::runtime_error(error_msg);
673 uint32_t subtree_depth,
676 using namespace crypto::merkle_tree;
686 std::string error_msg;
688 wrapper.tree->add_or_update_values(
690 if (response.success) {
696 error_msg = response.message;
705 throw std::runtime_error(error_msg);
716 using namespace crypto::merkle_tree;
726 std::string error_msg;
728 wrapper.tree->add_or_update_values_sequentially(
730 if (response.success) {
735 error_msg = response.message;
744 throw std::runtime_error(error_msg);
std::shared_ptr< Napi::ThreadSafeFunction > revert_checkpoint
std::shared_ptr< Napi::ThreadSafeFunction > commit_checkpoint
bb::bbapi::CommandResponse responses
Implements a simple append-only merkle tree All methods are asynchronous unless specified as otherwis...
void remove_historic_block(const block_number_t &blockNumber, const RemoveHistoricBlockCallback &on_completion)
void commit(const CommitCallback &on_completion)
Commit the tree to the backing store.
void unwind_block(const block_number_t &blockNumber, const UnwindBlockCallback &on_completion)
void find_leaf_indices_from(const std::vector< typename Store::LeafType > &leaves, const index_t &start_index, bool includeUncommitted, const FindLeafCallback &on_completion) const
Returns the index of the provided leaf in the tree only if it exists after the index value provided.
void find_leaf_sibling_paths(const std::vector< typename Store::LeafType > &leaves, bool includeUncommitted, const FindSiblingPathCallback &on_completion) const
Returns the sibling paths for the provided leaves in the tree.
Serves as a key-value node store for merkle trees. Caches all changes in memory before persisting the...
Implements a parallelized batch insertion indexed tree Accepts template argument of the type of store...
Used in parallel insertions in the the IndexedTree. Workers signal to other following workes as they ...
void signal_level(uint32_t level=0)
Signals that the given level has been passed.
void signal_decrement(uint32_t delta=1)
void wait_for_level(uint32_t level=0)
Causes the thread to wait until the required level has been signalled.
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
WorldStateStatusFull remove_historical_blocks(const block_number_t &toBlockNumber)
std::shared_ptr< bb::ThreadPool > _workers
void remove_forks_for_block(const block_number_t &blockNumber)
BatchInsertionResult< T > batch_insert_indexed_leaves(MerkleTreeId tree_id, const std::vector< T > &leaves, uint32_t subtree_depth, Fork::Id fork_id=CANONICAL_FORK_ID)
Batch inserts a set of leaves into an indexed Merkle Tree.
bool unwind_block(const block_number_t &blockNumber, WorldStateStatusFull &status)
static void get_status_summary_from_meta_responses(WorldStateStatusSummary &status, std::array< TreeMeta, NUM_TREES > &metaResponses)
void commit_tree(TreeDBStats &dbStats, Signal &signal, TreeType &tree, std::atomic_bool &success, std::string &message, TreeMeta &meta)
TreeStateReference get_tree_snapshot(MerkleTreeId id)
void append_leaves(MerkleTreeId tree_id, const std::vector< T > &leaves, Fork::Id fork_id=CANONICAL_FORK_ID)
Appends a set of leaves to an existing Merkle Tree.
StateReference get_initial_state_reference() const
Gets the initial state reference for all the trees in the world state.
void find_sibling_paths(const WorldStateRevision &revision, MerkleTreeId tree_id, const std::vector< T > &leaves, std::vector< std::optional< SiblingPathAndIndex > > &paths) const
Finds the sibling paths of leaves in a tree.
std::optional< crypto::merkle_tree::IndexedLeaf< T > > get_indexed_leaf(const WorldStateRevision &revision, MerkleTreeId tree_id, index_t leaf_index) const
Get the leaf preimage object.
uint32_t checkpoint(const uint64_t &forkId)
WorldStateStatusFull attempt_tree_resync()
crypto::merkle_tree::TreeMetaResponse get_tree_info(const WorldStateRevision &revision, MerkleTreeId tree_id) const
Get tree metadata for a particular tree.
static void populate_status_summary(WorldStateStatusFull &status)
void commit_all_checkpoints_to(const uint64_t &forkId, uint32_t depth)
void unwind_tree(TreeDBStats &dbStats, Signal &signal, TreeType &tree, std::atomic_bool &success, std::string &message, TreeMeta &meta, const block_number_t &blockNumber)
std::unordered_map< uint64_t, Fork::SharedPtr > _forks
void create_canonical_fork(const std::string &dataDir, const std::unordered_map< MerkleTreeId, uint64_t > &dbSize, const std::vector< PublicDataLeafValue > &prefilled_public_data, uint64_t maxReaders)
std::pair< bool, std::string > commit(WorldStateStatusFull &status)
Commits the current state of the world state.
void remove_historic_block_for_tree(TreeDBStats &dbStats, Signal &signal, TreeType &tree, std::atomic_bool &success, std::string &message, TreeMeta &meta, const block_number_t &blockNumber)
SequentialInsertionResult< T > insert_indexed_leaves(MerkleTreeId tree_id, const std::vector< T > &leaves, Fork::Id fork_id=CANONICAL_FORK_ID)
Inserts a set of leaves sequentially into an indexed Merkle Tree.
void get_block_numbers_for_leaf_indices(const WorldStateRevision &revision, MerkleTreeId tree_id, const std::vector< index_t > &leafIndices, std::vector< std::optional< block_number_t > > &blockNumbers) const
uint64_t _genesis_timestamp
StateReference get_state_reference(const WorldStateRevision &revision) const
Gets the state reference for all the trees in the world state.
WorldStateStatusFull unwind_blocks(const block_number_t &toBlockNumber)
uint32_t _initial_header_generator_point
bool is_archive_tip(const WorldStateRevision &revision, const bb::fr &block_header_hash) const
static bool determine_if_synched(std::array< TreeMeta, NUM_TREES > &metaResponses)
void update_public_data(const crypto::merkle_tree::PublicDataLeafValue &new_value, Fork::Id fork_id=CANONICAL_FORK_ID)
Updates a leaf in an existing Merkle Tree.
Fork::SharedPtr create_new_fork(const block_number_t &blockNumber)
void get_status_summary(WorldStateStatusSummary &status) const
void rollback()
Rolls back any uncommitted changes made to the world state.
WorldStateStatusFull sync_block(const StateReference &block_state_ref, const bb::fr &block_header_hash, const std::vector< bb::fr > ¬es, const std::vector< bb::fr > &l1_to_l2_messages, const std::vector< crypto::merkle_tree::NullifierLeafValue > &nullifiers, const std::vector< crypto::merkle_tree::PublicDataLeafValue > &public_writes)
WorldStateStatusSummary set_finalized_blocks(const block_number_t &toBlockNumber)
std::unordered_map< MerkleTreeId, index_t > _initial_tree_size
void delete_fork(const uint64_t &forkId)
bool remove_historical_block(const block_number_t &blockNumber, WorldStateStatusFull &status)
std::unordered_map< MerkleTreeId, uint32_t > _tree_heights
static bb::fr compute_initial_block_header_hash(const StateReference &initial_state_ref, uint32_t generator_point, uint64_t genesis_timestamp=0)
uint64_t create_fork(const std::optional< block_number_t > &blockNumber)
crypto::merkle_tree::fr_sibling_path get_sibling_path(const WorldStateRevision &revision, MerkleTreeId tree_id, index_t leaf_index) const
Get the sibling path object for a leaf in a tree.
void validate_trees_are_equally_synched()
void find_leaf_indices(const WorldStateRevision &revision, MerkleTreeId tree_id, const std::vector< T > &leaves, std::vector< std::optional< index_t > > &indices, index_t start_index=0) const
Finds the index of a leaf in a tree.
std::optional< T > get_leaf(const WorldStateRevision &revision, MerkleTreeId tree_id, index_t leaf_index) const
Gets the value of a leaf in a tree.
static bool block_state_matches_world_state(const StateReference &block_state_ref, const StateReference &tree_state_ref)
void revert_all_checkpoints_to(const uint64_t &forkId, uint32_t depth)
bool is_same_state_reference(const WorldStateRevision &revision, const StateReference &state_ref) const
crypto::merkle_tree::GetLowIndexedLeafResponse find_low_leaf_index(const WorldStateRevision &revision, MerkleTreeId tree_id, const bb::fr &leaf_key) const
Finds the leaf that would have its nextIdx/nextValue fields modified if the target leaf were to be in...
void copy_stores(const std::string &dstPath, bool compact) const
Copies all underlying LMDB stores to the target directory while acquiring a write lock.
void update_archive(const StateReference &block_state_ref, const bb::fr &block_header_hash, Fork::Id fork_id=CANONICAL_FORK_ID)
Updates the archive tree with a new block.
bool set_finalized_block(const block_number_t &blockNumber)
WorldStateStores::Ptr _persistentStores
Fork::SharedPtr retrieve_fork(const uint64_t &forkId) const
void get_all_tree_info(const WorldStateRevision &revision, std::array< TreeMeta, NUM_TREES > &responses) const
std::vector< fr > fr_sibling_path
std::variant< TreeWithStore< FrTree >, TreeWithStore< NullifierTree >, TreeWithStore< PublicDataTree > > Tree
const uint64_t DEFAULT_MIN_NUMBER_OF_READERS
std::pair< bb::fr, bb::crypto::merkle_tree::index_t > TreeStateReference
const uint64_t CANONICAL_FORK_ID
std::unordered_map< MerkleTreeId, TreeStateReference > StateReference
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
SERIALIZATION_FIELDS(low_leaf_witness_data, sorted_leaves, subtree_path)
std::vector< std::pair< LeafValueType, index_t > > sorted_leaves
crypto::merkle_tree::fr_sibling_path subtree_path
std::shared_ptr< Fork > SharedPtr
SERIALIZATION_FIELDS(low_leaf_witness_data, insertion_witness_data)
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > insertion_witness_data
block_number_t blockNumber
bool is_historical() const
std::shared_ptr< WorldStateStores > Ptr