@@ -93,7 +93,7 @@ pub const DEFAULT_MAX_BLOCK_RANGE: u64 = 1000;
9393// copied form https://github.com/taikoxyz/taiko-mono/blob/f4b3a0e830e42e2fee54829326389709dd422098/packages/taiko-client/pkg/chain_iterator/block_batch_iterator.go#L19
9494pub const DEFAULT_BLOCK_CONFIRMATIONS : u64 = 0 ;
9595
96- pub const MAX_BUFFERED_MESSAGES : usize = 50000 ;
96+ pub const DEFAULT_STREAM_BUFFER_CAPACITY : usize = 50000 ;
9797
9898// Maximum amount of reorged blocks on Ethereum (after this amount of block confirmations, a block
9999// is considered final)
@@ -121,10 +121,11 @@ impl IntoScannerResult<RangeInclusive<BlockNumber>> for RangeInclusive<BlockNumb
121121 }
122122}
123123
124- #[ derive( Clone , Debug ) ]
124+ #[ derive( Clone ) ]
125125pub struct BlockRangeScanner {
126126 pub max_block_range : u64 ,
127127 pub past_blocks_storage_capacity : RingBufferCapacity ,
128+ pub buffer_capacity : usize ,
128129}
129130
130131impl Default for BlockRangeScanner {
@@ -139,6 +140,7 @@ impl BlockRangeScanner {
139140 Self {
140141 max_block_range : DEFAULT_MAX_BLOCK_RANGE ,
141142 past_blocks_storage_capacity : RingBufferCapacity :: Limited ( 10 ) ,
143+ buffer_capacity : DEFAULT_STREAM_BUFFER_CAPACITY ,
142144 }
143145 }
144146
@@ -157,6 +159,12 @@ impl BlockRangeScanner {
157159 self
158160 }
159161
162+ #[ must_use]
163+ pub fn buffer_capacity ( mut self , buffer_capacity : usize ) -> Self {
164+ self . buffer_capacity = buffer_capacity;
165+ self
166+ }
167+
160168 /// Connects to an existing provider
161169 ///
162170 /// # Errors
@@ -166,20 +174,27 @@ impl BlockRangeScanner {
166174 self ,
167175 provider : impl IntoRobustProvider < N > ,
168176 ) -> Result < ConnectedBlockRangeScanner < N > , ScannerError > {
177+ if self . max_block_range == 0 {
178+ return Err ( ScannerError :: InvalidMaxBlockRange ) ;
179+ }
180+ if self . buffer_capacity == 0 {
181+ return Err ( ScannerError :: InvalidBufferCapacity ) ;
182+ }
169183 let provider = provider. into_robust_provider ( ) . await ?;
170184 Ok ( ConnectedBlockRangeScanner {
171185 provider,
172186 max_block_range : self . max_block_range ,
173187 past_blocks_storage_capacity : self . past_blocks_storage_capacity ,
188+ buffer_capacity : self . buffer_capacity ,
174189 } )
175190 }
176191}
177192
178- #[ derive( Debug ) ]
179193pub struct ConnectedBlockRangeScanner < N : Network > {
180194 provider : RobustProvider < N > ,
181195 max_block_range : u64 ,
182196 past_blocks_storage_capacity : RingBufferCapacity ,
197+ buffer_capacity : usize ,
183198}
184199
185200impl < N : Network > ConnectedBlockRangeScanner < N > {
@@ -189,6 +204,12 @@ impl<N: Network> ConnectedBlockRangeScanner<N> {
189204 & self . provider
190205 }
191206
207+ /// Returns the stream buffer capacity.
208+ #[ must_use]
209+ pub fn buffer_capacity ( & self ) -> usize {
210+ self . buffer_capacity
211+ }
212+
192213 /// Starts the subscription service and returns a client for sending commands.
193214 ///
194215 /// # Errors
@@ -203,7 +224,7 @@ impl<N: Network> ConnectedBlockRangeScanner<N> {
203224 tokio:: spawn ( async move {
204225 service. run ( ) . await ;
205226 } ) ;
206- Ok ( BlockRangeScannerClient :: new ( cmd_tx) )
227+ Ok ( BlockRangeScannerClient :: new ( cmd_tx, self . buffer_capacity ) )
207228 }
208229}
209230
@@ -585,6 +606,7 @@ impl<N: Network> Service<N> {
585606
586607pub struct BlockRangeScannerClient {
587608 command_sender : mpsc:: Sender < Command > ,
609+ buffer_capacity : usize ,
588610}
589611
590612impl BlockRangeScannerClient {
@@ -593,9 +615,10 @@ impl BlockRangeScannerClient {
593615 /// # Arguments
594616 ///
595617 /// * `command_sender` - The sender for sending commands to the subscription service.
618+ /// * `buffer_capacity` - The capacity for buffering messages in the stream.
596619 #[ must_use]
597- pub fn new ( command_sender : mpsc:: Sender < Command > ) -> Self {
598- Self { command_sender }
620+ pub fn new ( command_sender : mpsc:: Sender < Command > , buffer_capacity : usize ) -> Self {
621+ Self { command_sender, buffer_capacity }
599622 }
600623
601624 /// Streams live blocks starting from the latest block.
@@ -611,7 +634,7 @@ impl BlockRangeScannerClient {
611634 & self ,
612635 block_confirmations : u64 ,
613636 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
614- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
637+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
615638 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
616639
617640 let command = Command :: StreamLive {
@@ -642,7 +665,7 @@ impl BlockRangeScannerClient {
642665 start_id : impl Into < BlockId > ,
643666 end_id : impl Into < BlockId > ,
644667 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
645- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
668+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
646669 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
647670
648671 let command = Command :: StreamHistorical {
@@ -674,7 +697,7 @@ impl BlockRangeScannerClient {
674697 start_id : impl Into < BlockId > ,
675698 block_confirmations : u64 ,
676699 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
677- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
700+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
678701 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
679702
680703 let command = Command :: StreamFrom {
@@ -733,7 +756,7 @@ impl BlockRangeScannerClient {
733756 start_id : impl Into < BlockId > ,
734757 end_id : impl Into < BlockId > ,
735758 ) -> Result < ReceiverStream < BlockScannerResult > , ScannerError > {
736- let ( blocks_sender, blocks_receiver) = mpsc:: channel ( MAX_BUFFERED_MESSAGES ) ;
759+ let ( blocks_sender, blocks_receiver) = mpsc:: channel ( self . buffer_capacity ) ;
737760 let ( response_tx, response_rx) = oneshot:: channel ( ) ;
738761
739762 let command = Command :: Rewind {
@@ -754,23 +777,28 @@ impl BlockRangeScannerClient {
754777#[ cfg( test) ]
755778mod tests {
756779 use super :: * ;
757- use alloy:: eips:: { BlockId , BlockNumberOrTag } ;
780+ use alloy:: {
781+ eips:: { BlockId , BlockNumberOrTag } ,
782+ network:: Ethereum ,
783+ providers:: { RootProvider , mock:: Asserter } ,
784+ rpc:: client:: RpcClient ,
785+ } ;
758786 use tokio:: sync:: mpsc;
759787
760788 #[ test]
761789 fn block_range_scanner_defaults_match_constants ( ) {
762790 let scanner = BlockRangeScanner :: new ( ) ;
763791
764792 assert_eq ! ( scanner. max_block_range, DEFAULT_MAX_BLOCK_RANGE ) ;
793+ assert_eq ! ( scanner. buffer_capacity, DEFAULT_STREAM_BUFFER_CAPACITY ) ;
765794 }
766795
767796 #[ test]
768797 fn builder_methods_update_configuration ( ) {
769- let max_block_range = 42 ;
798+ let scanner = BlockRangeScanner :: new ( ) . max_block_range ( 42 ) . buffer_capacity ( 33 ) ;
770799
771- let scanner = BlockRangeScanner :: new ( ) . max_block_range ( max_block_range) ;
772-
773- assert_eq ! ( scanner. max_block_range, max_block_range) ;
800+ assert_eq ! ( scanner. max_block_range, 42 ) ;
801+ assert_eq ! ( scanner. buffer_capacity, 33 ) ;
774802 }
775803
776804 #[ tokio:: test]
@@ -784,4 +812,20 @@ mod tests {
784812 Some ( Err ( ScannerError :: BlockNotFound ( BlockId :: Number ( BlockNumberOrTag :: Number ( 4 ) ) ) ) )
785813 ) ) ;
786814 }
815+
816+ #[ tokio:: test]
817+ async fn returns_error_with_zero_buffer_capacity ( ) {
818+ let provider = RootProvider :: < Ethereum > :: new ( RpcClient :: mocked ( Asserter :: new ( ) ) ) ;
819+ let result = BlockRangeScanner :: new ( ) . buffer_capacity ( 0 ) . connect ( provider) . await ;
820+
821+ assert ! ( matches!( result, Err ( ScannerError :: InvalidBufferCapacity ) ) ) ;
822+ }
823+
824+ #[ tokio:: test]
825+ async fn returns_error_with_zero_max_block_range ( ) {
826+ let provider = RootProvider :: < Ethereum > :: new ( RpcClient :: mocked ( Asserter :: new ( ) ) ) ;
827+ let result = BlockRangeScanner :: new ( ) . max_block_range ( 0 ) . connect ( provider) . await ;
828+
829+ assert ! ( matches!( result, Err ( ScannerError :: InvalidMaxBlockRange ) ) ) ;
830+ }
787831}
0 commit comments