openethereum/src/evm/executive.rs

337 lines
9.3 KiB
Rust
Raw Normal View History

2016-01-09 17:55:47 +01:00
use std::mem;
2016-01-07 21:29:36 +01:00
use util::hash::*;
use util::uint::*;
2016-01-07 23:33:54 +01:00
use util::rlp::*;
use util::sha3::*;
2016-01-09 00:51:09 +01:00
use util::bytes::*;
2016-01-07 19:05:44 +01:00
use state::*;
use env_info::*;
use engine::*;
use transaction::*;
2016-01-09 18:25:18 +01:00
use evm::{VmFactory, Ext, LogEntry, EvmParams, EvmResult};
2016-01-07 23:33:54 +01:00
2016-01-09 00:51:09 +01:00
/// Returns new address created from address and given nonce.
2016-01-08 00:16:15 +01:00
pub fn contract_address(address: &Address, nonce: &U256) -> Address {
2016-01-07 23:33:54 +01:00
let mut stream = RlpStream::new_list(2);
stream.append(address);
stream.append(nonce);
From::from(stream.out().sha3())
}
2016-01-07 19:05:44 +01:00
2016-01-09 13:51:59 +01:00
#[derive(PartialEq, Debug)]
2016-01-07 19:05:44 +01:00
pub enum ExecutiveResult {
2016-01-09 13:51:59 +01:00
Ok,
OutOfGas,
InternalError
2016-01-07 19:05:44 +01:00
}
pub struct Executive<'a> {
state: &'a mut State,
info: &'a EnvInfo,
engine: &'a Engine,
2016-01-09 00:51:09 +01:00
depth: usize,
2016-01-07 19:05:44 +01:00
}
impl<'a> Executive<'a> {
2016-01-09 17:55:47 +01:00
pub fn new(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine) -> Self {
Executive::new_with_depth(state, info, engine, 0)
}
2016-01-09 00:51:09 +01:00
2016-01-09 17:55:47 +01:00
fn from_parent(e: &'a mut Externalities) -> Self {
Executive::new_with_depth(e.state, e.info, e.engine, e.depth + 1)
2016-01-09 00:51:09 +01:00
}
2016-01-09 17:55:47 +01:00
fn new_with_depth(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine, depth: usize) -> Self {
2016-01-07 19:05:44 +01:00
Executive {
state: state,
info: info,
engine: engine,
2016-01-09 17:55:47 +01:00
depth: depth,
2016-01-07 19:05:44 +01:00
}
}
2016-01-09 17:55:47 +01:00
pub fn transact(e: &mut Executive<'a>, t: &Transaction) -> ExecutiveResult {
2016-01-09 00:51:09 +01:00
// TODO: validate that we have enough funds
2016-01-09 17:55:47 +01:00
// TODO: validate nonce ?
let sender = t.sender();
2016-01-07 21:29:36 +01:00
2016-01-09 17:55:47 +01:00
match t.kind() {
TransactionKind::ContractCreation => {
let params = EvmParams {
address: contract_address(&sender, &t.nonce),
sender: sender.clone(),
origin: sender.clone(),
gas: t.gas,
gas_price: t.gas_price,
value: t.value,
code: t.data.clone(),
data: vec![],
};
e.state.inc_nonce(&params.address);
unimplemented!()
//Executive::call(e, &params)
2016-01-09 00:51:09 +01:00
},
2016-01-09 17:55:47 +01:00
TransactionKind::MessageCall => {
let params = EvmParams {
address: t.to.clone().unwrap(),
sender: sender.clone(),
origin: sender.clone(),
gas: t.gas,
gas_price: t.gas_price,
value: t.value,
code: e.state.code(&t.to.clone().unwrap()).unwrap_or(vec![]),
data: t.data.clone(),
};
e.state.inc_nonce(&params.address);
Executive::create(e, &params)
}
2016-01-07 19:05:44 +01:00
}
}
2016-01-09 17:55:47 +01:00
fn call(e: &mut Executive<'a>, p: &EvmParams) -> ExecutiveResult {
//let _ext = Externalities::from_executive(e, &p);
2016-01-07 19:05:44 +01:00
ExecutiveResult::Ok
}
2016-01-09 02:12:17 +01:00
2016-01-09 17:55:47 +01:00
fn create(e: &mut Executive<'a>, params: &EvmParams) -> ExecutiveResult {
2016-01-09 13:51:59 +01:00
//self.state.require_or_from(&self.params.address, false, ||Account::new_contract(U256::from(0)));
2016-01-09 18:25:18 +01:00
//TODO: ensure that account at given address is created
2016-01-09 17:55:47 +01:00
e.state.transfer_balance(&params.sender, &params.address, &params.value);
2016-01-09 13:51:59 +01:00
let code = {
2016-01-09 18:25:18 +01:00
let mut ext = Externalities::new(e.state, e.info, e.engine, e.depth, params);
2016-01-08 12:50:06 +01:00
let evm = VmFactory::create();
2016-01-09 17:55:47 +01:00
evm.exec(&params, &mut ext)
2016-01-09 13:51:59 +01:00
};
2016-01-08 12:50:06 +01:00
2016-01-09 13:51:59 +01:00
match code {
2016-01-09 18:25:18 +01:00
EvmResult::Stop => {
2016-01-09 13:51:59 +01:00
ExecutiveResult::Ok
},
2016-01-09 18:25:18 +01:00
EvmResult::Return(output) => {
//e.state.set_code(&params.address, output);
2016-01-09 13:51:59 +01:00
ExecutiveResult::Ok
},
2016-01-09 18:25:18 +01:00
EvmResult::OutOfGas => {
2016-01-09 13:51:59 +01:00
ExecutiveResult::OutOfGas
},
err => {
ExecutiveResult::InternalError
}
}
2016-01-07 19:05:44 +01:00
}
2016-01-09 17:55:47 +01:00
}
pub struct Externalities<'a> {
state: &'a mut State,
info: &'a EnvInfo,
engine: &'a Engine,
depth: usize,
params: &'a EvmParams,
logs: Vec<LogEntry>,
refunds: U256
}
impl<'a> Externalities<'a> {
pub fn new(state: &'a mut State, info: &'a EnvInfo, engine: &'a Engine, depth: usize, params: &'a EvmParams) -> Self {
Externalities {
state: state,
info: info,
engine: engine,
depth: depth,
params: params,
logs: vec![],
refunds: U256::zero()
}
}
2016-01-09 00:51:09 +01:00
2016-01-09 17:55:47 +01:00
// TODO: figure out how to use this function
// so the lifetime checker is satisfied
//pub fn from_executive(e: &mut Executive<'a>, params: &EvmParams) -> Self {
//Externalities::new(e.state, e.info, e.engine, e.depth, params)
//}
2016-01-09 00:51:09 +01:00
pub fn logs(&self) -> &[LogEntry] {
&self.logs
}
}
2016-01-09 17:55:47 +01:00
impl<'a> Ext for Externalities<'a> {
2016-01-09 00:51:09 +01:00
fn sload(&self, key: &H256) -> H256 {
self.state.storage_at(&self.params.address, key)
}
fn sstore(&mut self, key: H256, value: H256) {
if value == H256::new() && self.state.storage_at(&self.params.address, &key) != H256::new() {
self.refunds = self.refunds + U256::from(self.engine.evm_schedule(self.info).sstore_refund_gas);
}
self.state.set_storage(&self.params.address, key, value)
}
fn balance(&self, address: &Address) -> U256 {
self.state.balance(address)
}
fn blockhash(&self, number: &U256) -> H256 {
match *number < self.info.number {
false => H256::from(&U256::zero()),
true => {
let index = self.info.number - *number - U256::one();
self.info.last_hashes[index.low_u32() as usize].clone()
}
}
}
fn create(&mut self, gas: u64, endowment: &U256, code: &[u8]) -> (Address, u64) {
2016-01-09 13:51:59 +01:00
match self.state.balance(&self.params.address) >= *endowment && self.depth < 1024 {
2016-01-09 00:51:09 +01:00
false => (Address::new(), gas),
true => {
2016-01-09 13:51:59 +01:00
let address = contract_address(&self.params.address, &self.state.nonce(&self.params.address));
2016-01-09 00:51:09 +01:00
let params = EvmParams {
2016-01-09 13:51:59 +01:00
address: address.clone(),
2016-01-09 00:51:09 +01:00
sender: self.params.address.clone(),
origin: self.params.origin.clone(),
gas: U256::from(gas),
gas_price: self.params.gas_price.clone(),
value: endowment.clone(),
code: code.to_vec(),
data: vec![],
};
2016-01-09 17:55:47 +01:00
let mut ex = Executive::from_parent(self);
ex.state.inc_nonce(&address);
let res = Executive::create(&mut ex, &params);
2016-01-09 13:51:59 +01:00
println!("res: {:?}", res);
(address, gas)
2016-01-09 00:51:09 +01:00
}
}
}
fn call(&mut self, gas: u64, call_gas: u64, receive_address: &Address, value: &U256, data: &[u8], code_address: &Address) -> Option<(Vec<u8>, u64)>{
// TODO: validation of the call
let params = EvmParams {
address: code_address.clone(),
sender: receive_address.clone(),
origin: self.params.origin.clone(),
gas: U256::from(call_gas), // TODO:
gas_price: self.params.gas_price.clone(),
value: value.clone(),
code: self.state.code(code_address).unwrap_or(vec![]),
data: data.to_vec(),
};
{
2016-01-09 17:55:47 +01:00
let mut ex = Executive::from_parent(self);
Executive::call(&mut ex, &params);
2016-01-09 00:51:09 +01:00
unimplemented!();
}
}
fn extcode(&self, address: &Address) -> Vec<u8> {
self.state.code(address).unwrap_or(vec![])
}
fn log(&mut self, topics: Vec<H256>, data: Bytes) {
let address = self.params.address.clone();
self.logs.push(LogEntry::new(address, topics, data));
}
2016-01-07 19:05:44 +01:00
}
2016-01-07 23:33:54 +01:00
#[cfg(test)]
mod tests {
2016-01-09 01:33:50 +01:00
use rustc_serialize::hex::FromHex;
2016-01-07 23:33:54 +01:00
use std::str::FromStr;
use util::hash::*;
use util::uint::*;
2016-01-09 01:33:50 +01:00
use evm::*;
use transaction::*;
use env_info::*;
use state::*;
use spec::*;
use engine::*;
use evm_schedule::*;
2016-01-09 02:12:17 +01:00
use super::contract_address;
2016-01-09 01:33:50 +01:00
struct TestEngine {
spec: Spec
}
impl TestEngine {
fn new() -> Self {
TestEngine {
spec: Spec::new_like_frontier()
2016-01-09 01:33:50 +01:00
}
}
}
impl Engine for TestEngine {
fn name(&self) -> &str { "TestEngine" }
fn spec(&self) -> &Spec { &self.spec }
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
}
2016-01-07 23:33:54 +01:00
#[test]
fn test_contract_address() {
let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
2016-01-09 02:12:17 +01:00
let expected_address = Address::from_str("3f09c73a5ed19289fb9bdc72f1742566df146f56").unwrap();
assert_eq!(expected_address, contract_address(&address, &U256::from(88)));
2016-01-07 23:33:54 +01:00
}
2016-01-09 01:33:50 +01:00
#[test]
2016-01-09 13:51:59 +01:00
// TODO: replace params with transactions!
2016-01-09 01:33:50 +01:00
fn test_executive() {
2016-01-09 02:12:17 +01:00
let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
let address = contract_address(&sender, &U256::zero());
2016-01-09 17:55:47 +01:00
let mut params = EvmParams::new();
2016-01-09 02:12:17 +01:00
params.address = address.clone();
params.sender = sender.clone();
params.gas = U256::from(0x174876e800u64);
params.code = "3331600055".from_hex().unwrap();
params.value = U256::from(0x7);
let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(0x100u64));
let info = EnvInfo::new();
let engine = TestEngine::new();
{
2016-01-09 17:55:47 +01:00
let mut ex = Executive::new(&mut state, &info, &engine);
assert_eq!(Executive::create(&mut ex, &params), ExecutiveResult::Ok);
2016-01-09 02:12:17 +01:00
}
2016-01-09 13:51:59 +01:00
assert_eq!(state.storage_at(&address, &H256::new()), H256::from(&U256::from(0xf9u64)));
2016-01-09 02:12:17 +01:00
assert_eq!(state.balance(&sender), U256::from(0xf9));
assert_eq!(state.balance(&address), U256::from(0x7));
2016-01-09 01:33:50 +01:00
}
2016-01-09 13:51:59 +01:00
#[test]
fn test_create_contract() {
let sender = Address::from_str("cd1722f3947def4cf144679da39c4c32bdc35681").unwrap();
let address = contract_address(&sender, &U256::zero());
let next_address = contract_address(&address, &U256::zero());
println!("address: {:?}", address);
println!("next address: {:?}", next_address);
2016-01-09 17:55:47 +01:00
let mut params = EvmParams::new();
2016-01-09 13:51:59 +01:00
params.address = address.clone();
params.sender = sender.clone();
params.origin = sender.clone();
params.gas = U256::from(0x174876e800u64);
params.code = "7c601080600c6000396000f3006000355415600957005b60203560003555600052601d60036000f0600055".from_hex().unwrap();
let mut state = State::new_temp();
state.add_balance(&sender, &U256::from(0x100u64));
let info = EnvInfo::new();
let engine = TestEngine::new();
{
2016-01-09 17:55:47 +01:00
let mut ex = Executive::new(&mut state, &info, &engine);
assert_eq!(Executive::create(&mut ex, &params), ExecutiveResult::Ok);
2016-01-09 13:51:59 +01:00
}
assert_eq!(state.storage_at(&address, &H256::new()), H256::from(next_address.clone()));
//assert_eq!(state.code(&next_address).unwrap(), "601080600c6000396000f3006000355415600957005b602035600035".from_hex().unwrap());
//assert!(false);
}
2016-01-07 23:33:54 +01:00
}