Skip to content

Commit

Permalink
Merge pull request #162 from yangby-cryptape/bugfix/stuct-after-restart
Browse files Browse the repository at this point in the history
fix: stuck after restart when min filtered block is previous block of a check point
  • Loading branch information
quake authored Dec 2, 2023
2 parents 38aa9b2 + e7499f3 commit d25eabb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/protocols/filter/block_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ impl FilterProtocol {
nc: Arc<dyn CKBProtocolContext + Sync>,
immediately: bool,
) {
let start_number = self.storage.get_min_filtered_block_number() + 1;
let min_filtered_block_number = self.storage.get_min_filtered_block_number();
let start_number = min_filtered_block_number + 1;
let (finalized_check_point_index, _) = self.storage.get_last_check_point();
let could_ask_more = self
.peers
.could_request_more_block_filters(finalized_check_point_index, start_number);
let could_ask_more = self.peers.could_request_more_block_filters(
finalized_check_point_index,
min_filtered_block_number,
);
if log_enabled!(Level::Trace) {
let finalized_check_point_number = self
.peers
Expand All @@ -113,9 +115,9 @@ impl FilterProtocol {
.calc_check_point_number(cached_check_point_index + 1);
trace!(
"could request block filters from {} or not: {}, \
finalized: index {}, number {}; \
cached: index {}, number {}, length {}; \
next cached: number {}",
finalized: index {}, number {}; \
cached: index {}, number {}, length {}; \
next cached: number {}",
start_number,
could_ask_more,
finalized_check_point_index,
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/filter/components/block_filters_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ impl<'a> BlockFiltersProcess<'a> {
self.filter
.update_min_filtered_block_number(filtered_block_number);

let could_request_more_block_filters = self.filter.peers.could_request_more_block_filters(
finalized_check_point_index,
filtered_block_number + 1,
);
let could_request_more_block_filters = self
.filter
.peers
.could_request_more_block_filters(finalized_check_point_index, filtered_block_number);
if could_request_more_block_filters {
// send next batch GetBlockFilters message to a random best peer
let best_peer = self
Expand Down
16 changes: 11 additions & 5 deletions src/protocols/light_client/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,8 +1138,14 @@ impl Peers {
self.check_point_interval * BlockNumber::from(index)
}

fn calc_best_check_point_index_not_greater_than(&self, number: BlockNumber) -> u32 {
(number / self.check_point_interval) as u32
fn calc_cached_check_point_index_when_sync_at(&self, number: BlockNumber) -> u32 {
// Examples of `input -> output`, denote `check_point_interval` as `i`:
// - [0] -> 0
// - [1, i] -> 0
// - [i+1, 2i] -> 1
// - ... ...
// - [ki+1, (k+1)i] -> k
(number.saturating_sub(1) / self.check_point_interval) as u32
}

pub(crate) fn last_headers(&self) -> &RwLock<Vec<HeaderView>> {
Expand Down Expand Up @@ -1605,7 +1611,7 @@ impl Peers {

pub(crate) fn update_min_filtered_block_number(&self, min_filtered_block_number: BlockNumber) {
let should_cached_check_point_index =
self.calc_best_check_point_index_not_greater_than(min_filtered_block_number);
self.calc_cached_check_point_index_when_sync_at(min_filtered_block_number + 1);
let current_cached_check_point_index =
self.cached_block_filter_hashes.read().expect("poisoned").0;
if current_cached_check_point_index != should_cached_check_point_index {
Expand Down Expand Up @@ -1799,15 +1805,15 @@ impl Peers {
min_filtered_block_number: BlockNumber,
) -> bool {
let should_cached_check_point_index =
self.calc_best_check_point_index_not_greater_than(min_filtered_block_number);
self.calc_cached_check_point_index_when_sync_at(min_filtered_block_number + 1);
if should_cached_check_point_index >= finalized_check_point_index {
let finalized_check_point_number =
self.calc_check_point_number(finalized_check_point_index);
let latest_block_filter_hashes_count = self
.get_latest_block_filter_hashes(finalized_check_point_index)
.len();
finalized_check_point_number + latest_block_filter_hashes_count as BlockNumber
>= min_filtered_block_number
>= min_filtered_block_number + 1
} else {
// Check:
// - If cached block filter hashes is same check point as the required,
Expand Down

0 comments on commit d25eabb

Please sign in to comment.