drive/state_transition_action/contract/data_contract_create/
mod.rs1pub mod transformer;
3pub mod v0;
5
6use crate::state_transition_action::contract::data_contract_create::v0::DataContractCreateTransitionActionV0;
7use derive_more::From;
8use dpp::data_contract::DataContract;
9use dpp::prelude::{IdentityNonce, UserFeeIncrease};
10
11#[derive(Debug, Clone, From)]
13pub enum DataContractCreateTransitionAction {
14 V0(DataContractCreateTransitionActionV0),
16}
17
18impl DataContractCreateTransitionAction {
19 pub fn data_contract(self) -> DataContract {
21 match self {
22 DataContractCreateTransitionAction::V0(transition) => transition.data_contract,
23 }
24 }
25 pub fn data_contract_ref(&self) -> &DataContract {
27 match self {
28 DataContractCreateTransitionAction::V0(transition) => &transition.data_contract,
29 }
30 }
31
32 pub fn identity_nonce(&self) -> IdentityNonce {
34 match self {
35 DataContractCreateTransitionAction::V0(transition) => transition.identity_nonce,
36 }
37 }
38
39 pub fn user_fee_increase(&self) -> UserFeeIncrease {
41 match self {
42 DataContractCreateTransitionAction::V0(transition) => transition.user_fee_increase,
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use dpp::data_contract::accessors::v0::DataContractV0Getters;
51 use dpp::tests::fixtures::get_data_contract_fixture;
52 use platform_version::version::PlatformVersion;
53
54 fn make_action() -> DataContractCreateTransitionAction {
55 let fixture =
56 get_data_contract_fixture(None, 0, PlatformVersion::latest().protocol_version);
57 let dc = fixture.data_contract_owned();
58 let v0 = DataContractCreateTransitionActionV0 {
59 data_contract: dc,
60 identity_nonce: 42,
61 user_fee_increase: 7,
62 };
63 DataContractCreateTransitionAction::from(v0)
64 }
65
66 const EXPECTED_DOC_TYPES: [&str; 7] = [
68 "indexedDocument",
69 "niceDocument",
70 "noTimeDocument",
71 "optionalUniqueIndexedDocument",
72 "prettyDocument",
73 "uniqueDates",
74 "withByteArrays",
75 ];
76
77 #[test]
78 fn test_from_v0() {
79 let action = make_action();
80 assert!(matches!(action, DataContractCreateTransitionAction::V0(_)));
81 assert_eq!(action.identity_nonce(), 42);
83 assert_eq!(action.user_fee_increase(), 7);
84 assert_eq!(action.data_contract_ref().version(), 1);
85 }
86
87 #[test]
88 fn test_data_contract_ref() {
89 let action = make_action();
90 let dc_ref = action.data_contract_ref();
91 assert_eq!(dc_ref.version(), 1);
93 let doc_types = dc_ref.document_types();
94 assert_eq!(doc_types.len(), EXPECTED_DOC_TYPES.len());
95 for name in &EXPECTED_DOC_TYPES {
96 assert!(
97 dc_ref.has_document_type_for_name(name),
98 "expected document type '{}' not found",
99 name
100 );
101 }
102 }
103
104 #[test]
105 fn test_data_contract_owned() {
106 let action = make_action();
107 let original_id = action.data_contract_ref().id();
108 let original_owner = action.data_contract_ref().owner_id();
109 let dc = action.data_contract();
110 assert_eq!(dc.id(), original_id);
111 assert_eq!(dc.owner_id(), original_owner);
112 assert_eq!(dc.version(), 1);
113 assert_eq!(dc.document_types().len(), EXPECTED_DOC_TYPES.len());
114 }
115
116 #[test]
117 fn test_identity_nonce() {
118 let action = make_action();
119 assert_eq!(action.identity_nonce(), 42);
120 }
121
122 #[test]
123 fn test_user_fee_increase() {
124 let action = make_action();
125 assert_eq!(action.user_fee_increase(), 7);
126 }
127
128 #[test]
129 fn test_clone() {
130 let action = make_action();
131 let cloned = action.clone();
132 assert_eq!(cloned.identity_nonce(), 42);
133 assert_eq!(cloned.user_fee_increase(), 7);
134 assert_eq!(
136 cloned.data_contract_ref().id(),
137 action.data_contract_ref().id()
138 );
139 assert_eq!(
140 cloned.data_contract_ref().owner_id(),
141 action.data_contract_ref().owner_id()
142 );
143 assert_eq!(cloned.data_contract_ref().version(), 1);
144 assert_eq!(
145 cloned.data_contract_ref().document_types().len(),
146 EXPECTED_DOC_TYPES.len()
147 );
148 }
149
150 #[test]
151 fn test_debug() {
152 let action = make_action();
153 let debug_str = format!("{:?}", action);
154 assert!(debug_str.contains("V0"));
155 }
156}