Skip to content

Commit

Permalink
refactor: Service arch follow-up
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWaWaR authored and doitian committed Nov 19, 2018
1 parent c01efd3 commit 5032495
Show file tree
Hide file tree
Showing 48 changed files with 341 additions and 528 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
46 changes: 30 additions & 16 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use bigint::H256;
use chain_spec::consensus::Consensus;
use channel::{self, Receiver, Sender};
use ckb_notify::{ForkBlocks, NotifyController, NotifyService};
use core::block::Block;
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};
use std::cmp;
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use time::now_ms;
use verification::{BlockVerifier, Verifier};

pub struct ChainService<CI> {
shared: Shared<CI>,
Expand All @@ -23,11 +25,11 @@ pub struct ChainService<CI> {

#[derive(Clone)]
pub struct ChainController {
process_block_sender: Sender<Request<Arc<Block>, Result<(), SharedError>>>,
process_block_sender: Sender<Request<Arc<Block>, Result<(), ProcessBlockError>>>,
}

pub struct ChainReceivers {
process_block_receiver: Receiver<Request<Arc<Block>, Result<(), SharedError>>>,
process_block_receiver: Receiver<Request<Arc<Block>, Result<(), ProcessBlockError>>>,
}

impl ChainController {
Expand All @@ -43,7 +45,7 @@ impl ChainController {
)
}

pub fn process_block(&self, block: Arc<Block>) -> Result<(), SharedError> {
pub fn process_block(&self, block: Arc<Block>) -> Result<(), ProcessBlockError> {
Request::call(&self.process_block_sender, block).expect("process_block() failed")
}
}
Expand Down Expand Up @@ -85,9 +87,16 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
}).expect("Start ChainService failed")
}

fn process_block(&mut self, block: Arc<Block>) -> Result<(), SharedError> {
fn process_block(&mut self, block: Arc<Block>) -> 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(())
Expand Down Expand Up @@ -343,8 +352,8 @@ impl<CI: ChainIndex + 'static> ChainBuilder<CI> {
self
}

pub fn build(self) -> ChainService<CI> {
let notify = self.notify.unwrap_or_else(|| {
pub fn build(mut self) -> ChainService<CI> {
let notify = self.notify.take().unwrap_or_else(|| {
// FIXME: notify should not be optional
let (_handle, notify) = NotifyService::default().start::<&str>(None);
notify
Expand All @@ -371,11 +380,10 @@ pub mod test {
fn start_chain(
consensus: Option<Consensus>,
) -> (ChainController, Shared<ChainKVStore<MemoryKeyValueDB>>) {
let mut builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::new_memory();
if let Some(consensus) = consensus {
builder = builder.consensus(consensus);
}
let shared = builder.build();
let builder = SharedBuilder::<ChainKVStore<MemoryKeyValueDB>>::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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions chain/src/error.rs
Original file line number Diff line number Diff line change
@@ -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),
}
3 changes: 3 additions & 0 deletions chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -24,3 +26,4 @@ extern crate rand;
extern crate tempfile;

pub mod chain;
pub mod error;
5 changes: 0 additions & 5 deletions miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Loading

0 comments on commit 5032495

Please sign in to comment.