Skip to content

Commit 62c0d5d

Browse files
committed
header_map: reduce total_difficulty usage from HeaderIndexMap
Signed-off-by: Eval EXEC <[email protected]>
1 parent 439aad3 commit 62c0d5d

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

shared/src/types/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![allow(missing_docs)]
2-
use ckb_types::core::{BlockNumber, EpochNumberWithFraction};
3-
use ckb_types::packed::Byte32;
4-
use ckb_types::{BlockNumberAndHash, U256};
2+
use ckb_types::{
3+
BlockNumberAndHash, U256,
4+
core::{self, BlockNumber, EpochNumberWithFraction},
5+
packed::Byte32,
6+
};
57

68
pub mod header_map;
79

@@ -66,8 +68,8 @@ impl HeaderIndexView {
6668

6769
pub fn build_skip<F, G>(&mut self, tip_number: BlockNumber, get_header_view: F, fast_scanner: G)
6870
where
69-
F: Fn(&Byte32, bool) -> Option<HeaderIndexView>,
70-
G: Fn(BlockNumber, BlockNumberAndHash) -> Option<HeaderIndexView>,
71+
F: Fn(&Byte32, bool) -> Option<HeaderView>,
72+
G: Fn(BlockNumber, BlockNumberAndHash) -> Option<HeaderView>,
7173
{
7274
if self.number == 0 {
7375
return;
@@ -88,10 +90,10 @@ impl HeaderIndexView {
8890
number: BlockNumber,
8991
get_header_view: F,
9092
fast_scanner: G,
91-
) -> Option<HeaderIndexView>
93+
) -> Option<core::HeaderView>
9294
where
93-
F: Fn(&Byte32, bool) -> Option<HeaderIndexView>,
94-
G: Fn(BlockNumber, BlockNumberAndHash) -> Option<HeaderIndexView>,
95+
F: Fn(&Byte32, bool) -> Option<HeaderView>,
96+
G: Fn(BlockNumber, BlockNumberAndHash) -> Option<HeaderView>,
9597
{
9698
if number > self.number() {
9799
return None;

sync/src/relayer/compact_block_process.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn contextual_check(
261261
}
262262

263263
let store_first = tip.number() + 1 >= compact_block_header.number();
264-
let parent = shared.get_header_index_view(
264+
let parent = shared.get_header_view(
265265
&compact_block_header.data().raw().parent_hash(),
266266
store_first,
267267
);
@@ -307,7 +307,7 @@ fn contextual_check(
307307
})
308308
.or_else(|| {
309309
shared
310-
.get_header_index_view(&block_hash, false)
310+
.get_header_view(&block_hash, false)
311311
.map(|header| HeaderFields {
312312
hash: header.hash(),
313313
number: header.number(),

sync/src/synchronizer/block_fetcher.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl BlockFetcher {
196196
end.saturating_sub(start) as usize + 1,
197197
state.read_inflight_blocks().peer_can_fetch_count(self.peer),
198198
);
199-
let mut fetch = Vec::with_capacity(n_fetch);
199+
let mut fetch: Vec<ckb_types::core::HeaderView> = Vec::with_capacity(n_fetch);
200200
let now = unix_time_as_millis();
201201
debug!(
202202
"finding which blocks to fetch, start: {}, end: {}, best_known: {}",
@@ -263,9 +263,7 @@ impl BlockFetcher {
263263
.sync_shared
264264
.active_chain()
265265
.get_block_status(&parent_hash);
266-
header = self
267-
.sync_shared
268-
.get_header_index_view(&parent_hash, false)?;
266+
header = self.sync_shared.get_header_view(&parent_hash, false)?;
269267
}
270268

271269
// Move `start` forward

sync/src/types/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,14 +1144,14 @@ impl SyncShared {
11441144
let snapshot = Arc::clone(&self.shared.snapshot());
11451145
header_view.build_skip(
11461146
tip_number,
1147-
|hash, store_first| self.get_header_index_view(hash, store_first),
1147+
|hash, store_first| self.get_header_view(hash, store_first),
11481148
|number, current| {
11491149
// shortcut to return an ancestor block
11501150
if current.number <= snapshot.tip_number() && snapshot.is_main_chain(&current.hash)
11511151
{
11521152
snapshot
11531153
.get_block_hash(number)
1154-
.and_then(|hash| self.get_header_index_view(&hash, true))
1154+
.and_then(|hash| self.get_header_view(&hash, true))
11551155
} else {
11561156
None
11571157
}
@@ -1166,6 +1166,14 @@ impl SyncShared {
11661166
self.may_set_shared_best_header(header_view);
11671167
}
11681168

1169+
pub(crate) fn get_header_view(
1170+
&self,
1171+
hash: &Byte32,
1172+
store_first: bool,
1173+
) -> Option<core::HeaderView> {
1174+
todo!("get header view");
1175+
}
1176+
11691177
pub(crate) fn get_header_index_view(
11701178
&self,
11711179
hash: &Byte32,
@@ -1715,15 +1723,15 @@ impl ActiveChain {
17151723
self.unverified_tip_header().number()
17161724
}
17171725

1718-
pub fn get_ancestor(&self, base: &Byte32, number: BlockNumber) -> Option<HeaderIndexView> {
1726+
pub fn get_ancestor(&self, base: &Byte32, number: BlockNumber) -> Option<core::HeaderView> {
17191727
self.get_ancestor_internal(base, number, false)
17201728
}
17211729

17221730
pub fn get_ancestor_with_unverified(
17231731
&self,
17241732
base: &Byte32,
17251733
number: BlockNumber,
1726-
) -> Option<HeaderIndexView> {
1734+
) -> Option<core::HeaderView> {
17271735
self.get_ancestor_internal(base, number, true)
17281736
}
17291737

@@ -1732,7 +1740,7 @@ impl ActiveChain {
17321740
base: &Byte32,
17331741
number: BlockNumber,
17341742
with_unverified: bool,
1735-
) -> Option<HeaderIndexView> {
1743+
) -> Option<core::HeaderView> {
17361744
let tip_number = {
17371745
if with_unverified {
17381746
self.unverified_tip_number()
@@ -1749,15 +1757,14 @@ impl ActiveChain {
17491757
}
17501758
};
17511759

1752-
let get_header_view_fn = |hash: &Byte32, store_first: bool| {
1753-
self.sync_shared.get_header_index_view(hash, store_first)
1754-
};
1760+
let get_header_view_fn =
1761+
|hash: &Byte32, store_first: bool| self.sync_shared.get_header_view(hash, store_first);
17551762

17561763
let fast_scanner_fn = |number: BlockNumber, current: BlockNumberAndHash| {
17571764
// shortcut to return an ancestor block
17581765
if current.number <= tip_number && block_is_on_chain_fn(&current.hash) {
17591766
self.get_block_hash(number)
1760-
.and_then(|hash| self.sync_shared.get_header_index_view(&hash, true))
1767+
.and_then(|hash| self.sync_shared.get_header_view(&hash, true))
17611768
} else {
17621769
None
17631770
}

0 commit comments

Comments
 (0)