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

Commit

Permalink
cleanup a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
NikVolf committed Feb 9, 2018
1 parent 80d1088 commit e28fbb5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 111 deletions.
235 changes: 124 additions & 111 deletions ethcore/src/executive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,107 @@ mod tests {
assert_eq!(state.storage_at(&contract_address, &H256::from(&U256::zero())).unwrap(), H256::from(&U256::from(0)));
}

fn wasm_sample_code() -> Arc<Vec<u8>> {
Arc::new(wat2wasm(r#"
(module
(type (;0;) (func (param i32 i32)))
(type (;1;) (func (param i32)))
(type (;2;) (func))
(import "env" "ret" (func (;0;) (type 0)))
(import "env" "sender" (func (;1;) (type 1)))
(import "env" "memory" (memory (;0;) 1 16))
(func (;2;) (type 2)
(local i32 i32 i32 i32 i32 i64)
(i32.store offset=4
(i32.const 0)
(tee_local 4
(i32.sub
(i32.load offset=4
(i32.const 0))
(i32.const 64))))
(i32.store
(tee_local 0
(i32.add
(i32.add
(get_local 4)
(i32.const 44))
(i32.const 16)))
(i32.const 0))
(i64.store align=4
(tee_local 1
(i32.add
(i32.add
(get_local 4)
(i32.const 44))
(i32.const 8)))
(i64.const 0))
(i32.store
(tee_local 2
(i32.add
(i32.add
(get_local 4)
(i32.const 24))
(i32.const 16)))
(i32.const 0))
(i64.store
(tee_local 3
(i32.add
(i32.add
(get_local 4)
(i32.const 24))
(i32.const 8)))
(i64.const 0))
(i64.store offset=44 align=4
(get_local 4)
(i64.const 0))
(i32.store offset=28
(get_local 4)
(i32.const 0))
(i32.store offset=24
(get_local 4)
(i32.const 0))
(call 1
(i32.add
(get_local 4)
(i32.const 24)))
(i32.store
(get_local 0)
(tee_local 2
(i32.load
(get_local 2))))
(i64.store align=4
(get_local 1)
(tee_local 5
(i64.load
(get_local 3))))
(i32.store
(i32.add
(get_local 4)
(i32.const 16))
(get_local 2))
(i64.store
(i32.add
(get_local 4)
(i32.const 8))
(get_local 5))
(i64.store offset=44 align=4
(get_local 4)
(tee_local 5
(i64.load offset=24
(get_local 4))))
(i64.store
(get_local 4)
(get_local 5))
(call 0
(get_local 4)
(i32.const 20)))
(table (;0;) 0 anyfunc)
(export "call" (func 2))
(data (i32.const 4) "\10\c0\00\00"))
"#).unwrap()
)
}

#[test]
fn wasm_activated_test() {
let contract_address = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
Expand All @@ -1516,130 +1617,42 @@ mod tests {
state.add_balance(&sender, &U256::from(10000000000u64), CleanupMode::NoEmpty).unwrap();
state.commit().unwrap();

let mut params = ActionParams::default();
let mut params = ActionParams::default()
.from_origin(sender.clone());
params.address = contract_address.clone();
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(20025);
params.code = Some(Arc::new(
wat2wasm(r#"
(module
(type (;0;) (func (param i32 i32)))
(type (;1;) (func (param i32)))
(type (;2;) (func))
(import "env" "ret" (func (;0;) (type 0)))
(import "env" "sender" (func (;1;) (type 1)))
(import "env" "memory" (memory (;0;) 1 16))
(func (;2;) (type 2)
(local i32 i32 i32 i32 i32 i64)
i32.const 0
i32.const 0
i32.load offset=4
i32.const 64
i32.sub
tee_local 4
i32.store offset=4
get_local 4
i32.const 44
i32.add
i32.const 16
i32.add
tee_local 0
i32.const 0
i32.store
get_local 4
i32.const 44
i32.add
i32.const 8
i32.add
tee_local 1
i64.const 0
i64.store align=4
get_local 4
i32.const 24
i32.add
i32.const 16
i32.add
tee_local 2
i32.const 0
i32.store
get_local 4
i32.const 24
i32.add
i32.const 8
i32.add
tee_local 3
i64.const 0
i64.store
get_local 4
i64.const 0
i64.store offset=44 align=4
get_local 4
i32.const 0
i32.store offset=28
get_local 4
i32.const 0
i32.store offset=24
get_local 4
i32.const 24
i32.add
call 1
get_local 0
get_local 2
i32.load
tee_local 2
i32.store
get_local 1
get_local 3
i64.load
tee_local 5
i64.store align=4
get_local 4
i32.const 16
i32.add
get_local 2
i32.store
get_local 4
i32.const 8
i32.add
get_local 5
i64.store
get_local 4
get_local 4
i64.load offset=24
tee_local 5
i64.store offset=44 align=4
get_local 4
get_local 5
i64.store
get_local 4
i32.const 20
call 0
i32.const 0
get_local 4
i32.const 64
i32.add
i32.store offset=4)
(table (;0;) 0 anyfunc)
(export "call" (func 2))
(data (i32.const 4) "\10\c0\00\00"))
"#).unwrap()
));
params.value = ActionValue::Transfer(U256::zero());
params.code = Some(wasm_sample_code());

let mut info = EnvInfo::default();

// 100 > 10
info.number = 100;

// Network with wasm activated at block 10
let machine = ::ethereum::new_kovan_wasm_test_machine();
let mut substate = Substate::new();

let mut output = [0u8; 20];
let FinalizationResult { gas_left: result, .. } = {
let mut ex = Executive::new(&mut state, &info, &machine);
ex.call(params, &mut substate, BytesRef::Fixed(&mut output), &mut NoopTracer, &mut NoopVMTracer).unwrap()
ex.call(params.clone(), &mut Substate::new(), BytesRef::Fixed(&mut output), &mut NoopTracer, &mut NoopVMTracer).unwrap()
};

assert_eq!(result, U256::from(18433));
assert_eq!(result, U256::from(18435));
// Transaction successfully returned sender
assert_eq!(output[..], sender[..]);

// 1 < 10
info.number = 1;

let machine = ::ethereum::new_kovan_wasm_test_machine();
let mut output = [0u8; 20];
let FinalizationResult { gas_left: result, .. } = {
let mut ex = Executive::new(&mut state, &info, &machine);
ex.call(params, &mut Substate::new(), BytesRef::Fixed(&mut output), &mut NoopTracer, &mut NoopVMTracer).unwrap()
};

assert_eq!(result, U256::from(20025));
// Since transaction errored due to wasm was not activated, result is just empty
assert_eq!(output[..], [0u8; 20][..]);
}
}
8 changes: 8 additions & 0 deletions ethcore/vm/src/action_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ impl Default for ActionParams {
}
}

impl ActionParams {
pub fn from_origin(mut self, address: Address) -> Self {
self.origin = address;
self.sender = address;
self
}
}

impl From<ethjson::vm::Transaction> for ActionParams {
fn from(t: ethjson::vm::Transaction) -> Self {
let address: Address = t.address.into();
Expand Down

0 comments on commit e28fbb5

Please sign in to comment.