Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pure_bytecode_manager.cpp
Go to the documentation of this file.
2
3#include <cassert>
4
18
19namespace bb::avm2::simulation {
20
22{
23 auto cost_in_kb = [&]() {
24 size_t total_size = 0;
25 for (const auto& instruction : std::ranges::views::values(instruction_cache)) {
26 total_size += instruction.operands.size() * sizeof(Operand);
27 }
28 return total_size / 1024;
29 };
30 vinfo("PureTxBytecodeManager held ",
31 instruction_cache.size(),
32 " instructions in cache, totaling ~",
33 cost_in_kb(),
34 " kB.");
35}
36
55{
56 BB_BENCH_NAME("PureTxBytecodeManager::get_bytecode");
57
58 // Use shared ContractInstanceManager for contract instance retrieval and validation
59 // This handles nullifier checks, address derivation, and update validation
61
62 if (!maybe_instance.has_value()) {
63 vinfo("Contract ", field_to_string(address), " is not deployed!");
64 throw BytecodeRetrievalError("Contract " + field_to_string(address) + " is not deployed");
65 }
66
67 ContractInstance instance = maybe_instance.value();
68 ContractClassId current_class_id = instance.current_contract_class_id;
69
70 bool is_new_class = !retrieved_class_ids.contains(current_class_id);
71 size_t retrieved_bytecodes_count = retrieved_class_ids.size();
72
73 if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
74 throw BytecodeRetrievalError("Can't retrieve more than " +
76 " bytecodes per tx");
77 }
78
80
81 // For fast simulation, we use the class_id as the bytecode_id instead of computing the
82 // expensive bytecode commitment hash. This is safe because class_id uniquely identifies
83 // the bytecode. The actual commitment is only needed for trace generation / witgen.
84 BytecodeId bytecode_id = current_class_id;
85
86 // Check if we've already processed this class id.
87 // NOTE: If two different classes have the same bytecode, we cannot deduplicate them.
88 // This is the downside of using the class id as the bytecode id.
89 if (bytecodes.contains(bytecode_id)) {
90 return bytecode_id;
91 }
92
93 // Contract class retrieval and class ID validation
95 // Note: we don't need to silo and check the class id because the deployer contract guarantees
96 // that if a contract instance exists, the class has been registered.
97 BB_ASSERT(maybe_klass.has_value(), "Contract class not found");
98 auto& klass = maybe_klass.value();
99 debug("Bytecode for ", address, " successfully retrieved!");
100
101 // We now save the bytecode against the class id so that we don't repeat this process for the same class.
102 bytecodes[bytecode_id] = std::make_shared<std::vector<uint8_t>>(std::move(klass.packed_bytecode));
103 return bytecode_id;
104}
105
107{
108 // The corresponding bytecode is already stored in the cache if we call this routine. This is safe-guarded by the
109 // fact that it is added in the cache when we retrieve the bytecode_id.
110 return read_instruction(bytecode_id, get_bytecode_data(bytecode_id), pc);
111}
112
114 std::shared_ptr<std::vector<uint8_t>> bytecode_ptr,
115 PC pc)
116{
117 BB_BENCH_NAME("TxBytecodeManager::read_instruction");
118
119 // Try to get the instruction from the cache.
120 InstructionIdentifier instruction_identifier = { bytecode_ptr.get(), pc };
121 auto it = instruction_cache.find(instruction_identifier);
122 if (it != instruction_cache.end()) {
123 return it->second;
124 }
125
126 // If not found, deserialize the instruction, etc.
127 const auto& bytecode = *bytecode_ptr;
129
130 try {
132 } catch (const InstrDeserializationError& error) {
133 std::string error_msg = format("Instruction fetching error at pc ", pc);
134 if (error.message.has_value()) {
135 error_msg = format(error_msg, ": ", error.message.value());
136 }
137 throw InstructionFetchingError(error_msg);
138 }
139
140 // If the following code is executed, no error was thrown in deserialize_instruction().
141 if (!check_tag(instruction)) {
142 std::string error_msg = format("Instruction fetching error at pc ", pc, ": Tag check failed");
143 throw InstructionFetchingError(error_msg);
144 };
145
146 // Save the instruction to the cache.
147 instruction_cache.emplace(instruction_identifier, instruction);
148 return instruction;
149}
150
152{
153 auto it = bytecodes.find(bytecode_id);
154 BB_ASSERT_DEBUG(it != bytecodes.end(), "Bytecode not found for the given bytecode_id");
155 return it->second;
156}
157
158} // namespace bb::avm2::simulation
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
std::shared_ptr< Napi::ThreadSafeFunction > instance
std::shared_ptr< Napi::ThreadSafeFunction > bytecode
#define MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:225
virtual std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const =0
virtual std::optional< ContractInstance > get_contract_instance(const FF &contract_address)=0
Retrieve and validate a contract instance.
ContractInstanceManagerInterface & contract_instance_manager
std::shared_ptr< std::vector< uint8_t > > get_bytecode_data(const BytecodeId &bytecode_id) override
BytecodeId get_bytecode(const AztecAddress &address) override
Retrieves and validates bytecode from the PureTxBytecodeManager's ContractDBInterface.
unordered_flat_map< BytecodeId, std::shared_ptr< std::vector< uint8_t > > > bytecodes
Instruction read_instruction(const BytecodeId &bytecode_id, PC pc) override
unordered_flat_set< ContractClassId > retrieved_class_ids
unordered_flat_map< InstructionIdentifier, Instruction > instruction_cache
std::string format(Args... args)
Definition log.hpp:23
#define vinfo(...)
Definition log.hpp:94
#define debug(...)
Definition log.hpp:99
Instruction instruction
AVM range check gadget for witness generation.
bool check_tag(const Instruction &instruction)
Check whether the instruction must have a tag operand and whether the operand value is in the value t...
Instruction deserialize_instruction(std::span< const uint8_t > bytecode, size_t pos)
Parsing of an instruction in the supplied bytecode at byte position pos. This checks that the WireOpC...
uint32_t PC
FF ContractClassId
std::string field_to_string(const FF &ff)
Definition stringify.cpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
FF current_class_id