11mod piece_record_store;
22
33use crate :: dsn:: piece_record_store:: { AuxRecordStorage , SegmentIndexGetter } ;
4- use futures:: StreamExt ;
5- use sc_consensus_subspace:: { ArchivedSegmentNotification , SubspaceLink } ;
4+ use futures:: { Stream , StreamExt } ;
5+ use sc_client_api:: AuxStore ;
6+ use sc_consensus_subspace:: ArchivedSegmentNotification ;
67use sc_piece_cache:: AuxPieceCache ;
7- use sp_core:: traits:: SpawnEssentialNamed ;
8+ use sp_core:: traits:: SpawnNamed ;
89use sp_runtime:: traits:: Block as BlockT ;
910use std:: sync:: Arc ;
1011use subspace_core_primitives:: { Piece , PieceIndex , PieceIndexHash , PIECES_IN_SEGMENT } ;
1112use subspace_networking:: libp2p:: { identity, Multiaddr } ;
1213use subspace_networking:: {
1314 BootstrappedNetworkingParameters , CreationError , CustomRecordStore , MemoryProviderStorage ,
14- Node , PieceByHashRequestHandler , PieceByHashResponse , PieceKey , ToMultihash ,
15+ Node , NodeRunner , PieceByHashRequestHandler , PieceByHashResponse , PieceKey , ToMultihash ,
1516} ;
1617use tracing:: { debug, info, trace, Instrument } ;
1718
@@ -33,31 +34,28 @@ pub struct DsnConfig {
3334 pub allow_non_global_addresses_in_dht : bool ,
3435}
3536
36- /// Start an archiver that will listen for archived segments and send it to DSN network using
37- /// pub-sub protocol.
38- pub async fn start_dsn_node < Block , Spawner , AS : sc_client_api:: AuxStore + Sync + Send + ' static > (
39- subspace_link : & SubspaceLink < Block > ,
37+ pub ( crate ) async fn create_dsn_instance < Block , AS > (
4038 dsn_config : DsnConfig ,
41- spawner : Spawner ,
4239 piece_cache : AuxPieceCache < AS > ,
4340 piece_getter : PieceGetter ,
4441 segment_index_getter : SegmentIndexGetter ,
45- ) -> Result < Node , CreationError >
42+ ) -> Result <
43+ (
44+ Node ,
45+ NodeRunner < CustomRecordStore < AuxRecordStorage < AS > , MemoryProviderStorage > > ,
46+ ) ,
47+ CreationError ,
48+ >
4649where
4750 Block : BlockT ,
48- Spawner : SpawnEssentialNamed ,
51+ AS : AuxStore + Sync + Send + ' static ,
4952{
50- let span = tracing:: info_span!( sc_tracing:: logging:: PREFIX_LOG_SPAN , name = "DSN" ) ;
51- let _enter = span. enter ( ) ;
52-
5353 // TODO: Combine `AuxPieceCache` with `AuxRecordStorage` and remove `PieceCache` abstraction
5454 let record_storage = AuxRecordStorage :: new ( piece_cache, segment_index_getter) ;
5555
5656 trace ! ( "Subspace networking starting." ) ;
5757
58- let networking_config = subspace_networking:: Config :: <
59- CustomRecordStore < AuxRecordStorage < AS > , MemoryProviderStorage > ,
60- > {
58+ let networking_config = subspace_networking:: Config {
6159 keypair : dsn_config. keypair ,
6260 listen_on : dsn_config. listen_on ,
6361 allow_non_global_addresses_in_dht : dsn_config. allow_non_global_addresses_in_dht ,
@@ -80,58 +78,51 @@ where
8078 ..subspace_networking:: Config :: with_generated_keypair ( )
8179 } ;
8280
83- let ( node, mut node_runner) = subspace_networking:: create ( networking_config) . await ?;
84-
85- info ! ( "Subspace networking initialized: Node ID is {}" , node. id( ) ) ;
81+ subspace_networking:: create ( networking_config) . await
82+ }
8683
87- spawner. spawn_essential (
88- "node-runner" ,
89- Some ( "subspace-networking" ) ,
90- Box :: pin (
91- async move {
92- node_runner. run ( ) . await ;
84+ /// Start an archiver that will listen for archived segments and send it to DSN network using
85+ /// pub-sub protocol.
86+ pub ( crate ) async fn start_dsn_archiver < Spawner > (
87+ mut archived_segment_notification_stream : impl Stream < Item = ArchivedSegmentNotification > + Unpin ,
88+ node : Node ,
89+ spawner : Spawner ,
90+ ) where
91+ Spawner : SpawnNamed ,
92+ {
93+ trace ! ( "Subspace DSN archiver started." ) ;
94+
95+ let mut last_published_segment_index: Option < u64 > = None ;
96+ while let Some ( ArchivedSegmentNotification {
97+ archived_segment, ..
98+ } ) = archived_segment_notification_stream. next ( ) . await
99+ {
100+ let segment_index = archived_segment. root_block . segment_index ( ) ;
101+ let first_piece_index = segment_index * u64:: from ( PIECES_IN_SEGMENT ) ;
102+
103+ info ! ( %segment_index, "Processing a segment." ) ;
104+
105+ // skip repeating publication
106+ if let Some ( last_published_segment_index) = last_published_segment_index {
107+ if last_published_segment_index == segment_index {
108+ info ! ( ?segment_index, "Archived segment skipped." ) ;
109+ continue ;
93110 }
94- . in_current_span ( ) ,
95- ) ,
96- ) ;
97-
98- let mut archived_segment_notification_stream = subspace_link
99- . archived_segment_notification_stream ( )
100- . subscribe ( ) ;
101-
102- spawner. spawn_essential (
103- "archiver" ,
104- Some ( "subspace-networking" ) ,
105- Box :: pin ( {
106- let node = node. clone ( ) ;
107-
108- async move {
109- trace ! ( "Subspace DSN archiver started." ) ;
110-
111- let mut last_published_segment_index: Option < u64 > = None ;
112- while let Some ( ArchivedSegmentNotification {
113- archived_segment, ..
114- } ) = archived_segment_notification_stream. next ( ) . await
115- {
116- let segment_index = archived_segment. root_block . segment_index ( ) ;
117- let first_piece_index = segment_index * u64:: from ( PIECES_IN_SEGMENT ) ;
118-
119- info ! ( %segment_index, "Processing a segment." ) ;
120-
121- // skip repeating publication
122- if let Some ( last_published_segment_index) = last_published_segment_index {
123- if last_published_segment_index == segment_index {
124- info ! ( ?segment_index, "Archived segment skipped." ) ;
125- continue ;
126- }
127- }
128- let keys_iter = ( first_piece_index..)
129- . take ( archived_segment. pieces . count ( ) )
130- . map ( |idx| ( idx, PieceIndexHash :: from_index ( idx) ) )
131- . map ( |( idx, hash) | ( idx, hash. to_multihash ( ) ) ) ;
132-
111+ }
112+ let keys_iter = ( first_piece_index..)
113+ . take ( archived_segment. pieces . count ( ) )
114+ . map ( |idx| ( idx, PieceIndexHash :: from_index ( idx) ) )
115+ . map ( |( idx, hash) | ( idx, hash. to_multihash ( ) ) ) ;
116+
117+ spawner. spawn (
118+ "segment-publishing" ,
119+ Some ( "subspace-networking" ) ,
120+ Box :: pin ( {
121+ let node = node. clone ( ) ;
122+
123+ async move {
133124 for ( ( _idx, key) , piece) in keys_iter. zip ( archived_segment. pieces . as_pieces ( ) ) {
134- //TODO: restore annoucing after https://github.com/libp2p/rust-libp2p/issues/3048
125+ //TODO: restore announcing after https://github.com/libp2p/rust-libp2p/issues/3048
135126 // trace!(?key, ?idx, "Announcing key...");
136127 //
137128 // let announcing_result = node.start_announcing(key).await;
@@ -145,13 +136,12 @@ where
145136 //TODO: ensure republication of failed announcements
146137 }
147138
148- last_published_segment_index = Some ( segment_index) ;
149139 info ! ( %segment_index, "Segment processed." ) ;
150140 }
151- }
152- . in_current_span ( )
153- } ) ,
154- ) ;
141+ . in_current_span ( )
142+ } ) ,
143+ ) ;
155144
156- Ok ( node)
145+ last_published_segment_index = Some ( segment_index) ;
146+ }
157147}
0 commit comments