Skip to content

Commit e4633df

Browse files
committed
Trust sequencer ordering instead of sorting on insert
- Rename insert_transaction to push_transaction - Remove binary search insertion, just append in sequencer order - Update docs and test to reflect sequencer ordering is preserved
1 parent 475ec48 commit e4633df

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

crates/rpc/src/base/annotator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl ResourceAnnotator {
117117
let mut cache = self.cache.write();
118118
for tx_hash in &event.ordered_tx_hashes {
119119
if let Some(tx) = self.pending_transactions.shift_remove(tx_hash) {
120-
cache.insert_transaction(event.block_number, event.flashblock_index, tx);
120+
cache.push_transaction(event.block_number, event.flashblock_index, tx);
121121
matched += 1;
122122
} else {
123123
missed += 1;
@@ -201,9 +201,9 @@ mod tests {
201201
// Pre-populate cache with blocks 100, 101, 102
202202
{
203203
let mut c = cache.write();
204-
c.insert_transaction(100, 0, test_tx(1, 10));
205-
c.insert_transaction(101, 0, test_tx(2, 20));
206-
c.insert_transaction(102, 0, test_tx(3, 30));
204+
c.push_transaction(100, 0, test_tx(1, 10));
205+
c.push_transaction(101, 0, test_tx(2, 20));
206+
c.push_transaction(102, 0, test_tx(3, 30));
207207
}
208208

209209
assert!(cache.read().contains_block(100));
@@ -234,7 +234,7 @@ mod tests {
234234
// Pre-populate cache with block 100
235235
{
236236
let mut c = cache.write();
237-
c.insert_transaction(100, 0, test_tx(1, 10));
237+
c.push_transaction(100, 0, test_tx(1, 10));
238238
}
239239

240240
assert!(cache.read().contains_block(100));
@@ -261,7 +261,7 @@ mod tests {
261261
// Pre-populate cache with block 100
262262
{
263263
let mut c = cache.write();
264-
c.insert_transaction(100, 0, test_tx(1, 10));
264+
c.push_transaction(100, 0, test_tx(1, 10));
265265
}
266266

267267
assert!(cache.read().contains_block(100));

crates/rpc/src/base/cache.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
66
use 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)]
6868
pub 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

crates/rpc/src/base/estimator.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,8 @@ mod tests {
836836
let (cache, estimator) = setup_estimator(DEFAULT_LIMITS);
837837
{
838838
let mut guard = cache.write();
839-
guard.insert_transaction(1, 0, tx(10, 10));
840-
guard.insert_transaction(1, 0, tx(5, 10));
839+
guard.push_transaction(1, 0, tx(10, 10));
840+
guard.push_transaction(1, 0, tx(5, 10));
841841
}
842842
let mut demand = ResourceDemand::default();
843843
demand.gas_used = Some(15);
@@ -857,8 +857,8 @@ mod tests {
857857
let (cache, estimator) = setup_estimator(limits);
858858
{
859859
let mut guard = cache.write();
860-
guard.insert_transaction(1, 0, tx(10, 10));
861-
guard.insert_transaction(1, 0, tx(5, 10));
860+
guard.push_transaction(1, 0, tx(10, 10));
861+
guard.push_transaction(1, 0, tx(5, 10));
862862
}
863863
let mut demand = ResourceDemand::default();
864864
demand.gas_used = Some(15);
@@ -882,11 +882,11 @@ mod tests {
882882
{
883883
let mut guard = cache.write();
884884
// Block 1 → threshold 10
885-
guard.insert_transaction(1, 0, tx(10, 10));
886-
guard.insert_transaction(1, 0, tx(5, 10));
885+
guard.push_transaction(1, 0, tx(10, 10));
886+
guard.push_transaction(1, 0, tx(5, 10));
887887
// Block 2 → threshold 30
888-
guard.insert_transaction(2, 0, tx(30, 10));
889-
guard.insert_transaction(2, 0, tx(25, 10));
888+
guard.push_transaction(2, 0, tx(30, 10));
889+
guard.push_transaction(2, 0, tx(25, 10));
890890
}
891891

892892
let mut demand = ResourceDemand::default();

0 commit comments

Comments
 (0)