Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ num = { version = "0.1", default-features = false, features = ["bigint"] }
num_cpus = "1.2"
parity-bytes = "0.1"
parity-crypto = "0.3.0"
parity-machine = { path = "../machine" }
parity-snappy = "0.1"
parking_lot = "0.7"
trie-db = "0.11.0"
Expand Down
20 changes: 0 additions & 20 deletions ethcore/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,6 @@ impl IsBlock for ExecutedBlock {
fn block(&self) -> &ExecutedBlock { self }
}

impl ::parity_machine::LiveBlock for ExecutedBlock {
type Header = Header;

fn header(&self) -> &Header {
&self.header
}

fn uncles(&self) -> &[Header] {
&self.uncles
}
}

impl ::parity_machine::Transactions for ExecutedBlock {
type Transaction = SignedTransaction;

fn transactions(&self) -> &[SignedTransaction] {
&self.transactions
}
}

impl<'x> OpenBlock<'x> {
/// Create a new `OpenBlock` ready for transaction pushing.
pub fn new<'a>(
Expand Down
22 changes: 16 additions & 6 deletions ethcore/src/engines/block_reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ use ethereum_types::{H160, Address, U256};
use std::sync::Arc;
use hash::keccak;
use error::Error;
use machine::WithRewards;
use parity_machine::Machine;
use machine::Machine;
use trace;
use types::BlockNumber;
use super::{SystemOrCodeCall, SystemOrCodeCallKind};
use trace::{Tracer, ExecutiveTracer, Tracing};
use block::ExecutedBlock;

use_contract!(block_reward_contract, "res/contracts/block_reward.json");

Expand Down Expand Up @@ -152,17 +153,26 @@ impl BlockRewardContract {

/// Applies the given block rewards, i.e. adds the given balance to each beneficiary' address.
/// If tracing is enabled the operations are recorded.
pub fn apply_block_rewards<M: Machine + WithRewards>(
pub fn apply_block_rewards<M: Machine>(
rewards: &[(Address, RewardKind, U256)],
block: &mut M::LiveBlock,
block: &mut ExecutedBlock,
machine: &M,
) -> Result<(), M::Error> {
for &(ref author, _, ref block_reward) in rewards {
machine.add_balance(block, author, block_reward)?;
}

let rewards: Vec<_> = rewards.into_iter().map(|&(a, k, r)| (a, k.into(), r)).collect();
machine.note_rewards(block, &rewards)
if let Tracing::Enabled(ref mut traces) = *block.traces_mut() {
let mut tracer = ExecutiveTracer::default();

for &(address, reward_kind, amount) in rewards {
tracer.trace_reward(address, amount, reward_kind.into());
}

traces.push(tracer.drain().into());
}

Ok(())
}

#[cfg(test)]
Expand Down
17 changes: 11 additions & 6 deletions ethcore/src/engines/instant_seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

use engines::{Engine, Seal};
use parity_machine::{Machine, Transactions};
use types::header::ExtendedHeader;
use machine::Machine;
use types::header::{Header, ExtendedHeader};
use block::ExecutedBlock;

/// `InstantSeal` params.
#[derive(Default, Debug, PartialEq)]
Expand Down Expand Up @@ -49,7 +50,7 @@ impl<M> InstantSeal<M> {
}
}

impl<M: Machine> Engine<M> for InstantSeal<M> where M::LiveBlock: Transactions {
impl<M: Machine> Engine<M> for InstantSeal<M> {
fn name(&self) -> &str {
"InstantSeal"
}
Expand All @@ -58,11 +59,15 @@ impl<M: Machine> Engine<M> for InstantSeal<M> where M::LiveBlock: Transactions {

fn seals_internally(&self) -> Option<bool> { Some(true) }

fn generate_seal(&self, block: &M::LiveBlock, _parent: &M::Header) -> Seal {
if block.transactions().is_empty() { Seal::None } else { Seal::Regular(Vec::new()) }
fn generate_seal(&self, block: &ExecutedBlock, _parent: &Header) -> Seal {
if block.transactions.is_empty() {
Seal::None
} else {
Seal::Regular(Vec::new())
}
}

fn verify_local_seal(&self, _header: &M::Header) -> Result<(), M::Error> {
fn verify_local_seal(&self, _header: &Header) -> Result<(), M::Error> {
Ok(())
}

Expand Down
75 changes: 51 additions & 24 deletions ethcore/src/engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ pub mod signer;

pub use self::authority_round::AuthorityRound;
pub use self::basic_authority::BasicAuthority;
pub use self::epoch::{EpochVerifier, Transition as EpochTransition};
pub use self::instant_seal::{InstantSeal, InstantSealParams};
pub use self::null_engine::NullEngine;
pub use self::signer::EngineSigner;

// TODO [ToDr] Remove re-export (#10130)
pub use types::engines::ForkChoice;
pub use types::engines::epoch;
pub use types::engines::epoch::{self, Transition as EpochTransition};

use std::sync::{Weak, Arc};
use std::collections::{BTreeMap, HashMap};
Expand All @@ -50,11 +49,13 @@ use spec::CommonParams;
use types::transaction::{self, UnverifiedTransaction, SignedTransaction};

use ethkey::{Signature};
use parity_machine::{Machine, LocalizedMachine as Localized};
use machine::{Machine, LocalizedMachine as Localized};
use ethereum_types::{H256, U256, Address};
use unexpected::{Mismatch, OutOfBounds};
use bytes::Bytes;
use types::ancestry_action::AncestryAction;
use block::ExecutedBlock;
use machine;

/// Default EIP-210 contract code.
/// As defined in https://github.com/ethereum/EIPs/pull/210
Expand Down Expand Up @@ -235,10 +236,10 @@ pub trait Engine<M: Machine>: Sync + Send {
fn machine(&self) -> &M;

/// The number of additional header fields required for this engine.
fn seal_fields(&self, _header: &M::Header) -> usize { 0 }
fn seal_fields(&self, _header: &Header) -> usize { 0 }

/// Additional engine-specific information for the user/developer concerning `header`.
fn extra_info(&self, _header: &M::Header) -> BTreeMap<String, String> { BTreeMap::new() }
fn extra_info(&self, _header: &Header) -> BTreeMap<String, String> { BTreeMap::new() }

/// Maximum number of uncles a block is allowed to declare.
fn maximum_uncle_count(&self, _block: BlockNumber) -> usize { 0 }
Expand All @@ -253,15 +254,15 @@ pub trait Engine<M: Machine>: Sync + Send {
/// `epoch_begin` set to true if this block kicks off an epoch.
fn on_new_block(
&self,
_block: &mut M::LiveBlock,
_block: &mut ExecutedBlock,
_epoch_begin: bool,
_ancestry: &mut Iterator<Item = ExtendedHeader>,
) -> Result<(), M::Error> {
Ok(())
}

/// Block transformation functions, after the transactions.
fn on_close_block(&self, _block: &mut M::LiveBlock) -> Result<(), M::Error> {
fn on_close_block(&self, _block: &mut ExecutedBlock) -> Result<(), M::Error> {
Ok(())
}

Expand All @@ -279,7 +280,7 @@ pub trait Engine<M: Machine>: Sync + Send {
///
/// It is fine to require access to state or a full client for this function, since
/// light clients do not generate seals.
fn generate_seal(&self, _block: &M::LiveBlock, _parent: &M::Header) -> Seal { Seal::None }
fn generate_seal(&self, _block: &ExecutedBlock, _parent: &Header) -> Seal { Seal::None }

/// Verify a locally-generated seal of a header.
///
Expand All @@ -291,25 +292,25 @@ pub trait Engine<M: Machine>: Sync + Send {
///
/// It is fine to require access to state or a full client for this function, since
/// light clients do not generate seals.
fn verify_local_seal(&self, header: &M::Header) -> Result<(), M::Error>;
fn verify_local_seal(&self, header: &Header) -> Result<(), M::Error>;

/// Phase 1 quick block verification. Only does checks that are cheap. Returns either a null `Ok` or a general error detailing the problem with import.
/// The verification module can optionally avoid checking the seal (`check_seal`), if seal verification is disabled this method won't be called.
fn verify_block_basic(&self, _header: &M::Header) -> Result<(), M::Error> { Ok(()) }
fn verify_block_basic(&self, _header: &Header) -> Result<(), M::Error> { Ok(()) }

/// Phase 2 verification. Perform costly checks such as transaction signatures. Returns either a null `Ok` or a general error detailing the problem with import.
/// The verification module can optionally avoid checking the seal (`check_seal`), if seal verification is disabled this method won't be called.
fn verify_block_unordered(&self, _header: &M::Header) -> Result<(), M::Error> { Ok(()) }
fn verify_block_unordered(&self, _header: &Header) -> Result<(), M::Error> { Ok(()) }

/// Phase 3 verification. Check block information against parent. Returns either a null `Ok` or a general error detailing the problem with import.
fn verify_block_family(&self, _header: &M::Header, _parent: &M::Header) -> Result<(), M::Error> { Ok(()) }
fn verify_block_family(&self, _header: &Header, _parent: &Header) -> Result<(), M::Error> { Ok(()) }

/// Phase 4 verification. Verify block header against potentially external data.
/// Should only be called when `register_client` has been called previously.
fn verify_block_external(&self, _header: &M::Header) -> Result<(), M::Error> { Ok(()) }
fn verify_block_external(&self, _header: &Header) -> Result<(), M::Error> { Ok(()) }

/// Genesis epoch data.
fn genesis_epoch_data<'a>(&self, _header: &M::Header, _state: &<M as Localized<'a>>::StateContext) -> Result<Vec<u8>, String> { Ok(Vec::new()) }
fn genesis_epoch_data<'a>(&self, _header: &Header, _state: &<M as Localized<'a>>::StateContext) -> Result<Vec<u8>, String> { Ok(Vec::new()) }

/// Whether an epoch change is signalled at the given header but will require finality.
/// If a change can be enacted immediately then return `No` from this function but
Expand All @@ -320,7 +321,7 @@ pub trait Engine<M: Machine>: Sync + Send {
/// Return `Yes` or `No` when the answer is definitively known.
///
/// Should not interact with state.
fn signals_epoch_end<'a>(&self, _header: &M::Header, _aux: <M as Localized<'a>>::AuxiliaryData)
fn signals_epoch_end<'a>(&self, _header: &Header, _aux: <M as Localized<'a>>::AuxiliaryData)
-> EpochChange<M>
{
EpochChange::No
Expand All @@ -336,9 +337,9 @@ pub trait Engine<M: Machine>: Sync + Send {
/// Return optional transition proof.
fn is_epoch_end(
&self,
_chain_head: &M::Header,
_chain_head: &Header,
_finalized: &[H256],
_chain: &Headers<M::Header>,
_chain: &Headers<Header>,
_transition_store: &PendingTransitionStore,
) -> Option<Vec<u8>> {
None
Expand All @@ -355,30 +356,30 @@ pub trait Engine<M: Machine>: Sync + Send {
/// Return optional transition proof.
fn is_epoch_end_light(
&self,
_chain_head: &M::Header,
_chain: &Headers<M::Header>,
_chain_head: &Header,
_chain: &Headers<Header>,
_transition_store: &PendingTransitionStore,
) -> Option<Vec<u8>> {
None
}

/// Create an epoch verifier from validation proof and a flag indicating
/// whether finality is required.
fn epoch_verifier<'a>(&self, _header: &M::Header, _proof: &'a [u8]) -> ConstructedVerifier<'a, M> {
ConstructedVerifier::Trusted(Box::new(self::epoch::NoOp))
fn epoch_verifier<'a>(&self, _header: &Header, _proof: &'a [u8]) -> ConstructedVerifier<'a, M> {
ConstructedVerifier::Trusted(Box::new(NoOp))
}

/// Populate a header's fields based on its parent's header.
/// Usually implements the chain scoring rule based on weight.
fn populate_from_parent(&self, _header: &mut M::Header, _parent: &M::Header) { }
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) { }

/// Handle any potential consensus messages;
/// updating consensus state and potentially issuing a new one.
fn handle_message(&self, _message: &[u8]) -> Result<(), EngineError> { Err(EngineError::UnexpectedMessage) }

/// Find out if the block is a proposal block and should not be inserted into the DB.
/// Takes a header of a fully verified block.
fn is_proposal(&self, _verified_header: &M::Header) -> bool { false }
fn is_proposal(&self, _verified_header: &Header) -> bool { false }

/// Register a component which signs consensus messages.
fn set_signer(&self, _signer: Box<EngineSigner>) {}
Expand Down Expand Up @@ -421,7 +422,7 @@ pub trait Engine<M: Machine>: 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<Item = ExtendedHeader>) -> Vec<AncestryAction> {
fn ancestry_actions(&self, _header: &Header, _ancestry: &mut Iterator<Item = ExtendedHeader>) -> Vec<AncestryAction> {
Vec::new()
}

Expand Down Expand Up @@ -523,3 +524,29 @@ pub trait EthEngine: Engine<::machine::EthereumMachine> {

// convenience wrappers for existing functions.
impl<T> EthEngine for T where T: Engine<::machine::EthereumMachine> { }

/// Verifier for all blocks within an epoch with self-contained state.
pub trait EpochVerifier<M: machine::Machine>: Send + Sync {
/// Lightly verify the next block header.
/// This may not be a header belonging to a different epoch.
fn verify_light(&self, header: &Header) -> Result<(), M::Error>;

/// Perform potentially heavier checks on the next block header.
fn verify_heavy(&self, header: &Header) -> Result<(), M::Error> {
self.verify_light(header)
}

/// Check a finality proof against this epoch verifier.
/// Returns `Some(hashes)` if the proof proves finality of these hashes.
/// Returns `None` if the proof doesn't prove anything.
fn check_finality_proof(&self, _proof: &[u8]) -> Option<Vec<H256>> {
None
}
}

/// Special "no-op" verifier for stateless, epoch-less engines.
pub struct NoOp;

impl<M: machine::Machine> EpochVerifier<M> for NoOp {
fn verify_light(&self, _header: &Header) -> Result<(), M::Error> { Ok(()) }
}
Loading