Skip to content

Commit

Permalink
Handle gas depletion correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 30, 2020
1 parent b16ad45 commit 752a3ef
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ fn handle_panic() {
&to_vec(&HandleMsg::Panic {}).unwrap(),
);
match handle_res.unwrap_err() {
// TODO: Don't accept GasDepletion here (https://github.com/CosmWasm/cosmwasm/issues/501)
VmError::RuntimeErr { .. } | VmError::GasDepletion { .. } => {}
VmError::RuntimeErr { .. } => {}
err => panic!("Unexpected error: {:?}", err),
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ mod tests {
}

#[test]
#[ignore] // fails due to the inability to handle gas depletion (https://github.com/wasmerio/wasmer/issues/1931)
fn recovers_from_out_of_gas() {
let mut cache = unsafe { Cache::new(make_testing_options()).unwrap() };
let id = cache.save_wasm(CONTRACT).unwrap();
Expand Down
14 changes: 13 additions & 1 deletion packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,19 @@ impl<A: Api, S: Storage, Q: Querier> Environment<A, S, Q> {
}
None => Err(VmError::uninitialized_context_data("wasmer_instance")),
})?;
Ok(func.call(args)?)

func.call(args).map_err(|runtime_err| -> VmError {
self.with_context_data(|context_data| match context_data.wasmer_instance {
Some(instance_ptr) => {
let instance_ref = unsafe { instance_ptr.as_ref() };
match get_remaining_points(instance_ref) {
MeteringPoints::Remaining(_) => VmError::from(runtime_err),
MeteringPoints::Exhausted => VmError::gas_depletion(),
}
}
None => VmError::uninitialized_context_data("wasmer_instance"),
})
})
}

pub fn with_storage_from_context<C, T>(&self, callback: C) -> VmResult<T>
Expand Down

0 comments on commit 752a3ef

Please sign in to comment.