Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drive): nonce already present in past internal error #2343

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::error::execution::ExecutionError;
use crate::execution::check_tx::CheckTxLevel;
use crate::execution::types::state_transition_execution_context::StateTransitionExecutionContext;
use crate::execution::validation::state_transition::common::asset_lock::proof::verify_is_not_spent::AssetLockProofVerifyIsNotSpent;
use crate::execution::validation::state_transition::processor::v0::{StateTransitionIdentityBalanceValidationV0, StateTransitionBasicStructureValidationV0, StateTransitionNonceValidationV0, StateTransitionIdentityBasedSignatureValidationV0, StateTransitionStructureKnownInStateValidationV0, StateTransitionIsAllowedValidationV0};
use crate::execution::validation::state_transition::processor::v0::{StateTransitionIdentityBalanceValidationV0, StateTransitionBasicStructureValidationV0, StateTransitionNonceValidationV0, StateTransitionIdentityBasedSignatureValidationV0, StateTransitionStructureKnownInStateValidationV0, StateTransitionIsAllowedValidationV0, StateTransitionHasNonceValidationV0};
use crate::execution::validation::state_transition::ValidationMode;

pub(super) fn state_transition_to_execution_event_for_check_tx_v0<'a, C: CoreRPCLike>(
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(super) fn state_transition_to_execution_event_for_check_tx_v0<'a, C: CoreRPC
}

// Only identity top up and identity create do not have nonces validation
if state_transition.has_nonces_validation() {
if state_transition.has_nonce_validation(platform_version)? {
let result = state_transition.validate_nonces(
&platform.into(),
platform.state.last_block_info(),
Expand Down Expand Up @@ -240,7 +240,7 @@ pub(super) fn state_transition_to_execution_event_for_check_tx_v0<'a, C: CoreRPC
)
}
} else {
if state_transition.has_nonces_validation() {
if state_transition.has_nonce_validation(platform_version)? {
let result = state_transition.validate_nonces(
&platform.into(),
platform.state.last_block_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(super) fn process_state_transition_v0<'a, C: CoreRPCLike>(
};

// Only identity top up and identity create do not have nonces validation
if state_transition.has_nonces_validation() {
if state_transition.has_nonce_validation(platform_version)? {
// Validating identity contract nonce, this must happen after validating the signature
let result = state_transition.validate_nonces(
&platform.into(),
Expand Down Expand Up @@ -383,12 +383,11 @@ pub(crate) trait StateTransitionNonceValidationV0 {
execution_context: &mut StateTransitionExecutionContext,
platform_version: &PlatformVersion,
) -> Result<SimpleConsensusValidationResult, Error>;
}

/// True if the state transition validates nonces, either identity nonces or identity contract
/// nonces
fn has_nonces_validation(&self) -> bool {
true
}
pub(crate) trait StateTransitionHasNonceValidationV0 {
/// True if the state transition has nonces validation.
fn has_nonce_validation(&self, platform_version: &PlatformVersion) -> Result<bool, Error>;
}

/// A trait for validating state transitions within a blockchain.
Expand Down Expand Up @@ -607,17 +606,50 @@ impl StateTransitionNonceValidationV0 for StateTransition {
_ => Ok(SimpleConsensusValidationResult::new()),
}
}
}

fn has_nonces_validation(&self) -> bool {
matches!(
self,
StateTransition::DocumentsBatch(_)
| StateTransition::DataContractCreate(_)
| StateTransition::DataContractUpdate(_)
| StateTransition::IdentityUpdate(_)
| StateTransition::IdentityCreditTransfer(_)
| StateTransition::IdentityCreditWithdrawal(_)
)
impl StateTransitionHasNonceValidationV0 for StateTransition {
fn has_nonce_validation(&self, platform_version: &PlatformVersion) -> Result<bool, Error> {
match platform_version
.drive_abci
.validation_and_processing
.has_nonce_validation
{
0 => {
let has_nonce_validation = matches!(
self,
StateTransition::DocumentsBatch(_)
| StateTransition::DataContractCreate(_)
| StateTransition::DataContractUpdate(_)
| StateTransition::IdentityUpdate(_)
| StateTransition::IdentityCreditTransfer(_)
| StateTransition::IdentityCreditWithdrawal(_)
);

Ok(has_nonce_validation)
}
1 => {
// Preferably to use match without wildcard arm (_) to avoid missing cases
// in the future when new state transitions are added
let has_nonce_validation = match self {
StateTransition::DocumentsBatch(_)
| StateTransition::DataContractCreate(_)
| StateTransition::DataContractUpdate(_)
| StateTransition::IdentityUpdate(_)
| StateTransition::IdentityCreditTransfer(_)
| StateTransition::IdentityCreditWithdrawal(_)
| StateTransition::MasternodeVote(_) => true,
StateTransition::IdentityCreate(_) | StateTransition::IdentityTopUp(_) => false,
};

Ok(has_nonce_validation)
}
version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch {
method: "StateTransition::has_nonce_validation".to_string(),
known_versions: vec![0, 1],
received: version,
})),
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pub mod v1;
pub mod v2;
pub mod v3;
pub mod v4;

use versioned_feature_core::{FeatureVersion, OptionalFeatureVersion};

#[derive(Clone, Debug, Default)]
pub struct DriveAbciValidationVersions {
pub state_transitions: DriveAbciStateTransitionValidationVersions,
pub has_nonce_validation: FeatureVersion,
pub process_state_transition: FeatureVersion,
pub state_transition_to_execution_event_for_check_tx: FeatureVersion,
pub penalties: PenaltyAmounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V1: DriveAbciValidationVersions =
document_update_price_transition_state_validation: 0,
},
},
has_nonce_validation: 0,
shumkov marked this conversation as resolved.
Show resolved Hide resolved
process_state_transition: 0,
state_transition_to_execution_event_for_check_tx: 0,
penalties: PenaltyAmounts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V2: DriveAbciValidationVersions =
document_update_price_transition_state_validation: 0,
},
},
has_nonce_validation: 0,
process_state_transition: 0,
state_transition_to_execution_event_for_check_tx: 0,
penalties: PenaltyAmounts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V3: DriveAbciValidationVersions =
document_update_price_transition_state_validation: 0,
},
},
has_nonce_validation: 0,
process_state_transition: 0,
state_transition_to_execution_event_for_check_tx: 0,
penalties: PenaltyAmounts {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
use crate::version::drive_abci_versions::drive_abci_validation_versions::{
DriveAbciAssetLockValidationVersions, DriveAbciDocumentsStateTransitionValidationVersions,
DriveAbciStateTransitionCommonValidationVersions, DriveAbciStateTransitionValidationVersion,
DriveAbciStateTransitionValidationVersions, DriveAbciValidationConstants,
DriveAbciValidationDataTriggerAndBindingVersions, DriveAbciValidationDataTriggerVersions,
DriveAbciValidationVersions, PenaltyAmounts,
};

pub const DRIVE_ABCI_VALIDATION_VERSIONS_V4: DriveAbciValidationVersions =
DriveAbciValidationVersions {
state_transitions: DriveAbciStateTransitionValidationVersions {
common_validation_methods: DriveAbciStateTransitionCommonValidationVersions {
asset_locks: DriveAbciAssetLockValidationVersions {
fetch_asset_lock_transaction_output_sync: 0,
verify_asset_lock_is_not_spent_and_has_enough_balance: 0,
},
validate_identity_public_key_contract_bounds: 0,
validate_identity_public_key_ids_dont_exist_in_state: 0,
validate_identity_public_key_ids_exist_in_state: 0,
validate_state_transition_identity_signed: 0,
validate_unique_identity_public_key_hashes_in_state: 0,
validate_master_key_uniqueness: 0,
validate_simple_pre_check_balance: 0,
},
max_asset_lock_usage_attempts: 16,
identity_create_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: Some(0),
advanced_structure: Some(0),
identity_signatures: Some(0),
advanced_minimum_balance_pre_check: None,
nonce: None,
state: 0,
transform_into_action: 0,
},
identity_update_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: Some(0),
advanced_structure: Some(0),
identity_signatures: Some(0),
advanced_minimum_balance_pre_check: None,
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
identity_top_up_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: Some(0),
advanced_structure: None,
identity_signatures: None,
advanced_minimum_balance_pre_check: None,
nonce: None,
state: 0,
transform_into_action: 0,
},
identity_credit_withdrawal_state_transition:
DriveAbciStateTransitionValidationVersion {
basic_structure: Some(1),
advanced_structure: None,
identity_signatures: None,
advanced_minimum_balance_pre_check: Some(0),
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
identity_credit_withdrawal_state_transition_purpose_matches_requirements: 0,
identity_credit_transfer_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: Some(0),
advanced_structure: None,
identity_signatures: None,
advanced_minimum_balance_pre_check: Some(0),
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
masternode_vote_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: None,
advanced_structure: Some(0),
identity_signatures: None,
advanced_minimum_balance_pre_check: Some(0),
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
contract_create_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: Some(0),
advanced_structure: None,
identity_signatures: None,
advanced_minimum_balance_pre_check: None,
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
contract_update_state_transition: DriveAbciStateTransitionValidationVersion {
basic_structure: None,
advanced_structure: None,
identity_signatures: None,
advanced_minimum_balance_pre_check: None,
nonce: Some(0),
state: 0,
transform_into_action: 0,
},
documents_batch_state_transition: DriveAbciDocumentsStateTransitionValidationVersions {
balance_pre_check: 0,
basic_structure: 0,
advanced_structure: 0,
state: 0,
revision: 0,
transform_into_action: 0,
data_triggers: DriveAbciValidationDataTriggerAndBindingVersions {
bindings: 0,
triggers: DriveAbciValidationDataTriggerVersions {
create_contact_request_data_trigger: 0,
create_domain_data_trigger: 0,
create_identity_data_trigger: 0,
create_feature_flag_data_trigger: 0,
create_masternode_reward_shares_data_trigger: 0,
delete_withdrawal_data_trigger: 0,
reject_data_trigger: 0,
},
},
is_allowed: 0,
document_create_transition_structure_validation: 0,
document_delete_transition_structure_validation: 0,
document_replace_transition_structure_validation: 0,
document_transfer_transition_structure_validation: 0,
document_purchase_transition_structure_validation: 0,
document_update_price_transition_structure_validation: 0,
document_create_transition_state_validation: 1,
document_delete_transition_state_validation: 0,
document_replace_transition_state_validation: 0,
document_transfer_transition_state_validation: 0,
document_purchase_transition_state_validation: 0,
document_update_price_transition_state_validation: 0,
},
},
has_nonce_validation: 1,
process_state_transition: 0,
state_transition_to_execution_event_for_check_tx: 0,
penalties: PenaltyAmounts {
identity_id_not_correct: 50000000,
unique_key_already_present: 10000000,
validation_of_added_keys_structure_failure: 10000000,
validation_of_added_keys_proof_of_possession_failure: 50000000,
},
event_constants: DriveAbciValidationConstants {
maximum_vote_polls_to_process: 2,
maximum_contenders_to_consider: 100,
},
};
6 changes: 3 additions & 3 deletions packages/rs-platform-version/src/version/v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::version::dpp_versions::DPPVersion;
use crate::version::drive_abci_versions::drive_abci_method_versions::v4::DRIVE_ABCI_METHOD_VERSIONS_V4;
use crate::version::drive_abci_versions::drive_abci_query_versions::v1::DRIVE_ABCI_QUERY_VERSIONS_V1;
use crate::version::drive_abci_versions::drive_abci_structure_versions::v1::DRIVE_ABCI_STRUCTURE_VERSIONS_V1;
use crate::version::drive_abci_versions::drive_abci_validation_versions::v3::DRIVE_ABCI_VALIDATION_VERSIONS_V3;
use crate::version::drive_abci_versions::drive_abci_validation_versions::v4::DRIVE_ABCI_VALIDATION_VERSIONS_V4;
use crate::version::drive_abci_versions::drive_abci_withdrawal_constants::v2::DRIVE_ABCI_WITHDRAWAL_CONSTANTS_V2;
use crate::version::drive_abci_versions::DriveAbciVersion;
use crate::version::drive_versions::v2::DRIVE_VERSION_V2;
Expand All @@ -28,14 +28,14 @@ use crate::version::ProtocolVersion;

pub const PROTOCOL_VERSION_6: ProtocolVersion = 6;

/// This version will add multisig support and reduce withdrawal burdens
/// This version adds a fix for nonce validation.
pub const PLATFORM_V6: PlatformVersion = PlatformVersion {
protocol_version: PROTOCOL_VERSION_6,
drive: DRIVE_VERSION_V2,
drive_abci: DriveAbciVersion {
structs: DRIVE_ABCI_STRUCTURE_VERSIONS_V1,
methods: DRIVE_ABCI_METHOD_VERSIONS_V4,
validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V3,
validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V4, // Changed to version 4
withdrawal_constants: DRIVE_ABCI_WITHDRAWAL_CONSTANTS_V2,
query: DRIVE_ABCI_QUERY_VERSIONS_V1,
},
Expand Down
Loading