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