Skip to main content

drive/state_transition_action/shielded/shielded_transfer/
mod.rs

1/// transformer
2pub mod transformer;
3/// v0
4pub mod v0;
5
6use crate::state_transition_action::shielded::shielded_transfer::v0::ShieldedTransferTransitionActionV0;
7use crate::state_transition_action::shielded::ShieldedActionNote;
8use derive_more::From;
9use dpp::fee::Credits;
10
11/// Shielded transfer transition action
12#[derive(Debug, Clone, From)]
13pub enum ShieldedTransferTransitionAction {
14    /// v0
15    V0(ShieldedTransferTransitionActionV0),
16}
17
18impl ShieldedTransferTransitionAction {
19    /// Get notes
20    pub fn notes(&self) -> &[ShieldedActionNote] {
21        match self {
22            ShieldedTransferTransitionAction::V0(transition) => &transition.notes,
23        }
24    }
25    /// Get anchor
26    pub fn anchor(&self) -> &[u8; 32] {
27        match self {
28            ShieldedTransferTransitionAction::V0(transition) => &transition.anchor,
29        }
30    }
31    /// Get fee amount
32    pub fn fee_amount(&self) -> Credits {
33        match self {
34            ShieldedTransferTransitionAction::V0(transition) => transition.fee_amount,
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    fn make_note() -> ShieldedActionNote {
44        ShieldedActionNote {
45            nullifier: [0xAA; 32],
46            cmx: [0xBB; 32],
47            cv_net: [0x22; 32],
48            encrypted_note: vec![0x01, 0x02, 0x03],
49        }
50    }
51
52    fn make_action() -> ShieldedTransferTransitionAction {
53        let v0 = ShieldedTransferTransitionActionV0 {
54            notes: vec![make_note(), make_note(), make_note()],
55            anchor: [0xCC; 32],
56            fee_amount: 100,
57            current_total_balance: 999999,
58        };
59        ShieldedTransferTransitionAction::from(v0)
60    }
61
62    #[test]
63    fn test_from_v0() {
64        let action = make_action();
65        assert!(matches!(action, ShieldedTransferTransitionAction::V0(_)));
66    }
67
68    #[test]
69    fn test_notes() {
70        let action = make_action();
71        let notes = action.notes();
72        assert_eq!(notes.len(), 3);
73        for note in notes {
74            assert_eq!(note.nullifier, [0xAA; 32]);
75            assert_eq!(note.cmx, [0xBB; 32]);
76            assert_eq!(note.encrypted_note, vec![0x01, 0x02, 0x03]);
77        }
78    }
79
80    #[test]
81    fn test_anchor() {
82        let action = make_action();
83        assert_eq!(*action.anchor(), [0xCC; 32]);
84    }
85
86    #[test]
87    fn test_fee_amount() {
88        let action = make_action();
89        assert_eq!(action.fee_amount(), 100);
90    }
91
92    #[test]
93    fn test_empty_transfer() {
94        let v0 = ShieldedTransferTransitionActionV0 {
95            notes: vec![],
96            anchor: [0x00; 32],
97            fee_amount: 0,
98            current_total_balance: 0,
99        };
100        let action = ShieldedTransferTransitionAction::from(v0);
101        assert!(action.notes().is_empty());
102        assert_eq!(*action.anchor(), [0x00; 32]);
103        assert_eq!(action.fee_amount(), 0);
104    }
105
106    #[test]
107    fn test_clone() {
108        let action = make_action();
109        let cloned = action.clone();
110        assert_eq!(cloned.notes().len(), 3);
111        assert_eq!(*cloned.anchor(), [0xCC; 32]);
112        assert_eq!(cloned.fee_amount(), 100);
113    }
114
115    #[test]
116    fn test_debug() {
117        let action = make_action();
118        let debug_str = format!("{:?}", action);
119        assert!(debug_str.contains("V0"));
120    }
121}