|
3 | 3 | use std::{any::Any, collections::BTreeMap}; |
4 | 4 |
|
5 | 5 | use strata_asm_common::{ |
6 | | - AsmError, InterprotoMsg, Log, MsgRelayer, SectionState, Subprotocol, SubprotocolId, TxInput, |
| 6 | + AsmError, InterprotoMsg, Log, MsgRelayer, SectionState, SubprotoHandler, Subprotocol, |
| 7 | + SubprotocolId, SubprotocolManager, TxInput, |
7 | 8 | }; |
8 | 9 |
|
9 | | -/// Subprotocol handler trait for a loaded subprotocol. |
10 | | -pub(crate) trait SubprotoHandler { |
11 | | - /// Processes transactions that were previously collected. |
12 | | - fn process_txs(&mut self, txs: &[TxInput<'_>], relayer: &mut dyn MsgRelayer); |
13 | | - |
14 | | - /// Accepts a message. This is called while processing other subprotocols. |
15 | | - /// These should not be processed until we do the finalization. |
16 | | - /// |
17 | | - /// This MUST NOT act on any messages that were accepted before this was |
18 | | - /// called. |
19 | | - /// |
20 | | - /// # Panics |
21 | | - /// |
22 | | - /// If an mismatched message type (behind the `dyn`) is provided. |
23 | | - fn accept_msg(&mut self, msg: &dyn InterprotoMsg); |
24 | | - |
25 | | - /// Processes the messages received. |
26 | | - fn process_msgs(&mut self); |
27 | | - |
28 | | - /// Repacks the state into a [`SectionState`] instance. |
29 | | - fn to_section(&self) -> SectionState; |
30 | | -} |
31 | | - |
32 | 10 | /// Wrapper around the common subprotocol interface that handles the common |
33 | 11 | /// buffering logic for interproto messages. |
34 | 12 | pub(crate) struct HandlerImpl<S: Subprotocol, R> { |
@@ -84,60 +62,48 @@ pub(crate) struct HandlerRelayer { |
84 | 62 | logs: Vec<Log>, |
85 | 63 | } |
86 | 64 |
|
87 | | -impl HandlerRelayer { |
88 | | - pub(crate) fn new() -> Self { |
89 | | - Self { |
90 | | - handlers: BTreeMap::new(), |
91 | | - logs: Vec::new(), |
92 | | - } |
93 | | - } |
94 | | - |
| 65 | +impl SubprotocolManager for HandlerRelayer { |
95 | 66 | /// Inserts a subproto by creating a handler for it. |
96 | | - pub(crate) fn insert_subproto<S: Subprotocol>(&mut self, state: S::State) { |
| 67 | + fn insert_subproto<S: Subprotocol>(&mut self, state: S::State) { |
97 | 68 | let handler = HandlerImpl::<S, Self>::from_state(state); |
98 | 69 | if self.handlers.insert(S::ID, Box::new(handler)).is_some() { |
99 | 70 | panic!("asm: loaded state twice"); |
100 | 71 | } |
101 | 72 | } |
102 | 73 |
|
103 | | - pub(crate) fn get_handler(&self, id: SubprotocolId) -> Result<&dyn SubprotoHandler, AsmError> { |
| 74 | + fn insert_handler<S: Subprotocol>(&mut self, handler: Box<dyn SubprotoHandler>) { |
| 75 | + self.handlers.insert(S::ID, handler); |
| 76 | + } |
| 77 | + |
| 78 | + fn remove_handler(&mut self, id: SubprotocolId) -> Result<Box<dyn SubprotoHandler>, AsmError> { |
| 79 | + self.handlers |
| 80 | + .remove(&id) |
| 81 | + .ok_or(AsmError::InvalidSubprotocol(id)) |
| 82 | + } |
| 83 | + |
| 84 | + fn get_handler(&self, id: SubprotocolId) -> Result<&dyn SubprotoHandler, AsmError> { |
104 | 85 | self.handlers |
105 | 86 | .get(&id) |
106 | 87 | .map(Box::as_ref) |
107 | 88 | .ok_or(AsmError::InvalidSubprotocol(id)) |
108 | 89 | } |
109 | 90 |
|
110 | | - pub(crate) fn get_handler_mut( |
| 91 | + fn get_handler_mut( |
111 | 92 | &mut self, |
112 | 93 | id: SubprotocolId, |
113 | 94 | ) -> Result<&mut Box<dyn SubprotoHandler>, AsmError> { |
114 | 95 | self.handlers |
115 | 96 | .get_mut(&id) |
116 | 97 | .ok_or(AsmError::InvalidSubprotocol(id)) |
117 | 98 | } |
| 99 | +} |
118 | 100 |
|
119 | | - pub(crate) fn invoke_process_txs<S: Subprotocol>(&mut self, txs: &[TxInput<'_>]) { |
120 | | - // We temporarily take the handler out of the map so we can call |
121 | | - // `process_txs` with `self` as the relayer without violating the |
122 | | - // borrow checker. |
123 | | - let mut h = self |
124 | | - .handlers |
125 | | - .remove(&S::ID) |
126 | | - .expect("asm: unloaded subprotocol"); |
127 | | - h.process_txs(txs, self); |
128 | | - self.handlers.insert(S::ID, h); |
129 | | - } |
130 | | - |
131 | | - pub(crate) fn invoke_process_msgs<S: Subprotocol>(&mut self) { |
132 | | - let h = self |
133 | | - .get_handler_mut(S::ID) |
134 | | - .expect("asm: unloaded subprotocol"); |
135 | | - h.process_msgs() |
136 | | - } |
137 | | - |
138 | | - pub(crate) fn to_section_state<S: Subprotocol>(&self) -> SectionState { |
139 | | - let h = self.get_handler(S::ID).expect("asm: unloaded subprotocol"); |
140 | | - h.to_section() |
| 101 | +impl HandlerRelayer { |
| 102 | + pub(crate) fn new() -> Self { |
| 103 | + Self { |
| 104 | + handlers: BTreeMap::new(), |
| 105 | + logs: Vec::new(), |
| 106 | + } |
141 | 107 | } |
142 | 108 | } |
143 | 109 |
|
|
0 commit comments