p2p/channels/snark_job_commitment/
p2p_channels_snark_job_commitment_state.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone)]
4pub enum P2pChannelsSnarkJobCommitmentState {
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: SnarkJobCommitmentPropagationState,
17        /// We are the responders here.
18        remote: SnarkJobCommitmentPropagationState,
19        /// Last sent commitment index.
20        next_send_index: u64,
21    },
22}
23
24#[derive(Serialize, Deserialize, Debug, Clone)]
25pub enum SnarkJobCommitmentPropagationState {
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 P2pChannelsSnarkJobCommitmentState {
46    pub fn is_ready(&self) -> bool {
47        matches!(self, Self::Ready { .. })
48    }
49
50    pub fn next_send_index_and_limit(&self) -> (u64, u8) {
51        match self {
52            Self::Ready {
53                remote,
54                next_send_index,
55                ..
56            } => match remote {
57                SnarkJobCommitmentPropagationState::Requested {
58                    requested_limit, ..
59                } => (*next_send_index, *requested_limit),
60                _ => (*next_send_index, 0),
61            },
62            _ => (0, 0),
63        }
64    }
65}