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 <algorithm>
4#include <cstdint>
5#include <vector>
6
9
10namespace bb::avm2::simulation {
11
13// Base Context
15std::vector<MemoryValue> BaseContext::get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) const
16{
17 const MemoryInterface& child_memory = get_child_context().get_memory();
18 // The amount to rd copy is the minimum of the requested size (with the offset into rd) and the size of the
19 // returndata We need to do it over a wider integer type to avoid overflow issues, but the result is guaranteed to
20 // be a u32 since last_child_rd_size would have previously been constrained to be u32.
21 uint32_t data_index_upper_bound = static_cast<uint32_t>(
22 std::min(static_cast<uint64_t>(rd_offset) + rd_copy_size, static_cast<uint64_t>(last_child_rd_size)));
23
24 std::vector<MemoryValue> padded_returndata;
25 padded_returndata.reserve(rd_copy_size);
26
27 for (uint32_t i = rd_offset; i < data_index_upper_bound; i++) {
28 padded_returndata.push_back(child_memory.get(get_last_rd_addr() + i));
29 }
30 // If we have some padding (read goes beyond the end of the returndata), fill the rest of the vector with zeros.
31 padded_returndata.resize(rd_copy_size, MemoryValue::from<FF>(0));
32
33 return padded_returndata;
34};
35
37{
38 if (child_context == nullptr) {
39 return 0; // No child context, so no last child id.
40 }
41 return child_context->get_context_id();
42}
43
45// Enqueued Context
48{
49 uint64_t calldata_size = static_cast<uint64_t>(calldata.size());
50 // We first take a slice of the data, the most we can slice is the actual size of the data
51 uint64_t data_index_upper_bound = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, calldata_size);
52
53 std::vector<MemoryValue> padded_calldata;
54 padded_calldata.reserve(cd_copy_size);
55
56 for (size_t i = cd_offset; i < data_index_upper_bound; i++) {
57 padded_calldata.push_back(calldata[i]);
58 }
59 // If we have some padding (read goes beyond the end of the calldata), fill the rest of the vector with zeros.
60 padded_calldata.resize(cd_copy_size, MemoryValue::from<FF>(0));
61
62 return padded_calldata;
63};
64
66{
67 auto& call_stack = get_internal_call_stack_manager();
68 auto& side_effects = get_side_effect_tracker().get_side_effects();
69
70 return {
71 .id = get_context_id(),
72 .parent_id = 0,
73 .last_child_id = get_last_child_id(),
74 .pc = get_pc(),
75 .msg_sender = get_msg_sender(),
76 .contract_addr = get_address(),
77 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
78 .transaction_fee = get_transaction_fee(),
79 .is_static = get_is_static(),
80 .parent_cd_addr = 0,
81 .parent_cd_size = get_parent_cd_size(),
82 .last_child_rd_addr = get_last_rd_addr(),
83 .last_child_rd_size = get_last_rd_size(),
84 .last_child_success = get_last_success(),
85 .gas_used = get_gas_used(),
86 .gas_limit = get_gas_limit(),
87 .parent_gas_used = get_parent_gas_used(),
88 .parent_gas_limit = get_parent_gas_limit(),
89 // internal call stack
90 .internal_call_id = call_stack.get_call_id(),
91 .internal_call_return_id = call_stack.get_return_call_id(),
92 .next_internal_call_id = call_stack.get_next_call_id(),
93 // Tree States
94 .tree_states = merkle_db.get_tree_state(),
95 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
96 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
97 // Non-tree-tracked side effects
98 .numUnencryptedLogFields = side_effects.get_num_unencrypted_log_fields(),
99 .numL2ToL1Messages = static_cast<uint32_t>(side_effects.l2_to_l1_messages.size()),
100 // Phase
101 .phase = get_phase(),
102 };
103};
104
106// Nested Context
109{
110 // This is the amount of the parent calldata we will read
111 // We need to do it over a wider integer type to avoid overflow issues
112 // Explicit for clarity
113 uint64_t parent_cd_size_u64 = static_cast<uint64_t>(parent_cd_size);
114
115 uint64_t data_index_upper_bound = std::min(static_cast<uint64_t>(cd_offset) + cd_copy_size, parent_cd_size_u64);
116
117 std::vector<MemoryValue> padded_calldata;
118 padded_calldata.reserve(cd_copy_size);
119
120 for (uint32_t i = cd_offset; i < data_index_upper_bound; i++) {
121 padded_calldata.push_back(parent_context.get_memory().get(parent_cd_addr + i));
122 }
123
124 // If we have some padding (read goes beyond the end of the parent calldata), fill the rest of the vector with
125 // zeros.
126 padded_calldata.resize(cd_copy_size, MemoryValue::from<FF>(0));
127
128 return padded_calldata;
129};
130
132{
133 auto& call_stack = get_internal_call_stack_manager();
134 auto& side_effects = get_side_effect_tracker().get_side_effects();
135
136 return {
137 .id = get_context_id(),
138 .parent_id = get_parent_id(),
139 .last_child_id = get_last_child_id(),
140 .pc = get_pc(),
141 .msg_sender = get_msg_sender(),
142 .contract_addr = get_address(),
143 .bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
144 .transaction_fee = get_transaction_fee(),
145 .is_static = get_is_static(),
146 .parent_cd_addr = parent_cd_addr,
147 .parent_cd_size = parent_cd_size,
148 .last_child_rd_addr = get_last_rd_addr(),
149 .last_child_rd_size = get_last_rd_size(),
150 .last_child_success = get_last_success(),
151 .gas_used = get_gas_used(),
152 .gas_limit = get_gas_limit(),
153 .parent_gas_used = get_parent_gas_used(),
154 .parent_gas_limit = get_parent_gas_limit(),
155 // internal call stack
156 .internal_call_id = call_stack.get_call_id(),
157 .internal_call_return_id = call_stack.get_return_call_id(),
158 .next_internal_call_id = call_stack.get_next_call_id(),
159 // Tree states
160 .tree_states = merkle_db.get_tree_state(),
161 .written_public_data_slots_tree_snapshot = written_public_data_slots_tree.get_snapshot(),
162 .retrieved_bytecodes_tree_snapshot = retrieved_bytecodes_tree.get_snapshot(),
163 // Non-tree-tracked side effects
164 .numUnencryptedLogFields = side_effects.get_num_unencrypted_log_fields(),
165 .numL2ToL1Messages = static_cast<uint32_t>(side_effects.l2_to_l1_messages.size()),
166 // Phase
167 .phase = get_phase(),
168 };
169};
170
171} // 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
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
Definition context.cpp:15
uint32_t get_context_id() const override
Definition context.hpp:82
uint32_t get_last_child_id() const override
Definition context.cpp:36
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
uint32_t get_pc() const override
Definition context.hpp:75
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
Definition context.cpp:47
ContextEvent serialize_context_event() override
Definition context.cpp:65
Gas get_parent_gas_limit() const override
Definition context.hpp:216
uint32_t get_parent_cd_size() const override
Definition context.hpp:219
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:267
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
Gas get_parent_gas_used() const override
Definition context.hpp:270
Gas get_parent_gas_limit() const override
Definition context.hpp:271
ContextInterface & parent_context
Definition context.hpp:287
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