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
5
6namespace bb::avm2::simulation {
7
31
49{
50 // If the instance is found, we validate that with a nullifier check, perform address derivation, and update
51 // checking. If it is not found, we validate its NON-membership with a nullifier check, and skip the rest.
52 // Note: this call to get_contract_instance performs address derivation.
53 std::optional<ContractInstance> maybe_instance = contract_db.get_contract_instance(contract_address);
54
55 const auto& tree_state = merkle_db.get_tree_state();
56
57 // Check if this is a protocol contract address (addresses 1 to MAX_PROTOCOL_CONTRACTS).
58 // Protocol contracts are special reserved addresses that don't require nullifier checks.
59 if (ff_gt.ff_gt(MAX_PROTOCOL_CONTRACTS, contract_address - 1)) {
60 // Handle protocol contract addresses.
61 // The derived_address lookup returns nullopt if this protocol contract slot is empty.
62 // NOTE: MAX_PROTOCOL_CONTRACTS (currently 11) is the reserved capacity, but not all
63 // slots may be filled. For example, addresses 1-6 are currently used while 7-11 are
64 // empty (reserved for future protocol contracts).
65 std::optional<AztecAddress> derived_address = get_derived_address(protocol_contracts, contract_address);
66
67 // Sanity check: if we found a derived address, we should also have the instance, and vice versa.
68 BB_ASSERT_EQ(derived_address.has_value(),
69 maybe_instance.has_value(),
70 "Derived address should be found if the instance was retrieved and vice versa");
71
72 event_emitter.emit({
73 .address = contract_address,
74 .contract_instance = maybe_instance.value_or(ContractInstance{}),
75 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
76 .public_data_tree_root = tree_state.public_data_tree.tree.root,
77 .exists = derived_address.has_value(),
78 .is_protocol_contract = true,
79 });
80 return maybe_instance;
81 }
82
84 // Emit error event
85 event_emitter.emit({
86 .address = contract_address,
87 .contract_instance = {}, // Empty instance for error case
88 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
89 .public_data_tree_root = tree_state.public_data_tree.tree.root,
90 .deployment_nullifier = contract_address,
91 .exists = false, // Nullifier not found!
92 });
93
94 return std::nullopt;
95 }
96
97 BB_ASSERT(maybe_instance.has_value(), "Contract instance should be found if nullifier exists");
98 const ContractInstance& instance = maybe_instance.value();
99
100 // Validate that the contract instance is the latest if there have been any updates.
101 update_check.check_current_class_id(contract_address, instance);
102
103 event_emitter.emit({
104 .address = contract_address,
105 .contract_instance = instance,
106 // Tree context
107 .nullifier_tree_root = tree_state.nullifier_tree.tree.root,
108 .public_data_tree_root = tree_state.public_data_tree.tree.root,
109 .deployment_nullifier = contract_address, // Contract address nullifier
110 .exists = true, // Nullifier found!
111 });
112
113 return instance;
114}
115
116} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
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)
Construct a ContractInstanceManager.
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
AVM range check gadget for witness generation.
AvmFlavorSettings::FF FF
Definition field.hpp:10
std::optional< AztecAddress > get_derived_address(const ProtocolContracts &protocol_contracts, const AztecAddress &canonical_address)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13