From 8c6d6950ca98631972ccafbec63d1390ca2eb146 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 6 Jan 2016 20:00:32 +0100 Subject: [PATCH] evm call and create in progress --- rust-evmjit/src/lib.rs | 30 ++++++++++----------- src/evm/env.rs | 10 +++---- src/evm/jit.rs | 61 ++++++++++++++++++++++++++++++------------ 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/rust-evmjit/src/lib.rs b/rust-evmjit/src/lib.rs index bbc377703..2c1e9854c 100644 --- a/rust-evmjit/src/lib.rs +++ b/rust-evmjit/src/lib.rs @@ -109,19 +109,19 @@ pub trait Env { io_gas: *mut u64, endowment: *const JitI256, init_beg: *const u8, - init_size: *const u64, - address: *mut JitI256); + init_size: u64, + address: *mut JitH256); fn call(&mut self, io_gas: *mut u64, - call_gas: *const u64, - receive_address: *const JitI256, + call_gas: u64, + receive_address: *const JitH256, value: *const JitI256, in_beg: *const u8, - in_size: *const u64, + in_size: u64, out_beg: *mut u8, - out_size: *mut u64, - code_address: JitI256) -> bool; + out_size: u64, + code_address: *const JitH256) -> bool; fn log(&mut self, beg: *const u8, @@ -321,8 +321,8 @@ pub mod ffi { io_gas: *mut u64, endowment: *const JitI256, init_beg: *const u8, - init_size: *const u64, - address: *mut JitI256) { + init_size: u64, + address: *mut JitH256) { let env = &mut *env; env.create(io_gas, endowment, init_beg, init_size, address); } @@ -330,20 +330,20 @@ pub mod ffi { #[no_mangle] pub unsafe extern "C" fn env_call(env: *mut EnvHandle, io_gas: *mut u64, - call_gas: *const u64, - receive_address: *const JitI256, + call_gas: u64, + receive_address: *const JitH256, value: *const JitI256, in_beg: *const u8, - in_size: *const u64, + in_size: u64, out_beg: *mut u8, - out_size: *mut u64, - code_address: JitI256) -> bool { + out_size: u64, + code_address: *const JitH256) -> bool { let env = &mut *env; env.call(io_gas, call_gas, receive_address, value, in_beg, in_size, out_beg, out_size, code_address) } #[no_mangle] - pub unsafe extern "C" fn env_sha3(begin: *const u8, size: u64, out_hash: *mut JitI256) { + pub unsafe extern "C" fn env_sha3(begin: *const u8, size: u64, out_hash: *mut JitH256) { 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/env.rs b/src/evm/env.rs index 24a21bcd2..b13ec95cf 100644 --- a/src/evm/env.rs +++ b/src/evm/env.rs @@ -88,15 +88,15 @@ impl Env { } } - /// Creates new contract - /// Returns new contract address gas used + /// Creates new contract. + /// Returns new contract address and gas used. pub fn create(&self, _gas: u64, _endowment: &U256, _code: &[u8]) -> (Address, u64) { unimplemented!(); } - /// Calls existing contract - /// Returns call output and gas used - pub fn call(&self, _gas: u64, _call_gas: u64, _receive_address: &H256, _value: &U256, _data: &[u8], _code_address: &Address) -> Option<(Vec, u64)>{ + /// Calls existing contract. + /// Returns call output and gas used. + pub fn call(&self, _gas: u64, _call_gas: u64, _receive_address: &Address, _value: &U256, _data: &[u8], _code_address: &Address) -> Option<(Vec, u64)>{ unimplemented!(); } diff --git a/src/evm/jit.rs b/src/evm/jit.rs index 3055a84fe..93e4cf2ed 100644 --- a/src/evm/jit.rs +++ b/src/evm/jit.rs @@ -88,6 +88,12 @@ impl IntoJit for Address { } } +impl IntoJit for Address { + fn into_jit(self) -> evmjit::H256 { + H256::from(self).into_jit() + } +} + impl IntoJit for evm::RuntimeData { fn into_jit(self) -> evmjit::RuntimeDataHandle { let mut data = evmjit::RuntimeDataHandle::new(); @@ -157,25 +163,46 @@ impl<'a> evmjit::Env for EnvAdapter<'a> { } fn create(&mut self, - _io_gas: *mut u64, - _endowment: *const evmjit::I256, - _init_beg: *const u8, - _init_size: *const u64, - _address: *mut evmjit::I256) { - unimplemented!(); + io_gas: *mut u64, + endowment: *const evmjit::I256, + init_beg: *const u8, + init_size: u64, + address: *mut evmjit::H256) { + unsafe { + let (addr, gas) = self.env.create(*io_gas, &U256::from_jit(&*endowment), slice::from_raw_parts(init_beg, init_size as usize)); + *io_gas = gas; + *address = addr.into_jit(); + } } fn call(&mut self, - _io_gas: *mut u64, - _call_gas: *const u64, - _receive_address: *const evmjit::I256, - _value: *const evmjit::I256, - _in_beg: *const u8, - _in_size: *const u64, - _out_beg: *mut u8, - _out_size: *mut u64, - _code_address: evmjit::I256) -> bool { - unimplemented!(); + io_gas: *mut u64, + call_gas: u64, + receive_address: *const evmjit::H256, + value: *const evmjit::I256, + in_beg: *const u8, + in_size: u64, + mut out_beg: *mut u8, + out_size: u64, + code_address: *const evmjit::H256) -> bool { + unsafe { + let opt = self.env.call(*io_gas, + call_gas, + &Address::from_jit(&*receive_address), + &U256::from_jit(&*value), + slice::from_raw_parts(in_beg, in_size as usize), + &Address::from_jit(&*code_address)); + + if opt.is_none() { + return false; + } + + let (mut output, gas) = opt.unwrap(); + out_beg = output.as_mut_ptr(); + mem::forget(output); + *io_gas = gas; + true + } } fn log(&mut self, @@ -281,7 +308,7 @@ mod tests { use std::str::FromStr; let a = Address::from_str("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap(); - let j = a.clone().into_jit(); + let j: ::evmjit::I256 = a.clone().into_jit(); let a2 = Address::from_jit(&j); assert_eq!(a, a2); }