@@ -126,14 +126,21 @@ where
126126 }
127127 }
128128
129- /// Broadcasts the local [Shard] of a block to all peers.
130- ///
131- /// TODO: This should only send out the shard that was assigned to the local validator.
132- pub async fn try_broadcast_mine ( & mut self , commitment : B :: Commitment ) {
133- let available_shards = self . mailbox . get ( None , commitment, None ) . await ;
129+ /// Broadcasts a local [Shard] of a block to all peers, if the shard is present.
130+ pub async fn try_broadcast_shard ( & mut self , commitment : B :: Commitment , index : u16 ) {
131+ let shard = self
132+ . mailbox
133+ . get ( None , commitment, None )
134+ . await
135+ . iter ( )
136+ . find ( |c| c. chunk . index == index)
137+ . cloned ( ) ;
134138
135- for shard in available_shards {
139+ if let Some ( shard) = shard {
140+ debug ! ( %commitment, index, "broadcasted local shard to all peers" ) ;
136141 let _peers = self . mailbox . broadcast ( Recipients :: All , shard) . await ;
142+ } else {
143+ debug ! ( %commitment, index, "no local shard to broadcast" ) ;
137144 }
138145 }
139146
@@ -178,10 +185,20 @@ where
178185 // Attempt to decode the block from the recovered data.
179186 let block = B :: decode_cfg ( & mut recovered. as_slice ( ) , & self . block_codec_cfg ) ?;
180187
181- self . put_commitment ( block. height ( ) , block. digest ( ) , commitment)
182- . await ;
188+ // Persist the digest -> commitment mapping for future lookups.
189+ //
190+ // SAFETY: We just verified the block's integrity by reconstructing it from the chunks.
191+ unsafe {
192+ self . put_commitment ( block. height ( ) , block. digest ( ) , commitment)
193+ . await ;
194+ }
183195
184- info ! ( %commitment, ?block, "successfully reconstructed block" ) ;
196+ info ! (
197+ %commitment,
198+ digest = %block. digest( ) ,
199+ height = block. height( ) ,
200+ "successfully reconstructed block"
201+ ) ;
185202
186203 Ok ( Some ( block) )
187204 }
@@ -196,11 +213,16 @@ where
196213 _commitment : B :: Commitment ,
197214 _responder : oneshot:: Sender < B > ,
198215 ) -> Result < ( ) , ReconstructionError > {
199- todo ! ( "Subscribe to all chunks, reconstruct block when enough are available. " ) ;
216+ todo ! ( "Create subscription " ) ;
200217 }
201218
202219 /// Puts a coding commitment in the store, keyed by digest and block height.
203- pub async fn put_commitment (
220+ ///
221+ /// # Safety
222+ ///
223+ /// Callers of this function must ensure that the provided commitment is correct for the
224+ /// block with the given height and digest.
225+ pub async unsafe fn put_commitment (
204226 & mut self ,
205227 height : u64 ,
206228 digest : B :: Digest ,
0 commit comments