From c695e09bba42a6a3264bc156f5f706d44c8b53ba Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 23 Dec 2015 13:49:45 +0100 Subject: [PATCH] docs --- rust-evmjit/src/lib.rs | 9 +++-- src/evm/jit.rs | 78 ++++++++++++++++++++++++----------------- src/evm/mod.rs | 9 ++++- src/evm/runtime_data.rs | 38 ++++++++++++++++++++ 4 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 src/evm/runtime_data.rs diff --git a/rust-evmjit/src/lib.rs b/rust-evmjit/src/lib.rs index 5e11a27db..da52f3412 100644 --- a/rust-evmjit/src/lib.rs +++ b/rust-evmjit/src/lib.rs @@ -68,7 +68,8 @@ impl DerefMut for RuntimeDataHandle { /// Safe handle for jit context. pub struct ContextHandle { context: *mut JitContext, - _data_handle: RuntimeDataHandle + _data_handle: RuntimeDataHandle, + _env: EnvHandle } impl ContextHandle { @@ -77,7 +78,8 @@ impl ContextHandle { let context = unsafe { evmjit_create_context(data_handle.mut_runtime_data(), &mut env) }; ContextHandle { context: context, - _data_handle: data_handle + _data_handle: data_handle, + _env: env } } @@ -270,6 +272,9 @@ pub mod ffi { #[no_mangle] pub unsafe extern fn env_sha3(begin: *const u8, size: *const 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 outlen = out_hash.words.len() * 8; diff --git a/src/evm/jit.rs b/src/evm/jit.rs index 2efc5ce1c..97033eaba 100644 --- a/src/evm/jit.rs +++ b/src/evm/jit.rs @@ -5,18 +5,18 @@ use util::uint::*; use util::bytes::*; use evm; -/// Should be used to convert jit i256 to ethcore types -pub trait FromJit: Sized { - fn from_jit(input: &evmjit::I256) -> Self; +/// Should be used to convert jit types to ethcore +pub trait FromJit: Sized { + fn from_jit(input: T) -> Self; } -/// Should be used to covert ethcore types to jit i256 -pub trait IntoJit { - fn into_jit(self) -> evmjit::I256; +/// Should be used to covert ethcore types to jit +pub trait IntoJit { + fn into_jit(self) -> T; } -impl FromJit for U256 { - fn from_jit(input: &evmjit::I256) -> Self { +impl<'a> FromJit<&'a evmjit::I256> for U256 { + fn from_jit(input: &'a evmjit::I256) -> Self { let mut res: U256 = unsafe { mem::uninitialized() }; res.0[0] = input.words[3]; res.0[1] = input.words[2]; @@ -26,14 +26,14 @@ impl FromJit for U256 { } } -impl FromJit for H256 { - fn from_jit(input: &evmjit::I256) -> Self { +impl<'a> FromJit<&'a evmjit::I256> for H256 { + fn from_jit(input: &'a evmjit::I256) -> Self { let u = U256::from_jit(input); H256::from(&u) } } -impl IntoJit for U256 { +impl IntoJit for U256 { fn into_jit(self) -> evmjit::I256 { let mut res: evmjit::I256 = unsafe { mem::uninitialized() }; res.words[0] = self.0[3]; @@ -44,7 +44,7 @@ impl IntoJit for U256 { } } -impl IntoJit for H256 { +impl IntoJit for H256 { fn into_jit(self) -> evmjit::I256 { let mut ret = [0; 4]; for i in 0..self.bytes().len() { @@ -128,26 +128,40 @@ impl evmjit::Env for EnvAdapter { } } +#[cfg(test)] +mod tests { + use util::hash::*; + use util::uint::*; + use evmjit::{ContextHandle, RuntimeDataHandle, EnvHandle, ReturnCode}; + use evm::*; + use evm::jit::{FromJit, IntoJit}; -#[test] -fn test_to_and_from_u256() { - use std::str::FromStr; + #[test] + fn test_to_and_from_u256() { + use std::str::FromStr; + + let u = U256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); + let j = u.into_jit(); + let u2 = U256::from_jit(&j); + assert_eq!(u, u2); + } + + #[test] + fn test_to_and_from_h256() { + use std::str::FromStr; + + let h = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); + let j = h.clone().into_jit(); + let h2 = H256::from_jit(&j); + assert_eq!(h, h2); + } + + #[test] + fn test_env_adapter() { + let data = RuntimeDataHandle::new(); + let env = EnvAdapter::new(); + let mut context = ContextHandle::new(data, EnvHandle::new(env)); + assert_eq!(context.exec(), ReturnCode::Stop); + } - let u = U256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); - let j = u.into_jit(); - let u2 = U256::from_jit(&j); - assert_eq!(u, u2); } - -#[test] -fn test_to_and_from_h256() { - use std::str::FromStr; - - let h = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); - let j = h.clone().into_jit(); - println!("jit: {:?}", j); - let h2 = H256::from_jit(&j); - assert_eq!(h, h2); -} - - diff --git a/src/evm/mod.rs b/src/evm/mod.rs index fe429e186..a57e6bff4 100644 --- a/src/evm/mod.rs +++ b/src/evm/mod.rs @@ -1,5 +1,12 @@ +//! Ethereum virtual machine. + +pub mod env; +pub mod runtime_data; #[cfg(feature = "jit" )] pub mod jit; -pub mod env; pub use self::env::Env; +pub use self::runtime_data::RuntimeData; + +#[cfg(feature = "jit" )] +pub use self::jit::EnvAdapter; diff --git a/src/evm/runtime_data.rs b/src/evm/runtime_data.rs new file mode 100644 index 000000000..a7211dcd3 --- /dev/null +++ b/src/evm/runtime_data.rs @@ -0,0 +1,38 @@ +use util::hash::*; +use util::uint::*; + +pub struct RuntimeData { + pub gas: u64, + pub gas_price: u64, + pub call_data: Vec, + pub address: Address, + pub caller: Address, + pub origin: Address, + pub coinbase: Address, + pub difficulty: U256, + pub gas_limit: U256, + pub number: u64, + pub timestamp: u64, + pub code: Vec, + pub code_hash: H256 +} + +impl RuntimeData { + pub fn new() -> RuntimeData { + RuntimeData { + gas: 0, + gas_price: 0, + call_data: vec![], + address: Address::new(), + caller: Address::new(), + origin: Address::new(), + coinbase: Address::new(), + difficulty: U256::from(0), + gas_limit: U256::from(0), + number: 0, + timestamp: 0, + code: vec![], + code_hash: H256::new() + } + } +}