Skip to content

Commit de3dfb1

Browse files
authored
Merge pull request #606 from subspace/fix-archiver-restart
Fix archiver restart, simplify logic a bit
2 parents 130205f + d4cb1b8 commit de3dfb1

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

crates/sc-consensus-subspace/src/archiver.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,20 @@ where
117117
Some((last_root_block, last_archived_block, block_object_mappings))
118118
}
119119

120-
struct BlockHashesToArchive<BlockHash> {
121-
block_hashes: Vec<BlockHash>,
122-
best_archived: Option<BlockHash>,
120+
struct BlockHashesToArchive<Block>
121+
where
122+
Block: BlockT,
123+
{
124+
block_hashes: Vec<Block::Hash>,
125+
best_archived: Option<(Block::Hash, NumberFor<Block>)>,
123126
}
124127

125128
fn block_hashes_to_archive<Block, Client>(
126129
client: &Client,
127130
best_block_hash: Block::Hash,
128131
blocks_to_archive_from: NumberFor<Block>,
129132
blocks_to_archive_to: NumberFor<Block>,
130-
) -> BlockHashesToArchive<Block::Hash>
133+
) -> BlockHashesToArchive<Block>
131134
where
132135
Block: BlockT,
133136
Client: HeaderBackend<Block>,
@@ -147,7 +150,7 @@ where
147150
block_hashes.push(block_hash_to_check);
148151

149152
if best_archived.is_none() {
150-
best_archived.replace(block_hash_to_check);
153+
best_archived.replace((block_hash_to_check, *header.number()));
151154
}
152155
}
153156

@@ -164,19 +167,22 @@ where
164167
}
165168
}
166169

167-
struct InitializedArchiver<BlockHash> {
170+
struct InitializedArchiver<Block>
171+
where
172+
Block: BlockT,
173+
{
168174
confirmation_depth_k: BlockNumber,
169175
archiver: Archiver,
170176
older_archived_segments: Vec<ArchivedSegment>,
171-
best_archived_block_hash: Option<BlockHash>,
177+
best_archived_block: (Block::Hash, NumberFor<Block>),
172178
}
173179

174180
fn initialize_archiver<Block, Client>(
175181
best_block_hash: Block::Hash,
176182
best_block_number: NumberFor<Block>,
177183
subspace_link: &SubspaceLink<Block>,
178184
client: &Client,
179-
) -> InitializedArchiver<Block::Hash>
185+
) -> InitializedArchiver<Block>
180186
where
181187
Block: BlockT,
182188
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + HeaderBackend<Block>,
@@ -203,6 +209,7 @@ where
203209

204210
let maybe_last_archived_block = find_last_archived_block(client, best_block_id);
205211
let have_last_root_block = maybe_last_archived_block.is_some();
212+
let mut best_archived_block = None;
206213

207214
let mut archiver = if let Some((last_root_block, last_archived_block, block_object_mappings)) =
208215
maybe_last_archived_block
@@ -215,6 +222,13 @@ where
215222
last_archived_block_number,
216223
);
217224

225+
// Set initial value, this is needed in case only genesis block was archived and there is
226+
// nothing else available
227+
best_archived_block.replace((
228+
last_archived_block.block.hash(),
229+
*last_archived_block.block.header().number(),
230+
));
231+
218232
Archiver::with_initial_state(
219233
record_size as usize,
220234
recorded_history_segment_size as usize,
@@ -231,7 +245,6 @@ where
231245
};
232246

233247
let mut older_archived_segments = Vec::new();
234-
let mut best_archived_block_hash = None;
235248

236249
// Process blocks since last fully archived block (or genesis) up to the current head minus K
237250
{
@@ -270,7 +283,7 @@ where
270283
blocks_to_archive_from.into(),
271284
blocks_to_archive_to.into(),
272285
);
273-
best_archived_block_hash = block_hashes_to_archive.best_archived;
286+
best_archived_block = block_hashes_to_archive.best_archived;
274287
let block_hashes_to_archive = block_hashes_to_archive.block_hashes;
275288

276289
for block_hash_to_archive in block_hashes_to_archive.into_iter().rev() {
@@ -331,7 +344,8 @@ where
331344
confirmation_depth_k,
332345
archiver,
333346
older_archived_segments,
334-
best_archived_block_hash,
347+
best_archived_block: best_archived_block
348+
.expect("Must always set if there is no logical error; qed"),
335349
}
336350
}
337351

@@ -400,7 +414,7 @@ pub fn start_subspace_archiver<Block, Backend, Client>(
400414
confirmation_depth_k,
401415
mut archiver,
402416
older_archived_segments,
403-
mut best_archived_block_hash,
417+
best_archived_block: (mut best_archived_block_hash, mut best_archived_block_number),
404418
} = initialize_archiver(
405419
best_block_hash,
406420
best_block_number,
@@ -431,9 +445,6 @@ pub fn start_subspace_archiver<Block, Backend, Client>(
431445
drop(older_archived_segments);
432446
}
433447

434-
let mut last_archived_block_number =
435-
archiver.last_archived_block_number().map(Into::into);
436-
437448
while let Some(ImportedBlockNotification {
438449
block_number,
439450
mut root_block_sender,
@@ -447,17 +458,13 @@ pub fn start_subspace_archiver<Block, Backend, Client>(
447458
}
448459
};
449460

450-
if let Some(last_archived_block) = &mut last_archived_block_number {
451-
if *last_archived_block >= block_number_to_archive {
452-
// This block was already archived, skip
453-
continue;
454-
}
455-
456-
*last_archived_block = block_number_to_archive;
457-
} else {
458-
last_archived_block_number.replace(block_number_to_archive);
461+
if best_archived_block_number >= block_number_to_archive {
462+
// This block was already archived, skip
463+
continue;
459464
}
460465

466+
best_archived_block_number = block_number_to_archive;
467+
461468
let block = client
462469
.block(&BlockId::Number(block_number_to_archive))
463470
.expect("Older block by number must always exist")
@@ -473,20 +480,18 @@ pub fn start_subspace_archiver<Block, Backend, Client>(
473480
block_hash_to_archive
474481
);
475482

476-
if let Some(best_archived_block_hash) = best_archived_block_hash {
477-
if parent_block_hash != best_archived_block_hash {
478-
error!(
479-
target: "subspace",
480-
"Attempt to switch to a different fork beyond archiving depth, \
481-
can't do it: parent block hash {}, best archived block hash {}",
482-
parent_block_hash,
483-
best_archived_block_hash
484-
);
485-
return;
486-
}
483+
if parent_block_hash != best_archived_block_hash {
484+
error!(
485+
target: "subspace",
486+
"Attempt to switch to a different fork beyond archiving depth, \
487+
can't do it: parent block hash {}, best archived block hash {}",
488+
parent_block_hash,
489+
best_archived_block_hash
490+
);
491+
return;
487492
}
488493

489-
best_archived_block_hash.replace(block_hash_to_archive);
494+
best_archived_block_hash = block_hash_to_archive;
490495

491496
let block_object_mappings = client
492497
.runtime_api()

0 commit comments

Comments
 (0)