From e41a42a46eea78bb1be99334897d8f72ab9d0c6e Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Tue, 30 Apr 2019 21:06:08 +0900 Subject: [PATCH 1/6] Remove EpochChange `EpochChange::{Yes,Unsure}` is never constructed. Only `EpochChange::No` is used. We can remove these codes safely since they are dead-code after propagating constant. --- core/src/client/importer.rs | 30 ------------------- core/src/consensus/mod.rs | 25 ---------------- core/src/consensus/simple_poa/mod.rs | 13 -------- core/src/consensus/tendermint/engine.rs | 7 +---- core/src/consensus/validator_set/mod.rs | 8 ----- .../consensus/validator_set/null_validator.rs | 5 ---- .../consensus/validator_set/validator_list.rs | 5 ---- 7 files changed, 1 insertion(+), 92 deletions(-) diff --git a/core/src/client/importer.rs b/core/src/client/importer.rs index 6ddb9c1481..45326d321e 100644 --- a/core/src/client/importer.rs +++ b/core/src/client/importer.rs @@ -210,9 +210,6 @@ impl Importer { let mut batch = DBTransaction::new(); - // check epoch end signal - self.check_epoch_end_signal(block.header(), &chain, &mut batch); - block.state().journal_under(&mut batch, number).expect("DB commit failed"); let route = chain.insert_block(&mut batch, block_data, invoices.clone(), self.engine.borrow()); @@ -256,33 +253,6 @@ impl Importer { } } - // check for epoch end signal and write pending transition if it occurs. - // state for the given block must be available. - fn check_epoch_end_signal(&self, header: &Header, chain: &BlockChain, batch: &mut DBTransaction) { - use crate::consensus::EpochChange; - let hash = header.hash(); - - match self.engine.signals_epoch_end(header) { - EpochChange::Yes(proof) => { - use crate::consensus::epoch::PendingTransition; - use crate::consensus::Proof; - - let Proof::Known(proof) = proof; - cdebug!(CLIENT, "Block {} signals epoch end.", hash); - - let pending = PendingTransition { - proof, - }; - chain.insert_pending_transition(batch, hash, &pending); - } - EpochChange::No => {} - EpochChange::Unsure => { - cwarn!(CLIENT, "Detected invalid engine implementation."); - cwarn!(CLIENT, "Engine claims to require more block data, but everything provided."); - } - } - } - fn check_and_close_block(&self, block: &PreverifiedBlock, client: &Client) -> Result { let engine = &*self.engine; let header = &block.header; diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 6b5d4d2467..0611171af7 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -207,15 +207,6 @@ pub trait ConsensusEngine: Sync + Send { 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 - /// `Yes` from `is_epoch_end`. - /// - /// Return `Yes` or `No` when the answer is definitively known. - fn signals_epoch_end(&self, _header: &M::Header) -> EpochChange { - EpochChange::No - } - /// Whether a block is the end of an epoch. /// /// This either means that an immediate transition occurs or a block signalling transition @@ -313,22 +304,6 @@ pub trait ConsensusEngine: Sync + Send { } } -/// Results of a query of whether an epoch change occurred at the given block. -pub enum EpochChange { - /// Cannot determine until more data is passed. - Unsure, - /// No epoch change. - No, - /// The epoch will change, with proof. - Yes(Proof), -} - -/// Proof generated on epoch change. -pub enum Proof { - /// Known proof (extracted from signal) - Known(Vec), -} - /// Generated epoch verifier. pub enum ConstructedVerifier<'a, M: Machine> { /// Fully trusted verifier. diff --git a/core/src/consensus/simple_poa/mod.rs b/core/src/consensus/simple_poa/mod.rs index cd602dde8f..1fea87cc5b 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -132,19 +132,6 @@ impl ConsensusEngine for SimplePoA { self.validators.genesis_epoch_data(header) } - #[cfg(not(test))] - fn signals_epoch_end(&self, _header: &Header) -> super::EpochChange { - // don't bother signalling even though a contract might try. - super::EpochChange::No - } - - #[cfg(test)] - fn signals_epoch_end(&self, header: &Header) -> super::EpochChange { - // in test mode, always signal even though they don't be finalized. - let first = header.number() == 0; - self.validators.signals_epoch_end(first, header) - } - fn is_epoch_end( &self, chain_head: &Header, diff --git a/core/src/consensus/tendermint/engine.rs b/core/src/consensus/tendermint/engine.rs index 5e1a572365..c38f9e6f33 100644 --- a/core/src/consensus/tendermint/engine.rs +++ b/core/src/consensus/tendermint/engine.rs @@ -29,7 +29,7 @@ use primitives::H256; use rlp::UntrustedRlp; use super::super::stake; -use super::super::{ConsensusEngine, ConstructedVerifier, EngineError, EpochChange, Seal}; +use super::super::{ConsensusEngine, ConstructedVerifier, EngineError, Seal}; use super::epoch_verifier::EpochVerifier; use super::network::TendermintExtension; pub use super::params::{TendermintParams, TimeoutParams}; @@ -117,11 +117,6 @@ impl ConsensusEngine for Tendermint { receiver.recv().unwrap() } - fn signals_epoch_end(&self, header: &Header) -> EpochChange { - let first = header.number() == 0; - self.validators.signals_epoch_end(first, header) - } - fn is_epoch_end( &self, chain_head: &Header, diff --git a/core/src/consensus/validator_set/mod.rs b/core/src/consensus/validator_set/mod.rs index f2ed136e3d..0ac5d0b5c7 100644 --- a/core/src/consensus/validator_set/mod.rs +++ b/core/src/consensus/validator_set/mod.rs @@ -21,7 +21,6 @@ use ctypes::BlockNumber; use primitives::{Bytes, H256}; use self::validator_list::ValidatorList; -use super::EpochChange; use crate::client::EngineClient; use crate::codechain_machine::CodeChainMachine; use crate::error::Error; @@ -81,13 +80,6 @@ pub trait ValidatorSet: Send + Sync { /// `first` is true if this is the first block in the set. fn is_epoch_end(&self, first: bool, chain_head: &Header) -> Option>; - /// Whether the given block signals the end of an epoch, but change won't take effect - /// until finality. - /// - /// Engine should set `first` only if the header is genesis. Multiplexing validator - /// sets can set `first` to internal changes. - fn signals_epoch_end(&self, first: bool, header: &Header) -> EpochChange; - /// Recover the validator set from the given proof, the block number, and /// whether this header is first in its set. /// diff --git a/core/src/consensus/validator_set/null_validator.rs b/core/src/consensus/validator_set/null_validator.rs index ccd98908b9..cf667214bb 100644 --- a/core/src/consensus/validator_set/null_validator.rs +++ b/core/src/consensus/validator_set/null_validator.rs @@ -18,7 +18,6 @@ use ckey::{Address, Public}; use primitives::H256; -use super::super::EpochChange; use super::validator_list::ValidatorList; use super::ValidatorSet; use crate::codechain_machine::CodeChainMachine; @@ -62,10 +61,6 @@ impl ValidatorSet for NullValidator { unimplemented!() } - fn signals_epoch_end(&self, _first: bool, _header: &Header) -> EpochChange { - unimplemented!() - } - fn epoch_set( &self, _first: bool, diff --git a/core/src/consensus/validator_set/validator_list.rs b/core/src/consensus/validator_set/validator_list.rs index 3a551b8959..99f9d84f23 100644 --- a/core/src/consensus/validator_set/validator_list.rs +++ b/core/src/consensus/validator_set/validator_list.rs @@ -20,7 +20,6 @@ use ckey::{public_to_address, Address, Public}; use ctypes::BlockNumber; use primitives::H256; -use super::super::EpochChange; use super::ValidatorSet; use crate::codechain_machine::CodeChainMachine; use crate::error::Error; @@ -104,10 +103,6 @@ impl ValidatorSet for ValidatorList { } } - fn signals_epoch_end(&self, _: bool, _: &Header) -> EpochChange { - EpochChange::No - } - fn epoch_set( &self, _first: bool, From 8d5b972d4b776f879fa48d2b76637712a4670f0e Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Tue, 30 Apr 2019 21:42:41 +0900 Subject: [PATCH 2/6] Remove PendingTransition `insert_pending_transition` is not used anymore after removing `EpochChange`. `get_pending_transition` should alway return `None` since there are no inserted `PendingTransition`s. --- core/src/blockchain/blockchain.rs | 14 +------------- core/src/blockchain/extras.rs | 13 ++----------- core/src/client/importer.rs | 6 +----- core/src/consensus/epoch.rs | 21 --------------------- core/src/consensus/mod.rs | 12 ++---------- core/src/consensus/simple_poa/mod.rs | 7 +------ core/src/consensus/tendermint/engine.rs | 11 +---------- 7 files changed, 8 insertions(+), 76 deletions(-) diff --git a/core/src/blockchain/blockchain.rs b/core/src/blockchain/blockchain.rs index dc50940423..360ddbf3d0 100644 --- a/core/src/blockchain/blockchain.rs +++ b/core/src/blockchain/blockchain.rs @@ -30,7 +30,7 @@ use super::headerchain::{HeaderChain, HeaderProvider}; use super::invoice_db::{InvoiceDB, InvoiceProvider}; use super::route::{tree_route, ImportRoute}; use crate::blockchain_info::BlockChainInfo; -use crate::consensus::epoch::{PendingTransition as PendingEpochTransition, Transition as EpochTransition}; +use crate::consensus::epoch::Transition as EpochTransition; use crate::consensus::CodeChainEngine; use crate::db::{self, Readable, Writable}; use crate::encoded; @@ -427,18 +427,6 @@ impl BlockChain { None } } - - /// Write a pending epoch transition by block hash. - pub fn insert_pending_transition(&self, batch: &mut DBTransaction, hash: H256, t: &PendingEpochTransition) { - batch.write(db::COL_EXTRA, &hash, t); - } - - /// Get a pending epoch transition by block hash. - // TODO: implement removal safely: this can only be done upon finality of a block - // that _uses_ the pending transition. - pub fn get_pending_transition(&self, hash: H256) -> Option { - self.db.read(db::COL_EXTRA, &hash) - } } /// An iterator which walks the blockchain towards the genesis. diff --git a/core/src/blockchain/extras.rs b/core/src/blockchain/extras.rs index 51180029e8..8daa900eb0 100644 --- a/core/src/blockchain/extras.rs +++ b/core/src/blockchain/extras.rs @@ -21,7 +21,7 @@ use ctypes::BlockNumber; use kvdb::PREFIX_LEN as DB_PREFIX_LEN; use primitives::{H256, H264, U256}; -use crate::consensus::epoch::{PendingTransition as PendingEpochTransition, Transition as EpochTransition}; +use crate::consensus::epoch::Transition as EpochTransition; use crate::db::Key; use crate::types::TransactionId; @@ -38,8 +38,7 @@ enum ExtrasIndex { TransactionAddress = 3, /// Epoch transition data index. EpochTransitions = 4, - /// Pending epoch transition data index. - PendingEpochTransition = 5, + // (Reserved) = 5, } fn with_index(hash: &H256, i: ExtrasIndex) -> H264 { @@ -130,14 +129,6 @@ impl Key for u64 { } } -impl Key for H256 { - type Target = H264; - - fn key(&self) -> H264 { - with_index(self, ExtrasIndex::PendingEpochTransition) - } -} - /// Familial details concerning a block #[derive(Debug, Clone, RlpEncodable, RlpDecodable)] pub struct BlockDetails { diff --git a/core/src/client/importer.rs b/core/src/client/importer.rs index 45326d321e..df1322c006 100644 --- a/core/src/client/importer.rs +++ b/core/src/client/importer.rs @@ -230,11 +230,7 @@ impl Importer { // check for ending of epoch and write transition if it occurs. fn check_epoch_end(&self, header: &Header, chain: &BlockChain, client: &Client) { - let is_epoch_end = self.engine.is_epoch_end( - header, - &(|hash| chain.block_header(&hash)), - &(|hash| chain.get_pending_transition(hash)), // TODO: limit to current epoch. - ); + let is_epoch_end = self.engine.is_epoch_end(header, &(|hash| chain.block_header(&hash))); if let Some(proof) = is_epoch_end { cdebug!(CLIENT, "Epoch transition at block {}", header.hash()); diff --git a/core/src/consensus/epoch.rs b/core/src/consensus/epoch.rs index 1d782d6da8..4e67c41302 100644 --- a/core/src/consensus/epoch.rs +++ b/core/src/consensus/epoch.rs @@ -79,24 +79,3 @@ impl Decodable for Transition { }) } } - -/// An epoch transition pending a finality proof. -/// Not all transitions need one. -pub struct PendingTransition { - /// "transition/epoch" proof from the engine. - pub proof: Vec, -} - -impl Encodable for PendingTransition { - fn rlp_append(&self, s: &mut RlpStream) { - s.append_single_value(&self.proof); - } -} - -impl Decodable for PendingTransition { - fn decode(rlp: &UntrustedRlp) -> Result { - Ok(PendingTransition { - proof: rlp.as_val()?, - }) - } -} diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 0611171af7..a9da91db55 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -47,7 +47,7 @@ use ctypes::transaction::Action; use ctypes::util::unexpected::{Mismatch, OutOfBounds}; use primitives::{Bytes, H256, U256}; -use self::epoch::{EpochVerifier, NoOp, PendingTransition}; +use self::epoch::{EpochVerifier, NoOp}; use self::tendermint::types::{BitSet, View}; use crate::account_provider::AccountProvider; use crate::block::SealedBlock; @@ -214,12 +214,7 @@ pub trait ConsensusEngine: Sync + Send { /// from any epoch other than the current. /// /// Return optional transition proof. - fn is_epoch_end( - &self, - _chain_head: &M::Header, - _chain: &Headers, - _transition_store: &PendingTransitionStore, - ) -> Option> { + fn is_epoch_end(&self, _chain_head: &M::Header, _chain: &Headers) -> Option> { None } @@ -318,9 +313,6 @@ pub enum ConstructedVerifier<'a, M: Machine> { /// Type alias for a function we can get headers by hash through. pub type Headers<'a, H> = Fn(H256) -> Option + 'a; -/// Type alias for a function we can query pending transitions by block hash through. -pub type PendingTransitionStore<'a> = Fn(H256) -> Option + 'a; - /// Voting errors. #[derive(Debug)] pub enum EngineError { diff --git a/core/src/consensus/simple_poa/mod.rs b/core/src/consensus/simple_poa/mod.rs index 1fea87cc5b..3112b3ac8a 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -132,12 +132,7 @@ impl ConsensusEngine for SimplePoA { self.validators.genesis_epoch_data(header) } - fn is_epoch_end( - &self, - chain_head: &Header, - _chain: &super::Headers
, - _transition_store: &super::PendingTransitionStore, - ) -> Option> { + fn is_epoch_end(&self, chain_head: &Header, _chain: &super::Headers
) -> Option> { let first = chain_head.number() == 0; // finality never occurs so only apply immediate transitions. diff --git a/core/src/consensus/tendermint/engine.rs b/core/src/consensus/tendermint/engine.rs index c38f9e6f33..cb0bf36518 100644 --- a/core/src/consensus/tendermint/engine.rs +++ b/core/src/consensus/tendermint/engine.rs @@ -117,21 +117,12 @@ impl ConsensusEngine for Tendermint { receiver.recv().unwrap() } - fn is_epoch_end( - &self, - chain_head: &Header, - _chain: &super::super::Headers
, - transition_store: &super::super::PendingTransitionStore, - ) -> Option> { + fn is_epoch_end(&self, chain_head: &Header, _chain: &super::super::Headers
) -> Option> { let first = chain_head.number() == 0; if let Some(change) = self.validators.is_epoch_end(first, chain_head) { let change = combine_proofs(chain_head.number(), &change, &[]); return Some(change) - } else if let Some(pending) = transition_store(chain_head.hash()) { - let signal_number = chain_head.number(); - let finality_proof = ::rlp::encode(chain_head); - return Some(combine_proofs(signal_number, &pending.proof, &finality_proof)) } None From 5fc8338b03ab530f73d1c8df4c62013e194e8401 Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Tue, 30 Apr 2019 21:18:33 +0900 Subject: [PATCH 3/6] Remove EpochVerifier `ConsensusEngine::epoch_verifier` is not used anywhere. --- core/src/consensus/epoch.rs | 29 ------- core/src/consensus/mod.rs | 18 ---- core/src/consensus/simple_poa/mod.rs | 31 +------ core/src/consensus/tendermint/engine.rs | 33 +------ .../consensus/tendermint/epoch_verifier.rs | 87 ------------------- core/src/consensus/tendermint/mod.rs | 1 - 6 files changed, 3 insertions(+), 196 deletions(-) delete mode 100644 core/src/consensus/tendermint/epoch_verifier.rs diff --git a/core/src/consensus/epoch.rs b/core/src/consensus/epoch.rs index 4e67c41302..f891df20e8 100644 --- a/core/src/consensus/epoch.rs +++ b/core/src/consensus/epoch.rs @@ -14,38 +14,9 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use ctypes::machine::Machine; use primitives::H256; use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp}; -/// Verifier for all blocks within an epoch with self-contained state. -pub trait EpochVerifier: Send + Sync { - /// Lightly verify the next block header. - /// This may not be a header belonging to a different epoch. - fn verify_light(&self, header: &M::Header) -> Result<(), M::Error>; - - /// Perform potentially heavier checks on the next block header. - fn verify_heavy(&self, header: &M::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> { - None - } -} - -/// Special "no-op" verifier for stateless, epoch-less engines. -pub struct NoOp; - -impl EpochVerifier for NoOp { - fn verify_light(&self, _header: &M::Header) -> Result<(), M::Error> { - Ok(()) - } -} - /// A full epoch transition. #[derive(Debug, Clone)] pub struct Transition { diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index a9da91db55..5c00d7a44c 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -47,7 +47,6 @@ use ctypes::transaction::Action; use ctypes::util::unexpected::{Mismatch, OutOfBounds}; use primitives::{Bytes, H256, U256}; -use self::epoch::{EpochVerifier, NoOp}; use self::tendermint::types::{BitSet, View}; use crate::account_provider::AccountProvider; use crate::block::SealedBlock; @@ -218,12 +217,6 @@ pub trait ConsensusEngine: Sync + Send { 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(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) {} @@ -299,17 +292,6 @@ pub trait ConsensusEngine: Sync + Send { } } -/// Generated epoch verifier. -pub enum ConstructedVerifier<'a, M: Machine> { - /// Fully trusted verifier. - Trusted(Box>), - /// Verifier unconfirmed. Check whether given finality proof finalizes given hash - /// under previous epoch. - Unconfirmed(Box>, &'a [u8], H256), - /// Error constructing verifier. - Err(Error), -} - /// Type alias for a function we can get headers by hash through. pub type Headers<'a, H> = Fn(H256) -> Option + 'a; diff --git a/core/src/consensus/simple_poa/mod.rs b/core/src/consensus/simple_poa/mod.rs index 3112b3ac8a..315c075e18 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -26,7 +26,7 @@ use self::params::SimplePoAParams; use super::signer::EngineSigner; use super::validator_set::validator_list::ValidatorList; use super::validator_set::ValidatorSet; -use super::{ConsensusEngine, ConstructedVerifier, EngineError, Seal}; +use super::{ConsensusEngine, EngineError, Seal}; use crate::account_provider::AccountProvider; use crate::block::{ExecutedBlock, IsBlock}; use crate::client::EngineClient; @@ -55,16 +55,6 @@ impl SimplePoA { } } -struct EpochVerifier { - list: ValidatorList, -} - -impl super::epoch::EpochVerifier for EpochVerifier { - fn verify_light(&self, header: &Header) -> Result<(), Error> { - verify_external(header, &self.list) - } -} - fn verify_external(header: &Header, validators: &ValidatorSet) -> Result<(), Error> { use rlp::UntrustedRlp; @@ -139,25 +129,6 @@ impl ConsensusEngine for SimplePoA { self.validators.is_epoch_end(first, chain_head) } - fn epoch_verifier<'a>(&self, header: &Header, proof: &'a [u8]) -> ConstructedVerifier<'a, CodeChainMachine> { - let first = header.number() == 0; - - match self.validators.epoch_set(first, &self.machine, header.number(), proof) { - Ok((list, finalize)) => { - let verifier = Box::new(EpochVerifier { - list, - }); - - // our epoch verifier will ensure no unverified verifier is ever verified. - match finalize { - Some(finalize) => ConstructedVerifier::Unconfirmed(verifier, proof, finalize), - None => ConstructedVerifier::Trusted(verifier), - } - } - Err(e) => ConstructedVerifier::Err(e), - } - } - fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { let author = *block.header().author(); let total_reward = self.block_reward(block.header().number()) diff --git a/core/src/consensus/tendermint/engine.rs b/core/src/consensus/tendermint/engine.rs index cb0bf36518..beee9033a9 100644 --- a/core/src/consensus/tendermint/engine.rs +++ b/core/src/consensus/tendermint/engine.rs @@ -18,7 +18,7 @@ use std::iter::Iterator; use std::sync::atomic::Ordering as AtomicOrdering; use std::sync::{Arc, Weak}; -use ckey::{public_to_address, recover_schnorr, Address, Message, SchnorrSignature}; +use ckey::Address; use cnetwork::NetworkService; use crossbeam_channel as crossbeam; use cstate::ActionHandler; @@ -26,11 +26,9 @@ use ctypes::machine::WithBalances; use ctypes::transaction::Action; use ctypes::BlockNumber; use primitives::H256; -use rlp::UntrustedRlp; use super::super::stake; -use super::super::{ConsensusEngine, ConstructedVerifier, EngineError, Seal}; -use super::epoch_verifier::EpochVerifier; +use super::super::{ConsensusEngine, EngineError, Seal}; use super::network::TendermintExtension; pub use super::params::{TendermintParams, TimeoutParams}; use super::worker; @@ -128,28 +126,6 @@ impl ConsensusEngine for Tendermint { None } - fn epoch_verifier<'a>(&self, _header: &Header, proof: &'a [u8]) -> ConstructedVerifier<'a, CodeChainMachine> { - let (signal_number, set_proof, finality_proof) = match destructure_proofs(proof) { - Ok(x) => x, - Err(e) => return ConstructedVerifier::Err(e), - }; - - let first = signal_number == 0; - match self.validators.epoch_set(first, &self.machine, signal_number, set_proof) { - Ok((list, finalize)) => { - let verifier = Box::new(EpochVerifier::new(list, |signature: &SchnorrSignature, message: &Message| { - Ok(public_to_address(&recover_schnorr(&signature, &message)?)) - })); - - match finalize { - Some(finalize) => ConstructedVerifier::Unconfirmed(verifier, finality_proof, finalize), - None => ConstructedVerifier::Trusted(verifier), - } - } - Err(e) => ConstructedVerifier::Err(e), - } - } - fn populate_from_parent(&self, header: &mut Header, _parent: &Header) { let (result, receiver) = crossbeam::bounded(1); self.inner @@ -344,8 +320,3 @@ fn combine_proofs(signal_number: BlockNumber, set_proof: &[u8], finality_proof: stream.append(&signal_number).append(&set_proof).append(&finality_proof); stream.out() } - -fn destructure_proofs(combined: &[u8]) -> Result<(BlockNumber, &[u8], &[u8]), Error> { - let rlp = UntrustedRlp::new(combined); - Ok((rlp.at(0)?.as_val()?, rlp.at(1)?.data()?, rlp.at(2)?.data()?)) -} diff --git a/core/src/consensus/tendermint/epoch_verifier.rs b/core/src/consensus/tendermint/epoch_verifier.rs deleted file mode 100644 index 435e8e5e3b..0000000000 --- a/core/src/consensus/tendermint/epoch_verifier.rs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2018-2019 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -use std::collections::HashSet; - -use ckey::{Address, Message, SchnorrSignature}; -use ctypes::util::unexpected::OutOfBounds; -use primitives::H256; -use rlp::UntrustedRlp; - -use crate::codechain_machine::CodeChainMachine; -use crate::consensus::validator_set::validator_list::ValidatorList; -use crate::consensus::validator_set::ValidatorSet; -use crate::consensus::EngineError; -use crate::error::{BlockError, Error}; -use crate::header::Header; - -pub struct EpochVerifier -where - F: Fn(&SchnorrSignature, &Message) -> Result + Send + Sync, { - subchain_validators: ValidatorList, - recover: F, -} - -impl EpochVerifier -where - F: Fn(&SchnorrSignature, &Message) -> Result + Send + Sync, -{ - pub fn new(subchain_validators: ValidatorList, recover: F) -> Self { - Self { - subchain_validators, - recover, - } - } -} - -impl super::super::EpochVerifier for EpochVerifier -where - F: Fn(&SchnorrSignature, &Message) -> Result + Send + Sync, -{ - fn verify_light(&self, header: &Header) -> Result<(), Error> { - let message = header.hash(); - - let mut addresses = HashSet::new(); - let header_precommits_field = &header.seal().get(2).ok_or(BlockError::InvalidSeal)?; - for rlp in UntrustedRlp::new(header_precommits_field).iter() { - let signature: SchnorrSignature = rlp.as_val()?; - let address = (self.recover)(&signature, &message)?; - - if !self.subchain_validators.contains_address(header.parent_hash(), &address) { - return Err(EngineError::BlockNotAuthorized(address.to_owned()).into()) - } - addresses.insert(address); - } - - let n = addresses.len(); - let threshold = self.subchain_validators.len() * 2 / 3; - if n > threshold { - Ok(()) - } else { - Err(EngineError::BadSealFieldSize(OutOfBounds { - min: Some(threshold), - max: None, - found: n, - }) - .into()) - } - } - - fn check_finality_proof(&self, proof: &[u8]) -> Option> { - let header: Header = ::rlp::decode(proof); - self.verify_light(&header).ok().map(|_| vec![header.hash()]) - } -} diff --git a/core/src/consensus/tendermint/mod.rs b/core/src/consensus/tendermint/mod.rs index 8f08e9b057..00e21c88ae 100644 --- a/core/src/consensus/tendermint/mod.rs +++ b/core/src/consensus/tendermint/mod.rs @@ -17,7 +17,6 @@ mod backup; mod chain_notify; mod engine; -mod epoch_verifier; mod message; mod network; mod params; From 25ab3bae81bd5e388cec9d78df30a0bf8a95412d Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Tue, 30 Apr 2019 21:59:30 +0900 Subject: [PATCH 4/6] Remove ValidatorSet::is_epoch_end Constant propagate and remove is_epoch_end --- core/src/consensus/simple_poa/mod.rs | 8 +++++--- core/src/consensus/tendermint/engine.rs | 5 ++--- core/src/consensus/tendermint/mod.rs | 5 ----- core/src/consensus/validator_set/mod.rs | 8 -------- core/src/consensus/validator_set/null_validator.rs | 5 ----- core/src/consensus/validator_set/validator_list.rs | 9 --------- 6 files changed, 7 insertions(+), 33 deletions(-) diff --git a/core/src/consensus/simple_poa/mod.rs b/core/src/consensus/simple_poa/mod.rs index 315c075e18..2ee258b2d6 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -124,9 +124,11 @@ impl ConsensusEngine for SimplePoA { fn is_epoch_end(&self, chain_head: &Header, _chain: &super::Headers
) -> Option> { let first = chain_head.number() == 0; - - // finality never occurs so only apply immediate transitions. - self.validators.is_epoch_end(first, chain_head) + if first { + Some(Vec::new()) + } else { + None + } } fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> { diff --git a/core/src/consensus/tendermint/engine.rs b/core/src/consensus/tendermint/engine.rs index beee9033a9..3028c32950 100644 --- a/core/src/consensus/tendermint/engine.rs +++ b/core/src/consensus/tendermint/engine.rs @@ -117,9 +117,8 @@ impl ConsensusEngine for Tendermint { fn is_epoch_end(&self, chain_head: &Header, _chain: &super::super::Headers
) -> Option> { let first = chain_head.number() == 0; - - if let Some(change) = self.validators.is_epoch_end(first, chain_head) { - let change = combine_proofs(chain_head.number(), &change, &[]); + if first { + let change = combine_proofs(chain_head.number(), &[], &[]); return Some(change) } diff --git a/core/src/consensus/tendermint/mod.rs b/core/src/consensus/tendermint/mod.rs index 00e21c88ae..02c47d463a 100644 --- a/core/src/consensus/tendermint/mod.rs +++ b/core/src/consensus/tendermint/mod.rs @@ -37,7 +37,6 @@ use self::chain_notify::TendermintChainNotify; pub use self::params::{TendermintParams, TimeoutParams}; use self::types::{Height, Step, View}; use super::stake; -use super::validator_set::ValidatorSet; use crate::client::EngineClient; use crate::codechain_machine::CodeChainMachine; use ChainNotify; @@ -62,8 +61,6 @@ pub struct Tendermint { join: Option>, quit_tendermint: crossbeam::Sender<()>, inner: crossbeam::Sender, - /// Set used to determine the current validators. - validators: Arc, /// Reward per block, in base units. block_reward: u64, /// codechain machine descriptor @@ -90,7 +87,6 @@ impl Tendermint { pub fn new(our_params: TendermintParams, machine: CodeChainMachine) -> Arc { let stake = stake::Stake::new(our_params.genesis_stakes, Arc::clone(&our_params.validators)); let timeouts = our_params.timeouts; - let validators = Arc::clone(&our_params.validators); let machine = Arc::new(machine); let (join, extension_initializer, inner, quit_tendermint) = worker::spawn(our_params.validators); @@ -104,7 +100,6 @@ impl Tendermint { join: Some(join), quit_tendermint, inner, - validators, block_reward: our_params.block_reward, machine, action_handlers, diff --git a/core/src/consensus/validator_set/mod.rs b/core/src/consensus/validator_set/mod.rs index 0ac5d0b5c7..ef016764b9 100644 --- a/core/src/consensus/validator_set/mod.rs +++ b/core/src/consensus/validator_set/mod.rs @@ -72,14 +72,6 @@ pub trait ValidatorSet: Send + Sync { Ok(Vec::new()) } - /// Whether this block is the last one in its epoch. - /// - /// Indicates that the validator set changed at the given block in a manner - /// that doesn't require finality. - /// - /// `first` is true if this is the first block in the set. - fn is_epoch_end(&self, first: bool, chain_head: &Header) -> Option>; - /// Recover the validator set from the given proof, the block number, and /// whether this header is first in its set. /// diff --git a/core/src/consensus/validator_set/null_validator.rs b/core/src/consensus/validator_set/null_validator.rs index cf667214bb..67ead530b2 100644 --- a/core/src/consensus/validator_set/null_validator.rs +++ b/core/src/consensus/validator_set/null_validator.rs @@ -22,7 +22,6 @@ use super::validator_list::ValidatorList; use super::ValidatorSet; use crate::codechain_machine::CodeChainMachine; use crate::error::Error; -use crate::header::Header; /// Validator set containing a known set of public keys. #[derive(Clone, Debug, PartialEq, Eq, Default)] @@ -57,10 +56,6 @@ impl ValidatorSet for NullValidator { unimplemented!() } - fn is_epoch_end(&self, _first: bool, _chain_head: &Header) -> Option> { - unimplemented!() - } - fn epoch_set( &self, _first: bool, diff --git a/core/src/consensus/validator_set/validator_list.rs b/core/src/consensus/validator_set/validator_list.rs index 99f9d84f23..31ca5a9d87 100644 --- a/core/src/consensus/validator_set/validator_list.rs +++ b/core/src/consensus/validator_set/validator_list.rs @@ -23,7 +23,6 @@ use primitives::H256; use super::ValidatorSet; use crate::codechain_machine::CodeChainMachine; use crate::error::Error; -use crate::header::Header; /// Validator set containing a known set of public keys. #[derive(Clone, Debug, PartialEq, Eq, Default)] @@ -95,14 +94,6 @@ impl ValidatorSet for ValidatorList { self.validators.len() } - fn is_epoch_end(&self, first: bool, _chain_head: &Header) -> Option> { - if first { - Some(Vec::new()) // allow transition to fixed list, and instantly - } else { - None - } - } - fn epoch_set( &self, _first: bool, From 0486723f0ae3618a0257f29489b5e18017dfb76a Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Thu, 2 May 2019 11:36:58 +0900 Subject: [PATCH 5/6] Remove ValidatorSet::epoch_set --- core/src/consensus/validator_set/mod.rs | 17 ----------------- .../consensus/validator_set/null_validator.rs | 13 ------------- .../consensus/validator_set/validator_list.rs | 13 ------------- 3 files changed, 43 deletions(-) diff --git a/core/src/consensus/validator_set/mod.rs b/core/src/consensus/validator_set/mod.rs index ef016764b9..18c807f2a1 100644 --- a/core/src/consensus/validator_set/mod.rs +++ b/core/src/consensus/validator_set/mod.rs @@ -22,7 +22,6 @@ use primitives::{Bytes, H256}; use self::validator_list::ValidatorList; use crate::client::EngineClient; -use crate::codechain_machine::CodeChainMachine; use crate::error::Error; use crate::header::Header; @@ -72,22 +71,6 @@ pub trait ValidatorSet: Send + Sync { Ok(Vec::new()) } - /// Recover the validator set from the given proof, the block number, and - /// whether this header is first in its set. - /// - /// May fail if the given header doesn't kick off an epoch or - /// the proof is invalid. - /// - /// Returns the set, along with a flag indicating whether finality of a specific - /// hash should be proven. - fn epoch_set( - &self, - first: bool, - machine: &CodeChainMachine, - number: BlockNumber, - proof: &[u8], - ) -> Result<(ValidatorList, Option), Error>; - /// Notifies about malicious behaviour. fn report_malicious(&self, _validator: &Address, _set_block: BlockNumber, _block: BlockNumber, _proof: Bytes) {} /// Notifies about benign misbehaviour. diff --git a/core/src/consensus/validator_set/null_validator.rs b/core/src/consensus/validator_set/null_validator.rs index 67ead530b2..0d8666947b 100644 --- a/core/src/consensus/validator_set/null_validator.rs +++ b/core/src/consensus/validator_set/null_validator.rs @@ -18,10 +18,7 @@ use ckey::{Address, Public}; use primitives::H256; -use super::validator_list::ValidatorList; use super::ValidatorSet; -use crate::codechain_machine::CodeChainMachine; -use crate::error::Error; /// Validator set containing a known set of public keys. #[derive(Clone, Debug, PartialEq, Eq, Default)] @@ -55,14 +52,4 @@ impl ValidatorSet for NullValidator { fn count(&self, _parent: &H256) -> usize { unimplemented!() } - - fn epoch_set( - &self, - _first: bool, - _machine: &CodeChainMachine, - _number: u64, - _proof: &[u8], - ) -> Result<(ValidatorList, Option), Error> { - unimplemented!() - } } diff --git a/core/src/consensus/validator_set/validator_list.rs b/core/src/consensus/validator_set/validator_list.rs index 31ca5a9d87..6ba2486e65 100644 --- a/core/src/consensus/validator_set/validator_list.rs +++ b/core/src/consensus/validator_set/validator_list.rs @@ -17,12 +17,9 @@ use std::collections::HashSet; use ckey::{public_to_address, Address, Public}; -use ctypes::BlockNumber; use primitives::H256; use super::ValidatorSet; -use crate::codechain_machine::CodeChainMachine; -use crate::error::Error; /// Validator set containing a known set of public keys. #[derive(Clone, Debug, PartialEq, Eq, Default)] @@ -93,16 +90,6 @@ impl ValidatorSet for ValidatorList { fn count(&self, _bh: &H256) -> usize { self.validators.len() } - - fn epoch_set( - &self, - _first: bool, - _: &CodeChainMachine, - _: BlockNumber, - _: &[u8], - ) -> Result<(ValidatorList, Option), Error> { - Ok((self.clone(), None)) - } } #[cfg(test)] From ccd0c231b8e9f68282c7b6940c47f9743aef8e09 Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Thu, 2 May 2019 11:44:29 +0900 Subject: [PATCH 6/6] Remove genesis_epoch_data --- core/src/consensus/mod.rs | 5 ----- core/src/consensus/simple_poa/mod.rs | 4 ---- core/src/consensus/validator_set/mod.rs | 5 ----- 3 files changed, 14 deletions(-) diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 5c00d7a44c..21b09f0b07 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -201,11 +201,6 @@ pub trait ConsensusEngine: Sync + Send { Ok(()) } - /// Genesis epoch data. - fn genesis_epoch_data(&self, _header: &M::Header) -> Result, String> { - Ok(Vec::new()) - } - /// Whether a block is the end of an epoch. /// /// This either means that an immediate transition occurs or a block signalling transition diff --git a/core/src/consensus/simple_poa/mod.rs b/core/src/consensus/simple_poa/mod.rs index 2ee258b2d6..bd24f86b12 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -118,10 +118,6 @@ impl ConsensusEngine for SimplePoA { verify_external(header, &*self.validators) } - fn genesis_epoch_data(&self, header: &Header) -> Result, String> { - self.validators.genesis_epoch_data(header) - } - fn is_epoch_end(&self, chain_head: &Header, _chain: &super::Headers
) -> Option> { let first = chain_head.number() == 0; if first { diff --git a/core/src/consensus/validator_set/mod.rs b/core/src/consensus/validator_set/mod.rs index 18c807f2a1..1cc040a715 100644 --- a/core/src/consensus/validator_set/mod.rs +++ b/core/src/consensus/validator_set/mod.rs @@ -66,11 +66,6 @@ pub trait ValidatorSet: Send + Sync { Ok(()) } - /// Extract genesis epoch data from the genesis state and header. - fn genesis_epoch_data(&self, _header: &Header) -> Result, String> { - Ok(Vec::new()) - } - /// Notifies about malicious behaviour. fn report_malicious(&self, _validator: &Address, _set_block: BlockNumber, _block: BlockNumber, _proof: Bytes) {} /// Notifies about benign misbehaviour.