Skip to content

Commit b6b3637

Browse files
thedevbirbBrycePy
andauthored
feat(sequencer): fifo ordering (#237)
Co-authored-by: Suthiwat <[email protected]>
1 parent 1e056b1 commit b6b3637

5 files changed

Lines changed: 27 additions & 13 deletions

File tree

based/crates/common/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ pub struct GatewayArgs {
5656
/// Number of sims per loop
5757
#[arg(long = "sequencer.sim_threads", default_value_t = 5)]
5858
pub sim_threads: usize,
59+
/// Run the sequencer in fifo ordering mode. Note that this will result in using only one
60+
/// simulation thread. This must be used for chain replication testing.
61+
#[arg(long = "sequencer.fifo_ordering", default_value_t = false)]
62+
pub fifo_ordering: bool,
5963
/// vsync window in micros
6064
#[arg(long = "sequencer.vsync_window_us", default_value_t = 10)]
6165
pub vsync_window_us: usize,

based/crates/sequencer/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl From<&GatewayArgs> for SuperVisorConfig {
2626
pub struct SequencerConfig {
2727
pub frag_duration: Duration,
2828
pub n_per_loop: usize,
29+
pub fifo_ordering: bool,
2930
pub rpc_url: Url,
3031
pub evm_config: OpEvmConfig,
3132
/// If true, we will simulate txs at the top of each frag in the pools.
@@ -40,7 +41,8 @@ impl From<&GatewayArgs> for SequencerConfig {
4041
fn from(args: &GatewayArgs) -> Self {
4142
Self {
4243
frag_duration: Duration::from_millis(args.frag_duration_ms),
43-
n_per_loop: args.sim_threads,
44+
n_per_loop: if args.fifo_ordering { 1 } else { args.sim_threads },
45+
fifo_ordering: args.fifo_ordering,
4446
rpc_url: args.eth_client_url.clone(),
4547
simulate_tof_in_pools: false,
4648
evm_config: OpEvmConfig::new(args.chain.clone(), Default::default()),

based/crates/sequencer/src/sorting/frag_sequence.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ mod tests {
161161

162162
let config = SequencerConfig {
163163
frag_duration: Duration::from_millis(200),
164+
fifo_ordering: false,
164165
n_per_loop: 5,
165166
rpc_url: rpc_url.clone(),
166167
evm_config: evm_config.clone(),

based/crates/sequencer/src/sorting/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@ impl ActiveOrders {
5858
unreachable!("this should never happen");
5959
}
6060

61-
pub fn put(&mut self, tx: SimulatedTx) {
62-
let payment = tx.payment;
61+
pub fn put(&mut self, tx: SimulatedTx, fifo_ordering: bool) {
6362
let mut id = self.orders.len();
64-
let sender = tx.sender();
65-
for (i, order) in self.orders.iter_mut().enumerate().rev() {
66-
if order.sender() == sender {
67-
order.put(tx);
68-
return;
69-
}
70-
if payment < order.payment() {
71-
id = i;
63+
64+
if !fifo_ordering {
65+
let payment = tx.payment;
66+
let sender = tx.sender();
67+
for (i, order) in self.orders.iter_mut().enumerate().rev() {
68+
if order.sender() == sender {
69+
order.put(tx);
70+
return;
71+
}
72+
if payment < order.payment() {
73+
id = i;
74+
}
7275
}
7376
}
7477
// not found so we insert it at the id corresponding to the payment

based/crates/sequencer/src/sorting/sorting_data.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ pub struct SortingData<Db> {
103103
/// We wait until these are back before we apply the next
104104
/// and send the next round of simulations
105105
pub in_flight_sims: usize,
106+
/// Whether we should use fifo ordering instead of sorting by payments value.
107+
pub fifo_ordering: bool,
106108
/// Remaining orders to be sorted, ideally with top of frag (TOF)
107109
/// sim data. The TOF sim data can be used as a heuristic initial sort of
108110
/// the orders. The assumption is that applying some orders will not
@@ -160,6 +162,7 @@ impl<Db: DatabaseRead> SortingData<Db> {
160162
gas_remaining: seq.gas_remaining,
161163
da_config: data.config.da_config.clone(),
162164
da_remaining: seq.da_remaining,
165+
fifo_ordering: data.config.fifo_ordering,
163166
txs: vec![],
164167
start_t: Instant::now(),
165168
telemetry: Default::default(),
@@ -236,14 +239,15 @@ impl<Db> SortingData<Db> {
236239
);
237240

238241
let tx_to_put_back = if simulated_tx.gas_used() < self.gas_remaining &&
239-
self.next_to_be_applied.as_ref().is_none_or(|t| t.payment < simulated_tx.payment)
242+
self.next_to_be_applied.as_ref().is_none_or(|t| t.payment < simulated_tx.payment) &&
243+
!self.fifo_ordering
240244
{
241245
self.next_to_be_applied.replace(simulated_tx)
242246
} else {
243247
Some(simulated_tx)
244248
};
245249
if let Some(tx) = tx_to_put_back {
246-
self.tof_snapshot.put(tx)
250+
self.tof_snapshot.put(tx, self.fifo_ordering)
247251
}
248252
}
249253

0 commit comments

Comments
 (0)