Skip to content

Commit

Permalink
Merge pull request #19 from axic/checked-u64
Browse files Browse the repository at this point in the history
Add AsCheckedU64 trait to simplify gas checks
  • Loading branch information
axic authored Apr 25, 2019
2 parents 59a071c + 9471640 commit 029e822
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ use self::vm::{

use ewasm_api::types::{Bytes20, Bytes32, Uint128};

trait AsCheckedU64 {
fn as_checked_u64(&self) -> u64;
}

impl AsCheckedU64 for U256 {
fn as_checked_u64(&self) -> u64 {
if self > &U256::from("ffffffffffffffff") {
ewasm_api::abort();
}
self.as_u64()
}
}

// For some explanation see ethcore/vm/src/tests.rs::FakeExt

#[derive(Default)]
Expand Down Expand Up @@ -119,9 +132,7 @@ impl vm::Ext for EwasmExt {
call_type: CallType,
trap: bool,
) -> ::std::result::Result<MessageCallResult, TrapKind> {
// FIXME: set this properly
//let gas_limit = u64::from(gas);
let gas_limit = gas.as_u64();
let gas_limit = gas.as_checked_u64();

// FIXME: might not be good enough
let gas_start = ewasm_api::gas_left();
Expand Down Expand Up @@ -332,7 +343,7 @@ pub extern "C" fn main() {
// Could run `result.finalize(ext)` here, but processing manually seemed simpler.
match result {
Ok(Ok(GasLeft::Known(gas_left))) => {
ewasm_api::consume_gas(startgas - gas_left.as_u64());
ewasm_api::consume_gas(startgas - gas_left.as_checked_u64());
if ext.selfdestruct_address.is_some() {
let beneficiary: [u8; 20] = ext.selfdestruct_address.unwrap().into();
ewasm_api::selfdestruct(&ewasm_api::types::Bytes20 { bytes: beneficiary })
Expand All @@ -345,7 +356,7 @@ pub extern "C" fn main() {
data,
apply_state,
})) => {
ewasm_api::consume_gas(startgas - gas_left.as_u64());
ewasm_api::consume_gas(startgas - gas_left.as_checked_u64());
if apply_state {
ewasm_api::finish_data(&data.deref())
} else {
Expand Down

0 comments on commit 029e822

Please sign in to comment.