1mod calc;
4mod constants;
5
6pub use calc::*;
7pub use constants::*;
8
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct Gas {
13 limit: u64,
15 remaining: u64,
17 refunded: i64,
19 memory: MemoryGas,
21}
22
23impl Gas {
24 #[inline]
26 pub const fn new(limit: u64) -> Self {
27 Self {
28 limit,
29 remaining: limit,
30 refunded: 0,
31 memory: MemoryGas::new(),
32 }
33 }
34
35 #[inline]
37 pub const fn new_spent(limit: u64) -> Self {
38 Self {
39 limit,
40 remaining: 0,
41 refunded: 0,
42 memory: MemoryGas::new(),
43 }
44 }
45
46 #[inline]
48 pub const fn limit(&self) -> u64 {
49 self.limit
50 }
51
52 #[inline]
54 pub fn memory(&self) -> &MemoryGas {
55 &self.memory
56 }
57
58 #[inline]
60 pub fn memory_mut(&mut self) -> &mut MemoryGas {
61 &mut self.memory
62 }
63
64 #[inline]
66 pub const fn refunded(&self) -> i64 {
67 self.refunded
68 }
69
70 #[inline]
72 pub const fn spent(&self) -> u64 {
73 self.limit - self.remaining
74 }
75
76 #[inline]
78 pub const fn spent_sub_refunded(&self) -> u64 {
79 self.spent().saturating_sub(self.refunded as u64)
80 }
81
82 #[inline]
84 pub const fn remaining(&self) -> u64 {
85 self.remaining
86 }
87
88 pub const fn remaining_63_of_64_parts(&self) -> u64 {
90 self.remaining - self.remaining / 64
91 }
92
93 #[inline]
95 pub fn erase_cost(&mut self, returned: u64) {
96 self.remaining += returned;
97 }
98
99 #[inline]
101 pub fn spend_all(&mut self) {
102 self.remaining = 0;
103 }
104
105 #[inline]
110 pub fn record_refund(&mut self, refund: i64) {
111 self.refunded += refund;
112 }
113
114 #[inline]
120 pub fn set_final_refund(&mut self, is_london: bool) {
121 let max_refund_quotient = if is_london { 5 } else { 2 };
122 self.refunded = (self.refunded() as u64).min(self.spent() / max_refund_quotient) as i64;
123 }
124
125 #[inline]
127 pub fn set_refund(&mut self, refund: i64) {
128 self.refunded = refund;
129 }
130
131 #[inline]
133 pub fn set_spent(&mut self, spent: u64) {
134 self.remaining = self.limit.saturating_sub(spent);
135 }
136
137 #[inline]
141 #[must_use = "prefer using `gas!` instead to return an out-of-gas error on failure"]
142 pub fn record_cost(&mut self, cost: u64) -> bool {
143 if let Some(new_remaining) = self.remaining.checked_sub(cost) {
144 self.remaining = new_remaining;
145 return true;
146 }
147 false
148 }
149
150 #[inline]
152 #[must_use = "internally uses record_cost that flags out of gas error"]
153 pub fn record_memory_expansion(&mut self, new_len: usize) -> MemoryExtensionResult {
154 let Some(additional_cost) = self.memory.record_new_len(new_len) else {
155 return MemoryExtensionResult::Same;
156 };
157
158 if !self.record_cost(additional_cost) {
159 return MemoryExtensionResult::OutOfGas;
160 }
161
162 MemoryExtensionResult::Extended
163 }
164}
165
166pub enum MemoryExtensionResult {
167 Extended,
169 Same,
171 OutOfGas,
173}
174
175#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
180#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
181pub struct MemoryGas {
182 pub words_num: usize,
184 pub expansion_cost: u64,
186}
187
188impl MemoryGas {
189 pub const fn new() -> Self {
190 Self {
191 words_num: 0,
192 expansion_cost: 0,
193 }
194 }
195
196 #[inline]
197 pub fn record_new_len(&mut self, new_num: usize) -> Option<u64> {
198 if new_num <= self.words_num {
199 return None;
200 }
201 self.words_num = new_num;
202 let mut cost = crate::gas::calc::memory_gas(new_num);
203 core::mem::swap(&mut self.expansion_cost, &mut cost);
204 Some(self.expansion_cost - cost)
207 }
208}