44//! individual flashblocks. This is essentially where we define the block /
55//! flashblock partitioning logic.
66
7- use std:: sync:: { Arc , Mutex } ;
8- use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
97use {
108 crate :: Flashblocks ,
119 core:: time:: Duration ,
1210 rblib:: { alloy:: consensus:: BlockHeader , prelude:: * } ,
13- std:: ops:: { Div , Rem } ,
11+ std:: {
12+ ops:: { Div , Rem } ,
13+ sync:: {
14+ Arc ,
15+ Mutex ,
16+ atomic:: { AtomicU64 , Ordering } ,
17+ } ,
18+ } ,
1419} ;
1520
1621/// Specifies the limits for individual flashblocks.
@@ -35,25 +40,29 @@ pub struct FlashblockState {
3540 /// None - uninitialized state, happens only on first block building job
3641 current_block : Option < u64 > ,
3742 /// Current flashblock being built, number based
38- /// 0 - uninitialized state, use progress_state to initialize it
43+ /// 0 - uninitialized state, use ` progress_state` to initialize it
3944 current_flashblock : u64 ,
4045 /// Interval of the first flashblock, it absorbs leeway time and network lag
4146 first_flashblock_interval : Duration ,
4247 /// Gas for flashblock used for current block
4348 gas_per_flashblock : u64 ,
44- /// Used to communicate maximum number of flashblocks on every blocks for other steps
45- // TODO: once we remove max_flashblocks from publish step we could change it to u64
49+ /// Used to communicate maximum number of flashblocks on every blocks for
50+ /// other steps
51+ // TODO: once we remove max_flashblocks from publish step we could change it
52+ // to u64
4653 max_flashblocks : Arc < AtomicU64 > ,
4754}
4855
4956impl FlashblockState {
5057 fn current_gas_limit ( & self ) -> u64 {
51- self . gas_per_flashblock . saturating_mul ( self . current_flashblock )
58+ self
59+ . gas_per_flashblock
60+ . saturating_mul ( self . current_flashblock )
5261 }
5362}
5463impl FlashblockLimits {
5564 pub fn new ( interval : Duration , max_flashblocks : Arc < AtomicU64 > ) -> Self {
56- let state = FlashblockState {
65+ let state = FlashblockState {
5766 max_flashblocks,
5867 ..Default :: default ( )
5968 } ;
@@ -63,11 +72,13 @@ impl FlashblockLimits {
6372 }
6473 }
6574
66- /// Checks if we have started building new block, if so we need to reset the state
67- /// This will produce empty state, progress state before using it
68- pub fn update_state ( & self ,
75+ /// Checks if we have started building new block, if so we need to reset the
76+ /// state This will produce empty state, progress state before using it
77+ pub fn update_state (
78+ & self ,
6979 payload : & Checkpoint < Flashblocks > ,
70- enclosing : & Limits ) {
80+ enclosing : & Limits ,
81+ ) {
7182 let mut state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
7283
7384 if state. current_block != Some ( payload. block ( ) . number ( ) ) {
@@ -77,12 +88,15 @@ impl FlashblockLimits {
7788 let remaining_time =
7889 payload_deadline. saturating_sub ( payload. building_since ( ) . elapsed ( ) ) ;
7990
80- let ( target_flashblock, first_flashblock_interval) = self . calculate_flashblocks ( payload, remaining_time) ;
91+ let ( target_flashblock, first_flashblock_interval) =
92+ self . calculate_flashblocks ( payload, remaining_time) ;
8193 state. gas_per_flashblock = enclosing. gas_limit / target_flashblock;
8294 state. current_block = Some ( payload. block ( ) . number ( ) ) ;
8395 state. current_flashblock = 0 ;
8496 state. first_flashblock_interval = first_flashblock_interval;
85- state. max_flashblocks . store ( target_flashblock, Ordering :: Relaxed ) ;
97+ state
98+ . max_flashblocks
99+ . store ( target_flashblock, Ordering :: Relaxed ) ;
86100 }
87101 }
88102
@@ -96,20 +110,20 @@ impl FlashblockLimits {
96110 pub fn get_limits ( & self , enclosing : & Limits ) -> Limits {
97111 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
98112 // Check that state was progressed at least once
99- assert_ne ! ( state. current_flashblock, 0 , "Get limits on uninitialized state" ) ;
113+ assert_ne ! (
114+ state. current_flashblock, 0 ,
115+ "Get limits on uninitialized state"
116+ ) ;
100117 // If we don't need to create new flashblocks - exit with immediate deadline
101- if state. current_flashblock > state. max_flashblocks . load ( Ordering :: Relaxed ) {
118+ if state. current_flashblock > state. max_flashblocks . load ( Ordering :: Relaxed )
119+ {
102120 enclosing. with_deadline ( Duration :: from_millis ( 1 ) )
103121 } else {
104122 // If self.current_flashblock == 1, we are building first flashblock
105123 let enclosing = if state. current_flashblock == 1 {
106- enclosing
107- . with_deadline ( state. first_flashblock_interval )
108-
124+ enclosing. with_deadline ( state. first_flashblock_interval )
109125 } else {
110- enclosing
111- . with_deadline ( self . interval )
112-
126+ enclosing. with_deadline ( self . interval )
113127 } ;
114128 enclosing. with_gas_limit ( state. current_gas_limit ( ) )
115129 }
@@ -130,10 +144,11 @@ impl FlashblockLimits {
130144 . timestamp ( )
131145 . saturating_sub ( payload. block ( ) . parent ( ) . header ( ) . timestamp ( ) ) ,
132146 ) ;
133- let remaining_time = remaining_time
134- . min ( block_time) ;
135- let interval_millis = self . interval . as_millis ( ) as u64 ;
136- let remaining_time_millis = remaining_time. as_millis ( ) as u64 ;
147+ let remaining_time = remaining_time. min ( block_time) ;
148+ let interval_millis = u64:: try_from ( self . interval . as_millis ( ) )
149+ . expect ( "interval_millis should never be greater than u64::MAX" ) ;
150+ let remaining_time_millis = u64:: try_from ( remaining_time. as_millis ( ) )
151+ . expect ( "remaining_time_millis should never be greater than u64::MAX" ) ;
137152 let first_flashblock_offset = remaining_time_millis. rem ( interval_millis) ;
138153
139154 if first_flashblock_offset == 0 {
@@ -165,13 +180,14 @@ impl ScopedLimits<Flashblocks> for FlashblockLimits {
165180 let limits = self . get_limits ( enclosing) ;
166181
167182 let state = self . state . lock ( ) . expect ( "mutex is not poisoned" ) ;
168- if state. current_flashblock <= state. max_flashblocks . load ( Ordering :: Relaxed ) {
183+ if state. current_flashblock <= state. max_flashblocks . load ( Ordering :: Relaxed )
184+ {
169185 let gas_used = payload. cumulative_gas_used ( ) ;
170186 let remaining_gas = enclosing. gas_limit . saturating_sub ( gas_used) ;
171187 tracing:: warn!(
172188 ">---> flashblocks: {}/{}, payload txs: {}, gas used: {} ({}%), \
173- gas_remaining: {} ({}%), next_block_gas_limit: {} ({}%), gas per block: {} ({}%), \
174- remaining_time: {}ms, gas_limit: {}",
189+ gas_remaining: {} ({}%), next_block_gas_limit: {} ({}%), gas per \
190+ block: {} ({}%), remaining_time: {}ms, gas_limit: {}",
175191 state. current_flashblock,
176192 state. max_flashblocks. load( Ordering :: Relaxed ) ,
177193 payload. history( ) . transactions( ) . count( ) ,
@@ -188,7 +204,5 @@ impl ScopedLimits<Flashblocks> for FlashblockLimits {
188204 ) ;
189205 }
190206 limits
191-
192207 }
193208}
194-
0 commit comments