p2p/channels/transaction/
p2p_channels_transaction_state.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone)]
4pub enum P2pChannelsTransactionState {
5    Disabled,
6    Enabled,
7    Init {
8        time: redux::Timestamp,
9    },
10    Pending {
11        time: redux::Timestamp,
12    },
13    Ready {
14        time: redux::Timestamp,
15        /// We are the requestors here.
16        local: TransactionPropagationState,
17        /// We are the responders here.
18        remote: TransactionPropagationState,
19        /// Last sent snark index.
20        next_send_index: u64,
21    },
22}
23
24#[derive(Serialize, Deserialize, Debug, Clone)]
25pub enum TransactionPropagationState {
26    WaitingForRequest {
27        time: redux::Timestamp,
28    },
29    Requested {
30        time: redux::Timestamp,
31        requested_limit: u8,
32    },
33    Responding {
34        time: redux::Timestamp,
35        requested_limit: u8,
36        promised_count: u8,
37        current_count: u8,
38    },
39    Responded {
40        time: redux::Timestamp,
41        count: u8,
42    },
43}
44
45impl P2pChannelsTransactionState {
46    pub fn is_ready(&self) -> bool {
47        matches!(self, Self::Ready { .. })
48    }
49
50    pub fn can_send_request(&self) -> bool {
51        matches!(
52            self,
53            Self::Ready {
54                local: TransactionPropagationState::WaitingForRequest { .. }
55                    | TransactionPropagationState::Responded { .. },
56                ..
57            }
58        )
59    }
60
61    pub fn next_send_index_and_limit(&self) -> (u64, u8) {
62        match self {
63            Self::Ready {
64                remote,
65                next_send_index,
66                ..
67            } => match remote {
68                TransactionPropagationState::Requested {
69                    requested_limit, ..
70                } => (*next_send_index, *requested_limit),
71                _ => (*next_send_index, 0),
72            },
73            _ => (0, 0),
74        }
75    }
76}