Skip to content

Commit

Permalink
Updated metering middleware with cost setter
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Dec 4, 2020
1 parent 477058c commit 75c2f60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 9 additions & 0 deletions examples/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ fn main() -> anyhow::Result<()> {
"Remaining points: {:?}",
metering.get_remaining_points(&instance)
);

println!("Set new remaining points points to 10");
metering.set_remaining_points(&instance, 10);

println!(
"Remaining points: {:?}",
metering.get_remaining_points(&instance)
);

Ok(())
}

Expand Down
19 changes: 17 additions & 2 deletions lib/middlewares/src/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wasmer::wasmparser::{
};
use wasmer::{
ExportIndex, FunctionMiddleware, GlobalInit, GlobalType, Instance, LocalFunctionIndex,
MiddlewareReaderState, ModuleMiddleware, Mutability, Type,
MiddlewareReaderState, ModuleMiddleware, Mutability, Type, Value,
};
use wasmer_types::GlobalIndex;
use wasmer_vm::ModuleInfo;
Expand Down Expand Up @@ -53,14 +53,29 @@ impl<F: Fn(&Operator) -> u64 + Copy + Clone + Send + Sync> Metering<F> {
}
}

/// Get the remaining points in an Instance.
///
/// Important: the instance Module must been processed with the `Metering` middleware.
pub fn get_remaining_points(&self, instance: &Instance) -> u64 {
instance
.exports
.get_global("remaining_points")
.unwrap()
.expect("Can't get `remaining_points` from Instance")
.get()
.unwrap_i64() as _
}

/// Set the provided remaining points in an Instance.
///
/// Important: the instance Module must been processed with the `Metering` middleware.
pub fn set_remaining_points(&self, instance: &Instance, points: u64) {
instance
.exports
.get_global("remaining_points")
.expect("Can't get `remaining_points` from Instance")
.set(Value::I64(points as _))
.expect("Can't set `remaining_points` in Instance");
}
}

impl<F: Fn(&Operator) -> u64 + Copy + Clone + Send + Sync> fmt::Debug for Metering<F> {
Expand Down

0 comments on commit 75c2f60

Please sign in to comment.