21std::string
to_string(
const TreeSnapshots& snapshots)
23 return format(
"PUBLIC_DATA_TREE: ",
24 snapshots.public_data_tree,
26 snapshots.nullifier_tree,
28 snapshots.note_hash_tree,
29 "\nL1_TO_L2_MESSAGE_TREE: ",
30 snapshots.l1_to_l2_message_tree);
37 return "PUBLIC_DATA_TREE";
39 return "NULLIFIER_TREE";
41 return "NOTE_HASH_TREE";
43 return "L1_TO_L2_MESSAGE_TREE";
58 vinfo(
"Initializing HintedRawContractDB with...",
59 "\n * contract_instances: ",
61 "\n * contract_classes: ",
63 "\n * bytecode_commitments: ",
65 "\n * debug_function_names: ",
69 contract_instances[std::make_tuple(contract_instance_hint.hint_key, contract_instance_hint.address)] =
70 contract_instance_hint;
74 contract_classes[std::make_tuple(contract_class_hint.hint_key, contract_class_hint.class_id)] =
79 bytecode_commitments[std::make_tuple(bytecode_commitment_hint.hint_key, bytecode_commitment_hint.class_id)] =
80 bytecode_commitment_hint.commitment;
85 debug_function_name_hint.name;
102 auto key = std::make_tuple(hint_key,
address);
105 vinfo(
"Contract instance not found for key (", hint_key,
", ",
address,
")");
108 const auto& contract_instance_hint = it->second;
111 .salt = contract_instance_hint.salt,
112 .deployer = contract_instance_hint.deployer,
113 .current_contract_class_id = contract_instance_hint.current_contract_class_id,
114 .original_contract_class_id = contract_instance_hint.original_contract_class_id,
115 .initialization_hash = contract_instance_hint.initialization_hash,
116 .immutables_hash = contract_instance_hint.immutables_hash,
120 .incoming_viewing_key = contract_instance_hint.public_keys.ivpk_m,
121 .outgoing_viewing_key_hash = contract_instance_hint.public_keys.ovpk_m_hash,
122 .tagging_key_hash = contract_instance_hint.public_keys.tpk_m_hash,
123 .message_signing_key_hash = contract_instance_hint.public_keys.mspk_m_hash,
124 .fallback_key_hash = contract_instance_hint.public_keys.fbpk_m_hash,
132 auto key = std::make_tuple(hint_key, class_id);
135 vinfo(
"Contract class not found for key (", hint_key,
", ", class_id,
")");
138 const auto& contract_class_hint = it->second;
142 .artifact_hash = contract_class_hint.artifact_hash,
143 .private_functions_root = contract_class_hint.private_functions_root,
144 .packed_bytecode = contract_class_hint.packed_bytecode,
151 auto key = std::make_tuple(hint_key, class_id);
154 vinfo(
"Bytecode commitment not found for key (", hint_key,
", ", class_id,
")");
172 debug(
"add_contracts called (no-op in hinted mode)");
180 const auto& hint = hint_it->second;
182 hint.old_checkpoint_id,
checkpoint_stack.top(),
"Old checkpoint id does not match the current checkpoint id");
193 const auto& hint = hint_it->second;
195 hint.old_checkpoint_id,
checkpoint_stack.top(),
"Old checkpoint id does not match the current checkpoint id");
199 hint.new_checkpoint_id,
checkpoint_stack.top(),
"New checkpoint id does not match the current checkpoint id");
208 const auto& hint = hint_it->second;
210 hint.old_checkpoint_id,
checkpoint_stack.top(),
"Old checkpoint id does not match the current checkpoint id");
214 hint.new_checkpoint_id,
checkpoint_stack.top(),
"New checkpoint id does not match the current checkpoint id");
225 : tree_roots(hints.starting_tree_roots)
229 vinfo(
"Initializing HintedRawMerkleDB with...",
230 "\n * get_sibling_path_hints: ",
232 "\n * get_previous_value_index_hints: ",
234 "\n * get_leaf_preimage_hints_public_data_tree: ",
236 "\n * get_leaf_preimage_hints_nullifier_tree: ",
238 "\n * get_leaf_value_hints: ",
240 "\n * sequential_insert_hints_public_data_tree: ",
242 "\n * sequential_insert_hints_nullifier_tree: ",
244 "\n * append_leaves_hints: ",
246 "\n * create_checkpoint_hints: ",
248 "\n * commit_checkpoint_hints: ",
250 "\n * revert_checkpoint_hints: ",
256 get_sibling_path_hint.tree_id,
257 get_sibling_path_hint.index };
263 get_previous_value_index_hint.tree_id,
264 get_previous_value_index_hint.value };
266 get_previous_value_index_hint.already_present,
267 get_previous_value_index_hint.index,
282 GetLeafValueKey key = { get_leaf_value_hint.hint_key, get_leaf_value_hint.tree_id, get_leaf_value_hint.index };
288 sequential_insert_hint.tree_id,
289 sequential_insert_hint.leaf };
295 sequential_insert_hint.tree_id,
296 sequential_insert_hint.leaf };
303 append_leaves_hint.tree_id,
304 append_leaves_hint.leaves };
337 throw std::runtime_error(
format(
"Sibling path not found for key (root: ",
340 tree_info.next_available_leaf_index,
342 get_tree_name(tree_id),
357 throw std::runtime_error(
format(
"Low indexed leaf not found for key (root: ",
360 tree_info.next_available_leaf_index,
362 get_tree_name(tree_id),
376 throw std::runtime_error(
format(
"Leaf value not found for key (root: ",
379 tree_info.next_available_leaf_index,
381 get_tree_name(tree_id),
395 throw std::runtime_error(
format(
"Leaf preimage (PUBLIC_DATA_TREE) not found for key (root: ",
398 tree_info.next_available_leaf_index,
412 throw std::runtime_error(
format(
"Leaf preimage (NULLIFIER_TREE) not found for key (root: ",
415 tree_info.next_available_leaf_index,
430 throw std::runtime_error(
format(
"Sequential insert hint (PUBLIC_DATA_TREE) not found for key (root: ",
433 tree_info.next_available_leaf_index,
438 const auto& hint = it->second;
444 hint.low_leaves_witness_data.leaf, hint.low_leaves_witness_data.index, hint.low_leaves_witness_data.path);
448 hint.insertion_witness_data.leaf, hint.insertion_witness_data.index, hint.insertion_witness_data.path);
453 debug(
"Evolved state of PUBLIC_DATA_TREE: ",
469 throw std::runtime_error(
format(
"Sequential insert hint (NULLIFIER_TREE) not found for key (root: ",
472 tree_info.next_available_leaf_index,
477 const auto& hint = it->second;
483 hint.low_leaves_witness_data.leaf, hint.low_leaves_witness_data.index, hint.low_leaves_witness_data.path);
487 hint.insertion_witness_data.leaf, hint.insertion_witness_data.index, hint.insertion_witness_data.path);
492 debug(
"Evolved state of NULLIFIER_TREE: ",
505 throw std::runtime_error(
508 const auto& hint = it->second;
512 throw std::runtime_error(
format(
"[create_checkpoint@",
514 "] Old checkpoint id does not match the current checkpoint id: ",
515 hint.old_checkpoint_id,
520 debug(
"[create_checkpoint@",
522 "] Checkpoint evolved ",
523 hint.old_checkpoint_id,
525 hint.new_checkpoint_id);
535 throw std::runtime_error(
538 const auto& hint = it->second;
542 throw std::runtime_error(
format(
"[commit_checkpoint@",
544 "] Old checkpoint id does not match the current checkpoint id: ",
545 hint.old_checkpoint_id,
554 throw std::runtime_error(
format(
"[commit_checkpoint@",
556 "] New checkpoint id does not match the current checkpoint id: ",
557 hint.new_checkpoint_id,
562 debug(
"[commit_checkpoint@",
564 "] Checkpoint evolved ",
565 hint.old_checkpoint_id,
567 hint.new_checkpoint_id);
576 throw std::runtime_error(
579 const auto& hint = it->second;
583 throw std::runtime_error(
format(
"[revert_checkpoint@",
585 "] Old checkpoint id does not match the current checkpoint id: ",
586 hint.old_checkpoint_id,
595 throw std::runtime_error(
format(
"[revert_checkpoint@",
597 "] Hint tree snapshots do not match the current tree roots."));
604 throw std::runtime_error(
format(
"[revert_checkpoint@",
606 "] New checkpoint id does not match the current checkpoint id: ",
607 hint.new_checkpoint_id,
615 debug(
"[revert_checkpoint@",
617 "] Checkpoint evolved ",
618 hint.old_checkpoint_id,
620 hint.new_checkpoint_id);
631 throw std::runtime_error(
format(
"Append leaves hint not found for key (root: ",
634 tree_info.next_available_leaf_index,
636 get_tree_name(tree_id),
638 std::vector<FF>(leaves.begin(), leaves.end()),
647 debug(
"Evolved state of NOTE_HASH_TREE: ",
655 debug(
"Evolved state of L1_TO_L2_MESSAGE_TREE: ",
662 throw std::runtime_error(
"append_leaves is only supported for NOTE_HASH_TREE and L1_TO_L2_MESSAGE_TREE");
669 auto size_before = tree_info.next_available_leaf_index;
671 tree_info.next_available_leaf_index += num_leaves;
673 debug(
"Padded tree ",
674 get_tree_name(tree_id),
678 tree_info.next_available_leaf_index);
700 .next_available_leaf_index = l1_to_l2_info.meta.size },
702 .next_available_leaf_index = note_hash_info.meta.size },
704 .next_available_leaf_index = nullifier_info.meta.size },
706 .next_available_leaf_index = public_data_info.meta.size },
713 return tree_snapshots;
730 if (!res.has_value()) {
731 throw std::runtime_error(
732 format(
"Invalid get_leaf_value request",
static_cast<uint64_t
>(tree_id),
" for index ", leaf_index));
742 if (!res.has_value()) {
743 throw std::runtime_error(
format(
"Invalid get_leaf_preimage_public_data_tree request for index ", leaf_index));
753 if (!res.has_value()) {
754 throw std::runtime_error(
format(
"Invalid get_leaf_preimage_nullifier_tree request for index ", leaf_index));
809 case MerkleTreeId::NULLIFIER_TREE: {
815 case MerkleTreeId::NOTE_HASH_TREE: {
816 std::vector<FF> padding_leaves(num_leaves,
FF(0));
821 throw std::runtime_error(
"Padding not supported for tree " +
std::to_string(
static_cast<uint64_t
>(tree_id)));
#define BB_ASSERT(expression,...)
#define BB_ASSERT_EQ(actual, expected,...)
#define NULLIFIER_SUBTREE_HEIGHT
#define BB_BENCH_NAME(name)
HintedRawContractDB(const ExecutionHints &hints)
uint32_t get_checkpoint_id() const
std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const override
std::stack< uint32_t > checkpoint_stack
unordered_flat_map< uint32_t, ContractDBRevertCheckpointHint > revert_checkpoint_hints
unordered_flat_map< GetBytecodeCommitmentKey, FF > bytecode_commitments
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
unordered_flat_map< uint32_t, ContractDBCreateCheckpointHint > create_checkpoint_hints
void create_checkpoint() override
unordered_flat_map< GetContractInstanceKey, ContractInstanceHint > contract_instances
void add_contracts(const ContractDeploymentData &contract_deployment_data) override
unordered_flat_map< uint32_t, ContractDBCommitCheckpointHint > commit_checkpoint_hints
unordered_flat_map< GetContractClassKey, ContractClassHint > contract_classes
void commit_checkpoint() override
void revert_checkpoint() override
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
unordered_flat_map< GetDebugFunctionNameKey, std::string > debug_function_names
void create_checkpoint() override
unordered_flat_map< GetLeafValueKey, FF > get_leaf_value_hints
unordered_flat_map< uint32_t, RevertCheckpointHint > revert_checkpoint_hints
unordered_flat_map< SequentialInsertHintNullifierTreeKey, SequentialInsertHint< NullifierLeafValue > > sequential_insert_hints_nullifier_tree
GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF &value) const override
unordered_flat_map< AppendLeavesHintKey, AppendOnlyTreeSnapshot > append_leaves_hints
std::stack< uint32_t > checkpoint_stack
SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const override
unordered_flat_map< uint32_t, CommitCheckpointHint > commit_checkpoint_hints
unordered_flat_map< GetSiblingPathKey, SiblingPath > get_sibling_path_hints
SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value) override
unordered_flat_map< uint32_t, CreateCheckpointHint > create_checkpoint_hints
void commit_checkpoint() override
void revert_checkpoint() override
IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const override
IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const override
FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const override
unordered_flat_map< GetLeafPreimageKey, IndexedLeaf< NullifierLeafValue > > get_leaf_preimage_hints_nullifier_tree
unordered_flat_map< SequentialInsertHintPublicDataTreeKey, SequentialInsertHint< PublicDataLeafValue > > sequential_insert_hints_public_data_tree
HintedRawMerkleDB(const ExecutionHints &hints)
unordered_flat_map< GetLeafPreimageKey, IndexedLeaf< PublicDataLeafValue > > get_leaf_preimage_hints_public_data_tree
uint32_t get_checkpoint_id() const override
uint32_t checkpoint_action_counter
SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value) override
unordered_flat_map< GetPreviousValueIndexKey, GetLowIndexedLeafResponse > get_previous_value_index_hints
const AppendOnlyTreeSnapshot & get_tree_info(MerkleTreeId tree_id) const
void pad_tree(MerkleTreeId tree_id, size_t num_leaves) override
void append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves) override
world_state::WorldStateRevision ws_revision
IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const override
SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const override
void revert_checkpoint() override
void append_leaves(MerkleTreeId tree_id, std::span< const FF > leaves) override
world_state::WorldState & ws_instance
IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const override
void commit_checkpoint() override
GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF &value) const override
std::optional< TreeSnapshots > cached_tree_snapshots
void create_checkpoint() override
std::stack< uint32_t > checkpoint_stack
TreeSnapshots get_tree_roots() const override
void pad_tree(MerkleTreeId tree_id, size_t num_leaves) override
SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value) override
void throw_if_cancelled() const
FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const override
SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value) override
uint32_t get_checkpoint_id() const override
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.
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.
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)
void revert_checkpoint(const uint64_t &forkId)
crypto::merkle_tree::TreeMetaResponse get_tree_info(const WorldStateRevision &revision, MerkleTreeId tree_id) const
Get tree metadata for a particular tree.
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 commit_checkpoint(const uint64_t &forkId)
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.
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.
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...
std::string format(Args... args)
auto & get_tree_info_helper(world_state::MerkleTreeId tree_id, auto &tree_roots)
AVM range check gadget for witness generation.
::bb::crypto::merkle_tree::fr_sibling_path SiblingPath
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, index_t > GetSiblingPathKey
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, index_t > GetLeafValueKey
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, NullifierLeafValue > SequentialInsertHintNullifierTreeKey
std::tuple< AppendOnlyTreeSnapshot, index_t > GetLeafPreimageKey
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, PublicDataLeafValue > SequentialInsertHintPublicDataTreeKey
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, FF > GetPreviousValueIndexKey
std::tuple< AppendOnlyTreeSnapshot, MerkleTreeId, std::vector< FF > > AppendLeavesHintKey
::bb::crypto::merkle_tree::index_t index_t
std::string to_string(const std::array< FF, N > &arr)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::string to_string(bb::avm2::ValueTag tag)
uint64_t next_available_leaf_index
std::vector< SequentialInsertHint< crypto::merkle_tree::NullifierLeafValue > > sequential_insert_hints_nullifier_tree
std::vector< GetSiblingPathHint > get_sibling_path_hints
std::vector< DebugFunctionNameHint > debug_function_names
std::vector< ContractDBCreateCheckpointHint > contract_db_create_checkpoint_hints
std::vector< ContractDBCommitCheckpointHint > contract_db_commit_checkpoint_hints
std::vector< CommitCheckpointHint > commit_checkpoint_hints
std::vector< SequentialInsertHint< crypto::merkle_tree::PublicDataLeafValue > > sequential_insert_hints_public_data_tree
std::vector< RevertCheckpointHint > revert_checkpoint_hints
std::vector< ContractDBRevertCheckpointHint > contract_db_revert_checkpoint_hints
std::vector< GetPreviousValueIndexHint > get_previous_value_index_hints
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::PublicDataLeafValue > > > get_leaf_preimage_hints_public_data_tree
std::vector< GetLeafPreimageHint< crypto::merkle_tree::IndexedLeaf< crypto::merkle_tree::NullifierLeafValue > > > get_leaf_preimage_hints_nullifier_tree
std::vector< CreateCheckpointHint > create_checkpoint_hints
std::vector< GetLeafValueHint > get_leaf_value_hints
std::vector< AppendLeavesHint > append_leaves_hints
std::vector< ContractInstanceHint > contract_instances
std::vector< ContractClassHint > contract_classes
std::vector< BytecodeCommitmentHint > bytecode_commitments
AppendOnlyTreeSnapshot public_data_tree
AppendOnlyTreeSnapshot l1_to_l2_message_tree
AppendOnlyTreeSnapshot nullifier_tree
AppendOnlyTreeSnapshot note_hash_tree
static NullifierLeafValue empty()
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > low_leaf_witness_data
std::vector< crypto::merkle_tree::LeafUpdateWitnessData< LeafValueType > > insertion_witness_data