hotshot_testing/predicates/
upgrade_with_proposal.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_proposal::QuorumProposalTaskState;
12use hotshot_types::simple_certificate::UpgradeCertificate;
13
14use crate::predicates::{Predicate, PredicateResult};
15
16type QuorumProposalTaskTestState = QuorumProposalTaskState<TestTypes, MemoryImpl>;
17
18type UpgradeCertCallback =
19    Arc<dyn Fn(Arc<Option<UpgradeCertificate<TestTypes>>>) -> bool + Send + Sync>;
20
21pub struct UpgradeCertPredicate {
22    check: UpgradeCertCallback,
23    info: String,
24}
25
26impl std::fmt::Debug for UpgradeCertPredicate {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{}", self.info)
29    }
30}
31
32#[async_trait]
33impl Predicate<QuorumProposalTaskTestState> for UpgradeCertPredicate {
34    async fn evaluate(&self, input: &QuorumProposalTaskTestState) -> PredicateResult {
35        let upgrade_cert = input.upgrade_lock.decided_upgrade_cert();
36        PredicateResult::from((self.check)(upgrade_cert.into()))
37    }
38
39    async fn info(&self) -> String {
40        self.info.clone()
41    }
42}
43
44pub fn no_decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
45    let info = "expected decided_upgrade_certificate to be None".to_string();
46    let check: UpgradeCertCallback = Arc::new(move |s| s.is_none());
47    Box::new(UpgradeCertPredicate { info, check })
48}
49
50pub fn decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
51    let info = "expected decided_upgrade_certificate to be Some(_)".to_string();
52    let check: UpgradeCertCallback = Arc::new(move |s| s.is_some());
53    Box::new(UpgradeCertPredicate { info, check })
54}