Skip to main content

hypercall/rsm/unified_engine/
traits.rs

1//! Readability-focused capability traits for `UnifiedEngine`.
2//!
3//! These traits intentionally mirror the existing engine methods and do not
4//! change runtime behavior. Their purpose is to make the major responsibilities
5//! explicit when reading call sites and module boundaries.
6
7use super::*;
8
9/// Order admission checks before an order can enter matching.
10pub(crate) trait OrderAdmission {
11    fn validate_order(&self, order_info: &OrderInfo, wallet: &WalletAddress) -> Result<(), String>;
12
13    fn check_order_restrictions(
14        &self,
15        order_info: &OrderInfo,
16        wallet: &WalletAddress,
17    ) -> Result<(), String>;
18}
19
20impl OrderAdmission for UnifiedEngine {
21    fn validate_order(&self, order_info: &OrderInfo, wallet: &WalletAddress) -> Result<(), String> {
22        UnifiedEngine::validate_order(self, order_info, wallet)
23    }
24
25    fn check_order_restrictions(
26        &self,
27        order_info: &OrderInfo,
28        wallet: &WalletAddress,
29    ) -> Result<(), String> {
30        UnifiedEngine::check_order_restrictions(self, order_info, wallet)
31    }
32}
33
34/// Order execution pipeline after admission.
35pub(crate) trait OrderExecution {
36    async fn allocate_and_ack(
37        &mut self,
38        request: &UnifiedEngineRequest,
39        order_info: &OrderInfo,
40        wallet: &WalletAddress,
41        timestamp: u64,
42    ) -> AllocatedOrder;
43
44    async fn execute_matching(
45        &mut self,
46        order_id: u64,
47        order_info: &OrderInfo,
48        wallet: &WalletAddress,
49        timestamp: u64,
50    ) -> MatchingResult;
51
52    async fn finalize_order(
53        &mut self,
54        request: &UnifiedEngineRequest,
55        order_id: u64,
56        order_info: &OrderInfo,
57        wallet: &WalletAddress,
58        timestamp: u64,
59        result: &MatchingResult,
60    );
61}
62
63impl OrderExecution for UnifiedEngine {
64    async fn allocate_and_ack(
65        &mut self,
66        request: &UnifiedEngineRequest,
67        order_info: &OrderInfo,
68        wallet: &WalletAddress,
69        timestamp: u64,
70    ) -> AllocatedOrder {
71        UnifiedEngine::allocate_and_ack(self, request, order_info, wallet, timestamp).await
72    }
73
74    async fn execute_matching(
75        &mut self,
76        order_id: u64,
77        order_info: &OrderInfo,
78        wallet: &WalletAddress,
79        timestamp: u64,
80    ) -> MatchingResult {
81        UnifiedEngine::execute_matching(self, order_id, order_info, wallet, timestamp).await
82    }
83
84    async fn finalize_order(
85        &mut self,
86        request: &UnifiedEngineRequest,
87        order_id: u64,
88        order_info: &OrderInfo,
89        wallet: &WalletAddress,
90        timestamp: u64,
91        result: &MatchingResult,
92    ) {
93        UnifiedEngine::finalize_order(
94            self, request, order_id, order_info, wallet, timestamp, result,
95        )
96        .await
97    }
98}
99
100/// Durable journaling boundary for order processing.
101pub(crate) trait DurableJournaling {
102    async fn process_order_journaled(&mut self, request: UnifiedEngineRequest);
103}
104
105impl DurableJournaling for UnifiedEngine {
106    async fn process_order_journaled(&mut self, request: UnifiedEngineRequest) {
107        UnifiedEngine::process_order_journaled(self, request).await;
108    }
109}
110
111/// Replay and recovery lifecycle used at startup and reconciliation.
112pub(crate) trait ReplayRecovery {
113    fn load_markets_from_db(&mut self);
114    fn load_orderbook_snapshots(&mut self);
115    fn replay_commands_from_journal(&mut self);
116    fn check_restored_orderbooks_for_crosses(&self);
117    fn run_post_startup_reconciliation(&mut self);
118}
119
120impl ReplayRecovery for UnifiedEngine {
121    fn load_markets_from_db(&mut self) {
122        UnifiedEngine::load_markets_from_db(self);
123    }
124
125    fn load_orderbook_snapshots(&mut self) {
126        UnifiedEngine::load_orderbook_snapshots(self);
127    }
128
129    fn replay_commands_from_journal(&mut self) {
130        UnifiedEngine::replay_commands_from_journal(self);
131    }
132
133    fn check_restored_orderbooks_for_crosses(&self) {
134        UnifiedEngine::check_restored_orderbooks_for_crosses(self);
135    }
136
137    fn run_post_startup_reconciliation(&mut self) {
138        UnifiedEngine::run_post_startup_reconciliation(self);
139    }
140}