55//! flashblock partitioning logic.
66
77use {
8- crate :: { Flashblocks , state:: FlashblockNumber } ,
8+ crate :: {
9+ Flashblocks ,
10+ state:: { FlashblockNumber , TargetFlashblocks } ,
11+ } ,
912 core:: time:: Duration ,
1013 rblib:: { alloy:: consensus:: BlockHeader , prelude:: * } ,
1114 std:: sync:: { Arc , Mutex } ,
@@ -33,7 +36,7 @@ pub struct FlashblockState {
3336 current_block : Option < u64 > ,
3437 /// Current flashblock number. Used to check if we're on the first
3538 /// flashblock or to adjust the target number of flashblocks for a block.
36- flashblock_number : Arc < FlashblockNumber > ,
39+ target_flashblocks : Arc < TargetFlashblocks > ,
3740 /// Duration for the first flashblock, which may be shortened to absorb
3841 /// timing variance.
3942 first_flashblock_interval : Duration ,
@@ -43,20 +46,16 @@ pub struct FlashblockState {
4346}
4447
4548impl FlashblockState {
46- fn current_gas_limit ( & self ) -> u64 {
49+ fn current_gas_limit ( & self , flashblock_number : & FlashblockNumber ) -> u64 {
4750 self
4851 . gas_per_flashblock
49- . saturating_mul ( self . flashblock_number . current ( ) )
52+ . saturating_mul ( flashblock_number. current ( ) )
5053 }
5154}
5255
5356impl FlashblockLimits {
54- pub fn new (
55- interval : Duration ,
56- flashblock_number : Arc < FlashblockNumber > ,
57- ) -> Self {
57+ pub fn new ( interval : Duration ) -> Self {
5858 let state = FlashblockState {
59- flashblock_number,
6059 ..Default :: default ( )
6160 } ;
6261 FlashblockLimits {
@@ -97,10 +96,7 @@ impl FlashblockLimits {
9796 . unwrap_or ( enclosing. gas_limit ) ;
9897 state. current_block = Some ( payload. block ( ) . number ( ) ) ;
9998 state. first_flashblock_interval = first_flashblock_interval;
100- state. flashblock_number . reset_current_flashblock ( ) ;
101- state
102- . flashblock_number
103- . set_target_flashblocks ( target_flashblock) ;
99+ state. target_flashblocks . set ( target_flashblock) ;
104100 }
105101 }
106102
@@ -111,18 +107,19 @@ impl FlashblockLimits {
111107 pub fn get_limits (
112108 & self ,
113109 enclosing : & Limits < Flashblocks > ,
110+ flashblock_number : & FlashblockNumber ,
114111 ) -> Limits < Flashblocks > {
115112 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
116113 // If flashblock number == 1, we're building the first flashblock
117- let deadline = if state . flashblock_number . current ( ) == 1 {
114+ let deadline = if flashblock_number. current ( ) == 1 {
118115 state. first_flashblock_interval
119116 } else {
120117 self . interval
121118 } ;
122119
123120 enclosing
124121 . with_deadline ( deadline)
125- . with_gas_limit ( state. current_gas_limit ( ) )
122+ . with_gas_limit ( state. current_gas_limit ( flashblock_number ) )
126123 }
127124
128125 /// Calculates the number of flashblocks and first flashblock interval for
@@ -153,27 +150,30 @@ impl ScopedLimits<Flashblocks> for FlashblockLimits {
153150 payload : & Checkpoint < Flashblocks > ,
154151 enclosing : & Limits < Flashblocks > ,
155152 ) -> Limits < Flashblocks > {
153+ let flashblock_number = payload. context ( ) ;
156154 // Check the state and reset if we started building next block
157155 self . update_state ( payload, enclosing) ;
158156
159- let limits = self . get_limits ( enclosing) ;
157+ let limits = self . get_limits ( enclosing, flashblock_number ) ;
160158
161159 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
162- if state. flashblock_number . in_bounds ( ) {
160+ let flashblock_number = payload. context ( ) ;
161+ if flashblock_number. current ( ) <= state. target_flashblocks . get ( ) {
163162 let gas_used = payload. cumulative_gas_used ( ) ;
164163 let remaining_gas = enclosing. gas_limit . saturating_sub ( gas_used) ;
165164 tracing:: info!(
166165 "Creating flashblocks limits: {}, payload txs: {}, gas used: {} \
167166 ({}%), gas_remaining: {} ({}%), next_block_gas_limit: {} ({}%), gas \
168167 per block: {} ({}%), remaining_time: {}ms, gas_limit: {}",
169- state . flashblock_number,
168+ flashblock_number,
170169 payload. history( ) . transactions( ) . count( ) ,
171170 gas_used,
172171 ( gas_used * 100 / enclosing. gas_limit) ,
173172 remaining_gas,
174173 ( remaining_gas * 100 / enclosing. gas_limit) ,
175- state. current_gas_limit( ) ,
176- ( state. current_gas_limit( ) * 100 / enclosing. gas_limit) ,
174+ state. current_gas_limit( flashblock_number) ,
175+ ( state. current_gas_limit( flashblock_number) * 100
176+ / enclosing. gas_limit) ,
177177 state. gas_per_flashblock,
178178 ( state. gas_per_flashblock * 100 / enclosing. gas_limit) ,
179179 limits. deadline. expect( "deadline is set" ) . as_millis( ) ,
0 commit comments