rustrade-execution 0.15.0

Stream private account data from financial venues, and execute (live or mock) orders.
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
//! Which of an instrument's orders were sent reduce-only, kept so a fill can be matched to the
//! flag of its order.
//!
//! Neither an [`Order`](crate::order::Order) nor a venue's report of one carries the flag set by
//! [`RequestOpen::reduce_only`](crate::order::request::RequestOpen::reduce_only). Whoever sends
//! the request is the one place that learns it, so whoever needs it for a fill later, such as a
//! tracker that cuts a reduce-only fill short of flipping a position (see
//! [`Trade::holding_move`](crate::trade::Trade::holding_move)), has to keep it. A
//! [`ReduceOnlyBook`] is that record, and finds the order of a fill by its client order id or, for
//! a fill that carries only the venue's id (a recovered fill), by the venue order id.

use crate::{
    order::id::{ClientOrderId, OrderId},
    trade::Trade,
};
use rustrade_integration::collection::FnvIndexMap;
use serde::{Deserialize, Serialize};
use tracing::debug;

/// An order the book was told of, still in force.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
struct LiveOrder {
    reduce_only: bool,
    venue_id: Option<OrderId>,
}

/// An order found in a [`ReduceOnlyBook`].
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct BookedOrder<'a> {
    /// The order's client order id, as the book holds it.
    pub cid: &'a ClientOrderId,
    /// Whether the order was sent reduce-only.
    pub reduce_only: bool,
}

/// One instrument's record of which orders were sent reduce-only. Plain data with no locking and
/// no I/O, so the owner keeps it behind whatever synchronisation it already has, one book for each
/// instrument.
///
/// The owner calls, for each order:
/// 1. [`register`](Self::register) when the open request is sent,
/// 2. [`acknowledge`](Self::acknowledge) when the venue reports the order's id, and
/// 3. [`retire`](Self::retire) when the order ends. **Every** order must be retired, or the book
///    keeps it for good.
///
/// Then [`lookup`](Self::lookup) finds the flag of a fill's order.
///
/// A retired order stays findable only if it was reduce-only, and only the last
/// [`RETIRED_CAPACITY`](Self::RETIRED_CAPACITY) of those, oldest dropped first, so that a late
/// fill of one is still recognised and ordinary orders do not push them out. A late fill of any
/// other retired order is not found: read that as not reduce-only.
///
/// # Serialised form
/// `{"live": {<cid>: {"reduce_only": bool, "venue_id": <id>|null}}, "retired": {<cid>: <id>|null}}`,
/// each map in order, either part optional. It is stored as part of its owner's state, so a
/// change to it is a breaking change. A book loaded with more retired orders than
/// [`RETIRED_CAPACITY`](Self::RETIRED_CAPACITY) keeps the latest.
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(from = "BookParts")]
pub struct ReduceOnlyBook {
    /// In the order registered.
    live: FnvIndexMap<ClientOrderId, LiveOrder>,
    /// The reduce-only orders that ended, each with its venue order id once known, in the order
    /// they ended.
    retired: FnvIndexMap<ClientOrderId, Option<OrderId>>,
    /// How many of `live` are reduce-only, so that [`has_reduce_only`](Self::has_reduce_only) is
    /// O(1). Derived, so it is not serialised.
    #[serde(skip)]
    live_reduce_only: usize,
}

/// What is serialised of a [`ReduceOnlyBook`]; loading one rebuilds the rest.
#[derive(Deserialize)]
struct BookParts {
    #[serde(default)]
    live: FnvIndexMap<ClientOrderId, LiveOrder>,
    #[serde(default)]
    retired: FnvIndexMap<ClientOrderId, Option<OrderId>>,
}

impl From<BookParts> for ReduceOnlyBook {
    fn from(parts: BookParts) -> Self {
        let BookParts { live, mut retired } = parts;
        // An order is live or retired, never both; the live one wins.
        retired.retain(|cid, _| !live.contains_key(cid));
        let excess = retired.len().saturating_sub(Self::RETIRED_CAPACITY);
        retired.drain(..excess);
        let live_reduce_only = live.values().filter(|order| order.reduce_only).count();
        Self {
            live,
            retired,
            live_reduce_only,
        }
    }
}

impl ReduceOnlyBook {
    /// How many retired reduce-only orders the book keeps.
    pub const RETIRED_CAPACITY: usize = 64;

    /// An empty book.
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether the book holds no order, live or retired.
    pub fn is_empty(&self) -> bool {
        self.live.is_empty() && self.retired.is_empty()
    }

    /// How many orders are live: registered and not yet retired. An owner that retires every
    /// order sees this fall back to the number it has open.
    pub fn live_len(&self) -> usize {
        self.live.len()
    }

    /// Whether any order the book holds is reduce-only, live or retired. When it is not, every
    /// [`lookup`](Self::lookup) finds no reduce-only order, so a caller can skip it. O(1).
    pub fn has_reduce_only(&self) -> bool {
        self.live_reduce_only > 0 || !self.retired.is_empty()
    }

    /// Record that the open request `cid` is being sent, reduce-only or not.
    ///
    /// A client order id reused for a new order replaces everything an earlier order under it
    /// left, live or retired, so its flag does not outlive the order. The new order counts as the
    /// latest registered. Registering a client order id that is still live replaces that order,
    /// dropping the venue order id it was acknowledged with, so register once for each order sent.
    pub fn register(&mut self, cid: &ClientOrderId, reduce_only: bool) {
        self.retired.shift_remove(cid);
        if let Some(earlier) = self.live.shift_remove(cid) {
            self.live_reduce_only -= usize::from(earlier.reduce_only);
        }
        self.live_reduce_only += usize::from(reduce_only);
        self.live.insert(
            cid.clone(),
            LiveOrder {
                reduce_only,
                venue_id: None,
            },
        );
    }

    /// Record the venue's order id for `cid`, live or retired, so a fill that names only that id
    /// finds the order. Replaces an id given before. Does nothing for an order the book does not
    /// hold.
    ///
    /// An order can end before its id is known, such as one filled on its acknowledgement, so a
    /// retired order takes it too.
    pub fn acknowledge(&mut self, cid: &ClientOrderId, venue_id: &OrderId) {
        if let Some(live) = self.live.get_mut(cid) {
            live.venue_id = Some(venue_id.clone());
        } else if let Some(retired) = self.retired.get_mut(cid) {
            *retired = Some(venue_id.clone());
        }
    }

    /// Record that the order `cid` ended. Does nothing for an order the book does not hold.
    ///
    /// A reduce-only order moves to the retired orders, dropping the oldest of them when there
    /// are [`RETIRED_CAPACITY`](Self::RETIRED_CAPACITY). Any other order is forgotten.
    pub fn retire(&mut self, cid: &ClientOrderId) {
        if let Some(live) = self.live.shift_remove(cid)
            && live.reduce_only
        {
            self.live_reduce_only -= 1;
            self.insert_retired(cid.clone(), live.venue_id);
        }
    }

    /// [`retire`](Self::retire) every live order for which `is_tracked` is `false`, in the order
    /// they were registered, so which retirement is dropped first never depends on hashing.
    ///
    /// One pass over the live orders, so O(live orders), and it allocates only when a reduce-only
    /// order ended.
    pub fn retire_untracked(&mut self, mut is_tracked: impl FnMut(&ClientOrderId) -> bool) {
        let mut ended: Vec<(ClientOrderId, Option<OrderId>)> = Vec::new();
        self.live.retain(|cid, order| {
            let tracked = is_tracked(cid);
            if !tracked && order.reduce_only {
                ended.push((cid.clone(), order.venue_id.take()));
            }
            tracked
        });
        self.live_reduce_only -= ended.len();
        for (cid, venue_id) in ended {
            self.insert_retired(cid, venue_id);
        }
    }

    /// Find the order a fill belongs to.
    ///
    /// By the fill's client order id if it has one, and else by its venue order id: a live order
    /// first, then a retired one. A client order id the book does not hold finds nothing, and does
    /// not fall back to the venue's id.
    pub fn lookup<AssetKey, InstrumentKey>(
        &self,
        trade: &Trade<AssetKey, InstrumentKey>,
    ) -> Option<BookedOrder<'_>> {
        self.lookup_ids(trade.client_order_id.as_ref(), &trade.order_id)
    }

    /// [`lookup`](Self::lookup) for the ids of a fill, for a caller that resolved the client order
    /// id itself. A `cid` the book does not hold finds nothing and does not fall back to
    /// `order_id`; pass `None` to look up by the venue's id alone.
    pub fn lookup_ids(
        &self,
        cid: Option<&ClientOrderId>,
        order_id: &OrderId,
    ) -> Option<BookedOrder<'_>> {
        if let Some(cid) = cid {
            return self
                .live
                .get_key_value(cid)
                .map(|(cid, live)| BookedOrder {
                    cid,
                    reduce_only: live.reduce_only,
                })
                .or_else(|| {
                    self.retired.get_key_value(cid).map(|(cid, _)| BookedOrder {
                        cid,
                        reduce_only: true,
                    })
                });
        }
        // A live order outranks a retired one under the same venue order id. The scan is of the
        // open orders, and at most `RETIRED_CAPACITY` retired ones.
        self.live
            .iter()
            .find(|(_, live)| live.venue_id.as_ref() == Some(order_id))
            .map(|(cid, live)| BookedOrder {
                cid,
                reduce_only: live.reduce_only,
            })
            .or_else(|| {
                self.retired
                    .iter()
                    .find(|(_, venue_id)| venue_id.as_ref() == Some(order_id))
                    .map(|(cid, _)| BookedOrder {
                        cid,
                        reduce_only: true,
                    })
            })
    }

    /// Whether `trade` is a fill of an order sent reduce-only: [`lookup`](Self::lookup) with an
    /// order the book does not hold counted as not.
    pub fn is_reduce_only<AssetKey, InstrumentKey>(
        &self,
        trade: &Trade<AssetKey, InstrumentKey>,
    ) -> bool {
        self.lookup(trade).is_some_and(|order| order.reduce_only)
    }

    /// Forget every order, for when the orders the book records are dropped wholesale.
    pub fn clear(&mut self) {
        self.live.clear();
        self.retired.clear();
        self.live_reduce_only = 0;
    }

    fn insert_retired(&mut self, cid: ClientOrderId, venue_id: Option<OrderId>) {
        if self.retired.len() >= Self::RETIRED_CAPACITY
            && !self.retired.contains_key(&cid)
            && let Some((evicted, _)) = self.retired.shift_remove_index(0)
        {
            debug!(
                evicted_cid = %evicted,
                capacity = Self::RETIRED_CAPACITY,
                "Retired reduce-only orders at capacity: evicting the oldest. A late fill of the \
                 evicted order is now not recognised as reduce-only."
            );
        }
        self.retired.insert(cid, venue_id);
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
mod tests {
    use super::*;
    use crate::{order::id::StrategyId, trade::AssetFees};
    use chrono::DateTime;
    use rust_decimal_macros::dec;
    use rustrade_instrument::Side;
    use rustrade_instrument::asset::QuoteAsset;

    fn cid(id: &str) -> ClientOrderId {
        ClientOrderId::new(id)
    }

    fn venue(id: &str) -> OrderId {
        OrderId::new(id)
    }

    fn fill(cid: Option<&str>, venue_id: &str) -> Trade<QuoteAsset, ()> {
        let trade = Trade::new(
            crate::trade::TradeId::new("t"),
            venue(venue_id),
            (),
            StrategyId::new("s"),
            DateTime::UNIX_EPOCH,
            Side::Sell,
            dec!(1),
            dec!(1),
            None,
            AssetFees::quote_fees(dec!(0)),
        );
        match cid {
            Some(id) => trade.with_client_order_id(self::cid(id)),
            None => trade,
        }
    }

    fn flag(book: &ReduceOnlyBook, trade: &Trade<QuoteAsset, ()>) -> Option<bool> {
        book.lookup(trade).map(|order| order.reduce_only)
    }

    #[test]
    fn a_fill_finds_its_order_by_client_order_id() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        book.register(&cid("plain"), false);
        assert_eq!(flag(&book, &fill(Some("ro"), "v")), Some(true));
        assert_eq!(flag(&book, &fill(Some("plain"), "v")), Some(false));
        assert_eq!(flag(&book, &fill(Some("other"), "v")), None);
        assert!(book.is_reduce_only(&fill(Some("ro"), "v")));
        assert!(!book.is_reduce_only(&fill(Some("plain"), "v")));
        assert!(!book.is_reduce_only(&fill(Some("other"), "v")));
    }

    #[test]
    fn a_fill_naming_only_the_venue_id_finds_its_order_once_acknowledged() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        assert_eq!(flag(&book, &fill(None, "v1")), None, "not yet acknowledged");
        book.acknowledge(&cid("ro"), &venue("v1"));
        let trade = fill(None, "v1");
        let found = book.lookup(&trade).unwrap();
        assert_eq!((found.cid, found.reduce_only), (&cid("ro"), true));
    }

    #[test]
    fn a_client_order_id_the_book_lacks_does_not_fall_back_to_the_venue_id() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        book.acknowledge(&cid("ro"), &venue("v1"));
        assert_eq!(flag(&book, &fill(Some("other"), "v1")), None);
    }

    #[test]
    fn a_retired_reduce_only_order_is_still_found_and_an_ordinary_one_is_forgotten() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        book.register(&cid("plain"), false);
        book.acknowledge(&cid("ro"), &venue("v1"));
        book.acknowledge(&cid("plain"), &venue("v2"));
        book.retire(&cid("ro"));
        book.retire(&cid("plain"));
        assert_eq!(flag(&book, &fill(Some("ro"), "x")), Some(true));
        assert_eq!(flag(&book, &fill(None, "v1")), Some(true));
        assert_eq!(flag(&book, &fill(Some("plain"), "x")), None);
        assert_eq!(flag(&book, &fill(None, "v2")), None);
    }

    #[test]
    fn an_order_retired_before_its_acknowledgement_takes_the_venue_id_after() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        book.retire(&cid("ro"));
        assert_eq!(flag(&book, &fill(None, "v1")), None);
        book.acknowledge(&cid("ro"), &venue("v1"));
        assert_eq!(flag(&book, &fill(None, "v1")), Some(true));
    }

    #[test]
    fn a_live_order_outranks_a_retired_one_under_the_same_venue_id() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("old"), true);
        book.acknowledge(&cid("old"), &venue("v1"));
        book.retire(&cid("old"));
        book.register(&cid("new"), false);
        book.acknowledge(&cid("new"), &venue("v1"));
        let trade = fill(None, "v1");
        let found = book.lookup(&trade).unwrap();
        assert_eq!((found.cid, found.reduce_only), (&cid("new"), false));
    }

    #[test]
    fn registering_a_reused_client_order_id_drops_what_the_earlier_order_left() {
        for retire_first in [false, true] {
            let mut book = ReduceOnlyBook::new();
            book.register(&cid("c"), true);
            book.acknowledge(&cid("c"), &venue("v1"));
            if retire_first {
                book.retire(&cid("c"));
            }
            book.register(&cid("c"), false);
            assert_eq!(flag(&book, &fill(Some("c"), "x")), Some(false));
            assert_eq!(flag(&book, &fill(None, "v1")), None, "{retire_first}");
        }
    }

    #[test]
    fn the_oldest_retirement_is_dropped_at_capacity() {
        let mut book = ReduceOnlyBook::new();
        let cids: Vec<_> = (0..=ReduceOnlyBook::RETIRED_CAPACITY)
            .map(|i| cid(&format!("c{i}")))
            .collect();
        for c in &cids {
            book.register(c, true);
            book.retire(c);
        }
        assert_eq!(book.retired.len(), ReduceOnlyBook::RETIRED_CAPACITY);
        assert_eq!(flag(&book, &fill(Some("c0"), "x")), None);
        assert_eq!(flag(&book, &fill(Some("c1"), "x")), Some(true));
        assert_eq!(flag(&book, &fill(Some("c64"), "x")), Some(true));
    }

    #[test]
    fn ordinary_orders_do_not_push_out_retired_reduce_only_ones() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro"), true);
        book.retire(&cid("ro"));
        for i in 0..ReduceOnlyBook::RETIRED_CAPACITY * 2 {
            let c = cid(&format!("p{i}"));
            book.register(&c, false);
            book.retire(&c);
        }
        assert_eq!(flag(&book, &fill(Some("ro"), "x")), Some(true));
    }

    #[test]
    fn retire_untracked_retires_in_the_order_registered() {
        let mut book = ReduceOnlyBook::new();
        let cids: Vec<_> = (0..5).map(|i| cid(&format!("c{i}"))).collect();
        for c in &cids {
            book.register(c, true);
        }
        let kept = cid("c2");
        book.retire_untracked(|c| *c == kept);
        assert!(
            book.retired
                .keys()
                .eq([&cids[0], &cids[1], &cids[3], &cids[4]])
        );
        assert!(book.live.keys().eq([&kept]));
    }

    #[test]
    fn retire_untracked_keeps_the_reduce_only_count_and_forgets_ordinary_orders() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("ro1"), true);
        book.register(&cid("gone"), false);
        book.register(&cid("ro2"), true);
        book.register(&cid("kept"), false);
        book.retire_untracked(|c| *c == cid("kept"));
        assert_eq!(book.live_reduce_only, 0);
        assert!(book.live.keys().eq([&cid("kept")]));
        assert!(book.retired.keys().eq([&cid("ro1"), &cid("ro2")]));
        assert!(book.has_reduce_only(), "retired ones are still held");
        book.retired.clear();
        assert!(!book.has_reduce_only());
    }

    #[test]
    fn retiring_or_acknowledging_an_unknown_order_does_nothing() {
        let mut book = ReduceOnlyBook::new();
        book.retire(&cid("none"));
        book.acknowledge(&cid("none"), &venue("v"));
        assert!(book.is_empty());
    }

    #[test]
    fn clear_forgets_everything() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("a"), true);
        book.register(&cid("b"), true);
        book.retire(&cid("b"));
        book.clear();
        assert!(book.is_empty());
    }

    #[test]
    fn is_empty_and_live_len_count_live_and_retired_orders() {
        let mut book = ReduceOnlyBook::new();
        assert_eq!((book.is_empty(), book.live_len()), (true, 0));
        book.register(&cid("plain"), false);
        book.register(&cid("ro"), true);
        assert_eq!((book.is_empty(), book.live_len()), (false, 2), "live only");
        book.retire(&cid("ro"));
        assert_eq!((book.is_empty(), book.live_len()), (false, 1));
        book.retire(&cid("plain"));
        assert_eq!(
            (book.is_empty(), book.live_len()),
            (false, 0),
            "retired only"
        );
    }

    #[test]
    fn has_reduce_only_follows_the_orders_held() {
        let mut book = ReduceOnlyBook::new();
        assert!(!book.has_reduce_only());
        book.register(&cid("plain"), false);
        assert!(!book.has_reduce_only());
        book.register(&cid("ro"), true);
        assert!(book.has_reduce_only());
        book.register(&cid("ro"), false);
        assert!(!book.has_reduce_only(), "replaced by an ordinary order");
        book.register(&cid("ro"), true);
        book.retire(&cid("ro"));
        assert!(book.has_reduce_only(), "retired but still held");
        book.clear();
        assert!(!book.has_reduce_only());
        book.register(&cid("a"), true);
        book.register(&cid("b"), true);
        book.retire_untracked(|c| *c == cid("a"));
        assert_eq!((book.live_len(), book.has_reduce_only()), (1, true));
    }

    #[test]
    fn the_serialised_form_is_pinned_and_keeps_the_retirement_order() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("b"), true);
        book.acknowledge(&cid("b"), &venue("v"));
        book.retire(&cid("b"));
        book.register(&cid("a"), true);
        book.retire(&cid("a"));
        book.register(&cid("c"), false);
        let json = serde_json::to_string(&book).unwrap();
        assert_eq!(
            json,
            r#"{"live":{"c":{"reduce_only":false,"venue_id":null}},"retired":{"b":"v","a":null}}"#
        );
        let loaded: ReduceOnlyBook = serde_json::from_str(&json).unwrap();
        assert!(loaded.retired.keys().eq(book.retired.keys()));
        assert_eq!(loaded.has_reduce_only(), book.has_reduce_only());
    }

    #[test]
    fn a_loaded_book_keeps_its_bound_and_counts_its_live_reduce_only_orders() {
        // Built as text: `serde_json::Value` would sort the keys.
        let retired: Vec<String> = (0..100).map(|i| format!(r#""c{i}":null"#)).collect();
        let json = format!(
            r#"{{"live":{{"l":{{"reduce_only":true,"venue_id":null}},"c99":{{"reduce_only":false,"venue_id":null}}}},"retired":{{{}}}}}"#,
            retired.join(",")
        );
        let mut book: ReduceOnlyBook = serde_json::from_str(&json).unwrap();
        assert_eq!(book.retired.len(), ReduceOnlyBook::RETIRED_CAPACITY);
        assert!(book.retired.keys().next().is_some_and(|c| *c == cid("c35")));
        assert!(!book.retired.contains_key(&cid("c99")), "live wins");
        book.retire(&cid("l"));
        assert_eq!(book.retired.len(), ReduceOnlyBook::RETIRED_CAPACITY);
        book.retire(&cid("c99"));
        assert!(book.live.is_empty() && book.live_reduce_only == 0);
    }

    #[test]
    fn a_book_round_trips_through_json_and_loads_from_nothing() {
        let mut book = ReduceOnlyBook::new();
        book.register(&cid("live"), true);
        book.acknowledge(&cid("live"), &venue("v1"));
        book.register(&cid("gone"), true);
        book.retire(&cid("gone"));
        let json = serde_json::to_string(&book).unwrap();
        assert_eq!(serde_json::from_str::<ReduceOnlyBook>(&json).unwrap(), book);
        assert_eq!(
            serde_json::from_str::<ReduceOnlyBook>("{}").unwrap(),
            ReduceOnlyBook::new()
        );
    }
}