Skip to content
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
17 changes: 5 additions & 12 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ impl ExecutedBlock {
}

/// Block that is ready for transactions to be added.
pub struct OpenBlock<'x> {
pub struct OpenBlock {
block: ExecutedBlock,
engine: &'x dyn CodeChainEngine,
}

impl<'x> OpenBlock<'x> {
impl OpenBlock {
/// Create a new `OpenBlock` ready for transaction pushing.
pub fn try_new(
engine: &'x dyn CodeChainEngine,
engine: &dyn CodeChainEngine,
db: StateDB,
parent: &Header,
author: Address,
Expand All @@ -133,7 +132,6 @@ impl<'x> OpenBlock<'x> {
let state = TopLevelState::from_existing(db, *parent.state_root()).map_err(StateError::from)?;
let mut r = OpenBlock {
block: ExecutedBlock::new(state, parent),
engine,
};

r.block.header.set_author(author);
Expand Down Expand Up @@ -208,10 +206,6 @@ impl<'x> OpenBlock<'x> {

/// Turn this into a `ClosedBlock`.
pub fn close(mut self) -> Result<ClosedBlock, Error> {
if let Err(e) = self.engine.on_close_block(&mut self.block) {
warn!("Encountered error on closing the block: {}", e);
return Err(e)
}
let state_root = self.block.state.commit().map_err(|e| {
warn!("Encountered error on state commit: {}", e);
e
Expand Down Expand Up @@ -324,13 +318,13 @@ impl IsBlock for ExecutedBlock {
}
}

impl<'x> IsBlock for OpenBlock<'x> {
impl IsBlock for OpenBlock {
fn block(&self) -> &ExecutedBlock {
&self.block
}
}

impl<'x> IsBlock for ClosedBlock {
impl IsBlock for ClosedBlock {
fn block(&self) -> &ExecutedBlock {
&self.block
}
Expand All @@ -348,7 +342,6 @@ pub fn enact<C: EngineInfo + FindDoubleVoteHandler + TermInfo>(
let mut b = OpenBlock::try_new(engine, db, parent, Address::default(), vec![])?;

b.populate_from(header);
engine.on_open_block(b.inner_mut())?;
b.push_transactions(transactions, client, parent.number(), parent.timestamp())?;

b.close()
Expand Down
2 changes: 1 addition & 1 deletion core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl Shard for Client {
}

impl BlockProducer for Client {
fn prepare_open_block(&self, parent_block_id: BlockId, author: Address, extra_data: Bytes) -> OpenBlock<'_> {
fn prepare_open_block(&self, parent_block_id: BlockId, author: Address, extra_data: Bytes) -> OpenBlock {
let engine = &*self.engine;
let chain = self.block_chain();
let parent_hash = self.block_hash(&parent_block_id).expect("parent exist always");
Expand Down
2 changes: 1 addition & 1 deletion core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub type ImportResult = Result<BlockHash, DatabaseError>;
/// Provides methods used for sealing new state
pub trait BlockProducer {
/// Returns OpenBlock prepared for closing.
fn prepare_open_block(&self, parent_block: BlockId, author: Address, extra_data: Bytes) -> OpenBlock<'_>;
fn prepare_open_block(&self, parent_block: BlockId, author: Address, extra_data: Bytes) -> OpenBlock;
}

/// Extended client interface used for mining
Expand Down
13 changes: 6 additions & 7 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ use ckey::{
Generator, KeyPairTrait, NetworkId, PlatformAddress, Random,
};
use cstate::tests::helpers::empty_top_state_with_metadata;
use cstate::{FindDoubleVoteHandler, NextValidators, StateDB, TopLevelState, Validator};
use cstate::{FindDoubleVoteHandler, NextValidators, StateDB, TopLevelState};
use ctimer::{TimeoutHandler, TimerToken};
use ctypes::header::Header;
use ctypes::transaction::{Action, Transaction};
use ctypes::transaction::{Action, Transaction, Validator};
use ctypes::{BlockHash, BlockNumber, CommonParams, Header as BlockHeader, TxHash};
use kvdb::KeyValueDB;
use merkle_trie::skewed_merkle_root;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl TestBlockChainClient {
history: RwLock::new(None),
term_id: Some(1),
validator_keys: RwLock::new(HashMap::new()),
validators: NextValidators::from_vector_to_test(vec![]),
validators: vec![].into(),
};

// insert genesis hash.
Expand Down Expand Up @@ -310,9 +310,8 @@ impl TestBlockChainClient {
self.validator_keys.write().insert(public, private);
pubkeys.push(public);
}
let fixed_validators: NextValidators = NextValidators::from_vector_to_test(
pubkeys.into_iter().map(|pubkey| Validator::new_for_test(0, 0, pubkey)).collect(),
);
let fixed_validators: NextValidators =
pubkeys.into_iter().map(|pubkey| Validator::new(0, 0, pubkey)).collect::<Vec<_>>().into();

self.validators = fixed_validators;
}
Expand All @@ -329,7 +328,7 @@ pub fn get_temp_state_db() -> StateDB {
}

impl BlockProducer for TestBlockChainClient {
fn prepare_open_block(&self, _parent_block: BlockId, author: Address, extra_data: Bytes) -> OpenBlock<'_> {
fn prepare_open_block(&self, _parent_block: BlockId, author: Address, extra_data: Bytes) -> OpenBlock {
let engine = &*self.scheme.engine;
let genesis_header = self.scheme.genesis_header();
let db = get_temp_state_db();
Expand Down
8 changes: 4 additions & 4 deletions core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ pub trait ConsensusEngine: Sync + Send {
fn on_timeout(&self, _token: usize) {}

/// Block transformation functions, before the transactions.
fn on_open_block(&self, _block: &mut ExecutedBlock) -> Result<(), Error> {
Ok(())
fn open_block_action(&self, _block: &ExecutedBlock) -> Result<Option<Action>, Error> {
Ok(None)
}

/// Block transformation functions, after the transactions.
fn on_close_block(&self, _block: &mut ExecutedBlock) -> Result<(), Error> {
Ok(())
fn close_block_actions(&self, _block: &ExecutedBlock) -> Result<Vec<Action>, Error> {
Ok(vec![])
}

/// Add Client which can be used for sealing, potentially querying the state and sending messages.
Expand Down
31 changes: 17 additions & 14 deletions core/src/consensus/solo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::consensus::{EngineError, EngineType};
use crate::error::Error;
use ckey::Address;
use cstate::{init_stake, DoubleVoteHandler, StateDB, StateResult, StateWithCache, TopLevelState};
use ctypes::transaction::Action;
use ctypes::{BlockHash, Header};
use parking_lot::RwLock;
use primitives::H256;
Expand Down Expand Up @@ -71,28 +72,30 @@ impl ConsensusEngine for Solo {
Seal::Solo
}

fn on_close_block(&self, block: &mut ExecutedBlock) -> Result<(), Error> {
fn close_block_actions(&self, block: &ExecutedBlock) -> Result<Vec<Action>, Error> {
let client = self.client().ok_or(EngineError::CannotOpenBlock)?;

let parent_hash = *block.header().parent_hash();
let parent = client.block_header(&parent_hash.into()).expect("Parent header must exist");
let parent_common_params = client.common_params(parent_hash.into()).expect("CommonParams of parent must exist");
let term_seconds = parent_common_params.term_seconds();
if term_seconds == 0 {
return Ok(())
return Ok(vec![])
}
let last_term_finished_block_num = {
let header = block.header();
let current_term_period = header.timestamp() / term_seconds;
let parent_term_period = parent.timestamp() / term_seconds;
if current_term_period == parent_term_period {
return Ok(())
}
header.number()
};

stake::on_term_close(block.state_mut(), last_term_finished_block_num, &[])?;
Ok(())
let header = block.header();
let current_term_period = header.timestamp() / term_seconds;
let parent_term_period = parent.timestamp() / term_seconds;
if current_term_period == parent_term_period {
return Ok(vec![])
}

Ok(vec![Action::CloseTerm {
next_validators: vec![],
inactive_validators: vec![],
released_addresses: vec![],
custody_until: 0,
kick_at: 0,
}])
}

fn register_client(&self, client: Weak<dyn ConsensusClient>) {
Expand Down
Loading