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
20
21namespace bb::avm2::simulation {
22
24 public:
25 virtual ~ContextInterface() = default;
26
27 // Machine state.
31 virtual uint32_t get_pc() const = 0;
32 virtual void set_pc(uint32_t new_pc) = 0;
33 virtual uint32_t get_next_pc() const = 0;
34 virtual void set_next_pc(uint32_t new_next_pc) = 0;
35 virtual bool halted() const = 0;
36 virtual void halt() = 0;
37 virtual uint32_t get_context_id() const = 0;
38 virtual uint32_t get_parent_id() const = 0;
39 virtual uint32_t get_last_child_id() const = 0;
40 virtual bool has_parent() const = 0;
41
42 // Environment.
43 virtual const AztecAddress& get_address() const = 0;
44 virtual const AztecAddress& get_msg_sender() const = 0;
45 virtual const FF& get_transaction_fee() const = 0;
46 virtual bool get_is_static() const = 0;
49 virtual void set_side_effect_states(SideEffectStates side_effect_states) = 0;
50 virtual const GlobalVariables& get_globals() const = 0;
51
52 virtual TransactionPhase get_phase() const = 0;
53
54 virtual std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_size) const = 0;
55 virtual std::vector<FF> get_returndata(uint32_t rd_addr, uint32_t rd_size) = 0;
57 // The child context needs to be accessible by this context in order to access the child
58 // memory for returndata. We own it so that it's lifetime is as long as decided by this context
59 // (i.e. if it is replaced by another child OR this parent context falls out of scope)
61
62 virtual MemoryAddress get_parent_cd_addr() const = 0;
63 virtual uint32_t get_parent_cd_size() const = 0;
64
65 virtual MemoryAddress get_last_rd_addr() const = 0;
66 virtual void set_last_rd_addr(MemoryAddress rd_addr) = 0;
67
68 virtual uint32_t get_last_rd_size() const = 0;
69 virtual void set_last_rd_size(MemoryAddress rd_size) = 0;
70
71 virtual bool get_last_success() const = 0;
72 virtual void set_last_success(bool success) = 0;
73
74 virtual Gas get_gas_used() const = 0;
75 virtual Gas get_gas_limit() const = 0;
76 virtual void set_gas_used(Gas gas_used) = 0;
77
78 virtual Gas get_parent_gas_used() const = 0;
79 virtual Gas get_parent_gas_limit() const = 0;
80
81 virtual Gas gas_left() const = 0;
82
83 virtual uint32_t get_checkpoint_id_at_creation() const = 0;
84
85 // Events
87};
88
89// The context for a single nested call.
91 public:
124
125 uint32_t get_last_child_id() const override;
126
127 // Having getters and setters make it easier to mock the context.
128 // Machine state.
129 MemoryInterface& get_memory() override { return *memory; }
135
136 uint32_t get_pc() const override { return pc; }
137 void set_pc(uint32_t new_pc) override { pc = new_pc; }
138 uint32_t get_next_pc() const override { return next_pc; }
139 void set_next_pc(uint32_t new_next_pc) override { next_pc = new_next_pc; }
140 bool halted() const override { return has_halted; }
141 void halt() override { has_halted = true; }
142
143 uint32_t get_context_id() const override { return context_id; }
144
145 // Environment.
146 const AztecAddress& get_address() const override { return address; }
147 const AztecAddress& get_msg_sender() const override { return msg_sender; }
148 const FF& get_transaction_fee() const override { return transaction_fee; }
149 bool get_is_static() const override { return is_static; }
152 {
153 this->side_effect_states = side_effect_states;
154 }
155
156 TransactionPhase get_phase() const override { return phase; }
157
162 const GlobalVariables& get_globals() const override { return globals; }
163
166 {
167 child_context = std::move(child_ctx);
168 }
169
171 void set_last_rd_addr(MemoryAddress rd_addr) override { last_child_rd_addr = rd_addr; }
172
173 uint32_t get_last_rd_size() const override { return last_child_rd_size; }
174 void set_last_rd_size(MemoryAddress rd_size) override { last_child_rd_size = rd_size; }
175
176 bool get_last_success() const override { return last_child_success; }
177 void set_last_success(bool success) override { last_child_success = success; }
178
179 Gas get_gas_used() const override { return gas_used; }
180 Gas get_gas_limit() const override { return gas_limit; }
181
182 Gas gas_left() const override { return gas_limit - gas_used; }
183
184 void set_gas_used(Gas gas_used) override { this->gas_used = gas_used; }
185
186 uint32_t get_checkpoint_id_at_creation() const override { return checkpoint_id_at_creation; }
187
188 // Input / Output
189 std::vector<FF> get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) override;
190
191 protected:
193 uint32_t checkpoint_id_at_creation; // DB id when the context was created.
195
196 private:
197 // Environment.
204
205 uint32_t context_id;
206
207 // Machine state.
208 uint32_t pc = 0;
209 uint32_t next_pc = 0;
210 bool has_halted = false;
216
217 // Output
221 bool last_child_success = false;
222
224};
225
226// TODO(ilyas): move to cpp file
228 public:
262
263 uint32_t get_parent_id() const override { return 0; } // No parent context for the top-level context.
264 bool has_parent() const override { return false; }
265 // Event Emitting
267
268 // Input / Output
269 std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
270
271 Gas get_parent_gas_used() const override { return Gas{}; }
272 Gas get_parent_gas_limit() const override { return Gas{}; }
273
274 MemoryAddress get_parent_cd_addr() const override { return 0; }
275 uint32_t get_parent_cd_size() const override { return static_cast<uint32_t>(calldata.size()); }
276
277 private:
278 std::vector<FF> calldata;
279};
280
281// Parameters for a nested call need to be changed
283 public:
320
321 uint32_t get_parent_id() const override { return parent_context.get_context_id(); }
322 bool has_parent() const override { return true; }
323
324 Gas get_parent_gas_used() const override { return parent_context.get_gas_used(); }
326
327 // Event Emitting
329
330 // Input / Output
331 std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
332
334 uint32_t get_parent_cd_size() const override { return parent_cd_size; }
335
336 private:
337 // These are direct addresses to look up into the parent context during calldata copying
340
342};
343
344} // namespace bb::avm2::simulation
void set_last_success(bool success) override
Definition context.hpp:177
const AztecAddress & get_address() const override
Definition context.hpp:146
TransactionPhase get_phase() const override
Definition context.hpp:156
InternalCallStackManagerInterface & get_internal_call_stack_manager() override
Definition context.hpp:131
void set_gas_used(Gas gas_used) override
Definition context.hpp:184
std::vector< FF > get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) override
Definition context.cpp:15
MemoryAddress get_last_rd_addr() const override
Definition context.hpp:170
bool halted() const override
Definition context.hpp:140
void set_next_pc(uint32_t new_next_pc) override
Definition context.hpp:139
uint32_t get_last_rd_size() const override
Definition context.hpp:173
Gas get_gas_used() const override
Definition context.hpp:179
void set_last_rd_size(MemoryAddress rd_size) override
Definition context.hpp:174
const AztecAddress & get_msg_sender() const override
Definition context.hpp:147
AppendOnlyTreeSnapshot get_written_public_data_slots_tree_snapshot() override
Definition context.hpp:158
ContextInterface & get_child_context() override
Definition context.hpp:164
uint32_t get_checkpoint_id_at_creation() const override
Definition context.hpp:186
void set_side_effect_states(SideEffectStates side_effect_states) override
Definition context.hpp:151
SideEffectStates & get_side_effect_states() override
Definition context.hpp:150
WrittenPublicDataSlotsTreeCheckInterface & written_public_data_slots_tree
Definition context.hpp:194
uint32_t get_next_pc() const override
Definition context.hpp:138
std::unique_ptr< ContextInterface > child_context
Definition context.hpp:218
void set_child_context(std::unique_ptr< ContextInterface > child_ctx) override
Definition context.hpp:165
uint32_t get_context_id() const override
Definition context.hpp:143
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, SideEffectStates side_effect_states, TransactionPhase phase)
Definition context.hpp:92
uint32_t get_last_child_id() const override
Definition context.cpp:36
MemoryInterface & get_memory() override
Definition context.hpp:129
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:130
bool get_last_success() const override
Definition context.hpp:176
SideEffectStates side_effect_states
Definition context.hpp:203
std::unique_ptr< MemoryInterface > memory
Definition context.hpp:214
void set_last_rd_addr(MemoryAddress rd_addr) override
Definition context.hpp:171
Gas get_gas_limit() const override
Definition context.hpp:180
std::unique_ptr< BytecodeManagerInterface > bytecode
Definition context.hpp:213
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:192
const FF & get_transaction_fee() const override
Definition context.hpp:148
uint32_t get_pc() const override
Definition context.hpp:136
Gas gas_left() const override
Definition context.hpp:182
std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager
Definition context.hpp:215
void set_pc(uint32_t new_pc) override
Definition context.hpp:137
bool get_is_static() const override
Definition context.hpp:149
const GlobalVariables & get_globals() const override
Definition context.hpp:162
virtual uint32_t get_pc() const =0
virtual MemoryAddress get_last_rd_addr() const =0
virtual const AztecAddress & get_msg_sender() const =0
virtual std::vector< FF > get_returndata(uint32_t rd_addr, uint32_t rd_size)=0
virtual ContextInterface & get_child_context()=0
virtual bool get_last_success() const =0
virtual Gas get_gas_used() const =0
virtual void set_child_context(std::unique_ptr< ContextInterface > child_ctx)=0
virtual Gas get_parent_gas_limit() const =0
virtual void set_pc(uint32_t new_pc)=0
virtual uint32_t get_checkpoint_id_at_creation() const =0
virtual MemoryInterface & get_memory()=0
virtual void set_last_rd_size(MemoryAddress rd_size)=0
virtual InternalCallStackManagerInterface & get_internal_call_stack_manager()=0
virtual ContextEvent serialize_context_event()=0
virtual uint32_t get_next_pc() const =0
virtual const FF & get_transaction_fee() const =0
virtual uint32_t get_parent_cd_size() const =0
virtual uint32_t get_last_child_id() const =0
virtual MemoryAddress get_parent_cd_addr() const =0
virtual AppendOnlyTreeSnapshot get_written_public_data_slots_tree_snapshot()=0
virtual SideEffectStates & get_side_effect_states()=0
virtual const GlobalVariables & get_globals() const =0
virtual void set_gas_used(Gas gas_used)=0
virtual uint32_t get_parent_id() const =0
virtual uint32_t get_last_rd_size() const =0
virtual bool get_is_static() const =0
virtual BytecodeManagerInterface & get_bytecode_manager()=0
virtual bool has_parent() const =0
virtual Gas get_gas_limit() const =0
virtual std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_size) const =0
virtual const AztecAddress & get_address() const =0
virtual uint32_t get_context_id() const =0
virtual void set_side_effect_states(SideEffectStates side_effect_states)=0
virtual Gas get_parent_gas_used() const =0
virtual void set_last_rd_addr(MemoryAddress rd_addr)=0
virtual void set_last_success(bool success)=0
virtual TransactionPhase get_phase() const =0
virtual void set_next_pc(uint32_t new_next_pc)=0
std::vector< FF > 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:274
uint32_t get_parent_id() const override
Definition context.hpp:263
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, SideEffectStates side_effect_states, TransactionPhase phase, std::span< const FF > calldata)
Definition context.hpp:229
Gas get_parent_gas_limit() const override
Definition context.hpp:272
uint32_t get_parent_cd_size() const override
Definition context.hpp:275
uint32_t get_parent_id() const override
Definition context.hpp:321
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, SideEffectStates side_effect_states, TransactionPhase phase, ContextInterface &parent_context, MemoryAddress cd_offset_address, uint32_t cd_size)
Definition context.hpp:284
uint32_t get_parent_cd_size() const override
Definition context.hpp:334
ContextEvent serialize_context_event() override
Definition context.cpp:124
bool has_parent() const override
Definition context.hpp:322
std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:103
Gas get_parent_gas_used() const override
Definition context.hpp:324
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:333
Gas get_parent_gas_limit() const override
Definition context.hpp:325
ContextInterface & parent_context
Definition context.hpp:341
virtual AppendOnlyTreeSnapshot 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