diff --git a/src/protocols/filter/block_filter.rs b/src/protocols/filter/block_filter.rs index a311fb9..9dec278 100644 --- a/src/protocols/filter/block_filter.rs +++ b/src/protocols/filter/block_filter.rs @@ -95,11 +95,13 @@ impl FilterProtocol { nc: Arc, 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 @@ -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, diff --git a/src/protocols/filter/components/block_filters_process.rs b/src/protocols/filter/components/block_filters_process.rs index 267bb4a..07cb2de 100644 --- a/src/protocols/filter/components/block_filters_process.rs +++ b/src/protocols/filter/components/block_filters_process.rs @@ -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 diff --git a/src/protocols/light_client/peers.rs b/src/protocols/light_client/peers.rs index f3d0ade..dd4677e 100644 --- a/src/protocols/light_client/peers.rs +++ b/src/protocols/light_client/peers.rs @@ -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> { @@ -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 { @@ -1799,7 +1805,7 @@ 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); @@ -1807,7 +1813,7 @@ impl Peers { .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,