openethereum/ethcore/src/evm/factory.rs

207 lines
5.0 KiB
Rust
Raw Normal View History

// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2016-02-05 13:40:41 +01:00
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
2015-12-30 12:46:10 +01:00
//! Evm factory.
2016-02-02 15:55:44 +01:00
//!
//! TODO: consider spliting it into two separate files.
2016-01-14 18:29:18 +01:00
use std::fmt;
use std::sync::Arc;
2015-12-29 12:37:38 +01:00
use evm::Evm;
use util::U256;
use super::interpreter::SharedCache;
2015-12-29 12:37:38 +01:00
cli overhaul (#1600) * cli commands * cleanup parity/signer * cleanup parity/signer * remove redundant import of signer crate from main.rs * cli cleanup in progress * cli cleanup in progress * moved few commonly used functions to separate methods with tests * cleaning up blockchain import in progress * cleaning up blockchain import in progress2 * cleaning up blockchain import in progress3 * tests for database compaction profile parsing * cleaning up blockchain import in progress4 * cleaning up blockchain import in progress5 * blockchain import * export blockchain in progress * cleanup execute_export * Configuration::to_duration cleaned up * removed unused code, tests for to_duration * cleanup Configuration::mode function * parsing some of the cli params in params.rs * rpc and signer are no longer optional * move importing extern crates to main.rs file * swipe dies from rpc module * swipe dies from dapps * finding deprecated * several tests and fixes for parity * parity cleanup in progress * cleanup price parsing * parity cleanup in progress * swiped all dies * parity cleanup in progress * replace usages of from_str with parse() in parity/params.rs * removed few more from_str * split parity/params.rs into params and helpers * removed wildcard import from configuration.rs * cleanup directories/path creation * cleaning up run cmd * moved LoggerConfig * defaults for cli params * fixed indention in raise_fd_limit * tests for rpc_apis * tests for default ipc and rpc settings * ipc socket * cleanup in progress * account service * cleanup miner config * BlockChain commands use Directiores structure now * client_config * network settings and dapps configuration * removing warnings * default logger config * fixed client_path * overhaul * fixing export && import * default export DataFormat * import and export also upgrade db * fixed export && import * polishing pr * polishing pr * fixed custom bootnodes * fixed daemonize on windows * fixed setting up enable network * finished pr * fixed compiling on windows * Fixed warning; windows build * Better cache management * Fixed tests on windows * Fixed test * Restored pruning method names * --cache alias * Fixed more tests * Ensure default options actually listed as valid [ci:skip]
2016-07-25 16:09:47 +02:00
#[derive(Debug, PartialEq, Clone)]
2016-02-02 18:02:58 +01:00
/// Type of EVM to use.
2016-01-14 13:36:45 +01:00
pub enum VMType {
2016-02-02 18:02:58 +01:00
/// JIT EVM
2016-03-20 11:33:36 +01:00
#[cfg(feature = "jit")]
2016-01-14 13:36:45 +01:00
Jit,
2016-02-02 18:02:58 +01:00
/// RUST EVM
2016-01-14 13:36:45 +01:00
Interpreter
}
2016-01-16 17:08:57 +01:00
2016-01-14 18:29:18 +01:00
impl fmt::Display for VMType {
2016-02-03 15:57:17 +01:00
#[cfg(feature="jit")]
2016-01-14 18:29:18 +01:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
VMType::Jit => "JIT",
VMType::Interpreter => "INT"
})
}
2016-02-03 15:57:17 +01:00
#[cfg(not(feature="jit"))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match *self {
VMType::Interpreter => "INT"
})
}
2016-01-14 18:29:18 +01:00
}
impl Default for VMType {
fn default() -> Self {
VMType::Interpreter
}
}
2016-01-14 18:29:18 +01:00
impl VMType {
2016-01-16 17:08:57 +01:00
/// Return all possible VMs (JIT, Interpreter)
2016-03-20 11:33:36 +01:00
#[cfg(feature = "jit")]
2016-01-14 18:29:18 +01:00
pub fn all() -> Vec<VMType> {
vec![VMType::Jit, VMType::Interpreter]
}
2016-01-16 17:08:57 +01:00
/// Return all possible VMs (Interpreter)
2016-03-20 11:33:36 +01:00
#[cfg(not(feature = "jit"))]
2016-01-14 18:29:18 +01:00
pub fn all() -> Vec<VMType> {
vec![VMType::Interpreter]
}
/// Return new jit if it's possible
#[cfg(not(feature = "jit"))]
pub fn jit() -> Option<Self> {
None
}
/// Return new jit if it's possible
#[cfg(feature = "jit")]
pub fn jit() -> Option<Self> {
Some(VMType::Jit)
}
2016-01-14 18:29:18 +01:00
}
2016-01-14 13:36:45 +01:00
2016-01-11 02:42:02 +01:00
/// Evm factory. Creates appropriate Evm.
#[derive(Clone)]
2016-01-14 13:36:45 +01:00
pub struct Factory {
evm: VMType,
evm_cache: Arc<SharedCache>,
2016-01-14 13:36:45 +01:00
}
2015-12-29 12:37:38 +01:00
2016-01-11 19:01:42 +01:00
impl Factory {
2016-01-16 17:08:57 +01:00
/// Create fresh instance of VM
/// Might choose implementation depending on supplied gas.
2016-03-20 11:33:36 +01:00
#[cfg(feature = "jit")]
pub fn create(&self, gas: U256) -> Box<Evm> {
2016-01-14 13:36:45 +01:00
match self.evm {
VMType::Jit => {
Box::new(super::jit::JitEvm::default())
2016-01-14 13:36:45 +01:00
},
VMType::Interpreter => if Self::can_fit_in_usize(gas) {
Box::new(super::interpreter::Interpreter::<usize>::new(self.evm_cache.clone()))
} else {
Box::new(super::interpreter::Interpreter::<U256>::new(self.evm_cache.clone()))
2016-01-14 13:36:45 +01:00
}
}
2016-01-14 13:36:45 +01:00
}
2016-02-03 15:57:17 +01:00
/// Create fresh instance of VM
/// Might choose implementation depending on supplied gas.
2016-03-20 11:33:36 +01:00
#[cfg(not(feature = "jit"))]
pub fn create(&self, gas: U256) -> Box<Evm> {
2016-02-03 15:57:17 +01:00
match self.evm {
VMType::Interpreter => if Self::can_fit_in_usize(gas) {
Box::new(super::interpreter::Interpreter::<usize>::new(self.evm_cache.clone()))
} else {
Box::new(super::interpreter::Interpreter::<U256>::new(self.evm_cache.clone()))
2016-02-03 15:57:17 +01:00
}
}
2016-02-03 15:57:17 +01:00
}
2016-10-07 12:18:42 +02:00
/// Create new instance of specific `VMType` factory, with a size in bytes
/// for caching jump destinations.
pub fn new(evm: VMType, cache_size: usize) -> Self {
2016-01-14 13:36:45 +01:00
Factory {
evm: evm,
2016-10-07 12:18:42 +02:00
evm_cache: Arc::new(SharedCache::new(cache_size)),
2016-01-14 13:36:45 +01:00
}
}
fn can_fit_in_usize(gas: U256) -> bool {
gas == U256::from(gas.low_u64() as usize)
}
2016-01-19 11:10:38 +01:00
}
2016-01-19 11:10:38 +01:00
impl Default for Factory {
2016-01-14 13:36:45 +01:00
/// Returns jitvm factory
#[cfg(all(feature = "jit", not(test)))]
2016-01-19 11:10:38 +01:00
fn default() -> Factory {
2016-01-14 13:36:45 +01:00
Factory {
evm: VMType::Jit,
evm_cache: Arc::new(SharedCache::default()),
2016-01-14 13:36:45 +01:00
}
}
/// Returns native rust evm factory
#[cfg(any(not(feature = "jit"), test))]
2016-01-19 11:10:38 +01:00
fn default() -> Factory {
2016-01-14 13:36:45 +01:00
Factory {
evm: VMType::Interpreter,
evm_cache: Arc::new(SharedCache::default()),
2016-01-14 13:36:45 +01:00
}
2015-12-29 12:37:38 +01:00
}
}
#[test]
fn test_create_vm() {
let _vm = Factory::default().create(U256::zero());
2015-12-29 12:37:38 +01:00
}
2016-01-14 16:17:44 +01:00
2016-01-16 17:08:57 +01:00
/// Create tests by injecting different VM factories
2016-01-14 16:17:44 +01:00
#[macro_export]
macro_rules! evm_test(
2016-02-03 16:21:52 +01:00
(ignorejit => $name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]
#[ignore]
#[cfg(feature = "jit")]
fn $name_jit() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Jit, 1024 * 32));
2016-02-03 16:21:52 +01:00
}
#[test]
fn $name_int() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Interpreter, 1024 * 32));
2016-02-03 16:21:52 +01:00
}
};
2016-01-14 16:17:44 +01:00
($name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]
#[cfg(feature = "jit")]
fn $name_jit() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Jit, 1024 * 32));
2016-01-14 16:17:44 +01:00
}
#[test]
fn $name_int() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Interpreter, 1024 * 32));
2016-01-14 16:17:44 +01:00
}
}
);
2016-01-16 17:08:57 +01:00
/// Create ignored tests by injecting different VM factories
#[macro_export]
macro_rules! evm_test_ignore(
($name_test: ident: $name_jit: ident, $name_int: ident) => {
#[test]
#[ignore]
#[cfg(feature = "jit")]
#[cfg(feature = "ignored-tests")]
fn $name_jit() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Jit, 1024 * 32));
}
#[test]
#[ignore]
#[cfg(feature = "ignored-tests")]
fn $name_int() {
2016-10-07 12:18:42 +02:00
$name_test(Factory::new(VMType::Interpreter, 1024 * 32));
}
}
);