2727//! In the future, there will be a fallback for allowing sending the same message 
2828//! under certain conditions that are used to un-stick the protocol. 
2929
30- use  futures:: { prelude:: * ,  sync :: mpsc } ; 
30+ use  futures:: prelude:: * ; 
3131use  futures03:: { 
3232	channel:: mpsc as  mpsc03, 
3333	compat:: Compat , 
34- 	future:: { Future  as  Future03 } , 
35- 	stream:: StreamExt , 
34+ 	future:: { self  as  future03,  Future  as  Future03 } , 
35+ 	sink:: Sink  as  Sink03 , 
36+ 	stream:: { Stream  as  Stream03 ,  StreamExt } , 
3637} ; 
3738use  log:: { debug,  trace} ; 
3839use  parking_lot:: Mutex ; 
@@ -276,8 +277,8 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
276277		local_key :  Option < AuthorityPair > , 
277278		has_voted :  HasVoted < B > , 
278279	)  -> ( 
279- 		impl  Stream < Item =SignedMessage < B > , Error = Error > , 
280- 		impl   Sink < SinkItem = Message < B > , SinkError = Error > , 
280+ 		impl  Stream03 < Item =SignedMessage < B > >  +  Unpin , 
281+ 		OutgoingMessages < B > , 
281282	)  { 
282283		self . note_round ( 
283284			round, 
@@ -295,22 +296,20 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
295296		} ) ; 
296297
297298		let  topic = round_topic :: < B > ( round. 0 ,  set_id. 0 ) ; 
298- 		let  incoming = Compat :: new ( self . gossip_engine . messages_for ( topic) 
299- 			. map ( |item| Ok :: < _ ,  ( ) > ( item) ) ) 
300- 			. filter_map ( |notification| { 
299+ 		let  incoming = self . gossip_engine . messages_for ( topic) 
300+ 			. filter_map ( move  |notification| { 
301301				let  decoded = GossipMessage :: < B > :: decode ( & mut  & notification. message [ ..] ) ; 
302- 				if  let  Err ( ref  e)  = decoded { 
303- 					debug ! ( target:  "afg" ,  "Skipping malformed message {:?}: {}" ,  notification,  e) ; 
304- 				} 
305- 				decoded. ok ( ) 
306- 			} ) 
307- 			. and_then ( move  |msg| { 
308- 				match  msg { 
309- 					GossipMessage :: Vote ( msg)  => { 
302+ 
303+ 				match  decoded { 
304+ 					Err ( ref  e)  => { 
305+ 						debug ! ( target:  "afg" ,  "Skipping malformed message {:?}: {}" ,  notification,  e) ; 
306+ 						return  future03:: ready ( None ) ; 
307+ 					} 
308+ 					Ok ( GossipMessage :: Vote ( msg) )  => { 
310309						// check signature. 
311310						if  !voters. contains_key ( & msg. message . id )  { 
312311							debug ! ( target:  "afg" ,  "Skipping message from unknown voter {}" ,  msg. message. id) ; 
313- 							return  Ok ( None ) ; 
312+ 							return  future03 :: ready ( None ) ; 
314313						} 
315314
316315						if  voters. len ( )  <= TELEMETRY_VOTERS_LIMIT  { 
@@ -339,18 +338,16 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
339338							} ; 
340339						} 
341340
342- 						Ok ( Some ( msg. message ) ) 
341+ 						future03 :: ready ( Some ( msg. message ) ) 
343342					} 
344343					_ => { 
345344						debug ! ( target:  "afg" ,  "Skipping unknown message type" ) ; 
346- 						return  Ok ( None ) ; 
345+ 						return  future03 :: ready ( None ) ; 
347346					} 
348347				} 
349- 			} ) 
350- 			. filter_map ( |x| x) 
351- 			. map_err ( |( ) | Error :: Network ( format ! ( "Failed to receive message on unbounded stream" ) ) ) ; 
348+ 			} ) ; 
352349
353- 		let  ( tx,  out_rx)  = mpsc :: unbounded ( ) ; 
350+ 		let  ( tx,  out_rx)  = mpsc03 :: channel ( 0 ) ; 
354351		let  outgoing = OutgoingMessages :: < B >  { 
355352			round :  round. 0 , 
356353			set_id :  set_id. 0 , 
@@ -360,14 +357,10 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
360357			has_voted, 
361358		} ; 
362359
363- 		let  out_rx = out_rx. map_err ( move  |( ) | Error :: Network ( 
364- 			format ! ( "Failed to receive on unbounded receiver for round {}" ,  round. 0 ) 
365- 		) ) ; 
366- 
367360		// Combine incoming votes from external GRANDPA nodes with outgoing 
368361		// votes from our own GRANDPA voter to have a single 
369362		// vote-import-pipeline. 
370- 		let  incoming = incoming . select ( out_rx) ; 
363+ 		let  incoming = futures03 :: stream :: select ( incoming ,   out_rx) ; 
371364
372365		( incoming,  outgoing) 
373366	} 
@@ -690,21 +683,29 @@ pub(crate) fn check_message_sig_with_buffer<Block: BlockT>(
690683/// use the same raw message and key to sign. This is currently true for 
691684/// `ed25519` and `BLS` signatures (which we might use in the future), care must 
692685/// be taken when switching to different key types. 
693- OutgoingMessages < Block :  BlockT >  { 
686+ pub ( crate )   struct  OutgoingMessages < Block :  BlockT >  { 
694687	round :  RoundNumber , 
695688	set_id :  SetIdNumber , 
696689	locals:  Option < ( AuthorityPair ,  AuthorityId ) > , 
697- 	sender :  mpsc :: UnboundedSender < SignedMessage < Block > > , 
690+ 	sender:  mpsc03 :: Sender < SignedMessage < Block > > , 
698691	network:  GossipEngine < Block > , 
699692	has_voted:  HasVoted < Block > , 
700693} 
701694
702- impl <Block :  BlockT > Sink  for  OutgoingMessages < Block > 
695+ impl<B :  BlockT > Unpin  for OutgoingMessages <B > { } 
696+ 
697+ impl < Block :  BlockT >  Sink03 < Message < Block > >  for  OutgoingMessages < Block > 
703698{ 
704- 	type  SinkItem  = Message < Block > ; 
705- 	type  SinkError  = Error ; 
699+ 	type  Error  = Error ; 
700+ 
701+ 	fn  poll_ready ( mut  self :  Pin < & mut  Self > ,  cx :  & mut  Context )  -> Poll03 < Result < ( ) ,  Self :: Error > >  { 
702+ 		Sink03 :: poll_ready ( Pin :: new ( & mut  self . sender ) ,  cx) 
703+ 			. map ( |elem| {  elem. map_err ( |e| { 
704+ 				Error :: Network ( format ! ( "Failed to poll_ready channel sender: {:?}" ,  e) ) 
705+ 			} ) } ) 
706+ 	} 
706707
707- 	fn  start_send ( & mut  self ,  mut  msg :  Message < Block > )  -> StartSend < Message < Block > ,   Error >  { 
708+ 	fn  start_send ( mut  self :   Pin < & mut   Self > ,  mut  msg :  Message < Block > )  -> Result < ( ) ,   Self :: Error >  { 
708709		// if we've voted on this round previously under the same key, send that vote instead 
709710		match  & mut  msg { 
710711			finality_grandpa:: Message :: PrimaryPropose ( ref  mut  vote)  =>
@@ -760,17 +761,23 @@ impl<Block: BlockT> Sink for OutgoingMessages<Block>
760761			self . network . gossip_message ( topic,  message. encode ( ) ,  false ) ; 
761762
762763			// forward the message to the inner sender. 
763- 			let  _ = self . sender . unbounded_send ( signed) ; 
764- 		} 
764+ 			return  self . sender . start_send ( signed) . map_err ( |e| { 
765+ 				Error :: Network ( format ! ( "Failed to start_send on channel sender: {:?}" ,  e) ) 
766+ 			} ) ; 
767+ 		} ; 
765768
766- 		Ok ( AsyncSink :: Ready ) 
769+ 		Ok ( ( ) ) 
767770	} 
768771
769- 	fn  poll_complete ( & mut  self )  -> Poll < ( ) ,  Error >  {  Ok ( Async :: Ready ( ( ) ) )  } 
772+ 	fn  poll_flush ( self :  Pin < & mut  Self > ,  _cx :  & mut  Context )  -> Poll03 < Result < ( ) ,  Self :: Error > >  { 
773+ 		Poll03 :: Ready ( Ok ( ( ) ) ) 
774+ 	} 
770775
771- 	fn  close ( & mut  self )  -> Poll < ( ) ,  Error >  { 
772- 		// ignore errors since we allow this inner sender to be closed already. 
773- 		self . sender . close ( ) . or_else ( |_| Ok ( Async :: Ready ( ( ) ) ) ) 
776+ 	fn  poll_close ( mut  self :  Pin < & mut  Self > ,  cx :  & mut  Context )  -> Poll03 < Result < ( ) ,  Self :: Error > >  { 
777+ 		Sink03 :: poll_close ( Pin :: new ( & mut  self . sender ) ,  cx) 
778+ 			. map ( |elem| {  elem. map_err ( |e| { 
779+ 				Error :: Network ( format ! ( "Failed to poll_close channel sender: {:?}" ,  e) ) 
780+ 			} ) } ) 
774781	} 
775782} 
776783
0 commit comments