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 } ,
15+ tracing:: debug,
1216} ;
1317
1418/// Specifies the limits for individual flashblocks.
@@ -33,7 +37,7 @@ pub struct FlashblockState {
3337 current_block : Option < u64 > ,
3438 /// Current flashblock number. Used to check if we're on the first
3539 /// flashblock or to adjust the target number of flashblocks for a block.
36- flashblock_number : Arc < FlashblockNumber > ,
40+ target_flashblocks : Arc < TargetFlashblocks > ,
3741 /// Duration for the first flashblock, which may be shortened to absorb
3842 /// timing variance.
3943 first_flashblock_interval : Duration ,
@@ -43,20 +47,20 @@ pub struct FlashblockState {
4347}
4448
4549impl FlashblockState {
46- fn current_gas_limit ( & self ) -> u64 {
50+ fn current_gas_limit ( & self , flashblock_number : & FlashblockNumber ) -> u64 {
4751 self
4852 . gas_per_flashblock
49- . saturating_mul ( self . flashblock_number . current ( ) )
53+ . saturating_mul ( flashblock_number. current ( ) )
5054 }
5155}
5256
5357impl FlashblockLimits {
5458 pub fn new (
5559 interval : Duration ,
56- flashblock_number : Arc < FlashblockNumber > ,
60+ target_flashblocks : Arc < TargetFlashblocks > ,
5761 ) -> Self {
5862 let state = FlashblockState {
59- flashblock_number ,
63+ target_flashblocks ,
6064 ..Default :: default ( )
6165 } ;
6266 FlashblockLimits {
@@ -77,7 +81,7 @@ impl FlashblockLimits {
7781 pub fn update_state (
7882 & self ,
7983 payload : & Checkpoint < Flashblocks > ,
80- enclosing : & Limits ,
84+ enclosing : & Limits < Flashblocks > ,
8185 ) {
8286 let mut state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
8387
@@ -88,38 +92,45 @@ impl FlashblockLimits {
8892 let elapsed = payload. building_since ( ) . elapsed ( ) ;
8993 let remaining_time = payload_deadline. saturating_sub ( elapsed) ;
9094
91- let ( target_flashblock , first_flashblock_interval) =
95+ let ( target_flashblocks , first_flashblock_interval) =
9296 self . calculate_flashblocks ( payload, remaining_time) ;
9397
9498 state. gas_per_flashblock = enclosing
9599 . gas_limit
96- . checked_div ( target_flashblock )
100+ . checked_div ( target_flashblocks )
97101 . unwrap_or ( enclosing. gas_limit ) ;
98102 state. current_block = Some ( payload. block ( ) . number ( ) ) ;
99103 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) ;
104+ state. target_flashblocks . set ( target_flashblocks) ;
105+
106+ debug ! (
107+ target_flashblocks = target_flashblocks,
108+ first_flashblock_interval = ?first_flashblock_interval,
109+ "Set flashblock timing for this block"
110+ ) ;
104111 }
105112 }
106113
107114 /// Returns limits for the current flashblock.
108115 ///
109116 /// If all flashblocks have been produced, returns a deadline of 1ms to stop
110117 /// production.
111- pub fn get_limits ( & self , enclosing : & Limits ) -> Limits {
118+ pub fn get_limits (
119+ & self ,
120+ enclosing : & Limits < Flashblocks > ,
121+ flashblock_number : & FlashblockNumber ,
122+ ) -> Limits < Flashblocks > {
112123 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
113124 // If flashblock number == 1, we're building the first flashblock
114- let deadline = if state . flashblock_number . current ( ) == 1 {
125+ let deadline = if flashblock_number. current ( ) == 1 {
115126 state. first_flashblock_interval
116127 } else {
117128 self . interval
118129 } ;
119130
120131 enclosing
121132 . with_deadline ( deadline)
122- . with_gas_limit ( state. current_gas_limit ( ) )
133+ . with_gas_limit ( state. current_gas_limit ( flashblock_number ) )
123134 }
124135
125136 /// Calculates the number of flashblocks and first flashblock interval for
@@ -148,29 +159,32 @@ impl ScopedLimits<Flashblocks> for FlashblockLimits {
148159 fn create (
149160 & self ,
150161 payload : & Checkpoint < Flashblocks > ,
151- enclosing : & Limits ,
152- ) -> Limits {
162+ enclosing : & Limits < Flashblocks > ,
163+ ) -> Limits < Flashblocks > {
164+ let flashblock_number = payload. context ( ) ;
153165 // Check the state and reset if we started building next block
154166 self . update_state ( payload, enclosing) ;
155167
156- let limits = self . get_limits ( enclosing) ;
168+ let limits = self . get_limits ( enclosing, flashblock_number ) ;
157169
158170 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
159- if state. flashblock_number . in_bounds ( ) {
171+ let flashblock_number = payload. context ( ) ;
172+ if flashblock_number. current ( ) <= state. target_flashblocks . get ( ) {
160173 let gas_used = payload. cumulative_gas_used ( ) ;
161174 let remaining_gas = enclosing. gas_limit . saturating_sub ( gas_used) ;
162175 tracing:: info!(
163176 "Creating flashblocks limits: {}, payload txs: {}, gas used: {} \
164177 ({}%), gas_remaining: {} ({}%), next_block_gas_limit: {} ({}%), gas \
165178 per block: {} ({}%), remaining_time: {}ms, gas_limit: {}",
166- state . flashblock_number,
179+ flashblock_number,
167180 payload. history( ) . transactions( ) . count( ) ,
168181 gas_used,
169182 ( gas_used * 100 / enclosing. gas_limit) ,
170183 remaining_gas,
171184 ( remaining_gas * 100 / enclosing. gas_limit) ,
172- state. current_gas_limit( ) ,
173- ( state. current_gas_limit( ) * 100 / enclosing. gas_limit) ,
185+ state. current_gas_limit( flashblock_number) ,
186+ ( state. current_gas_limit( flashblock_number) * 100
187+ / enclosing. gas_limit) ,
174188 state. gas_per_flashblock,
175189 ( state. gas_per_flashblock * 100 / enclosing. gas_limit) ,
176190 limits. deadline. expect( "deadline is set" ) . as_millis( ) ,
0 commit comments