Skip to content

Commit

Permalink
Simplify a complex return type
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Oct 30, 2024
1 parent 4003ea7 commit 0eaf4f1
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,33 +435,45 @@ impl CreateObjectMappings {
}
}

#[expect(clippy::type_complexity)]
#[derive(Clone, Debug, Eq, PartialEq)]
enum LastArchivedBlock<Block> {
/// The last archived block and associated segment info.
Found {
last_segment_header: SegmentHeader,
last_segment_index: SegmentIndex,
last_archived_block: SignedBlock<Block>,
block_object_mappings: BlockObjectMapping,
},

/// There is no last archived block.
NotFound,
}

impl<Block> LastArchivedBlock<Block> {
fn was_found(&self) -> bool {
matches!(self, LastArchivedBlock::Found { .. })
}
}

fn find_last_archived_block<Block, Client, AS>(
client: &Client,
segment_headers_store: &SegmentHeadersStore<AS>,
best_block_to_archive: NumberFor<Block>,
create_object_mappings: CreateObjectMappings,
) -> sp_blockchain::Result<
Option<(
SegmentHeader,
SegmentIndex,
SignedBlock<Block>,
BlockObjectMapping,
)>,
>
) -> sp_blockchain::Result<LastArchivedBlock<Block>>
where
Block: BlockT,
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + HeaderBackend<Block>,
Client::Api: SubspaceApi<Block, PublicKey> + ObjectsApi<Block>,
AS: AuxStore,
{
let Some(max_segment_index) = segment_headers_store.max_segment_index() else {
return Ok(None);
return Ok(LastArchivedBlock::NotFound);
};

if max_segment_index == SegmentIndex::ZERO {
// Just genesis, nothing else to check
return Ok(None);
return Ok(LastArchivedBlock::NotFound);
}

for (segment_header, segment_index) in (SegmentIndex::ZERO..=max_segment_index)
Expand Down Expand Up @@ -510,15 +522,15 @@ where
BlockObjectMapping::default()
};

return Ok(Some((
segment_header,
segment_index,
return Ok(LastArchivedBlock::Found {
last_segment_header: segment_header,
last_segment_index: segment_index,
last_archived_block,
block_object_mappings,
)));
});
}

Ok(None)
Ok(LastArchivedBlock::NotFound)
}

/// Derive genesis segment on demand, returns `Ok(None)` in case genesis block was already pruned
Expand Down Expand Up @@ -669,16 +681,16 @@ where
create_object_mappings,
)?;

let have_last_segment_header = maybe_last_archived_block.is_some();
let have_last_segment_header = maybe_last_archived_block.was_found();
let mut best_archived_block = None;
let mut first_new_segment_index = SegmentIndex::ZERO;

let mut archiver = if let Some((
let mut archiver = if let LastArchivedBlock::Found {
last_segment_header,
last_segment_index,
last_archived_block,
block_object_mappings,
)) = maybe_last_archived_block
} = maybe_last_archived_block
{
first_new_segment_index = last_segment_index + SegmentIndex::ONE;

Expand Down

0 comments on commit 0eaf4f1

Please sign in to comment.