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
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ccore = { package = "codechain-core", path = "core" }
cdiscovery = { package = "codechain-discovery", path = "discovery" }
codechain-logger = { path = "util/logger" }
ckey = { package = "codechain-key", path = "key" }
cstate = { package = "codechain-state", path = "state" }
ctypes = { package = "codechain-types", path = "types" }
ckeystore = { package = "codechain-keystore", path = "keystore" }
cnetwork = { package = "codechain-network", path = "network" }
Expand Down
11 changes: 5 additions & 6 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ use super::invoice::Invoice;
use crate::client::{EngineInfo, TermInfo};
use crate::consensus::CodeChainEngine;
use crate::error::{BlockError, Error};
use crate::stake;
use crate::transaction::{UnverifiedTransaction, VerifiedTransaction};
use ccrypto::BLAKE_NULL_RLP;
use ckey::Address;
use cstate::{FindStakeHandler, StateDB, StateError, StateWithCache, TopLevelState};
use cstate::{FindDoubleVoteHandler, NextValidators, StateDB, StateError, StateWithCache, TopLevelState};
use ctypes::errors::HistoryError;
use ctypes::header::{Header, Seal};
use ctypes::util::unexpected::Mismatch;
Expand Down Expand Up @@ -147,7 +146,7 @@ impl<'x> OpenBlock<'x> {
}

/// Push a transaction into the block.
pub fn push_transaction<C: FindStakeHandler>(
pub fn push_transaction<C: FindDoubleVoteHandler>(
&mut self,
tx: VerifiedTransaction,
h: Option<TxHash>,
Expand Down Expand Up @@ -188,7 +187,7 @@ impl<'x> OpenBlock<'x> {
}

/// Push transactions onto the block.
pub fn push_transactions<C: FindStakeHandler>(
pub fn push_transactions<C: FindDoubleVoteHandler>(
&mut self,
transactions: &[VerifiedTransaction],
client: &C,
Expand Down Expand Up @@ -226,7 +225,7 @@ impl<'x> OpenBlock<'x> {
It doesn't cause a direct error since we use unwrap_or_default() here, but should be aware of such problem.
Remove this comment after we completely omit static-validator-set-mode.
*/
let vset_raw = stake::NextValidators::load_from_state(self.block.state())?;
let vset_raw = NextValidators::load_from_state(self.block.state())?;
let vset = vset_raw.create_compact_validator_set();
self.block.header.set_next_validator_set_hash(vset.hash());

Expand Down Expand Up @@ -352,7 +351,7 @@ impl<'x> IsBlock for ClosedBlock {
}

/// Enact the block given by block header, transactions and uncles
pub fn enact<C: EngineInfo + FindStakeHandler + TermInfo>(
pub fn enact<C: EngineInfo + FindDoubleVoteHandler + TermInfo>(
header: &Header,
transactions: &[VerifiedTransaction],
engine: &dyn CodeChainEngine,
Expand Down
6 changes: 3 additions & 3 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::MemPoolMinFees;
use cdb::{new_journaldb, Algorithm, AsHashDB};
use cio::IoChannel;
use ckey::{Address, NetworkId, PlatformAddress};
use cstate::{FindStakeHandler, StakeHandler, StateDB, StateResult, TopLevelState, TopStateView};
use cstate::{DoubleVoteHandler, FindDoubleVoteHandler, StateDB, StateResult, TopLevelState, TopStateView};
use ctimer::{TimeoutHandler, TimerApi, TimerScheduleError, TimerToken};
use ctypes::header::Header;
use ctypes::transaction::ShardTransaction;
Expand Down Expand Up @@ -705,8 +705,8 @@ impl MiningBlockChainClient for Client {
}
}

impl FindStakeHandler for Client {
fn stake_handler(&self) -> Option<&dyn StakeHandler> {
impl FindDoubleVoteHandler for Client {
fn double_vote_handler(&self) -> Option<&dyn DoubleVoteHandler> {
self.engine.stake_handler()
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::transaction::{LocalizedTransaction, PendingVerifiedTransactions, Veri
use crate::types::{BlockId, BlockStatus, TransactionId, VerificationQueueInfo as BlockQueueInfo};
use cdb::DatabaseError;
use ckey::{Address, NetworkId, PlatformAddress};
use cstate::{FindStakeHandler, StateResult, TopLevelState, TopStateView};
use cstate::{FindDoubleVoteHandler, StateResult, TopLevelState, TopStateView};
use ctypes::header::Header;
use ctypes::transaction::ShardTransaction;
use ctypes::{BlockHash, BlockNumber, CommonParams, ShardId, TxHash};
Expand Down Expand Up @@ -247,7 +247,7 @@ pub trait BlockProducer {
}

/// Extended client interface used for mining
pub trait MiningBlockChainClient: BlockChainClient + BlockProducer + FindStakeHandler {
pub trait MiningBlockChainClient: BlockChainClient + BlockProducer + FindDoubleVoteHandler {
/// Returns malicious users who sent failing transactions.
fn get_malicious_users(&self) -> Vec<Address>;

Expand Down
5 changes: 2 additions & 3 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use crate::client::{
AccountData, BlockChainClient, BlockChainTrait, BlockProducer, BlockStatus, ConsensusClient, EngineInfo,
ImportBlock, ImportResult, MiningBlockChainClient, StateInfo, StateOrBlock, TermInfo,
};
use crate::consensus::stake::{NextValidators, Validator};
use crate::consensus::EngineError;
use crate::db::{COL_STATE, NUM_COLUMNS};
use crate::encoded;
Expand All @@ -51,7 +50,7 @@ use ckey::{
Generator, KeyPairTrait, NetworkId, PlatformAddress, Random,
};
use cstate::tests::helpers::empty_top_state_with_metadata;
use cstate::{FindStakeHandler, StateDB, TopLevelState};
use cstate::{FindDoubleVoteHandler, NextValidators, StateDB, TopLevelState, Validator};
use ctimer::{TimeoutHandler, TimerToken};
use ctypes::header::Header;
use ctypes::transaction::{Action, Transaction};
Expand Down Expand Up @@ -578,7 +577,7 @@ impl TimeoutHandler for TestBlockChainClient {
fn on_timeout(&self, _token: TimerToken) {}
}

impl FindStakeHandler for TestBlockChainClient {}
impl FindDoubleVoteHandler for TestBlockChainClient {}

impl super::EngineClient for TestBlockChainClient {
fn update_sealing(&self, parent_block: BlockId, allow_empty_block: bool) {
Expand Down
18 changes: 14 additions & 4 deletions core/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ use crate::views::HeaderView;
use crate::Client;
use ckey::{Address, Signature};
use cnetwork::NetworkService;
use cstate::{StakeHandler, StateDB, StateResult};
use cstate::{DoubleVoteHandler, StateDB, StateResult};
use ctypes::errors::SyntaxError;
use ctypes::transaction::Action;
use ctypes::transaction::{Action, StakeAction};
use ctypes::util::unexpected::{Mismatch, OutOfBounds};
use ctypes::{BlockHash, CommonParams, Header};
use primitives::{Bytes, H256};
Expand Down Expand Up @@ -233,7 +233,7 @@ pub trait ConsensusEngine: Sync + Send {
true
}

fn stake_handler(&self) -> Option<&dyn StakeHandler> {
fn stake_handler(&self) -> Option<&dyn DoubleVoteHandler> {
None
}

Expand Down Expand Up @@ -327,9 +327,19 @@ pub trait CodeChainEngine: ConsensusEngine {
..
} = &tx.transaction().action
{
let action = rlp::decode(bytes).map_err(|err| SyntaxError::InvalidCustomAction(err.to_string()))?;

let handler =
self.stake_handler().ok_or_else(|| SyntaxError::InvalidCustomAction("no valid handler".to_string()))?;
handler.verify(bytes, common_params)?;
if let StakeAction::ReportDoubleVote {
message1,
message2,
} = &action
{
handler.verify(message1, message2)?;
}

action.verify(common_params)?;
}
self.machine().verify_transaction_with_params(tx, common_params)
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/consensus/solo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::codechain_machine::CodeChainMachine;
use crate::consensus::{EngineError, EngineType};
use crate::error::Error;
use ckey::Address;
use cstate::{StakeHandler, StateDB, StateResult, StateWithCache, TopLevelState};
use cstate::{init_stake, DoubleVoteHandler, StateDB, StateResult, StateWithCache, TopLevelState};
use ctypes::{BlockHash, Header};
use parking_lot::RwLock;
use primitives::H256;
Expand Down Expand Up @@ -126,7 +126,7 @@ impl ConsensusEngine for Solo {
}
}

fn stake_handler(&self) -> Option<&dyn StakeHandler> {
fn stake_handler(&self) -> Option<&dyn DoubleVoteHandler> {
Some(&self.stake)
}

Expand All @@ -136,7 +136,7 @@ impl ConsensusEngine for Solo {

fn initialize_genesis_state(&self, db: StateDB, root: H256) -> StateResult<(StateDB, H256)> {
let mut top_level = TopLevelState::from_existing(db, root)?;
stake::init(&mut top_level, self.genesis_stakes.clone(), Default::default(), Default::default())?;
init_stake(&mut top_level, self.genesis_stakes.clone(), Default::default(), Default::default())?;
Ok(top_level.commit_and_into_db()?)
}
}
Expand Down
Loading