oasis_core_runtime/transaction/
types.rs

1//! Transaction protocol types.
2use std::{
3    collections::VecDeque,
4    ops::{Deref, DerefMut},
5};
6
7/// Batch of transaction inputs/outputs.
8#[derive(Clone, Debug, Default, Eq, PartialEq, cbor::Encode, cbor::Decode)]
9#[cbor(transparent)]
10pub struct TxnBatch(pub Vec<Vec<u8>>);
11
12impl TxnBatch {
13    pub fn new(txs: Vec<Vec<u8>>) -> TxnBatch {
14        TxnBatch(txs)
15    }
16}
17
18impl Deref for TxnBatch {
19    type Target = Vec<Vec<u8>>;
20
21    fn deref(&self) -> &Self::Target {
22        &self.0
23    }
24}
25
26impl DerefMut for TxnBatch {
27    fn deref_mut(&mut self) -> &mut Self::Target {
28        &mut self.0
29    }
30}
31
32impl From<Vec<Vec<u8>>> for TxnBatch {
33    fn from(other: Vec<Vec<u8>>) -> TxnBatch {
34        TxnBatch(other)
35    }
36}
37
38impl From<VecDeque<Vec<u8>>> for TxnBatch {
39    fn from(other: VecDeque<Vec<u8>>) -> TxnBatch {
40        TxnBatch(other.into())
41    }
42}
43
44impl From<TxnBatch> for Vec<Vec<u8>> {
45    fn from(val: TxnBatch) -> Self {
46        val.0
47    }
48}
49
50impl From<TxnBatch> for VecDeque<Vec<u8>> {
51    fn from(val: TxnBatch) -> Self {
52        val.0.into()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    use crate::common::crypto::hash::Hash;
61
62    #[test]
63    fn test_consistent_hash() {
64        let batch = TxnBatch(vec![b"foo".to_vec(), b"bar".to_vec(), b"aaa".to_vec()]);
65        let h = Hash::digest_bytes(&cbor::to_vec(batch));
66        assert_eq!(
67            h,
68            Hash::from("c451dd4fd065b815e784aac6b300e479b2167408f0eebbb95a8bd36b9e71e34d")
69        );
70    }
71}