From 0eaf4f11f1412df4160a83e7fe42b93786636ed5 Mon Sep 17 00:00:00 2001 From: teor Date: Wed, 30 Oct 2024 16:52:05 +1000 Subject: [PATCH] Simplify a complex return type --- crates/sc-consensus-subspace/src/archiver.rs | 50 ++++++++++++-------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/crates/sc-consensus-subspace/src/archiver.rs b/crates/sc-consensus-subspace/src/archiver.rs index 740b846c3b..6f2f9f1701 100644 --- a/crates/sc-consensus-subspace/src/archiver.rs +++ b/crates/sc-consensus-subspace/src/archiver.rs @@ -435,20 +435,32 @@ impl CreateObjectMappings { } } -#[expect(clippy::type_complexity)] +#[derive(Clone, Debug, Eq, PartialEq)] +enum LastArchivedBlock { + /// The last archived block and associated segment info. + Found { + last_segment_header: SegmentHeader, + last_segment_index: SegmentIndex, + last_archived_block: SignedBlock, + block_object_mappings: BlockObjectMapping, + }, + + /// There is no last archived block. + NotFound, +} + +impl LastArchivedBlock { + fn was_found(&self) -> bool { + matches!(self, LastArchivedBlock::Found { .. }) + } +} + fn find_last_archived_block( client: &Client, segment_headers_store: &SegmentHeadersStore, best_block_to_archive: NumberFor, create_object_mappings: CreateObjectMappings, -) -> sp_blockchain::Result< - Option<( - SegmentHeader, - SegmentIndex, - SignedBlock, - BlockObjectMapping, - )>, -> +) -> sp_blockchain::Result> where Block: BlockT, Client: ProvideRuntimeApi + BlockBackend + HeaderBackend, @@ -456,12 +468,12 @@ where 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) @@ -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 @@ -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;