pub struct Gas { /* private fields */ }Expand description
Represents the state of gas during execution.
Implements the EIP-8037 reservoir model for dual-limit gas accounting:
remaining: regular gas left (gas_left). Does NOT includereservoir.reservoir: state gas pool (separate fromremaining). Starts asexecution_gas - gas_left.state_gas_spent: tracks total state gas spent
Regular gas charges (record_cost): deduct from remaining, checked against remaining.
State gas charges (record_state_cost): deduct from reservoir first; when exhausted, spill into remaining.
Total gas available = remaining + reservoir.
On mainnet (no state gas), reservoir = 0 so all gas is regular gas and behavior is unchanged.
Implementations§
Source§impl Gas
impl Gas
Sourcepub const fn new(limit: u64) -> Self
pub const fn new(limit: u64) -> Self
Creates a new Gas struct with the given gas limit.
Sets reservoir = 0 so all gas is regular gas (standard mainnet behavior).
Sourcepub const fn tracker(&self) -> &GasTracker
pub const fn tracker(&self) -> &GasTracker
Returns the tracker for gas during execution.
Sourcepub const fn tracker_mut(&mut self) -> &mut GasTracker
pub const fn tracker_mut(&mut self) -> &mut GasTracker
Returns the mutable tracker for gas during execution.
Sourcepub const fn new_with_regular_gas_and_reservoir(
limit: u64,
reservoir: u64,
) -> Self
pub const fn new_with_regular_gas_and_reservoir( limit: u64, reservoir: u64, ) -> Self
Creates a new Gas struct with a regular gas budget and reservoir (EIP-8037 reservoir model).
Following the EIP-8037 spec:
remaining = limit(regular gas available, i.e.gas_left)reservoir= state gas pool (separate fromremaining)- Total gas available =
remaining + reservoir = limit + reservoir
§Arguments
limit: regular gas budget (capped execution gas, i.e.gas_left)reservoir: state gas pool (execution gas exceeding the regular gas cap)
Sourcepub const fn new_spent_with_reservoir(limit: u64, reservoir: u64) -> Self
pub const fn new_spent_with_reservoir(limit: u64, reservoir: u64) -> Self
Creates a new Gas struct with the given gas limit, but without any gas remaining.
Sourcepub const fn memory_mut(&mut self) -> &mut MemoryGas
pub const fn memory_mut(&mut self) -> &mut MemoryGas
Returns the memory gas.
Sourcepub const fn spent(&self) -> u64
👎Deprecated since 32.0.0: After EIP-8037 gas is split on
regular and state gas, this method is no longer valid.
Use Gas::total_gas_spent instead
pub const fn spent(&self) -> u64
After EIP-8037 gas is split on
regular and state gas, this method is no longer valid.
Use Gas::total_gas_spent instead
Returns the total amount of gas spent.
Sourcepub const fn total_gas_spent(&self) -> u64
pub const fn total_gas_spent(&self) -> u64
Returns the regular gas spent.
Sourcepub const fn used(&self) -> u64
pub const fn used(&self) -> u64
Returns the final amount of gas used by subtracting the refund from spent gas.
Sourcepub const fn spent_sub_refunded(&self) -> u64
pub const fn spent_sub_refunded(&self) -> u64
Returns the total amount of gas spent, minus the refunded gas.
Sourcepub const fn set_reservoir(&mut self, val: u64)
pub const fn set_reservoir(&mut self, val: u64)
Sets the state gas reservoir (used when propagating from child frame).
Sourcepub const fn state_gas_spent(&self) -> u64
pub const fn state_gas_spent(&self) -> u64
Returns total state gas spent so far.
Sourcepub const fn set_state_gas_spent(&mut self, val: u64)
pub const fn set_state_gas_spent(&mut self, val: u64)
Sets the total state gas spent (used when propagating from child frame).
Sourcepub const fn erase_cost(&mut self, returned: u64)
pub const fn erase_cost(&mut self, returned: u64)
Erases a gas cost from remaining (returns gas from child frame).
Sourcepub const fn spend_all(&mut self)
pub const fn spend_all(&mut self)
Spends all remaining gas excluding the reservoir.
On exceptional halt, the remaining gas must be zeroed to prevent state operations from succeeding via remaining gas.
Note that this does not affect the reservoir.
Sourcepub const fn record_refund(&mut self, refund: i64)
pub const fn record_refund(&mut self, refund: i64)
Records a refund value.
refund can be negative but self.refunded should always be positive
at the end of transact.
Sourcepub fn set_final_refund(&mut self, is_london: bool)
pub fn set_final_refund(&mut self, is_london: bool)
Set a refund value for final refund.
Max refund value is limited to Nth part (depending of fork) of gas spend.
Related to EIP-3529: Reduction in refunds
Sourcepub const fn set_refund(&mut self, refund: i64)
pub const fn set_refund(&mut self, refund: i64)
Set a refund value. This overrides the current refund value.
Sourcepub const fn set_remaining(&mut self, remaining: u64)
pub const fn set_remaining(&mut self, remaining: u64)
Set a remaining value. This overrides the current remaining value.
Sourcepub const fn set_spent(&mut self, spent: u64)
pub const fn set_spent(&mut self, spent: u64)
Set a spent value. This overrides the current spent value.
Sourcepub const fn record_cost(&mut self, cost: u64) -> bool
👎Deprecated since 32.0.0: use record_regular_cost instead
pub const fn record_cost(&mut self, cost: u64) -> bool
use record_regular_cost instead
Records a regular gas cost (EIP-8037 reservoir model).
Deducts from remaining and checks against implicit gas_left budget.
Regular gas charges cannot draw from the reservoir.
Returns false if the regular gas limit is exceeded.
On failure, values contain wrapped (invalid) state — callers must not read after OOG.
Sourcepub const fn record_cost_unsafe(&mut self, cost: u64) -> bool
pub const fn record_cost_unsafe(&mut self, cost: u64) -> bool
Records an explicit cost without bounds checking (unsafe path).
Returns true if the gas limit is exceeded. Values wrap on underflow.
Only the regular gas check is meaningful here; total remaining can underflow
without consequence if the caller handles it.
Sourcepub const fn record_state_cost(&mut self, cost: u64) -> bool
pub const fn record_state_cost(&mut self, cost: u64) -> bool
Records a state gas cost (EIP-8037 reservoir model).
State gas charges deduct from the reservoir first. If the reservoir is exhausted,
remaining charges spill into gas_left (requiring total remaining >= cost).
Tracks state gas spent.
Returns false if total remaining gas is insufficient.
Sourcepub const fn record_regular_cost(&mut self, cost: u64) -> bool
pub const fn record_regular_cost(&mut self, cost: u64) -> bool
Deducts from remaining only (used for child frame gas forwarding).
Does not affect reservoir or regular gas budget.
Used for forwarding gas to child frames.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Gas
impl<'de> Deserialize<'de> for Gas
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Copy for Gas
impl Eq for Gas
impl StructuralPartialEq for Gas
Auto Trait Implementations§
impl Freeze for Gas
impl RefUnwindSafe for Gas
impl Send for Gas
impl Sync for Gas
impl Unpin for Gas
impl UnsafeUnpin for Gas
impl UnwindSafe for Gas
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.