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