cuprate_dandelion_tower/pool/
incoming_tx.rs1use crate::{State, TxState};
3
4pub struct IncomingTx<Tx, TxId, PeerId> {
6 pub(crate) tx: Tx,
8 pub(crate) tx_id: TxId,
10 pub(crate) routing_state: TxState<PeerId>,
12}
13
14pub struct IncomingTxBuilder<const RS: bool, const DBS: bool, Tx, TxId, PeerId> {
21 tx: Tx,
23 tx_id: TxId,
25 routing_state: Option<TxState<PeerId>>,
27 state_in_db: Option<State>,
29}
30
31impl<Tx, TxId, PeerId> IncomingTxBuilder<false, false, Tx, TxId, PeerId> {
32 pub const fn new(tx: Tx, tx_id: TxId) -> Self {
34 Self {
35 tx,
36 tx_id,
37 routing_state: None,
38 state_in_db: None,
39 }
40 }
41}
42
43impl<const DBS: bool, Tx, TxId, PeerId> IncomingTxBuilder<false, DBS, Tx, TxId, PeerId> {
44 pub fn with_routing_state(
48 self,
49 state: TxState<PeerId>,
50 ) -> IncomingTxBuilder<true, DBS, Tx, TxId, PeerId> {
51 IncomingTxBuilder {
52 tx: self.tx,
53 tx_id: self.tx_id,
54 routing_state: Some(state),
55 state_in_db: self.state_in_db,
56 }
57 }
58}
59
60impl<const RS: bool, Tx, TxId, PeerId> IncomingTxBuilder<RS, false, Tx, TxId, PeerId> {
61 pub fn with_state_in_db(
65 self,
66 state: Option<State>,
67 ) -> IncomingTxBuilder<RS, true, Tx, TxId, PeerId> {
68 IncomingTxBuilder {
69 tx: self.tx,
70 tx_id: self.tx_id,
71 routing_state: self.routing_state,
72 state_in_db: state,
73 }
74 }
75}
76
77impl<Tx, TxId, PeerId> IncomingTxBuilder<true, true, Tx, TxId, PeerId> {
78 pub fn build(self) -> Option<IncomingTx<Tx, TxId, PeerId>> {
83 let routing_state = self.routing_state.unwrap();
84
85 if self.state_in_db == Some(State::Fluff) {
86 return None;
87 }
88
89 Some(IncomingTx {
90 tx: self.tx,
91 tx_id: self.tx_id,
92 routing_state,
93 })
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 #[test]
102 fn test_builder() {
103 IncomingTxBuilder::new(1, 2)
104 .with_routing_state(TxState::Stem { from: 3 })
105 .with_state_in_db(None)
106 .build();
107
108 IncomingTxBuilder::new(1, 2)
109 .with_state_in_db(None)
110 .with_routing_state(TxState::Stem { from: 3 })
111 .build();
112 }
113}