@@ -20,8 +20,9 @@ use crate::{
2020} ;
2121use codec:: Encode ;
2222use 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 } ;
2526use sc_utils:: mpsc:: tracing_unbounded;
2627use sp_api:: ProvideRuntimeApi ;
2728use 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 } ) ,
0 commit comments