From 23d977eccefe471adbcd9dfdd50cd6415420c154 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 11 Mar 2019 11:37:48 +0100 Subject: [PATCH] simplify ethcore machine by removing redundant traits (#10454) --- ethcore/src/engines/instant_seal.rs | 11 ++++------ ethcore/src/engines/mod.rs | 12 +++++------ ethcore/src/engines/null_engine.rs | 10 ++++----- ethcore/src/machine.rs | 3 +-- ethcore/types/src/header.rs | 27 +++++------------------ machine/src/lib.rs | 33 ----------------------------- 6 files changed, 20 insertions(+), 76 deletions(-) diff --git a/ethcore/src/engines/instant_seal.rs b/ethcore/src/engines/instant_seal.rs index 595805c19..189015f08 100644 --- a/ethcore/src/engines/instant_seal.rs +++ b/ethcore/src/engines/instant_seal.rs @@ -15,7 +15,8 @@ // along with Parity Ethereum. If not, see . use engines::{Engine, Seal}; -use parity_machine::{Machine, Transactions, TotalScoredHeader}; +use parity_machine::{Machine, Transactions}; +use types::header::ExtendedHeader; /// `InstantSeal` params. #[derive(Default, Debug, PartialEq)] @@ -48,11 +49,7 @@ impl InstantSeal { } } -impl Engine for InstantSeal - where M::LiveBlock: Transactions, - M::ExtendedHeader: TotalScoredHeader, - ::Value: Ord -{ +impl Engine for InstantSeal where M::LiveBlock: Transactions { fn name(&self) -> &str { "InstantSeal" } @@ -84,7 +81,7 @@ impl Engine for InstantSeal header_timestamp >= parent_timestamp } - fn fork_choice(&self, new: &M::ExtendedHeader, current: &M::ExtendedHeader) -> super::ForkChoice { + fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice { super::total_difficulty_fork_choice(new, current) } } diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index 9cced0a0d..a63c69039 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -44,13 +44,13 @@ use builtin::Builtin; use vm::{EnvInfo, Schedule, CreateContractAddress, CallType, ActionValue}; use error::Error; use types::BlockNumber; -use types::header::Header; +use types::header::{Header, ExtendedHeader}; use snapshot::SnapshotComponents; use spec::CommonParams; use types::transaction::{self, UnverifiedTransaction, SignedTransaction}; use ethkey::{Signature}; -use parity_machine::{Machine, LocalizedMachine as Localized, TotalScoredHeader}; +use parity_machine::{Machine, LocalizedMachine as Localized}; use ethereum_types::{H256, U256, Address}; use unexpected::{Mismatch, OutOfBounds}; use bytes::Bytes; @@ -255,7 +255,7 @@ pub trait Engine: Sync + Send { &self, _block: &mut M::LiveBlock, _epoch_begin: bool, - _ancestry: &mut Iterator, + _ancestry: &mut Iterator, ) -> Result<(), M::Error> { Ok(()) } @@ -421,16 +421,16 @@ pub trait Engine: Sync + Send { /// Gather all ancestry actions. Called at the last stage when a block is committed. The Engine must guarantee that /// the ancestry exists. - fn ancestry_actions(&self, _header: &M::Header, _ancestry: &mut Iterator) -> Vec { + fn ancestry_actions(&self, _header: &M::Header, _ancestry: &mut Iterator) -> Vec { Vec::new() } /// Check whether the given new block is the best block, after finalization check. - fn fork_choice(&self, new: &M::ExtendedHeader, best: &M::ExtendedHeader) -> ForkChoice; + fn fork_choice(&self, new: &ExtendedHeader, best: &ExtendedHeader) -> ForkChoice; } /// Check whether a given block is the best block based on the default total difficulty rule. -pub fn total_difficulty_fork_choice(new: &T, best: &T) -> ForkChoice where ::Value: Ord { +pub fn total_difficulty_fork_choice(new: &ExtendedHeader, best: &ExtendedHeader) -> ForkChoice { if new.total_score() > best.total_score() { ForkChoice::New } else { diff --git a/ethcore/src/engines/null_engine.rs b/ethcore/src/engines/null_engine.rs index 4a2610259..74eb2dae8 100644 --- a/ethcore/src/engines/null_engine.rs +++ b/ethcore/src/engines/null_engine.rs @@ -18,8 +18,9 @@ use engines::Engine; use engines::block_reward::{self, RewardKind}; use ethereum_types::U256; use machine::WithRewards; -use parity_machine::{Machine, Header, LiveBlock, TotalScoredHeader}; +use parity_machine::{Machine, Header, LiveBlock}; use types::BlockNumber; +use types::header::ExtendedHeader; /// Params for a null engine. #[derive(Clone, Default)] @@ -58,10 +59,7 @@ impl Default for NullEngine { } } -impl Engine for NullEngine - where M::ExtendedHeader: TotalScoredHeader, - ::Value: Ord -{ +impl Engine for NullEngine { fn name(&self) -> &str { "NullEngine" } @@ -105,7 +103,7 @@ impl Engine for NullEngine Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000))) } - fn fork_choice(&self, new: &M::ExtendedHeader, current: &M::ExtendedHeader) -> super::ForkChoice { + fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice { super::total_difficulty_fork_choice(new, current) } } diff --git a/ethcore/src/machine.rs b/ethcore/src/machine.rs index c0eae63d1..c273fc84c 100644 --- a/ethcore/src/machine.rs +++ b/ethcore/src/machine.rs @@ -24,7 +24,7 @@ use ethereum_types::{U256, H256, Address}; use rlp::Rlp; use types::transaction::{self, SYSTEM_ADDRESS, UNSIGNED_SENDER, UnverifiedTransaction, SignedTransaction}; use types::BlockNumber; -use types::header::{Header, ExtendedHeader}; +use types::header::Header; use vm::{CallType, ActionParams, ActionValue, ParamsType}; use vm::{EnvInfo, Schedule, CreateContractAddress}; @@ -430,7 +430,6 @@ pub enum AuxiliaryRequest { impl ::parity_machine::Machine for EthereumMachine { type Header = Header; - type ExtendedHeader = ExtendedHeader; type LiveBlock = ExecutedBlock; type EngineClient = ::client::EngineClient; diff --git a/ethcore/types/src/header.rs b/ethcore/types/src/header.rs index 829776f01..e2b90e754 100644 --- a/ethcore/types/src/header.rs +++ b/ethcore/types/src/header.rs @@ -376,13 +376,6 @@ impl ::parity_machine::Header for Header { fn number(&self) -> BlockNumber { Header::number(self) } } -impl ::parity_machine::ScoredHeader for Header { - type Value = U256; - - fn score(&self) -> &U256 { self.difficulty() } - fn set_score(&mut self, score: U256) { self.set_difficulty(score) } -} - impl ::parity_machine::Header for ExtendedHeader { fn bare_hash(&self) -> H256 { self.header.bare_hash() } fn hash(&self) -> H256 { self.header.hash() } @@ -391,21 +384,11 @@ impl ::parity_machine::Header for ExtendedHeader { fn number(&self) -> BlockNumber { self.header.number() } } -impl ::parity_machine::ScoredHeader for ExtendedHeader { - type Value = U256; - - fn score(&self) -> &U256 { self.header.difficulty() } - fn set_score(&mut self, score: U256) { self.header.set_difficulty(score) } -} - -impl ::parity_machine::TotalScoredHeader for ExtendedHeader { - type Value = U256; - - fn total_score(&self) -> U256 { self.parent_total_difficulty + *self.header.difficulty() } -} - -impl ::parity_machine::FinalizableHeader for ExtendedHeader { - fn is_finalized(&self) -> bool { self.is_finalized } +impl ExtendedHeader { + /// Returns combined difficulty of all ancestors together with the difficulty of this header. + pub fn total_score(&self) -> U256 { + self.parent_total_difficulty + *self.header.difficulty() + } } #[cfg(test)] diff --git a/machine/src/lib.rs b/machine/src/lib.rs index b7054ca03..bf2b35e8b 100644 --- a/machine/src/lib.rs +++ b/machine/src/lib.rs @@ -40,37 +40,6 @@ pub trait Header { fn number(&self) -> u64; } -/// A header with an associated score (difficulty in PoW terms) -pub trait ScoredHeader: Header { - type Value; - - /// Get the score of this header. - fn score(&self) -> &Self::Value; - - /// Set the score of this header. - fn set_score(&mut self, score: Self::Value); -} - -/// A header with associated total score. -pub trait TotalScoredHeader: Header { - type Value; - - /// Get the total score of this header. - fn total_score(&self) -> Self::Value; -} - -/// A header with finalized information. -pub trait FinalizableHeader: Header { - /// Get whether this header is considered finalized, so that it will never be replaced in reorganization. - fn is_finalized(&self) -> bool; -} - -/// A header with metadata information. -pub trait WithMetadataHeader: Header { - /// Get the current header metadata. - fn metadata(&self) -> Option<&[u8]>; -} - /// A "live" block is one which is in the process of the transition. /// The state of this block can be mutated by arbitrary rules of the /// state transition function. @@ -101,8 +70,6 @@ pub trait Machine: for<'a> LocalizedMachine<'a> { type Header: Header; /// The live block type. type LiveBlock: LiveBlock; - /// Block header with metadata information. - type ExtendedHeader: Header; /// A handle to a blockchain client for this machine. type EngineClient: ?Sized; /// A description of needed auxiliary data.