fuel_core_txpool/
tx_status_stream.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use fuel_core_types::{
    fuel_tx::Bytes32,
    services::txpool::TransactionStatus,
};
use std::pin::Pin;
use tokio_stream::Stream;

#[derive(Debug, Clone)]
pub struct TxUpdate {
    pub(crate) tx_id: Bytes32,
    pub(crate) message: TxStatusMessage,
}

impl TxUpdate {
    pub fn new(tx_id: Bytes32, message: TxStatusMessage) -> Self {
        Self { tx_id, message }
    }

    pub fn tx_id(&self) -> &Bytes32 {
        &self.tx_id
    }

    pub fn into_msg(self) -> TxStatusMessage {
        self.message
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TxStatusMessage {
    Status(TransactionStatus),
    FailedStatus,
}

impl<E> From<Result<TransactionStatus, E>> for TxStatusMessage {
    fn from(result: Result<TransactionStatus, E>) -> Self {
        match result {
            Ok(status) => TxStatusMessage::Status(status),
            _ => TxStatusMessage::FailedStatus,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum State {
    Empty,
    Initial(TransactionStatus),
    EarlySuccess(TransactionStatus),
    Success(TransactionStatus, TransactionStatus),
    Failed,
    LateFailed(TransactionStatus),
    SenderClosed(TransactionStatus),
    Closed,
}

#[derive(Debug)]
pub struct TxUpdateStream {
    state: State,
}

impl TxUpdateStream {
    pub fn new() -> Self {
        Self {
            state: State::Empty,
        }
    }

    #[cfg(test)]
    pub(super) fn with_state(state: State) -> Self {
        Self { state }
    }

    #[cfg(test)]
    pub(super) fn state(&self) -> &State {
        &self.state
    }

    pub fn add_msg(&mut self, msg: TxStatusMessage) {
        let state = std::mem::replace(&mut self.state, State::Empty);
        self.state = match state {
            State::Empty => match msg {
                TxStatusMessage::Status(TransactionStatus::Submitted { time }) => {
                    State::Initial(TransactionStatus::Submitted { time })
                }
                TxStatusMessage::Status(s) => State::EarlySuccess(s),
                TxStatusMessage::FailedStatus => State::Failed,
            },
            State::Initial(s1) => {
                if let TxStatusMessage::Status(s2) = msg {
                    State::Success(s1, s2)
                } else {
                    State::LateFailed(s1)
                }
            }
            s => s,
        };
    }

    pub fn add_failure(&mut self) {
        let state = std::mem::replace(&mut self.state, State::Empty);
        self.state = match state {
            State::Initial(s) => State::LateFailed(s),
            State::Empty => State::Failed,
            s => s,
        };
    }

    pub fn close_recv(&mut self) {
        self.state = State::Closed;
    }

    pub fn try_next(&mut self) -> Option<TxStatusMessage> {
        let state = std::mem::replace(&mut self.state, State::Empty);
        match state {
            State::Initial(s) => Some(TxStatusMessage::Status(s)),
            State::Empty => None,
            State::EarlySuccess(s) | State::SenderClosed(s) => {
                self.state = State::Closed;
                Some(TxStatusMessage::Status(s))
            }
            State::Failed => {
                self.state = State::Closed;
                Some(TxStatusMessage::FailedStatus)
            }
            State::LateFailed(s) => {
                self.state = State::Failed;
                Some(TxStatusMessage::Status(s))
            }
            State::Success(s1, s2) => {
                self.state = State::SenderClosed(s2);
                Some(TxStatusMessage::Status(s1))
            }
            State::Closed => {
                self.state = State::Closed;
                None
            }
        }
    }

    pub fn is_closed(&self) -> bool {
        matches!(self.state, State::Closed)
    }
}

pub type TxStatusStream = Pin<Box<dyn Stream<Item = TxStatusMessage> + Send + Sync>>;