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
//! Transaction protocol types.
use std::{
    collections::VecDeque,
    ops::{Deref, DerefMut},
};

/// Batch of transaction inputs/outputs.
#[derive(Clone, Debug, Default, Eq, PartialEq, cbor::Encode, cbor::Decode)]
#[cbor(transparent)]
pub struct TxnBatch(pub Vec<Vec<u8>>);

impl TxnBatch {
    pub fn new(txs: Vec<Vec<u8>>) -> TxnBatch {
        TxnBatch(txs)
    }
}

impl Deref for TxnBatch {
    type Target = Vec<Vec<u8>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for TxnBatch {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Vec<Vec<u8>>> for TxnBatch {
    fn from(other: Vec<Vec<u8>>) -> TxnBatch {
        TxnBatch(other)
    }
}

impl From<VecDeque<Vec<u8>>> for TxnBatch {
    fn from(other: VecDeque<Vec<u8>>) -> TxnBatch {
        TxnBatch(other.into())
    }
}

impl From<TxnBatch> for Vec<Vec<u8>> {
    fn from(val: TxnBatch) -> Self {
        val.0
    }
}

impl From<TxnBatch> for VecDeque<Vec<u8>> {
    fn from(val: TxnBatch) -> Self {
        val.0.into()
    }
}

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

    use crate::common::crypto::hash::Hash;

    #[test]
    fn test_consistent_hash() {
        let batch = TxnBatch(vec![b"foo".to_vec(), b"bar".to_vec(), b"aaa".to_vec()]);
        let h = Hash::digest_bytes(&cbor::to_vec(batch));
        assert_eq!(
            h,
            Hash::from("c451dd4fd065b815e784aac6b300e479b2167408f0eebbb95a8bd36b9e71e34d")
        );
    }
}