Skip to main content

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