|
| 1 | +use crate::error::Error; |
| 2 | +use crate::execution::types::execution_operation::ValidationOperation; |
| 3 | +use crate::execution::types::state_transition_execution_context::{ |
| 4 | + StateTransitionExecutionContext, StateTransitionExecutionContextMethodsV0, |
| 5 | +}; |
| 6 | +use crate::execution::validation::state_transition::identity_create::identity_and_signatures::v0::IdentityCreateStateTransitionIdentityAndSignaturesValidationV0; |
| 7 | +use dpp::consensus::basic::invalid_identifier_error::InvalidIdentifierError; |
| 8 | +use dpp::consensus::basic::BasicError; |
| 9 | +use dpp::consensus::ConsensusError; |
| 10 | +use dpp::identity::state_transition::AssetLockProved; |
| 11 | +use dpp::state_transition::identity_create_transition::accessors::IdentityCreateTransitionAccessorsV0; |
| 12 | +use dpp::state_transition::identity_create_transition::IdentityCreateTransition; |
| 13 | +use dpp::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; |
| 14 | +use dpp::validation::ConsensusValidationResult; |
| 15 | +use dpp::version::PlatformVersion; |
| 16 | +use dpp::ProtocolError; |
| 17 | +use drive::state_transition_action::identity::identity_create::IdentityCreateTransitionAction; |
| 18 | +use drive::state_transition_action::system::partially_use_asset_lock_action::PartiallyUseAssetLockAction; |
| 19 | +use drive::state_transition_action::StateTransitionAction; |
| 20 | + |
| 21 | +pub(in crate::execution::validation::state_transition::state_transitions::identity_create) trait IdentityCreateStateTransitionAdvancedStructureValidationV1 |
| 22 | +{ |
| 23 | + fn validate_advanced_structure_from_state_v1( |
| 24 | + &self, |
| 25 | + action: &IdentityCreateTransitionAction, |
| 26 | + signable_bytes: Vec<u8>, |
| 27 | + execution_context: &mut StateTransitionExecutionContext, |
| 28 | + platform_version: &PlatformVersion, |
| 29 | + ) -> Result<ConsensusValidationResult<StateTransitionAction>, Error>; |
| 30 | +} |
| 31 | + |
| 32 | +impl IdentityCreateStateTransitionAdvancedStructureValidationV1 for IdentityCreateTransition { |
| 33 | + /// Like v0, except that an identity id other than the one the asset lock derives and a key |
| 34 | + /// whose proof of possession fails are refused unpaid. |
| 35 | + /// |
| 36 | + /// The asset lock signature covers the transition's signable bytes, which leave out the |
| 37 | + /// identity id and the key signatures, so an identity id that is not the derived one, or a |
| 38 | + /// key signature that does not verify, is in a field the asset lock signature does not |
| 39 | + /// cover. Such a failure is not charged to the asset lock: the transition is refused like |
| 40 | + /// one whose asset lock signature fails, and the asset lock is left as it was. |
| 41 | + /// |
| 42 | + /// The data of a key that carries a proof of possession is signed, yet a key whose data |
| 43 | + /// does not parse fails that proof too, and is refused unpaid like any other proof failure. |
| 44 | + /// Only the holder of the asset lock key can sign such data, and check_tx refuses it. A key |
| 45 | + /// structure failure is still charged, because the keys are part of the signed bytes. |
| 46 | + fn validate_advanced_structure_from_state_v1( |
| 47 | + &self, |
| 48 | + action: &IdentityCreateTransitionAction, |
| 49 | + signable_bytes: Vec<u8>, |
| 50 | + execution_context: &mut StateTransitionExecutionContext, |
| 51 | + platform_version: &PlatformVersion, |
| 52 | + ) -> Result<ConsensusValidationResult<StateTransitionAction>, Error> { |
| 53 | + // The outpoint was already checked when the transition was transformed into an action, |
| 54 | + // so failing to derive the identifier here is not a consensus error |
| 55 | + let identifier_from_outpoint = self.asset_lock_proof().create_identifier()?; |
| 56 | + |
| 57 | + // Deriving the identifier costs one double SHA-256 |
| 58 | + execution_context.add_operation(ValidationOperation::DoubleSha256(1)); |
| 59 | + |
| 60 | + if identifier_from_outpoint != self.identity_id() { |
| 61 | + // No action: the failure is refused unpaid |
| 62 | + return Ok(ConsensusValidationResult::new_with_error( |
| 63 | + ConsensusError::BasicError(BasicError::InvalidIdentifierError( |
| 64 | + InvalidIdentifierError::new( |
| 65 | + "identity_id".to_string(), |
| 66 | + "does not match created identifier from asset lock".to_string(), |
| 67 | + ), |
| 68 | + )), |
| 69 | + )); |
| 70 | + } |
| 71 | + |
| 72 | + let validation_result = |
| 73 | + IdentityPublicKeyInCreation::validate_identity_public_keys_structure( |
| 74 | + self.public_keys(), |
| 75 | + true, |
| 76 | + platform_version, |
| 77 | + ) |
| 78 | + .map_err(Error::Protocol)?; |
| 79 | + |
| 80 | + if !validation_result.is_valid() { |
| 81 | + let penalty = platform_version |
| 82 | + .drive_abci |
| 83 | + .validation_and_processing |
| 84 | + .penalties |
| 85 | + .validation_of_added_keys_structure_failure; |
| 86 | + |
| 87 | + let used_credits = penalty |
| 88 | + .checked_add(execution_context.fee_cost(platform_version)?.processing_fee) |
| 89 | + .ok_or(ProtocolError::Overflow("processing fee overflow error"))?; |
| 90 | + |
| 91 | + let bump_action = StateTransitionAction::PartiallyUseAssetLockAction( |
| 92 | + PartiallyUseAssetLockAction::from_borrowed_identity_create_transition_action( |
| 93 | + action, |
| 94 | + used_credits, |
| 95 | + ), |
| 96 | + ); |
| 97 | + |
| 98 | + return Ok(ConsensusValidationResult::new_with_data_and_errors( |
| 99 | + bump_action, |
| 100 | + validation_result.errors, |
| 101 | + )); |
| 102 | + } |
| 103 | + |
| 104 | + let validation_result = self.validate_identity_create_state_transition_signatures_v0( |
| 105 | + signable_bytes, |
| 106 | + execution_context, |
| 107 | + ); |
| 108 | + |
| 109 | + if validation_result.is_valid() { |
| 110 | + Ok(ConsensusValidationResult::new()) |
| 111 | + } else { |
| 112 | + // No action: the failure is refused unpaid |
| 113 | + Ok(ConsensusValidationResult::new_with_errors( |
| 114 | + validation_result.errors, |
| 115 | + )) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments