Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
contract_instance_manager.cpp
Go to the documentation of this file.
2
6
7namespace bb::avm2::simulation {
8
22
35{
36 // If the instance is found, we validate that with a nullifier check, perform address derivation, and update
37 // checking. If it is not found, we validate its NON-membership with a nullifier check, and skip the rest.
38 // Note: this call to get_contract_instance performs address derivation.
40
41 const auto& tree_state = merkle_db.get_tree_state();
42
43 // Check if this is a protocol contract address (addresses 1 to MAX_PROTOCOL_CONTRACTS).
44 // Protocol contracts are special reserved addresses that don't require nullifier checks.
46 // Handle protocol contract addresses.
47 // The derived_address lookup returns nullopt if this protocol contract slot is empty.
48 // NOTE: MAX_PROTOCOL_CONTRACTS (currently 11) is the reserved capacity, but not all
49 // slots may be filled. For example, addresses 1-6 are currently used while 7-11 are
50 // empty (reserved for future protocol contracts).
52
53 // Sanity check: if we found a derived address, we should also have the instance, and vice versa.
54 BB_ASSERT_EQ(derived_address.has_value(),
55 maybe_instance.has_value(),
56 "Derived address should be found if the instance was retrieved and vice versa");
57
58 event_emitter.emit({
59 .address = contract_address,
60 .contract_instance = maybe_instance.value_or(ContractInstance{}),
61 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
62 .public_data_tree_root = tree_state.public_data_tree.tree.root,
63 .exists = derived_address.has_value(),
64 .is_protocol_contract = true,
65 });
66 return maybe_instance;
67 }
68
70 // Emit error event
71 event_emitter.emit({
72 .address = contract_address,
73 .contract_instance = {}, // Empty instance for error case
74 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
75 .public_data_tree_root = tree_state.public_data_tree.tree.root,
76 .deployment_nullifier = contract_address,
77 .exists = false, // Nullifier not found!
78 });
79
80 return std::nullopt;
81 }
82
83 BB_ASSERT(maybe_instance.has_value(), "Contract instance should be found if nullifier exists");
84 const ContractInstance& instance = maybe_instance.value();
85
86 // Validate that the contract instance is the latest if there have been any updates.
87 update_check.check_current_class_id(contract_address, instance);
88
89 event_emitter.emit({
90 .address = contract_address,
91 .contract_instance = instance,
92 // Tree context
93 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
94 .public_data_tree_root = tree_state.public_data_tree.tree.root,
95 .deployment_nullifier = contract_address, // Contract address nullifier
96 .exists = true, // Nullifier found!
97 });
98
99 return instance;
100}
101
102} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:93
std::shared_ptr< Napi::ThreadSafeFunction > instance
#define MAX_PROTOCOL_CONTRACTS
#define CONTRACT_INSTANCE_REGISTRY_CONTRACT_ADDRESS
StrictMock< MockHighLevelMerkleDB > merkle_db
StrictMock< MockContractDB > contract_db
virtual std::optional< ContractInstance > get_contract_instance(const AztecAddress &address) const =0
ContractInstanceManager(ContractDBInterface &contract_db, HighLevelMerkleDBInterface &merkle_db, UpdateCheckInterface &update_check, FieldGreaterThanInterface &ff_gt, const ProtocolContracts &protocol_contracts, EventEmitterInterface< ContractInstanceRetrievalEvent > &event_emitter)
std::optional< ContractInstance > get_contract_instance(const FF &contract_address) override
Retrieves a contract instance from the contract database.
EventEmitterInterface< ContractInstanceRetrievalEvent > & event_emitter
virtual bool nullifier_exists(const AztecAddress &contract_address, const FF &nullifier) const =0
virtual TreeStates get_tree_state() const =0
EventEmitter< DataCopyEvent > event_emitter
std::optional< AztecAddress > get_derived_address(const ProtocolContracts &protocol_contracts, const AztecAddress &canonical_address)
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13