This commit is contained in:
debris 2015-12-23 13:49:45 +01:00
parent 353d4fbea0
commit c695e09bba
4 changed files with 99 additions and 35 deletions

View File

@ -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;

View File

@ -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<T>: 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<T> {
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<evmjit::I256> 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<evmjit::I256> for H256 {
fn into_jit(self) -> evmjit::I256 {
let mut ret = [0; 4];
for i in 0..self.bytes().len() {
@ -128,6 +128,13 @@ 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() {
@ -145,9 +152,16 @@ fn test_to_and_from_h256() {
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);
}
#[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);
}
}

View File

@ -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;

38
src/evm/runtime_data.rs Normal file
View File

@ -0,0 +1,38 @@
use util::hash::*;
use util::uint::*;
pub struct RuntimeData {
pub gas: u64,
pub gas_price: u64,
pub call_data: Vec<u8>,
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<u8>,
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()
}
}
}