Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions server/src/listeners/order_book/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,42 @@ impl<T> BatchQueue<T> {
self.deque.front()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::types::inner::InnerL4Order;
use crate::order_book::types::{Coin, Px, Sz, Side};
use alloy::primitives::Address;

#[test]
fn test_snapshot_consistency_timestamp_mismatch() {
let user = Address::ZERO;
let coin = Coin::new("ETH");
let order1 = InnerL4Order {
user,
coin: coin.clone(),
side: Side::Bid,
limit_px: Px::new(1000),
sz: Sz::new(1),
oid: 1,
timestamp: 100,
trigger_condition: "".to_string(),
is_trigger: false,
trigger_px: "".to_string(),
is_position_tpsl: false,
reduce_only: false,
order_type: "".to_string(),
tif: None,
cloid: None,
};
let mut order2 = order1.clone();
order2.timestamp = 200;

let snapshot = Snapshots::new(HashMap::from_iter(vec![(coin.clone(), Snapshot::new(vec![order1], vec![]))]));
let expected = Snapshots::new(HashMap::from_iter(vec![(coin.clone(), Snapshot::new(vec![order2], vec![]))]));

let result = validate_snapshot_consistency(&snapshot, expected, false);
assert!(result.is_ok(), "Should succeed despite timestamp mismatch");
}
}
5 changes: 5 additions & 0 deletions server/src/order_book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub(crate) struct OrderBook<O> {
pub(crate) struct Snapshot<O>([Vec<O>; 2]);

impl<O: Clone> Snapshot<O> {
#[cfg(test)]
pub(crate) fn new(bids: Vec<O>, asks: Vec<O>) -> Self {
Self([bids, asks])
}

pub(crate) const fn as_ref(&self) -> &[Vec<O>; 2] {
&self.0
}
Expand Down
13 changes: 12 additions & 1 deletion server/src/types/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};

// L4Order: the struct we keep in the orderbook (computationally better)
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Eq)]
pub(crate) struct InnerL4Order {
pub user: Address,
pub coin: Coin,
Expand All @@ -30,6 +30,17 @@ pub(crate) struct InnerL4Order {
pub cloid: Option<String>,
}

impl PartialEq for InnerL4Order {
fn eq(&self, other: &Self) -> bool {
self.user == other.user
&& self.coin == other.coin
&& self.side == other.side
&& self.limit_px == other.limit_px
&& self.sz == other.sz
&& self.oid == other.oid
}
}

impl InnerOrder for InnerL4Order {
fn oid(&self) -> Oid {
Oid::new(self.oid)
Expand Down