Skip to main content

amaru_kernel/cardano/
constitutional_committee.rs

1// Copyright 2025 PRAGMA
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{RationalNumber, cbor};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum ConstitutionalCommitteeStatus {
19    NoConfidence,
20    Trusted { threshold: RationalNumber },
21}
22
23impl<C> cbor::encode::Encode<C> for ConstitutionalCommitteeStatus {
24    fn encode<W: cbor::encode::Write>(
25        &self,
26        e: &mut cbor::Encoder<W>,
27        ctx: &mut C,
28    ) -> Result<(), cbor::encode::Error<W::Error>> {
29        match self {
30            Self::NoConfidence => {
31                e.array(1)?;
32                e.u8(0)?;
33            }
34            Self::Trusted { threshold } => {
35                e.array(2)?;
36                e.u8(1)?;
37                e.encode_with(threshold, ctx)?;
38            }
39        };
40        Ok(())
41    }
42}
43
44impl<'d, C> cbor::decode::Decode<'d, C> for ConstitutionalCommitteeStatus {
45    fn decode(d: &mut cbor::Decoder<'d>, ctx: &mut C) -> Result<Self, cbor::decode::Error> {
46        cbor::heterogeneous_array(d, |d, assert_len| match d.u8()? {
47            0 => {
48                assert_len(1)?;
49                Ok(Self::NoConfidence)
50            }
51            1 => {
52                assert_len(2)?;
53                let threshold = d.decode_with(ctx)?;
54                Ok(Self::Trusted { threshold })
55            }
56            t => Err(cbor::decode::Error::message(format!(
57                "unexpected ConstitutionalCommittee kind: {t}; expected 0 or 1."
58            ))),
59        })
60    }
61}
62
63#[cfg(any(test, feature = "test-utils"))]
64pub use tests::*;
65
66#[cfg(any(test, feature = "test-utils"))]
67mod tests {
68    use proptest::prelude::*;
69
70    use super::ConstitutionalCommitteeStatus::{self, *};
71    use crate::{any_rational_number, prop_cbor_roundtrip};
72
73    prop_cbor_roundtrip!(ConstitutionalCommitteeStatus, any_constitutional_committee_status());
74
75    pub fn any_constitutional_committee_status() -> impl Strategy<Value = ConstitutionalCommitteeStatus> {
76        prop_oneof![Just(NoConfidence), any_rational_number().prop_map(|threshold| Trusted { threshold }),]
77    }
78}