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
15
16namespace bb::avm2::simulation {
17
19{
20 auto cost_in_kb = [&]() {
21 size_t total_size = 0;
22 for (const auto& instruction : std::ranges::views::values(instruction_cache)) {
23 total_size += instruction.operands.size() * sizeof(Operand);
24 }
25 return total_size / 1024;
26 };
27 vinfo("PureTxBytecodeManager held ",
28 instruction_cache.size(),
29 " instructions in cache, totaling ~",
30 cost_in_kb(),
31 " kB.");
32}
33
52{
53 BB_BENCH_NAME("PureTxBytecodeManager::get_bytecode");
54
55 // Use shared ContractInstanceManager for contract instance retrieval and validation
56 // This handles nullifier checks, address derivation, and update validation
58
59 if (!maybe_instance.has_value()) {
60 vinfo("Contract ", field_to_string(address), " is not deployed!");
61 throw BytecodeRetrievalError("Contract " + field_to_string(address) + " is not deployed");
62 }
63
64 ContractInstance instance = maybe_instance.value();
65 ContractClassId current_class_id = instance.current_contract_class_id;
66
67 bool is_new_class = !retrieved_class_ids.contains(current_class_id);
68 size_t retrieved_bytecodes_count = retrieved_class_ids.size();
69
70 if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
71 throw BytecodeRetrievalError("Can't retrieve more than " +
73 " bytecodes per tx");
74 }
75
77
78 // For fast simulation, we use the class_id as the bytecode_id instead of computing the
79 // expensive bytecode commitment hash. This is safe because class_id uniquely identifies
80 // the bytecode. The actual commitment is only needed for trace generation / witgen.
81 BytecodeId bytecode_id = current_class_id;
82
83 // Check if we've already processed this class id.
84 // NOTE: If two different classes have the same bytecode, we cannot deduplicate them.
85 // This is the downside of using the class id as the bytecode id.
86 if (bytecodes.contains(bytecode_id)) {
87 return bytecode_id;
88 }
89
90 // Contract class retrieval and class ID validation
91
93 // Note: we don't need to silo and check the class id because the deployer contract guarantees
94 // that if a contract instance exists, the class has been registered.
95 BB_ASSERT(maybe_klass.has_value(), "Contract class not found");
96 auto& klass = maybe_klass.value();
97 debug("Bytecode for ", address, " successfully retrieved!");
98
99 // We now save the bytecode against the class id so that we don't repeat this process for the same class.
100 bytecodes[bytecode_id] = std::make_shared<std::vector<uint8_t>>(std::move(klass.packed_bytecode));
101
102 return bytecode_id;
103}
104
106{
107 // The corresponding bytecode is already stored in the cache if we call this routine. This is safe-guarded by the
108 // fact that it is added in the cache when we retrieve the bytecode_id.
109 return read_instruction(bytecode_id, get_bytecode_data(bytecode_id), pc);
110}
111
113 std::shared_ptr<std::vector<uint8_t>> bytecode_ptr,
114 PC pc)
115{
116 BB_BENCH_NAME("TxBytecodeManager::read_instruction");
117
118 // Try to get the instruction from the cache.
119 InstructionIdentifier instruction_identifier = { bytecode_ptr.get(), pc };
120 auto it = instruction_cache.find(instruction_identifier);
121 if (it != instruction_cache.end()) {
122 return it->second;
123 }
124
125 // If not found, deserialize the instruction, etc.
126 const auto& bytecode = *bytecode_ptr;
128
129 try {
131 } catch (const InstrDeserializationError& error) {
132 std::string error_msg = format("Instruction fetching error at pc ", pc);
133 if (error.message.has_value()) {
134 error_msg = format(error_msg, ": ", error.message.value());
135 }
136 throw InstructionFetchingError(error_msg);
137 }
138
139 // If the following code is executed, no error was thrown in deserialize_instruction().
140 if (!check_tag(instruction)) {
141 std::string error_msg = format("Instruction fetching error at pc ", pc, ": Tag check failed");
142 throw InstructionFetchingError(error_msg);
143 };
144
145 // Save the instruction to the cache.
146 instruction_cache.emplace(instruction_identifier, instruction);
147 return instruction;
148}
149
151{
152 auto it = bytecodes.find(bytecode_id);
153 BB_ASSERT_DEBUG(it != bytecodes.end(), "Bytecode not found for the given bytecode_id");
154 return it->second;
155}
156
157} // 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