amaru_kernel/cardano/
ballot_id.rs1use 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}