casper_types/transaction/
transaction_id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use alloc::vec::Vec;
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "datasize")]
use datasize::DataSize;
use serde::{Deserialize, Serialize};

#[cfg(doc)]
use super::Transaction;
use super::{ApprovalsHash, TransactionHash};
use crate::bytesrepr::{self, FromBytes, ToBytes};
#[cfg(any(feature = "testing", test))]
use crate::testing::TestRng;

/// The unique identifier of a [`Transaction`], comprising its [`TransactionHash`] and
/// [`ApprovalsHash`].
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[serde(deny_unknown_fields)]
pub struct TransactionId {
    /// The transaction hash.
    transaction_hash: TransactionHash,
    /// The approvals hash.
    approvals_hash: ApprovalsHash,
}

impl TransactionId {
    /// Returns a new `TransactionId`.
    pub fn new(transaction_hash: TransactionHash, approvals_hash: ApprovalsHash) -> Self {
        TransactionId {
            transaction_hash,
            approvals_hash,
        }
    }

    /// Returns the transaction hash.
    pub fn transaction_hash(&self) -> TransactionHash {
        self.transaction_hash
    }

    /// Returns the approvals hash.
    pub fn approvals_hash(&self) -> ApprovalsHash {
        self.approvals_hash
    }

    /// Returns a random `TransactionId`.
    #[cfg(any(feature = "testing", test))]
    pub fn random(rng: &mut TestRng) -> Self {
        TransactionId::new(TransactionHash::random(rng), ApprovalsHash::random(rng))
    }
}

impl Display for TransactionId {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(
            formatter,
            "transaction-id({}, {})",
            self.transaction_hash(),
            self.approvals_hash()
        )
    }
}

impl ToBytes for TransactionId {
    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
        self.transaction_hash.write_bytes(writer)?;
        self.approvals_hash.write_bytes(writer)
    }

    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        let mut buffer = bytesrepr::allocate_buffer(self)?;
        self.write_bytes(&mut buffer)?;
        Ok(buffer)
    }

    fn serialized_length(&self) -> usize {
        self.transaction_hash.serialized_length() + self.approvals_hash.serialized_length()
    }
}

impl FromBytes for TransactionId {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (transaction_hash, rem) = TransactionHash::from_bytes(bytes)?;
        let (approvals_hash, rem) = ApprovalsHash::from_bytes(rem)?;
        let transaction_id = TransactionId::new(transaction_hash, approvals_hash);
        Ok((transaction_id, rem))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bytesrepr_roundtrip() {
        let rng = &mut TestRng::new();
        let id = TransactionId::random(rng);
        bytesrepr::test_serialization_roundtrip(&id);
    }
}