Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
context.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4
5namespace bb::avm2::simulation {
6
8// Base Context
10
19std::vector<MemoryValue> BaseContext::get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) const
20{
21 const MemoryInterface& child_memory = get_child_context().get_memory();
22 // The amount of rd to copy is the minimum of the requested size (with the offset into rd) and the size of the
23 // returndata. We need to do it over a wider integer type to avoid overflow issues, but the result is guaranteed to
24 // be a u32 since last_child_rd_size would have previously been constrained to be u32.
25 uint32_t data_index_upper_bound = static_cast<uint32_t>(
26 std::min(static_cast<uint64_t>(rd_offset) + rd_copy_size, static_cast<uint64_t>(last_child_rd_size)));
27
28 std::vector<MemoryValue> padded_returndata;
29 padded_returndata.reserve(rd_copy_size);
30
31 for (uint32_t i = rd_offset; i < data_index_upper_bound; i++) {
32 padded_returndata.push_back(child_memory.get(get_last_rd_addr() + i));
33 }
34 // If we have some padding (read goes beyond the end of the returndata), fill the rest of the vector with zeros.
35 padded_returndata.resize(rd_copy_size, MemoryValue::from<FF>(0));
36
37 return padded_returndata;
38};
39
48{
49 if (child_context == nullptr) {
50 return 0; // No child context, so no last child id.
51 }
52 return child_context->get_context_id();
53}
54
56// Enqueued Context
58
68{
69 uint64_t calldata_size = static_cast<uint64_t>(calldata.size());
70 // We first take a slice of the data, the most we can slice is the actual size of the data
71 uint64_t data_index_upper_bound = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, calldata_size);
72
73 std::vector<MemoryValue> padded_calldata;
74 padded_calldata.reserve(cd_copy_size);
75
76 for (size_t i = cd_offset; i < data_index_upper_bound; i++) {
77 padded_calldata.push_back(calldata[i]);
78 }
79 // If we have some padding (read goes beyond the end of the calldata), fill the rest of the vector with zeros.
80 padded_calldata.resize(cd_copy_size, MemoryValue::from<FF>(0));
81
82 return padded_calldata;
83};
84
91{
93 const auto& side_effects = get_side_effect_tracker().get_side_effects();
94
95 return {
96 .id = get_context_id(),
97 .parent_id = 0,
98 .last_child_id = get_last_child_id(),
99 .pc = get_pc(),
100 .msg_sender = get_msg_sender(),
101 .contract_addr = get_address(),
102 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
103 .transaction_fee = get_transaction_fee(),
104 .is_static = get_is_static(),
105 .parent_cd_addr = 0,
106 .parent_cd_size = get_parent_cd_size(),
107 .last_child_rd_addr = get_last_rd_addr(),
108 .last_child_rd_size = get_last_rd_size(),
109 .last_child_success = get_last_success(),
110 .gas_used = get_gas_used(),
111 .gas_limit = get_gas_limit(),
112 .parent_gas_used = get_parent_gas_used(),
113 .parent_gas_limit = get_parent_gas_limit(),
114 // Internal call stack
115 .internal_call_id = internal_call_stack.get_call_id(),
116 .internal_call_return_id = internal_call_stack.get_return_call_id(),
117 .next_internal_call_id = internal_call_stack.get_next_call_id(),
118 // Tree States
119 .tree_states = merkle_db.get_tree_state(),
120 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
121 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
122 // Non-tree-tracked side effects
123 .numUnencryptedLogFields = side_effects.get_num_unencrypted_log_fields(),
124 .numL2ToL1Messages = static_cast<uint32_t>(side_effects.l2_to_l1_messages.size()),
125 // Phase
126 .phase = get_phase(),
127 };
128};
129
131// Nested Context
133
143{
144 // This is the amount of the parent calldata we will read
145 // We need to do it over a wider integer type to avoid overflow issues
146 // Explicit for clarity
147 uint64_t parent_cd_size_u64 = static_cast<uint64_t>(parent_cd_size);
148
149 uint64_t data_index_upper_bound = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, parent_cd_size_u64);
150
151 std::vector<MemoryValue> padded_calldata;
152 padded_calldata.reserve(cd_copy_size);
153
154 for (uint32_t i = cd_offset; i < data_index_upper_bound; i++) {
155 padded_calldata.push_back(parent_context.get_memory().get(parent_cd_addr + i));
156 }
157
158 // If we have some padding (read goes beyond the end of the parent calldata), fill the rest of the vector with
159 // zeros.
160 padded_calldata.resize(cd_copy_size, MemoryValue::from<FF>(0));
161
162 return padded_calldata;
163};
164
171{
173 const auto& side_effects = get_side_effect_tracker().get_side_effects();
174
175 return {
176 .id = get_context_id(),
177 .parent_id = get_parent_id(),
178 .last_child_id = get_last_child_id(),
179 .pc = get_pc(),
180 .msg_sender = get_msg_sender(),
181 .contract_addr = get_address(),
182 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
183 .transaction_fee = get_transaction_fee(),
184 .is_static = get_is_static(),
185 .parent_cd_addr = parent_cd_addr,
186 .parent_cd_size = parent_cd_size,
187 .last_child_rd_addr = get_last_rd_addr(),
188 .last_child_rd_size = get_last_rd_size(),
189 .last_child_success = get_last_success(),
190 .gas_used = get_gas_used(),
191 .gas_limit = get_gas_limit(),
192 .parent_gas_used = get_parent_gas_used(),
193 .parent_gas_limit = get_parent_gas_limit(),
194 // Internal call stack
195 .internal_call_id = internal_call_stack.get_call_id(),
196 .internal_call_return_id = internal_call_stack.get_return_call_id(),
197 .next_internal_call_id = internal_call_stack.get_next_call_id(),
198 // Tree states
199 .tree_states = merkle_db.get_tree_state(),
200 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
201 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
202 // Non-tree-tracked side effects
203 .numUnencryptedLogFields = side_effects.get_num_unencrypted_log_fields(),
204 .numL2ToL1Messages = static_cast<uint32_t>(side_effects.l2_to_l1_messages.size()),
205 // Phase
206 .phase = get_phase(),
207 };
208};
209
210} // namespace bb::avm2::simulation
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
MemoryAddress get_last_rd_addr() const override
Definition context.hpp:106
SideEffectTrackerInterface & get_side_effect_tracker() override
Definition context.hpp:89
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
const AztecAddress & get_msg_sender() const override
Definition context.hpp:86
ContextInterface & get_child_context() override
Definition context.hpp:99
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
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
uint32_t get_context_id() const override
Definition context.hpp:82
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
BytecodeManagerInterface & get_bytecode_manager() override
Definition context.hpp:69
bool get_last_success() const override
Definition context.hpp:112
Gas get_gas_limit() const override
Definition context.hpp:116
HighLevelMerkleDBInterface & merkle_db
Definition context.hpp:128
const FF & get_transaction_fee() const override
Definition context.hpp:87
bool get_is_static() const override
Definition context.hpp:88
virtual std::optional< BytecodeId > get_retrieved_bytecode_id()=0
virtual MemoryInterface & get_memory()=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
ContextEvent serialize_context_event() override
Serialize the enqueued call context into a ContextEvent.
Definition context.cpp:90
Gas get_parent_gas_limit() const override
Definition context.hpp:215
uint32_t get_parent_cd_size() const override
Definition context.hpp:218
virtual TreeStates get_tree_state() const =0
virtual const MemoryValue & get(MemoryAddress index) const =0
uint32_t get_parent_id() const override
Definition context.hpp:266
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
Gas get_parent_gas_used() const override
Definition context.hpp:269
Gas get_parent_gas_limit() const override
Definition context.hpp:270
ContextInterface & parent_context
Definition context.hpp:286
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
virtual const TrackedSideEffects & get_side_effects() const =0
virtual AppendOnlyTreeSnapshot get_snapshot() const =0
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint32_t cd_offset