1- use std:: { marker:: PhantomData , time:: Duration } ;
1+ #![ allow( unused) ]
2+
3+ use std:: { future, marker:: PhantomData , time:: Duration } ;
24
35use tokio:: sync:: mpsc:: { self , Receiver , Sender } ;
46use tokio_stream:: wrappers:: ReceiverStream ;
@@ -16,7 +18,7 @@ use alloy::{
1618} ;
1719
1820// copied form https://github.com/taikoxyz/taiko-mono/blob/f4b3a0e830e42e2fee54829326389709dd422098/packages/taiko-client/pkg/chain_iterator/block_batch_iterator.go#L19
19- const DEFAULT_BLOCKS_READ_PER_EPOCH : u64 = 1000 ;
21+ const DEFAULT_BLOCKS_READ_PER_EPOCH : usize = 1000 ;
2022const DEFAULT_RETRY_INTERVAL : Duration = Duration :: from_secs ( 12 ) ;
2123const DEFAULT_BLOCK_CONFIRMATIONS : u64 = 0 ;
2224const BACK_OFF_MAX_RETRIES : u64 = 5 ;
@@ -41,7 +43,7 @@ impl std::fmt::Display for BlockScannerError {
4143 BlockScannerError :: ErrEOF => write ! ( f, "end of block batch iterator" ) ,
4244 BlockScannerError :: ErrContinue => write ! ( f, "continue" ) ,
4345 BlockScannerError :: TerminalError ( height) => {
44- write ! ( f, "terminal error at block height {}" , height )
46+ write ! ( f, "terminal error at block height {height}" )
4547 }
4648 }
4749 }
@@ -53,7 +55,7 @@ pub type OnBlocksFunc<N> =
5355 fn ( <N as Network >:: BlockResponse , UpdateCurrentFunc , EndIterFunc ) -> anyhow:: Result < ( ) > ;
5456
5557pub struct BlockScannerBuilder < N : Network > {
56- blocks_read_per_epoch : u64 ,
58+ blocks_read_per_epoch : usize ,
5759 start_height : BlockNumberOrTag ,
5860 end_height : BlockNumberOrTag ,
5961 on_blocks : OnBlocksFunc < N > ,
@@ -69,6 +71,7 @@ impl<N: Network> Default for BlockScannerBuilder<N> {
6971}
7072
7173impl < N : Network > BlockScannerBuilder < N > {
74+ #[ must_use]
7275 pub fn new ( ) -> Self {
7376 Self {
7477 blocks_read_per_epoch : DEFAULT_BLOCKS_READ_PER_EPOCH ,
@@ -81,41 +84,53 @@ impl<N: Network> BlockScannerBuilder<N> {
8184 }
8285 }
8386
84- pub fn with_blocks_read_per_epoch ( & mut self , blocks_read_per_epoch : u64 ) -> & mut Self {
87+ #[ must_use]
88+ pub fn with_blocks_read_per_epoch ( & mut self , blocks_read_per_epoch : usize ) -> & mut Self {
8589 self . blocks_read_per_epoch = blocks_read_per_epoch;
8690 self
8791 }
8892
93+ #[ must_use]
8994 pub fn with_start_height ( & mut self , start_height : BlockNumberOrTag ) -> & mut Self {
9095 self . start_height = start_height;
9196 self
9297 }
9398
99+ #[ must_use]
94100 pub fn with_end_height ( & mut self , end_height : BlockNumberOrTag ) -> & mut Self {
95101 self . end_height = end_height;
96102 self
97103 }
98104
105+ #[ must_use]
99106 pub fn with_on_blocks ( & mut self , on_blocks : OnBlocksFunc < N > ) -> & mut Self {
100107 self . on_blocks = on_blocks;
101108 self
102109 }
103110
111+ #[ must_use]
104112 pub fn with_reorg_rewind_depth ( & mut self , reorg_rewind_depth : u64 ) -> & mut Self {
105113 self . reorg_rewind_depth = reorg_rewind_depth;
106114 self
107115 }
108116
117+ #[ must_use]
109118 pub fn with_retry_interval ( & mut self , retry_interval : Duration ) -> & mut Self {
110119 self . retry_interval = retry_interval;
111120 self
112121 }
113122
123+ #[ must_use]
114124 pub fn with_block_confirmations ( & mut self , block_confirmations : u64 ) -> & mut Self {
115125 self . block_confirmations = block_confirmations;
116126 self
117127 }
118128
129+ /// Connects to the provider via WebSocket
130+ ///
131+ /// # Errors
132+ ///
133+ /// Returns an error if the connection fails
119134 pub async fn connect_ws (
120135 self ,
121136 connect : WsConnect ,
@@ -124,6 +139,11 @@ impl<N: Network> BlockScannerBuilder<N> {
124139 Ok ( self . connect_client ( client) )
125140 }
126141
142+ /// Connects to the provider via IPC
143+ ///
144+ /// # Errors
145+ ///
146+ /// Returns an error if the connection fails
127147 pub async fn connect_ipc < T > (
128148 self ,
129149 connect : IpcConnect < T > ,
@@ -135,6 +155,7 @@ impl<N: Network> BlockScannerBuilder<N> {
135155 Ok ( self . connect_client ( client) )
136156 }
137157
158+ #[ must_use]
138159 pub fn connect_client ( self , client : RpcClient ) -> BlockScanner < RootProvider < N > , N > {
139160 let provider = RootProvider :: new ( client) ;
140161 self . connect_provider ( provider)
@@ -144,7 +165,7 @@ impl<N: Network> BlockScannerBuilder<N> {
144165 where
145166 P : Provider < N > ,
146167 {
147- let ( sender, receiver) = mpsc:: channel ( self . blocks_read_per_epoch . try_into ( ) . unwrap ( ) ) ;
168+ let ( sender, receiver) = mpsc:: channel ( self . blocks_read_per_epoch ) ;
148169
149170 BlockScanner {
150171 provider,
@@ -170,7 +191,7 @@ pub struct BlockScanner<P: Provider<N>, N: Network> {
170191 provider : P ,
171192 sender : Sender < Result < N :: BlockResponse , BlockScannerError > > ,
172193 receiver : Receiver < Result < N :: BlockResponse , BlockScannerError > > ,
173- blocks_read_per_epoch : u64 ,
194+ blocks_read_per_epoch : usize ,
174195 start_height : BlockNumberOrTag ,
175196 end_height : BlockNumberOrTag ,
176197 current : Header ,
@@ -190,6 +211,8 @@ where
190211 pub async fn start ( self ) -> ReceiverStream < Result < N :: BlockResponse , BlockScannerError > > {
191212 let receiver_stream = ReceiverStream :: new ( self . receiver ) ;
192213
214+ future:: ready ( ( ) ) . await ;
215+
193216 tokio:: spawn ( async move {
194217 if self . sender . send ( Err ( BlockScannerError :: ErrEOF { } ) ) . await . is_err ( ) { }
195218 } ) ;
@@ -205,6 +228,7 @@ mod tests {
205228 use alloy_node_bindings:: Anvil ;
206229 use tokio_stream:: StreamExt ;
207230
231+ #[ allow( clippy:: unnecessary_wraps) ]
208232 fn no_op_on_blocks < N : Network > (
209233 _block : <N as Network >:: BlockResponse ,
210234 _update_current : UpdateCurrentFunc ,
@@ -266,7 +290,7 @@ mod tests {
266290 let first = stream. next ( ) . await ;
267291 match first {
268292 Some ( Err ( BlockScannerError :: ErrEOF ) ) => { }
269- other => panic ! ( "expected first stream item to be ErrEOF, got: {:?}" , other ) ,
293+ other => panic ! ( "expected first stream item to be ErrEOF, got: {other :?}" ) ,
270294 }
271295 }
272296
0 commit comments