11//! In-memory cache for metering data used by the priority fee estimator.
22//!
3- //! Transactions are stored sorted by priority fee (descending) so the estimator
4- //! can iterate from highest to lowest fee without re-sorting on each request .
3+ //! Transactions are stored in sequencer order (highest priority fee first) as received
4+ //! from flashblock events .
55
66use std:: collections:: { BTreeMap , HashMap , VecDeque } ;
77
@@ -63,14 +63,14 @@ impl ResourceTotals {
6363
6464/// Metrics for a single flashblock within a block.
6565///
66- /// Transactions are stored sorted by priority fee in descending order (highest first).
66+ /// Transactions are stored in sequencer order (highest priority fee first).
6767#[ derive( Debug ) ]
6868pub struct FlashblockMetrics {
6969 /// Block number.
7070 pub block_number : u64 ,
7171 /// Flashblock index within the block.
7272 pub flashblock_index : u64 ,
73- /// Transactions sorted by priority fee descending .
73+ /// Transactions in sequencer order .
7474 transactions : Vec < MeteredTransaction > ,
7575 totals : ResourceTotals ,
7676}
@@ -86,23 +86,18 @@ impl FlashblockMetrics {
8686 }
8787 }
8888
89- /// Inserts a transaction, maintaining descending sort order by priority fee .
90- pub fn insert_transaction ( & mut self , tx : MeteredTransaction ) {
89+ /// Appends a transaction, preserving sequencer order.
90+ pub fn push_transaction ( & mut self , tx : MeteredTransaction ) {
9191 self . totals . accumulate ( & tx) ;
92- // Binary search for insertion point (descending order)
93- let pos = self
94- . transactions
95- . binary_search_by ( |probe| tx. priority_fee_per_gas . cmp ( & probe. priority_fee_per_gas ) )
96- . unwrap_or_else ( |pos| pos) ;
97- self . transactions . insert ( pos, tx) ;
92+ self . transactions . push ( tx) ;
9893 }
9994
10095 /// Returns the resource totals for this flashblock.
10196 pub fn totals ( & self ) -> ResourceTotals {
10297 self . totals
10398 }
10499
105- /// Returns transactions sorted by priority fee descending (highest first) .
100+ /// Returns transactions in sequencer order .
106101 pub fn transactions ( & self ) -> & [ MeteredTransaction ] {
107102 & self . transactions
108103 }
@@ -213,16 +208,16 @@ impl MeteringCache {
213208 self . blocks . get_mut ( * self . block_index . get ( & block_number) . unwrap ( ) ) . unwrap ( )
214209 }
215210
216- /// Inserts a transaction into the cache.
217- pub fn insert_transaction (
211+ /// Appends a transaction to the cache, preserving sequencer order .
212+ pub fn push_transaction (
218213 & mut self ,
219214 block_number : u64 ,
220215 flashblock_index : u64 ,
221216 tx : MeteredTransaction ,
222217 ) {
223218 let block = self . block_mut ( block_number) ;
224219 let ( flashblock, _) = block. flashblock_mut ( flashblock_index) ;
225- flashblock. insert_transaction ( tx) ;
220+ flashblock. push_transaction ( tx) ;
226221 block. recompute_totals ( ) ;
227222 }
228223
@@ -308,7 +303,7 @@ mod tests {
308303 fn insert_and_retrieve_transactions ( ) {
309304 let mut cache = MeteringCache :: new ( 12 ) ;
310305 let tx1 = test_tx ( 1 , 2 ) ;
311- cache. insert_transaction ( 100 , 0 , tx1. clone ( ) ) ;
306+ cache. push_transaction ( 100 , 0 , tx1. clone ( ) ) ;
312307
313308 let block = cache. block ( 100 ) . unwrap ( ) ;
314309 let flashblock = block. flashblocks ( ) . next ( ) . unwrap ( ) ;
@@ -317,26 +312,26 @@ mod tests {
317312 }
318313
319314 #[ test]
320- fn transactions_sorted_descending_by_priority_fee ( ) {
315+ fn transactions_preserve_sequencer_order ( ) {
321316 let mut cache = MeteringCache :: new ( 12 ) ;
322- // Insert in random order
323- cache. insert_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
324- cache. insert_transaction ( 100 , 0 , test_tx ( 2 , 30 ) ) ;
325- cache. insert_transaction ( 100 , 0 , test_tx ( 3 , 20 ) ) ;
317+ // Insert in sequencer order (highest priority first)
318+ cache. push_transaction ( 100 , 0 , test_tx ( 1 , 30 ) ) ;
319+ cache. push_transaction ( 100 , 0 , test_tx ( 2 , 20 ) ) ;
320+ cache. push_transaction ( 100 , 0 , test_tx ( 3 , 10 ) ) ;
326321
327322 let block = cache. block ( 100 ) . unwrap ( ) ;
328323 let flashblock = block. flashblocks ( ) . next ( ) . unwrap ( ) ;
329324 let fees: Vec < _ > =
330325 flashblock. transactions ( ) . iter ( ) . map ( |tx| tx. priority_fee_per_gas ) . collect ( ) ;
331- // Should be sorted descending: 30, 20, 10
326+ // Order should be preserved as inserted
332327 assert_eq ! ( fees, vec![ U256 :: from( 30u64 ) , U256 :: from( 20u64 ) , U256 :: from( 10u64 ) ] ) ;
333328 }
334329
335330 #[ test]
336331 fn evicts_old_blocks ( ) {
337332 let mut cache = MeteringCache :: new ( 2 ) ;
338333 for block_number in 0 ..3u64 {
339- cache. insert_transaction ( block_number, 0 , test_tx ( block_number, block_number) ) ;
334+ cache. push_transaction ( block_number, 0 , test_tx ( block_number, block_number) ) ;
340335 }
341336 assert ! ( cache. block( 0 ) . is_none( ) ) ;
342337 assert ! ( cache. block( 1 ) . is_some( ) ) ;
@@ -346,8 +341,8 @@ mod tests {
346341 #[ test]
347342 fn contains_block_returns_correct_values ( ) {
348343 let mut cache = MeteringCache :: new ( 10 ) ;
349- cache. insert_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
350- cache. insert_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
344+ cache. push_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
345+ cache. push_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
351346
352347 assert ! ( cache. contains_block( 100 ) ) ;
353348 assert ! ( cache. contains_block( 101 ) ) ;
@@ -358,9 +353,9 @@ mod tests {
358353 #[ test]
359354 fn clear_blocks_from_clears_subsequent_blocks ( ) {
360355 let mut cache = MeteringCache :: new ( 10 ) ;
361- cache. insert_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
362- cache. insert_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
363- cache. insert_transaction ( 102 , 0 , test_tx ( 3 , 30 ) ) ;
356+ cache. push_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
357+ cache. push_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
358+ cache. push_transaction ( 102 , 0 , test_tx ( 3 , 30 ) ) ;
364359
365360 let cleared = cache. clear_blocks_from ( 101 ) ;
366361
@@ -374,8 +369,8 @@ mod tests {
374369 #[ test]
375370 fn clear_blocks_from_returns_zero_when_no_match ( ) {
376371 let mut cache = MeteringCache :: new ( 10 ) ;
377- cache. insert_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
378- cache. insert_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
372+ cache. push_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
373+ cache. push_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
379374
380375 let cleared = cache. clear_blocks_from ( 200 ) ;
381376
@@ -386,9 +381,9 @@ mod tests {
386381 #[ test]
387382 fn clear_blocks_from_clears_all_blocks ( ) {
388383 let mut cache = MeteringCache :: new ( 10 ) ;
389- cache. insert_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
390- cache. insert_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
391- cache. insert_transaction ( 102 , 0 , test_tx ( 3 , 30 ) ) ;
384+ cache. push_transaction ( 100 , 0 , test_tx ( 1 , 10 ) ) ;
385+ cache. push_transaction ( 101 , 0 , test_tx ( 2 , 20 ) ) ;
386+ cache. push_transaction ( 102 , 0 , test_tx ( 3 , 30 ) ) ;
392387
393388 let cleared = cache. clear_blocks_from ( 100 ) ;
394389
0 commit comments