Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstdint>
5#include <memory>
6#include <span>
7#include <vector>
8
22
23namespace bb::avm2::simulation {
24
25// The context for a single nested call.
27 public:
62
63 uint32_t get_last_child_id() const override;
64
65 // Having getters and setters make it easier to mock the context.
66 // Machine state.
67 MemoryInterface& get_memory() override { return *memory; }
68 const MemoryInterface& get_memory() const override { return *memory; }
74
75 uint32_t get_pc() const override { return pc; }
76 void set_pc(uint32_t new_pc) override { pc = new_pc; }
77 uint32_t get_next_pc() const override { return next_pc; }
78 void set_next_pc(uint32_t new_next_pc) override { next_pc = new_next_pc; }
79 bool halted() const override { return has_halted; }
80 void halt() override { has_halted = true; }
81
82 uint32_t get_context_id() const override { return context_id; }
83
84 // Environment.
85 const AztecAddress& get_address() const override { return address; }
86 const AztecAddress& get_msg_sender() const override { return msg_sender; }
87 const FF& get_transaction_fee() const override { return transaction_fee; }
88 bool get_is_static() const override { return is_static; }
90
91 TransactionPhase get_phase() const override { return phase; }
92
97 const GlobalVariables& get_globals() const override { return globals; }
98
100 const ContextInterface& get_child_context() const override { return *child_context; }
102 {
103 child_context = std::move(child_ctx);
104 }
105
107 void set_last_rd_addr(MemoryAddress rd_addr) override { last_child_rd_addr = rd_addr; }
108
109 uint32_t get_last_rd_size() const override { return last_child_rd_size; }
110 void set_last_rd_size(MemoryAddress rd_size) override { last_child_rd_size = rd_size; }
111
112 bool get_last_success() const override { return last_child_success; }
113 void set_last_success(bool success) override { last_child_success = success; }
114
115 Gas get_gas_used() const override { return gas_used; }
116 Gas get_gas_limit() const override { return gas_limit; }
117
118 Gas gas_left() const override { return gas_limit - gas_used; }
119
120 void set_gas_used(Gas gas_used) override { this->gas_used = gas_used; }
121
122 uint32_t get_checkpoint_id_at_creation() const override { return checkpoint_id_at_creation; }
123
124 // Input / Output
125 std::vector<MemoryValue> get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) const override;
126
127 protected:
129 uint32_t checkpoint_id_at_creation; // DB id when the context was created.
133
134 private:
135 // Environment.
141
142 uint32_t context_id;
143
144 // Machine state.
145 uint32_t pc = 0;
146 uint32_t next_pc = 0;
147 bool has_halted = false;
153
154 // Output
158 bool last_child_success = false;
159
161};
162
163// TODO(ilyas): move to cpp file
165 public:
206
207 uint32_t get_parent_id() const override { return 0; } // No parent context for the top-level context.
208 bool has_parent() const override { return false; }
209 // Event Emitting
211
212 // Input / Output
213 std::vector<MemoryValue> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
214
215 Gas get_parent_gas_used() const override { return Gas{}; }
216 Gas get_parent_gas_limit() const override { return Gas{}; }
217
218 MemoryAddress get_parent_cd_addr() const override { return 0; }
219 uint32_t get_parent_cd_size() const override { return static_cast<uint32_t>(calldata.size()); }
220
221 private:
223};
224
225// Parameters for a nested call need to be changed
227 public:
266
267 uint32_t get_parent_id() const override { return parent_context.get_context_id(); }
268 bool has_parent() const override { return true; }
269
270 Gas get_parent_gas_used() const override { return parent_context.get_gas_used(); }
272
273 // Event Emitting
275
276 // Input / Output
277 std::vector<MemoryValue> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
278
280 uint32_t get_parent_cd_size() const override { return parent_cd_size; }
281
282 private:
283 // These are direct addresses to look up into the parent context during calldata copying
286
288};
289
290} // namespace bb::avm2::simulation
const ContextInterface & get_child_context() const override
Definition context.hpp:100
const MemoryInterface & get_memory() const override
Definition context.hpp:68
void set_last_success(bool success) override
Definition context.hpp:113
const AztecAddress & get_address() const override
Definition context.hpp:85
TransactionPhase get_phase() const override
Definition context.hpp:91
InternalCallStackManagerInterface & get_internal_call_stack_manager() override
Definition context.hpp:70
void set_gas_used(Gas gas_used) override
Definition context.hpp:120
MemoryAddress get_last_rd_addr() const override
Definition context.hpp:106
SideEffectTrackerInterface & get_side_effect_tracker() override
Definition context.hpp:89
bool halted() const override
Definition context.hpp:79
RetrievedBytecodesTreeCheckInterface & retrieved_bytecodes_tree
Definition context.hpp:131
void set_next_pc(uint32_t new_next_pc) override
Definition context.hpp:78
uint32_t get_last_rd_size() const override
Definition context.hpp:109
Gas get_gas_used() const override
Definition context.hpp:115
void set_last_rd_size(MemoryAddress rd_size) override
Definition context.hpp:110
const AztecAddress & get_msg_sender() const override
Definition context.hpp:86
AppendOnlyTreeSnapshot get_written_public_data_slots_tree_snapshot() override
Definition context.hpp:93
ContextInterface & get_child_context() override
Definition context.hpp:99
uint32_t get_checkpoint_id_at_creation() const override
Definition context.hpp:122
WrittenPublicDataSlotsTreeCheckInterface & written_public_data_slots_tree
Definition context.hpp:130
uint32_t get_next_pc() const override
Definition context.hpp:77
std::unique_ptr< ContextInterface > child_context
Definition context.hpp:155
void set_child_context(std::unique_ptr< ContextInterface > child_ctx) override
Definition context.hpp:101
std::vector< MemoryValue > get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) const override
Definition context.cpp:15
BaseContext(uint32_t context_id, AztecAddress address, AztecAddress msg_sender, FF transaction_fee, bool is_static, Gas gas_limit, Gas gas_used, GlobalVariables globals, std::unique_ptr< BytecodeManagerInterface > bytecode, std::unique_ptr< MemoryInterface > memory, std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager, HighLevelMerkleDBInterface &merkle_db, WrittenPublicDataSlotsTreeCheckInterface &written_public_data_slots_tree, RetrievedBytecodesTreeCheckInterface &retrieved_bytecodes_tree, SideEffectTrackerInterface &side_effect_tracker, TransactionPhase phase)
Definition context.hpp:28
uint32_t get_context_id() const override
Definition context.hpp:82
uint32_t get_last_child_id() const override
Definition context.cpp:36
MemoryInterface & get_memory() override
Definition context.hpp:67
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:69
bool get_last_success() const override
Definition context.hpp:112
std::unique_ptr< MemoryInterface > memory
Definition context.hpp:151
SideEffectTrackerInterface & side_effect_tracker
Definition context.hpp:132
void set_last_rd_addr(MemoryAddress rd_addr) override
Definition context.hpp:107
Gas get_gas_limit() const override
Definition context.hpp:116
std::unique_ptr< BytecodeManagerInterface > bytecode
Definition context.hpp:150
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:128
const FF & get_transaction_fee() const override
Definition context.hpp:87
uint32_t get_pc() const override
Definition context.hpp:75
Gas gas_left() const override
Definition context.hpp:118
std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager
Definition context.hpp:152
void set_pc(uint32_t new_pc) override
Definition context.hpp:76
bool get_is_static() const override
Definition context.hpp:88
const GlobalVariables & get_globals() const override
Definition context.hpp:97
virtual Gas get_gas_used() const =0
virtual Gas get_gas_limit() const =0
virtual uint32_t get_context_id() const =0
std::vector< MemoryValue > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:47
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:218
uint32_t get_parent_id() const override
Definition context.hpp:207
ContextEvent serialize_context_event() override
Definition context.cpp:65
EnqueuedCallContext(uint32_t context_id, AztecAddress address, AztecAddress msg_sender, FF transaction_fee, bool is_static, Gas gas_limit, Gas gas_used, GlobalVariables globals, std::unique_ptr< BytecodeManagerInterface > bytecode, std::unique_ptr< MemoryInterface > memory, std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager, HighLevelMerkleDBInterface &merkle_db, WrittenPublicDataSlotsTreeCheckInterface &written_public_data_slots_tree, RetrievedBytecodesTreeCheckInterface &retrieved_bytecodes_tree, SideEffectTrackerInterface &side_effect_tracker, TransactionPhase phase, std::span< const FF > calldata)
Definition context.hpp:166
std::vector< MemoryValue > calldata
Definition context.hpp:222
Gas get_parent_gas_limit() const override
Definition context.hpp:216
uint32_t get_parent_cd_size() const override
Definition context.hpp:219
uint32_t get_parent_id() const override
Definition context.hpp:267
uint32_t get_parent_cd_size() const override
Definition context.hpp:280
ContextEvent serialize_context_event() override
Definition context.cpp:131
std::vector< MemoryValue > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:108
bool has_parent() const override
Definition context.hpp:268
Gas get_parent_gas_used() const override
Definition context.hpp:270
NestedContext(uint32_t context_id, AztecAddress address, AztecAddress msg_sender, FF transaction_fee, bool is_static, Gas gas_limit, GlobalVariables globals, std::unique_ptr< BytecodeManagerInterface > bytecode, std::unique_ptr< MemoryInterface > memory, std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager, HighLevelMerkleDBInterface &merkle_db, WrittenPublicDataSlotsTreeCheckInterface &written_public_data_slots_tree, RetrievedBytecodesTreeCheckInterface &retrieved_bytecodes_tree, SideEffectTrackerInterface &side_effect_tracker, TransactionPhase phase, ContextInterface &parent_context, MemoryAddress cd_offset_address, uint32_t cd_size)
Definition context.hpp:228
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:279
Gas get_parent_gas_limit() const override
Definition context.hpp:271
ContextInterface & parent_context
Definition context.hpp:287
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
uint32_t MemoryAddress
AvmFlavorSettings::FF FF
Definition field.hpp:10
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint32_t cd_offset