Skip to main content

amaru_kernel/cardano/
proposal_pointer.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::{Slot, TransactionPointer, cbor};
16
17#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
18pub struct ProposalPointer {
19    pub transaction: TransactionPointer,
20    pub proposal_index: usize,
21}
22
23impl ProposalPointer {
24    pub fn slot(&self) -> Slot {
25        self.transaction.slot
26    }
27}
28
29impl<C> cbor::encode::Encode<C> for ProposalPointer {
30    fn encode<W: cbor::encode::Write>(
31        &self,
32        e: &mut cbor::Encoder<W>,
33        ctx: &mut C,
34    ) -> Result<(), cbor::encode::Error<W::Error>> {
35        e.array(2)?;
36        e.encode_with(self.transaction, ctx)?;
37        e.encode_with(self.proposal_index, ctx)?;
38        Ok(())
39    }
40}
41
42impl<'b, C> cbor::decode::Decode<'b, C> for ProposalPointer {
43    fn decode(d: &mut cbor::Decoder<'b>, ctx: &mut C) -> Result<Self, cbor::decode::Error> {
44        cbor::heterogeneous_array(d, |d, assert_len| {
45            assert_len(2)?;
46            Ok(ProposalPointer { transaction: d.decode_with(ctx)?, proposal_index: d.decode_with(ctx)? })
47        })
48    }
49}
50
51#[cfg(any(test, feature = "test-utils"))]
52pub use tests::*;
53
54#[cfg(any(test, feature = "test-utils"))]
55mod tests {
56    use proptest::{prelude::*, prop_compose};
57
58    use super::*;
59    use crate::{any_transaction_pointer, prop_cbor_roundtrip};
60
61    prop_cbor_roundtrip!(ProposalPointer, any_proposal_pointer(u64::MAX));
62
63    prop_compose! {
64        pub fn any_proposal_pointer(max_slot: u64)(
65            transaction in any_transaction_pointer(max_slot),
66            proposal_index in any::<usize>(),
67        ) -> ProposalPointer {
68            ProposalPointer {
69                transaction,
70                proposal_index,
71            }
72        }
73    }
74}