depthbook 0.7.0

In-process L2 order book store with fixed-capacity sorted sides, scale-9 integer prices, and sequence-gap detection
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Concurrent store holding one [`Book`] per venue and instrument.

use crate::book_state::BookState;
use crate::error::{Error, Result};
use crate::intern::InternedString;
use crate::types::{Book, Insertion, Level};
use dashmap::DashMap;
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

/// Owned map key: venue and instrument.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Key(String, String);

/// Lets the map be probed with two `&str` without building a `Key`, which would cost two
/// allocations on every delta. `Key` and `(&str, &str)` hash and compare identically
/// through this trait, so `DashMap::get(&(venue, inst) as &dyn KeyLike)` finds the entry.
trait KeyLike {
    fn venue(&self) -> &str;
    fn inst(&self) -> &str;
}

impl KeyLike for Key {
    fn venue(&self) -> &str {
        &self.0
    }
    fn inst(&self) -> &str {
        &self.1
    }
}

impl KeyLike for (&str, &str) {
    fn venue(&self) -> &str {
        self.0
    }
    fn inst(&self) -> &str {
        self.1
    }
}

impl Hash for dyn KeyLike + '_ {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.venue().hash(state);
        self.inst().hash(state);
    }
}

impl PartialEq for dyn KeyLike + '_ {
    fn eq(&self, other: &Self) -> bool {
        self.venue() == other.venue() && self.inst() == other.inst()
    }
}

impl Eq for dyn KeyLike + '_ {}

impl<'a> Borrow<dyn KeyLike + 'a> for Key {
    fn borrow(&self) -> &(dyn KeyLike + 'a) {
        self
    }
}

/// Counters describing what a [`BookStore`] has seen since it was created.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Stats {
    /// Number of books currently tracked.
    pub books: usize,
    /// Number of deltas rejected because they skipped a sequence number.
    pub sequence_gaps: u64,
    /// Number of snapshots applied.
    pub snapshots_applied: u64,
    /// Number of deltas applied.
    pub deltas_applied: u64,
    /// Number of snapshots rejected for being older than the book they would replace.
    pub snapshots_rejected: u64,
    /// Number of price levels discarded because a side was already full.
    ///
    /// Non-zero means a book is no longer a faithful copy of the venue's below the top
    /// [`MAX_LEVELS`](crate::MAX_LEVELS) levels. Harmless for top-of-book work, and the
    /// reason this crate is the wrong choice for full-depth archival.
    pub levels_dropped: u64,
}

/// Stores order books keyed by venue and instrument.
///
/// Books are held in a [`DashMap`], so updates to different instruments proceed
/// independently. Each book is guarded by a read-write lock: any number of readers may
/// hold a book while no writer does. Reads take the lock without blocking where possible
/// and fall back to a blocking acquire under write contention.
///
/// The intended usage is one writer per instrument — typically the task that owns the
/// exchange connection — with any number of concurrent readers.
///
/// # Example
///
/// ```
/// use depthbook::{BookStore, Level, f64_to_scale9};
///
/// let store = BookStore::new();
/// store.apply_snapshot(
///     "binance",
///     "BTC-USDT",
///     &[Level::new(f64_to_scale9(50_000.0), f64_to_scale9(1.0))],
///     &[Level::new(f64_to_scale9(50_010.0), f64_to_scale9(1.0))],
///     1,
///     1_700_000_000_000_000_000,
/// )?;
///
/// let book = store.snapshot("binance", "BTC-USDT", 10).unwrap();
/// assert_eq!(book.spread(), Some(f64_to_scale9(10.0)));
/// # Ok::<(), depthbook::Error>(())
/// ```
#[derive(Debug, Default)]
pub struct BookStore {
    books: DashMap<Key, Arc<BookState>>,
    sequence_gaps: AtomicU64,
    snapshots_applied: AtomicU64,
    deltas_applied: AtomicU64,
    snapshots_rejected: AtomicU64,
    levels_dropped: AtomicU64,
}

/// Reject levels no feed can legitimately send.
///
/// Quantity may be zero, which deletes the level in a delta, but not negative. Price may
/// not be negative.
fn validate(levels: &[Level]) -> Result<()> {
    for level in levels {
        if level.qty.raw() < 0 || level.price.raw() < 0 {
            return Err(Error::InvalidData(format!(
                "negative price or quantity: {} @ {}",
                level.qty, level.price
            )));
        }
    }
    Ok(())
}

impl BookStore {
    /// Create an empty store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace a book with a full snapshot and clear its gapped flag.
    ///
    /// Creates the book if it does not exist. Levels with zero quantity are skipped.
    /// A snapshot does not need to be the next sequence number, which is what makes it
    /// usable for recovery after a gap, but it may not be older than the book.
    ///
    /// `ts` is the exchange timestamp in nanoseconds since the Unix epoch.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidData`] if any level has a negative price or quantity.
    /// - [`Error::OutOfOrder`] if `seq` is below the book's current sequence number. The
    ///   snapshot is **not** applied and is counted in [`Stats::snapshots_rejected`]. If
    ///   the venue reset its sequence numbers, call [`BookStore::remove`] first.
    pub fn apply_snapshot(
        &self,
        venue: &str,
        inst: &str,
        bids: &[Level],
        asks: &[Level],
        seq: u64,
        ts: u64,
    ) -> Result<()> {
        validate(bids)?;
        validate(asks)?;

        let mut book = Book::new(
            InternedString::new(venue),
            InternedString::new(inst),
            ts,
            seq,
        );

        // Feeds send snapshots best-first. Levels are stored best-last, so inserting in
        // reverse appends each one instead of shifting the whole array.
        let mut dropped = 0u64;
        for level in bids.iter().rev() {
            if !level.is_empty() && book.bids.insert_outcome(*level, true) == Insertion::Dropped {
                dropped += 1;
            }
        }
        for level in asks.iter().rev() {
            if !level.is_empty() && book.asks.insert_outcome(*level, false) == Insertion::Dropped {
                dropped += 1;
            }
        }
        self.record_drops(venue, inst, dropped);

        let state = self.get_or_create(venue, inst, seq, ts);
        state.replace(book, ts).inspect_err(|e| {
            self.snapshots_rejected.fetch_add(1, Ordering::Relaxed);
            tracing::warn!(venue, inst, error = %e, "snapshot rejected");
        })?;
        self.snapshots_applied.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Apply an incremental update to an existing book.
    ///
    /// A level with zero quantity removes that price. `ts` is the exchange timestamp in
    /// nanoseconds since the Unix epoch.
    ///
    /// # Errors
    ///
    /// - [`Error::InvalidData`] if any level has a negative price or quantity.
    /// - [`Error::NotFound`] if no snapshot has been applied for this venue and instrument.
    /// - [`Error::SequenceGap`] if `seq` skips ahead of the book's sequence number. The
    ///   update is **not** applied and the book is flagged as gapped (see
    ///   [`BookStore::is_gapped`]) until the next snapshot. The gap is counted in
    ///   [`Stats::sequence_gaps`].
    ///
    /// An update whose sequence number is already known is ignored and returns `Ok`.
    pub fn apply_delta(
        &self,
        venue: &str,
        inst: &str,
        bids: &[Level],
        asks: &[Level],
        seq: u64,
        ts: u64,
    ) -> Result<()> {
        validate(bids)?;
        validate(asks)?;

        let state = self
            .books
            .get(&(venue, inst) as &dyn KeyLike)
            .ok_or_else(|| Error::NotFound {
                venue: venue.to_string(),
                inst: inst.to_string(),
            })?;

        // Counted inside the closure but reported outside it, so the write lock is not
        // held across the atomic and the tracing call.
        let mut dropped = 0u64;
        let applied = state
            .apply(
                |book| {
                    for level in bids {
                        if book.bids.insert_outcome(*level, true) == Insertion::Dropped {
                            dropped += 1;
                        }
                    }
                    for level in asks {
                        if book.asks.insert_outcome(*level, false) == Insertion::Dropped {
                            dropped += 1;
                        }
                    }
                },
                seq,
                ts,
            )
            .inspect_err(|e| {
                if let Error::SequenceGap { expected, received } = e {
                    self.sequence_gaps.fetch_add(1, Ordering::Relaxed);
                    tracing::warn!(
                        venue,
                        inst,
                        expected,
                        received,
                        "sequence gap; book needs a fresh snapshot"
                    );
                }
            })?;
        if !applied {
            return Ok(());
        }
        self.record_drops(venue, inst, dropped);
        self.deltas_applied.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Get a copy of a book, keeping at most `depth` levels per side.
    ///
    /// `depth` of `0` returns every level. Returns `None` if the book does not exist.
    pub fn snapshot(&self, venue: &str, inst: &str, depth: usize) -> Option<Book> {
        let state = self.books.get(&(venue, inst) as &dyn KeyLike)?;

        let mut book = state.snapshot();
        if depth > 0 {
            book.bids.truncate(depth);
            book.asks.truncate(depth);
        }
        Some(book)
    }

    /// Get the best bid and best ask.
    ///
    /// Returns `None` if the book does not exist or either side is empty. This is cheaper
    /// than [`BookStore::snapshot`], which copies the whole book.
    pub fn bbo(&self, venue: &str, inst: &str) -> Option<(Level, Level)> {
        self.books.get(&(venue, inst) as &dyn KeyLike)?.bbo()
    }

    /// Report whether a book is older than `max_age_ms`.
    ///
    /// A book that does not exist counts as stale.
    pub fn is_stale(&self, venue: &str, inst: &str, max_age_ms: u64) -> bool {
        match self.books.get(&(venue, inst) as &dyn KeyLike) {
            Some(state) => state.is_stale(max_age_ms),
            None => true,
        }
    }

    /// Report whether a book has rejected a delta for a sequence gap since its last
    /// snapshot.
    ///
    /// A gapped book still serves its last good state; this is how a reader finds out
    /// that state is behind the venue. A book that does not exist counts as gapped.
    pub fn is_gapped(&self, venue: &str, inst: &str) -> bool {
        match self.books.get(&(venue, inst) as &dyn KeyLike) {
            Some(state) => state.is_gapped(),
            None => true,
        }
    }

    /// Forget a book, returning whether it existed.
    ///
    /// Call this before the first snapshot of a new sequence when a venue restarts its
    /// numbering, for example after a reconnect, or when an instrument is delisted.
    pub fn remove(&self, venue: &str, inst: &str) -> bool {
        self.books.remove(&(venue, inst) as &dyn KeyLike).is_some()
    }

    /// Count levels lost to a full side, and say so once per batch rather than per level.
    fn record_drops(&self, venue: &str, inst: &str, dropped: u64) {
        if dropped == 0 {
            return;
        }
        self.levels_dropped.fetch_add(dropped, Ordering::Relaxed);
        tracing::debug!(
            venue,
            inst,
            dropped,
            "side full; deepest levels discarded to keep the book sorted"
        );
    }

    /// Current counters. See [`Stats`].
    pub fn stats(&self) -> Stats {
        Stats {
            books: self.books.len(),
            sequence_gaps: self.sequence_gaps.load(Ordering::Relaxed),
            snapshots_applied: self.snapshots_applied.load(Ordering::Relaxed),
            deltas_applied: self.deltas_applied.load(Ordering::Relaxed),
            snapshots_rejected: self.snapshots_rejected.load(Ordering::Relaxed),
            levels_dropped: self.levels_dropped.load(Ordering::Relaxed),
        }
    }

    fn get_or_create(&self, venue: &str, inst: &str, seq: u64, ts: u64) -> Arc<BookState> {
        self.books
            .entry(Key(venue.to_string(), inst.to_string()))
            .or_insert_with(|| {
                let book = Book::new(
                    InternedString::new(venue),
                    InternedString::new(inst),
                    ts,
                    seq,
                );
                Arc::new(BookState::new(book))
            })
            .clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::decimal::f64_to_scale9;

    const TS: u64 = 1_700_000_000_000_000_000;

    #[test]
    fn apply_snapshot_populates_both_sides() {
        let store = BookStore::new();

        let bids = vec![
            Level::new(f64_to_scale9(50000.0), f64_to_scale9(1.0)),
            Level::new(f64_to_scale9(49999.0), f64_to_scale9(2.0)),
        ];
        let asks = vec![
            Level::new(f64_to_scale9(50001.0), f64_to_scale9(1.5)),
            Level::new(f64_to_scale9(50002.0), f64_to_scale9(2.5)),
        ];

        store
            .apply_snapshot("binance", "BTC-USDT", &bids, &asks, 1, TS)
            .unwrap();

        let book = store.snapshot("binance", "BTC-USDT", 0).unwrap();
        assert_eq!(book.bids.count(), 2);
        assert_eq!(book.asks.count(), 2);
        assert_eq!(store.stats().snapshots_applied, 1);
    }

    #[test]
    fn apply_delta_adds_a_level() {
        let store = BookStore::new();
        let bids = vec![Level::new(f64_to_scale9(50000.0), f64_to_scale9(1.0))];
        let asks = vec![Level::new(f64_to_scale9(50001.0), f64_to_scale9(1.0))];

        store
            .apply_snapshot("binance", "BTC-USDT", &bids, &asks, 1, TS)
            .unwrap();

        let delta = vec![Level::new(f64_to_scale9(49999.0), f64_to_scale9(2.0))];
        store
            .apply_delta("binance", "BTC-USDT", &delta, &[], 2, TS + 1)
            .unwrap();

        let book = store.snapshot("binance", "BTC-USDT", 0).unwrap();
        assert_eq!(book.bids.count(), 2);
        assert_eq!(store.stats().deltas_applied, 1);
    }

    #[test]
    fn delta_with_a_gap_is_rejected_and_counted() {
        let store = BookStore::new();
        store
            .apply_snapshot("binance", "BTC-USDT", &[], &[], 1, TS)
            .unwrap();

        let result = store.apply_delta("binance", "BTC-USDT", &[], &[], 5, TS + 1);

        assert!(matches!(
            result,
            Err(Error::SequenceGap {
                expected: 2,
                received: 5
            })
        ));
        assert_eq!(store.stats().sequence_gaps, 1);
    }

    #[test]
    fn delta_keeps_the_exchange_timestamp() {
        let store = BookStore::new();
        store
            .apply_snapshot("binance", "BTC-USDT", &[], &[], 1, TS)
            .unwrap();
        store
            .apply_delta("binance", "BTC-USDT", &[], &[], 2, TS + 42)
            .unwrap();

        let book = store.snapshot("binance", "BTC-USDT", 0).unwrap();
        assert_eq!(book.ts, TS + 42);
    }

    #[test]
    fn snapshot_honours_depth() {
        let store = BookStore::new();
        let bids: Vec<Level> = (0..20)
            .map(|i| Level::new(f64_to_scale9(50000.0 - i as f64), f64_to_scale9(1.0)))
            .collect();
        let asks: Vec<Level> = (0..20)
            .map(|i| Level::new(f64_to_scale9(50001.0 + i as f64), f64_to_scale9(1.0)))
            .collect();

        store
            .apply_snapshot("binance", "BTC-USDT", &bids, &asks, 1, TS)
            .unwrap();

        let shallow = store.snapshot("binance", "BTC-USDT", 5).unwrap();
        assert_eq!(shallow.bids.count(), 5);
        assert_eq!(shallow.asks.count(), 5);
        // Truncation keeps the best levels.
        assert_eq!(shallow.bids.best().unwrap().price, f64_to_scale9(50000.0));

        let full = store.snapshot("binance", "BTC-USDT", 0).unwrap();
        assert_eq!(full.bids.count(), 20);
    }

    #[test]
    fn bbo_returns_top_of_book() {
        let store = BookStore::new();
        let bids = vec![
            Level::new(f64_to_scale9(50000.0), f64_to_scale9(1.0)),
            Level::new(f64_to_scale9(49999.0), f64_to_scale9(2.0)),
        ];
        let asks = vec![
            Level::new(f64_to_scale9(50001.0), f64_to_scale9(1.5)),
            Level::new(f64_to_scale9(50002.0), f64_to_scale9(2.5)),
        ];

        store
            .apply_snapshot("binance", "BTC-USDT", &bids, &asks, 1, TS)
            .unwrap();

        let (bid, ask) = store.bbo("binance", "BTC-USDT").unwrap();
        assert_eq!(bid.price, f64_to_scale9(50000.0));
        assert_eq!(ask.price, f64_to_scale9(50001.0));
    }

    #[test]
    fn missing_book_is_stale_and_not_found() {
        let store = BookStore::new();

        assert!(store.is_stale("binance", "BTC-USDT", 1000));
        assert!(matches!(
            store.apply_delta("binance", "BTC-USDT", &[], &[], 1, TS),
            Err(Error::NotFound { .. })
        ));
    }

    #[test]
    fn fresh_book_is_not_stale() {
        let store = BookStore::new();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system clock is before the Unix epoch")
            .as_nanos() as u64;

        store
            .apply_snapshot("binance", "BTC-USDT", &[], &[], 1, now)
            .unwrap();

        assert!(!store.is_stale("binance", "BTC-USDT", 1000));
    }

    #[test]
    fn old_delta_is_ignored() {
        let store = BookStore::new();
        store
            .apply_snapshot("binance", "BTC-USDT", &[], &[], 5, TS)
            .unwrap();

        assert!(store
            .apply_delta("binance", "BTC-USDT", &[], &[], 3, TS + 1)
            .is_ok());
        assert_eq!(store.stats().deltas_applied, 0);
    }

    fn one(price: f64) -> Vec<Level> {
        vec![Level::new(f64_to_scale9(price), f64_to_scale9(1.0))]
    }

    #[test]
    fn gap_flags_the_book_until_a_snapshot_clears_it() {
        let store = BookStore::new();
        assert!(store.is_gapped("binance", "BTC-USDT"));

        store
            .apply_snapshot("binance", "BTC-USDT", &one(1.0), &one(2.0), 1, TS)
            .unwrap();
        assert!(!store.is_gapped("binance", "BTC-USDT"));

        assert!(store
            .apply_delta("binance", "BTC-USDT", &[], &[], 5, TS)
            .is_err());
        assert!(store.is_gapped("binance", "BTC-USDT"));
        assert!(store.snapshot("binance", "BTC-USDT", 0).unwrap().gapped);
        // Still serving the last good state.
        assert!(store.bbo("binance", "BTC-USDT").is_some());

        store
            .apply_snapshot("binance", "BTC-USDT", &one(1.0), &one(2.0), 5, TS)
            .unwrap();
        assert!(!store.is_gapped("binance", "BTC-USDT"));
        assert!(!store.snapshot("binance", "BTC-USDT", 0).unwrap().gapped);
    }

    #[test]
    fn older_snapshot_is_rejected_until_the_book_is_removed() {
        let store = BookStore::new();
        store
            .apply_snapshot("binance", "BTC-USDT", &one(1.0), &one(2.0), 10, TS)
            .unwrap();

        let err = store
            .apply_snapshot("binance", "BTC-USDT", &one(3.0), &one(4.0), 9, TS)
            .unwrap_err();
        assert_eq!(
            err,
            Error::OutOfOrder {
                current: 10,
                received: 9
            }
        );
        assert!(err.to_string().contains("call remove first"));
        assert_eq!(store.stats().snapshots_rejected, 1);
        assert_eq!(store.snapshot("binance", "BTC-USDT", 0).unwrap().seq, 10);

        // Same sequence is a harmless re-snapshot.
        store
            .apply_snapshot("binance", "BTC-USDT", &one(1.0), &one(2.0), 10, TS)
            .unwrap();

        assert!(store.remove("binance", "BTC-USDT"));
        assert!(!store.remove("binance", "BTC-USDT"));
        store
            .apply_snapshot("binance", "BTC-USDT", &one(3.0), &one(4.0), 1, TS)
            .unwrap();
        assert_eq!(store.snapshot("binance", "BTC-USDT", 0).unwrap().seq, 1);
    }

    #[test]
    fn dropped_levels_are_counted() {
        let store = BookStore::new();
        let bids: Vec<Level> = (0..crate::MAX_LEVELS + 5)
            .map(|i| Level::new(f64_to_scale9(50_000.0 - i as f64), f64_to_scale9(1.0)))
            .collect();

        // A snapshot deeper than the side can hold loses its worst levels.
        store
            .apply_snapshot("binance", "BTC-USDT", &bids, &one(60_000.0), 1, TS)
            .unwrap();
        assert_eq!(store.stats().levels_dropped, 5);
        let book = store.snapshot("binance", "BTC-USDT", 0).unwrap();
        assert_eq!(book.bids.count(), crate::MAX_LEVELS);
        assert_eq!(book.bids.best().unwrap().price, f64_to_scale9(50_000.0));

        // A delta adding a new level to a full side drops another one.
        store
            .apply_delta(
                "binance",
                "BTC-USDT",
                &[Level::new(f64_to_scale9(50_001.0), f64_to_scale9(1.0))],
                &[],
                2,
                TS,
            )
            .unwrap();
        assert_eq!(store.stats().levels_dropped, 6);

        // Updating a level that is already there is not a drop.
        store
            .apply_delta(
                "binance",
                "BTC-USDT",
                &[Level::new(f64_to_scale9(50_001.0), f64_to_scale9(3.0))],
                &[],
                3,
                TS,
            )
            .unwrap();
        assert_eq!(store.stats().levels_dropped, 6);
    }

    #[test]
    fn negative_prices_and_quantities_are_rejected() {
        let store = BookStore::new();
        let bad_qty = vec![Level::new(f64_to_scale9(1.0), f64_to_scale9(-1.0))];
        let bad_price = vec![Level::new(f64_to_scale9(-1.0), f64_to_scale9(1.0))];

        assert!(matches!(
            store.apply_snapshot("binance", "BTC-USDT", &bad_qty, &[], 1, TS),
            Err(Error::InvalidData(_))
        ));
        assert!(store.snapshot("binance", "BTC-USDT", 0).is_none());

        store
            .apply_snapshot("binance", "BTC-USDT", &one(1.0), &one(2.0), 1, TS)
            .unwrap();
        assert!(matches!(
            store.apply_delta("binance", "BTC-USDT", &[], &bad_price, 2, TS),
            Err(Error::InvalidData(_))
        ));
        assert_eq!(store.snapshot("binance", "BTC-USDT", 0).unwrap().seq, 1);
    }
}