@@ -21,6 +21,7 @@ use crossbeam_channel::{bounded, select, Receiver, Sender};
2121
2222use std:: io:: { self , ErrorKind , Write } ;
2323use std:: net:: { IpAddr , Ipv4Addr , SocketAddr , TcpStream } ;
24+ use std:: ops:: ControlFlow ;
2425use std:: sync:: Arc ;
2526use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
2627
@@ -92,21 +93,26 @@ impl Connection {
9293 /// Request and process the specified blocks (in the specified order).
9394 /// See https://en.bitcoin.it/wiki/Protocol_documentation#getblocks for details.
9495 /// Defined as `&mut self` to prevent concurrent invocations (https://github.com/romanz/electrs/pull/526#issuecomment-934685515).
95- pub ( crate ) fn for_blocks < B , F > ( & mut self , blockhashes : B , mut func : F ) -> Result < ( ) >
96+ pub ( crate ) fn for_blocks < B , F , R > (
97+ & mut self ,
98+ blockhashes : B ,
99+ mut func : F ,
100+ ) -> Result < ControlFlow < R > >
96101 where
97102 B : IntoIterator < Item = BlockHash > ,
98- F : FnMut ( BlockHash , Block ) ,
103+ F : FnMut ( BlockHash , Block ) -> ControlFlow < R > ,
99104 {
100105 self . blocks_duration . observe_duration ( "total" , || {
101106 let blockhashes: Vec < BlockHash > = blockhashes. into_iter ( ) . collect ( ) ;
102107 if blockhashes. is_empty ( ) {
103- return Ok ( ( ) ) ;
108+ return Ok ( ControlFlow :: Continue ( ( ) ) ) ;
104109 }
105110 self . blocks_duration . observe_duration ( "request" , || {
106111 debug ! ( "loading {} blocks" , blockhashes. len( ) ) ;
107112 self . req_send . send ( Request :: get_blocks ( & blockhashes) )
108113 } ) ?;
109114
115+ let mut ret = ControlFlow :: Continue ( ( ) ) ;
110116 for hash in blockhashes {
111117 let block = self . blocks_duration . observe_duration ( "response" , || {
112118 let block = self
@@ -116,10 +122,13 @@ impl Connection {
116122 ensure ! ( block. block_hash( ) == hash, "got unexpected block" ) ;
117123 Ok ( block)
118124 } ) ?;
119- self . blocks_duration
120- . observe_duration ( "process" , || func ( hash, block) ) ;
125+ if ret. is_continue ( ) {
126+ ret = self
127+ . blocks_duration
128+ . observe_duration ( "process" , || func ( hash, block) ) ;
129+ }
121130 }
122- Ok ( ( ) )
131+ Ok ( ret )
123132 } )
124133 }
125134
0 commit comments