From 000d8fe8d023e2060ac4a6d6ffaddc4ac47c433a Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 24 Dec 2015 01:07:46 +0100 Subject: [PATCH] runtime_data, tests in progress --- rust-evmjit/src/lib.rs | 4 ++-- src/evm/jit.rs | 37 +++++++++++++++++++++++++++++++++++-- src/evm/runtime_data.rs | 8 ++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/rust-evmjit/src/lib.rs b/rust-evmjit/src/lib.rs index da52f3412..75cdbeea7 100644 --- a/rust-evmjit/src/lib.rs +++ b/rust-evmjit/src/lib.rs @@ -271,12 +271,12 @@ pub mod ffi { } #[no_mangle] - pub unsafe extern fn env_sha3(begin: *const u8, size: *const u64, out_hash: *mut JitI256) { + pub unsafe extern fn env_sha3(begin: *const u8, size: u64, out_hash: *mut JitI256) { // TODO: write tests // it may be incorrect due to endianess // if it is, don't use `from_raw_parts` let out_hash = &mut *out_hash; - let input = slice::from_raw_parts(begin, *size as usize); + let input = slice::from_raw_parts(begin, size as usize); let outlen = out_hash.words.len() * 8; let output = slice::from_raw_parts_mut(out_hash.words.as_mut_ptr() as *mut u8, outlen); let mut sha3 = Keccak::new_sha3_256(); diff --git a/src/evm/jit.rs b/src/evm/jit.rs index 97033eaba..72b71cec3 100644 --- a/src/evm/jit.rs +++ b/src/evm/jit.rs @@ -3,6 +3,7 @@ use evmjit; use util::hash::*; use util::uint::*; use util::bytes::*; +use util::sha3::*; use evm; /// Should be used to convert jit types to ethcore @@ -56,6 +57,37 @@ impl IntoJit for H256 { } } +impl IntoJit for Address { + fn into_jit(self) -> evmjit::I256 { + H256::from(self).into_jit() + } +} + +impl IntoJit for evm::RuntimeData { + fn into_jit(self) -> evmjit::RuntimeDataHandle { + let mut data = evmjit::RuntimeDataHandle::new(); + data.gas = self.gas as i64; + data.gas_price = self.gas_price as i64; + data.call_data = self.call_data.as_ptr(); + data.call_data_size = self.call_data.len() as u64; + mem::forget(self.call_data); + data.address = self.address.into_jit(); + data.caller = self.caller.into_jit(); + data.origin = self.origin.into_jit(); + data.call_value = self.call_value.into_jit(); + data.coinbase = self.coinbase.into_jit(); + data.difficulty = self.difficulty.into_jit(); + data.gas_limit = self.gas_limit.into_jit(); + data.number = self.number; + data.timestamp = self.timestamp as i64; + data.code = self.code.as_ptr(); + data.code_size = self.code.len() as u64; + data.code_hash = self.code.sha3().into_jit(); + mem::forget(self.code); + data + } +} + pub struct EnvAdapter { env: evm::Env } @@ -158,9 +190,10 @@ mod tests { #[test] fn test_env_adapter() { - let data = RuntimeDataHandle::new(); + let mut data = RuntimeData::new(); + data.code = vec![0x60, 0x00, 0x60, 0x00, 0x20, 0x60, 0x00, 0x55]; let env = EnvAdapter::new(); - let mut context = ContextHandle::new(data, EnvHandle::new(env)); + let mut context = ContextHandle::new(data.into_jit(), EnvHandle::new(env)); assert_eq!(context.exec(), ReturnCode::Stop); } diff --git a/src/evm/runtime_data.rs b/src/evm/runtime_data.rs index a7211dcd3..7fba9362d 100644 --- a/src/evm/runtime_data.rs +++ b/src/evm/runtime_data.rs @@ -8,13 +8,13 @@ pub struct RuntimeData { pub address: Address, pub caller: Address, pub origin: Address, + pub call_value: U256, pub coinbase: Address, pub difficulty: U256, pub gas_limit: U256, pub number: u64, pub timestamp: u64, - pub code: Vec, - pub code_hash: H256 + pub code: Vec } impl RuntimeData { @@ -26,13 +26,13 @@ impl RuntimeData { address: Address::new(), caller: Address::new(), origin: Address::new(), + call_value: U256::from(0), coinbase: Address::new(), difficulty: U256::from(0), gas_limit: U256::from(0), number: 0, timestamp: 0, - code: vec![], - code_hash: H256::new() + code: vec![] } } }