@@ -92,7 +92,7 @@ pub const DEFAULT_MAX_BLOCK_RANGE: u64 = 1000;
9292// copied form https://github.com/taikoxyz/taiko-mono/blob/f4b3a0e830e42e2fee54829326389709dd422098/packages/taiko-client/pkg/chain_iterator/block_batch_iterator.go#L19
9393pub const DEFAULT_BLOCK_CONFIRMATIONS : u64 = 0 ;
9494
95- pub const MAX_BUFFERED_MESSAGES : usize = 50000 ;
95+ pub const DEFAULT_STREAM_BUFFER_CAPACITY : usize = 50000 ;
9696
9797// Maximum amount of reorged blocks on Ethereum (after this amount of block confirmations, a block
9898// is considered final)
@@ -120,10 +120,11 @@ impl IntoScannerResult<RangeInclusive<BlockNumber>> for RangeInclusive<BlockNumb
120120 }
121121}
122122
123- #[ derive( Clone , Debug ) ]
123+ #[ derive( Clone ) ]
124124pub struct BlockRangeScanner {
125125 pub max_block_range : u64 ,
126126 pub past_blocks_storage_capacity : RingBufferCapacity ,
127+ pub buffer_capacity : usize ,
127128}
128129
129130impl Default for BlockRangeScanner {
@@ -138,6 +139,7 @@ impl BlockRangeScanner {
138139 Self {
139140 max_block_range : DEFAULT_MAX_BLOCK_RANGE ,
140141 past_blocks_storage_capacity : RingBufferCapacity :: Limited ( 10 ) ,
142+ buffer_capacity : DEFAULT_STREAM_BUFFER_CAPACITY ,
141143 }
142144 }
143145
@@ -156,6 +158,12 @@ impl BlockRangeScanner {
156158 self
157159 }
158160
161+ #[ must_use]
162+ pub fn buffer_capacity ( mut self , buffer_capacity : usize ) -> Self {
163+ self . buffer_capacity = buffer_capacity;
164+ self
165+ }
166+
159167 /// Connects to an existing provider
160168 ///
161169 /// # Errors
@@ -165,20 +173,27 @@ impl BlockRangeScanner {
165173 self ,
166174 provider : impl IntoRobustProvider < N > ,
167175 ) -> Result < ConnectedBlockRangeScanner < N > , ScannerError > {
176+ if self . max_block_range == 0 {
177+ return Err ( ScannerError :: InvalidMaxBlockRange ) ;
178+ }
179+ if self . buffer_capacity == 0 {
180+ return Err ( ScannerError :: InvalidBufferCapacity ) ;
181+ }
168182 let provider = provider. into_robust_provider ( ) . await ?;
169183 Ok ( ConnectedBlockRangeScanner {
170184 provider,
171185 max_block_range : self . max_block_range ,
172186 past_blocks_storage_capacity : self . past_blocks_storage_capacity ,
187+ buffer_capacity : self . buffer_capacity ,
173188 } )
174189 }
175190}
176191
177- #[ derive( Debug ) ]
178192pub struct ConnectedBlockRangeScanner < N : Network > {
179193 provider : RobustProvider < N > ,
180194 max_block_range : u64 ,
181195 past_blocks_storage_capacity : RingBufferCapacity ,
196+ buffer_capacity : usize ,
182197}
183198
184199impl < N : Network > ConnectedBlockRangeScanner < N > {
@@ -188,6 +203,12 @@ impl<N: Network> ConnectedBlockRangeScanner<N> {
188203 & self . provider
189204 }
190205
206+ /// Returns the stream buffer capacity.
207+ #[ must_use]
208+ pub fn buffer_capacity ( & self ) -> usize {
209+ self . buffer_capacity
210+ }
211+
191212 /// Starts the subscription service and returns a client for sending commands.
192213 ///
193214 /// # Errors
@@ -202,7 +223,7 @@ impl<N: Network> ConnectedBlockRangeScanner<N> {
202223 tokio:: spawn ( async move {
203224 service. run ( ) . await ;
204225 } ) ;
205- Ok ( BlockRangeScannerClient :: new ( cmd_tx) )
226+ Ok ( BlockRangeScannerClient :: new ( cmd_tx, self . buffer_capacity ) )
206227 }
207228}
208229
@@ -584,6 +605,7 @@ impl<N: Network> Service<N> {
584605
585606pub struct BlockRangeScannerClient {
586607 command_sender : mpsc:: Sender < Command > ,
608+ buffer_capacity : usize ,
587609}
588610
589611impl BlockRangeScannerClient {
@@ -592,9 +614,10 @@ impl BlockRangeScannerClient {
592614 /// # Arguments
593615 ///
594616 /// * `command_sender` - The sender for sending commands to the subscription service.
617+ /// * `buffer_capacity` - The capacity for buffering messages in the stream.
595618 #[ must_use]
596- pub fn new ( command_sender : mpsc:: Sender < Command > ) -> Self {
597- Self { command_sender }
619+ pub fn new ( command_sender : mpsc:: Sender < Command > , buffer_capacity : usize ) -> Self {
620+ Self { command_sender, buffer_capacity }
598621 }
599622
600623 /// Streams live blocks starting from the latest block.
@@ -610,7 +633,7 @@ impl BlockRangeScannerClient {
610633 & self ,
611634 block_confirmations : u64 ,
612635 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
613- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
636+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
614637 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
615638
616639 let command = Command :: StreamLive {
@@ -641,7 +664,7 @@ impl BlockRangeScannerClient {
641664 start_id : impl Into < BlockId > ,
642665 end_id : impl Into < BlockId > ,
643666 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
644- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
667+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
645668 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
646669
647670 let command = Command :: StreamHistorical {
@@ -673,7 +696,7 @@ impl BlockRangeScannerClient {
673696 start_id : impl Into < BlockId > ,
674697 block_confirmations : u64 ,
675698 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
676- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
699+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
677700 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
678701
679702 let command = Command :: StreamFrom {
@@ -732,7 +755,7 @@ impl BlockRangeScannerClient {
732755 start_id : impl Into < BlockId > ,
733756 end_id : impl Into < BlockId > ,
734757 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
735- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
758+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
736759 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
737760
738761 let command = Command :: Rewind {
@@ -753,23 +776,28 @@ impl BlockRangeScannerClient {
753776#[ cfg( test) ]
754777mod tests {
755778 use super :: * ;
756- use alloy:: eips:: { BlockId , BlockNumberOrTag } ;
779+ use alloy:: {
780+ eips:: { BlockId , BlockNumberOrTag } ,
781+ network:: Ethereum ,
782+ providers:: { RootProvider , mock:: Asserter } ,
783+ rpc:: client:: RpcClient ,
784+ } ;
757785 use tokio:: sync:: mpsc;
758786
759787 #[ test]
760788 fn block_range_scanner_defaults_match_constants ( ) {
761789 let scanner = BlockRangeScanner :: new ( ) ;
762790
763791 assert_eq ! ( scanner. max_block_range, DEFAULT_MAX_BLOCK_RANGE ) ;
792+ assert_eq ! ( scanner. buffer_capacity, DEFAULT_STREAM_BUFFER_CAPACITY ) ;
764793 }
765794
766795 #[ test]
767796 fn builder_methods_update_configuration ( ) {
768- let max_block_range = 42 ;
797+ let scanner = BlockRangeScanner :: new ( ) . max_block_range ( 42 ) . buffer_capacity ( 33 ) ;
769798
770- let scanner = BlockRangeScanner :: new ( ) . max_block_range ( max_block_range) ;
771-
772- assert_eq ! ( scanner. max_block_range, max_block_range) ;
799+ assert_eq ! ( scanner. max_block_range, 42 ) ;
800+ assert_eq ! ( scanner. buffer_capacity, 33 ) ;
773801 }
774802
775803 #[ tokio:: test]
@@ -783,4 +811,20 @@ mod tests {
783811 Some ( Err ( ScannerError :: BlockNotFound ( BlockId :: Number ( BlockNumberOrTag :: Number ( 4 ) ) ) ) )
784812 ) ) ;
785813 }
814+
815+ #[ tokio:: test]
816+ async fn returns_error_with_zero_buffer_capacity ( ) {
817+ let provider = RootProvider :: < Ethereum > :: new ( RpcClient :: mocked ( Asserter :: new ( ) ) ) ;
818+ let result = BlockRangeScanner :: new ( ) . buffer_capacity ( 0 ) . connect ( provider) . await ;
819+
820+ assert ! ( matches!( result, Err ( ScannerError :: InvalidBufferCapacity ) ) ) ;
821+ }
822+
823+ #[ tokio:: test]
824+ async fn returns_error_with_zero_max_block_range ( ) {
825+ let provider = RootProvider :: < Ethereum > :: new ( RpcClient :: mocked ( Asserter :: new ( ) ) ) ;
826+ let result = BlockRangeScanner :: new ( ) . max_block_range ( 0 ) . connect ( provider) . await ;
827+
828+ assert ! ( matches!( result, Err ( ScannerError :: InvalidMaxBlockRange ) ) ) ;
829+ }
786830}
0 commit comments