From fc6b33d4b10fc456c6bcd618343aa000055e722d Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Thu, 9 May 2019 01:58:21 +0900 Subject: [PATCH 1/3] Add TermTransition --- core/src/blockchain/blockchain.rs | 11 +++++++++ core/src/blockchain/extras.rs | 37 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/core/src/blockchain/blockchain.rs b/core/src/blockchain/blockchain.rs index 360ddbf3d0..6073e400d5 100644 --- a/core/src/blockchain/blockchain.rs +++ b/core/src/blockchain/blockchain.rs @@ -29,6 +29,7 @@ use super::extras::{BlockDetails, EpochTransitions, TransactionAddress, EPOCH_KE use super::headerchain::{HeaderChain, HeaderProvider}; use super::invoice_db::{InvoiceDB, InvoiceProvider}; use super::route::{tree_route, ImportRoute}; +use crate::blockchain::extras::{TermId, TermTransition}; use crate::blockchain_info::BlockChainInfo; use crate::consensus::epoch::Transition as EpochTransition; use crate::consensus::CodeChainEngine; @@ -343,6 +344,16 @@ impl BlockChain { self.headerchain.best_proposal_header() } + pub fn insert_term_transition(&self, batch: &mut DBTransaction, block_number: u64) { + batch.write(db::COL_EXTRA, &TermId(block_number), &TermTransition { + block_number, + }); + } + + pub fn term_transition(&self, block_number: u64) -> Option { + self.db.read(db::COL_EXTRA, &TermId(block_number)) + } + /// Insert an epoch transition. Provide an epoch number being transitioned to /// and epoch transition object. /// diff --git a/core/src/blockchain/extras.rs b/core/src/blockchain/extras.rs index 8daa900eb0..e3030482c5 100644 --- a/core/src/blockchain/extras.rs +++ b/core/src/blockchain/extras.rs @@ -39,6 +39,8 @@ enum ExtrasIndex { /// Epoch transition data index. EpochTransitions = 4, // (Reserved) = 5, + /// Term transition data index + TermTransitions = 6, } fn with_index(hash: &H256, i: ExtrasIndex) -> H264 { @@ -129,6 +131,36 @@ impl Key for u64 { } } +const TERM_KEY_LEN: usize = DB_PREFIX_LEN + 16; +pub const TERM_KEY_PREFIX: &[u8; DB_PREFIX_LEN] = + &[ExtrasIndex::TermTransitions as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + +pub struct TermTransitionKey([u8; TERM_KEY_LEN]); + +impl Deref for TermTransitionKey { + type Target = [u8]; + + fn deref(&self) -> &[u8] { + &self.0[..] + } +} + +pub struct TermId(pub u64); + +impl Key for TermId { + type Target = TermTransitionKey; + + fn key(&self) -> Self::Target { + let mut key = [0u8; TERM_KEY_LEN]; + key[..DB_PREFIX_LEN].copy_from_slice(&TERM_KEY_PREFIX[..]); + + write!(&mut key[DB_PREFIX_LEN..], "{:016x}", self.0) + .expect("format arg is valid; no more than 16 chars will be written; qed"); + + TermTransitionKey(key) + } +} + /// Familial details concerning a block #[derive(Debug, Clone, RlpEncodable, RlpDecodable)] pub struct BlockDetails { @@ -168,6 +200,11 @@ pub struct EpochTransitions { pub candidates: Vec, } +#[derive(Debug, Clone, RlpEncodable, RlpDecodable)] +pub struct TermTransition { + pub block_number: u64, +} + impl TransactionAddresses { pub fn new(address: TransactionAddress) -> Self { Self { From fac6c23effb401a8cf333b9fbe2ea84f7f31fb95 Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Thu, 9 May 2019 02:59:08 +0900 Subject: [PATCH 2/3] Replace EpochTransition with TermTransition `is_epoch_begin` flag which is determined by the result of `epoch_tranistion` call doesn't make any differences whether it is true or false since `on_new_block`, `on_epoch_begin` does nothing on it. So it is safe to remove and to replace with `is_term_begin` which also does nothing for now for the further implementation. Inserted `EpochTransition` is not read anymore and inserting it does not change state root. So removing it doesn't introduce breaking change unless reusing the key index. --- core/src/block.rs | 8 +++---- core/src/client/client.rs | 4 ++-- core/src/client/importer.rs | 30 +++++++++---------------- core/src/consensus/mod.rs | 17 ++++---------- core/src/consensus/simple_poa/mod.rs | 9 -------- core/src/consensus/tendermint/engine.rs | 21 ++--------------- core/src/consensus/tendermint/worker.rs | 17 +++++--------- core/src/consensus/validator_set/mod.rs | 11 --------- 8 files changed, 28 insertions(+), 89 deletions(-) diff --git a/core/src/block.rs b/core/src/block.rs index 43a748b813..fc69f252c2 100644 --- a/core/src/block.rs +++ b/core/src/block.rs @@ -140,7 +140,7 @@ impl<'x> OpenBlock<'x> { parent: &Header, author: Address, extra_data: Bytes, - is_epoch_begin: bool, + is_term_begin: bool, ) -> Result { let number = parent.number() + 1; let state = TopLevelState::from_existing(db, *parent.state_root()).map_err(StateError::from)?; @@ -159,7 +159,7 @@ impl<'x> OpenBlock<'x> { engine.machine().populate_from_parent(&mut r.block.header, parent); engine.populate_from_parent(&mut r.block.header, parent); - engine.on_new_block(&mut r.block, is_epoch_begin)?; + engine.on_new_block(&mut r.block, is_term_begin)?; Ok(r) } @@ -451,9 +451,9 @@ pub fn enact( client: &C, db: StateDB, parent: &Header, - is_epoch_begin: bool, + is_term_begin: bool, ) -> Result { - let mut b = OpenBlock::try_new(engine, db, parent, Address::default(), vec![], is_epoch_begin)?; + let mut b = OpenBlock::try_new(engine, db, parent, Address::default(), vec![], is_term_begin)?; b.populate_from(header); b.push_transactions(transactions, client, parent.number(), parent.timestamp())?; diff --git a/core/src/client/client.rs b/core/src/client/client.rs index 2f062b2d9d..f0b890d348 100644 --- a/core/src/client/client.rs +++ b/core/src/client/client.rs @@ -766,14 +766,14 @@ impl PrepareOpenBlock for Client { let parent_hash = self.block_hash(&parent_block_id).expect("parent exist always"); let parent_header = chain.block_header(&parent_hash).expect("parent exist always"); - let is_epoch_begin = chain.epoch_transition(parent_header.number(), parent_hash).is_some(); + let is_term_begin = chain.term_transition(parent_header.number()).is_some(); OpenBlock::try_new( engine, self.state_db.read().clone(&parent_header.state_root()), &parent_header, author, extra_data, - is_epoch_begin, + is_term_begin, ).expect("OpenBlock::new only fails if parent state root invalid; state root of best block's header is never invalid; qed") } } diff --git a/core/src/client/importer.rs b/core/src/client/importer.rs index df1322c006..33b3064997 100644 --- a/core/src/client/importer.rs +++ b/core/src/client/importer.rs @@ -29,7 +29,6 @@ use super::BlockInfo; use super::{Client, ClientConfig}; use crate::block::{enact, IsBlock, LockedBlock}; use crate::blockchain::{BlockChain, BodyProvider, HeaderProvider, ImportRoute}; -use crate::consensus::epoch::Transition as EpochTransition; use crate::consensus::CodeChainEngine; use crate::encoded; use crate::error::Error; @@ -217,7 +216,7 @@ impl Importer { client.db().write_buffered(batch); chain.commit(); - self.check_epoch_end(block.header(), &chain, client); + self.check_term_end(block.header(), &chain, client); if hash == chain.best_block_hash() { let mut state_db = client.state_db().write(); @@ -228,21 +227,16 @@ impl Importer { route } - // 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))); - - if let Some(proof) = is_epoch_end { - cdebug!(CLIENT, "Epoch transition at block {}", header.hash()); + // check for ending of term and write transition if it occurs. + fn check_term_end(&self, header: &Header, chain: &BlockChain, client: &Client) { + let is_term_end = self.engine.is_term_end(header); + if is_term_end { + cdebug!(CLIENT, "Term transition at block {}", header.hash()); let mut batch = DBTransaction::new(); - chain.insert_epoch_transition(&mut batch, header.number(), EpochTransition { - block_hash: header.hash(), - block_number: header.number(), - proof, - }); + chain.insert_term_transition(&mut batch, header.number()); - // always write the batch directly since epoch transition proofs are + // always write the batch directly since term transitions are // fetched from a DB iterator and DB iterators are only available on // flushed data. client.db().write(batch).expect("DB flush failed"); @@ -314,8 +308,8 @@ impl Importer { // Enact Verified Block let db = client.state_db().read().clone(&parent.state_root()); - let is_epoch_begin = chain.epoch_transition(parent.number(), *header.parent_hash()).is_some(); - let enact_result = enact(&block.header, &block.transactions, engine, client, db, &parent, is_epoch_begin); + let is_term_begin = chain.term_transition(parent.number()).is_some(); + let enact_result = enact(&block.header, &block.transactions, engine, client, db, &parent, is_term_begin); let locked_block = enact_result.map_err(|e| { cwarn!(CLIENT, "Block import failed for #{} ({})\nError: {:?}", header.number(), header.hash(), e); })?; @@ -437,14 +431,12 @@ impl Importer { let chain = client.block_chain(); let mut batch = DBTransaction::new(); - // FIXME: Check if this line is still necessary. - // self.check_epoch_end_signal(header, &chain, &mut batch); let route = chain.insert_header(&mut batch, &HeaderView::new(&header.rlp_bytes()), self.engine.borrow()); client.db().write_buffered(batch); chain.commit(); // FIXME: Check if this line is still necessary. - // self.check_epoch_end(&header, &chain, client); + // self.check_term_end(&header, &chain, client); route } diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 21b09f0b07..0f6aa2e733 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -201,15 +201,9 @@ pub trait ConsensusEngine: Sync + Send { Ok(()) } - /// Whether a block is the end of an epoch. - /// - /// This either means that an immediate transition occurs or a block signalling transition - /// has reached finality. The `Headers` given are not guaranteed to return any blocks - /// from any epoch other than the current. - /// - /// Return optional transition proof. - fn is_epoch_end(&self, _chain_head: &M::Header, _chain: &Headers) -> Option> { - None + /// Whether a block is the end of an term. + fn is_term_end(&self, _chain_head: &M::Header) -> bool { + false } /// Populate a header's fields based on its parent's header. @@ -223,7 +217,7 @@ pub trait ConsensusEngine: Sync + Send { fn stop(&self) {} /// Block transformation functions, before the transactions. - fn on_new_block(&self, _block: &mut M::LiveBlock, _epoch_begin: bool) -> Result<(), M::Error> { + fn on_new_block(&self, _block: &mut M::LiveBlock, _term_begin: bool) -> Result<(), M::Error> { Ok(()) } @@ -287,9 +281,6 @@ pub trait ConsensusEngine: Sync + Send { } } -/// Type alias for a function we can get headers by hash through. -pub type Headers<'a, H> = 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 bd24f86b12..a00db54386 100644 --- a/core/src/consensus/simple_poa/mod.rs +++ b/core/src/consensus/simple_poa/mod.rs @@ -118,15 +118,6 @@ impl ConsensusEngine for SimplePoA { verify_external(header, &*self.validators) } - fn is_epoch_end(&self, chain_head: &Header, _chain: &super::Headers
) -> Option> { - let first = chain_head.number() == 0; - if first { - Some(Vec::new()) - } else { - None - } - } - 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 6cef2089fe..8433001904 100644 --- a/core/src/consensus/tendermint/engine.rs +++ b/core/src/consensus/tendermint/engine.rs @@ -24,7 +24,6 @@ use crossbeam_channel as crossbeam; use cstate::ActionHandler; use ctypes::machine::WithBalances; use ctypes::transaction::Action; -use ctypes::BlockNumber; use primitives::H256; use super::super::stake; @@ -115,16 +114,6 @@ impl ConsensusEngine for Tendermint { receiver.recv().unwrap() } - fn is_epoch_end(&self, chain_head: &Header, _chain: &super::super::Headers
) -> Option> { - let first = chain_head.number() == 0; - if first { - let change = combine_proofs(chain_head.number(), &[], &[]); - return Some(change) - } - - None - } - fn populate_from_parent(&self, header: &mut Header, _parent: &Header) { let (result, receiver) = crossbeam::bounded(1); self.inner @@ -144,12 +133,12 @@ impl ConsensusEngine for Tendermint { fn stop(&self) {} - fn on_new_block(&self, block: &mut ExecutedBlock, epoch_begin: bool) -> Result<(), Error> { + fn on_new_block(&self, block: &mut ExecutedBlock, term_begin: bool) -> Result<(), Error> { let (result, receiver) = crossbeam::bounded(1); self.inner .send(worker::Event::OnNewBlock { header: Box::from(block.header().clone()), - epoch_begin, + term_begin, result, }) .unwrap(); @@ -313,9 +302,3 @@ impl Tendermint { } } } - -fn combine_proofs(signal_number: BlockNumber, set_proof: &[u8], finality_proof: &[u8]) -> Vec { - let mut stream = ::rlp::RlpStream::new_list(3); - stream.append(&signal_number).append(&set_proof).append(&finality_proof); - stream.out() -} diff --git a/core/src/consensus/tendermint/worker.rs b/core/src/consensus/tendermint/worker.rs index 4a47fdf27c..1e2109ad2f 100644 --- a/core/src/consensus/tendermint/worker.rs +++ b/core/src/consensus/tendermint/worker.rs @@ -116,7 +116,7 @@ pub enum Event { OnTimeout(usize), OnNewBlock { header: Box
, - epoch_begin: bool, + term_begin: bool, result: crossbeam::Sender>, }, HandleMessages { @@ -253,10 +253,10 @@ impl Worker { } Ok(Event::OnNewBlock { header, - epoch_begin, + term_begin, result, }) => { - result.send(inner.on_new_block(&header, epoch_begin)).unwrap(); + result.send(inner.on_new_block(&header, term_begin)).unwrap(); } Ok(Event::HandleMessages { messages, @@ -1220,15 +1220,8 @@ impl Worker { nonce < self.timeout_token_nonce } - fn on_new_block(&self, header: &Header, epoch_begin: bool) -> Result<(), Error> { - if !epoch_begin { - return Ok(()) - } - - // genesis is never a new block, but might as well check. - let first = header.number() == 0; - - self.validators.on_epoch_begin(first, header) + fn on_new_block(&self, _header: &Header, _term_begin: bool) -> Result<(), Error> { + Ok(()) } fn handle_message(&mut self, rlp: &[u8], is_restoring: bool) -> Result<(), EngineError> { diff --git a/core/src/consensus/validator_set/mod.rs b/core/src/consensus/validator_set/mod.rs index 1cc040a715..623baf8024 100644 --- a/core/src/consensus/validator_set/mod.rs +++ b/core/src/consensus/validator_set/mod.rs @@ -22,8 +22,6 @@ use primitives::{Bytes, H256}; use self::validator_list::ValidatorList; use crate::client::EngineClient; -use crate::error::Error; -use crate::header::Header; pub mod null_validator; pub mod validator_list; @@ -57,15 +55,6 @@ pub trait ValidatorSet: Send + Sync { /// Returns the current number of validators. fn count(&self, parent: &H256) -> usize; - /// Signalling that a new epoch has begun. - /// - /// The caller provided here may not generate proofs. - /// - /// `first` is true if this is the first block in the set. - fn on_epoch_begin(&self, _first: bool, _header: &Header) -> Result<(), Error> { - Ok(()) - } - /// Notifies about malicious behaviour. fn report_malicious(&self, _validator: &Address, _set_block: BlockNumber, _block: BlockNumber, _proof: Bytes) {} /// Notifies about benign misbehaviour. From 81d7d0c04f31023dfc7cc4a3b65c7cf5531c9d16 Mon Sep 17 00:00:00 2001 From: SeongChan Lee Date: Tue, 7 May 2019 12:01:54 +0900 Subject: [PATCH 3/3] Remove unused EpochTransition `EpochTransition` is not used anymore after being replaced by `TermTransition` --- core/src/blockchain/blockchain.rs | 146 +----------------------------- core/src/blockchain/extras.rs | 45 +-------- core/src/consensus/epoch.rs | 52 ----------- core/src/consensus/mod.rs | 1 - 4 files changed, 3 insertions(+), 241 deletions(-) delete mode 100644 core/src/consensus/epoch.rs diff --git a/core/src/blockchain/blockchain.rs b/core/src/blockchain/blockchain.rs index 6073e400d5..3172a4bf7d 100644 --- a/core/src/blockchain/blockchain.rs +++ b/core/src/blockchain/blockchain.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use std::mem; use std::sync::Arc; use ctypes::BlockNumber; @@ -25,13 +24,12 @@ use rlp::RlpStream; use super::block_info::BestBlockChanged; use super::body_db::{BodyDB, BodyProvider}; -use super::extras::{BlockDetails, EpochTransitions, TransactionAddress, EPOCH_KEY_PREFIX}; +use super::extras::{BlockDetails, TransactionAddress}; use super::headerchain::{HeaderChain, HeaderProvider}; use super::invoice_db::{InvoiceDB, InvoiceProvider}; use super::route::{tree_route, ImportRoute}; use crate::blockchain::extras::{TermId, TermTransition}; use crate::blockchain_info::BlockChainInfo; -use crate::consensus::epoch::Transition as EpochTransition; use crate::consensus::CodeChainEngine; use crate::db::{self, Readable, Writable}; use crate::encoded; @@ -353,148 +351,6 @@ impl BlockChain { pub fn term_transition(&self, block_number: u64) -> Option { self.db.read(db::COL_EXTRA, &TermId(block_number)) } - - /// Insert an epoch transition. Provide an epoch number being transitioned to - /// and epoch transition object. - /// - /// The block the transition occurred at should have already been inserted into the chain. - pub fn insert_epoch_transition(&self, batch: &mut DBTransaction, epoch_num: u64, transition: EpochTransition) { - let mut transitions = match self.db.read(db::COL_EXTRA, &epoch_num) { - Some(existing) => existing, - None => EpochTransitions { - number: epoch_num, - candidates: Vec::with_capacity(1), - }, - }; - - // ensure we don't write any duplicates. - if transitions.candidates.iter().find(|c| c.block_hash == transition.block_hash).is_none() { - transitions.candidates.push(transition); - batch.write(db::COL_EXTRA, &epoch_num, &transitions); - } - } - - /// Iterate over all epoch transitions. - /// This will only return transitions within the canonical chain. - #[allow(dead_code)] - pub fn epoch_transitions(&self) -> EpochTransitionIter { - let iter = self.db.iter_from_prefix(db::COL_EXTRA, &EPOCH_KEY_PREFIX[..]); - EpochTransitionIter { - chain: self, - prefix_iter: iter, - } - } - - /// Get a specific epoch transition by block number and provided block hash. - pub fn epoch_transition(&self, block_num: u64, block_hash: H256) -> Option { - ctrace!(BLOCKCHAIN, "Loading epoch transition at block {}, {}", block_num, block_hash); - - self.db.read(db::COL_EXTRA, &block_num).and_then(|transitions: EpochTransitions| { - transitions.candidates.into_iter().find(|c| c.block_hash == block_hash) - }) - } - - /// Get the transition to the epoch the given parent hash is part of - /// or transitions to. - /// This will give the epoch that any children of this parent belong to. - /// - /// The block corresponding the the parent hash must be stored already. - #[allow(dead_code)] - pub fn epoch_transition_for(&self, parent_hash: H256) -> Option { - // slow path: loop back block by block - #[allow(clippy::identity_conversion)] - // This is a false alarm. https://github.com/rust-lang/rust-clippy/issues/3944 - for hash in self.ancestry_iter(parent_hash)? { - let details = self.block_details(&hash)?; - - // look for transition in database. - if let Some(transition) = self.epoch_transition(details.number, hash) { - return Some(transition) - } - - // canonical hash -> fast breakout: - // get the last epoch transition up to this block. - // - // if `block_hash` is canonical it will only return transitions up to - // the parent. - if self.block_hash(details.number)? == hash { - return self.epoch_transitions().map(|(_, t)| t).take_while(|t| t.block_number <= details.number).last() - } - } - - // should never happen as the loop will encounter genesis before concluding. - None - } - - /// Iterator that lists `first` and then all of `first`'s ancestors, by hash. - #[allow(dead_code)] - pub fn ancestry_iter(&self, first: H256) -> Option { - if self.is_known(&first) { - Some(AncestryIter { - current: first, - chain: self, - }) - } else { - None - } - } -} - -/// An iterator which walks the blockchain towards the genesis. -#[derive(Clone)] -pub struct AncestryIter<'a> { - current: H256, - chain: &'a BlockChain, -} - -impl<'a> Iterator for AncestryIter<'a> { - type Item = H256; - fn next(&mut self) -> Option { - if self.current.is_zero() { - None - } else { - self.chain.block_details(&self.current).map(|details| mem::replace(&mut self.current, details.parent)) - } - } -} - -type TransitionIterInternal<'a> = Box, Box<[u8]>)> + 'a>; -/// An iterator which walks all epoch transitions. -/// Returns epoch transitions. -#[allow(dead_code)] -pub struct EpochTransitionIter<'a> { - chain: &'a BlockChain, - prefix_iter: TransitionIterInternal<'a>, -} - -impl<'a> Iterator for EpochTransitionIter<'a> { - type Item = (u64, EpochTransition); - - fn next(&mut self) -> Option { - loop { - // some epochs never occurred on the main chain. - let (key, val) = self.prefix_iter.next()?; - - // iterator may continue beyond values beginning with this - // prefix. - if !key.starts_with(&EPOCH_KEY_PREFIX[..]) { - return None - } - - let transitions: EpochTransitions = ::rlp::decode(&val[..]); - - // if there are multiple candidates, at most one will be on the - // canon chain. - for transition in transitions.candidates.into_iter() { - let is_in_canon_chain = - self.chain.block_hash(transition.block_number).map_or(false, |hash| hash == transition.block_hash); - - if is_in_canon_chain { - return Some((transitions.number, transition)) - } - } - } - } } /// Interface for querying blocks by hash and by number. diff --git a/core/src/blockchain/extras.rs b/core/src/blockchain/extras.rs index e3030482c5..97cc9e8e1c 100644 --- a/core/src/blockchain/extras.rs +++ b/core/src/blockchain/extras.rs @@ -15,13 +15,12 @@ // along with this program. If not, see . use std::io::Write; -use std::ops::{self, Add, AddAssign, Deref, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Deref, Sub, SubAssign}; use ctypes::BlockNumber; use kvdb::PREFIX_LEN as DB_PREFIX_LEN; use primitives::{H256, H264, U256}; -use crate::consensus::epoch::Transition as EpochTransition; use crate::db::Key; use crate::types::TransactionId; @@ -36,8 +35,7 @@ enum ExtrasIndex { ParcelAddress = 2, /// Transaction address index TransactionAddress = 3, - /// Epoch transition data index. - EpochTransitions = 4, + // (Reserved) = 4, // (Reserved) = 5, /// Term transition data index TermTransitions = 6, @@ -99,38 +97,6 @@ impl Key for H256 { } } -/// length of epoch keys. -const EPOCH_KEY_LEN: usize = DB_PREFIX_LEN + 16; - -/// epoch key prefix. -/// used to iterate over all epoch transitions in order from genesis. -pub const EPOCH_KEY_PREFIX: &[u8; DB_PREFIX_LEN] = - &[ExtrasIndex::EpochTransitions as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - -pub struct EpochTransitionsKey([u8; EPOCH_KEY_LEN]); - -impl ops::Deref for EpochTransitionsKey { - type Target = [u8]; - - fn deref(&self) -> &[u8] { - &self.0[..] - } -} - -impl Key for u64 { - type Target = EpochTransitionsKey; - - fn key(&self) -> Self::Target { - let mut arr = [0u8; EPOCH_KEY_LEN]; - arr[..DB_PREFIX_LEN].copy_from_slice(&EPOCH_KEY_PREFIX[..]); - - write!(&mut arr[DB_PREFIX_LEN..], "{:016x}", self) - .expect("format arg is valid; no more than 16 chars will be written; qed"); - - EpochTransitionsKey(arr) - } -} - const TERM_KEY_LEN: usize = DB_PREFIX_LEN + 16; pub const TERM_KEY_PREFIX: &[u8; DB_PREFIX_LEN] = &[ExtrasIndex::TermTransitions as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; @@ -193,13 +159,6 @@ pub struct TransactionAddresses { addresses: Vec, } -/// Candidate transitions to an epoch with specific number. -#[derive(Clone, RlpEncodable, RlpDecodable)] -pub struct EpochTransitions { - pub number: u64, - pub candidates: Vec, -} - #[derive(Debug, Clone, RlpEncodable, RlpDecodable)] pub struct TermTransition { pub block_number: u64, diff --git a/core/src/consensus/epoch.rs b/core/src/consensus/epoch.rs deleted file mode 100644 index f891df20e8..0000000000 --- a/core/src/consensus/epoch.rs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2018 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 primitives::H256; -use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp}; - -/// A full epoch transition. -#[derive(Debug, Clone)] -pub struct Transition { - /// Block hash at which the transition occurred. - pub block_hash: H256, - /// Block number at which the transition occurred. - pub block_number: u64, - /// "transition/epoch" proof from the engine combined with a finality proof. - pub proof: Vec, -} - -impl Encodable for Transition { - fn rlp_append(&self, s: &mut RlpStream) { - s.begin_list(3).append(&self.block_hash).append(&self.block_number).append(&self.proof); - } -} - -impl Decodable for Transition { - fn decode(rlp: &UntrustedRlp) -> Result { - let item_count = rlp.item_count()?; - if item_count != 3 { - return Err(DecoderError::RlpInvalidLength { - expected: 3, - got: item_count, - }) - } - Ok(Transition { - block_hash: rlp.val_at(0)?, - block_number: rlp.val_at(1)?, - proof: rlp.val_at(2)?, - }) - } -} diff --git a/core/src/consensus/mod.rs b/core/src/consensus/mod.rs index 0f6aa2e733..515501e176 100644 --- a/core/src/consensus/mod.rs +++ b/core/src/consensus/mod.rs @@ -16,7 +16,6 @@ mod blake_pow; mod cuckoo; -pub mod epoch; mod null_engine; mod signer; mod simple_poa;