Skip to content

Commit

Permalink
Merge pull request nervosnetwork#450 from nervosnetwork/quake/index_s…
Browse files Browse the repository at this point in the history
…tore_trait_merge

refactor: merge ChainIndex and ChainStore trait
  • Loading branch information
quake committed Apr 14, 2019
2 parents ac79f50 + 7662ee0 commit 0db3859
Show file tree
Hide file tree
Showing 35 changed files with 314 additions and 368 deletions.
37 changes: 18 additions & 19 deletions chain/src/chain.rs
Expand Up @@ -11,9 +11,8 @@ use ckb_notify::NotifyController;
use ckb_shared::cell_set::CellSetDiff;
use ckb_shared::chain_state::ChainState;
use ckb_shared::error::SharedError;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::Shared;
use ckb_shared::store::StoreBatch;
use ckb_shared::store::{ChainStore, StoreBatch};
use ckb_traits::{BlockMedianTimeContext, ChainProvider};
use ckb_verification::{BlockVerifier, TransactionsVerifier, Verifier};
use crossbeam_channel::{self, select, Receiver, Sender};
Expand Down Expand Up @@ -98,13 +97,13 @@ impl GlobalIndex {
}

// Verification context for fork
struct ForkContext<'a, CI> {
struct ForkContext<'a, CS> {
pub fork_blocks: &'a [Block],
pub store: Arc<CI>,
pub store: Arc<CS>,
pub consensus: &'a Consensus,
}

impl<'a, CI: ChainIndex> ForkContext<'a, CI> {
impl<'a, CS: ChainStore> ForkContext<'a, CS> {
fn get_header(&self, number: BlockNumber) -> Option<Header> {
match self
.fork_blocks
Expand All @@ -120,7 +119,7 @@ impl<'a, CI: ChainIndex> ForkContext<'a, CI> {
}
}

impl<'a, CI: ChainIndex> BlockMedianTimeContext for ForkContext<'a, CI> {
impl<'a, CS: ChainStore> BlockMedianTimeContext for ForkContext<'a, CS> {
fn median_block_count(&self) -> u64 {
self.consensus.median_time_block_count() as u64
}
Expand All @@ -130,18 +129,18 @@ impl<'a, CI: ChainIndex> BlockMedianTimeContext for ForkContext<'a, CI> {
}
}

pub struct ChainService<CI> {
shared: Shared<CI>,
pub struct ChainService<CS> {
shared: Shared<CS>,
notify: NotifyController,
verification: bool,
}

impl<CI: ChainIndex + 'static> ChainService<CI> {
impl<CS: ChainStore + 'static> ChainService<CS> {
pub fn new(
shared: Shared<CI>,
shared: Shared<CS>,
notify: NotifyController,
verification: bool,
) -> ChainService<CI> {
) -> ChainService<CS> {
ChainService {
shared,
notify,
Expand Down Expand Up @@ -288,7 +287,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
Ok(())
}

pub(crate) fn update_proposal_ids(&self, chain_state: &mut ChainState<CI>, fork: &ForkChanges) {
pub(crate) fn update_proposal_ids(&self, chain_state: &mut ChainState<CS>, fork: &ForkChanges) {
for blk in fork.attached_blocks() {
chain_state.update_proposal_ids(&blk);
}
Expand Down Expand Up @@ -426,7 +425,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
&self,
batch: &mut StoreBatch,
fork: &mut ForkChanges,
chain_state: &mut ChainState<CI>,
chain_state: &mut ChainState<CS>,
) -> Result<CellSetDiff, FailureError> {
let mut cell_set_diff = CellSetDiff::default();

Expand Down Expand Up @@ -516,7 +515,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
}

// TODO: beatify
fn print_chain(&self, chain_state: &ChainState<CI>, len: u64) {
fn print_chain(&self, chain_state: &ChainState<CS>, len: u64) {
debug!(target: "chain", "Chain {{");

let tip = chain_state.tip_number();
Expand Down Expand Up @@ -568,14 +567,14 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
}
}

pub struct ChainBuilder<CI> {
shared: Shared<CI>,
pub struct ChainBuilder<CS> {
shared: Shared<CS>,
notify: NotifyController,
verification: bool,
}

impl<CI: ChainIndex + 'static> ChainBuilder<CI> {
pub fn new(shared: Shared<CI>, notify: NotifyController) -> ChainBuilder<CI> {
impl<CS: ChainStore + 'static> ChainBuilder<CS> {
pub fn new(shared: Shared<CS>, notify: NotifyController) -> ChainBuilder<CS> {
ChainBuilder {
shared,
notify,
Expand All @@ -593,7 +592,7 @@ impl<CI: ChainIndex + 'static> ChainBuilder<CI> {
self
}

pub fn build(self) -> ChainService<CI> {
pub fn build(self) -> ChainService<CS> {
ChainService::new(self.shared, self.notify, self.verification)
}
}
12 changes: 6 additions & 6 deletions core/src/difficulty.rs
@@ -1,23 +1,23 @@
#![allow(clippy::op_ref)]

use numext_fixed_hash::H256;
use numext_fixed_uint::U256;

const ONE: U256 = U256::one();

/// f(x) = 2^256 / x
pub fn boundary_to_difficulty(boundary: &H256) -> U256 {
let d: U256 = boundary.into();
if d <= U256::one() {
if d.le(&ONE) {
U256::max_value()
} else {
((U256::one() << 255) / d) << 1
((ONE << 255) / d) << 1
}
}

pub fn difficulty_to_boundary(difficulty: &U256) -> H256 {
if difficulty <= &U256::one() {
if difficulty.le(&ONE) {
U256::max_value().into()
} else {
let t = U256::one() << 255;
let t = ONE << 255;
let boundary = (&t / difficulty) << 1u8;
boundary.into()
}
Expand Down
19 changes: 9 additions & 10 deletions miner/src/block_assembler.rs
Expand Up @@ -7,7 +7,7 @@ use ckb_core::transaction::{CellInput, CellOutput, Transaction, TransactionBuild
use ckb_core::uncle::UncleBlock;
use ckb_core::{Cycle, Version};
use ckb_notify::NotifyController;
use ckb_shared::{index::ChainIndex, shared::Shared, tx_pool::PoolEntry};
use ckb_shared::{shared::Shared, store::ChainStore, tx_pool::PoolEntry};
use ckb_traits::ChainProvider;
use ckb_util::Mutex;
use crossbeam_channel::{self, select, Receiver, Sender};
Expand Down Expand Up @@ -84,17 +84,17 @@ impl BlockAssemblerController {
}
}

pub struct BlockAssembler<CI> {
shared: Shared<CI>,
pub struct BlockAssembler<CS> {
shared: Shared<CS>,
candidate_uncles: LruCache<H256, Arc<Block>>,
config: BlockAssemblerConfig,
work_id: AtomicUsize,
last_uncles_updated_at: AtomicU64,
template_caches: Mutex<LruCache<(Cycle, u64, Version), TemplateCache>>,
}

impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
pub fn new(shared: Shared<CI>, config: BlockAssemblerConfig) -> Self {
impl<CS: ChainStore + 'static> BlockAssembler<CS> {
pub fn new(shared: Shared<CS>, config: BlockAssemblerConfig) -> Self {
Self {
shared,
config,
Expand Down Expand Up @@ -430,10 +430,9 @@ mod tests {
use ckb_db::memorydb::MemoryKeyValueDB;
use ckb_notify::{NotifyController, NotifyService};
use ckb_pow::Pow;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::Shared;
use ckb_shared::shared::SharedBuilder;
use ckb_shared::store::ChainKVStore;
use ckb_shared::store::{ChainKVStore, ChainStore};
use ckb_traits::ChainProvider;
use ckb_verification::{BlockVerifier, HeaderResolverWrapper, HeaderVerifier, Verifier};
use jsonrpc_types::{BlockTemplate, CellbaseTemplate};
Expand Down Expand Up @@ -464,10 +463,10 @@ mod tests {
(chain_controller, shared, notify)
}

fn setup_block_assembler<CI: ChainIndex + 'static>(
shared: Shared<CI>,
fn setup_block_assembler<CS: ChainStore + 'static>(
shared: Shared<CS>,
config: BlockAssemblerConfig,
) -> BlockAssembler<CI> {
) -> BlockAssembler<CS> {
BlockAssembler::new(shared, config)
}

Expand Down
2 changes: 1 addition & 1 deletion network/src/network.rs
Expand Up @@ -451,7 +451,7 @@ pub struct NetworkService {
p2p_service: Service<EventHandler>,
network_state: Arc<NetworkState>,
// Background services
bg_services: Vec<Box<dyn Future<Item = (), Error = ()> + Send + 'static>>,
bg_services: Vec<Box<dyn Future<Item = (), Error = ()> + Send>>,
}

impl NetworkService {
Expand Down
3 changes: 1 addition & 2 deletions pow/src/lib.rs
Expand Up @@ -38,10 +38,9 @@ fn pow_message(pow_hash: &[u8], nonce: u64) -> [u8; 40] {
pub trait PowEngine: Send + Sync {
fn init(&self, number: BlockNumber);

#[allow(clippy::op_ref)]
fn verify_header(&self, header: &Header) -> bool {
let proof_hash: H256 = blake2b_256(&header.proof()).into();
if &boundary_to_difficulty(&proof_hash) < header.difficulty() {
if boundary_to_difficulty(&proof_hash).lt(header.difficulty()) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions rpc/src/module/chain.rs
@@ -1,6 +1,6 @@
use ckb_core::cell::CellProvider;
use ckb_core::BlockNumber;
use ckb_shared::{index::ChainIndex, shared::Shared};
use ckb_shared::{shared::Shared, store::ChainStore};
use ckb_traits::ChainProvider;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
Expand Down Expand Up @@ -37,11 +37,11 @@ pub trait ChainRpc {
fn get_tip_block_number(&self) -> Result<String>;
}

pub(crate) struct ChainRpcImpl<CI> {
pub shared: Shared<CI>,
pub(crate) struct ChainRpcImpl<CS> {
pub shared: Shared<CS>,
}

impl<CI: ChainIndex + 'static> ChainRpc for ChainRpcImpl<CI> {
impl<CS: ChainStore + 'static> ChainRpc for ChainRpcImpl<CS> {
fn get_block(&self, hash: H256) -> Result<Option<Block>> {
Ok(self.shared.block(&hash).as_ref().map(Into::into))
}
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/module/miner.rs
Expand Up @@ -4,7 +4,7 @@ use ckb_core::Cycle;
use ckb_miner::BlockAssemblerController;
use ckb_network::{NetworkController, ProtocolId};
use ckb_protocol::RelayMessage;
use ckb_shared::{index::ChainIndex, shared::Shared};
use ckb_shared::{shared::Shared, store::ChainStore};
use ckb_sync::NetworkProtocol;
use ckb_traits::ChainProvider;
use ckb_verification::{HeaderResolverWrapper, HeaderVerifier, Verifier};
Expand Down Expand Up @@ -34,14 +34,14 @@ pub trait MinerRpc {
fn submit_block(&self, _work_id: String, _data: Block) -> Result<Option<H256>>;
}

pub(crate) struct MinerRpcImpl<CI> {
pub(crate) struct MinerRpcImpl<CS> {
pub network_controller: NetworkController,
pub shared: Shared<CI>,
pub shared: Shared<CS>,
pub block_assembler: BlockAssemblerController,
pub chain: ChainController,
}

impl<CI: ChainIndex + 'static> MinerRpc for MinerRpcImpl<CI> {
impl<CS: ChainStore + 'static> MinerRpc for MinerRpcImpl<CS> {
fn get_block_template(
&self,
cycles_limit: Option<String>,
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/module/pool.rs
Expand Up @@ -2,8 +2,8 @@ use crate::error::RPCError;
use ckb_core::transaction::{ProposalShortId, Transaction as CoreTransaction};
use ckb_network::{NetworkController, ProtocolId};
use ckb_protocol::RelayMessage;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::Shared;
use ckb_shared::store::ChainStore;
use ckb_shared::tx_pool::types::PoolEntry;
use ckb_sync::NetworkProtocol;
use ckb_traits::chain_provider::ChainProvider;
Expand All @@ -27,12 +27,12 @@ pub trait PoolRpc {
fn get_pool_transaction(&self, _hash: H256) -> Result<Option<Transaction>>;
}

pub(crate) struct PoolRpcImpl<CI> {
pub(crate) struct PoolRpcImpl<CS> {
pub network_controller: NetworkController,
pub shared: Shared<CI>,
pub shared: Shared<CS>,
}

impl<CI: ChainIndex + 'static> PoolRpc for PoolRpcImpl<CI> {
impl<CS: ChainStore + 'static> PoolRpc for PoolRpcImpl<CS> {
fn send_transaction(&self, tx: Transaction) -> Result<H256> {
let tx: CoreTransaction = tx.try_into().map_err(|_| Error::parse_error())?;
let tx_hash = tx.hash().clone();
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/module/trace.rs
Expand Up @@ -2,8 +2,8 @@ use crate::error::RPCError;
use ckb_core::transaction::Transaction as CoreTransaction;
use ckb_network::{NetworkController, ProtocolId};
use ckb_protocol::RelayMessage;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::Shared;
use ckb_shared::store::ChainStore;
use ckb_shared::tx_pool::types::PoolEntry;
use ckb_sync::NetworkProtocol;
use ckb_traits::chain_provider::ChainProvider;
Expand All @@ -25,12 +25,12 @@ pub trait TraceRpc {
fn get_transaction_trace(&self, _hash: H256) -> Result<Option<Vec<TxTrace>>>;
}

pub(crate) struct TraceRpcImpl<CI> {
pub(crate) struct TraceRpcImpl<CS> {
pub network_controller: NetworkController,
pub shared: Shared<CI>,
pub shared: Shared<CS>,
}

impl<CI: ChainIndex + 'static> TraceRpc for TraceRpcImpl<CI> {
impl<CS: ChainStore + 'static> TraceRpc for TraceRpcImpl<CS> {
fn trace_transaction(&self, tx: Transaction) -> Result<H256> {
let tx: CoreTransaction = tx.try_into().map_err(|_| Error::parse_error())?;
let tx_hash = tx.hash().clone();
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/server.rs
Expand Up @@ -6,8 +6,8 @@ use crate::module::{
use ckb_chain::chain::ChainController;
use ckb_miner::BlockAssemblerController;
use ckb_network::NetworkController;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::Shared;
use ckb_shared::store::ChainStore;
use jsonrpc_core::IoHandler;
use jsonrpc_http_server::{Server, ServerBuilder};
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
Expand All @@ -18,15 +18,15 @@ pub struct RpcServer {
}

impl RpcServer {
pub fn new<CI: ChainIndex + 'static>(
pub fn new<CS: ChainStore + 'static>(
config: Config,
network_controller: NetworkController,
shared: Shared<CI>,
shared: Shared<CS>,
chain: ChainController,
block_assembler: BlockAssemblerController,
) -> RpcServer
where
CI: ChainIndex,
CS: ChainStore,
{
let mut io = IoHandler::new();

Expand Down

0 comments on commit 0db3859

Please sign in to comment.