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            encrypted_note: vec![0x01, 0x02, 0x03],
48        }
49    }
50
51    fn make_action() -> ShieldedTransferTransitionAction {
52        let v0 = ShieldedTransferTransitionActionV0 {
53            notes: vec![make_note(), make_note(), make_note()],
54            anchor: [0xCC; 32],
55            fee_amount: 100,
56            current_total_balance: 999999,
57        };
58        ShieldedTransferTransitionAction::from(v0)
59    }
60
61    #[test]
62    fn test_from_v0() {
63        let action = make_action();
64        assert!(matches!(action, ShieldedTransferTransitionAction::V0(_)));
65    }
66
67    #[test]
68    fn test_notes() {
69        let action = make_action();
70        let notes = action.notes();
71        assert_eq!(notes.len(), 3);
72        for note in notes {
73            assert_eq!(note.nullifier, [0xAA; 32]);
74            assert_eq!(note.cmx, [0xBB; 32]);
75            assert_eq!(note.encrypted_note, vec![0x01, 0x02, 0x03]);
76        }
77    }
78
79    #[test]
80    fn test_anchor() {
81        let action = make_action();
82        assert_eq!(*action.anchor(), [0xCC; 32]);
83    }
84
85    #[test]
86    fn test_fee_amount() {
87        let action = make_action();
88        assert_eq!(action.fee_amount(), 100);
89    }
90
91    #[test]
92    fn test_empty_transfer() {
93        let v0 = ShieldedTransferTransitionActionV0 {
94            notes: vec![],
95            anchor: [0x00; 32],
96            fee_amount: 0,
97            current_total_balance: 0,
98        };
99        let action = ShieldedTransferTransitionAction::from(v0);
100        assert!(action.notes().is_empty());
101        assert_eq!(*action.anchor(), [0x00; 32]);
102        assert_eq!(action.fee_amount(), 0);
103    }
104
105    #[test]
106    fn test_clone() {
107        let action = make_action();
108        let cloned = action.clone();
109        assert_eq!(cloned.notes().len(), 3);
110        assert_eq!(*cloned.anchor(), [0xCC; 32]);
111        assert_eq!(cloned.fee_amount(), 100);
112    }
113
114    #[test]
115    fn test_debug() {
116        let action = make_action();
117        let debug_str = format!("{:?}", action);
118        assert!(debug_str.contains("V0"));
119    }
120}