1- use alloy_primitives:: B256 ;
2- use indexmap:: IndexMap ;
31use std:: collections:: { BTreeMap , HashMap , VecDeque } ;
42
5- use alloy_primitives:: U256 ;
3+ use alloy_primitives:: { B256 , U256 } ;
4+ use indexmap:: IndexMap ;
65
76#[ derive( Debug , Clone ) ]
87pub struct MeteredTransaction {
@@ -39,23 +38,17 @@ impl ResourceTotals {
3938 fn accumulate ( & mut self , tx : & MeteredTransaction ) {
4039 self . gas_used = self . gas_used . saturating_add ( tx. gas_used ) ;
4140 self . execution_time_us = self . execution_time_us . saturating_add ( tx. execution_time_us ) ;
42- self . state_root_time_us = self
43- . state_root_time_us
44- . saturating_add ( tx. state_root_time_us ) ;
45- self . data_availability_bytes = self
46- . data_availability_bytes
47- . saturating_add ( tx. data_availability_bytes ) ;
41+ self . state_root_time_us = self . state_root_time_us . saturating_add ( tx. state_root_time_us ) ;
42+ self . data_availability_bytes =
43+ self . data_availability_bytes . saturating_add ( tx. data_availability_bytes ) ;
4844 }
4945
5046 fn subtract ( & mut self , tx : & MeteredTransaction ) {
5147 self . gas_used = self . gas_used . saturating_sub ( tx. gas_used ) ;
5248 self . execution_time_us = self . execution_time_us . saturating_sub ( tx. execution_time_us ) ;
53- self . state_root_time_us = self
54- . state_root_time_us
55- . saturating_sub ( tx. state_root_time_us ) ;
56- self . data_availability_bytes = self
57- . data_availability_bytes
58- . saturating_sub ( tx. data_availability_bytes ) ;
49+ self . state_root_time_us = self . state_root_time_us . saturating_sub ( tx. state_root_time_us ) ;
50+ self . data_availability_bytes =
51+ self . data_availability_bytes . saturating_sub ( tx. data_availability_bytes ) ;
5952 }
6053}
6154
@@ -129,11 +122,7 @@ pub struct BlockMetrics {
129122
130123impl BlockMetrics {
131124 pub fn new ( block_number : u64 ) -> Self {
132- Self {
133- block_number,
134- flashblocks : BTreeMap :: new ( ) ,
135- totals : ResourceTotals :: default ( ) ,
136- }
125+ Self { block_number, flashblocks : BTreeMap :: new ( ) , totals : ResourceTotals :: default ( ) }
137126 }
138127
139128 pub fn flashblock_count ( & self ) -> usize {
@@ -166,18 +155,12 @@ impl BlockMetrics {
166155 for flashblock in self . flashblocks . values ( ) {
167156 let totals = flashblock. totals ( ) ;
168157 self . totals . gas_used = self . totals . gas_used . saturating_add ( totals. gas_used ) ;
169- self . totals . execution_time_us = self
170- . totals
171- . execution_time_us
172- . saturating_add ( totals. execution_time_us ) ;
173- self . totals . state_root_time_us = self
174- . totals
175- . state_root_time_us
176- . saturating_add ( totals. state_root_time_us ) ;
177- self . totals . data_availability_bytes = self
178- . totals
179- . data_availability_bytes
180- . saturating_add ( totals. data_availability_bytes ) ;
158+ self . totals . execution_time_us =
159+ self . totals . execution_time_us . saturating_add ( totals. execution_time_us ) ;
160+ self . totals . state_root_time_us =
161+ self . totals . state_root_time_us . saturating_add ( totals. state_root_time_us ) ;
162+ self . totals . data_availability_bytes =
163+ self . totals . data_availability_bytes . saturating_add ( totals. data_availability_bytes ) ;
181164 }
182165 }
183166}
@@ -193,21 +176,15 @@ pub struct MeteringCache {
193176impl MeteringCache {
194177 /// Creates a new cache retaining at most `max_blocks` recent blocks.
195178 pub fn new ( max_blocks : usize ) -> Self {
196- Self {
197- max_blocks,
198- blocks : VecDeque :: new ( ) ,
199- block_index : HashMap :: new ( ) ,
200- }
179+ Self { max_blocks, blocks : VecDeque :: new ( ) , block_index : HashMap :: new ( ) }
201180 }
202181
203182 pub fn max_blocks ( & self ) -> usize {
204183 self . max_blocks
205184 }
206185
207186 pub fn block ( & self , block_number : u64 ) -> Option < & BlockMetrics > {
208- self . block_index
209- . get ( & block_number)
210- . and_then ( |& idx| self . blocks . get ( idx) )
187+ self . block_index . get ( & block_number) . and_then ( |& idx| self . blocks . get ( idx) )
211188 }
212189
213190 pub fn block_mut ( & mut self , block_number : u64 ) -> & mut BlockMetrics {
@@ -221,18 +198,15 @@ impl MeteringCache {
221198 self . block_index . insert ( block_number, idx) ;
222199
223200 self . evict_if_needed ( ) ;
224- self . blocks
225- . get_mut ( * self . block_index . get ( & block_number) . unwrap ( ) )
226- . unwrap ( )
201+ self . blocks . get_mut ( * self . block_index . get ( & block_number) . unwrap ( ) ) . unwrap ( )
227202 }
228203
229204 pub fn flashblock (
230205 & self ,
231206 block_number : u64 ,
232207 flashblock_index : u64 ,
233208 ) -> Option < & FlashblockMetrics > {
234- self . block ( block_number)
235- . and_then ( |block| block. flashblock ( flashblock_index) )
209+ self . block ( block_number) . and_then ( |block| block. flashblock ( flashblock_index) )
236210 }
237211
238212 pub fn upsert_transaction (
@@ -320,10 +294,7 @@ mod tests {
320294 let block = cache. block ( 100 ) . unwrap ( ) ;
321295 let flashblock = block. flashblocks ( ) . next ( ) . unwrap ( ) ;
322296 assert_eq ! ( flashblock. len( ) , 1 ) ;
323- assert_eq ! (
324- flashblock. transactions( ) . next( ) . unwrap( ) . tx_hash,
325- tx1. tx_hash
326- ) ;
297+ assert_eq ! ( flashblock. transactions( ) . next( ) . unwrap( ) . tx_hash, tx1. tx_hash) ;
327298 }
328299
329300 #[ test]
@@ -337,10 +308,7 @@ mod tests {
337308 let block = cache. block ( 100 ) . unwrap ( ) ;
338309 let flashblock = block. flashblocks ( ) . next ( ) . unwrap ( ) ;
339310 assert_eq ! ( flashblock. len( ) , 1 ) ;
340- assert_eq ! (
341- flashblock. transactions( ) . next( ) . unwrap( ) . gas_used,
342- tx1. gas_used
343- ) ;
311+ assert_eq ! ( flashblock. transactions( ) . next( ) . unwrap( ) . gas_used, tx1. gas_used) ;
344312 }
345313
346314 #[ test]
@@ -368,9 +336,6 @@ mod tests {
368336 . iter ( )
369337 . map ( |tx| tx. priority_fee_per_gas )
370338 . collect ( ) ;
371- assert_eq ! (
372- sorted,
373- vec![ U256 :: from( 10u64 ) , U256 :: from( 20u64 ) , U256 :: from( 30u64 ) ]
374- ) ;
339+ assert_eq ! ( sorted, vec![ U256 :: from( 10u64 ) , U256 :: from( 20u64 ) , U256 :: from( 30u64 ) ] ) ;
375340 }
376341}
0 commit comments