diff --git a/sync/src/relayer/mod.rs b/sync/src/relayer/mod.rs index 80c5695290..bfcdc28dfa 100644 --- a/sync/src/relayer/mod.rs +++ b/sync/src/relayer/mod.rs @@ -21,7 +21,7 @@ use self::get_transactions_process::GetTransactionsProcess; use self::transaction_hashes_process::TransactionHashesProcess; use self::transactions_process::TransactionsProcess; use crate::block_status::BlockStatus; -use crate::types::{ActiveChain, BlockNumberAndHash, SyncShared}; +use crate::types::{ActiveChain, SyncShared}; use crate::utils::{ is_internal_db_error, metric_ckb_message_bytes, send_message_to, MetricDirection, }; @@ -35,6 +35,7 @@ use ckb_network::{ }; use ckb_systemtime::unix_time_as_millis; use ckb_tx_pool::service::TxVerificationResult; +use ckb_types::BlockNumberAndHash; use ckb_types::{ core::{self, BlockView}, packed::{self, Byte32, ProposalShortId}, diff --git a/sync/src/synchronizer/block_fetcher.rs b/sync/src/synchronizer/block_fetcher.rs index 5ba4fcee8e..d7c1094864 100644 --- a/sync/src/synchronizer/block_fetcher.rs +++ b/sync/src/synchronizer/block_fetcher.rs @@ -1,5 +1,5 @@ use crate::block_status::BlockStatus; -use crate::types::{ActiveChain, BlockNumberAndHash, HeaderIndex, HeaderIndexView, IBDState}; +use crate::types::{ActiveChain, HeaderIndex, HeaderIndexView, IBDState}; use crate::SyncShared; use ckb_constant::sync::{ BLOCK_DOWNLOAD_WINDOW, CHECK_POINT_WINDOW, INIT_BLOCKS_IN_TRANSIT_PER_PEER, @@ -9,6 +9,7 @@ use ckb_logger::{debug, trace}; use ckb_network::PeerIndex; use ckb_systemtime::unix_time_as_millis; use ckb_types::packed; +use ckb_types::BlockNumberAndHash; use std::cmp::min; use std::sync::Arc; diff --git a/sync/src/tests/inflight_blocks.rs b/sync/src/tests/inflight_blocks.rs index 46e6f45437..4511a85821 100644 --- a/sync/src/tests/inflight_blocks.rs +++ b/sync/src/tests/inflight_blocks.rs @@ -1,7 +1,8 @@ -use crate::types::{BlockNumberAndHash, InflightBlocks}; +use crate::types::InflightBlocks; use ckb_constant::sync::BLOCK_DOWNLOAD_TIMEOUT; use ckb_types::h256; use ckb_types::prelude::*; +use ckb_types::BlockNumberAndHash; use std::collections::HashSet; #[test] diff --git a/sync/src/types/mod.rs b/sync/src/types/mod.rs index c444f870fa..e634c5f838 100644 --- a/sync/src/types/mod.rs +++ b/sync/src/types/mod.rs @@ -21,6 +21,7 @@ use ckb_store::{ChainDB, ChainStore}; use ckb_systemtime::unix_time_as_millis; use ckb_traits::{HeaderFields, HeaderFieldsProvider}; use ckb_tx_pool::service::TxVerificationResult; +use ckb_types::BlockNumberAndHash; use ckb_types::{ core::{self, BlockNumber, EpochExt}, packed::{self, Byte32}, @@ -402,53 +403,6 @@ impl InflightState { } } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BlockNumberAndHash { - pub number: BlockNumber, - pub hash: Byte32, -} - -impl BlockNumberAndHash { - pub fn new(number: BlockNumber, hash: Byte32) -> Self { - Self { number, hash } - } - - pub fn number(&self) -> BlockNumber { - self.number - } - - pub fn hash(&self) -> Byte32 { - self.hash.clone() - } -} - -impl From<(BlockNumber, Byte32)> for BlockNumberAndHash { - fn from(inner: (BlockNumber, Byte32)) -> Self { - Self { - number: inner.0, - hash: inner.1, - } - } -} - -impl From<&core::HeaderView> for BlockNumberAndHash { - fn from(header: &core::HeaderView) -> Self { - Self { - number: header.number(), - hash: header.hash(), - } - } -} - -impl From for BlockNumberAndHash { - fn from(header: core::HeaderView) -> Self { - Self { - number: header.number(), - hash: header.hash(), - } - } -} - enum TimeQuantile { MinToFast, FastToNormal, diff --git a/util/types/src/block_number_and_hash.rs b/util/types/src/block_number_and_hash.rs new file mode 100644 index 0000000000..81097539d0 --- /dev/null +++ b/util/types/src/block_number_and_hash.rs @@ -0,0 +1,54 @@ +//! Block Number and Hash struct +use crate::core::BlockNumber; +use ckb_gen_types::packed::Byte32; + +/// Block Number And Hash struct +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct BlockNumberAndHash { + /// Block Number + pub number: BlockNumber, + /// Block Hash + pub hash: Byte32, +} + +impl BlockNumberAndHash { + /// Create new BlockNumberAndHash + pub fn new(number: BlockNumber, hash: Byte32) -> Self { + Self { number, hash } + } + /// Return BlockNumber + pub fn number(&self) -> BlockNumber { + self.number + } + /// Return Hash + pub fn hash(&self) -> Byte32 { + self.hash.clone() + } +} + +impl From<(BlockNumber, Byte32)> for BlockNumberAndHash { + fn from(inner: (BlockNumber, Byte32)) -> Self { + Self { + number: inner.0, + hash: inner.1, + } + } +} + +impl From<&crate::core::HeaderView> for BlockNumberAndHash { + fn from(header: &crate::core::HeaderView) -> Self { + Self { + number: header.number(), + hash: header.hash(), + } + } +} + +impl From for BlockNumberAndHash { + fn from(header: crate::core::HeaderView) -> Self { + Self { + number: header.number(), + hash: header.hash(), + } + } +} diff --git a/util/types/src/lib.rs b/util/types/src/lib.rs index 6b559dd614..4fcbd9fe2a 100644 --- a/util/types/src/lib.rs +++ b/util/types/src/lib.rs @@ -4,12 +4,12 @@ pub mod prelude; +pub use block_number_and_hash::BlockNumberAndHash; pub use bytes; pub use ckb_fixed_hash::{h160, h256, H160, H256}; pub use ckb_gen_types::packed; pub use molecule::{self, error}; pub use numext_fixed_uint::{u256, U128, U256}; - pub mod core; pub mod global; @@ -18,5 +18,6 @@ mod conversion; mod extension; pub mod utilities; +mod block_number_and_hash; #[cfg(test)] mod tests;