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
// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
// This file is part of the HotShot repository.

// You should have received a copy of the MIT License
// along with the HotShot repository. If not, see <https://mit-license.org/>.

use std::sync::Arc;

use async_lock::RwLock;
use async_trait::async_trait;
use hotshot_task_impls::events::{HotShotEvent, HotShotEvent::*};
use hotshot_types::{
    data::null_block,
    traits::{block_contents::BlockHeader, node_implementation::NodeType},
};

use crate::predicates::{Predicate, PredicateResult};

type EventCallback<TYPES> = Arc<dyn Fn(Arc<HotShotEvent<TYPES>>) -> bool + Send + Sync>;

pub struct EventPredicate<TYPES>
where
    TYPES: NodeType + Send + Sync,
{
    check: EventCallback<TYPES>,
    info: String,
}

impl<TYPES: NodeType> std::fmt::Debug for EventPredicate<TYPES> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.info)
    }
}

#[allow(clippy::type_complexity)]
pub struct TestPredicate<INPUT> {
    pub function: Arc<RwLock<dyn FnMut(&INPUT) -> PredicateResult + Send + Sync>>,
    pub info: String,
}

impl<INPUT> std::fmt::Debug for TestPredicate<INPUT> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.info)
    }
}

#[async_trait]
impl<INPUT> Predicate<INPUT> for TestPredicate<INPUT>
where
    INPUT: Send + Sync,
{
    async fn evaluate(&self, input: &INPUT) -> PredicateResult {
        let mut function = self.function.write().await;
        function(input)
    }

    async fn info(&self) -> String {
        self.info.clone()
    }
}

pub fn all<TYPES>(events: Vec<HotShotEvent<TYPES>>) -> Box<TestPredicate<Arc<HotShotEvent<TYPES>>>>
where
    TYPES: NodeType,
{
    all_predicates(events.into_iter().map(exact).collect())
}

pub fn all_predicates<TYPES: NodeType>(
    predicates: Vec<Box<EventPredicate<TYPES>>>,
) -> Box<TestPredicate<Arc<HotShotEvent<TYPES>>>> {
    let info = format!("{:?}", predicates);

    let mut unsatisfied: Vec<_> = predicates.into_iter().map(Arc::new).collect();

    let function = move |e: &Arc<HotShotEvent<TYPES>>| {
        if !unsatisfied
            .clone()
            .into_iter()
            .map(|pred| (pred.check)(e.clone()))
            .any(|val| val)
        {
            return PredicateResult::Fail;
        }

        unsatisfied.retain(|pred| !(pred.check)(e.clone()));

        if unsatisfied.is_empty() {
            PredicateResult::Pass
        } else {
            PredicateResult::Incomplete
        }
    };

    Box::new(TestPredicate {
        function: Arc::new(RwLock::new(function)),
        info,
    })
}

#[macro_export]
macro_rules! all_predicates {
    ($($x:expr),* $(,)?) => {
        {
            vec![all_predicates(vec![$($x),*])]
        }
    };
}

#[async_trait]
impl<TYPES> Predicate<Arc<HotShotEvent<TYPES>>> for EventPredicate<TYPES>
where
    TYPES: NodeType + Send + Sync + 'static,
{
    async fn evaluate(&self, input: &Arc<HotShotEvent<TYPES>>) -> PredicateResult {
        PredicateResult::from((self.check)(input.clone()))
    }

    async fn info(&self) -> String {
        self.info.clone()
    }
}

pub fn exact<TYPES>(event: HotShotEvent<TYPES>) -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = format!("{:?}", event);
    let event = Arc::new(event);

    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        let event_clone = event.clone();
        *e == *event_clone
    });

    Box::new(EventPredicate { check, info })
}

pub fn quorum_vote_send<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumVoteSend".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), QuorumVoteSend(_)));

    Box::new(EventPredicate { check, info })
}

pub fn view_change<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "ViewChange".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), ViewChange(_)));
    Box::new(EventPredicate { check, info })
}

pub fn upgrade_certificate_formed<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "UpgradeCertificateFormed".to_string();
    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        matches!(e.as_ref(), UpgradeCertificateFormed(_))
    });
    Box::new(EventPredicate { check, info })
}

pub fn quorum_proposal_send_with_upgrade_certificate<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumProposalSend with UpgradeCertificate attached".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| match e.as_ref() {
            QuorumProposalSend(proposal, _) => proposal.data.upgrade_certificate.is_some(),
            _ => false,
        });
    Box::new(EventPredicate { info, check })
}

pub fn quorum_proposal_validated<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumProposalValidated".to_string();
    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        matches!(*e.clone(), QuorumProposalValidated(..))
    });
    Box::new(EventPredicate { check, info })
}

pub fn quorum_proposal_send<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumProposalSend".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), QuorumProposalSend(..)));
    Box::new(EventPredicate { check, info })
}

pub fn quorum_proposal_send_with_null_block<TYPES>(
    num_storage_nodes: usize,
) -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumProposalSend with null block payload".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| match e.as_ref() {
            QuorumProposalSend(proposal, _) => {
                Some(proposal.data.block_header.payload_commitment())
                    == null_block::commitment(num_storage_nodes)
            }
            _ => false,
        });
    Box::new(EventPredicate { check, info })
}

pub fn timeout_vote_send<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "TimeoutVoteSend".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), TimeoutVoteSend(..)));
    Box::new(EventPredicate { check, info })
}

pub fn view_sync_timeout<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "ViewSyncTimeout".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), ViewSyncTimeout(..)));
    Box::new(EventPredicate { check, info })
}

pub fn view_sync_precommit_vote_send<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "ViewSyncPreCommitVoteSend".to_string();
    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        matches!(e.as_ref(), ViewSyncPreCommitVoteSend(..))
    });
    Box::new(EventPredicate { check, info })
}

pub fn vid_share_validated<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "VidShareValidated".to_string();
    let check: EventCallback<TYPES> =
        Arc::new(move |e: Arc<HotShotEvent<TYPES>>| matches!(e.as_ref(), VidShareValidated(..)));
    Box::new(EventPredicate { check, info })
}

pub fn da_certificate_validated<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "DaCertificateValidated".to_string();
    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        matches!(e.as_ref(), DaCertificateValidated(..))
    });
    Box::new(EventPredicate { check, info })
}

pub fn quorum_proposal_preliminarily_validated<TYPES>() -> Box<EventPredicate<TYPES>>
where
    TYPES: NodeType,
{
    let info = "QuorumProposalPreliminarilyValidated".to_string();
    let check: EventCallback<TYPES> = Arc::new(move |e: Arc<HotShotEvent<TYPES>>| {
        matches!(e.as_ref(), QuorumProposalPreliminarilyValidated(..))
    });
    Box::new(EventPredicate { check, info })
}