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 <utility>
8#include <vector>
9
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 PC get_pc() const override { return pc; }
76 void set_pc(PC new_pc) override { pc = new_pc; }
77 PC get_next_pc() const override { return next_pc; }
78 void set_next_pc(PC 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 PC pc = 0;
147 bool has_halted = false;
153
154 // Output
158 bool last_child_success = false;
159
161};
162
164 public:
205
206 uint32_t get_parent_id() const override { return 0; } // No parent context for the top-level context.
207 bool has_parent() const override { return false; }
208 // Event Emitting
210
211 // Input / Output
212 std::vector<MemoryValue> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
213
214 Gas get_parent_gas_used() const override { return Gas{}; }
215 Gas get_parent_gas_limit() const override { return Gas{}; }
216
217 MemoryAddress get_parent_cd_addr() const override { return 0; }
218 uint32_t get_parent_cd_size() const override { return static_cast<uint32_t>(calldata.size()); }
219
220 private:
222};
223
224// Parameters for a nested call need to be changed
226 public:
265
266 uint32_t get_parent_id() const override { return parent_context.get_context_id(); }
267 bool has_parent() const override { return true; }
268
269 Gas get_parent_gas_used() const override { return parent_context.get_gas_used(); }
271
272 // Event Emitting
274
275 // Input / Output
276 std::vector<MemoryValue> get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override;
277
279 uint32_t get_parent_cd_size() const override { return parent_cd_size; }
280
281 private:
282 // These are direct addresses to look up into the parent context during calldata copying
285
287};
288
289} // 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
void set_next_pc(PC new_next_pc) override
Definition context.hpp:78
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
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
PC get_pc() const override
Definition context.hpp:75
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
Get the returndata from the child context.
Definition context.cpp:19
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
void set_pc(PC new_pc) override
Definition context.hpp:76
uint32_t get_last_child_id() const override
Get the last child id. This is the context id of the last child context. If there is no child context...
Definition context.cpp:47
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
PC get_next_pc() const override
Definition context.hpp:77
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
Gas gas_left() const override
Definition context.hpp:118
std::unique_ptr< InternalCallStackManagerInterface > internal_call_stack_manager
Definition context.hpp:152
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
Get the calldata of the enqueued call context.
Definition context.cpp:67
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:217
uint32_t get_parent_id() const override
Definition context.hpp:206
ContextEvent serialize_context_event() override
Serialize the enqueued call context into a ContextEvent.
Definition context.cpp:90
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:165
std::vector< MemoryValue > calldata
Definition context.hpp:221
Gas get_parent_gas_limit() const override
Definition context.hpp:215
uint32_t get_parent_cd_size() const override
Definition context.hpp:218
uint32_t get_parent_id() const override
Definition context.hpp:266
uint32_t get_parent_cd_size() const override
Definition context.hpp:279
ContextEvent serialize_context_event() override
Serialize the nested context into a ContextEvent.
Definition context.cpp:170
std::vector< MemoryValue > get_calldata(uint32_t cd_offset, uint32_t cd_copy_size) const override
Get the calldata of the nested context. It is present in the parent memory.
Definition context.cpp:142
bool has_parent() const override
Definition context.hpp:267
Gas get_parent_gas_used() const override
Definition context.hpp:269
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:227
MemoryAddress get_parent_cd_addr() const override
Definition context.hpp:278
Gas get_parent_gas_limit() const override
Definition context.hpp:270
ContextInterface & parent_context
Definition context.hpp:286
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
uint32_t PC
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