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 l2_gas;
14 uint64_t da_gas;
15
16 IntermediateGas operator+(const IntermediateGas& other) const
17 {
18 return IntermediateGas{ .l2_gas = this->l2_gas + other.l2_gas, .da_gas = this->da_gas + other.da_gas };
19 }
20
21 IntermediateGas operator*(const IntermediateGas& other) const
22 {
23 return IntermediateGas{ .l2_gas = l2_gas * other.l2_gas, .da_gas = da_gas * other.da_gas };
24 }
25
26 Gas to_gas() const
27 {
28 assert(l2_gas <= std::numeric_limits<uint32_t>::max());
29 assert(da_gas <= std::numeric_limits<uint32_t>::max());
30 return Gas{ .l2_gas = static_cast<uint32_t>(l2_gas), .da_gas = static_cast<uint32_t>(da_gas) };
31 }
32};
33
34IntermediateGas to_intermediate_gas(const Gas& gas)
35{
36 return IntermediateGas{ .l2_gas = static_cast<uint64_t>(gas.l2_gas), .da_gas = static_cast<uint64_t>(gas.da_gas) };
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.l2_gas > gas_limit.l2_gas;
67 bool oog_base_da = base_actual_gas_used.da_gas > gas_limit.da_gas;
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.l2_gas;
80 gas_event.total_gas_used_da = total_gas_used.da_gas;
81
82 gas_event.oog_l2 = greater_than.gt(total_gas_used.l2_gas, gas_limit.l2_gas);
83 gas_event.oog_da = greater_than.gt(total_gas_used.da_gas, gas_limit.da_gas);
84
85 if (oog_base_l2 || oog_base_da) {
86 throw OutOfGasException(format("Out of gas (base): L2 used ",
87 base_actual_gas_used.l2_gas,
88 " of ",
89 gas_limit.l2_gas,
90 ", DA used ",
91 base_actual_gas_used.da_gas,
92 " of ",
93 gas_limit.da_gas));
94 }
95
97 throw OutOfGasException(format("Out of gas (dynamic): L2 used ",
98 total_gas_used.l2_gas,
99 " of ",
100 gas_limit.l2_gas,
101 ", DA used ",
102 total_gas_used.da_gas,
103 " of ",
104 gas_limit.da_gas));
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.l2_gas, allocated_gas.l2_gas);
119 bool is_da_gas_allocated_lt_left = greater_than.gt(gas_left.da_gas, allocated_gas.da_gas);
120
121 return {
122 .l2_gas = is_l2_gas_allocated_lt_left ? allocated_gas.l2_gas : gas_left.l2_gas,
123 .da_gas = is_da_gas_allocated_lt_left ? allocated_gas.da_gas : gas_left.da_gas,
124 };
125}
126
127} // namespace bb::avm2::simulation
GreaterThan greater_than
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:22
uint64_t da_gas
uint64_t l2_gas
InstructionInfoDB instruction_info_db
GasEvent gas_event
Instruction instruction
uint32_t compute_addressing_gas(uint16_t indirect_flag)
Definition gas.cpp:10
Univariate< Fr, domain_end > operator+(const Fr &ff, const Univariate< Fr, domain_end > &uv)
Univariate< Fr, domain_end > operator*(const Fr &ff, const Univariate< Fr, domain_end > &uv)