2015-12-30 12:46:10 +01:00
|
|
|
//! Contract execution environment.
|
|
|
|
|
2015-12-23 13:02:01 +01:00
|
|
|
use util::hash::*;
|
2015-12-29 12:04:03 +01:00
|
|
|
use util::uint::*;
|
2015-12-30 12:03:40 +01:00
|
|
|
use state::*;
|
2015-12-23 13:02:01 +01:00
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
/// This structure represents contract execution environment.
|
|
|
|
/// It should be initalized with `State` and contract address.
|
|
|
|
///
|
|
|
|
/// ```markdown
|
|
|
|
/// extern crate ethcore_util as util;
|
|
|
|
/// extern crate ethcore;
|
|
|
|
/// use util::hash::*;
|
|
|
|
/// use ethcore::state::*;
|
|
|
|
/// use ethcore::evm::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let address = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap();
|
|
|
|
/// let mut data = RuntimeData::new();
|
|
|
|
/// let mut env = Env::new(State::new_temp(), address);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-12-30 12:03:40 +01:00
|
|
|
pub struct Env {
|
|
|
|
state: State,
|
|
|
|
address: Address
|
|
|
|
}
|
2015-12-23 13:02:01 +01:00
|
|
|
|
|
|
|
impl Env {
|
2015-12-30 12:46:10 +01:00
|
|
|
/// Creates new evm environment object with backing state.
|
2015-12-30 12:03:40 +01:00
|
|
|
pub fn new(state: State, address: Address) -> Env {
|
|
|
|
Env {
|
|
|
|
state: state,
|
|
|
|
address: address
|
|
|
|
}
|
2015-12-23 13:02:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
/// Returns a value for given key.
|
|
|
|
pub fn sload(&self, key: &H256) -> H256 {
|
|
|
|
self.state.storage_at(&self.address, key)
|
2015-12-23 13:02:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
/// Stores a value for given key.
|
|
|
|
pub fn sstore(&mut self, key: H256, value: H256) {
|
|
|
|
self.state.set_storage(&self.address, key, value)
|
2015-12-23 13:02:01 +01:00
|
|
|
}
|
2015-12-29 12:04:03 +01:00
|
|
|
|
2015-12-30 12:46:10 +01:00
|
|
|
/// Returns address balance.
|
|
|
|
pub fn balance(&self, address: &Address) -> U256 {
|
|
|
|
self.state.balance(address)
|
2015-12-29 12:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blockhash(&self, _number: &U256) -> H256 {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates new contract
|
|
|
|
/// Returns new contract address 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<u8>, u64)>{
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns code at given address
|
2016-01-05 18:43:46 +01:00
|
|
|
pub fn extcode(&self, address: &Address) -> Vec<u8> {
|
|
|
|
self.state.code(address).unwrap_or(vec![])
|
2015-12-29 12:04:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log(&self, _topics: &[H256], _data: &[u8]) {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
2015-12-30 12:03:40 +01:00
|
|
|
|
|
|
|
/// Drain state
|
|
|
|
// not sure if this is the best solution, but seems to be the easiest one, mk
|
|
|
|
pub fn state(self) -> State {
|
|
|
|
self.state
|
|
|
|
}
|
2015-12-23 13:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|