Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
gas_tracker.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4#include <cstdint>
5
7
8namespace bb::avm2::simulation {
9namespace {
10
11// Wider type used for intermediate gas calculations.
12struct IntermediateGas {
13 uint64_t l2Gas;
14 uint64_t daGas;
15
16 IntermediateGas operator+(const IntermediateGas& other) const
17 {
18 return IntermediateGas{ .l2Gas = l2Gas + other.l2Gas, .daGas = daGas + other.daGas };
19 }
20
21 IntermediateGas operator*(const IntermediateGas& other) const
22 {
23 return IntermediateGas{ .l2Gas = l2Gas * other.l2Gas, .daGas = daGas * other.daGas };
24 }
25
26 Gas to_gas() const
27 {
28 assert(l2Gas <= std::numeric_limits<uint32_t>::max());
29 assert(daGas <= std::numeric_limits<uint32_t>::max());
30 return Gas{ .l2Gas = static_cast<uint32_t>(l2Gas), .daGas = static_cast<uint32_t>(daGas) };
31 }
32};
33
34IntermediateGas to_intermediate_gas(const Gas& gas)
35{
36 return IntermediateGas{ .l2Gas = static_cast<uint64_t>(gas.l2Gas), .daGas = static_cast<uint64_t>(gas.daGas) };
37}
38
39} // namespace
40
53
54void GasTracker::consume_gas(const Gas& dynamic_gas_factor)
55{
56 // Base.
57 Gas prev_gas_used = context.get_gas_used();
58 const uint32_t base_da_gas = spec.gas_cost.base_da;
59
60 // Previous gas used can be up to 2**32 - 1
61 IntermediateGas base_actual_gas_used =
62 to_intermediate_gas(prev_gas_used) +
63 to_intermediate_gas({ gas_event.addressing_gas + spec.gas_cost.opcode_gas, base_da_gas });
64 IntermediateGas gas_limit = to_intermediate_gas(context.get_gas_limit());
65
66 bool oog_base_l2 = base_actual_gas_used.l2Gas > gas_limit.l2Gas;
67 bool oog_base_da = base_actual_gas_used.daGas > gas_limit.daGas;
68
69 // Dynamic.
70 gas_event.dynamic_gas_factor = dynamic_gas_factor;
71
72 const uint32_t dynamic_l2_gas = spec.gas_cost.dyn_l2;
73 const uint32_t dynamic_da_gas = spec.gas_cost.dyn_da;
74
75 IntermediateGas total_gas_used =
76 base_actual_gas_used +
77 (to_intermediate_gas(Gas{ dynamic_l2_gas, dynamic_da_gas }) * to_intermediate_gas(dynamic_gas_factor));
78
79 gas_event.total_gas_used_l2 = total_gas_used.l2Gas;
80 gas_event.total_gas_used_da = total_gas_used.daGas;
81
82 gas_event.oog_l2 = greater_than.gt(total_gas_used.l2Gas, gas_limit.l2Gas);
83 gas_event.oog_da = greater_than.gt(total_gas_used.daGas, gas_limit.daGas);
84
85 if (oog_base_l2 || oog_base_da) {
86 throw OutOfGasException(format("Out of gas (base): L2 used ",
87 base_actual_gas_used.l2Gas,
88 " of ",
89 gas_limit.l2Gas,
90 ", DA used ",
91 base_actual_gas_used.daGas,
92 " of ",
93 gas_limit.daGas));
94 }
95
97 throw OutOfGasException(format("Out of gas (dynamic): L2 used ",
98 total_gas_used.l2Gas,
99 " of ",
100 gas_limit.l2Gas,
101 ", DA used ",
102 total_gas_used.daGas,
103 " of ",
104 gas_limit.daGas));
105 }
106
107 // Safe downcast since if we were over 32 bits, we would have OOG'd.
108 context.set_gas_used(total_gas_used.to_gas());
109}
110
111// Gas limit for call is the minimum between the gas allocated to the call by the user, and the gas left.
112// This applies to both gas dimensions independently.
113// This method does not emit a gas event.
115{
116 Gas gas_left = context.gas_left();
117
118 bool is_l2_gas_allocated_lt_left = greater_than.gt(gas_left.l2Gas, allocated_gas.l2Gas);
119 bool is_da_gas_allocated_lt_left = greater_than.gt(gas_left.daGas, allocated_gas.daGas);
120
121 return {
122 .l2Gas = is_l2_gas_allocated_lt_left ? allocated_gas.l2Gas : gas_left.l2Gas,
123 .daGas = is_da_gas_allocated_lt_left ? allocated_gas.daGas : gas_left.daGas,
124 };
125}
126
127} // namespace bb::avm2::simulation
Gas compute_gas_limit_for_call(const Gas &allocated_gas) override
GreaterThanInterface & greater_than
const ExecInstructionSpec & spec
void consume_gas(const Gas &dynamic_gas_factor={ 0, 0 }) override
GasTracker(GasEvent &gas_event, const Instruction &instruction, const InstructionInfoDBInterface &instruction_info_db, ContextInterface &context, GreaterThanInterface &greater_than)
virtual bool gt(const FF &a, const FF &b)=0
std::string format(Args... args)
Definition log.hpp:21
uint64_t l2Gas
uint64_t daGas
InstructionInfoDB instruction_info_db
GasEvent gas_event
Instruction instruction
testing::StrictMock< MockGreaterThan > greater_than
uint32_t compute_addressing_gas(uint16_t indirect_flag)
Definition gas.cpp:10
Univariate< Fr, domain_end, domain_start, skip_count > operator+(const Fr &ff, const Univariate< Fr, domain_end, domain_start, skip_count > &uv)
Univariate< Fr, domain_end, domain_start, skip_count > operator*(const Fr &ff, const Univariate< Fr, domain_end, domain_start, skip_count > &uv)