Skip to main content

amaru_kernel/cardano/
ballot_id.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::{ComparableProposalId, Voter, cbor};
16
17#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
18pub struct BallotId {
19    pub proposal: ComparableProposalId,
20    pub voter: Voter,
21}
22
23impl<C> cbor::encode::Encode<C> for BallotId {
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        e.array(2)?;
30        e.encode_with(&self.proposal, ctx)?;
31        e.encode_with(&self.voter, ctx)?;
32        Ok(())
33    }
34}
35
36impl<'d, C> cbor::decode::Decode<'d, C> for BallotId {
37    fn decode(d: &mut cbor::Decoder<'d>, ctx: &mut C) -> Result<Self, cbor::decode::Error> {
38        cbor::heterogeneous_array(d, |d, assert_len| {
39            assert_len(2)?;
40            Ok(Self { proposal: d.decode_with(ctx)?, voter: d.decode_with(ctx)? })
41        })
42    }
43}
44
45#[cfg(any(test, feature = "test-utils"))]
46pub use tests::*;
47
48#[cfg(any(test, feature = "test-utils"))]
49mod tests {
50    use proptest::{prelude::*, prop_compose};
51
52    use super::BallotId;
53    use crate::{Voter, any_comparable_proposal_id, any_hash28, prop_cbor_roundtrip};
54
55    prop_cbor_roundtrip!(BallotId, any_ballot_id());
56
57    pub fn any_voter() -> impl Strategy<Value = Voter> {
58        prop_oneof![
59            any_hash28().prop_map(Voter::ConstitutionalCommitteeKey),
60            any_hash28().prop_map(Voter::ConstitutionalCommitteeScript),
61            any_hash28().prop_map(Voter::DRepKey),
62            any_hash28().prop_map(Voter::DRepScript),
63            any_hash28().prop_map(Voter::StakePoolKey),
64        ]
65    }
66
67    prop_compose! {
68        pub fn any_ballot_id()(
69            proposal in any_comparable_proposal_id(),
70            voter in any_voter(),
71        ) -> BallotId {
72            BallotId {
73                proposal,
74                voter,
75            }
76        }
77    }
78}