drive/state_transition_action/contract/data_contract_update/
mod.rs

1/// transformer
2pub mod transformer;
3/// v0
4pub mod v0;
5
6use crate::state_transition_action::contract::data_contract_update::v0::DataContractUpdateTransitionActionV0;
7use derive_more::From;
8use dpp::data_contract::DataContract;
9use dpp::prelude::{IdentityNonce, UserFeeIncrease};
10
11/// data contract update transition action
12#[derive(Debug, Clone, From)]
13pub enum DataContractUpdateTransitionAction {
14    /// v0
15    V0(DataContractUpdateTransitionActionV0),
16}
17
18impl DataContractUpdateTransitionAction {
19    /// data contract
20    pub fn data_contract(self) -> DataContract {
21        match self {
22            DataContractUpdateTransitionAction::V0(transition) => transition.data_contract,
23        }
24    }
25    /// data contract ref
26    pub fn data_contract_ref(&self) -> &DataContract {
27        match self {
28            DataContractUpdateTransitionAction::V0(transition) => &transition.data_contract,
29        }
30    }
31
32    /// data contract mut
33    pub fn data_contract_mut(&mut self) -> &mut DataContract {
34        match self {
35            DataContractUpdateTransitionAction::V0(transition) => &mut transition.data_contract,
36        }
37    }
38
39    /// identity contract nonce
40    pub fn identity_contract_nonce(&self) -> IdentityNonce {
41        match self {
42            DataContractUpdateTransitionAction::V0(transition) => {
43                transition.identity_contract_nonce
44            }
45        }
46    }
47
48    /// fee multiplier
49    pub fn user_fee_increase(&self) -> UserFeeIncrease {
50        match self {
51            DataContractUpdateTransitionAction::V0(transition) => transition.user_fee_increase,
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use dpp::data_contract::accessors::v0::DataContractV0Getters;
60    use dpp::tests::fixtures::get_data_contract_fixture;
61    use platform_version::version::PlatformVersion;
62
63    fn make_action() -> DataContractUpdateTransitionAction {
64        let fixture =
65            get_data_contract_fixture(None, 0, PlatformVersion::latest().protocol_version);
66        let dc = fixture.data_contract_owned();
67        let v0 = DataContractUpdateTransitionActionV0 {
68            data_contract: dc,
69            identity_contract_nonce: 99,
70            user_fee_increase: 3,
71        };
72        DataContractUpdateTransitionAction::from(v0)
73    }
74
75    /// The expected document type names produced by `get_data_contract_fixture`.
76    const EXPECTED_DOC_TYPES: [&str; 7] = [
77        "indexedDocument",
78        "niceDocument",
79        "noTimeDocument",
80        "optionalUniqueIndexedDocument",
81        "prettyDocument",
82        "uniqueDates",
83        "withByteArrays",
84    ];
85
86    #[test]
87    fn test_from_v0() {
88        let action = make_action();
89        assert!(matches!(action, DataContractUpdateTransitionAction::V0(_)));
90        // Verify the fixture values are accessible through the enum wrapper.
91        assert_eq!(action.identity_contract_nonce(), 99);
92        assert_eq!(action.user_fee_increase(), 3);
93        assert_eq!(action.data_contract_ref().version(), 1);
94    }
95
96    #[test]
97    fn test_data_contract_ref() {
98        let action = make_action();
99        let dc_ref = action.data_contract_ref();
100        // Verify the contract has the expected version and document types.
101        assert_eq!(dc_ref.version(), 1);
102        let doc_types = dc_ref.document_types();
103        assert_eq!(doc_types.len(), EXPECTED_DOC_TYPES.len());
104        for name in &EXPECTED_DOC_TYPES {
105            assert!(
106                dc_ref.has_document_type_for_name(name),
107                "expected document type '{}' not found",
108                name
109            );
110        }
111    }
112
113    #[test]
114    fn test_data_contract_owned() {
115        let action = make_action();
116        let original_id = action.data_contract_ref().id();
117        let original_owner = action.data_contract_ref().owner_id();
118        let dc = action.data_contract();
119        assert_eq!(dc.id(), original_id);
120        assert_eq!(dc.owner_id(), original_owner);
121        assert_eq!(dc.version(), 1);
122        assert_eq!(dc.document_types().len(), EXPECTED_DOC_TYPES.len());
123    }
124
125    #[test]
126    fn test_data_contract_mut() {
127        let mut action = make_action();
128        let original_id = action.data_contract_ref().id();
129        let dc_mut = action.data_contract_mut();
130        // Verify the mutable reference provides access to the same contract.
131        assert_eq!(dc_mut.id(), original_id);
132        assert_eq!(dc_mut.version(), 1);
133        assert_eq!(dc_mut.document_types().len(), EXPECTED_DOC_TYPES.len());
134    }
135
136    #[test]
137    fn test_identity_contract_nonce() {
138        let action = make_action();
139        assert_eq!(action.identity_contract_nonce(), 99);
140    }
141
142    #[test]
143    fn test_user_fee_increase() {
144        let action = make_action();
145        assert_eq!(action.user_fee_increase(), 3);
146    }
147
148    #[test]
149    fn test_clone() {
150        let action = make_action();
151        let cloned = action.clone();
152        assert_eq!(cloned.identity_contract_nonce(), 99);
153        assert_eq!(cloned.user_fee_increase(), 3);
154        // Verify the cloned contract preserves identity and structure.
155        assert_eq!(
156            cloned.data_contract_ref().id(),
157            action.data_contract_ref().id()
158        );
159        assert_eq!(
160            cloned.data_contract_ref().owner_id(),
161            action.data_contract_ref().owner_id()
162        );
163        assert_eq!(cloned.data_contract_ref().version(), 1);
164        assert_eq!(
165            cloned.data_contract_ref().document_types().len(),
166            EXPECTED_DOC_TYPES.len()
167        );
168    }
169
170    #[test]
171    fn test_debug() {
172        let action = make_action();
173        let debug_str = format!("{:?}", action);
174        assert!(debug_str.contains("V0"));
175    }
176}