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
38{
39 BB_BENCH_NAME("PureTxBytecodeManager::get_bytecode");
40
41 // Use shared ContractInstanceManager for contract instance retrieval and validation
42 // This handles nullifier checks, address derivation, and update validation
44
45 if (!maybe_instance.has_value()) {
46 vinfo("Contract ", field_to_string(address), " is not deployed!");
47 throw BytecodeRetrievalError("Contract " + field_to_string(address) + " is not deployed");
48 }
49
50 ContractInstance instance = maybe_instance.value();
51 ContractClassId current_class_id = instance.current_contract_class_id;
52
53 bool is_new_class = !retrieved_class_ids.contains(current_class_id);
54 size_t retrieved_bytecodes_count = retrieved_class_ids.size();
55
56 if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
57 throw BytecodeRetrievalError("Can't retrieve more than " +
59 " bytecodes per tx");
60 }
61
63
64 // For fast simulation, we use the class_id as the bytecode_id instead of computing the
65 // expensive bytecode commitment hash. This is safe because class_id uniquely identifies
66 // the bytecode. The actual commitment is only needed for trace generation / witgen.
67 BytecodeId bytecode_id = current_class_id;
68
69 // Check if we've already processed this class id.
70 // NOTE: If two different classes have the same bytecode, we cannot deduplicate them.
71 // This is the downside of using the class id as the bytecode id.
72 if (bytecodes.contains(bytecode_id)) {
73 return bytecode_id;
74 }
75
76 // Contract class retrieval and class ID validation
78 // Note: we don't need to silo and check the class id because the deployer contract guarantees
79 // that if a contract instance exists, the class has been registered.
80 assert(maybe_klass.has_value());
81 auto& klass = maybe_klass.value();
82 debug("Bytecode for ", address, " successfully retrieved!");
83
84 // We now save the bytecode so that we don't repeat this process.
85 bytecodes[bytecode_id] = std::make_shared<std::vector<uint8_t>>(std::move(klass.packed_bytecode));
86 return bytecode_id;
87}
88
90{
91 // The corresponding bytecode is already stored in the cache if we call this routine. This is safe-guarded by the
92 // fact that it is added in the cache when we retrieve the bytecode_id.
93 return read_instruction(bytecode_id, get_bytecode_data(bytecode_id), pc);
94}
95
97 std::shared_ptr<std::vector<uint8_t>> bytecode_ptr,
98 uint32_t pc)
99{
100 BB_BENCH_NAME("TxBytecodeManager::read_instruction");
101
102 // Try to get the instruction from the cache.
103 InstructionIdentifier instruction_identifier = { bytecode_ptr.get(), pc };
104 auto it = instruction_cache.find(instruction_identifier);
105 if (it != instruction_cache.end()) {
106 return it->second;
107 }
108
109 // If not found, deserialize the instruction, etc.
110 const auto& bytecode = *bytecode_ptr;
112
113 try {
115 } catch (const InstrDeserializationError& error) {
116 std::string error_msg = format("Instruction fetching error at pc ", pc);
117 if (error.message.has_value()) {
118 error_msg = format(error_msg, ": ", error.message.value());
119 }
120 throw InstructionFetchingError(error_msg);
121 }
122
123 // If the following code is executed, no error was thrown in deserialize_instruction().
124 if (!check_tag(instruction)) {
125 std::string error_msg = format("Instruction fetching error at pc ", pc, ": Tag check failed");
126 throw InstructionFetchingError(error_msg);
127 };
128
129 // Save the instruction to the cache.
130 instruction_cache.emplace(instruction_identifier, instruction);
131 return instruction;
132}
133
138
139} // namespace bb::avm2::simulation
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:219
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
Instruction read_instruction(const BytecodeId &bytecode_id, uint32_t pc) override
BytecodeId get_bytecode(const AztecAddress &address) override
unordered_flat_map< BytecodeId, std::shared_ptr< std::vector< uint8_t > > > bytecodes
unordered_flat_set< ContractClassId > retrieved_class_ids
std::tuple< void *, uint32_t > InstructionIdentifier
unordered_flat_map< InstructionIdentifier, Instruction > instruction_cache
std::string format(Args... args)
Definition log.hpp:22
#define vinfo(...)
Definition log.hpp:80
#define debug(...)
Definition log.hpp:62
Instruction instruction
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...
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