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, TestVersions};
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, TestVersions>;
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
36            .upgrade_lock
37            .decided_upgrade_certificate
38            .read()
39            .await
40            .clone();
41        PredicateResult::from((self.check)(upgrade_cert.into()))
42    }
43
44    async fn info(&self) -> String {
45        self.info.clone()
46    }
47}
48
49pub fn no_decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
50    let info = "expected decided_upgrade_certificate to be None".to_string();
51    let check: UpgradeCertCallback = Arc::new(move |s| s.is_none());
52    Box::new(UpgradeCertPredicate { info, check })
53}
54
55pub fn decided_upgrade_certificate() -> Box<UpgradeCertPredicate> {
56    let info = "expected decided_upgrade_certificate to be Some(_)".to_string();
57    let check: UpgradeCertCallback = Arc::new(move |s| s.is_some());
58    Box::new(UpgradeCertPredicate { info, check })
59}