@@ -22,6 +22,7 @@ use crossbeam_channel::{bounded, select, Receiver, Sender};
2222
2323use std:: io:: { self , ErrorKind , Write } ;
2424use std:: net:: { IpAddr , Ipv4Addr , SocketAddr , TcpStream } ;
25+ use std:: ops:: ControlFlow ;
2526use std:: sync:: Arc ;
2627use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
2728
@@ -94,21 +95,26 @@ impl Connection {
9495 /// Request and process the specified blocks (in the specified order).
9596 /// See https://en.bitcoin.it/wiki/Protocol_documentation#getblocks for details.
9697 /// Defined as `&mut self` to prevent concurrent invocations (https://github.com/romanz/electrs/pull/526#issuecomment-934685515).
97- pub ( crate ) fn for_blocks < B , F > ( & mut self , blockhashes : B , mut func : F ) -> Result < ( ) >
98+ pub ( crate ) fn for_blocks < B , F , R > (
99+ & mut self ,
100+ blockhashes : B ,
101+ mut func : F ,
102+ ) -> Result < ControlFlow < R > >
98103 where
99104 B : IntoIterator < Item = BlockHash > ,
100- F : FnMut ( BlockHash , SerBlock ) ,
105+ F : FnMut ( BlockHash , SerBlock ) -> ControlFlow < R > ,
101106 {
102107 self . blocks_duration . observe_duration ( "total" , || {
103108 let blockhashes: Vec < BlockHash > = blockhashes. into_iter ( ) . collect ( ) ;
104109 if blockhashes. is_empty ( ) {
105- return Ok ( ( ) ) ;
110+ return Ok ( ControlFlow :: Continue ( ( ) ) ) ;
106111 }
107112 self . blocks_duration . observe_duration ( "request" , || {
108113 debug ! ( "loading {} blocks" , blockhashes. len( ) ) ;
109114 self . req_send . send ( Request :: get_blocks ( & blockhashes) )
110115 } ) ?;
111116
117+ let mut ret = ControlFlow :: Continue ( ( ) ) ;
112118 for hash in blockhashes {
113119 let block = self . blocks_duration . observe_duration ( "response" , || {
114120 let block = self
@@ -124,10 +130,13 @@ impl Connection {
124130 ) ;
125131 Ok ( block)
126132 } ) ?;
127- self . blocks_duration
128- . observe_duration ( "process" , || func ( hash, block) ) ;
133+ if ret. is_continue ( ) {
134+ ret = self
135+ . blocks_duration
136+ . observe_duration ( "process" , || func ( hash, block) ) ;
137+ }
129138 }
130- Ok ( ( ) )
139+ Ok ( ret )
131140 } )
132141 }
133142
0 commit comments