Skip to content

Commit

Permalink
Add accessors and modifiers for fuel amount and FuelCosts
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 authored and graydon committed Dec 1, 2023
1 parent da85b60 commit b220780
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/wasmi/src/engine/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ impl Config {
self
}

pub fn set_fuel_costs(&mut self, costs: FuelCosts) -> &mut Self {
self.fuel_costs = costs;
self
}

/// Returns the [`FuelConsumptionMode`] for the [`Engine`].
///
/// Returns `None` if fuel metering is disabled for the [`Engine`].
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests;
pub use self::{
bytecode::DropKeep,
code_map::CompiledFunc,
config::{Config, FuelConsumptionMode},
config::{Config, FuelConsumptionMode, FuelCosts},
func_builder::{
FuncBuilder,
FuncTranslatorAllocations,
Expand Down
18 changes: 17 additions & 1 deletion crates/wasmi/src/func/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Caller<'a, T> {

impl<'a, T> Caller<'a, T> {
/// Creates a new [`Caller`] from the given store context and [`Instance`] handle.
pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
pub fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
where
C: AsContextMut<UserState = T>,
{
Expand Down Expand Up @@ -69,6 +69,13 @@ impl<'a, T> Caller<'a, T> {
self.ctx.store.fuel_consumed()
}

/// Returns the amount of total [`Fuel`] supplied to the [`Store`](crate::Store).
///
/// Returns `None` if fuel metering is disabled.
pub fn fuel_total(&self) -> Option<u64> {
self.ctx.store.fuel_total()
}

/// Synthetically consumes an amount of fuel for the [`Store`](crate::Store).
///
/// Returns the remaining amount of fuel after this operation.
Expand All @@ -84,6 +91,15 @@ impl<'a, T> Caller<'a, T> {
pub fn consume_fuel(&mut self, delta: u64) -> Result<u64, FuelError> {
self.ctx.store.consume_fuel(delta)
}

/// Resets the total and consumed amounts of fuel to 0 for the [`Store`](crate::Store).
///
/// # Errors
///
/// - If fuel metering is disabled.
pub fn reset_fuel(&mut self) -> Result<(), FuelError> {
self.ctx.store.reset_fuel()
}
}

impl<T> AsContext for Caller<'_, T> {
Expand Down
1 change: 1 addition & 0 deletions crates/wasmi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub use self::{
Config,
Engine,
FuelConsumptionMode,
FuelCosts,
ResumableCall,
ResumableInvocation,
StackLimits,
Expand Down
26 changes: 25 additions & 1 deletion crates/wasmi/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub struct Fuel {
/// The remaining fuel.
remaining: u64,
/// The total amount of fuel so far.
total: u64,
pub(crate) total: u64,
}

impl Fuel {
Expand Down Expand Up @@ -256,6 +256,12 @@ impl Fuel {
.ok_or(TrapCode::OutOfFuel)?;
Ok(self.remaining)
}

/// Reset the total and remaining fuel amounts to 0.
pub fn reset_fuel(&mut self) {
self.remaining = 0;
self.total = 0;
}
}

impl StoreInner {
Expand Down Expand Up @@ -849,6 +855,14 @@ impl<T> Store<T> {
Some(self.inner.fuel.fuel_consumed())
}

/// Returns the amount of total [`Fuel`] supplied to the [`Store`].
///
/// Returns `None` if fuel metering is disabled.
pub fn fuel_total(&self) -> Option<u64> {
self.check_fuel_metering_enabled().ok()?;
Some(self.inner.fuel.total)
}

/// Synthetically consumes an amount of fuel for the [`Store`].
///
/// Returns the remaining amount of fuel after this operation.
Expand All @@ -869,6 +883,16 @@ impl<T> Store<T> {
.map_err(|_error| FuelError::out_of_fuel())
}

/// Resets the total and consumed amounts of fuel to 0 for the [`Store`].
///
/// # Errors
///
/// - If fuel metering is disabled.
pub fn reset_fuel(&mut self) -> Result<(), FuelError> {
self.check_fuel_metering_enabled()?;
Ok(self.inner.fuel.reset_fuel())
}

/// Allocates a new [`TrampolineEntity`] and returns a [`Trampoline`] reference to it.
pub(super) fn alloc_trampoline(&mut self, func: TrampolineEntity<T>) -> Trampoline {
let idx = self.trampolines.alloc(func);
Expand Down

0 comments on commit b220780

Please sign in to comment.