removed redundant fork choice abstraction (#10849)

This commit is contained in:
Marek Kotewicz 2019-07-09 04:27:33 +02:00 committed by Wei Tang
parent 0f337171bf
commit fdc7b0fdaa
8 changed files with 17 additions and 61 deletions

View File

@ -40,7 +40,7 @@ use types::encoded;
use types::filter::Filter; use types::filter::Filter;
use types::log_entry::LocalizedLogEntry; use types::log_entry::LocalizedLogEntry;
use types::receipt::{Receipt, LocalizedReceipt}; use types::receipt::{Receipt, LocalizedReceipt};
use types::{BlockNumber, header::{Header, ExtendedHeader}}; use types::{BlockNumber, header::Header};
use vm::{EnvInfo, LastHashes}; use vm::{EnvInfo, LastHashes};
use hash_db::EMPTY_PREFIX; use hash_db::EMPTY_PREFIX;
use block::{LockedBlock, Drain, ClosedBlock, OpenBlock, enact_verified, SealedBlock}; use block::{LockedBlock, Drain, ClosedBlock, OpenBlock, enact_verified, SealedBlock};
@ -502,32 +502,24 @@ impl Importer {
let traces = block.traces.drain(); let traces = block.traces.drain();
let best_hash = chain.best_block_hash(); let best_hash = chain.best_block_hash();
let new = ExtendedHeader { let new_total_difficulty = {
header: header.clone(), let parent_total_difficulty = chain.block_details(&parent)
is_finalized, .expect("Parent block is in the database; qed")
parent_total_difficulty: chain.block_details(&parent).expect("Parent block is in the database; qed").total_difficulty .total_difficulty;
parent_total_difficulty + header.difficulty()
}; };
let best = { let best_total_difficulty = chain.block_details(&best_hash)
let header = chain.block_header_data(&best_hash) .expect("Best block is in the database; qed")
.expect("Best block is in the database; qed") .total_difficulty;
.decode()
.expect("Stored block header is valid RLP; qed");
let details = chain.block_details(&best_hash)
.expect("Best block is in the database; qed");
ExtendedHeader {
parent_total_difficulty: details.total_difficulty - *header.difficulty(),
is_finalized: details.is_finalized,
header,
}
};
let route = chain.tree_route(best_hash, *parent).expect("forks are only kept when it has common ancestors; tree route from best to prospective's parent always exists; qed"); let route = chain.tree_route(best_hash, *parent).expect("forks are only kept when it has common ancestors; tree route from best to prospective's parent always exists; qed");
let fork_choice = if route.is_from_route_finalized { let fork_choice = if route.is_from_route_finalized {
ForkChoice::Old ForkChoice::Old
} else if new_total_difficulty > best_total_difficulty {
ForkChoice::New
} else { } else {
self.engine.fork_choice(&new, &best) ForkChoice::Old
}; };
// CHECK! I *think* this is fine, even if the state_root is equal to another // CHECK! I *think* this is fine, even if the state_root is equal to another

View File

@ -1592,10 +1592,6 @@ impl Engine for AuthorityRound {
} }
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
fn ancestry_actions(&self, header: &Header, ancestry: &mut dyn Iterator<Item=ExtendedHeader>) -> Vec<AncestryAction> { fn ancestry_actions(&self, header: &Header, ancestry: &mut dyn Iterator<Item=ExtendedHeader>) -> Vec<AncestryAction> {
let finalized = self.build_finality( let finalized = self.build_finality(
header, header,

View File

@ -27,7 +27,7 @@ use error::{BlockError, Error};
use ethjson; use ethjson;
use client::EngineClient; use client::EngineClient;
use machine::{AuxiliaryData, Call, Machine}; use machine::{AuxiliaryData, Call, Machine};
use types::header::{Header, ExtendedHeader}; use types::header::Header;
use super::validator_set::{ValidatorSet, SimpleList, new_validator_set}; use super::validator_set::{ValidatorSet, SimpleList, new_validator_set};
/// `BasicAuthority` params. /// `BasicAuthority` params.
@ -208,10 +208,6 @@ impl Engine for BasicAuthority {
fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> { fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> {
None None
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -83,7 +83,7 @@ use super::signer::EngineSigner;
use unexpected::{Mismatch, OutOfBounds}; use unexpected::{Mismatch, OutOfBounds};
use time_utils::CheckedSystemTime; use time_utils::CheckedSystemTime;
use types::BlockNumber; use types::BlockNumber;
use types::header::{ExtendedHeader, Header}; use types::header::Header;
use self::block_state::CliqueBlockState; use self::block_state::CliqueBlockState;
use self::params::CliqueParams; use self::params::CliqueParams;
@ -766,10 +766,6 @@ impl Engine for Clique {
header_timestamp >= parent_timestamp.saturating_add(self.period) header_timestamp >= parent_timestamp.saturating_add(self.period)
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
// Clique uses the author field for voting, the real author is hidden in the `extra_data` field. // Clique uses the author field for voting, the real author is hidden in the `extra_data` field.
// So when executing tx's (like in `enact()`) we want to use the executive author // So when executing tx's (like in `enact()`) we want to use the executive author
fn executive_author(&self, header: &Header) -> Result<Address, Error> { fn executive_author(&self, header: &Header) -> Result<Address, Error> {

View File

@ -16,7 +16,7 @@
use engines::{Engine, Seal, SealingState}; use engines::{Engine, Seal, SealingState};
use machine::Machine; use machine::Machine;
use types::header::{Header, ExtendedHeader}; use types::header::Header;
use block::ExecutedBlock; use block::ExecutedBlock;
use error::Error; use error::Error;
@ -87,10 +87,6 @@ impl Engine for InstantSeal {
fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool { fn is_timestamp_valid(&self, header_timestamp: u64, parent_timestamp: u64) -> bool {
header_timestamp >= parent_timestamp header_timestamp >= parent_timestamp
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -469,9 +469,6 @@ pub trait Engine: Sync + Send {
Vec::new() Vec::new()
} }
/// Check whether the given new block is the best block, after finalization check.
fn fork_choice(&self, new: &ExtendedHeader, best: &ExtendedHeader) -> ForkChoice;
/// Returns author should used when executing tx's for this block. /// Returns author should used when executing tx's for this block.
fn executive_author(&self, header: &Header) -> Result<Address, Error> { fn executive_author(&self, header: &Header) -> Result<Address, Error> {
Ok(*header.author()) Ok(*header.author())
@ -550,15 +547,6 @@ pub trait Engine: Sync + Send {
} }
} }
/// Check whether a given block is the best block based on the default total difficulty rule.
pub fn total_difficulty_fork_choice(new: &ExtendedHeader, best: &ExtendedHeader) -> ForkChoice {
if new.total_score() > best.total_score() {
ForkChoice::New
} else {
ForkChoice::Old
}
}
/// Verifier for all blocks within an epoch with self-contained state. /// Verifier for all blocks within an epoch with self-contained state.
pub trait EpochVerifier: Send + Sync { pub trait EpochVerifier: Send + Sync {
/// Lightly verify the next block header. /// Lightly verify the next block header.

View File

@ -19,7 +19,7 @@ use engines::block_reward::{self, RewardKind};
use ethereum_types::U256; use ethereum_types::U256;
use machine::Machine; use machine::Machine;
use types::BlockNumber; use types::BlockNumber;
use types::header::{Header, ExtendedHeader}; use types::header::Header;
use block::ExecutedBlock; use block::ExecutedBlock;
use error::Error; use error::Error;
@ -101,8 +101,4 @@ impl Engine for NullEngine {
fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> { fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> {
Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000))) Some(Box::new(::snapshot::PowSnapshot::new(10000, 10000)))
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> super::ForkChoice {
super::total_difficulty_fork_choice(new, current)
}
} }

View File

@ -23,7 +23,7 @@ use ethereum_types::{H256, H64, U256};
use ethjson; use ethjson;
use hash::{KECCAK_EMPTY_LIST_RLP}; use hash::{KECCAK_EMPTY_LIST_RLP};
use rlp::Rlp; use rlp::Rlp;
use types::header::{Header, ExtendedHeader}; use types::header::Header;
use types::BlockNumber; use types::BlockNumber;
use unexpected::{OutOfBounds, Mismatch}; use unexpected::{OutOfBounds, Mismatch};
@ -380,10 +380,6 @@ impl Engine for Arc<Ethash> {
fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> { fn snapshot_components(&self) -> Option<Box<dyn (::snapshot::SnapshotComponents)>> {
Some(Box::new(::snapshot::PowSnapshot::new(SNAPSHOT_BLOCKS, MAX_SNAPSHOT_BLOCKS))) Some(Box::new(::snapshot::PowSnapshot::new(SNAPSHOT_BLOCKS, MAX_SNAPSHOT_BLOCKS)))
} }
fn fork_choice(&self, new: &ExtendedHeader, current: &ExtendedHeader) -> engines::ForkChoice {
engines::total_difficulty_fork_choice(new, current)
}
} }
impl Ethash { impl Ethash {