Skip to content

Commit

Permalink
Migrate metering to new Context API
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb authored and epilys committed Jun 30, 2022
1 parent 845d868 commit 08c9880
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions lib/middlewares/src/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,12 @@ impl<F: Fn(&Operator) -> u64 + Send + Sync> FunctionMiddleware for FunctionMeter
/// matches!(get_remaining_points(instance), MeteringPoints::Remaining(points) if points > 0)
/// }
/// ```
pub fn get_remaining_points<T>(
mut ctx: impl AsContextMut<Data = T>,
instance: &Instance,
) -> MeteringPoints {
pub fn get_remaining_points(ctx: &mut impl AsContextMut, instance: &Instance) -> MeteringPoints {
let exhausted: i32 = instance
.exports
.get_global("wasmer_metering_points_exhausted")
.expect("Can't get `wasmer_metering_points_exhausted` from Instance")
.get(&mut ctx)
.get(ctx)
.try_into()
.expect("`wasmer_metering_points_exhausted` from Instance has wrong type");

Expand All @@ -301,7 +298,7 @@ pub fn get_remaining_points<T>(
.exports
.get_global("wasmer_metering_remaining_points")
.expect("Can't get `wasmer_metering_remaining_points` from Instance")
.get(&mut ctx)
.get(ctx)
.try_into()
.expect("`wasmer_metering_remaining_points` from Instance has wrong type");

Expand Down Expand Up @@ -334,23 +331,19 @@ pub fn get_remaining_points<T>(
/// set_remaining_points(instance, new_limit);
/// }
/// ```
pub fn set_remaining_points<T>(
mut ctx: impl AsContextMut<Data = T>,
instance: &Instance,
points: u64,
) {
pub fn set_remaining_points(ctx: &mut impl AsContextMut, instance: &Instance, points: u64) {
instance
.exports
.get_global("wasmer_metering_remaining_points")
.expect("Can't get `wasmer_metering_remaining_points` from Instance")
.set(&mut ctx, points.into())
.set(ctx, points.into())
.expect("Can't set `wasmer_metering_remaining_points` in Instance");

instance
.exports
.get_global("wasmer_metering_points_exhausted")
.expect("Can't get `wasmer_metering_points_exhausted` from Instance")
.set(&mut ctx, 0i32.into())
.set(ctx, 0i32.into())
.expect("Can't set `wasmer_metering_points_exhausted` in Instance");
}

Expand Down Expand Up @@ -395,12 +388,12 @@ mod tests {
compiler_config.push_middleware(metering);
let store = Store::new_with_engine(&Universal::new(compiler_config).engine());
let module = Module::new(&store, bytecode()).unwrap();
let mut ctx = Context::new(module.store(), ());
let mut ctx = WasmerContext::new(&store, ());

// Instantiate
let instance = Instance::new(&mut ctx, &module, &imports! {}).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx.as_context_mut(), &instance),
MeteringPoints::Remaining(10)
);

Expand All @@ -418,21 +411,21 @@ mod tests {
.unwrap();
add_one.call(&mut ctx.as_context_mut(), 1).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx.as_context_mut(), &instance),
MeteringPoints::Remaining(6)
);

// Second call
add_one.call(&mut ctx.as_context_mut(), 1).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx.as_context_mut(), &instance),
MeteringPoints::Remaining(2)
);

// Third call fails due to limit
assert!(add_one.call(&mut ctx.as_context_mut(), 1).is_err());
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx.as_context_mut(), &instance),
MeteringPoints::Exhausted
);
}
Expand All @@ -449,7 +442,7 @@ mod tests {
// Instantiate
let instance = Instance::new(&mut ctx, &module, &imports! {}).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Remaining(10)
);
let add_one: TypedFunction<i32, i32> = instance
Expand All @@ -460,37 +453,37 @@ mod tests {
.unwrap();

// Increase a bit to have enough for 3 calls
set_remaining_points(ctx.as_context_mut(), &instance, 12);
set_remaining_points(&mut ctx, &instance, 12);

// Ensure we can use the new points now
add_one.call(&mut ctx.as_context_mut(), 1).unwrap();
add_one.call(&mut &mut ctx, 1).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Remaining(8)
);

add_one.call(&mut ctx.as_context_mut(), 1).unwrap();
add_one.call(&mut &mut ctx, 1).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Remaining(4)
);

add_one.call(&mut ctx.as_context_mut(), 1).unwrap();
add_one.call(&mut &mut ctx, 1).unwrap();
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Remaining(0)
);

assert!(add_one.call(&mut ctx.as_context_mut(), 1).is_err());
assert!(add_one.call(&mut &mut ctx, 1).is_err());
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Exhausted
);

// Add some points for another call
set_remaining_points(ctx.as_context_mut(), &instance, 4);
set_remaining_points(&mut ctx, &instance, 4);
assert_eq!(
get_remaining_points(ctx.as_context_mut(), &instance),
get_remaining_points(&mut ctx, &instance),
MeteringPoints::Remaining(4)
);
}
Expand Down

0 comments on commit 08c9880

Please sign in to comment.