@@ -163,7 +163,7 @@ struct InboundInfo<E: EthSpec> {
163
163
/// Protocol of the original request we received from the peer.
164
164
protocol : Protocol ,
165
165
/// Responses that the peer is still expecting from us.
166
- remaining_chunks : u64 ,
166
+ max_remaining_chunks : u64 ,
167
167
/// Useful to timing how long each request took to process. Currently only used by
168
168
/// BlocksByRange.
169
169
request_start_time : Instant ,
@@ -180,7 +180,7 @@ struct OutboundInfo<Id, E: EthSpec> {
180
180
/// Info over the protocol this substream is handling.
181
181
proto : Protocol ,
182
182
/// Number of chunks to be seen from the peer's response.
183
- remaining_chunks : Option < u64 > ,
183
+ max_remaining_chunks : Option < u64 > ,
184
184
/// `Id` as given by the application that sent the request.
185
185
req_id : Id ,
186
186
}
@@ -471,7 +471,7 @@ where
471
471
// Process one more message if one exists.
472
472
if let Some ( message) = info. pending_items . pop_front ( ) {
473
473
// If this is the last chunk, terminate the stream.
474
- let last_chunk = info. remaining_chunks <= 1 ;
474
+ let last_chunk = info. max_remaining_chunks <= 1 ;
475
475
let fut =
476
476
send_message_to_inbound_substream ( substream, message, last_chunk)
477
477
. boxed ( ) ;
@@ -537,7 +537,8 @@ where
537
537
{
538
538
// The substream is still active, decrement the remaining
539
539
// chunks expected.
540
- info. remaining_chunks = info. remaining_chunks . saturating_sub ( 1 ) ;
540
+ info. max_remaining_chunks =
541
+ info. max_remaining_chunks . saturating_sub ( 1 ) ;
541
542
542
543
// If this substream has not ended, we reset the timer.
543
544
// Each chunk is allowed RESPONSE_TIMEOUT to be sent.
@@ -552,7 +553,7 @@ where
552
553
// Process one more message if one exists.
553
554
if let Some ( message) = info. pending_items . pop_front ( ) {
554
555
// If this is the last chunk, terminate the stream.
555
- let last_chunk = info. remaining_chunks <= 1 ;
556
+ let last_chunk = info. max_remaining_chunks <= 1 ;
556
557
let fut = send_message_to_inbound_substream (
557
558
substream, message, last_chunk,
558
559
)
@@ -664,15 +665,19 @@ where
664
665
request,
665
666
} => match substream. poll_next_unpin ( cx) {
666
667
Poll :: Ready ( Some ( Ok ( response) ) ) => {
667
- if request. expected_responses ( ) > 1 && !response. close_after ( ) {
668
+ if request. expect_exactly_one_response ( ) || response. close_after ( ) {
669
+ // either this is a single response request or this response closes the
670
+ // stream
671
+ entry. get_mut ( ) . state = OutboundSubstreamState :: Closing ( substream) ;
672
+ } else {
668
673
let substream_entry = entry. get_mut ( ) ;
669
674
let delay_key = & substream_entry. delay_key ;
670
675
// chunks left after this one
671
- let remaining_chunks = substream_entry
672
- . remaining_chunks
676
+ let max_remaining_chunks = substream_entry
677
+ . max_remaining_chunks
673
678
. map ( |count| count. saturating_sub ( 1 ) )
674
679
. unwrap_or_else ( || 0 ) ;
675
- if remaining_chunks == 0 {
680
+ if max_remaining_chunks == 0 {
676
681
// this is the last expected message, close the stream as all expected chunks have been received
677
682
substream_entry. state = OutboundSubstreamState :: Closing ( substream) ;
678
683
} else {
@@ -682,14 +687,10 @@ where
682
687
substream,
683
688
request,
684
689
} ;
685
- substream_entry. remaining_chunks = Some ( remaining_chunks ) ;
690
+ substream_entry. max_remaining_chunks = Some ( max_remaining_chunks ) ;
686
691
self . outbound_substreams_delay
687
692
. reset ( delay_key, self . resp_timeout ) ;
688
693
}
689
- } else {
690
- // either this is a single response request or this response closes the
691
- // stream
692
- entry. get_mut ( ) . state = OutboundSubstreamState :: Closing ( substream) ;
693
694
}
694
695
695
696
// Check what type of response we got and report it accordingly
@@ -725,7 +726,16 @@ where
725
726
self . outbound_substreams_delay . remove ( delay_key) ;
726
727
entry. remove_entry ( ) ;
727
728
// notify the application error
728
- if request. expected_responses ( ) > 1 {
729
+ if request. expect_exactly_one_response ( ) {
730
+ // return an error, stream should not have closed early.
731
+ return Poll :: Ready ( ConnectionHandlerEvent :: NotifyBehaviour (
732
+ HandlerEvent :: Err ( HandlerErr :: Outbound {
733
+ id : request_id,
734
+ proto : request. versioned_protocol ( ) . protocol ( ) ,
735
+ error : RPCError :: IncompleteStream ,
736
+ } ) ,
737
+ ) ) ;
738
+ } else {
729
739
// return an end of stream result
730
740
return Poll :: Ready ( ConnectionHandlerEvent :: NotifyBehaviour (
731
741
HandlerEvent :: Ok ( RPCReceived :: EndOfStream (
@@ -734,16 +744,6 @@ where
734
744
) ) ,
735
745
) ) ;
736
746
}
737
-
738
- // else we return an error, stream should not have closed early.
739
- let outbound_err = HandlerErr :: Outbound {
740
- id : request_id,
741
- proto : request. versioned_protocol ( ) . protocol ( ) ,
742
- error : RPCError :: IncompleteStream ,
743
- } ;
744
- return Poll :: Ready ( ConnectionHandlerEvent :: NotifyBehaviour (
745
- HandlerEvent :: Err ( outbound_err) ,
746
- ) ) ;
747
747
}
748
748
Poll :: Pending => {
749
749
entry. get_mut ( ) . state =
@@ -880,10 +880,10 @@ where
880
880
}
881
881
882
882
let ( req, substream) = substream;
883
- let expected_responses = req. expected_responses ( ) ;
883
+ let max_responses = req. max_responses ( ) ;
884
884
885
885
// store requests that expect responses
886
- if expected_responses > 0 {
886
+ if max_responses > 0 {
887
887
if self . inbound_substreams . len ( ) < MAX_INBOUND_SUBSTREAMS {
888
888
// Store the stream and tag the output.
889
889
let delay_key = self
@@ -894,14 +894,13 @@ where
894
894
self . current_inbound_substream_id ,
895
895
InboundInfo {
896
896
state : awaiting_stream,
897
- pending_items : VecDeque :: with_capacity ( std:: cmp:: min (
898
- expected_responses,
899
- 128 ,
900
- ) as usize ) ,
897
+ pending_items : VecDeque :: with_capacity (
898
+ std:: cmp:: min ( max_responses, 128 ) as usize
899
+ ) ,
901
900
delay_key : Some ( delay_key) ,
902
901
protocol : req. versioned_protocol ( ) . protocol ( ) ,
903
902
request_start_time : Instant :: now ( ) ,
904
- remaining_chunks : expected_responses ,
903
+ max_remaining_chunks : max_responses ,
905
904
} ,
906
905
) ;
907
906
} else {
@@ -948,8 +947,14 @@ where
948
947
}
949
948
950
949
// add the stream to substreams if we expect a response, otherwise drop the stream.
951
- let expected_responses = request. expected_responses ( ) ;
952
- if expected_responses > 0 {
950
+ let max_responses = request. max_responses ( ) ;
951
+ if max_responses > 0 {
952
+ let max_remaining_chunks = if request. expect_exactly_one_response ( ) {
953
+ // Currently enforced only for multiple responses
954
+ None
955
+ } else {
956
+ Some ( max_responses)
957
+ } ;
953
958
// new outbound request. Store the stream and tag the output.
954
959
let delay_key = self
955
960
. outbound_substreams_delay
@@ -958,12 +963,6 @@ where
958
963
substream : Box :: new ( substream) ,
959
964
request,
960
965
} ;
961
- let expected_responses = if expected_responses > 1 {
962
- // Currently enforced only for multiple responses
963
- Some ( expected_responses)
964
- } else {
965
- None
966
- } ;
967
966
if self
968
967
. outbound_substreams
969
968
. insert (
@@ -972,7 +971,7 @@ where
972
971
state : awaiting_stream,
973
972
delay_key,
974
973
proto,
975
- remaining_chunks : expected_responses ,
974
+ max_remaining_chunks ,
976
975
req_id : id,
977
976
} ,
978
977
)
0 commit comments