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, TestVersions};
11use hotshot_task_impls::quorum_vote::QuorumVoteTaskState;
12use hotshot_types::simple_certificate::UpgradeCertificate;
13
14use crate::predicates::{Predicate, PredicateResult};
15type QuorumVoteTaskTestState = QuorumVoteTaskState<TestTypes, MemoryImpl, TestVersions>;
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
35            .upgrade_lock
36            .decided_upgrade_certificate
37            .read()
38            .await
39            .clone();
40        PredicateResult::from((self.check)(upgrade_cert.into()))
41    }
42
43    async fn info(&self) -> String {
44        self.info.clone()
45    }
46}
47
48pub fn no_decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
49    let info = "expected decided_upgrade_certificate to be None".to_string();
50    let check: UpgradeCertCallback = Arc::new(move |s| s.is_none());
51    Box::new(UpgradeCertPredicate { info, check })
52}
53
54pub fn decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
55    let info = "expected decided_upgrade_certificate to be Some(_)".to_string();
56    let check: UpgradeCertCallback = Arc::new(move |s| s.is_some());
57    Box::new(UpgradeCertPredicate { info, check })
58}