Skip to content

Commit 46e037e

Browse files
test(dpp): improve coverage for identity state transitions and public keys in creation (#3456)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1dee9a0 commit 46e037e

15 files changed

Lines changed: 2409 additions & 0 deletions

File tree

packages/rs-dpp/src/state_transition/state_transitions/identity/identity_create_transition/mod.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,121 @@ impl StateTransitionFieldTypes for IdentityCreateTransition {
9494
vec![]
9595
}
9696
}
97+
98+
#[cfg(test)]
99+
mod test {
100+
use super::*;
101+
use crate::identity::state_transition::asset_lock_proof::AssetLockProof;
102+
use crate::serialization::{PlatformDeserializable, PlatformSerializable};
103+
use crate::state_transition::identity_create_transition::accessors::IdentityCreateTransitionAccessorsV0;
104+
use crate::state_transition::{
105+
StateTransitionEstimatedFeeValidation, StateTransitionHasUserFeeIncrease,
106+
StateTransitionLike, StateTransitionOwned, StateTransitionSingleSigned,
107+
StateTransitionType,
108+
};
109+
use crate::version::LATEST_PLATFORM_VERSION;
110+
use platform_value::{BinaryData, Identifier};
111+
112+
fn make_create() -> IdentityCreateTransition {
113+
IdentityCreateTransition::V0(IdentityCreateTransitionV0 {
114+
public_keys: vec![],
115+
asset_lock_proof: AssetLockProof::default(),
116+
user_fee_increase: 0,
117+
signature: [0u8; 65].to_vec().into(),
118+
identity_id: Identifier::random(),
119+
})
120+
}
121+
122+
#[test]
123+
fn test_default_versioned() {
124+
let t = IdentityCreateTransition::default_versioned(LATEST_PLATFORM_VERSION)
125+
.expect("should create default");
126+
match t {
127+
IdentityCreateTransition::V0(_) => {}
128+
}
129+
}
130+
131+
#[test]
132+
fn test_serialization_roundtrip() {
133+
let t = make_create();
134+
let bytes = t.serialize_to_bytes().expect("should serialize");
135+
let restored =
136+
IdentityCreateTransition::deserialize_from_bytes(&bytes).expect("should deserialize");
137+
assert_eq!(t, restored);
138+
}
139+
140+
#[test]
141+
fn test_state_transition_like() {
142+
let t = make_create();
143+
assert_eq!(
144+
t.state_transition_type(),
145+
StateTransitionType::IdentityCreate
146+
);
147+
assert_eq!(t.state_transition_protocol_version(), 0);
148+
let ids = t.modified_data_ids();
149+
assert_eq!(ids.len(), 1);
150+
}
151+
152+
#[test]
153+
fn test_owner_id() {
154+
let t = make_create();
155+
match &t {
156+
IdentityCreateTransition::V0(v0) => {
157+
assert_eq!(t.owner_id(), v0.identity_id);
158+
}
159+
}
160+
}
161+
162+
#[test]
163+
fn test_user_fee_increase() {
164+
let mut t = make_create();
165+
assert_eq!(t.user_fee_increase(), 0);
166+
t.set_user_fee_increase(5);
167+
assert_eq!(t.user_fee_increase(), 5);
168+
}
169+
170+
#[test]
171+
fn test_single_signed() {
172+
let mut t = make_create();
173+
assert_eq!(t.signature().len(), 65);
174+
t.set_signature(BinaryData::new(vec![1, 2, 3]));
175+
assert_eq!(t.signature().as_slice(), &[1, 2, 3]);
176+
t.set_signature_bytes(vec![4, 5]);
177+
assert_eq!(t.signature().as_slice(), &[4, 5]);
178+
}
179+
180+
#[test]
181+
fn test_accessors() {
182+
let t = make_create();
183+
assert!(t.public_keys().is_empty());
184+
assert_ne!(t.identity_id(), Identifier::default());
185+
}
186+
187+
#[test]
188+
fn test_field_types() {
189+
let sig = IdentityCreateTransition::signature_property_paths();
190+
assert_eq!(sig.len(), 2);
191+
let ids = IdentityCreateTransition::identifiers_property_paths();
192+
assert_eq!(ids.len(), 1);
193+
let bin = IdentityCreateTransition::binary_property_paths();
194+
assert!(bin.is_empty());
195+
}
196+
197+
#[test]
198+
fn test_estimated_fee() {
199+
let t = make_create();
200+
let fee = t
201+
.calculate_min_required_fee(LATEST_PLATFORM_VERSION)
202+
.expect("fee calc should work");
203+
assert!(fee > 0);
204+
}
205+
206+
#[test]
207+
fn test_into_from_v0() {
208+
let v0 = IdentityCreateTransitionV0::default();
209+
let t: IdentityCreateTransition = v0.clone().into();
210+
match t {
211+
IdentityCreateTransition::V0(inner) => assert_eq!(inner, v0),
212+
}
213+
}
214+
}

packages/rs-dpp/src/state_transition/state_transitions/identity/identity_create_transition/v0/mod.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,122 @@ impl IdentityCreateTransitionV0 {
130130
}
131131
}
132132
}
133+
134+
#[cfg(test)]
135+
mod test {
136+
use super::*;
137+
use crate::state_transition::identity_create_transition::accessors::IdentityCreateTransitionAccessorsV0;
138+
use crate::state_transition::{
139+
StateTransitionHasUserFeeIncrease, StateTransitionLike, StateTransitionOwned,
140+
StateTransitionSingleSigned, StateTransitionType,
141+
};
142+
use platform_value::BinaryData;
143+
144+
fn make_create_v0() -> IdentityCreateTransitionV0 {
145+
IdentityCreateTransitionV0 {
146+
public_keys: vec![],
147+
asset_lock_proof: AssetLockProof::default(),
148+
user_fee_increase: 0,
149+
signature: [0u8; 65].to_vec().into(),
150+
identity_id: Identifier::random(),
151+
}
152+
}
153+
154+
#[test]
155+
fn test_default() {
156+
let t = IdentityCreateTransitionV0::default();
157+
assert_eq!(t.user_fee_increase, 0);
158+
assert!(t.public_keys.is_empty());
159+
assert!(t.signature.is_empty());
160+
}
161+
162+
#[test]
163+
fn test_state_transition_like() {
164+
let t = make_create_v0();
165+
assert_eq!(
166+
t.state_transition_type(),
167+
StateTransitionType::IdentityCreate
168+
);
169+
assert_eq!(t.state_transition_protocol_version(), 0);
170+
assert_eq!(t.modified_data_ids(), vec![t.identity_id]);
171+
}
172+
173+
#[test]
174+
fn test_unique_identifiers() {
175+
let t = make_create_v0();
176+
let ids = t.unique_identifiers();
177+
assert_eq!(ids.len(), 1);
178+
assert!(!ids[0].is_empty());
179+
}
180+
181+
#[test]
182+
fn test_owner_id() {
183+
let t = make_create_v0();
184+
assert_eq!(t.owner_id(), t.identity_id);
185+
}
186+
187+
#[test]
188+
fn test_user_fee_increase() {
189+
let mut t = make_create_v0();
190+
assert_eq!(t.user_fee_increase(), 0);
191+
t.set_user_fee_increase(7);
192+
assert_eq!(t.user_fee_increase(), 7);
193+
}
194+
195+
#[test]
196+
fn test_single_signed() {
197+
let mut t = make_create_v0();
198+
assert_eq!(t.signature().len(), 65);
199+
t.set_signature(BinaryData::new(vec![1, 2, 3]));
200+
assert_eq!(t.signature().as_slice(), &[1, 2, 3]);
201+
t.set_signature_bytes(vec![4, 5]);
202+
assert_eq!(t.signature().as_slice(), &[4, 5]);
203+
}
204+
205+
#[test]
206+
fn test_into_state_transition() {
207+
use crate::state_transition::StateTransition;
208+
let t = make_create_v0();
209+
let st: StateTransition = t.into();
210+
match st {
211+
StateTransition::IdentityCreate(_) => {}
212+
_ => panic!("expected IdentityCreate"),
213+
}
214+
}
215+
216+
#[test]
217+
fn test_accessors() {
218+
let mut t = make_create_v0();
219+
assert!(t.public_keys().is_empty());
220+
assert_eq!(t.identity_id(), t.identity_id);
221+
222+
// Test set_public_keys and add_public_keys
223+
t.set_public_keys(vec![]);
224+
assert!(t.public_keys().is_empty());
225+
}
226+
227+
#[test]
228+
fn test_to_object_produces_value() {
229+
use crate::state_transition::StateTransitionValueConvert;
230+
let t = make_create_v0();
231+
let obj = t.to_object(false).expect("to_object should work");
232+
assert!(obj.is_map());
233+
}
234+
235+
#[test]
236+
fn test_value_conversion_skip_signature() {
237+
use crate::state_transition::StateTransitionValueConvert;
238+
let t = make_create_v0();
239+
let obj = t.to_object(true).expect("to_object should work");
240+
let map = obj.into_btree_string_map().expect("should be a map");
241+
assert!(!map.contains_key("signature"));
242+
}
243+
244+
#[test]
245+
fn test_to_cleaned_object() {
246+
use crate::state_transition::StateTransitionValueConvert;
247+
let t = make_create_v0();
248+
let obj = t.to_cleaned_object(false).expect("should work");
249+
assert!(obj.is_map());
250+
}
251+
}

0 commit comments

Comments
 (0)