Skip to content

Commit

Permalink
refactor: remove static bound for ChainIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
quake committed Apr 10, 2019
1 parent f37264b commit 89bfb95
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 70 deletions.
7 changes: 2 additions & 5 deletions sync/src/relayer/block_proposal_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ use ckb_traits::chain_provider::ChainProvider;
use ckb_util::TryInto;
use failure::Error as FailureError;

pub struct BlockProposalProcess<'a, CI: ChainIndex + 'a> {
pub struct BlockProposalProcess<'a, CI> {
message: &'a BlockProposal<'a>,
relayer: &'a Relayer<CI>,
}

impl<'a, CI> BlockProposalProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> BlockProposalProcess<'a, CI> {
pub fn new(message: &'a BlockProposal, relayer: &'a Relayer<CI>) -> Self {
BlockProposalProcess { message, relayer }
}
Expand Down
7 changes: 2 additions & 5 deletions sync/src/relayer/block_transactions_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ use ckb_util::TryInto;
use failure::Error as FailureError;
use std::sync::Arc;

pub struct BlockTransactionsProcess<'a, CI: ChainIndex + 'a> {
pub struct BlockTransactionsProcess<'a, CI> {
message: &'a BlockTransactions<'a>,
relayer: &'a Relayer<CI>,
peer: PeerIndex,
nc: &'a mut CKBProtocolContext,
}

impl<'a, CI> BlockTransactionsProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> BlockTransactionsProcess<'a, CI> {
pub fn new(
message: &'a BlockTransactions,
relayer: &'a Relayer<CI>,
Expand Down
29 changes: 4 additions & 25 deletions sync/src/relayer/compact_block_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ use fnv::FnvHashMap;
use numext_fixed_hash::H256;
use std::sync::Arc;

pub struct CompactBlockProcess<'a, CI: ChainIndex + 'a> {
pub struct CompactBlockProcess<'a, CI> {
message: &'a FbsCompactBlock<'a>,
relayer: &'a Relayer<CI>,
peer: PeerIndex,
nc: &'a mut CKBProtocolContext,
}

impl<'a, CI> CompactBlockProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> CompactBlockProcess<'a, CI> {
pub fn new(
message: &'a FbsCompactBlock,
relayer: &'a Relayer<CI>,
Expand Down Expand Up @@ -100,30 +97,12 @@ where
}
}

struct CompactBlockMedianTimeView<'a, CI>
where
CI: ChainIndex + 'static,
{
struct CompactBlockMedianTimeView<'a, CI> {
pending_compact_blocks: &'a FnvHashMap<H256, CompactBlock>,
shared: &'a Shared<CI>,
}

impl<'a, CI> ::std::clone::Clone for CompactBlockMedianTimeView<'a, CI>
where
CI: ChainIndex + 'static,
{
fn clone(&self) -> Self {
CompactBlockMedianTimeView {
pending_compact_blocks: self.pending_compact_blocks,
shared: self.shared,
}
}
}

impl<'a, CI> BlockMedianTimeContext for CompactBlockMedianTimeView<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> BlockMedianTimeContext for CompactBlockMedianTimeView<'a, CI> {
fn block_count(&self) -> u32 {
self.shared.consensus().median_time_block_count() as u32
}
Expand Down
7 changes: 2 additions & 5 deletions sync/src/relayer/get_block_proposal_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ use ckb_util::TryInto;
use failure::Error as FailureError;
use flatbuffers::FlatBufferBuilder;

pub struct GetBlockProposalProcess<'a, CI: ChainIndex + 'a> {
pub struct GetBlockProposalProcess<'a, CI> {
message: &'a GetBlockProposal<'a>,
relayer: &'a Relayer<CI>,
peer: PeerIndex,
nc: &'a mut CKBProtocolContext,
}

impl<'a, CI> GetBlockProposalProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> GetBlockProposalProcess<'a, CI> {
pub fn new(
message: &'a GetBlockProposal,
relayer: &'a Relayer<CI>,
Expand Down
7 changes: 2 additions & 5 deletions sync/src/relayer/get_block_transactions_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ use failure::Error as FailureError;
use flatbuffers::FlatBufferBuilder;
use log::debug;

pub struct GetBlockTransactionsProcess<'a, CI: ChainIndex + 'a> {
pub struct GetBlockTransactionsProcess<'a, CI> {
message: &'a GetBlockTransactions<'a>,
relayer: &'a Relayer<CI>,
peer: PeerIndex,
nc: &'a mut CKBProtocolContext,
}

impl<'a, CI> GetBlockTransactionsProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> GetBlockTransactionsProcess<'a, CI> {
pub fn new(
message: &'a GetBlockTransactions,
relayer: &'a Relayer<CI>,
Expand Down
13 changes: 3 additions & 10 deletions sync/src/relayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ use std::time::Duration;

pub const TX_PROPOSAL_TOKEN: u64 = 0;

#[derive(Clone)]
pub struct Relayer<CI: ChainIndex> {
pub struct Relayer<CI> {
chain: ChainController,
pub(crate) shared: Shared<CI>,
state: Arc<RelayState>,
// TODO refactor shared Peers struct with Synchronizer
peers: Arc<Peers>,
}

impl<CI> Relayer<CI>
where
CI: ChainIndex + 'static,
{
impl<CI: ChainIndex> Relayer<CI> {
pub fn new(chain: ChainController, shared: Shared<CI>, peers: Arc<Peers>) -> Self {
Relayer {
chain,
Expand Down Expand Up @@ -292,10 +288,7 @@ where
}
}

impl<CI> CKBProtocolHandler for Relayer<CI>
where
CI: ChainIndex + 'static,
{
impl<CI: ChainIndex> CKBProtocolHandler for Relayer<CI> {
fn initialize(&self, nc: Box<CKBProtocolContext>) {
nc.register_timer(Duration::from_millis(100), TX_PROPOSAL_TOKEN);
}
Expand Down
7 changes: 2 additions & 5 deletions sync/src/relayer/transaction_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ use std::time::Duration;

const DEFAULT_BAN_TIME: Duration = Duration::from_secs(3600 * 24 * 3);

pub struct TransactionProcess<'a, CI: ChainIndex + 'a> {
pub struct TransactionProcess<'a, CI> {
message: &'a FbsValidTransaction<'a>,
relayer: &'a Relayer<CI>,
peer: PeerIndex,
nc: &'a mut CKBProtocolContext,
}

impl<'a, CI> TransactionProcess<'a, CI>
where
CI: ChainIndex + 'static,
{
impl<'a, CI: ChainIndex> TransactionProcess<'a, CI> {
pub fn new(
message: &'a FbsValidTransaction,
relayer: &'a Relayer<CI>,
Expand Down
7 changes: 2 additions & 5 deletions sync/src/synchronizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,7 @@ impl<CI: ChainIndex> Synchronizer<CI> {
}
}

impl<CI> CKBProtocolHandler for Synchronizer<CI>
where
CI: ChainIndex + 'static,
{
impl<CI: ChainIndex> CKBProtocolHandler for Synchronizer<CI> {
fn initialize(&self, nc: Box<CKBProtocolContext>) {
// NOTE: 100ms is what bitcoin use.
nc.register_timer(Duration::from_millis(1000), SEND_GET_HEADERS_TOKEN);
Expand Down Expand Up @@ -812,7 +809,7 @@ mod tests {
(chain_controller, shared, notify)
}

fn gen_synchronizer<CI: ChainIndex + 'static>(
fn gen_synchronizer<CI: ChainIndex>(
chain_controller: ChainController,
shared: Shared<CI>,
) -> Synchronizer<CI> {
Expand Down
10 changes: 5 additions & 5 deletions verification/src/header_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct HeaderVerifier<T, M> {
_phantom: PhantomData<T>,
}

impl<T, M: BlockMedianTimeContext + Clone> HeaderVerifier<T, M> {
impl<T, M: BlockMedianTimeContext> HeaderVerifier<T, M> {
pub fn new(block_median_time_context: M, pow: Arc<dyn PowEngine>) -> Self {
HeaderVerifier {
pow,
Expand All @@ -33,7 +33,7 @@ impl<T, M: BlockMedianTimeContext + Clone> HeaderVerifier<T, M> {
}
}

impl<T: HeaderResolver, M: BlockMedianTimeContext + Clone> Verifier for HeaderVerifier<T, M> {
impl<T: HeaderResolver, M: BlockMedianTimeContext> Verifier for HeaderVerifier<T, M> {
type Target = T;
fn verify(&self, target: &T) -> Result<(), Error> {
let header = target.header();
Expand All @@ -44,7 +44,7 @@ impl<T: HeaderResolver, M: BlockMedianTimeContext + Clone> Verifier for HeaderVe
.parent()
.ok_or_else(|| Error::UnknownParent(header.parent_hash().clone()))?;
NumberVerifier::new(parent, header).verify()?;
TimestampVerifier::new(self.block_median_time_context.clone(), header).verify()?;
TimestampVerifier::new(&self.block_median_time_context, header).verify()?;
DifficultyVerifier::verify(target)?;
Ok(())
}
Expand All @@ -69,12 +69,12 @@ impl<'a> VersionVerifier<'a> {

pub struct TimestampVerifier<'a, M> {
header: &'a Header,
block_median_time_context: M,
block_median_time_context: &'a M,
now: u64,
}

impl<'a, M: BlockMedianTimeContext> TimestampVerifier<'a, M> {
pub fn new(block_median_time_context: M, header: &'a Header) -> Self {
pub fn new(block_median_time_context: &'a M, header: &'a Header) -> Self {
TimestampVerifier {
block_median_time_context,
header,
Expand Down

0 comments on commit 89bfb95

Please sign in to comment.