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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use chain_impl_mockchain::fragment::FragmentId;
use jormungandr_automation::jormungandr::{FragmentNode, FragmentNodeError, MemPoolCheck};
use jormungandr_lib::interfaces::{FragmentLog, FragmentStatus};
use jortestkit::prelude::Wait;
use std::{collections::HashMap, time::Duration};

#[derive(custom_debug::Debug, thiserror::Error)]
pub enum FragmentVerifierError {
    #[error("fragment sent to node: {alias} is not in block :({status:?})")]
    FragmentNotInBlock {
        alias: String,
        status: FragmentStatus,
        #[debug(skip)]
        logs: Vec<String>,
    },
    #[error("cannot match rejection reason '{message}' does not contains '{expected_part}'")]
    UnexpectedRejectionReason {
        message: String,
        expected_part: String,
    },
    #[error("fragment sent to node: {alias} is not rejected :({status:?})")]
    FragmentNotRejected {
        alias: String,
        status: FragmentStatus,
        #[debug(skip)]
        logs: Vec<String>,
    },
    #[error("transaction is pending for too long")]
    FragmentIsPendingForTooLong {
        fragment_id: FragmentId,
        timeout: Duration,
        alias: String,
        #[debug(skip)]
        logs: Vec<String>,
    },
    #[error("transactions are pending for too long")]
    FragmentsArePendingForTooLong {
        timeout: Duration,
        alias: String,
        #[debug(skip)]
        logs: Vec<String>,
    },

    #[error("fragment sent to node: {alias} is not in in fragment pool :({fragment_id})")]
    FragmentNotInMemPoolLogs {
        alias: String,
        fragment_id: FragmentId,
        #[debug(skip)]
        logs: Vec<String>,
    },
    #[error("fragment node error")]
    FragmentNode(#[from] FragmentNodeError),
    #[error("at least on rejected fragment error")]
    AtLeastOneRejectedFragment {
        fragment_id: FragmentId,
        #[debug(skip)]
        logs: Vec<String>,
    },
    #[error("timeout reached while waiting for all fragments in a block")]
    TimeoutReachedWhileWaitingForAllFragmentsInBlock {
        #[debug(skip)]
        logs: Vec<String>,
    },
}

impl FragmentVerifierError {
    pub fn logs(&self) -> impl Iterator<Item = &str> {
        use self::FragmentVerifierError::*;
        let maybe_logs = match self {
            FragmentNotInBlock { logs, .. }
            | FragmentIsPendingForTooLong { logs, .. }
            | FragmentsArePendingForTooLong { logs, .. }
            | FragmentNotInMemPoolLogs { logs, .. }
            | FragmentNotRejected { logs, .. }
            | FragmentNode(FragmentNodeError::CannotSendFragment { logs, .. }) => Some(logs),
            AtLeastOneRejectedFragment { logs, .. } => Some(logs),
            TimeoutReachedWhileWaitingForAllFragmentsInBlock { logs } => Some(logs),
            FragmentNode(_) => None,
            UnexpectedRejectionReason { .. } => None,
        };
        maybe_logs
            .into_iter()
            .flat_map(|logs| logs.iter())
            .map(String::as_str)
    }
}

pub struct FragmentVerifier;

impl FragmentVerifier {
    pub fn wait_until_all_processed<A: FragmentNode + ?Sized>(
        wait: Wait,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        for _ in 0..wait.attempts() {
            let fragment_logs = match node.fragment_logs() {
                Err(_) => {
                    std::thread::sleep(wait.sleep_duration());
                    continue;
                }
                Ok(fragment_logs) => fragment_logs,
            };

            if let Some((id, _)) = fragment_logs.iter().find(|(_, x)| x.is_rejected()) {
                return Err(FragmentVerifierError::AtLeastOneRejectedFragment {
                    fragment_id: *id,
                    logs: node.log_content(),
                });
            }

            if fragment_logs.iter().all(|(_, x)| x.is_in_a_block()) {
                return Ok(());
            }
            std::thread::sleep(wait.sleep_duration());
        }
        Err(
            FragmentVerifierError::TimeoutReachedWhileWaitingForAllFragmentsInBlock {
                logs: node.log_content(),
            },
        )
    }

    pub fn wait_and_verify_all_are_in_block<A: FragmentNode + ?Sized>(
        duration: Duration,
        checks: Vec<MemPoolCheck>,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        for check in checks {
            let status = Self::wait_fragment(duration, check, ExitStrategy::OnProcessed, node)?;
            Self::is_in_block(status, node)?;
        }
        Ok(())
    }

    pub fn wait_and_verify_is_in_block<A: FragmentNode + ?Sized>(
        duration: Duration,
        check: MemPoolCheck,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        let status = Self::wait_fragment(duration, check, ExitStrategy::OnProcessed, node)?;
        Self::is_in_block(status, node)
    }

    pub fn wait_and_verify_is_rejected<A: FragmentNode + ?Sized>(
        duration: Duration,
        check: MemPoolCheck,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        let status = Self::wait_fragment(duration, check, ExitStrategy::OnProcessed, node)?;
        Self::is_rejected(status, node)
    }

    pub fn wait_and_verify_is_rejected_with_message<A: FragmentNode + ?Sized, S: Into<String>>(
        duration: Duration,
        check: MemPoolCheck,
        message: S,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        let status = Self::wait_fragment(duration, check, ExitStrategy::OnProcessed, node)?;
        Self::is_rejected_with_message(status, message, node)
    }

    pub fn is_in_block<A: FragmentNode + ?Sized>(
        status: FragmentStatus,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        if !status.is_in_a_block() {
            return Err(FragmentVerifierError::FragmentNotInBlock {
                alias: node.alias(),
                status,
                logs: node.log_content(),
            });
        }
        Ok(())
    }

    pub fn is_rejected<A: FragmentNode + ?Sized>(
        status: FragmentStatus,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        if !status.is_rejected() {
            return Err(FragmentVerifierError::FragmentNotRejected {
                alias: node.alias(),
                status,
                logs: node.log_content(),
            });
        }
        Ok(())
    }

    pub fn is_rejected_with_message<A: FragmentNode + ?Sized, S: Into<String>>(
        status: FragmentStatus,
        expected_part: S,
        node: &A,
    ) -> Result<(), FragmentVerifierError> {
        if let FragmentStatus::Rejected { reason } = status {
            let expected_part = expected_part.into();
            reason.contains(&expected_part).then_some(()).ok_or(
                FragmentVerifierError::UnexpectedRejectionReason {
                    message: reason,
                    expected_part,
                },
            )
        } else {
            Err(FragmentVerifierError::FragmentNotRejected {
                alias: node.alias(),
                status,
                logs: node.log_content(),
            })
        }
    }

    pub fn fragment_status<A: FragmentNode + ?Sized>(
        check: MemPoolCheck,
        node: &A,
    ) -> Result<FragmentStatus, FragmentVerifierError> {
        let logs = node.fragment_logs()?;
        if let Some(log) = logs.get(check.fragment_id()) {
            let status = log.status().clone();
            match log.status() {
                FragmentStatus::Pending => {
                    node.log_pending_fragment(*check.fragment_id());
                }
                FragmentStatus::Rejected { reason } => {
                    node.log_rejected_fragment(*check.fragment_id(), reason.to_string());
                }
                FragmentStatus::InABlock { date, block } => {
                    node.log_in_block_fragment(*check.fragment_id(), *date, *block);
                }
            }
            return Ok(status);
        }

        Err(FragmentVerifierError::FragmentNotInMemPoolLogs {
            alias: node.alias(),
            fragment_id: *check.fragment_id(),
            logs: node.log_content(),
        })
    }

    pub fn wait_fragment<A: FragmentNode + ?Sized>(
        duration: Duration,
        check: MemPoolCheck,
        exit_strategy: ExitStrategy,
        node: &A,
    ) -> Result<FragmentStatus, FragmentVerifierError> {
        let max_try = 50;
        for _ in 0..max_try {
            let status_result = Self::fragment_status(check.clone(), node);

            if status_result.is_err() {
                std::thread::sleep(duration);
                continue;
            }

            let status = status_result.unwrap();

            match (&status, exit_strategy) {
                (FragmentStatus::Rejected { .. }, _) => return Ok(status),
                (FragmentStatus::InABlock { .. }, _) => return Ok(status),
                (FragmentStatus::Pending, ExitStrategy::OnPending) => return Ok(status),
                _ => (),
            }
            std::thread::sleep(duration);
        }

        Err(FragmentVerifierError::FragmentIsPendingForTooLong {
            fragment_id: *check.fragment_id(),
            timeout: Duration::from_secs(duration.as_secs() * max_try),
            alias: node.alias(),
            logs: node.log_content(),
        })
    }

    pub fn wait_for_all_fragments<A: FragmentNode + ?Sized>(
        duration: Duration,
        node: &A,
    ) -> Result<HashMap<FragmentId, FragmentLog>, FragmentVerifierError> {
        let max_try = 50;
        for _ in 0..max_try {
            let status_result = node.fragment_logs();

            if status_result.is_err() {
                std::thread::sleep(duration);
                continue;
            }

            let statuses = status_result.unwrap();

            let any_rejected = statuses.iter().any(|(_, log)| {
                !matches!(
                    log.status(),
                    FragmentStatus::Rejected { .. } | FragmentStatus::InABlock { .. }
                )
            });

            if !any_rejected {
                return Ok(statuses);
            }
            std::thread::sleep(duration);
        }

        Err(FragmentVerifierError::FragmentsArePendingForTooLong {
            timeout: Duration::from_secs(duration.as_secs() * max_try),
            alias: node.alias(),
            logs: node.log_content(),
        })
    }
}

#[derive(Clone, Copy)]
pub enum ExitStrategy {
    /// Exit as soon as the fragment enters the mempool
    OnPending,
    /// Exit when the fragment has been processed (i.e. either Rejected or InABlock)
    OnProcessed,
}