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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! #230: admitting a strandable maker takes the exclusive submit gate, in
//! every `STPMode`.
//!
//! A sweep decides **once**, before walking any level, whether to run its
//! per-level strandable-maker capture, by reading
//! `OrderBook::strandable_makers_resting`. That read is only coherent with
//! what the sweep later encounters if no strandable maker can be admitted
//! while the sweep is in flight. Without the exclusive gate, this
//! interleaving loses a discard entirely:
//!
//! ```text
//! asks 1@100, 1@101; a buy of 3@101 starts and reads the count as 0
//! ...the 100 level fills...
//! another thread admits a reserve {visible 1, hidden 20, auto off} @101
//! ...the sweep reaches 101, consumes and removes it, reports nothing
//! ```
//!
//! These tests park a plain (`STPMode::None`) sweep between the two levels
//! with the test-only `level_interleave_hook` and drive a competing
//! admission against it. The interleaving is channel rendezvous only: no
//! sleeps, and every timeout is a hung-test detector that panics rather than
//! a branch selector.
//!
//! The competitor branches on `submit_needs_exclusive_gate`, the same pure
//! predicate the production path consults, so the test is deterministic
//! under **both** policies: with the exclusive gate it must release the
//! parked sweep before its admission can proceed, and with the shared gate
//! it lands inside the window, which is the defect the final assertions
//! catch.
#[cfg(test)]
mod tests {
use crate::orderbook::book::OrderBook;
use pricelevel::{Hash32, Id, OrderType, Price, Quantity, Side, TimeInForce, TimestampMs};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
/// Hung-test detector only: every rendezvous is expected to complete
/// immediately, so a timeout means a deadlock, never a slow machine
/// taking a different branch.
const RENDEZVOUS_TIMEOUT: Duration = Duration::from_secs(10);
const NEAR_PRICE: u128 = 100;
const FAR_PRICE: u128 = 101;
/// What the competitor observed about the gate while the sweep was
/// parked.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum GateObservation {
/// `try_write` failed: the parked sweep still holds the shared side,
/// so an exclusive acquire would block behind it.
SweepHoldsShared,
/// `try_read` failed: the parked sweep holds the **exclusive** side,
/// so nothing at all can interleave with it.
SweepHoldsExclusive,
/// Nothing held the gate, so the sweep was not actually in flight
/// and the test proves nothing.
Free,
}
/// Channels joining the parked sweep and the competing thread.
struct Rendezvous {
parked_rx: Receiver<u128>,
resume_tx: Sender<()>,
}
/// The book's strandable-maker count.
fn count_of(book: &OrderBook<()>) -> usize {
book.strandable_makers_resting.load(Ordering::Relaxed)
}
/// A reserve SELL at `FAR_PRICE`: 1 visible / 20 hidden, no automatic
/// replenishment. The strandable shape, resting on the side the buy
/// sweep consumes.
fn strandable_reserve(id: Id) -> OrderType<()> {
OrderType::ReserveOrder {
id,
price: Price::new(FAR_PRICE),
visible_quantity: Quantity::new(1),
hidden_quantity: Quantity::new(20),
side: Side::Sell,
user_id: Hash32::zero(),
timestamp: TimestampMs::new(0),
time_in_force: TimeInForce::Gtc,
replenish_threshold: Quantity::new(0),
replenish_amount: None,
auto_replenish: false,
extra_fields: (),
}
}
/// Install the test-only level hook so the sweep parks once, just before
/// it matches `price`.
fn install_level_hook(book: &mut OrderBook<()>, price: u128) -> Rendezvous {
let (parked_tx, parked_rx) = channel::<u128>();
let (resume_tx, resume_rx) = channel::<()>();
let armed = AtomicBool::new(true);
let parked_tx = Mutex::new(parked_tx);
let resume_rx = Mutex::new(resume_rx);
book.level_interleave_hook = Some(Arc::new(move |level_price: u128| {
if level_price != price {
return;
}
if !armed.swap(false, Ordering::SeqCst) {
return;
}
parked_tx
.lock()
.expect("park channel mutex")
.send(level_price)
.expect("competing thread dropped the park channel");
let released = resume_rx
.lock()
.expect("resume channel mutex")
.recv_timeout(RENDEZVOUS_TIMEOUT);
assert!(
released.is_ok(),
"hung test: the competing thread never released the parked sweep \
within {RENDEZVOUS_TIMEOUT:?}"
);
}));
Rendezvous {
parked_rx,
resume_tx,
}
}
/// The book with the two ask levels the sweep will walk.
fn book_with_two_asks() -> OrderBook<()> {
let book: OrderBook<()> = OrderBook::new("STRANDABLE-GATE");
book.add_limit_order(
Id::from_u64(1),
NEAR_PRICE,
1,
Side::Sell,
TimeInForce::Gtc,
None,
)
.expect("near ask");
book.add_limit_order(
Id::from_u64(2),
FAR_PRICE,
1,
Side::Sell,
TimeInForce::Gtc,
None,
)
.expect("far ask");
book
}
/// The reviewer's interleaving: a strandable maker admitted while a
/// sweep is in flight must not be swallowed by that sweep.
///
/// With the exclusive gate the admission blocks until the sweep ends, so
/// the sweep executes 2 (1@100 + 1@101) against the two plain asks and
/// the reserve then rests 1 / 20 intact, counted by
/// `strandable_makers_resting`. A second sweep consumes it and reports
/// the discard.
#[test]
fn test_strandable_admission_blocks_until_an_in_flight_sweep_ends() {
let executed_log: Arc<Mutex<u64>> = Arc::new(Mutex::new(0));
let mut book = book_with_two_asks();
let listener_log = Arc::clone(&executed_log);
book.trade_listener = Some(Arc::new(move |result| {
let filled: u64 = result
.match_result
.trades()
.as_vec()
.iter()
.map(|print| print.quantity().as_u64())
.sum();
*listener_log.lock().expect("executed log mutex") += filled;
}));
let rendezvous = install_level_hook(&mut book, FAR_PRICE);
let book = Arc::new(book);
assert_eq!(
book.strandable_makers_resting.load(Ordering::Relaxed),
0,
"no strandable maker rests before the interleaving"
);
let reserve_id = Id::from_u64(3);
let competitor_book = Arc::clone(&book);
let competitor = thread::spawn(move || {
let parked = rendezvous.parked_rx.recv_timeout(RENDEZVOUS_TIMEOUT);
assert!(
parked.is_ok(),
"hung test: the sweep never reached the level hook within \
{RENDEZVOUS_TIMEOUT:?}"
);
// `try_write` fails while the sweep holds the shared side, which
// is what an exclusive admission would have to wait for.
let observation = match competitor_book.submit_gate.try_write() {
Err(std::sync::TryLockError::WouldBlock) => GateObservation::SweepHoldsShared,
Ok(guard) => {
drop(guard);
GateObservation::Free
}
Err(std::sync::TryLockError::Poisoned(_)) => {
panic!("submit gate poisoned: a prior panic unwound while it was held")
}
};
let reserve = strandable_reserve(reserve_id);
// Branch on the production predicate so the rendezvous is
// deadlock-free under either policy. Exclusive: the admission
// cannot proceed until the parked sweep releases, so release
// first. Shared (the defect): run the admission inside the
// window, where it lands in front of the sweep.
let exclusive = competitor_book.submit_needs_exclusive_gate(
false,
Hash32::zero(),
false,
OrderBook::<()>::is_strandable_maker(&reserve),
);
if exclusive {
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
let admitted = competitor_book.add_order(reserve);
assert!(
admitted.is_ok(),
"the reserve must be admitted: {admitted:?}"
);
} else {
let admitted = competitor_book.add_order(reserve);
assert!(
admitted.is_ok(),
"the reserve must be admitted: {admitted:?}"
);
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
}
(observation, exclusive)
});
let sweep_book = Arc::clone(&book);
let sweeper = thread::spawn(move || {
// IOC: the reviewer's buy of 3 walks both levels and its
// remainder is dropped rather than rested, so no residual bid is
// left at FAR_PRICE for the reserve's own admission to cross.
sweep_book.add_limit_order(
Id::from_u64(4),
FAR_PRICE,
3,
Side::Buy,
TimeInForce::Ioc,
None,
)
});
let (observation, exclusive) = competitor.join().expect("competitor thread panicked");
// An IOC with an unfilled remainder reports `InsufficientLiquidity`;
// the executed quantity is read off the trade listener instead.
let _ = sweeper.join().expect("sweeping thread panicked");
assert_eq!(
observation,
GateObservation::SweepHoldsShared,
"the sweep must still hold the shared side while parked, otherwise \
this test proves nothing about the interleaving"
);
assert!(
exclusive,
"admitting a strandable maker must take the exclusive gate (#230)"
);
let executed = *executed_log.lock().expect("executed log mutex");
assert_eq!(
executed, 2,
"the sweep must execute only the two plain asks it started against; \
executing 3 means it swallowed the reserve admitted mid-flight"
);
// The decisive assertion: the reserve was NOT swallowed by the sweep.
match book.get_order(reserve_id) {
Some(order) => assert_eq!(
(
order.visible_quantity().as_u64(),
order.hidden_quantity().as_u64()
),
(1, 20),
"the reserve must rest intact, admitted after the sweep ended"
),
None => panic!(
"the reserve was consumed by a sweep that never captured it: \
its hidden tranche was discarded with no report (#230)"
),
}
assert_eq!(
book.strandable_makers_resting.load(Ordering::Relaxed),
1,
"the rested reserve is counted, so the next sweep will capture it"
);
// A second sweep now sees the maker, captures it, and removes it with
// its hidden tranche discarded.
book.add_limit_order(
Id::from_u64(5),
FAR_PRICE,
1,
Side::Buy,
TimeInForce::Gtc,
None,
)
.expect("the second sweep is accepted");
assert!(
book.get_order(reserve_id).is_none(),
"the depleted non-replenishing reserve leaves the book"
);
assert_eq!(
book.strandable_makers_resting.load(Ordering::Relaxed),
0,
"the count returns to zero, closing the gate again"
);
}
/// Capture attribution survives a concurrent cancel and id reuse
/// (#230). This is the window exclusive *admission* alone does not
/// close, and the reason every sweep in a book holding strandable
/// makers runs exclusively.
///
/// Level 101 holds a plain ask A (1) **and** the strandable reserve X
/// (1 visible / 20 hidden), so removing X alone never empties the
/// level. A buy of 2@101 parks after capturing that level and before
/// matching it; the competitor tries to cancel X and re-admit a plain
/// `Standard` 1@101 reusing X's id.
///
/// With the fix the competitor's cancel blocks on the gate the sweep
/// holds exclusively, so the sweep consumes A and X, strands X's 20
/// hidden and reports them; the cancel then fails with `OrderNotFound`
/// and the re-admission rests as a fresh order. Without it the cancel
/// and the re-admission land inside the window, the sweep fills the
/// impostor, and the stale capture reports 20 units discarded that
/// nothing ever stranded.
#[test]
fn test_capture_attribution_survives_id_reuse() {
let discarded: Arc<Mutex<Vec<u64>>> = Arc::new(Mutex::new(Vec::new()));
let mut book: OrderBook<()> = OrderBook::new("STRANDABLE-ID-REUSE");
// Level 101: a plain ask first, then the strandable reserve behind
// it, so the level survives X's removal either way.
let plain_ask = Id::from_u64(31);
book.add_limit_order(plain_ask, FAR_PRICE, 1, Side::Sell, TimeInForce::Gtc, None)
.expect("plain ask rests");
let reused_id = Id::from_u64(32);
book.add_order(strandable_reserve(reused_id))
.expect("the strandable reserve rests behind it");
assert_eq!(count_of(&book), 1, "the reserve is counted");
let rendezvous = install_level_hook(&mut book, FAR_PRICE);
let book = Arc::new(book);
let competitor_book = Arc::clone(&book);
let competitor = thread::spawn(move || {
let parked = rendezvous.parked_rx.recv_timeout(RENDEZVOUS_TIMEOUT);
assert!(
parked.is_ok(),
"hung test: the sweep never reached the level hook within \
{RENDEZVOUS_TIMEOUT:?}"
);
// A sweep in this book runs exclusively, so even the shared side
// is unavailable while it is parked.
let observation = match competitor_book.submit_gate.try_read() {
Err(std::sync::TryLockError::WouldBlock) => GateObservation::SweepHoldsExclusive,
Ok(guard) => {
drop(guard);
GateObservation::SweepHoldsShared
}
Err(std::sync::TryLockError::Poisoned(_)) => {
panic!("submit gate poisoned: a prior panic unwound while it was held")
}
};
// Branch on what the gate actually allows, so the rendezvous is
// deadlock-free under either policy and the defect is reachable
// under the shared one. Exclusive: these operations cannot run
// until the sweep releases, so release first. Shared (the
// defect): run them *inside* the capture window, where the
// cancel and the id reuse invalidate the sweep's capture.
let inside_window = observation == GateObservation::SweepHoldsShared;
let run_competing_ops = |book: &OrderBook<()>| {
let cancelled = book.cancel_order(reused_id);
let readmitted = book.add_limit_order(
reused_id,
FAR_PRICE,
1,
Side::Sell,
TimeInForce::Gtc,
None,
);
(cancelled, readmitted)
};
let (cancelled, readmitted) = if inside_window {
let outcome = run_competing_ops(&competitor_book);
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
outcome
} else {
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
run_competing_ops(&competitor_book)
};
// `cancel_order` reports "nothing to cancel" as `Ok(None)`, not
// an error, so the meaningful question is whether it actually
// removed an order.
(
observation,
matches!(cancelled, Ok(Some(_))),
readmitted.is_ok(),
)
});
let sweep_book = Arc::clone(&book);
let sweep_log = Arc::clone(&discarded);
let sweeper = thread::spawn(move || {
// Capture what the sweep reports as stranded, by observing the
// reserve's terminal state rather than the metrics recorder,
// which this test binary does not install.
let result = sweep_book.add_limit_order(
Id::from_u64(33),
FAR_PRICE,
2,
Side::Buy,
TimeInForce::Gtc,
None,
);
sweep_log
.lock()
.expect("discard log mutex")
.push(u64::from(result.is_ok()));
result.is_ok()
});
let (observation, cancel_removed, readmit_ok) =
competitor.join().expect("competitor thread panicked");
let swept_ok = sweeper.join().expect("sweeping thread panicked");
assert!(swept_ok, "the sweep itself must succeed");
assert!(
!cancel_removed,
"the cancel must run only after the sweep consumed the reserve, so \
it finds nothing to remove (`Ok(None)`); removing it means the \
cancel landed inside the sweep's capture window and the sweep \
went on to fill an impostor under the captured id"
);
assert!(
readmit_ok,
"the re-admission under the freed id must succeed as a fresh order"
);
match book.get_order(reused_id) {
Some(order) => assert_eq!(
(
order.visible_quantity().as_u64(),
order.hidden_quantity().as_u64()
),
(1, 0),
"the id now belongs to the plain re-admitted order, not the reserve"
),
None => panic!("the re-admitted order must rest"),
}
assert_eq!(
count_of(&book),
0,
"the reserve was consumed by the sweep and the plain order that \
reused its id is not strandable"
);
assert_eq!(
observation,
GateObservation::SweepHoldsExclusive,
"a sweep in a book holding a strandable maker must hold the \
exclusive side, so a concurrent cancel cannot land inside its \
capture window"
);
assert_eq!(
observation,
GateObservation::SweepHoldsExclusive,
"the mechanism behind the assertions above: a sweep in a book \
holding a strandable maker holds the exclusive side, so nothing \
can land inside its capture window"
);
}
/// The same race through the **anonymous match-only** entry point
/// (#230). `match_order` is a sweep like any other, so it must take the
/// exclusive side in a book holding strandable makers; before the fix it
/// took `submit_gate_read()` directly and a concurrent cancel could land
/// inside its capture window, leaving the drain to report a discard that
/// never happened and to decrement the count a second time.
#[test]
fn test_capture_attribution_survives_id_reuse_through_match_order() {
let mut book: OrderBook<()> = OrderBook::new("STRANDABLE-MATCH-ORDER");
let plain_ask = Id::from_u64(51);
book.add_limit_order(plain_ask, FAR_PRICE, 1, Side::Sell, TimeInForce::Gtc, None)
.expect("plain ask rests");
let reused_id = Id::from_u64(52);
book.add_order(strandable_reserve(reused_id))
.expect("the strandable reserve rests behind it");
assert_eq!(count_of(&book), 1, "the reserve is counted");
let rendezvous = install_level_hook(&mut book, FAR_PRICE);
let book = Arc::new(book);
let competitor_book = Arc::clone(&book);
let competitor = thread::spawn(move || {
let parked = rendezvous.parked_rx.recv_timeout(RENDEZVOUS_TIMEOUT);
assert!(
parked.is_ok(),
"hung test: the sweep never reached the level hook within \
{RENDEZVOUS_TIMEOUT:?}"
);
let observation = match competitor_book.submit_gate.try_read() {
Err(std::sync::TryLockError::WouldBlock) => GateObservation::SweepHoldsExclusive,
Ok(guard) => {
drop(guard);
GateObservation::SweepHoldsShared
}
Err(std::sync::TryLockError::Poisoned(_)) => {
panic!("submit gate poisoned: a prior panic unwound while it was held")
}
};
let inside_window = observation == GateObservation::SweepHoldsShared;
let run_competing_ops = |book: &OrderBook<()>| {
let cancelled = book.cancel_order(reused_id);
let readmitted = book.add_limit_order(
reused_id,
FAR_PRICE,
1,
Side::Sell,
TimeInForce::Gtc,
None,
);
(cancelled, readmitted)
};
let (cancelled, _readmitted) = if inside_window {
let outcome = run_competing_ops(&competitor_book);
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
outcome
} else {
rendezvous
.resume_tx
.send(())
.expect("parked sweep dropped the resume channel");
run_competing_ops(&competitor_book)
};
(observation, matches!(cancelled, Ok(Some(_))))
});
let sweep_book = Arc::clone(&book);
let sweeper = thread::spawn(move || {
// The anonymous match-only entry point: no order is submitted,
// so only the strandable rule can make this exclusive.
sweep_book.match_order(Id::from_u64(53), Side::Buy, 2, Some(FAR_PRICE))
});
let (observation, cancel_removed) = competitor.join().expect("competitor thread panicked");
let matched = sweeper.join().expect("sweeping thread panicked");
assert!(
matched.is_ok(),
"the sweep itself must succeed: {matched:?}"
);
assert!(
!cancel_removed,
"the cancel must run only after the sweep consumed the reserve; \
removing it means the cancel landed inside `match_order`'s \
capture window and the drain reported a discard that never \
happened"
);
assert_eq!(
count_of(&book),
0,
"the reserve was consumed once; the plain order that reused its \
id is not strandable and must not decrement again"
);
assert_eq!(
observation,
GateObservation::SweepHoldsExclusive,
"`match_order` is a sweep and must take the exclusive side in a \
book holding a strandable maker"
);
}
/// The ordinary case for contrast: a strandable maker admitted *before*
/// the sweep starts is captured and reported, and the count tracks it.
#[test]
fn test_strandable_maker_admitted_before_the_sweep_is_captured() {
let book: OrderBook<()> = OrderBook::new("STRANDABLE-BEFORE");
let reserve_id = Id::from_u64(11);
book.add_order(strandable_reserve(reserve_id))
.expect("the reserve rests");
assert_eq!(
book.strandable_makers_resting.load(Ordering::Relaxed),
1,
"the resting maker is counted"
);
book.add_limit_order(
Id::from_u64(12),
FAR_PRICE,
1,
Side::Buy,
TimeInForce::Gtc,
None,
)
.expect("the sweep is accepted");
assert!(
book.get_order(reserve_id).is_none(),
"the depleted maker leaves the book"
);
assert_eq!(
book.strandable_makers_resting.load(Ordering::Relaxed),
0,
"the fill drain decrements the count"
);
}
/// A re-price of a strandable reserve holds the **exclusive** side for
/// the whole cancel-then-add, so its residual dry run cannot race a
/// concurrent mutation of the opposite side.
#[test]
fn test_reprice_of_strandable_reserve_takes_the_exclusive_gate() {
let book: OrderBook<()> = OrderBook::new("STRANDABLE-REPRICE");
let reserve_id = Id::from_u64(21);
book.add_order(strandable_reserve(reserve_id))
.expect("the reserve rests");
let plain_id = Id::from_u64(22);
book.add_limit_order(plain_id, NEAR_PRICE, 5, Side::Sell, TimeInForce::Gtc, None)
.expect("a plain maker rests too");
let reprice = pricelevel::OrderUpdate::UpdatePrice {
order_id: reserve_id,
new_price: Price::new(NEAR_PRICE),
};
assert!(
book.modify_needs_exclusive_gate(&reprice),
"re-pricing a strandable reserve must take the exclusive gate (#230)"
);
// The rule is the book's count, not the order being modified: while
// a strandable maker rests, re-pricing *any* order is exclusive.
// Deciding from a lookup of the order would read state outside the
// gate, where a concurrent cancel and id reuse can invalidate it —
// which is the case `test_capture_attribution_survives_id_reuse`
// pins.
let plain_reprice = pricelevel::OrderUpdate::UpdatePrice {
order_id: plain_id,
new_price: Price::new(NEAR_PRICE - 1),
};
assert!(
book.modify_needs_exclusive_gate(&plain_reprice),
"every re-price is exclusive while the book holds a strandable maker"
);
// Once the strandable maker is gone the count is zero and re-prices
// return to the shared side.
book.cancel_order(reserve_id).expect("cancel the reserve");
assert_eq!(
count_of(&book),
0,
"the cancel decrements the strandable-maker count"
);
assert!(
!book.modify_needs_exclusive_gate(&plain_reprice),
"a book holding none is unaffected: re-prices stay shared"
);
// And the non-matching variants stay shared throughout: they cannot
// match, so they cannot invalidate a capture. They simply never
// overlap an exclusive sweep.
let cancel = pricelevel::OrderUpdate::Cancel {
order_id: reserve_id,
};
assert!(
!book.modify_needs_exclusive_gate(&cancel),
"Cancel cannot match, so it keeps the shared side"
);
let resize = pricelevel::OrderUpdate::UpdateQuantity {
order_id: plain_id,
new_quantity: Quantity::new(3),
};
assert!(
!book.modify_needs_exclusive_gate(&resize),
"UpdateQuantity adjusts in place and keeps the shared side"
);
}
}