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
21
22namespace bb::avm2::simulation {
23
24// The context for a single nested call.
26 public:
61
62 uint32_t get_last_child_id() const override;
63
64 // Having getters and setters make it easier to mock the context.
65 // Machine state.
66 MemoryInterface& get_memory() override { return *memory; }
72
73 uint32_t get_pc() const override { return pc; }
74 void set_pc(uint32_t new_pc) override { pc = new_pc; }
75 uint32_t get_next_pc() const override { return next_pc; }
76 void set_next_pc(uint32_t new_next_pc) override { next_pc = new_next_pc; }
77 bool halted() const override { return has_halted; }
78 void halt() override { has_halted = true; }
79
80 uint32_t get_context_id() const override { return context_id; }
81
82 // Environment.
83 const AztecAddress& get_address() const override { return address; }
84 const AztecAddress& get_msg_sender() const override { return msg_sender; }
85 const FF& get_transaction_fee() const override { return transaction_fee; }
86 bool get_is_static() const override { return is_static; }
89 {
90 this->side_effect_states = side_effect_states;
91 }
92
93 TransactionPhase get_phase() const override { return phase; }
94
99 const GlobalVariables& get_globals() const override { return globals; }
100
103 {
104 child_context = std::move(child_ctx);
105 }
106
108 void set_last_rd_addr(MemoryAddress rd_addr) override { last_child_rd_addr = rd_addr; }
109
110 uint32_t get_last_rd_size() const override { return last_child_rd_size; }
111 void set_last_rd_size(MemoryAddress rd_size) override { last_child_rd_size = rd_size; }
112
113 bool get_last_success() const override { return last_child_success; }
114 void set_last_success(bool success) override { last_child_success = success; }
115
116 Gas get_gas_used() const override { return gas_used; }
117 Gas get_gas_limit() const override { return gas_limit; }
118
119 Gas gas_left() const override { return gas_limit - gas_used; }
120
121 void set_gas_used(Gas gas_used) override { this->gas_used = gas_used; }
122
123 uint32_t get_checkpoint_id_at_creation() const override { return checkpoint_id_at_creation; }
124
125 // Input / Output
126 std::vector<FF> get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) override;
127
128 protected:
130 uint32_t checkpoint_id_at_creation; // DB id when the context was created.
133
134 private:
135 // Environment.
142
143 uint32_t context_id;
144
145 // Machine state.
146 uint32_t pc = 0;
147 uint32_t next_pc = 0;
148 bool has_halted = false;
154
155 // Output
159 bool last_child_success = false;
160
162};
163
164// TODO(ilyas): move to cpp file
166 public:
202
203 uint32_t get_parent_id() const override { return 0; } // No parent context for the top-level context.
204 bool has_parent() const override { return false; }
205 // Event Emitting
207
208 // Input / Output
209 std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
210
211 Gas get_parent_gas_used() const override { return Gas{}; }
212 Gas get_parent_gas_limit() const override { return Gas{}; }
213
214 MemoryAddress get_parent_cd_addr() const override { return 0; }
215 uint32_t get_parent_cd_size() const override { return static_cast<uint32_t>(calldata.size()); }
216
217 private:
218 std::vector<FF> calldata;
219};
220
221// Parameters for a nested call need to be changed
223 public:
262
263 uint32_t get_parent_id() const override { return parent_context.get_context_id(); }
264 bool has_parent() const override { return true; }
265
266 Gas get_parent_gas_used() const override { return parent_context.get_gas_used(); }
268
269 // Event Emitting
271
272 // Input / Output
273 std::vector<FF> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
274
276 uint32_t get_parent_cd_size() const override { return parent_cd_size; }
277
278 private:
279 // These are direct addresses to look up into the parent context during calldata copying
282
284};
285
286} // namespace bb::avm2::simulation
void set_last_success(bool success) override
Definition context.hpp:114
const AztecAddress & get_address() const override
Definition context.hpp:83
TransactionPhase get_phase() const override
Definition context.hpp:93
InternalCallStackManagerInterface & get_internal_call_stack_manager() override
Definition context.hpp:68
void set_gas_used(Gas gas_used) override
Definition context.hpp:121
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:107
bool halted() const override
Definition context.hpp:77
RetrievedBytecodesTreeCheckInterface & retrieved_bytecodes_tree
Definition context.hpp:132
void set_next_pc(uint32_t new_next_pc) override
Definition context.hpp:76
uint32_t get_last_rd_size() const override
Definition context.hpp:110
Gas get_gas_used() const override
Definition context.hpp:116
void set_last_rd_size(MemoryAddress rd_size) override
Definition context.hpp:111
const AztecAddress & get_msg_sender() const override
Definition context.hpp:84
AppendOnlyTreeSnapshot get_written_public_data_slots_tree_snapshot() override
Definition context.hpp:95
ContextInterface & get_child_context() override
Definition context.hpp:101
uint32_t get_checkpoint_id_at_creation() const override
Definition context.hpp:123
void set_side_effect_states(SideEffectStates side_effect_states) override
Definition context.hpp:88
SideEffectStates & get_side_effect_states() override
Definition context.hpp:87
WrittenPublicDataSlotsTreeCheckInterface & written_public_data_slots_tree
Definition context.hpp:131
uint32_t get_next_pc() const override
Definition context.hpp:75
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, SideEffectStates side_effect_states, TransactionPhase phase)
Definition context.hpp:27
std::unique_ptr< ContextInterface > child_context
Definition context.hpp:156
void set_child_context(std::unique_ptr< ContextInterface > child_ctx) override
Definition context.hpp:102
uint32_t get_context_id() const override
Definition context.hpp:80
uint32_t get_last_child_id() const override
Definition context.cpp:36
MemoryInterface & get_memory() override
Definition context.hpp:66
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:67
bool get_last_success() const override
Definition context.hpp:113
SideEffectStates side_effect_states
Definition context.hpp:141
std::unique_ptr< MemoryInterface > memory
Definition context.hpp:152
void set_last_rd_addr(MemoryAddress rd_addr) override
Definition context.hpp:108
Gas get_gas_limit() const override
Definition context.hpp:117
std::unique_ptr< BytecodeManagerInterface > bytecode
Definition context.hpp:151
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:129
const FF & get_transaction_fee() const override
Definition context.hpp:85
uint32_t get_pc() const override
Definition context.hpp:73
Gas gas_left() const override
Definition context.hpp:119
std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager
Definition context.hpp:153
void set_pc(uint32_t new_pc) override
Definition context.hpp:74
bool get_is_static() const override
Definition context.hpp:86
const GlobalVariables & get_globals() const override
Definition context.hpp:99
virtual Gas get_gas_used() const =0
virtual Gas get_gas_limit() const =0
virtual uint32_t get_context_id() const =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:214
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, SideEffectStates side_effect_states, TransactionPhase phase, std::span< const FF > calldata)
Definition context.hpp:167
uint32_t get_parent_id() const override
Definition context.hpp:203
ContextEvent serialize_context_event() override
Definition context.cpp:65
Gas get_parent_gas_limit() const override
Definition context.hpp:212
uint32_t get_parent_cd_size() const override
Definition context.hpp:215
uint32_t get_parent_id() const override
Definition context.hpp:263
uint32_t get_parent_cd_size() const override
Definition context.hpp:276
ContextEvent serialize_context_event() override
Definition context.cpp:125
bool has_parent() const override
Definition context.hpp:264
std::vector< FF > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Definition context.cpp:104
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, SideEffectStates side_effect_states, TransactionPhase phase, ContextInterface &parent_context, MemoryAddress cd_offset_address, uint32_t cd_size)
Definition context.hpp:224
Gas get_parent_gas_used() const override
Definition context.hpp:266
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:275
Gas get_parent_gas_limit() const override
Definition context.hpp:267
ContextInterface & parent_context
Definition context.hpp:283
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