Skip to content

Commit

Permalink
Merge pull request nervosnetwork#4494 from eval-exec/exec/block_numbe…
Browse files Browse the repository at this point in the history
…r_and_hash

Move `BlockNumberAndHash` from`ckb-sync` to `ckb-types`
  • Loading branch information
quake authored Jun 26, 2024
2 parents 36413b0 + 77c8e84 commit 189e665
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 51 deletions.
3 changes: 2 additions & 1 deletion sync/src/relayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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},
Expand Down
3 changes: 2 additions & 1 deletion sync/src/synchronizer/block_fetcher.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion sync/src/tests/inflight_blocks.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
48 changes: 1 addition & 47 deletions sync/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<core::HeaderView> for BlockNumberAndHash {
fn from(header: core::HeaderView) -> Self {
Self {
number: header.number(),
hash: header.hash(),
}
}
}

enum TimeQuantile {
MinToFast,
FastToNormal,
Expand Down
54 changes: 54 additions & 0 deletions util/types/src/block_number_and_hash.rs
Original file line number Diff line number Diff line change
@@ -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<crate::core::HeaderView> for BlockNumberAndHash {
fn from(header: crate::core::HeaderView) -> Self {
Self {
number: header.number(),
hash: header.hash(),
}
}
}
3 changes: 2 additions & 1 deletion util/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,5 +18,6 @@ mod conversion;
mod extension;
pub mod utilities;

mod block_number_and_hash;
#[cfg(test)]
mod tests;

0 comments on commit 189e665

Please sign in to comment.