openpit 0.5.0

Embeddable pre-trade risk SDK
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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://github.com/openpitkit and the OWNERS file for details.

//! Account-group membership registry for [`Engine`](crate::Engine).
//!
//! Maps each [`AccountId`] to at most one [`AccountGroupId`]. The registry is
//! populated by the application through the [`Accounts`](crate::Accounts)
//! handle returned by [`Engine::accounts`](crate::Engine::accounts) and read by
//! policies and contexts to route per-account behavior by group.

use std::cell::OnceCell;
use std::fmt::{Display, Formatter};

use crate::param::{AccountGroupId, AccountId, DEFAULT_ACCOUNT_GROUP};
use crate::storage::{self, LockingPolicy, Storage, StorageBuilder};

// ─── AccountGroupError ───────────────────────────────────────────────────────

/// Error returned by [`Accounts::register_group`](crate::Accounts::register_group)
/// and [`Accounts::unregister_group`](crate::Accounts::unregister_group).
///
/// Both operations are atomic: when they fail, the registry is left unchanged
/// and the error names the offending account.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AccountGroupError {
    /// The target group passed to
    /// [`Accounts::register_group`](crate::Accounts::register_group) or
    /// [`Accounts::unregister_group`](crate::Accounts::unregister_group) is
    /// the reserved [`DEFAULT_ACCOUNT_GROUP`](crate::param::DEFAULT_ACCOUNT_GROUP).
    ///
    /// Accounts belong to the default group implicitly, so it cannot be a
    /// target of an explicit registration or unregistration.
    ReservedGroup,
    /// An account passed to
    /// [`Accounts::register_group`](crate::Accounts::register_group) is already
    /// a member of a group.
    ///
    /// The conflict applies whether the existing group equals the requested
    /// group or differs from it: an account may belong to at most one group,
    /// so it must be unregistered before it can be registered again.
    AlreadyRegistered {
        /// Account that is already a member of a group.
        account: AccountId,
        /// Group the account currently belongs to.
        current_group: AccountGroupId,
    },
    /// An account passed to
    /// [`Accounts::unregister_group`](crate::Accounts::unregister_group) is not
    /// currently a member of the requested group.
    ///
    /// `current_group` is `Some` when the account belongs to a different group
    /// and `None` when the account belongs to no group at all.
    NotInGroup {
        /// Account that is not a member of the requested group.
        account: AccountId,
        /// Group requested by the caller.
        requested_group: AccountGroupId,
        /// Group the account currently belongs to, or `None` when ungrouped.
        current_group: Option<AccountGroupId>,
    },
}

impl Display for AccountGroupError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ReservedGroup => {
                formatter.write_str("the reserved default account group is not a valid target")
            }
            Self::AlreadyRegistered {
                account,
                current_group,
            } => write!(
                formatter,
                "account {account} is already registered in group {current_group}"
            ),
            Self::NotInGroup {
                account,
                requested_group,
                current_group: Some(current),
            } => write!(
                formatter,
                "account {account} is not in group {requested_group}; \
                 it belongs to group {current}"
            ),
            Self::NotInGroup {
                account,
                requested_group,
                current_group: None,
            } => write!(
                formatter,
                "account {account} is not in group {requested_group}; \
                 it belongs to no group"
            ),
        }
    }
}

impl std::error::Error for AccountGroupError {}

// ─── AccountGroups ───────────────────────────────────────────────────────────

/// Per-engine storage for account-group membership.
///
/// Membership is stored in `memberships`, mapping each registered
/// [`AccountId`] to its single [`AccountGroupId`]. Per-key synchronization is
/// delegated to the `Storage` infrastructure matching the engine's
/// synchronization mode.
///
/// # Multi-account atomicity
///
/// [`register_group`](Self::register_group) and
/// [`unregister_group`](Self::unregister_group) mutate several keys and must be
/// all-or-nothing even under [`FullSync`](crate::core::FullSync), where real
/// threads can interleave. A naive check-all-then-mutate-all has a TOCTOU race.
/// To close it, the registry owns a dedicated standalone locking policy
/// (`guard`) and brackets every multi-account section in that policy's index
/// domain: an exclusive guard for mutations, a shared guard for reads. The
/// guard is a zero-cost no-op under [`LocalSync`](crate::core::LocalSync)
/// (single-observer, no real threads) and a real reader-writer lock under
/// both [`AccountSync`](crate::core::AccountSync) and
/// [`FullSync`](crate::core::FullSync). The mechanism reuses the same
/// `LockingPolicyFactory`/`SyncMode` machinery the rest of the crate relies on,
/// so no raw `std::sync::Mutex` is introduced and `LocalSync`'s `!Send`
/// guarantee is preserved.
pub(crate) struct AccountGroups<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    guard: <StorageFactory as storage::LockingPolicyFactory>::Policy,
    memberships: Storage<AccountId, AccountGroupId, StorageFactory::Policy>,
}

impl<StorageFactory> AccountGroups<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    /// Creates a new, empty account-group registry using `builder`'s locking
    /// policy.
    pub(crate) fn new(builder: &StorageBuilder<StorageFactory>) -> Self {
        Self {
            guard: builder.create_policy(),
            memberships: builder.create_for_bound_key(),
        }
    }

    /// Atomically registers every account in `accounts` into `group`.
    ///
    /// Fails with [`AccountGroupError::ReservedGroup`] when `group` is the
    /// reserved [`DEFAULT_ACCOUNT_GROUP`], or with
    /// [`AccountGroupError::AlreadyRegistered`] when any listed account already
    /// belongs to a group (including `group` itself); in either case no account
    /// is registered.
    pub(crate) fn register_group(
        &self,
        accounts: &[AccountId],
        group: AccountGroupId,
    ) -> Result<(), AccountGroupError> {
        if group == DEFAULT_ACCOUNT_GROUP {
            return Err(AccountGroupError::ReservedGroup);
        }

        // Exclusive whole-map section: the check and the inserts are serialized
        // against any other registry mutation, so the multi-account update is
        // all-or-nothing under every sync mode.
        let _guard = self.guard.write_index();

        for account in accounts {
            if let Some(current_group) = self.memberships.with(account, |group| *group) {
                return Err(AccountGroupError::AlreadyRegistered {
                    account: *account,
                    current_group,
                });
            }
        }

        for account in accounts {
            self.memberships.with_mut(
                *account,
                || group,
                |slot, _| {
                    *slot = group;
                },
            );
        }

        Ok(())
    }

    /// Atomically removes every account in `accounts` from `group`.
    ///
    /// Fails with [`AccountGroupError::ReservedGroup`] when `group` is the
    /// reserved [`DEFAULT_ACCOUNT_GROUP`], or with
    /// [`AccountGroupError::NotInGroup`] when any listed account is not
    /// currently a member of `group` (ungrouped or in another group); in either
    /// case no account is removed.
    pub(crate) fn unregister_group(
        &self,
        accounts: &[AccountId],
        group: AccountGroupId,
    ) -> Result<(), AccountGroupError> {
        if group == DEFAULT_ACCOUNT_GROUP {
            return Err(AccountGroupError::ReservedGroup);
        }

        // Exclusive whole-map section: the check and the removals are
        // serialized against any other registry mutation, so the
        // multi-account update is all-or-nothing under every sync mode.
        let _guard = self.guard.write_index();

        for account in accounts {
            let current_group = self.memberships.with(account, |group| *group);
            if current_group != Some(group) {
                return Err(AccountGroupError::NotInGroup {
                    account: *account,
                    requested_group: group,
                    current_group,
                });
            }
        }

        for account in accounts {
            self.memberships.remove(account);
        }

        Ok(())
    }

    /// Returns the group of `account`, or `None` when it is not registered.
    pub(crate) fn group_of(&self, account: AccountId) -> Option<AccountGroupId> {
        // Shared whole-map section: reads observe a consistent snapshot with
        // respect to multi-account mutations.
        let _guard = self.guard.read_index();
        self.memberships.with(&account, |group| *group)
    }
}

// ─── AccountGroupsHandle ─────────────────────────────────────────────────────

/// Shared handle to the engine's [`AccountGroups`] facility.
///
/// Cloneable; every clone refers to the same membership map. The handle's
/// auto-traits derive from `StorageFactory::Shared<...>`, the sync-mode-aware
/// wrapper chosen by [`LockingPolicyFactory::Shared`](crate::storage::LockingPolicyFactory::Shared):
///
/// - Under [`FullSync`](crate::core::FullSync) this is `Arc<...>`:
///   `Send + Sync`.
/// - Under [`LocalSync`](crate::core::LocalSync) this is `Rc<...>`:
///   `!Send + !Sync`.
/// - Under [`AccountSync`](crate::core::AccountSync) this is `IndexShared<...>`:
///   `Send + !Sync`.
pub(crate) struct AccountGroupsHandle<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    inner: StorageFactory::Shared<AccountGroups<StorageFactory>>,
}

impl<StorageFactory> Clone for AccountGroupsHandle<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<StorageFactory> AccountGroupsHandle<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    /// Wraps a shared [`AccountGroups`] in a handle.
    ///
    /// Used by the engine builder so that the engine and every context share
    /// one [`AccountGroups`] instance.
    pub(crate) fn from_inner(inner: StorageFactory::Shared<AccountGroups<StorageFactory>>) -> Self {
        Self { inner }
    }

    /// Atomically registers every account in `accounts` into `group`.
    pub(crate) fn register_group(
        &self,
        accounts: &[AccountId],
        group: AccountGroupId,
    ) -> Result<(), AccountGroupError> {
        self.inner.register_group(accounts, group)
    }

    /// Atomically removes every account in `accounts` from `group`.
    pub(crate) fn unregister_group(
        &self,
        accounts: &[AccountId],
        group: AccountGroupId,
    ) -> Result<(), AccountGroupError> {
        self.inner.unregister_group(accounts, group)
    }

    /// Returns the group of `account`, or `None` when it is not registered.
    pub(crate) fn group_of(&self, account: AccountId) -> Option<AccountGroupId> {
        self.inner.group_of(account)
    }
}

// ─── GroupLookup ─────────────────────────────────────────────────────────────

/// Lazy, per-context account-group lookup shared by the engine contexts.
///
/// Holds a cloned [`AccountGroupsHandle`], the bound account (or `None` when the
/// request carried no recognizable account identifier), and a single-thread
/// cache for the bound account's group. A context is created and consumed
/// within one engine call and is never shared across threads, so a non-`Sync`
/// [`OnceCell`] cache is sound; it keeps the embedding context `Send` (the cell
/// is `Send` when its value is) while making it `!Sync`.
pub(crate) struct GroupLookup<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    handle: AccountGroupsHandle<StorageFactory>,
    account: Option<AccountId>,
    cached_group: OnceCell<Option<AccountGroupId>>,
}

impl<StorageFactory> GroupLookup<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    pub(crate) fn new(
        handle: AccountGroupsHandle<StorageFactory>,
        account: Option<AccountId>,
    ) -> Self {
        Self {
            handle,
            account,
            cached_group: OnceCell::new(),
        }
    }

    /// Lazily looks up the bound account's group and caches the result.
    ///
    /// Computed once on first call; subsequent calls return the cached value
    /// even if the registry changes afterwards. Returns `None` when no account
    /// is bound.
    pub(crate) fn group(&self) -> Option<AccountGroupId> {
        *self.cached_group.get_or_init(|| {
            self.account
                .and_then(|account| self.handle.group_of(account))
        })
    }
}

impl<StorageFactory> crate::marketdata::AccountInfo for GroupLookup<StorageFactory>
where
    StorageFactory: storage::LockingPolicyFactory + storage::CreateStorageFor<AccountId> + 'static,
{
    /// Delegates to [`GroupLookup::group`], preserving its OnceCell laziness so
    /// the membership lookup runs at most once per context.
    fn group(&self) -> Option<AccountGroupId> {
        self.group()
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    use crate::param::{AccountGroupId, AccountId};
    use crate::storage::{LockingPolicyFactory, NoLocking, StorageBuilder};

    fn new_registry() -> AccountGroups<NoLocking> {
        AccountGroups::new(&StorageBuilder::new(NoLocking))
    }

    fn account(id: u64) -> AccountId {
        AccountId::from_u64(id)
    }

    fn group(id: u32) -> AccountGroupId {
        AccountGroupId::from_u32(id).expect("account group id must be valid")
    }

    #[test]
    fn register_group_happy_path() {
        let registry = new_registry();
        registry
            .register_group(&[account(1), account(2)], group(7))
            .expect("registration must succeed");

        assert_eq!(registry.group_of(account(1)), Some(group(7)));
        assert_eq!(registry.group_of(account(2)), Some(group(7)));
    }

    #[test]
    fn register_group_rejects_account_in_other_group_and_changes_nothing() {
        let registry = new_registry();
        registry
            .register_group(&[account(1)], group(1))
            .expect("first registration must succeed");

        let error = registry
            .register_group(&[account(2), account(1)], group(2))
            .expect_err("registration must fail on conflict");

        assert_eq!(
            error,
            AccountGroupError::AlreadyRegistered {
                account: account(1),
                current_group: group(1),
            }
        );
        // Atomic rollback: account 2 must NOT have been registered.
        assert_eq!(registry.group_of(account(2)), None);
        assert_eq!(registry.group_of(account(1)), Some(group(1)));
    }

    #[test]
    fn register_group_rejects_account_already_in_same_group_and_changes_nothing() {
        let registry = new_registry();
        registry
            .register_group(&[account(1)], group(5))
            .expect("first registration must succeed");

        let error = registry
            .register_group(&[account(2), account(1)], group(5))
            .expect_err("re-registering into the same group must fail");

        assert_eq!(
            error,
            AccountGroupError::AlreadyRegistered {
                account: account(1),
                current_group: group(5),
            }
        );
        assert_eq!(registry.group_of(account(2)), None);
    }

    #[test]
    fn unregister_group_happy_path() {
        let registry = new_registry();
        registry
            .register_group(&[account(1), account(2)], group(3))
            .expect("registration must succeed");

        registry
            .unregister_group(&[account(1), account(2)], group(3))
            .expect("unregistration must succeed");

        assert_eq!(registry.group_of(account(1)), None);
        assert_eq!(registry.group_of(account(2)), None);
    }

    #[test]
    fn unregister_group_rejects_ungrouped_account_and_removes_nothing() {
        let registry = new_registry();
        registry
            .register_group(&[account(1)], group(3))
            .expect("registration must succeed");

        let error = registry
            .unregister_group(&[account(1), account(2)], group(3))
            .expect_err("unregistration must fail when an account is ungrouped");

        assert_eq!(
            error,
            AccountGroupError::NotInGroup {
                account: account(2),
                requested_group: group(3),
                current_group: None,
            }
        );
        // Atomic rollback: account 1 must still be registered.
        assert_eq!(registry.group_of(account(1)), Some(group(3)));
    }

    #[test]
    fn unregister_group_rejects_account_in_other_group_and_removes_nothing() {
        let registry = new_registry();
        registry
            .register_group(&[account(1)], group(3))
            .expect("registration must succeed");
        registry
            .register_group(&[account(2)], group(4))
            .expect("registration must succeed");

        let error = registry
            .unregister_group(&[account(1), account(2)], group(3))
            .expect_err("unregistration must fail on group mismatch");

        assert_eq!(
            error,
            AccountGroupError::NotInGroup {
                account: account(2),
                requested_group: group(3),
                current_group: Some(group(4)),
            }
        );
        assert_eq!(registry.group_of(account(1)), Some(group(3)));
        assert_eq!(registry.group_of(account(2)), Some(group(4)));
    }

    #[test]
    fn group_of_present_and_absent() {
        let registry = new_registry();
        registry
            .register_group(&[account(1)], group(9))
            .expect("registration must succeed");

        assert_eq!(registry.group_of(account(1)), Some(group(9)));
        assert_eq!(registry.group_of(account(2)), None);
    }

    #[test]
    fn register_group_empty_slice_is_noop() {
        let registry = new_registry();
        registry
            .register_group(&[], group(1))
            .expect("empty registration must succeed");
        assert_eq!(registry.group_of(account(1)), None);
    }

    #[test]
    fn register_group_rejects_reserved_default_group() {
        let registry = new_registry();
        let error = registry
            .register_group(&[account(1)], DEFAULT_ACCOUNT_GROUP)
            .expect_err("registering into the default group must fail");

        assert_eq!(error, AccountGroupError::ReservedGroup);
        assert_eq!(registry.group_of(account(1)), None);
    }

    #[test]
    fn unregister_group_rejects_reserved_default_group() {
        let registry = new_registry();
        let error = registry
            .unregister_group(&[account(1)], DEFAULT_ACCOUNT_GROUP)
            .expect_err("unregistering from the default group must fail");

        assert_eq!(error, AccountGroupError::ReservedGroup);
    }

    #[test]
    fn account_group_error_display_is_stable() {
        assert_eq!(
            AccountGroupError::ReservedGroup.to_string(),
            "the reserved default account group is not a valid target"
        );

        let already = AccountGroupError::AlreadyRegistered {
            account: account(1),
            current_group: group(2),
        };
        assert_eq!(
            already.to_string(),
            "account 1 is already registered in group 2"
        );

        let mismatch = AccountGroupError::NotInGroup {
            account: account(1),
            requested_group: group(2),
            current_group: Some(group(3)),
        };
        assert_eq!(
            mismatch.to_string(),
            "account 1 is not in group 2; it belongs to group 3"
        );

        let ungrouped = AccountGroupError::NotInGroup {
            account: account(1),
            requested_group: group(2),
            current_group: None,
        };
        assert_eq!(
            ungrouped.to_string(),
            "account 1 is not in group 2; it belongs to no group"
        );
    }

    fn new_handle() -> AccountGroupsHandle<NoLocking> {
        AccountGroupsHandle::from_inner(NoLocking::new_shared(new_registry()))
    }

    #[test]
    fn pre_trade_context_group_returns_bound_account_group() {
        use crate::pretrade::PreTradeContext;

        let handle = new_handle();
        handle
            .inner
            .register_group(&[account(1)], group(7))
            .expect("registration must succeed");

        let ctx = PreTradeContext::with_groups(None, handle, Some(account(1)));
        assert_eq!(ctx.account_group(), Some(group(7)));
    }

    #[test]
    fn pre_trade_context_group_is_none_when_account_absent() {
        use crate::pretrade::PreTradeContext;

        let handle = new_handle();
        handle
            .inner
            .register_group(&[account(1)], group(7))
            .expect("registration must succeed");

        let ctx = PreTradeContext::with_groups(None, handle, None);
        assert_eq!(ctx.account_group(), None);
    }

    #[test]
    fn pre_trade_context_group_is_cached_after_first_call() {
        use crate::pretrade::PreTradeContext;

        let handle = new_handle();
        handle
            .inner
            .register_group(&[account(1)], group(7))
            .expect("registration must succeed");

        let ctx = PreTradeContext::with_groups(None, handle.clone(), Some(account(1)));
        // First call populates the cache.
        assert_eq!(ctx.account_group(), Some(group(7)));

        // Mutate the registry after the first lookup; the cached value must win.
        handle
            .inner
            .unregister_group(&[account(1)], group(7))
            .expect("unregistration must succeed");
        handle
            .inner
            .register_group(&[account(1)], group(9))
            .expect("re-registration must succeed");

        assert_eq!(ctx.account_group(), Some(group(7)));
    }

    #[test]
    fn post_trade_context_group_returns_report_account_group() {
        use crate::pretrade::PostTradeContext;

        let handle = new_handle();
        handle
            .inner
            .register_group(&[account(5)], group(3))
            .expect("registration must succeed");

        let ctx = PostTradeContext::with_groups(handle, Some(account(5)));
        assert_eq!(ctx.account_group(), Some(group(3)));
    }

    #[test]
    fn account_adjustment_context_group_returns_adjusted_account_group() {
        use crate::core::account_control::BlockedAccounts;
        use crate::core::{AccountBlockHandle, AccountControl};
        use crate::AccountAdjustmentContext;

        let handle = new_handle();
        handle
            .inner
            .register_group(&[account(8)], group(4))
            .expect("registration must succeed");

        let block_handle = AccountBlockHandle::from_inner(NoLocking::new_shared(
            BlockedAccounts::new(&StorageBuilder::new(NoLocking)),
        ));
        let control = AccountControl::new(block_handle, account(8));
        let ctx = AccountAdjustmentContext::with_groups(control, handle, account(8));
        assert_eq!(ctx.account_group(), Some(group(4)));
    }

    // Concurrency: under FullLocking (FullSync engines) the whole-map guard must
    // serialize register/unregister so a failed multi-account call leaves the
    // membership map completely unchanged even with real thread contention.
    #[test]
    fn full_locking_failed_register_leaves_membership_unchanged_under_contention() {
        use crate::storage::FullLocking;
        use std::sync::Arc;
        use std::thread;

        let registry: Arc<AccountGroups<FullLocking>> =
            Arc::new(AccountGroups::new(&StorageBuilder::new(FullLocking)));
        // Pre-register a conflict account that makes every batch including it
        // fail atomically.
        registry
            .register_group(&[account(0)], group(100))
            .expect("seed registration must succeed");

        thread::scope(|scope| {
            for tid in 1..=8u64 {
                let registry = Arc::clone(&registry);
                scope.spawn(move || {
                    for _ in 0..200 {
                        // Each batch ends with the conflicting account(0), so the
                        // whole batch must roll back: account(tid) must never be
                        // registered.
                        let result =
                            registry.register_group(&[account(tid), account(0)], group(tid as u32));
                        assert!(result.is_err(), "batch with conflict must fail");
                    }
                });
            }
        });

        // No worker account leaked into the map despite heavy contention.
        for tid in 1..=8u64 {
            assert_eq!(registry.group_of(account(tid)), None);
        }
        assert_eq!(registry.group_of(account(0)), Some(group(100)));
    }
}