From 65a4cc8889e549f88d119e1d595e700a919abb0d Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Sat, 23 Sep 2023 05:24:41 +0300 Subject: [PATCH] Fix last archived segment search --- crates/sc-consensus-subspace/src/archiver.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/sc-consensus-subspace/src/archiver.rs b/crates/sc-consensus-subspace/src/archiver.rs index 815c0c8472..8743b89cf4 100644 --- a/crates/sc-consensus-subspace/src/archiver.rs +++ b/crates/sc-consensus-subspace/src/archiver.rs @@ -40,6 +40,7 @@ use sp_consensus::SyncOracle; use sp_consensus_subspace::{FarmerPublicKey, SubspaceApi}; use sp_objects::ObjectsApi; use sp_runtime::traits::{Block as BlockT, CheckedSub, Header, NumberFor, One, Zero}; +use sp_runtime::Saturating; use std::error::Error; use std::future::Future; use std::slice; @@ -209,6 +210,7 @@ pub(crate) const FINALIZATION_DEPTH_IN_SEGMENTS: usize = 5; fn find_last_archived_block( client: &Client, segment_headers_store: &SegmentHeadersStore, + best_block_to_archive: NumberFor, ) -> sp_blockchain::Result> where Block: BlockT, @@ -230,6 +232,12 @@ where .filter_map(|segment_index| segment_headers_store.get_segment_header(segment_index)) { let last_archived_block_number = segment_header.last_archived_block().number; + if NumberFor::::from(last_archived_block_number) > best_block_to_archive { + // Last archived block in segment header it too high for current stat of the chain + // (segment headers store may know about more blocks in existence than is currently + // imported) + continue; + } let Some(last_archived_block_hash) = client.hash(last_archived_block_number.into())? else { // This block number is not in our chain yet (segment headers store may know about more // blocks in existence than is currently imported) @@ -358,7 +366,11 @@ where .expect("Must always be able to get chain constants") .confirmation_depth_k(); - let maybe_last_archived_block = find_last_archived_block(client, segment_headers_store)?; + let maybe_last_archived_block = find_last_archived_block( + client, + segment_headers_store, + best_block_number.saturating_sub(confirmation_depth_k.into()), + )?; let have_last_segment_header = maybe_last_archived_block.is_some(); let mut best_archived_block = None;