pub struct ResourceMeter<T: Config, S: State> { /* private fields */ }Expand description
Resource meter tracking weight and storage deposit consumption.
Implementations§
Source§impl<T: Config, S: State> ResourceMeter<T, S>
impl<T: Config, S: State> ResourceMeter<T, S>
Sourcepub fn new_nested(
&self,
limit: &CallResources<T>,
) -> Result<FrameMeter<T>, DispatchError>
pub fn new_nested( &self, limit: &CallResources<T>, ) -> Result<FrameMeter<T>, DispatchError>
Create a new nested meter with derived resource limits.
Sourcepub fn absorb_weight_meter_only(&mut self, other: FrameMeter<T>)
pub fn absorb_weight_meter_only(&mut self, other: FrameMeter<T>)
Absorb only the weight consumption from a nested frame meter.
Sourcepub fn absorb_all_meters(
&mut self,
other: FrameMeter<T>,
contract: &T::AccountId,
info: Option<&mut ContractInfo<T>>,
)
pub fn absorb_all_meters( &mut self, other: FrameMeter<T>, contract: &T::AccountId, info: Option<&mut ContractInfo<T>>, )
Absorb all resource consumption from a nested frame meter.
Sourcepub fn charge_weight_token<Tok: Token<T>>(
&mut self,
token: Tok,
) -> Result<ChargedAmount, DispatchError>
pub fn charge_weight_token<Tok: Token<T>>( &mut self, token: Tok, ) -> Result<ChargedAmount, DispatchError>
Charge a weight token against this meter’s remaining weight limit.
Returns Err(Error::OutOfGas) if the weight limit would be exceeded.
Sourcepub fn charge_or_halt<Tok: Token<T>>(
&mut self,
token: Tok,
) -> ControlFlow<Halt, ChargedAmount>
pub fn charge_or_halt<Tok: Token<T>>( &mut self, token: Tok, ) -> ControlFlow<Halt, ChargedAmount>
Try to charge a weight token or halt if not enough weight is left.
Sourcepub fn adjust_weight<Tok: Token<T>>(
&mut self,
charged_amount: ChargedAmount,
token: Tok,
)
pub fn adjust_weight<Tok: Token<T>>( &mut self, charged_amount: ChargedAmount, token: Tok, )
Adjust an earlier weight charge with the actual weight consumed.
Sourcepub fn sync_from_executor(
&mut self,
engine_fuel: Gas,
) -> Result<(), DispatchError>
pub fn sync_from_executor( &mut self, engine_fuel: Gas, ) -> Result<(), DispatchError>
Synchronize meter state with PolkaVM executor’s fuel consumption.
Maps the VM’s internal fuel accounting to weight consumption:
- Converts engine fuel units to weight units
- Updates meter state to match actual VM resource usage
Sourcepub fn sync_to_executor(&mut self) -> Gas
pub fn sync_to_executor(&mut self) -> Gas
Convert meter state to PolkaVM executor fuel units.
Prepares for VM execution by:
- Computing remaining available weight
- Converting weight units to VM fuel units and return
Sourcepub fn consume_all_weight(&mut self)
pub fn consume_all_weight(&mut self)
Consume all remaining weight in the meter.
Sourcepub fn charge_deposit(
&mut self,
deposit: &StorageDeposit<BalanceOf<T>>,
) -> DispatchResult
pub fn charge_deposit( &mut self, deposit: &StorageDeposit<BalanceOf<T>>, ) -> DispatchResult
Record a storage deposit charge against this meter.
Sourcepub fn eth_gas_left(&self) -> Option<BalanceOf<T>>
pub fn eth_gas_left(&self) -> Option<BalanceOf<T>>
Get remaining ethereum gas equivalent.
Converts remaining resources to ethereum gas units:
- For ethereum mode: computes directly from gas accounting
- For substrate mode: converts weight+deposit to gas equivalent Returns None if resources are exhausted or conversion fails.
Sourcepub fn weight_left(&self) -> Option<Weight>
pub fn weight_left(&self) -> Option<Weight>
Get remaining weight available.
Computes remaining computational capacity:
- For ethereum mode: converts from gas to weight units
- For substrate mode: subtracts consumed from weight limit Returns None if resources are exhausted.
Sourcepub fn deposit_left(&self) -> Option<BalanceOf<T>>
pub fn deposit_left(&self) -> Option<BalanceOf<T>>
Get remaining deposit available.
Computes remaining storage deposit allowance:
- For ethereum mode: converts from gas to deposit units
- For substrate mode: subtracts consumed from deposit limit Returns None if resources are exhausted.
Sourcepub fn total_consumed_gas(&self) -> BalanceOf<T>
pub fn total_consumed_gas(&self) -> BalanceOf<T>
Calculate total gas consumed so far.
Computes the ethereum-gas equivalent of all resource usage:
- Converts weight and deposit consumption to gas units
- For ethereum mode: uses direct gas accounting
- For substrate mode: synthesizes from weight+deposit usage
Sourcepub fn weight_consumed(&self) -> Weight
pub fn weight_consumed(&self) -> Weight
Get total weight consumed
Sourcepub fn weight_required(&self) -> Weight
pub fn weight_required(&self) -> Weight
Get total weight required This is the maximum amount of weight consumption that occurred during execution so far This is relevant because consumed weight can decrease in case it is asjusted a posteriori for some operations
Sourcepub fn deposit_consumed(&self) -> StorageDeposit<BalanceOf<T>>
pub fn deposit_consumed(&self) -> StorageDeposit<BalanceOf<T>>
Get total storage deposit consumed in the current frame.
Returns the net storage deposit change from this frame,
Sourcepub fn deposit_required(&self) -> StorageDeposit<BalanceOf<T>>
pub fn deposit_required(&self) -> StorageDeposit<BalanceOf<T>>
Get maximum storage deposit required at any point.
Returns the highest deposit amount needed during execution, accounting for temporary storage spikes before later refunds.
Sourcepub fn eth_gas_consumed(&self) -> BalanceOf<T>
pub fn eth_gas_consumed(&self) -> BalanceOf<T>
Get the Ethereum gas that has been consumed during the lifetime of this meter
Source§impl<T: Config> ResourceMeter<T, Root>
impl<T: Config> ResourceMeter<T, Root>
Sourcepub fn new(
transaction_limits: TransactionLimits<T>,
) -> Result<Self, DispatchError>
pub fn new( transaction_limits: TransactionLimits<T>, ) -> Result<Self, DispatchError>
Create a new transaction-level meter with the specified resource limits.
Initializes either:
- An ethereum-style gas-based meter or
- A substrate-style meter with explicit weight and deposit limits
Sourcepub fn new_from_limits(
weight_limit: Weight,
deposit_limit: BalanceOf<T>,
) -> Result<Self, DispatchError>
pub fn new_from_limits( weight_limit: Weight, deposit_limit: BalanceOf<T>, ) -> Result<Self, DispatchError>
Convenience constructor for substrate-style weight+deposit limits.
Sourcepub fn execute_postponed_deposits(
&mut self,
origin: &Origin<T>,
exec_config: &ExecConfig<T>,
) -> Result<StorageDeposit<BalanceOf<T>>, DispatchError>
pub fn execute_postponed_deposits( &mut self, origin: &Origin<T>, exec_config: &ExecConfig<T>, ) -> Result<StorageDeposit<BalanceOf<T>>, DispatchError>
Execute all postponed storage deposit operations.
Returns Err(Error::StorageDepositNotEnoughFunds) if deposit limit would be exceeded.
Sourcepub fn terminate(
&mut self,
contract_account: T::AccountId,
refunded: BalanceOf<T>,
)
pub fn terminate( &mut self, contract_account: T::AccountId, refunded: BalanceOf<T>, )
Mark a contract as terminated
This will signal to the meter to discard all charged and refunds incured by this
contract. Furthermore it will record that there was a refund of refunded and adapt the
total deposit accordingly
Source§impl<T: Config> ResourceMeter<T, Nested>
impl<T: Config> ResourceMeter<T, Nested>
Sourcepub fn charge_contract_deposit_and_transfer(
&mut self,
contract: T::AccountId,
amount: StorageDeposit<BalanceOf<T>>,
) -> DispatchResult
pub fn charge_contract_deposit_and_transfer( &mut self, contract: T::AccountId, amount: StorageDeposit<BalanceOf<T>>, ) -> DispatchResult
Record a contract’s storage deposit and schedule the transfer.
Updates the frame’s deposit accounting and schedules the actual token transfer for later execution – at the end of the transaction execution.
Sourcepub fn record_contract_storage_changes(&mut self, diff: &Diff) -> DispatchResult
pub fn record_contract_storage_changes(&mut self, diff: &Diff) -> DispatchResult
Record storage changes of a contract.
Sourcepub fn finalize(&mut self, info: Option<&mut ContractInfo<T>>) -> DispatchResult
pub fn finalize(&mut self, info: Option<&mut ContractInfo<T>>) -> DispatchResult
Self::charge_contract_deposit_and_transfer and Self::record_contract_storage_changes
does not enforce the storage limit since we want to do this check as late as possible to
allow later refunds to offset earlier charges.
Trait Implementations§
Auto Trait Implementations§
impl<T, S> Freeze for ResourceMeter<T, S>
impl<T, S> RefUnwindSafe for ResourceMeter<T, S>where
<T as Config>::RuntimeEvent: Sized,
<T as Config>::Balance: RefUnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
<T as Config>::AccountId: RefUnwindSafe,
impl<T, S> Send for ResourceMeter<T, S>
impl<T, S> Sync for ResourceMeter<T, S>
impl<T, S> Unpin for ResourceMeter<T, S>
impl<T, S> UnwindSafe for ResourceMeter<T, S>where
<T as Config>::RuntimeEvent: Sized,
<T as Config>::Balance: UnwindSafe,
S: UnwindSafe,
T: UnwindSafe,
<T as Config>::AccountId: UnwindSafe,
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> CheckedConversion for T
impl<T> CheckedConversion for T
Source§impl<T, U> DefensiveTruncateInto<U> for Twhere
U: DefensiveTruncateFrom<T>,
impl<T, U> DefensiveTruncateInto<U> for Twhere
U: DefensiveTruncateFrom<T>,
Source§fn defensive_truncate_into(self) -> U
fn defensive_truncate_into(self) -> U
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
impl<Src, Dest> IntoTuple<Dest> for Srcwhere
Dest: FromTuple<Src>,
fn into_tuple(self) -> Dest
Source§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§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 moreSource§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 moreSource§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
Source§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
Source§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.Source§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.Source§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.Source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
Source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
Source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read moreSource§impl<T> Tap for T
impl<T> Tap for T
Source§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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 moreSource§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§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.Source§impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
type Error = <U as TryFromKey<T>>::Error
fn try_into_key(self) -> Result<U, <U as TryFromKey<T>>::Error>
Source§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
Source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.Source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
Source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.