openethereum/src/engine.rs

79 lines
4.2 KiB
Rust
Raw Normal View History

use common::*;
2016-01-08 21:33:41 +01:00
use block::Block;
use spec::Spec;
2016-01-11 16:28:30 +01:00
use evm::Schedule;
2016-01-14 17:24:57 +01:00
use evm::Factory;
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
/// Provides hooks into each of the major parts of block import.
2016-01-11 11:51:31 +01:00
pub trait Engine : Sync + Send {
/// The name of this engine.
fn name(&self) -> &str;
/// The version of this engine. Should be of the form
2015-12-20 16:12:53 +01:00
fn version(&self) -> SemanticVersion { SemanticVersion::new(0, 0, 0) }
/// The number of additional header fields required for this engine.
2016-01-10 14:05:39 +01:00
fn seal_fields(&self) -> usize { 0 }
/// Default values of the additional fields RLP-encoded in a raw (non-list) harness.
fn seal_rlp(&self) -> Bytes { vec![] }
/// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
2015-12-20 16:12:53 +01:00
/// Get the general parameters of the chain.
fn spec(&self) -> &Spec;
2015-12-20 16:12:53 +01:00
2016-01-14 17:24:57 +01:00
/// Get current EVM factory
fn vm_factory(&self) -> &Factory;
2015-12-23 12:56:38 +01:00
/// Get the EVM schedule for the given `env_info`.
2016-01-11 16:28:30 +01:00
fn schedule(&self, env_info: &EnvInfo) -> Schedule;
2015-12-20 16:12:53 +01:00
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
2016-01-09 19:10:05 +01:00
fn maximum_extra_data_size(&self) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) }
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-10 14:05:39 +01:00
fn maximum_uncle_count(&self) -> usize { 2 }
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) }
/// Block transformation functions, before and after the transactions.
2016-01-08 22:04:21 +01:00
fn on_new_block(&self, _block: &mut Block) {}
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-08 22:04:21 +01:00
fn on_close_block(&self, _block: &mut Block) {}
2016-01-11 13:51:58 +01:00
// TODO: consider including State in the params for verification functions.
/// Phase 1 quick block verification. Only does checks that are cheap. `block` (the header's full block)
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
fn verify_block_basic(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
2016-01-11 13:51:58 +01:00
/// Phase 2 verification. Perform costly checks such as transaction signatures. `block` (the header's full block)
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
fn verify_block_unordered(&self, _header: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
2016-01-11 13:51:58 +01:00
/// Phase 3 verification. Check block information against parent and uncles. `block` (the header's full block)
/// may be provided for additional checks. Returns either a null `Ok` or a general error detailing the problem with import.
2016-01-14 19:03:48 +01:00
fn verify_block_family(&self, _header: &Header, _parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> { Ok(()) }
2015-12-20 16:12:53 +01:00
/// Additional verification for transactions in blocks.
2015-12-20 16:12:53 +01:00
// TODO: Add flags for which bits of the transaction to check.
2015-12-23 12:56:38 +01:00
// TODO: consider including State in the params.
2016-01-16 18:30:27 +01:00
fn verify_transaction_basic(&self, _t: &Transaction, _header: &Header) -> Result<(), Error> { Ok(()) }
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-10 14:05:39 +01:00
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), Error> { Ok(()) }
2015-12-20 16:12:53 +01:00
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
2015-12-23 12:56:38 +01:00
// TODO: consider including State in the params.
2016-01-08 22:04:21 +01:00
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) {}
2016-01-07 23:55:14 +01:00
// TODO: builtin contract routing - to do this properly, it will require removing the built-in configuration-reading logic
// from Spec into here and removing the Spec::builtins field.
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-08 19:12:19 +01:00
fn is_builtin(&self, a: &Address) -> bool { self.spec().builtins.contains_key(a) }
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-08 19:12:19 +01:00
fn cost_of_builtin(&self, a: &Address, input: &[u8]) -> U256 { self.spec().builtins.get(a).unwrap().cost(input.len()) }
2016-01-19 17:02:01 +01:00
/// TODO [Gav Wood] Please document me
2016-01-08 19:12:19 +01:00
fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut [u8]) { self.spec().builtins.get(a).unwrap().execute(input, output); }
2015-12-23 12:56:38 +01:00
// TODO: sealing stuff - though might want to leave this for later.
}