Skip to content

Commit d4cb1b8

Browse files
committed
Fix archiver restart, simplify logic a bit
1 parent 1d14c1c commit d4cb1b8

1 file changed

Lines changed: 41 additions & 36 deletions

File tree

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

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

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

124127
fn block_hashes_to_archive<Block, Client>(
125128
client: &Client,
126129
best_block_hash: Block::Hash,
127130
blocks_to_archive_from: NumberFor<Block>,
128131
blocks_to_archive_to: NumberFor<Block>,
129-
) -> BlockHashesToArchive<Block::Hash>
132+
) -> BlockHashesToArchive<Block>
130133
where
131134
Block: BlockT,
132135
Client: HeaderBackend<Block>,
@@ -146,7 +149,7 @@ where
146149
block_hashes.push(block_hash_to_check);
147150

148151
if best_archived.is_none() {
149-
best_archived.replace(block_hash_to_check);
152+
best_archived.replace((block_hash_to_check, *header.number()));
150153
}
151154
}
152155

@@ -163,19 +166,22 @@ where
163166
}
164167
}
165168

166-
struct InitializedArchiver<BlockHash> {
169+
struct InitializedArchiver<Block>
170+
where
171+
Block: BlockT,
172+
{
167173
confirmation_depth_k: BlockNumber,
168174
archiver: Archiver,
169175
older_archived_segments: Vec<ArchivedSegment>,
170-
best_archived_block_hash: Option<BlockHash>,
176+
best_archived_block: (Block::Hash, NumberFor<Block>),
171177
}
172178

173179
fn initialize_archiver<Block, Client>(
174180
best_block_hash: Block::Hash,
175181
best_block_number: NumberFor<Block>,
176182
subspace_link: &SubspaceLink<Block>,
177183
client: &Client,
178-
) -> InitializedArchiver<Block::Hash>
184+
) -> InitializedArchiver<Block>
179185
where
180186
Block: BlockT,
181187
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + HeaderBackend<Block>,
@@ -202,6 +208,7 @@ where
202208

203209
let maybe_last_archived_block = find_last_archived_block(client, best_block_id);
204210
let have_last_root_block = maybe_last_archived_block.is_some();
211+
let mut best_archived_block = None;
205212

206213
let mut archiver = if let Some((last_root_block, last_archived_block, block_object_mappings)) =
207214
maybe_last_archived_block
@@ -214,6 +221,13 @@ where
214221
last_archived_block_number,
215222
);
216223

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

232246
let mut older_archived_segments = Vec::new();
233-
let mut best_archived_block_hash = None;
234247

235248
// Process blocks since last fully archived block (or genesis) up to the current head minus K
236249
{
@@ -269,7 +282,7 @@ where
269282
blocks_to_archive_from.into(),
270283
blocks_to_archive_to.into(),
271284
);
272-
best_archived_block_hash = block_hashes_to_archive.best_archived;
285+
best_archived_block = block_hashes_to_archive.best_archived;
273286
let block_hashes_to_archive = block_hashes_to_archive.block_hashes;
274287

275288
for block_hash_to_archive in block_hashes_to_archive.into_iter().rev() {
@@ -330,7 +343,8 @@ where
330343
confirmation_depth_k,
331344
archiver,
332345
older_archived_segments,
333-
best_archived_block_hash,
346+
best_archived_block: best_archived_block
347+
.expect("Must always set if there is no logical error; qed"),
334348
}
335349
}
336350

@@ -360,7 +374,7 @@ pub fn start_subspace_archiver<Block, Client>(
360374
confirmation_depth_k,
361375
mut archiver,
362376
older_archived_segments,
363-
mut best_archived_block_hash,
377+
best_archived_block: (mut best_archived_block_hash, mut best_archived_block_number),
364378
} = initialize_archiver(
365379
best_block_hash,
366380
best_block_number,
@@ -391,9 +405,6 @@ pub fn start_subspace_archiver<Block, Client>(
391405
drop(older_archived_segments);
392406
}
393407

394-
let mut last_archived_block_number =
395-
archiver.last_archived_block_number().map(Into::into);
396-
397408
while let Some(ImportedBlockNotification {
398409
block_number,
399410
mut root_block_sender,
@@ -407,17 +418,13 @@ pub fn start_subspace_archiver<Block, Client>(
407418
}
408419
};
409420

410-
if let Some(last_archived_block) = &mut last_archived_block_number {
411-
if *last_archived_block >= block_number_to_archive {
412-
// This block was already archived, skip
413-
continue;
414-
}
415-
416-
*last_archived_block = block_number_to_archive;
417-
} else {
418-
last_archived_block_number.replace(block_number_to_archive);
421+
if best_archived_block_number >= block_number_to_archive {
422+
// This block was already archived, skip
423+
continue;
419424
}
420425

426+
best_archived_block_number = block_number_to_archive;
427+
421428
let block = client
422429
.block(&BlockId::Number(block_number_to_archive))
423430
.expect("Older block by number must always exist")
@@ -433,20 +440,18 @@ pub fn start_subspace_archiver<Block, Client>(
433440
block_hash_to_archive
434441
);
435442

436-
if let Some(best_archived_block_hash) = best_archived_block_hash {
437-
if parent_block_hash != best_archived_block_hash {
438-
error!(
439-
target: "subspace",
440-
"Attempt to switch to a different fork beyond archiving depth, \
441-
can't do it: parent block hash {}, best archived block hash {}",
442-
parent_block_hash,
443-
best_archived_block_hash
444-
);
445-
return;
446-
}
443+
if parent_block_hash != best_archived_block_hash {
444+
error!(
445+
target: "subspace",
446+
"Attempt to switch to a different fork beyond archiving depth, \
447+
can't do it: parent block hash {}, best archived block hash {}",
448+
parent_block_hash,
449+
best_archived_block_hash
450+
);
451+
return;
447452
}
448453

449-
best_archived_block_hash.replace(block_hash_to_archive);
454+
best_archived_block_hash = block_hash_to_archive;
450455

451456
let block_object_mappings = client
452457
.runtime_api()

0 commit comments

Comments
 (0)