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
use crate::errors::{Error, Result};
use crate::{
    choices::{decidable::Decidable, status::Status},
    ids::Id,
};

#[derive(Clone, Debug)]
pub struct TestDecidable {
    pub id: Id,

    /// "Status" enum uses String
    /// so cannot implement/derive "Copy" to use "Cell"
    /// ref. <https://stackoverflow.com/questions/38215753/how-do-i-implement-copy-and-clone-for-a-type-that-contains-a-string-or-any-type>
    ///
    /// Use "Box" instead to overwrite.
    pub status: Box<Status>,

    pub accept_result: Result<()>,
    pub reject_result: Result<()>,
}

impl Default for TestDecidable {
    fn default() -> Self {
        Self::default()
    }
}

impl TestDecidable {
    pub fn default() -> Self {
        Self {
            id: Id::empty(),

            status: Box::new(Status::Processing),

            accept_result: Ok(()),
            reject_result: Ok(()),
        }
    }
}

impl TestDecidable {
    pub fn new(id: Id, status: Status) -> Self {
        Self {
            id,
            status: Box::new(status),
            accept_result: Ok(()),
            reject_result: Ok(()),
        }
    }

    pub fn set_accept_result(&mut self, rs: Result<()>) {
        self.accept_result = rs;
    }

    pub fn set_reject_result(&mut self, rs: Result<()>) {
        self.reject_result = rs;
    }

    pub fn create_decidable(
        id: Id,
        status: Status,
        accept_result: Result<()>,
        reject_result: Result<()>,
    ) -> impl Decidable {
        Self {
            id,
            status: Box::new(status),
            accept_result,
            reject_result,
        }
    }
}

impl Decidable for TestDecidable {
    fn id(&self) -> Id {
        self.id
    }

    fn status(&self) -> Status {
        Status::from(self.status.as_str())
    }

    fn accept(&mut self) -> Result<()> {
        let status = self.status.as_ref();
        if matches!(status, Status::Unknown(_) | Status::Rejected) {
            return Err(Error::Other {
                message: format!(
                    "invalid state transaction from {} to {}",
                    status,
                    Status::Accepted
                ),
                is_retryable: false,
            });
        }
        if self.accept_result.is_ok() {
            self.status = Box::new(Status::Accepted);
        }

        self.accept_result.clone()
    }

    fn reject(&mut self) -> Result<()> {
        let status = self.status.as_ref();
        if matches!(status, Status::Unknown(_) | Status::Accepted) {
            return Err(Error::Other {
                message: format!(
                    "invalid state transaction from {} to {}",
                    status,
                    Status::Rejected
                ),
                is_retryable: false,
            });
        }
        if self.reject_result.is_ok() {
            self.status = Box::new(Status::Rejected);
        }

        self.reject_result.clone()
    }
}

/// RUST_LOG=debug cargo test --package avalanche-consensus --lib -- decidable::test_decidable::test_decidable --exact --show-output
#[test]
fn test_decidable() {
    let id = Id::from_slice(&[1, 2, 3]);

    let mut decidable = TestDecidable::create_decidable(id, Status::Processing, Ok(()), Ok(()));
    assert_eq!(decidable.id(), id);
    assert_eq!(decidable.status(), Status::Processing);
    assert!(decidable.accept().is_ok());
    assert_eq!(decidable.status(), Status::Accepted);

    let mut decidable = TestDecidable::create_decidable(id, Status::Processing, Ok(()), Ok(()));
    assert_eq!(decidable.id(), id);
    assert_eq!(decidable.status(), Status::Processing);
    assert!(decidable.reject().is_ok());
    assert_eq!(decidable.status(), Status::Rejected);

    let mut decidable = TestDecidable::new(id, Status::Processing);
    decidable.set_accept_result(Err(Error::Other {
        message: "test error".to_string(),
        is_retryable: false,
    }));
    assert_eq!(decidable.id(), id);
    assert_eq!(decidable.status(), Status::Processing);
    assert!(decidable.accept().is_err());
    assert_eq!(decidable.status(), Status::Processing);

    let mut decidable = TestDecidable::create_decidable(
        id,
        Status::Processing,
        Ok(()),
        Err(Error::Other {
            message: "test error".to_string(),
            is_retryable: false,
        }),
    );
    assert_eq!(decidable.id(), id);
    assert_eq!(decidable.status(), Status::Processing);
    assert!(decidable.reject().is_err());
    assert_eq!(decidable.status(), Status::Processing);
}