|
| 1 | +use super::*; |
| 2 | + |
| 3 | +mod additional_validation_tests { |
| 4 | + use super::*; |
| 5 | + use dpp::tokens::info::v0::IdentityTokenInfoV0Accessors; |
| 6 | + |
| 7 | + #[test] |
| 8 | + fn test_token_freeze_already_frozen_should_fail() { |
| 9 | + let platform_version = PlatformVersion::latest(); |
| 10 | + let mut platform = TestPlatformBuilder::new() |
| 11 | + .with_latest_protocol_version() |
| 12 | + .build_with_mock_rpc() |
| 13 | + .set_genesis_state(); |
| 14 | + |
| 15 | + let mut rng = StdRng::seed_from_u64(49853); |
| 16 | + let platform_state = platform.state.load(); |
| 17 | + |
| 18 | + let (identity, signer, key) = |
| 19 | + setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5)); |
| 20 | + |
| 21 | + let (identity_2, _, _) = setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5)); |
| 22 | + |
| 23 | + let (contract, token_id) = create_token_contract_with_owner_identity( |
| 24 | + &mut platform, |
| 25 | + identity.id(), |
| 26 | + Some(|token_configuration: &mut TokenConfiguration| { |
| 27 | + token_configuration.set_freeze_rules(ChangeControlRules::V0( |
| 28 | + ChangeControlRulesV0 { |
| 29 | + authorized_to_make_change: AuthorizedActionTakers::ContractOwner, |
| 30 | + admin_action_takers: AuthorizedActionTakers::NoOne, |
| 31 | + changing_authorized_action_takers_to_no_one_allowed: false, |
| 32 | + changing_admin_action_takers_to_no_one_allowed: false, |
| 33 | + self_changing_admin_action_takers_allowed: false, |
| 34 | + }, |
| 35 | + )); |
| 36 | + }), |
| 37 | + None, |
| 38 | + None, |
| 39 | + None, |
| 40 | + platform_version, |
| 41 | + ); |
| 42 | + |
| 43 | + // Freeze identity_2 |
| 44 | + let freeze_transition = BatchTransition::new_token_freeze_transition( |
| 45 | + token_id, |
| 46 | + identity.id(), |
| 47 | + contract.id(), |
| 48 | + 0, |
| 49 | + identity_2.id(), |
| 50 | + None, |
| 51 | + None, |
| 52 | + &key, |
| 53 | + 2, |
| 54 | + 0, |
| 55 | + &signer, |
| 56 | + platform_version, |
| 57 | + None, |
| 58 | + ) |
| 59 | + .expect("expected to create freeze transition"); |
| 60 | + |
| 61 | + let serialized = freeze_transition |
| 62 | + .serialize_to_bytes() |
| 63 | + .expect("expected to serialize"); |
| 64 | + |
| 65 | + let transaction = platform.drive.grove.start_transaction(); |
| 66 | + |
| 67 | + let processing_result = platform |
| 68 | + .platform |
| 69 | + .process_raw_state_transitions( |
| 70 | + &[serialized], |
| 71 | + &platform_state, |
| 72 | + &BlockInfo::default(), |
| 73 | + &transaction, |
| 74 | + platform_version, |
| 75 | + false, |
| 76 | + None, |
| 77 | + ) |
| 78 | + .expect("expected to process state transition"); |
| 79 | + |
| 80 | + assert_matches!( |
| 81 | + processing_result.execution_results().as_slice(), |
| 82 | + [StateTransitionExecutionResult::SuccessfulExecution { .. }] |
| 83 | + ); |
| 84 | + |
| 85 | + platform |
| 86 | + .drive |
| 87 | + .grove |
| 88 | + .commit_transaction(transaction) |
| 89 | + .unwrap() |
| 90 | + .expect("expected to commit transaction"); |
| 91 | + |
| 92 | + // Verify frozen |
| 93 | + let info = platform |
| 94 | + .drive |
| 95 | + .fetch_identity_token_info( |
| 96 | + token_id.to_buffer(), |
| 97 | + identity_2.id().to_buffer(), |
| 98 | + None, |
| 99 | + platform_version, |
| 100 | + ) |
| 101 | + .expect("expected to fetch token info"); |
| 102 | + assert_eq!(info.map(|i| i.frozen()), Some(true)); |
| 103 | + |
| 104 | + // Try to freeze again - should fail |
| 105 | + let freeze_again_transition = BatchTransition::new_token_freeze_transition( |
| 106 | + token_id, |
| 107 | + identity.id(), |
| 108 | + contract.id(), |
| 109 | + 0, |
| 110 | + identity_2.id(), |
| 111 | + None, |
| 112 | + None, |
| 113 | + &key, |
| 114 | + 3, |
| 115 | + 0, |
| 116 | + &signer, |
| 117 | + platform_version, |
| 118 | + None, |
| 119 | + ) |
| 120 | + .expect("expected to create freeze transition"); |
| 121 | + |
| 122 | + let serialized = freeze_again_transition |
| 123 | + .serialize_to_bytes() |
| 124 | + .expect("expected to serialize"); |
| 125 | + |
| 126 | + let transaction = platform.drive.grove.start_transaction(); |
| 127 | + |
| 128 | + let processing_result = platform |
| 129 | + .platform |
| 130 | + .process_raw_state_transitions( |
| 131 | + &[serialized], |
| 132 | + &platform_state, |
| 133 | + &BlockInfo::default(), |
| 134 | + &transaction, |
| 135 | + platform_version, |
| 136 | + false, |
| 137 | + None, |
| 138 | + ) |
| 139 | + .expect("expected to process state transition"); |
| 140 | + |
| 141 | + assert_matches!( |
| 142 | + processing_result.execution_results().as_slice(), |
| 143 | + [PaidConsensusError { |
| 144 | + error: ConsensusError::StateError( |
| 145 | + StateError::IdentityTokenAccountAlreadyFrozenError(_) |
| 146 | + ), |
| 147 | + .. |
| 148 | + }] |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_token_unfreeze_not_frozen_should_fail() { |
| 154 | + let platform_version = PlatformVersion::latest(); |
| 155 | + let mut platform = TestPlatformBuilder::new() |
| 156 | + .with_latest_protocol_version() |
| 157 | + .build_with_mock_rpc() |
| 158 | + .set_genesis_state(); |
| 159 | + |
| 160 | + let mut rng = StdRng::seed_from_u64(49853); |
| 161 | + let platform_state = platform.state.load(); |
| 162 | + |
| 163 | + let (identity, signer, key) = |
| 164 | + setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5)); |
| 165 | + |
| 166 | + let (identity_2, _, _) = setup_identity(&mut platform, rng.gen(), dash_to_credits!(0.5)); |
| 167 | + |
| 168 | + let (contract, token_id) = create_token_contract_with_owner_identity( |
| 169 | + &mut platform, |
| 170 | + identity.id(), |
| 171 | + Some(|token_configuration: &mut TokenConfiguration| { |
| 172 | + token_configuration.set_unfreeze_rules(ChangeControlRules::V0( |
| 173 | + ChangeControlRulesV0 { |
| 174 | + authorized_to_make_change: AuthorizedActionTakers::ContractOwner, |
| 175 | + admin_action_takers: AuthorizedActionTakers::NoOne, |
| 176 | + changing_authorized_action_takers_to_no_one_allowed: false, |
| 177 | + changing_admin_action_takers_to_no_one_allowed: false, |
| 178 | + self_changing_admin_action_takers_allowed: false, |
| 179 | + }, |
| 180 | + )); |
| 181 | + }), |
| 182 | + None, |
| 183 | + None, |
| 184 | + None, |
| 185 | + platform_version, |
| 186 | + ); |
| 187 | + |
| 188 | + // Try to unfreeze without being frozen |
| 189 | + let unfreeze_transition = BatchTransition::new_token_unfreeze_transition( |
| 190 | + token_id, |
| 191 | + identity.id(), |
| 192 | + contract.id(), |
| 193 | + 0, |
| 194 | + identity_2.id(), |
| 195 | + None, |
| 196 | + None, |
| 197 | + &key, |
| 198 | + 2, |
| 199 | + 0, |
| 200 | + &signer, |
| 201 | + platform_version, |
| 202 | + None, |
| 203 | + ) |
| 204 | + .expect("expected to create unfreeze transition"); |
| 205 | + |
| 206 | + let serialized = unfreeze_transition |
| 207 | + .serialize_to_bytes() |
| 208 | + .expect("expected to serialize"); |
| 209 | + |
| 210 | + let transaction = platform.drive.grove.start_transaction(); |
| 211 | + |
| 212 | + let processing_result = platform |
| 213 | + .platform |
| 214 | + .process_raw_state_transitions( |
| 215 | + &[serialized], |
| 216 | + &platform_state, |
| 217 | + &BlockInfo::default(), |
| 218 | + &transaction, |
| 219 | + platform_version, |
| 220 | + false, |
| 221 | + None, |
| 222 | + ) |
| 223 | + .expect("expected to process state transition"); |
| 224 | + |
| 225 | + assert_matches!( |
| 226 | + processing_result.execution_results().as_slice(), |
| 227 | + [PaidConsensusError { |
| 228 | + error: ConsensusError::StateError(StateError::IdentityTokenAccountNotFrozenError( |
| 229 | + _ |
| 230 | + )), |
| 231 | + .. |
| 232 | + }] |
| 233 | + ); |
| 234 | + } |
| 235 | +} |
0 commit comments