hotshot_testing/predicates/
upgrade_with_vote.rs

1// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
2// This file is part of the HotShot repository.
3
4// You should have received a copy of the MIT License
5// along with the HotShot repository. If not, see <https://mit-license.org/>.
6
7use std::sync::Arc;
8
9use async_trait::async_trait;
10use hotshot_example_types::node_types::{MemoryImpl, TestTypes};
11use hotshot_task_impls::quorum_vote::QuorumVoteTaskState;
12use hotshot_types::simple_certificate::UpgradeCertificate;
13
14use crate::predicates::{Predicate, PredicateResult};
15type QuorumVoteTaskTestState = QuorumVoteTaskState<TestTypes, MemoryImpl>;
16
17type UpgradeCertCallback =
18    Arc<dyn Fn(Arc<Option<UpgradeCertificate<TestTypes>>>) -> bool + Send + Sync>;
19
20pub struct UpgradeCertPredicate {
21    check: UpgradeCertCallback,
22    info: String,
23}
24
25impl std::fmt::Debug for UpgradeCertPredicate {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{}", self.info)
28    }
29}
30
31#[async_trait]
32impl Predicate<QuorumVoteTaskTestState> for UpgradeCertPredicate {
33    async fn evaluate(&self, input: &QuorumVoteTaskTestState) -> PredicateResult {
34        let upgrade_cert = input.upgrade_lock.decided_upgrade_cert();
35        PredicateResult::from((self.check)(upgrade_cert.into()))
36    }
37
38    async fn info(&self) -> String {
39        self.info.clone()
40    }
41}
42
43pub fn no_decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
44    let info = "expected decided_upgrade_certificate to be None".to_string();
45    let check: UpgradeCertCallback = Arc::new(move |s| s.is_none());
46    Box::new(UpgradeCertPredicate { info, check })
47}
48
49pub fn decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
50    let info = "expected decided_upgrade_certificate to be Some(_)".to_string();
51    let check: UpgradeCertCallback = Arc::new(move |s| s.is_some());
52    Box::new(UpgradeCertPredicate { info, check })
53}