Skip to content

Commit

Permalink
Merge pull request #1998 from subspace/gemini-3f-backport-fix-last-ar…
Browse files Browse the repository at this point in the history
…chived-segment-search

Gemini 3f backport: Fix last archived segment search
  • Loading branch information
nazar-pc authored Sep 24, 2023
2 parents c9546e9 + 65a4cc8 commit 69b9b8b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -209,6 +210,7 @@ pub(crate) const FINALIZATION_DEPTH_IN_SEGMENTS: usize = 5;
fn find_last_archived_block<Block, Client, AS>(
client: &Client,
segment_headers_store: &SegmentHeadersStore<AS>,
best_block_to_archive: NumberFor<Block>,
) -> sp_blockchain::Result<Option<(SegmentHeader, Block, BlockObjectMapping)>>
where
Block: BlockT,
Expand All @@ -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::<Block>::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)
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 69b9b8b

Please sign in to comment.