diff --git a/Cargo.lock b/Cargo.lock index 36515c32c4..84efdd5fe5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -382,12 +382,14 @@ dependencies = [ "avl-merkle 0.1.0", "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ckb-chain-spec 0.1.0", "ckb-core 0.1.0", "ckb-db 0.1.0", "ckb-notify 0.1.0", "ckb-shared 0.1.0", "ckb-time 0.1.0", "ckb-util 0.1.0", + "ckb-verification 0.1.0", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -404,10 +406,8 @@ name = "ckb-chain-spec" version = "0.1.0" dependencies = [ "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", - "ckb-chain 0.1.0", "ckb-core 0.1.0", "ckb-pow 0.1.0", - "ckb-shared 0.1.0", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -463,17 +463,14 @@ dependencies = [ "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "ckb-chain 0.1.0", "ckb-core 0.1.0", - "ckb-db 0.1.0", "ckb-network 0.1.0", "ckb-notify 0.1.0", - "ckb-pool 0.1.0", "ckb-pow 0.1.0", "ckb-protocol 0.1.0", "ckb-rpc 0.1.0", "ckb-shared 0.1.0", "ckb-sync 0.1.0", "ckb-time 0.1.0", - "ckb-verification 0.1.0", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "flatbuffers 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -518,6 +515,7 @@ dependencies = [ "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "ckb-chain 0.1.0", + "ckb-chain-spec 0.1.0", "ckb-core 0.1.0", "ckb-db 0.1.0", "ckb-notify 0.1.0", @@ -580,6 +578,7 @@ dependencies = [ "ckb-shared 0.1.0", "ckb-sync 0.1.0", "ckb-time 0.1.0", + "ckb-verification 0.1.0", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 8.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -618,6 +617,7 @@ dependencies = [ "avl-merkle 0.1.0", "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "bincode 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ckb-chain-spec 0.1.0", "ckb-core 0.1.0", "ckb-db 0.1.0", "ckb-time 0.1.0", @@ -640,12 +640,12 @@ dependencies = [ "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "ckb-chain 0.1.0", + "ckb-chain-spec 0.1.0", "ckb-core 0.1.0", "ckb-db 0.1.0", "ckb-network 0.1.0", "ckb-notify 0.1.0", "ckb-pool 0.1.0", - "ckb-pow 0.1.0", "ckb-protocol 0.1.0", "ckb-shared 0.1.0", "ckb-time 0.1.0", @@ -679,6 +679,7 @@ version = "0.1.0" dependencies = [ "bigint 0.2.3 (git+https://github.com/nervosnetwork/bigint)", "ckb-chain 0.1.0", + "ckb-chain-spec 0.1.0", "ckb-core 0.1.0", "ckb-db 0.1.0", "ckb-notify 0.1.0", @@ -689,6 +690,7 @@ dependencies = [ "ckb-util 0.1.0", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "merkle-root 0.1.0", "rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/chain/Cargo.toml b/chain/Cargo.toml index 9267f84bc6..36b0677df8 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -11,10 +11,12 @@ bincode = "1.0" log = "0.4" ckb-core = { path = "../core" } ckb-shared = { path = "../shared" } +ckb-chain-spec = { path = "../spec" } ckb-util = { path = "../util" } ckb-db = { path = "../db" } ckb-time = { path = "../util/time" } ckb-notify = { path = "../notify" } +ckb-verification = { path = "../verification" } avl-merkle = { path = "../util/avl" } bigint = { git = "https://github.com/nervosnetwork/bigint" } lru-cache = { git = "https://github.com/nervosnetwork/lru-cache" } diff --git a/chain/src/chain.rs b/chain/src/chain.rs index ae3689e584..dfa507b372 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -1,4 +1,5 @@ use bigint::H256; +use chain_spec::consensus::Consensus; use channel::{self, Receiver, Sender}; use ckb_notify::{ForkBlocks, NotifyController, NotifyService}; use core::block::Block; @@ -6,8 +7,8 @@ use core::extras::BlockExt; use core::header::BlockNumber; use core::service::{Request, DEFAULT_CHANNEL_SIZE}; use db::batch::Batch; +use error::ProcessBlockError; use log; -use shared::consensus::Consensus; use shared::error::SharedError; use shared::index::ChainIndex; use shared::shared::{ChainProvider, Shared, TipHeader}; @@ -15,6 +16,7 @@ use std::cmp; use std::sync::Arc; use std::thread::{self, JoinHandle}; use time::now_ms; +use verification::{BlockVerifier, Verifier}; pub struct ChainService { shared: Shared, @@ -23,11 +25,11 @@ pub struct ChainService { #[derive(Clone)] pub struct ChainController { - process_block_sender: Sender, Result<(), SharedError>>>, + process_block_sender: Sender, Result<(), ProcessBlockError>>>, } pub struct ChainReceivers { - process_block_receiver: Receiver, Result<(), SharedError>>>, + process_block_receiver: Receiver, Result<(), ProcessBlockError>>>, } impl ChainController { @@ -43,7 +45,7 @@ impl ChainController { ) } - pub fn process_block(&self, block: Arc) -> Result<(), SharedError> { + pub fn process_block(&self, block: Arc) -> Result<(), ProcessBlockError> { Request::call(&self.process_block_sender, block).expect("process_block() failed") } } @@ -85,9 +87,16 @@ impl ChainService { }).expect("Start ChainService failed") } - fn process_block(&mut self, block: Arc) -> Result<(), SharedError> { + fn process_block(&mut self, block: Arc) -> Result<(), ProcessBlockError> { debug!(target: "chain", "begin processing block: {}", block.header().hash()); - let insert_result = self.insert_block(&block)?; + if self.shared.consensus().verification { + BlockVerifier::new(self.shared.clone()) + .verify(&block) + .map_err(ProcessBlockError::Verification)? + } + let insert_result = self + .insert_block(&block) + .map_err(ProcessBlockError::Shared)?; self.post_insert_result(block, insert_result); debug!(target: "chain", "finish processing block"); Ok(()) @@ -343,8 +352,8 @@ impl ChainBuilder { self } - pub fn build(self) -> ChainService { - let notify = self.notify.unwrap_or_else(|| { + pub fn build(mut self) -> ChainService { + let notify = self.notify.take().unwrap_or_else(|| { // FIXME: notify should not be optional let (_handle, notify) = NotifyService::default().start::<&str>(None); notify @@ -371,11 +380,10 @@ pub mod test { fn start_chain( consensus: Option, ) -> (ChainController, Shared>) { - let mut builder = SharedBuilder::>::new_memory(); - if let Some(consensus) = consensus { - builder = builder.consensus(consensus); - } - let shared = builder.build(); + let builder = SharedBuilder::>::new_memory(); + let shared = builder + .consensus(consensus.unwrap_or(Consensus::default().set_verification(false))) + .build(); let (chain_controller, chain_receivers) = ChainController::new(); let chain_service = ChainBuilder::new(shared.clone()).build(); @@ -447,7 +455,9 @@ pub mod test { .commit_transaction(tx) .with_header_builder(HeaderBuilder::default().difficulty(&U256::from(1000))); - let consensus = Consensus::default().set_genesis_block(genesis_block); + let consensus = Consensus::default() + .set_genesis_block(genesis_block) + .set_verification(false); let (chain_controller, shared) = start_chain(Some(consensus)); let end = 21; @@ -492,7 +502,9 @@ pub mod test { .commit_transaction(tx) .with_header_builder(HeaderBuilder::default().difficulty(&U256::from(1000))); - let consensus = Consensus::default().set_genesis_block(genesis_block); + let consensus = Consensus::default() + .set_genesis_block(genesis_block) + .set_verification(false); let (_chain_controller, shared) = start_chain(Some(consensus)); let outpoint = OutPoint::new(root_hash, 0); @@ -667,7 +679,9 @@ pub mod test { fn test_calculate_difficulty() { let genesis_block = BlockBuilder::default() .with_header_builder(HeaderBuilder::default().difficulty(&U256::from(1000))); - let mut consensus = Consensus::default().set_genesis_block(genesis_block); + let mut consensus = Consensus::default() + .set_genesis_block(genesis_block) + .set_verification(false); consensus.pow_time_span = 200; consensus.pow_spacing = 1; diff --git a/chain/src/error.rs b/chain/src/error.rs new file mode 100644 index 0000000000..f8ad501df8 --- /dev/null +++ b/chain/src/error.rs @@ -0,0 +1,8 @@ +use shared::error::SharedError; +use verification::Error as VerifyError; + +#[derive(Debug, PartialEq, Clone, Eq)] +pub enum ProcessBlockError { + Shared(SharedError), + Verification(VerifyError), +} diff --git a/chain/src/lib.rs b/chain/src/lib.rs index 371ff856e2..643e7bba1f 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -8,11 +8,13 @@ //! implement `ChainProvider` extern crate bigint; +extern crate ckb_chain_spec as chain_spec; extern crate ckb_core as core; extern crate ckb_db as db; extern crate ckb_notify; extern crate ckb_shared as shared; extern crate ckb_time as time; +extern crate ckb_verification as verification; #[macro_use] extern crate log; #[macro_use] @@ -24,3 +26,4 @@ extern crate rand; extern crate tempfile; pub mod chain; +pub mod error; diff --git a/miner/Cargo.toml b/miner/Cargo.toml index f84f7e4d25..25c4a0d527 100644 --- a/miner/Cargo.toml +++ b/miner/Cargo.toml @@ -23,8 +23,3 @@ serde = "1.0" serde_derive = "1.0" crossbeam-channel = "0.2" fnv = "1.0.3" - -[dev-dependencies] -ckb-db = { path = "../db" } -ckb-verification = { path = "../verification" } -ckb-pool = { path = "../pool" } diff --git a/miner/src/block_template.rs b/miner/src/block_template.rs deleted file mode 100644 index 300384f679..0000000000 --- a/miner/src/block_template.rs +++ /dev/null @@ -1,145 +0,0 @@ -use bigint::H256; -use chain::shared::ChainProvider; -use chain::index::ChainIndex; -use chain::error::Error; -use core::block::BlockBuilder; -use core::header::{Header, HeaderBuilder, RawHeader}; -use core::transaction::{CellInput, CellOutput, ProposalShortId, Transaction, TransactionBuilder}; -use core::uncle::UncleBlock; -use pool::TransactionPool; -use std::cmp; -use std::sync::Arc; -use time::now_ms; - -#[derive(Serialize, Debug)] -pub struct BlockTemplate { - pub raw_header: RawHeader, - pub uncles: Vec, - pub commit_transactions: Vec, - pub proposal_transactions: Vec, -} - -pub fn build_block_template( - chain: &Arc, - tx_pool: &Arc>, - type_hash: H256, - max_tx: usize, - max_prop: usize, -) -> Result { - let header = chain.get_tip_header().header.clone(); - let now = cmp::max(now_ms(), header.timestamp + 1); - let difficulty = chain.calculate_difficulty(&header).expect("get difficulty"); - let commit_transactions = tx_pool.get_mineable_transactions(max_tx); - let cellbase = create_cellbase_transaction(&chain, &header, &commit_transactions, type_hash)?; - - let header_builder = HeaderBuilder::default() - .parent_hash(&header.hash()) - .timestamp(now) - .number(header.number() + 1) - .difficulty(&difficulty) - .cellbase_id(&cellbase.hash()); - - let block = BlockBuilder::default() - .commit_transaction(cellbase) - .commit_transactions(commit_transactions) - .proposal_transactions(tx_pool.prepare_proposal(max_prop)) - .uncles(chain.get_tip_uncles()) - .with_header_builder(header_builder); - - Ok(BlockTemplate { - raw_header: block.header().clone().raw(), - uncles: block.uncles().to_vec(), - commit_transactions: block.commit_transactions().to_vec(), - proposal_transactions: block.proposal_transactions().to_vec(), - }) -} - -fn create_cellbase_transaction( - chain: &Arc, - header: &Header, - transactions: &[Transaction], - type_hash: H256, -) -> Result { - // NOTE: To generate different cellbase txid, we put header number in the input script - let input = CellInput::new_cellbase_input(header.number() + 1); - // NOTE: We could've just used byteorder to serialize u64 and hex string into bytes, - // but the truth is we will modify this after we designed lock script anyway, so let's - // stick to the simpler way and just convert everything to a single string, then to UTF8 - // bytes, they really serve the same purpose at the moment - let block_reward = chain.block_reward(header.number() + 1); - let mut fee = 0; - for transaction in transactions { - fee += chain.calculate_transaction_fee(transaction)?; - } - - let output = CellOutput::new(block_reward + fee, Vec::new(), type_hash, None); - - Ok(TransactionBuilder::default() - .input(input) - .output(output) - .build()) -} - -#[cfg(test)] -pub mod test { - use super::*; - use bigint::H256; - use chain::chain::ChainBuilder; - use chain::store::ChainKVStore; - use ckb_db::memorydb::MemoryKeyValueDB; - use ckb_notify::NotifyService; - use ckb_pow::{DummyPowEngine, PowEngine}; - use ckb_verification::{BlockVerifier, HeaderResolverWrapper, HeaderVerifier, Verifier}; - use core::block::BlockBuilder; - use pool::PoolConfig; - - fn dummy_pow_engine() -> Arc { - Arc::new(DummyPowEngine::new()) - } - - #[test] - fn test_block_template() { - let chain = Arc::new( - ChainBuilder::>::new_memory() - .build() - .unwrap(), - ); - - let pow_engine = dummy_pow_engine(); - - let (_handle, notify) = NotifyService::default().start::<&str>(None); - let tx_pool = Arc::new(TransactionPool::new( - PoolConfig::default(), - Arc::clone(&chain), - notify, - )); - - let block_template = - build_block_template(&chain, &tx_pool, H256::from(0), 1000, 1000).unwrap(); - - let BlockTemplate { - raw_header, - uncles, - commit_transactions, - proposal_transactions, - } = block_template; - - //do not verfiy pow here - let header = raw_header.with_seal(Default::default()); - - let block = BlockBuilder::default() - .header(header) - .uncles(uncles) - .commit_transactions(commit_transactions) - .proposal_transactions(proposal_transactions) - .build(); - - let resolver = HeaderResolverWrapper::new(block.header(), &chain); - let header_verify = HeaderVerifier::new(resolver, &pow_engine); - - assert!(header_verify.verify().is_ok()); - - let block_verfiy = BlockVerifier::new(&block, &chain, &pow_engine); - assert!(block_verfiy.verify().is_ok()); - } -} diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 05359cfc33..47a729fd0b 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -17,13 +17,6 @@ extern crate rand; extern crate serde_derive; extern crate ckb_pow; -#[cfg(test)] -extern crate ckb_db as db; -#[cfg(test)] -extern crate ckb_pool as pool; -#[cfg(test)] -extern crate ckb_verification as verification; - mod config; mod miner; diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 5bb41fb5e4..7d633654db 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -168,70 +168,3 @@ impl MinerService { }); } } - -#[cfg(test)] -pub mod test { - use super::*; - use bigint::H256; - use ckb_notify::NotifyService; - use ckb_pow::{DummyPowEngine, PowEngine}; - use core::block::BlockBuilder; - use db::memorydb::MemoryKeyValueDB; - use pool::txs_pool::{PoolConfig, TransactionPoolController, TransactionPoolService}; - use rpc::RpcService; - use shared::shared::SharedBuilder; - use shared::store::ChainKVStore; - use verification::{BlockVerifier, HeaderResolverWrapper, HeaderVerifier, Verifier}; - - #[test] - fn test_block_template() { - let (_handle, notify) = NotifyService::default().start::<&str>(None); - let (tx_pool_controller, tx_pool_receivers) = TransactionPoolController::new(); - let (rpc_controller, rpc_receivers) = RpcController::new(); - - let shared = SharedBuilder::>::new_memory().build(); - let tx_pool_service = - TransactionPoolService::new(PoolConfig::default(), shared.clone(), notify.clone()); - let _handle = tx_pool_service.start::<&str>(None, tx_pool_receivers); - - let rpc_service = RpcService::new(shared.clone(), tx_pool_controller.clone()); - let _handle = rpc_service.start(Some("RpcService"), rpc_receivers, ¬ify); - - let block_template = rpc_controller - .get_block_template(H256::from(0), 1000, 1000) - .unwrap(); - - let BlockTemplate { - raw_header, - uncles, - commit_transactions, - proposal_transactions, - } = block_template; - - //do not verfiy pow here - let header = raw_header.with_seal(Default::default()); - - let block = BlockBuilder::default() - .header(header) - .uncles(uncles) - .commit_transactions(commit_transactions) - .proposal_transactions(proposal_transactions) - .build(); - - fn dummy_pow_engine() -> Arc { - Arc::new(DummyPowEngine::new()) - } - let pow_engine = dummy_pow_engine(); - let resolver = HeaderResolverWrapper::new(block.header(), shared.clone()); - let header_verifier = HeaderVerifier::new(Arc::clone(&pow_engine)); - - assert!(header_verifier.verify(&resolver).is_ok()); - - let block_verfier = BlockVerifier::new( - shared.clone(), - shared.consensus().clone(), - Arc::clone(&pow_engine), - ); - assert!(block_verfier.verify(&block).is_ok()); - } -} diff --git a/pool/Cargo.toml b/pool/Cargo.toml index f99ea9562f..56b6768804 100644 --- a/pool/Cargo.toml +++ b/pool/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Nervos Core Dev "] [dependencies] ckb-core = { path = "../core" } ckb-shared = { path = "../shared" } +ckb-chain-spec = { path = "../spec" } ckb-util = { path = "../util" } ckb-time = { path = "../util/time" } ckb-notify = { path = "../notify" } diff --git a/pool/src/lib.rs b/pool/src/lib.rs index 0f517b1dd2..33a1549ac1 100644 --- a/pool/src/lib.rs +++ b/pool/src/lib.rs @@ -1,4 +1,5 @@ extern crate bigint; +extern crate ckb_chain_spec as chain_spec; extern crate ckb_core as core; extern crate ckb_notify; extern crate ckb_shared; diff --git a/pool/src/tests/pool.rs b/pool/src/tests/pool.rs index 0fdda4b55b..336bf23af1 100644 --- a/pool/src/tests/pool.rs +++ b/pool/src/tests/pool.rs @@ -1,4 +1,5 @@ use bigint::H256; +use chain_spec::consensus::Consensus; use channel::{self, Receiver}; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_db::memorydb::MemoryKeyValueDB; @@ -464,7 +465,9 @@ impl TestPool { let (_handle, notify) = NotifyService::default().start::<&str>(None); let new_tip_receiver = notify.subscribe_new_tip(TXS_POOL_SUBSCRIBER); let switch_fork_receiver = notify.subscribe_switch_fork(TXS_POOL_SUBSCRIBER); - let shared = SharedBuilder::>::new_memory().build(); + let shared = SharedBuilder::>::new_memory() + .consensus(Consensus::default().set_verification(false)) + .build(); let (chain_controller, chain_receivers) = ChainController::new(); let chain_service = ChainBuilder::new(shared.clone()) diff --git a/pool/src/txs_pool/types.rs b/pool/src/txs_pool/types.rs index a412d8008c..ab12d5882f 100644 --- a/pool/src/txs_pool/types.rs +++ b/pool/src/txs_pool/types.rs @@ -1,7 +1,7 @@ //! The primary module containing the implementations of the transaction pool //! and its top-level members. -use ckb_shared::consensus::{TRANSACTION_PROPAGATION_TIME, TRANSACTION_PROPAGATION_TIMEOUT}; +use chain_spec::consensus::{TRANSACTION_PROPAGATION_TIME, TRANSACTION_PROPAGATION_TIMEOUT}; use ckb_verification::TransactionError; use core::transaction::{CellOutput, OutPoint, ProposalShortId, Transaction}; use core::BlockNumber; diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 04059f6e9c..53ad35ef50 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -27,5 +27,9 @@ log = "0.4" crossbeam-channel = "0.2" fnv = "1.0.3" +[dev-dependencies] +ckb-db = { path = "../db" } +ckb-verification = { path = "../verification" } + [features] integration_test = ["ckb-pow"] diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index f43b2c10b9..1bde3dfd95 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -7,11 +7,15 @@ extern crate jsonrpc_server_utils; #[macro_use] extern crate log; extern crate ckb_core as core; +#[cfg(test)] +extern crate ckb_db as db; extern crate ckb_network as network; extern crate ckb_notify as notify; extern crate ckb_pool as pool; extern crate ckb_shared as shared; extern crate ckb_time; +#[cfg(test)] +extern crate ckb_verification as verification; #[macro_use] extern crate serde_derive; #[cfg(feature = "integration_test")] diff --git a/rpc/src/service.rs b/rpc/src/service.rs index 6d39f94588..507c74e5ea 100644 --- a/rpc/src/service.rs +++ b/rpc/src/service.rs @@ -288,3 +288,60 @@ impl RpcService { uncles } } + +#[cfg(test)] +pub mod test { + use super::*; + use bigint::H256; + use core::block::BlockBuilder; + use db::memorydb::MemoryKeyValueDB; + use notify::NotifyService; + use pool::txs_pool::{PoolConfig, TransactionPoolController, TransactionPoolService}; + use shared::shared::SharedBuilder; + use shared::store::ChainKVStore; + use verification::{BlockVerifier, HeaderResolverWrapper, HeaderVerifier, Verifier}; + + #[test] + fn test_block_template() { + let (_handle, notify) = NotifyService::default().start::<&str>(None); + let (tx_pool_controller, tx_pool_receivers) = TransactionPoolController::new(); + let (rpc_controller, rpc_receivers) = RpcController::new(); + + let shared = SharedBuilder::>::new_memory().build(); + let tx_pool_service = + TransactionPoolService::new(PoolConfig::default(), shared.clone(), notify.clone()); + let _handle = tx_pool_service.start::<&str>(None, tx_pool_receivers); + + let rpc_service = RpcService::new(shared.clone(), tx_pool_controller.clone()); + let _handle = rpc_service.start(Some("RpcService"), rpc_receivers, ¬ify); + + let block_template = rpc_controller + .get_block_template(H256::from(0), 1000, 1000) + .unwrap(); + + let BlockTemplate { + raw_header, + uncles, + commit_transactions, + proposal_transactions, + } = block_template; + + //do not verfiy pow here + let header = raw_header.with_seal(Default::default()); + + let block = BlockBuilder::default() + .header(header) + .uncles(uncles) + .commit_transactions(commit_transactions) + .proposal_transactions(proposal_transactions) + .build(); + + let resolver = HeaderResolverWrapper::new(block.header(), shared.clone()); + let header_verifier = HeaderVerifier::new(Arc::clone(&shared.consensus().pow_engine())); + + assert!(header_verifier.verify(&resolver).is_ok()); + + let block_verfier = BlockVerifier::new(shared.clone()); + assert!(block_verfier.verify(&block).is_ok()); + } +} diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 55b3fce9bf..44e7107beb 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -9,6 +9,7 @@ serde_derive = "1.0" bincode = "1.0" log = "0.4" ckb-core = { path = "../core" } +ckb-chain-spec = { path = "../spec" } ckb-util = { path = "../util" } ckb-db = { path = "../db" } ckb-time = { path = "../util/time" } diff --git a/shared/src/index.rs b/shared/src/index.rs index 1a0bf35c3e..72d3e8ae6b 100644 --- a/shared/src/index.rs +++ b/shared/src/index.rs @@ -156,7 +156,7 @@ impl ChainIndex for ChainKVStore { mod tests { use super::super::COLUMNS; use super::*; - use consensus::Consensus; + use chain_spec::consensus::Consensus; use db::diskdb::RocksDB; use tempfile; diff --git a/shared/src/lib.rs b/shared/src/lib.rs index 4e944aad26..b8dce8d281 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -10,6 +10,7 @@ extern crate avl_merkle as avl; extern crate bigint; extern crate bincode; +extern crate ckb_chain_spec as chain_spec; extern crate ckb_core as core; extern crate ckb_db as db; extern crate ckb_util as util; @@ -25,7 +26,6 @@ extern crate rand; extern crate tempfile; pub mod cachedb; -pub mod consensus; // mod config; pub mod error; mod flat_serializer; diff --git a/shared/src/shared.rs b/shared/src/shared.rs index 8ac968c8d9..334aef4921 100644 --- a/shared/src/shared.rs +++ b/shared/src/shared.rs @@ -1,7 +1,7 @@ use super::{COLUMNS, COLUMN_BLOCK_HEADER}; use bigint::{H256, U256}; use cachedb::CacheDB; -use consensus::Consensus; +use chain_spec::consensus::Consensus; use core::block::Block; use core::cell::{CellProvider, CellStatus}; use core::extras::BlockExt; @@ -119,10 +119,6 @@ impl Shared { pub fn store(&self) -> &Arc { &self.store } - - pub fn consensus(&self) -> &Consensus { - &self.consensus - } } impl CellProvider for Shared { @@ -211,6 +207,8 @@ pub trait ChainProvider: Sync + Send { -> Result; fn calculate_difficulty(&self, last: &Header) -> Option; + + fn consensus(&self) -> &Consensus; } impl ChainProvider for Shared { @@ -423,6 +421,10 @@ impl ChainProvider for Shared { } None } + + fn consensus(&self) -> &Consensus { + &self.consensus + } } pub struct SharedBuilder { diff --git a/shared/src/store.rs b/shared/src/store.rs index 6581faf91b..42691209a9 100644 --- a/shared/src/store.rs +++ b/shared/src/store.rs @@ -317,7 +317,7 @@ impl ChainStore for ChainKVStore { mod tests { use super::super::COLUMNS; use super::*; - use consensus::Consensus; + use chain_spec::consensus::Consensus; use db::diskdb::RocksDB; use tempfile; diff --git a/spec/Cargo.toml b/spec/Cargo.toml index 8895fcce73..3f3ab6a63b 100644 --- a/spec/Cargo.toml +++ b/spec/Cargo.toml @@ -8,8 +8,6 @@ authors = ["Nervos Core Dev "] serde_yaml = "0.7" bigint = { git = "https://github.com/nervosnetwork/bigint" } ckb-core = { path = "../core" } -ckb-chain = { path = "../chain" } -ckb-shared = { path = "../shared" } serde_derive = "1.0" serde = "1.0" ckb-pow = { path = "../pow" } diff --git a/shared/src/consensus.rs b/spec/src/consensus.rs similarity index 85% rename from shared/src/consensus.rs rename to spec/src/consensus.rs index 7ba2ad092b..3300262f7c 100644 --- a/shared/src/consensus.rs +++ b/spec/src/consensus.rs @@ -1,8 +1,10 @@ use bigint::U256; +use ckb_pow::{Pow, PowEngine}; use core::block::{Block, BlockBuilder}; use core::header::HeaderBuilder; use core::transaction::Capacity; use core::BlockNumber; +use std::sync::Arc; pub const DEFAULT_BLOCK_REWARD: Capacity = 5_000; pub const MAX_UNCLE_LEN: usize = 2; @@ -27,6 +29,8 @@ pub struct Consensus { pub pow_spacing: u64, pub transaction_propagation_time: BlockNumber, pub transaction_propagation_timeout: BlockNumber, + pub pow: Pow, + pub verification: bool, } // genesis difficulty should not be zero @@ -46,6 +50,8 @@ impl Default for Consensus { pow_spacing: POW_SPACING, transaction_propagation_time: TRANSACTION_PROPAGATION_TIME, transaction_propagation_timeout: TRANSACTION_PROPAGATION_TIMEOUT, + pow: Pow::Dummy, + verification: true, } } } @@ -66,6 +72,16 @@ impl Consensus { self } + pub fn set_pow(mut self, pow: Pow) -> Self { + self.pow = pow; + self + } + + pub fn set_verification(mut self, verification: bool) -> Self { + self.verification = verification; + self + } + pub fn genesis_block(&self) -> &Block { &self.genesis_block } @@ -93,4 +109,8 @@ impl Consensus { pub fn orphan_rate_target(&self) -> f32 { self.orphan_rate_target } + + pub fn pow_engine(&self) -> Arc { + self.pow.engine() + } } diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 189ffe9787..646dc7e6f6 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -8,7 +8,6 @@ extern crate bigint; extern crate ckb_core as core; -extern crate ckb_shared as shared; extern crate serde_yaml; #[macro_use] extern crate serde_derive; @@ -16,17 +15,19 @@ extern crate ckb_pow; use bigint::{H256, U256}; use ckb_pow::{Pow, PowEngine}; +use consensus::Consensus; use core::block::BlockBuilder; use core::header::HeaderBuilder; use core::transaction::{CellOutput, Transaction, TransactionBuilder}; use core::Capacity; -use shared::consensus::Consensus; use std::error::Error; use std::fs::File; use std::io::Read; use std::path::Path; use std::sync::Arc; +pub mod consensus; + #[derive(Debug, PartialEq, Eq)] pub enum SpecType { Dev, @@ -131,7 +132,8 @@ impl ChainSpec { let consensus = Consensus::default() .set_id(self.name.clone()) .set_genesis_block(genesis_block) - .set_initial_block_reward(self.params.initial_block_reward); + .set_initial_block_reward(self.params.initial_block_reward) + .set_pow(self.pow.clone()); Ok(consensus) } diff --git a/src/cli/export.rs b/src/cli/export.rs index 032ab70f4e..3639decd6a 100644 --- a/src/cli/export.rs +++ b/src/cli/export.rs @@ -1,5 +1,5 @@ use super::super::setup::Configs; -use ckb_chain_spec::SpecType; +use chain_spec::SpecType; use ckb_instrument::{Export, Format}; use clap::ArgMatches; use config_tool::{Config as ConfigTool, File, FileFormat}; diff --git a/src/cli/import.rs b/src/cli/import.rs index d363a420d9..bdd1a07a3a 100644 --- a/src/cli/import.rs +++ b/src/cli/import.rs @@ -1,6 +1,6 @@ use super::super::setup::Configs; use chain::chain::{ChainBuilder, ChainController}; -use ckb_chain_spec::SpecType; +use chain_spec::SpecType; use ckb_instrument::{Format, Import}; use clap::ArgMatches; use config_tool::{Config as ConfigTool, File, FileFormat}; diff --git a/src/cli/run_impl.rs b/src/cli/run_impl.rs index 9eeee704b2..39fe80a3ad 100644 --- a/src/cli/run_impl.rs +++ b/src/cli/run_impl.rs @@ -27,7 +27,6 @@ use std::io::Write; use std::sync::Arc; use std::thread; use sync::{Relayer, Synchronizer, RELAY_PROTOCOL_ID, SYNC_PROTOCOL_ID}; -use verification::BlockVerifier; pub fn run(setup: Setup) { logger::init(setup.configs.logger.clone()).expect("Init Logger"); @@ -53,12 +52,6 @@ pub fn run(setup: Setup) { info!(target: "main", "chain genesis hash: {:?}", shared.genesis_hash()); - let block_verifier = BlockVerifier::new( - shared.clone(), - shared.consensus().clone(), - Arc::clone(&pow_engine), - ); - let tx_pool_service = TransactionPoolService::new(setup.configs.pool, shared.clone(), notify.clone()); let _handle = tx_pool_service.start(Some("TransactionPoolService"), tx_pool_receivers); @@ -69,17 +62,13 @@ pub fn run(setup: Setup) { let synchronizer = Arc::new(Synchronizer::new( chain_controller.clone(), shared.clone(), - Arc::clone(&pow_engine), - block_verifier.clone(), setup.configs.sync, )); let relayer = Arc::new(Relayer::new( chain_controller.clone(), shared.clone(), - Arc::clone(&pow_engine), tx_pool_controller.clone(), - block_verifier, )); let network_config = NetworkConfig::from(setup.configs.network); diff --git a/src/main.rs b/src/main.rs index fe3650b1e4..bd3edc307d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate dir; extern crate log; extern crate bigint; extern crate ckb_chain as chain; -extern crate ckb_chain_spec; +extern crate ckb_chain_spec as chain_spec; extern crate ckb_core as core; extern crate ckb_db as db; extern crate ckb_miner as miner; @@ -17,7 +17,6 @@ extern crate ckb_rpc as rpc; extern crate ckb_shared as shared; extern crate ckb_sync as sync; extern crate ckb_util as util; -extern crate ckb_verification as verification; extern crate hash; extern crate logger; #[macro_use] diff --git a/src/setup.rs b/src/setup.rs index d109da68cc..77c6f3262f 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,4 +1,4 @@ -use ckb_chain_spec::{ChainSpec, SpecType}; +use chain_spec::{ChainSpec, SpecType}; use clap; use config_tool::{Config as ConfigTool, File, FileFormat}; use dir::{default_base_path, Directories}; diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 492eef936c..a5c077f30c 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -23,7 +23,6 @@ ckb-verification = { path = "../verification" } serde = "1.0" serde_derive = "1.0" flatbuffers = "0.5.0" -ckb-pow = { path = "../pow" } [dev-dependencies] ckb-notify = { path = "../notify" } @@ -31,3 +30,4 @@ ckb-db = { path = "../db" } ckb-time = { path = "../util/time", features = ["mock_timer"] } env_logger = "0.5" crossbeam-channel = "0.2" +ckb-chain-spec = { path = "../spec" } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 96ba951f41..e6be0ad222 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -11,7 +11,6 @@ extern crate ckb_chain; extern crate ckb_core as core; extern crate ckb_network as network; extern crate ckb_pool as pool; -extern crate ckb_pow; extern crate ckb_protocol; extern crate ckb_shared; extern crate ckb_time; @@ -23,6 +22,9 @@ extern crate ckb_verification; extern crate bitflags; #[macro_use] extern crate serde_derive; + +#[cfg(test)] +extern crate ckb_chain_spec as chain_spec; #[cfg(test)] extern crate ckb_db as db; #[cfg(test)] @@ -35,6 +37,7 @@ extern crate crossbeam_channel; mod config; mod relayer; mod synchronizer; + #[cfg(test)] mod tests; @@ -70,21 +73,3 @@ pub const EVICTION_HEADERS_RESPONSE_TIME: u64 = 120 * 1000; // 2 minutes //The maximum number of entries in a locator pub const MAX_LOCATOR_SIZE: usize = 101; - -#[derive(Debug, PartialEq, Clone, Eq)] -pub enum AcceptBlockError { - Chain(ckb_shared::error::SharedError), - Verification(ckb_verification::Error), -} - -impl From for AcceptBlockError { - fn from(error: ckb_shared::error::SharedError) -> Self { - AcceptBlockError::Chain(error) - } -} - -impl From for AcceptBlockError { - fn from(error: ckb_verification::Error) -> Self { - AcceptBlockError::Verification(error) - } -} diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index ec7d0fd75d..db9fcc4879 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -17,11 +17,10 @@ use self::get_block_transactions_process::GetBlockTransactionsProcess; use self::transaction_process::TransactionProcess; use bigint::H256; use ckb_chain::chain::ChainController; -use ckb_pow::PowEngine; +use ckb_chain::error::ProcessBlockError; use ckb_protocol::{short_transaction_id, short_transaction_id_keys, RelayMessage, RelayPayload}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::{ChainProvider, Shared}; -use ckb_verification::{BlockVerifier, Verifier}; use core::block::{Block, BlockBuilder}; use core::transaction::{ProposalShortId, Transaction}; use flatbuffers::{get_root, FlatBufferBuilder}; @@ -31,16 +30,13 @@ use pool::txs_pool::TransactionPoolController; use std::sync::Arc; use std::time::Duration; use util::Mutex; -use AcceptBlockError; pub const TX_PROPOSAL_TOKEN: TimerToken = 0; pub struct Relayer { chain: ChainController, shared: Shared, - pow: Arc, tx_pool: TransactionPoolController, - block_verifier: BlockVerifier>, state: Arc, } @@ -49,9 +45,7 @@ impl ::std::clone::Clone for Relayer { Relayer { chain: self.chain.clone(), shared: self.shared.clone(), - pow: Arc::clone(&self.pow), tx_pool: self.tx_pool.clone(), - block_verifier: self.block_verifier.clone(), state: Arc::clone(&self.state), } } @@ -64,16 +58,12 @@ where pub fn new( chain: ChainController, shared: Shared, - pow: Arc, tx_pool: TransactionPoolController, - block_verifier: BlockVerifier>, ) -> Self { Relayer { chain, shared, - pow, tx_pool, - block_verifier, state: Arc::new(RelayState::default()), } } @@ -142,10 +132,8 @@ where let _ = nc.send(peer, fbb.finished_data().to_vec()); } - pub fn accept_block(&self, _peer: PeerIndex, block: Block) -> Result<(), AcceptBlockError> { - self.block_verifier.verify(&block)?; - self.chain.process_block(Arc::new(block))?; - Ok(()) + pub fn accept_block(&self, _peer: PeerIndex, block: Block) -> Result<(), ProcessBlockError> { + self.chain.process_block(Arc::new(block)) } pub fn reconstruct_block( diff --git a/sync/src/synchronizer/block_pool.rs b/sync/src/synchronizer/block_pool.rs index 7adc43c042..e78e074b68 100644 --- a/sync/src/synchronizer/block_pool.rs +++ b/sync/src/synchronizer/block_pool.rs @@ -59,7 +59,7 @@ impl OrphanBlockPool { #[cfg(test)] mod tests { use super::*; - use ckb_shared::consensus::Consensus; + use chain_spec::consensus::Consensus; use ckb_time::now_ms; use core::block::BlockBuilder; use core::header::{Header, HeaderBuilder}; diff --git a/sync/src/synchronizer/headers_process.rs b/sync/src/synchronizer/headers_process.rs index 0ca03cedae..1727e747eb 100644 --- a/sync/src/synchronizer/headers_process.rs +++ b/sync/src/synchronizer/headers_process.rs @@ -93,7 +93,9 @@ where pub fn accept_first(&self, first: &Header) -> ValidationResult { let parent = self.synchronizer.get_header(&first.parent_hash()); let resolver = VerifierResolver::new(parent.as_ref(), &first, &self.synchronizer.shared); - let verifier = HeaderVerifier::new(Arc::clone(&self.synchronizer.pow)); + let verifier = HeaderVerifier::new(Arc::clone( + &self.synchronizer.shared.consensus().pow_engine(), + )); let acceptor = HeaderAcceptor::new(first, self.peer, &self.synchronizer, resolver, verifier); acceptor.accept() @@ -138,7 +140,9 @@ where if let [parent, header] = &window { let resolver = VerifierResolver::new(Some(&parent), &header, &self.synchronizer.shared); - let verifier = HeaderVerifier::new(Arc::clone(&self.synchronizer.pow)); + let verifier = HeaderVerifier::new(Arc::clone( + &self.synchronizer.shared.consensus().pow_engine(), + )); let acceptor = HeaderAcceptor::new(&header, self.peer, &self.synchronizer, resolver, verifier); let result = acceptor.accept(); diff --git a/sync/src/synchronizer/mod.rs b/sync/src/synchronizer/mod.rs index de67830e9f..001f83a195 100644 --- a/sync/src/synchronizer/mod.rs +++ b/sync/src/synchronizer/mod.rs @@ -17,12 +17,11 @@ use self::headers_process::HeadersProcess; use self::peers::Peers; use bigint::H256; use ckb_chain::chain::ChainController; -use ckb_pow::PowEngine; +use ckb_chain::error::ProcessBlockError; use ckb_protocol::{SyncMessage, SyncPayload}; use ckb_shared::index::ChainIndex; use ckb_shared::shared::{ChainProvider, Shared}; use ckb_time::now_ms; -use ckb_verification::{BlockVerifier, Verifier}; use config::Config; use core::block::Block; use core::header::{BlockNumber, Header}; @@ -35,7 +34,6 @@ use std::sync::atomic::Ordering; use std::sync::Arc; use std::time::Duration; use util::{RwLock, RwLockUpgradableReadGuard}; -use AcceptBlockError; use { CHAIN_SYNC_TIMEOUT, EVICTION_HEADERS_RESPONSE_TIME, HEADERS_DOWNLOAD_TIMEOUT_BASE, HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER, MAX_HEADERS_LEN, @@ -72,8 +70,6 @@ pub type BlockHeaderMap = Arc>>; pub struct Synchronizer { chain: ChainController, shared: Shared, - pow: Arc, - block_verifier: BlockVerifier>, pub status_map: BlockStatusMap, pub header_map: BlockHeaderMap, pub best_known_header: Arc>, @@ -89,8 +85,6 @@ impl ::std::clone::Clone for Synchronizer { Synchronizer { chain: self.chain.clone(), shared: self.shared.clone(), - pow: Arc::clone(&self.pow), - block_verifier: self.block_verifier.clone(), status_map: Arc::clone(&self.status_map), header_map: Arc::clone(&self.header_map), best_known_header: Arc::clone(&self.best_known_header), @@ -109,13 +103,7 @@ fn is_outbound(nc: &CKBProtocolContext, peer: PeerIndex) -> Option { } impl Synchronizer { - pub fn new( - chain: ChainController, - shared: Shared, - pow: Arc, - block_verifier: BlockVerifier>, - config: Config, - ) -> Synchronizer { + pub fn new(chain: ChainController, shared: Shared, config: Config) -> Synchronizer { let (total_difficulty, header) = { let tip_header = shared.tip_header().read(); (tip_header.total_difficulty(), tip_header.inner().clone()) @@ -127,8 +115,6 @@ impl Synchronizer { config: Arc::new(config), chain, shared, - pow, - block_verifier, peers: Arc::new(Peers::default()), orphan_block_pool: Arc::new(OrphanBlockPool::with_capacity(orphan_block_limit)), best_known_header: Arc::new(RwLock::new(best_known_header)), @@ -403,8 +389,7 @@ impl Synchronizer { } } - fn accept_block(&self, peer: PeerIndex, block: &Arc) -> Result<(), AcceptBlockError> { - self.block_verifier.verify(&block)?; + fn accept_block(&self, peer: PeerIndex, block: &Arc) -> Result<(), ProcessBlockError> { self.chain.process_block(Arc::clone(&block))?; self.mark_block_stored(block.header().hash()); self.peers.set_last_common_header(peer, &block.header()); @@ -678,16 +663,14 @@ mod tests { use self::headers_process::HeadersProcess; use super::*; use bigint::U256; + use chain_spec::consensus::Consensus; use ckb_chain::chain::ChainBuilder; use ckb_notify::{NotifyController, NotifyService, MINER_SUBSCRIBER}; - use ckb_pow::DummyPowEngine; use ckb_protocol::{Block as FbsBlock, Headers as FbsHeaders}; - use ckb_shared::consensus::Consensus; use ckb_shared::index::ChainIndex; use ckb_shared::shared::SharedBuilder; use ckb_shared::store::ChainKVStore; use ckb_time::set_mock_timer; - use ckb_verification::BlockVerifier; use core::block::BlockBuilder; use core::header::{Header, HeaderBuilder}; use core::transaction::{CellInput, CellOutput, Transaction, TransactionBuilder}; @@ -726,26 +709,10 @@ mod tests { } fn gen_synchronizer( - pow_engine: Arc, chain_controller: ChainController, shared: Shared, ) -> Synchronizer { - let block_verifier = BlockVerifier::new( - shared.clone(), - shared.consensus().clone(), - Arc::clone(&pow_engine), - ); - Synchronizer::new( - chain_controller, - shared, - pow_engine, - block_verifier, - Config::default(), - ) - } - - fn dummy_pow_engine() -> Arc { - Arc::new(DummyPowEngine::new()) + Synchronizer::new(chain_controller, shared, Config::default()) } #[test] @@ -810,8 +777,7 @@ mod tests { insert_block(&chain_controller, &shared, i, i); } - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller.clone(), shared.clone()); + let synchronizer = gen_synchronizer(chain_controller.clone(), shared.clone()); let locator = synchronizer.get_locator(&shared.tip_header().read().inner()); @@ -841,19 +807,9 @@ mod tests { insert_block(&chain_controller2, &shared2, i + 1, i); } - let pow_engine = dummy_pow_engine(); - - let synchronizer1 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller1.clone(), - shared1.clone(), - ); + let synchronizer1 = gen_synchronizer(chain_controller1.clone(), shared1.clone()); - let synchronizer2 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller2.clone(), - shared2.clone(), - ); + let synchronizer2 = gen_synchronizer(chain_controller2.clone(), shared2.clone()); let locator1 = synchronizer1.get_locator(&shared1.tip_header().read().inner()); @@ -868,11 +824,7 @@ mod tests { insert_block(&chain_controller3, &shared3, j, i); } - let synchronizer3 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller3.clone(), - shared3.clone(), - ); + let synchronizer3 = gen_synchronizer(chain_controller3.clone(), shared3.clone()); let latest_common3 = synchronizer3.locate_latest_common_block(&H256::zero(), &locator1[..]); assert_eq!(latest_common3, Some(192)); @@ -912,18 +864,8 @@ mod tests { parent = new_block.header().clone(); } - let pow_engine = dummy_pow_engine(); - - let synchronizer1 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller1.clone(), - shared1.clone(), - ); - let synchronizer2 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller2.clone(), - shared2.clone(), - ); + let synchronizer1 = gen_synchronizer(chain_controller1.clone(), shared1.clone()); + let synchronizer2 = gen_synchronizer(chain_controller2.clone(), shared2.clone()); let locator1 = synchronizer1.get_locator(&shared1.tip_header().read().inner()); let latest_common = synchronizer2 @@ -951,8 +893,7 @@ mod tests { insert_block(&chain_controller, &shared, i, i); } - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller.clone(), shared.clone()); + let synchronizer = gen_synchronizer(chain_controller.clone(), shared.clone()); let header = synchronizer.get_ancestor(&shared.tip_header().read().hash(), 100); let tip = synchronizer.get_ancestor(&shared.tip_header().read().hash(), 199); @@ -991,8 +932,7 @@ mod tests { parent = new_block.header().clone(); } - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller2.clone(), shared2.clone()); + let synchronizer = gen_synchronizer(chain_controller2.clone(), shared2.clone()); blocks.clone().into_iter().for_each(|block| { synchronizer.insert_new_block(peer, block); @@ -1022,8 +962,7 @@ mod tests { parent = new_block.header().clone(); } - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller.clone(), shared.clone()); + let synchronizer = gen_synchronizer(chain_controller.clone(), shared.clone()); let headers = synchronizer.get_locator_response(180, &H256::zero()); @@ -1130,12 +1069,8 @@ mod tests { for i in 1..num { insert_block(&chain_controller1, &shared1, i, i); } - let pow_engine = dummy_pow_engine(); - let synchronizer1 = gen_synchronizer( - Arc::clone(&pow_engine), - chain_controller1.clone(), - shared1.clone(), - ); + + let synchronizer1 = gen_synchronizer(chain_controller1.clone(), shared1.clone()); let locator1 = synchronizer1.get_locator(&shared1.tip_header().read().inner()); @@ -1144,8 +1079,7 @@ mod tests { insert_block(&chain_controller2, &shared2, j, i); } - let synchronizer2 = - gen_synchronizer(pow_engine, chain_controller2.clone(), shared2.clone()); + let synchronizer2 = gen_synchronizer(chain_controller2.clone(), shared2.clone()); let latest_common = synchronizer2.locate_latest_common_block(&H256::zero(), &locator1[..]); assert_eq!(latest_common, Some(192)); @@ -1222,8 +1156,7 @@ mod tests { let (chain_controller, shared, _notify) = start_chain(None, None); - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller.clone(), shared.clone()); + let synchronizer = gen_synchronizer(chain_controller.clone(), shared.clone()); let network_context = mock_network_context(5); set_mock_timer(MAX_TIP_AGE * 2); @@ -1253,8 +1186,7 @@ mod tests { assert_eq!(shared.tip_header().read().total_difficulty(), U256::from(2)); - let pow_engine = dummy_pow_engine(); - let synchronizer = gen_synchronizer(pow_engine, chain_controller.clone(), shared.clone()); + let synchronizer = gen_synchronizer(chain_controller.clone(), shared.clone()); let network_context = mock_network_context(6); let peers = synchronizer.peers(); diff --git a/sync/src/tests/mod.rs b/sync/src/tests/mod.rs index 3f01a1e017..9a5fb5b365 100644 --- a/sync/src/tests/mod.rs +++ b/sync/src/tests/mod.rs @@ -1,4 +1,3 @@ -use ckb_pow::{DummyPowEngine, PowEngine}; use network::{ Error as NetworkError, NetworkContext, NetworkProtocolHandler, PacketId, PeerId, ProtocolId, SessionInfo, Severity, TimerToken, @@ -192,7 +191,3 @@ impl NetworkContext for TestNetworkContext { [1, 1, 1] } } - -fn dummy_pow_engine() -> Arc { - Arc::new(DummyPowEngine::new()) -} diff --git a/sync/src/tests/relayer.rs b/sync/src/tests/relayer.rs index 1b6146b12a..4f34e0f121 100644 --- a/sync/src/tests/relayer.rs +++ b/sync/src/tests/relayer.rs @@ -1,13 +1,12 @@ use bigint::{H256, U256}; +use chain_spec::consensus::Consensus; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_notify::NotifyService; use ckb_pool::txs_pool::{PoolConfig, TransactionPoolController, TransactionPoolService}; use ckb_protocol::RelayMessage; -use ckb_shared::consensus::Consensus; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use ckb_time::now_ms; -use ckb_verification::BlockVerifier; use core::block::BlockBuilder; use core::header::HeaderBuilder; use core::script::Script; @@ -22,7 +21,7 @@ use std::io::Read; use std::sync::mpsc::channel; use std::sync::{Arc, Barrier}; use std::{thread, time}; -use tests::{dummy_pow_engine, TestNode}; +use tests::TestNode; use {Relayer, RELAY_PROTOCOL_ID}; #[test] @@ -326,19 +325,7 @@ fn setup_node( .expect("process block should be OK"); } - let pow_engine = dummy_pow_engine(); - let block_verifier = BlockVerifier::new( - shared.clone(), - shared.consensus().clone(), - Arc::clone(&pow_engine), - ); - let relayer = Relayer::new( - chain_controller.clone(), - shared.clone(), - pow_engine, - tx_pool_controller, - block_verifier, - ); + let relayer = Relayer::new(chain_controller.clone(), shared.clone(), tx_pool_controller); let mut node = TestNode::default(); node.add_protocol( diff --git a/sync/src/tests/synchronizer.rs b/sync/src/tests/synchronizer.rs index ea780375b4..f426b86d63 100644 --- a/sync/src/tests/synchronizer.rs +++ b/sync/src/tests/synchronizer.rs @@ -1,12 +1,11 @@ use bigint::U256; +use chain_spec::consensus::Consensus; use ckb_chain::chain::{ChainBuilder, ChainController}; use ckb_notify::NotifyService; use ckb_protocol::SyncMessage; -use ckb_shared::consensus::Consensus; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use ckb_time::now_ms; -use ckb_verification::BlockVerifier; use core::block::BlockBuilder; use core::header::HeaderBuilder; use core::transaction::{CellInput, CellOutput, TransactionBuilder}; @@ -16,7 +15,7 @@ use std::sync::mpsc::channel; use std::sync::Arc; use std::thread; use synchronizer::BLOCK_FETCH_TOKEN; -use tests::{dummy_pow_engine, TestNode}; +use tests::TestNode; use {Config, Synchronizer, SYNC_PROTOCOL_ID}; #[test] @@ -96,19 +95,7 @@ fn setup_node(height: u64) -> (TestNode, Shared>) .expect("process block should be OK"); } - let pow_engine = dummy_pow_engine(); - let block_verifier = BlockVerifier::new( - shared.clone(), - shared.consensus().clone(), - Arc::clone(&pow_engine), - ); - let synchronizer = Synchronizer::new( - chain_controller, - shared.clone(), - pow_engine, - block_verifier, - Config::default(), - ); + let synchronizer = Synchronizer::new(chain_controller, shared.clone(), Config::default()); let mut node = TestNode::default(); node.add_protocol( SYNC_PROTOCOL_ID, diff --git a/util/instrument/src/export.rs b/util/instrument/src/export.rs index 76e70e5f69..1561ce0369 100644 --- a/util/instrument/src/export.rs +++ b/util/instrument/src/export.rs @@ -1,7 +1,7 @@ use super::format::Format; use super::iter::ChainIterator; use ckb_shared::index::ChainIndex; -use ckb_shared::shared::Shared; +use ckb_shared::shared::{ChainProvider, Shared}; #[cfg(feature = "progress_bar")] use indicatif::{ProgressBar, ProgressStyle}; use serde_json; diff --git a/verification/Cargo.toml b/verification/Cargo.toml index 24ffcb198a..8e59f47ac6 100644 --- a/verification/Cargo.toml +++ b/verification/Cargo.toml @@ -23,3 +23,5 @@ ckb-db = { path = "../db" } ckb-notify = { path = "../notify" } ckb-time = { path = "../util/time", features = ["mock_timer"] } ckb-chain = { path = "../chain" } +ckb-chain-spec = { path = "../spec" } +hash = {path = "../util/hash"} diff --git a/verification/src/block_verifier.rs b/verification/src/block_verifier.rs index 9fa082aa28..4c1b7d6835 100644 --- a/verification/src/block_verifier.rs +++ b/verification/src/block_verifier.rs @@ -1,7 +1,6 @@ use super::header_verifier::HeaderResolver; use super::{TransactionVerifier, Verifier}; use bigint::{H256, U256}; -use ckb_shared::consensus::Consensus; use ckb_shared::shared::ChainProvider; use core::block::Block; use core::cell::{CellProvider, CellStatus}; @@ -11,13 +10,11 @@ use error::TransactionError; use error::{CellbaseError, CommitError, Error, UnclesError}; use fnv::{FnvHashMap, FnvHashSet}; use merkle_root::merkle_root; -use pow::PowEngine; use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use std::collections::HashSet; -use std::sync::Arc; //TODO: cellbase, witness -pub struct BlockVerifier { +pub struct BlockVerifier

{ // Verify if the committed transactions is empty empty: EmptyVerifier, // Verify if the committed and proposed transactions contains duplicate @@ -52,15 +49,15 @@ impl

BlockVerifier

where P: ChainProvider + CellProvider + Clone + 'static, { - pub fn new(provider: P, consensus: Consensus, pow: Arc) -> Self { + pub fn new(provider: P) -> Self { BlockVerifier { // TODO change all new fn's chain to reference empty: EmptyVerifier::new(), duplicate: DuplicateVerifier::new(), cellbase: CellbaseVerifier::new(provider.clone()), merkle_root: MerkleRootVerifier::new(), - uncles: UnclesVerifier::new(provider.clone(), consensus.clone(), pow), - commit: CommitVerifier::new(provider.clone(), consensus), + uncles: UnclesVerifier::new(provider.clone()), + commit: CommitVerifier::new(provider.clone()), transactions: TransactionsVerifier::new(provider), } } @@ -83,7 +80,7 @@ impl Verifier for BlockVerifier

{ } #[derive(Clone)] -pub struct CellbaseVerifier { +pub struct CellbaseVerifier { provider: CP, } @@ -249,29 +246,21 @@ impl<'a, CP: ChainProvider> HeaderResolver for HeaderResolverWrapper<'a, CP> { } // TODO redo uncle verifier, check uncle proposal duplicate -pub struct UnclesVerifier { +pub struct UnclesVerifier { provider: CP, - consensus: Consensus, - pow: Arc, } impl ::std::clone::Clone for UnclesVerifier { fn clone(&self) -> Self { UnclesVerifier { provider: self.provider.clone(), - consensus: self.consensus.clone(), - pow: Arc::clone(&self.pow), } } } impl UnclesVerifier { - pub fn new(provider: CP, consensus: Consensus, pow: Arc) -> Self { - UnclesVerifier { - provider, - consensus, - pow, - } + pub fn new(provider: CP) -> Self { + UnclesVerifier { provider } } // - uncles_hash @@ -296,7 +285,7 @@ impl UnclesVerifier { // verify uncles lenght =< max_uncles_len let uncles_len = block.uncles().len(); - let max_uncles_len = self.consensus.max_uncles_len(); + let max_uncles_len = self.provider.consensus().max_uncles_len(); if uncles_len > max_uncles_len { return Err(Error::Uncles(UnclesError::OverLength { max: max_uncles_len, @@ -305,7 +294,7 @@ impl UnclesVerifier { } // verify uncles age - let max_uncles_age = self.consensus.max_uncles_age(); + let max_uncles_age = self.provider.consensus().max_uncles_age(); for uncle in block.uncles() { let depth = block.header().number().saturating_sub(uncle.number()); @@ -348,11 +337,12 @@ impl UnclesVerifier { } let block_difficulty_epoch = - block.header().number() / self.consensus.difficulty_adjustment_interval(); + block.header().number() / self.provider.consensus().difficulty_adjustment_interval(); for uncle in block.uncles() { let uncle_difficulty_epoch = - uncle.header().number() / self.consensus.difficulty_adjustment_interval(); + uncle.header().number() + / self.provider.consensus().difficulty_adjustment_interval(); if uncle.header().difficulty() != block.header().difficulty() { return Err(Error::Uncles(UnclesError::InvalidDifficulty)); @@ -396,7 +386,12 @@ impl UnclesVerifier { return Err(Error::Uncles(UnclesError::ProposalTransactionDuplicate)); } - if !self.pow.verify_header(&uncle_header) { + if !self + .provider + .consensus() + .pow_engine() + .verify_header(&uncle_header) + { return Err(Error::Uncles(UnclesError::InvalidProof)); } @@ -493,23 +488,19 @@ impl TransactionsVerifier

{ } #[derive(Clone)] -pub struct CommitVerifier { +pub struct CommitVerifier { provider: CP, - consensus: Consensus, } impl CommitVerifier { - pub fn new(provider: CP, consensus: Consensus) -> Self { - CommitVerifier { - provider, - consensus, - } + pub fn new(provider: CP) -> Self { + CommitVerifier { provider } } pub fn verify(&self, block: &Block) -> Result<(), Error> { let block_number = block.header().number(); - let t_prop = self.consensus.transaction_propagation_time; - let mut walk = self.consensus.transaction_propagation_timeout; + let t_prop = self.provider.consensus().transaction_propagation_time; + let mut walk = self.provider.consensus().transaction_propagation_timeout; let start = block_number.saturating_sub(t_prop); if start < 1 { diff --git a/verification/src/lib.rs b/verification/src/lib.rs index 446eed11b0..1b50926b34 100644 --- a/verification/src/lib.rs +++ b/verification/src/lib.rs @@ -11,9 +11,13 @@ extern crate rayon; #[cfg(test)] extern crate ckb_chain as chain; #[cfg(test)] +extern crate ckb_chain_spec as chain_spec; +#[cfg(test)] extern crate ckb_db as db; #[cfg(test)] extern crate ckb_notify as notify; +#[cfg(test)] +extern crate hash; mod block_verifier; mod error; diff --git a/verification/src/tests/block_verifier.rs b/verification/src/tests/block_verifier.rs index bd079babec..a390d907b8 100644 --- a/verification/src/tests/block_verifier.rs +++ b/verification/src/tests/block_verifier.rs @@ -1,9 +1,7 @@ use super::super::block_verifier::{BlockVerifier, CellbaseVerifier, EmptyVerifier}; use super::super::error::{CellbaseError, Error as VerifyError}; use super::dummy::DummyChainProvider; -use super::utils::dummy_pow_engine; use bigint::H256; -use ckb_shared::consensus::Consensus; use ckb_shared::error::SharedError; use core::block::BlockBuilder; use core::transaction::{CellInput, CellOutput, OutPoint, Transaction, TransactionBuilder}; @@ -235,7 +233,6 @@ pub fn test_cellbase_with_two_outputs_and_more_rewards_than_maximum() { #[test] pub fn test_empty_transactions() { let block = BlockBuilder::default().build(); - let consensus = Consensus::default(); let transaction_fees = HashMap::>::new(); let provider = DummyChainProvider { @@ -243,10 +240,8 @@ pub fn test_empty_transactions() { transaction_fees: transaction_fees, }; - let pow = dummy_pow_engine(); - let verifier = EmptyVerifier::new(); - let full_verifier = BlockVerifier::new(provider, consensus, pow); + let full_verifier = BlockVerifier::new(provider); assert_eq!( verifier.verify(&block), Err(VerifyError::CommitTransactionsEmpty) diff --git a/verification/src/tests/commit_verifier.rs b/verification/src/tests/commit_verifier.rs index 8aaefd2ff5..f396785828 100644 --- a/verification/src/tests/commit_verifier.rs +++ b/verification/src/tests/commit_verifier.rs @@ -2,11 +2,12 @@ use super::super::block_verifier::CommitVerifier; use super::super::error::{CommitError, Error}; use bigint::{H256, U256}; use chain::chain::{ChainBuilder, ChainController}; -use ckb_shared::consensus::Consensus; +use chain_spec::consensus::Consensus; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use core::block::{Block, BlockBuilder}; use core::header::{Header, HeaderBuilder}; +use core::script::Script; use core::transaction::{ CellInput, CellOutput, OutPoint, ProposalShortId, Transaction, TransactionBuilder, }; @@ -14,6 +15,8 @@ use core::uncle::UncleBlock; use core::BlockNumber; use db::memorydb::MemoryKeyValueDB; use fnv::FnvHashMap; +use std::fs::File; +use std::io::Read; use std::sync::Arc; fn gen_block( @@ -42,13 +45,30 @@ fn gen_block( .with_header_builder(header_builder) } +fn get_script() -> Script { + let mut file = File::open("../spec/res/cells/always_success").unwrap(); + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer).unwrap(); + + Script::new(0, Vec::new(), None, Some(buffer), Vec::new()) +} + fn create_transaction(parent: H256) -> Transaction { - let mut output = CellOutput::default(); - output.capacity = 100_000_000 / 100 as u64; + let script = get_script(); + let capacity = 100_000_000 / 100 as u64; + let output = CellOutput::new( + capacity, + Vec::new(), + script.type_hash(), + Some(script.clone()), + ); + let inputs: Vec = (0..100) + .map(|index| CellInput::new(OutPoint::new(parent, index), script.clone())) + .collect(); TransactionBuilder::default() - .input(CellInput::new(OutPoint::new(parent, 0), Default::default())) - .outputs(vec![output.clone(); 100]) + .inputs(inputs) + .outputs(vec![output; 100]) .build() } @@ -76,14 +96,15 @@ fn create_cellbase(number: BlockNumber) -> Transaction { #[test] fn test_blank_proposal() { + let script = get_script(); let tx = TransactionBuilder::default() .input(CellInput::new(OutPoint::null(), Default::default())) .outputs(vec![ CellOutput::new( - 100_000_000, + 1_000_000, Vec::new(), - H256::default(), - None, + script.type_hash(), + Some(script), ); 100 ]).build(); @@ -93,51 +114,70 @@ fn test_blank_proposal() { let (chain_controller, shared) = start_chain(Some(consensus)); let mut txs = FnvHashMap::default(); - let end = 21; let mut blocks: Vec = Vec::new(); let mut parent = shared.block_header(&shared.block_hash(0).unwrap()).unwrap(); - for i in 1..end { + let mut prev_txs = Vec::new(); + for i in 1..6 { txs.insert(i, Vec::new()); let tx = create_transaction(root_hash); root_hash = tx.hash(); txs.get_mut(&i).unwrap().push(tx.clone()); - let new_block = gen_block(parent, vec![tx], vec![], vec![]); + let next_proposal_ids = vec![tx.proposal_short_id()]; + let new_block = gen_block(parent, prev_txs, next_proposal_ids, vec![]); + prev_txs = vec![tx]; blocks.push(new_block.clone()); parent = new_block.header().clone(); } - for block in &blocks[0..10] { - assert!( - chain_controller - .process_block(Arc::new(block.clone())) - .is_ok() + for block in &blocks[0..5] { + let result = chain_controller.process_block(Arc::new(block.clone())); + if result.is_err() { + println!("number: {}, result: {:?}", block.header().number(), result); + } + assert!(result.is_ok()); + } + + let blank_proposal_block = gen_block(parent, prev_txs, Vec::new(), Vec::new()); + parent = blank_proposal_block.header().clone(); + let result = chain_controller.process_block(Arc::new(blank_proposal_block.clone())); + if result.is_err() { + println!( + "[blank proposal] number: {}, result: {:?}", + blank_proposal_block.header().number(), + result ); } + assert!(result.is_ok()); - let verifier = CommitVerifier::new(shared.clone(), shared.consensus().clone()); + let tx = create_transaction(root_hash); + let invalid_block = gen_block(parent, vec![tx], Vec::new(), Vec::new()); + let verifier = CommitVerifier::new(shared.clone()); assert_eq!( - verifier.verify(&blocks[10]), + verifier.verify(&invalid_block), Err(Error::Commit(CommitError::Invalid)) ); } #[test] fn test_uncle_proposal() { + let script = get_script(); let tx = TransactionBuilder::default() .input(CellInput::new(OutPoint::null(), Default::default())) .outputs(vec![ CellOutput::new( - 100_000_000, + 1_000_000, Vec::new(), - H256::default(), - None, + script.type_hash(), + Some(script), ); 100 ]).build(); let mut root_hash = tx.hash(); let genesis_block = BlockBuilder::default().commit_transaction(tx).build(); - let consensus = Consensus::default().set_genesis_block(genesis_block); + let consensus = Consensus::default() + .set_genesis_block(genesis_block) + .set_verification(false); let (chain_controller, shared) = start_chain(Some(consensus)); let mut txs = Vec::new(); @@ -151,19 +191,43 @@ fn test_uncle_proposal() { } let proposal_ids: Vec<_> = txs.iter().map(|tx| tx.proposal_short_id()).collect(); - let uncle = gen_block(parent.clone(), vec![], proposal_ids, vec![]).into(); - let block = gen_block(parent.clone(), vec![], vec![], vec![uncle]); - - assert!( - chain_controller - .process_block(Arc::new(block.clone())) - .is_ok() - ); - - parent = block.header().clone(); + let uncle: Block = gen_block(parent.clone(), vec![], proposal_ids, vec![]).into(); + let result = chain_controller.process_block(Arc::new(uncle.clone())); + if result.is_err() { + println!( + "[uncle] number: {}, result: {:?}", + uncle.header().number(), + result + ); + } + assert!(result.is_ok()); + + let block1 = gen_block(parent.clone(), vec![], vec![], vec![]); + parent = block1.header().clone(); + let result = chain_controller.process_block(Arc::new(block1.clone())); + if result.is_err() { + println!( + "[block1] number: {}, result: {:?}", + block1.header().number(), + result + ); + } + assert!(result.is_ok()); + + let block2 = gen_block(parent.clone(), vec![], vec![], vec![uncle.into()]); + let result = chain_controller.process_block(Arc::new(block2.clone())); + if result.is_err() { + println!( + "[block2] number: {}, result: {:?}", + block2.header().number(), + result + ); + } + assert!(result.is_ok()); + parent = block2.header().clone(); let new_block = gen_block(parent, txs, vec![], vec![]); - let verifier = CommitVerifier::new(shared.clone(), shared.consensus().clone()); + let verifier = CommitVerifier::new(shared.clone()); assert_eq!(verifier.verify(&new_block), Ok(())); } @@ -207,7 +271,7 @@ fn test_block_proposal() { parent = block.header().clone(); let new_block = gen_block(parent, txs, vec![], vec![]); - let verifier = CommitVerifier::new(shared.clone(), shared.consensus().clone()); + let verifier = CommitVerifier::new(shared.clone()); assert_eq!(verifier.verify(&new_block), Ok(())); } @@ -260,7 +324,7 @@ fn test_proposal_timeout() { parent = block.header().clone(); } - let verifier = CommitVerifier::new(shared.clone(), shared.consensus().clone()); + let verifier = CommitVerifier::new(shared.clone()); let new_block = gen_block(parent.clone(), txs.clone(), vec![], vec![]); assert_eq!(verifier.verify(&new_block), Ok(())); diff --git a/verification/src/tests/dummy.rs b/verification/src/tests/dummy.rs index 9856594ab0..ef57a912ea 100644 --- a/verification/src/tests/dummy.rs +++ b/verification/src/tests/dummy.rs @@ -1,4 +1,5 @@ use bigint::{H256, U256}; +use chain_spec::consensus::Consensus; use ckb_shared::error::SharedError; use ckb_shared::shared::ChainProvider; use core::block::Block; @@ -95,6 +96,10 @@ impl ChainProvider for DummyChainProvider { fn calculate_difficulty(&self, _last: &Header) -> Option { panic!("Not implemented!"); } + + fn consensus(&self) -> &Consensus { + panic!("Not implemented!"); + } } impl CellProvider for DummyChainProvider { diff --git a/verification/src/tests/mod.rs b/verification/src/tests/mod.rs index f7e445152d..b2b54aef07 100644 --- a/verification/src/tests/mod.rs +++ b/verification/src/tests/mod.rs @@ -1,8 +1,5 @@ -extern crate ckb_util as util; - mod block_verifier; mod commit_verifier; mod dummy; mod transaction_verifier; mod uncle_verifier; -mod utils; diff --git a/verification/src/tests/uncle_verifier.rs b/verification/src/tests/uncle_verifier.rs index e123451c2c..f3562edc80 100644 --- a/verification/src/tests/uncle_verifier.rs +++ b/verification/src/tests/uncle_verifier.rs @@ -1,9 +1,8 @@ use super::super::block_verifier::UnclesVerifier; use super::super::error::{Error, UnclesError}; -use super::utils::dummy_pow_engine; use bigint::{H256, U256}; use chain::chain::{ChainBuilder, ChainController}; -use ckb_shared::consensus::Consensus; +use chain_spec::consensus::Consensus; use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder}; use ckb_shared::store::ChainKVStore; use core::block::{Block, BlockBuilder}; @@ -63,7 +62,6 @@ fn test_uncle_verifier() { let (chain_controller, shared) = start_chain(Some(consensus)); assert_eq!(shared.consensus().difficulty_adjustment_interval(), 10); - let pow = dummy_pow_engine(); let number = 20; let mut chain1: Vec = Vec::new(); let mut chain2: Vec = Vec::new(); @@ -94,8 +92,7 @@ fn test_uncle_verifier() { parent = new_block.header().clone(); } - let verifier = - UnclesVerifier::new(shared.clone(), shared.consensus().clone(), Arc::clone(&pow)); + let verifier = UnclesVerifier::new(shared.clone()); let block = BlockBuilder::default() .block(chain1.last().cloned().unwrap()) diff --git a/verification/src/tests/utils.rs b/verification/src/tests/utils.rs deleted file mode 100644 index 09ea8b8f19..0000000000 --- a/verification/src/tests/utils.rs +++ /dev/null @@ -1,6 +0,0 @@ -use pow::{DummyPowEngine, PowEngine}; -use std::sync::Arc; - -pub fn dummy_pow_engine() -> Arc { - Arc::new(DummyPowEngine::new()) -}