Skip to content

Commit 0c94d65

Browse files
authored
Merge pull request #604 from subspace/block-finalization
Implement finalization of blocks based on archiving depth
2 parents 5f0afe8 + 6718359 commit 0c94d65

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use crate::{
2020
};
2121
use codec::Encode;
2222
use futures::{future, SinkExt, StreamExt};
23-
use log::{debug, error, info};
24-
use sc_client_api::BlockBackend;
23+
use log::{debug, error, info, warn};
24+
use sc_client_api::{Backend as BackendT, BlockBackend, Finalizer, LockImportRun};
25+
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO};
2526
use sc_utils::mpsc::tracing_unbounded;
2627
use sp_api::ProvideRuntimeApi;
2728
use sp_blockchain::HeaderBackend;
@@ -334,19 +335,58 @@ where
334335
}
335336
}
336337

338+
fn finalize_block<Block, Backend, Client>(
339+
client: &Client,
340+
telemetry: Option<TelemetryHandle>,
341+
hash: Block::Hash,
342+
number: NumberFor<Block>,
343+
) where
344+
Block: BlockT,
345+
Backend: BackendT<Block>,
346+
Client: LockImportRun<Block, Backend> + Finalizer<Block, Backend>,
347+
{
348+
// We don't have anything useful to do with this result yet, the only source of errors was
349+
// logged already inside
350+
let _result: Result<_, sp_blockchain::Error> = client.lock_import_and_run(|import_op| {
351+
// Ideally some handle to a synchronization oracle would be used to avoid unconditionally
352+
// notifying.
353+
client
354+
.apply_finality(import_op, BlockId::Hash(hash), None, true)
355+
.map_err(|error| {
356+
warn!(target: "subspace", "Error applying finality to block {:?}: {}", (hash, number), error);
357+
error
358+
})?;
359+
360+
debug!(target: "subspace", "Finalizing blocks up to ({:?}, {})", number, hash);
361+
362+
telemetry!(
363+
telemetry;
364+
CONSENSUS_INFO;
365+
"subspace.finalized_blocks_up_to";
366+
"number" => ?number, "hash" => ?hash,
367+
);
368+
369+
Ok(())
370+
});
371+
}
372+
337373
/// Start an archiver that will listen for imported blocks and archive blocks at `K` depth,
338374
/// producing pieces and root blocks (root blocks are then added back to the blockchain as
339375
/// `store_root_block` extrinsic).
340-
pub fn start_subspace_archiver<Block, Client>(
376+
pub fn start_subspace_archiver<Block, Backend, Client>(
341377
subspace_link: &SubspaceLink<Block>,
342378
client: Arc<Client>,
379+
telemetry: Option<TelemetryHandle>,
343380
spawner: &impl sp_core::traits::SpawnEssentialNamed,
344381
is_authoring_blocks: bool,
345382
) where
346383
Block: BlockT,
384+
Backend: BackendT<Block>,
347385
Client: ProvideRuntimeApi<Block>
348386
+ BlockBackend<Block>
349387
+ HeaderBackend<Block>
388+
+ LockImportRun<Block, Backend>
389+
+ Finalizer<Block, Backend>
350390
+ Send
351391
+ Sync
352392
+ 'static,
@@ -481,6 +521,13 @@ pub fn start_subspace_archiver<Block, Client>(
481521

482522
let _ = root_block_sender.send(root_block).await;
483523
}
524+
525+
finalize_block(
526+
client.as_ref(),
527+
telemetry.clone(),
528+
block_hash_to_archive,
529+
block_number_to_archive,
530+
);
484531
}
485532
}
486533
}),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
523523
super::start_subspace_archiver(
524524
&data.link,
525525
client.clone(),
526+
None,
526527
&task_manager.spawn_essential_handle(),
527528
false,
528529
);

crates/subspace-service/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ where
256256
sc_consensus_subspace::start_subspace_archiver(
257257
&subspace_link,
258258
client.clone(),
259+
telemetry.as_ref().map(|telemetry| telemetry.handle()),
259260
&task_manager.spawn_essential_handle(),
260261
config.role.is_authority(),
261262
);

0 commit comments

Comments
 (0)