openethereum/ethcore/evm/src/factory.rs

105 lines
2.9 KiB
Rust
Raw Normal View History

// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
2016-02-05 13:40:41 +01:00
// Parity Ethereum is free software: you can redistribute it and/or modify
2016-02-05 13:40:41 +01:00
// 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 Ethereum is distributed in the hope that it will be useful,
2016-02-05 13:40:41 +01:00
// 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 Ethereum. If not, see <http://www.gnu.org/licenses/>.
2016-02-05 13:40:41 +01:00
2015-12-30 12:46:10 +01:00
//! Evm factory.
2016-02-02 15:55:44 +01:00
//!
use std::sync::Arc;
2018-10-02 16:33:19 +02:00
use vm::{Exec, Schedule};
use ethereum_types::U256;
use super::vm::ActionParams;
use super::interpreter::SharedCache;
use super::vmtype::VMType;
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.
pub fn create(&self, params: ActionParams, schedule: &Schedule, depth: usize) -> Box<dyn Exec> {
2016-02-03 15:57:17 +01:00
match self.evm {
VMType::Interpreter => if Self::can_fit_in_usize(&params.gas) {
Box::new(super::interpreter::Interpreter::<usize>::new(params, self.evm_cache.clone(), schedule, depth))
} else {
Box::new(super::interpreter::Interpreter::<U256>::new(params, self.evm_cache.clone(), schedule, depth))
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 {
EIP-1344 Add CHAINID op-code (#10983) * Add client-traits crate Move the BlockInfo trait to new crate * New crate `machine` Contains code extracted from ethcore that defines `Machine`, `Externalities` and other execution related code. * Use new machine and client-traits crates in ethcore * Use new crates machine and client-traits instead of ethcore where appropriate * Fix tests * Don't re-export so many types from ethcore::client * Fixing more fallout from removing re-export * fix test * More fallout from not re-exporting types * Add some docs * cleanup * import the macro edition style * Tweak docs * Add missing import * remove unused ethabi_derive imports * Use latest ethabi-contract * Move many traits from ethcore/client/traits to client-traits crate Initial version of extracted Engine trait * Move snapshot related traits to the engine crate (eew) * Move a few snapshot related types to common_types Cleanup Executed as exported from machine crate * fix warning * Gradually introduce new engine crate: snapshot * ethcore typechecks with new engine crate * Sort out types outside ethcore * Add an EpochVerifier to ethash and use that in Engine.epoch_verifier() Cleanup * Document pub members * Sort out tests Sort out default impls for EpochVerifier * Add test-helpers feature and move EngineSigner impl to the right place * Sort out tests * Sort out tests and refactor verification types * Fix missing traits * More missing traits Fix Histogram * Fix tests and cleanup * cleanup * Put back needed logger import * Don't rexport common_types from ethcore/src/client Don't export ethcore::client::* * Remove files no longer used Use types from the engine crate Explicit exports from engine::engine * Get rid of itertools * Move a few more traits from ethcore to client-traits: BlockChainReset, ScheduleInfo, StateClient * Move ProvingBlockChainClient to client-traits * Don't re-export ForkChoice and Transition from ethcore * Address grumbles: sort imports, remove commented out code * Fix merge resolution error * Extract the Clique engine to own crate * Extract NullEngine and the block_reward module from ethcore * Extract InstantSeal engine to own crate * Extract remaining engines * Extract executive_state to own crate so it can be used by engine crates * Remove snapshot stuff from the engine crate * Put snapshot traits back in ethcore * cleanup * Remove stuff from ethcore * Don't use itertools * itertools in aura is legit-ish * More post-merge fixes * Re-export less types in client * cleanup * Extract spec to own crate * Put back the test-helpers from basic-authority * Fix ethcore benchmarks * Reduce the public api of ethcore/verification * WIP * Add Cargo.toml * Fix compilation outside ethcore * Audit uses of import_verified_blocks() and remove unneeded calls Cleanup * cleanup * Remove unused imports from ethcore * Cleanup * remove double semi-colons * Add missing generic param * More missing generics * Update ethcore/block-reward/Cargo.toml Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update ethcore/engines/basic-authority/Cargo.toml Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update ethcore/engines/ethash/Cargo.toml Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Update ethcore/engines/clique/src/lib.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * signers is already a ref * Add an EngineType enum to tighten up Engine.name() * Add CHAINID opcode * Introduce Snapshotting enum to distinguish the type of snapshots a chain uses * Rename supports_warp to snapshot_mode * Missing import * Add chain_id wherever we instantiate EnvInfo * more missing chain_id * Tell serde to ignore the chain_id field on Env * Update ethcore/src/snapshot/consensus/mod.rs Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * Use the chain_id from the machine by adding chain_id() to the Ext trait * cleanup * add missing impl cleanup * missing import * Fix import * Add transition marker for EIP 1344 * double semi * Fix merge problem * cleanup * merge conflict error * Fix a few warnings * Update ethcore/vm/src/schedule.rs Co-Authored-By: Andronik Ordian <write@reusable.software> * more merge fallout
2019-08-28 16:15:50 +02:00
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 native rust evm factory
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() {
use vm::Ext;
use vm::tests::FakeExt;
use bytes::Bytes;
let mut params = ActionParams::default();
params.code = Some(Arc::new(Bytes::default()));
let ext = FakeExt::new();
let _vm = Factory::default().create(params, ext.schedule(), ext.depth());
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(
($name_test: ident: $name_int: ident) => {
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_int: ident) => {
#[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));
}
}
);