Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
ethcore: handle vm exception when estimating gas (#9615)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva authored and ordian committed Oct 26, 2018
1 parent b395da9 commit 708e0e5
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,28 +1486,34 @@ impl Call for Client {
let sender = t.sender();
let options = || TransactOptions::with_tracing().dont_check_nonce();

let cond = |gas| {
let exec = |gas| {
let mut tx = t.as_unsigned().clone();
tx.gas = gas;
let tx = tx.fake_sign(sender);

let mut clone = state.clone();
Ok(Executive::new(&mut clone, &env_info, self.engine.machine())
Executive::new(&mut clone, &env_info, self.engine.machine())
.transact_virtual(&tx, options())
.ok()
.map(|r| r.exception.is_none())
.unwrap_or(false))
};

if !cond(upper)? {
let cond = |gas| exec(gas).unwrap_or(false);

if !cond(upper) {
upper = max_upper;
if !cond(upper)? {
trace!(target: "estimate_gas", "estimate_gas failed with {}", upper);
let err = ExecutionError::Internal(format!("Requires higher than upper limit of {}", upper));
return Err(err.into())
match exec(upper) {
Some(false) => return Err(CallError::Exceptional),
None => {
trace!(target: "estimate_gas", "estimate_gas failed with {}", upper);
let err = ExecutionError::Internal(format!("Requires higher than upper limit of {}", upper));
return Err(err.into())
},
_ => {},
}
}
let lower = t.gas_required(&self.engine.schedule(env_info.number)).into();
if cond(lower)? {
if cond(lower) {
trace!(target: "estimate_gas", "estimate_gas succeeded with {}", lower);
return Ok(lower)
}
Expand All @@ -1516,12 +1522,12 @@ impl Call for Client {
/// Returns the lowest value between `lower` and `upper` for which `cond` returns true.
/// We assert: `cond(lower) = false`, `cond(upper) = true`
fn binary_chop<F, E>(mut lower: U256, mut upper: U256, mut cond: F) -> Result<U256, E>
where F: FnMut(U256) -> Result<bool, E>
where F: FnMut(U256) -> bool
{
while upper - lower > 1.into() {
let mid = (lower + upper) / 2.into();
trace!(target: "estimate_gas", "{} .. {} .. {}", lower, mid, upper);
let c = cond(mid)?;
let c = cond(mid);
match c {
true => upper = mid,
false => lower = mid,
Expand Down

0 comments on commit 708e0e5

Please sign in to comment.