Skip to content

Commit

Permalink
Add a unit test for the simulation of orders (#132)
Browse files Browse the repository at this point in the history
## 📝 Summary

This PR moves the debug cli `debug-fake-order-sim` into a unit test for
the order simulator. The command was already in the form of a test.

## 💡 Motivation and Context

<!--- (Optional) Why is this change required? What problem does it
solve? Remove this section if not applicable. -->

---

## ✅ I have completed the following steps:

* [x] Run `make lint`
* [x] Run `make test`
* [x] Added tests (if applicable)
  • Loading branch information
ferranbt authored Aug 21, 2024
1 parent d81b321 commit 1886791
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 68 deletions.
68 changes: 0 additions & 68 deletions crates/rbuilder/src/bin/debug-fake-order-sim.rs

This file was deleted.

59 changes: 59 additions & 0 deletions crates/rbuilder/src/live_builder/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,62 @@ impl<DB: Database + Clone + Send + 'static> OrderSimulationPool<DB> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
building::testing::test_chain_state::{BlockArgs, NamedAddr, TestChainState, TxArgs},
live_builder::order_input::order_sink::OrderPoolCommand,
primitives::{MempoolTx, Order, TransactionSignedEcRecoveredWithBlobs},
};
use reth_primitives::U256;

#[tokio::test]
async fn test_simulate_order_to_coinbase() {
let test_context = TestChainState::new(BlockArgs::default().number(11)).unwrap();

// Create simulation core
let cancel = CancellationToken::new();
let provider_factory_reopener = ProviderFactoryReopener::new_from_existing_for_testing(
test_context.provider_factory().clone(),
)
.unwrap();

let sim_pool = OrderSimulationPool::new(provider_factory_reopener, 4, cancel.clone());
let (order_sender, order_receiver) = mpsc::unbounded_channel();
let orders_for_block = OrdersForBlock {
new_order_sub: order_receiver,
};

let mut sim_results = sim_pool.spawn_simulation_job(
test_context.block_building_context().clone(),
orders_for_block,
cancel.clone(),
);

// Create a simple tx that sends to coinbase 5 wei.
let coinbase_profit = 5;
// max_priority_fee will be 0
let tx_args = TxArgs::new_send_to_coinbase(NamedAddr::User(1), 0, coinbase_profit);
let tx = test_context.sign_tx(tx_args).unwrap();
let tx = TransactionSignedEcRecoveredWithBlobs::new_no_blobs(tx).unwrap();
order_sender
.send(OrderPoolCommand::Insert(Order::Tx(MempoolTx::new(tx))))
.unwrap();

// We expect to receive the simulation giving a profit of coinbase_profit since that's what we sent directly to coinbase.
// and we are not paying any priority fee
if let Some(command) = sim_results.orders.recv().await {
match command {
SimulatedOrderCommand::Simulation(sim_order) => {
assert_eq!(
sim_order.sim_value.coinbase_profit,
U256::from(coinbase_profit)
);
}
SimulatedOrderCommand::Cancellation(_) => panic!("Cancellation not expected"),
};
}
}
}

0 comments on commit 1886791

Please sign in to comment.