Skip to content

Commit

Permalink
chore(platform)!: disable credit withdrawals in V1 (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Jul 16, 2024
1 parent 08a8aea commit 1739623
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 87 deletions.
7 changes: 6 additions & 1 deletion packages/rs-dpp/src/errors/consensus/basic/basic_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ use crate::consensus::basic::state_transition::{
InvalidStateTransitionTypeError, MissingStateTransitionTypeError,
StateTransitionMaxSizeExceededError,
};
use crate::consensus::basic::{IncompatibleProtocolVersionError, UnsupportedProtocolVersionError};
use crate::consensus::basic::{
IncompatibleProtocolVersionError, UnsupportedFeatureError, UnsupportedProtocolVersionError,
};
use crate::consensus::ConsensusError;

use crate::consensus::basic::overflow_error::OverflowError;
Expand Down Expand Up @@ -382,6 +384,9 @@ pub enum BasicError {

#[error(transparent)]
OverflowError(OverflowError),

#[error(transparent)]
UnsupportedFeatureError(UnsupportedFeatureError),
}

impl From<BasicError> for ConsensusError {
Expand Down
2 changes: 2 additions & 0 deletions packages/rs-dpp/src/errors/consensus/basic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use basic_error::*;
pub use incompatible_protocol_version_error::*;
pub use unsupported_feature_error::*;
pub use unsupported_protocol_version_error::*;
pub use unsupported_version_error::*;

Expand All @@ -18,5 +19,6 @@ pub mod json_schema_compilation_error;
pub mod json_schema_error;
pub mod overflow_error;
pub mod state_transition;
pub mod unsupported_feature_error;
pub mod unsupported_version_error;
pub mod value_error;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::consensus::basic::BasicError;
use crate::consensus::ConsensusError;
use crate::errors::ProtocolError;
use bincode::{Decode, Encode};
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};

use thiserror::Error;

#[derive(
Error, Debug, Clone, PartialEq, Eq, Encode, Decode, PlatformSerialize, PlatformDeserialize,
)]
#[error("feature {feature_name} is not supported in version {current_protocol_version}")]
#[platform_serialize(unversioned)]
pub struct UnsupportedFeatureError {
/*
DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION
*/
feature_name: String,
current_protocol_version: u32,
}

impl UnsupportedFeatureError {
pub fn new(feature_name: String, current_protocol_version: u32) -> Self {
Self {
feature_name,
current_protocol_version,
}
}

pub fn feature(&self) -> &String {
&self.feature_name
}

pub fn current_protocol_version(&self) -> u32 {
self.current_protocol_version
}
}

impl From<UnsupportedFeatureError> for ConsensusError {
fn from(err: UnsupportedFeatureError) -> Self {
Self::BasicError(BasicError::UnsupportedFeatureError(err))
}
}
5 changes: 3 additions & 2 deletions packages/rs-dpp/src/errors/consensus/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl ErrorWithCode for BasicError {
Self::UnsupportedProtocolVersionError(_) => 10003,
Self::IncompatibleProtocolVersionError(_) => 10004,
Self::VersionError(_) => 10005,
Self::UnsupportedFeatureError(_) => 10006,

// Structure Errors: 10100-10199
#[cfg(feature = "json-schema-validation")]
Expand Down Expand Up @@ -93,8 +94,9 @@ impl ErrorWithCode for BasicError {
Self::UnknownTransferableTypeError { .. } => 10243,
Self::UnknownTradeModeError { .. } => 10244,
Self::UnknownDocumentCreationRestrictionModeError { .. } => 10245,
Self::ContractError(DataContractError::RegexError(_)) => 10247,
Self::IncompatibleDocumentTypeSchemaError { .. } => 10246,
Self::ContractError(DataContractError::RegexError(_)) => 10247,
Self::ContestedUniqueIndexOnMutableDocumentTypeError(_) => 10248,

// Document Errors: 10400-10499
Self::DataContractNotPresentError { .. } => 10400,
Expand Down Expand Up @@ -156,7 +158,6 @@ impl ErrorWithCode for BasicError {

// General Errors 10700-10799
Self::OverflowError(_) => 10700,
Self::ContestedUniqueIndexOnMutableDocumentTypeError(_) => 10701,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::identity::Identity;

#[cfg(feature = "state-transition-signing")]
use crate::identity::core_script::CoreScript;
use crate::identity::IdentityPublicKey;

Check warning on line 15 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/mod.rs:15:5 | 15 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 15 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/mod.rs:15:5 | 15 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#[cfg(feature = "state-transition-signing")]
use crate::prelude::{IdentityNonce, UserFeeIncrease};
#[cfg(feature = "state-transition-signing")]
Expand All @@ -29,6 +30,7 @@ impl IdentityCreditWithdrawalTransitionMethodsV0 for IdentityCreditWithdrawalTra
#[cfg(feature = "state-transition-signing")]
fn try_from_identity<S: Signer>(
identity: &Identity,
withdrawal_key_to_use: Option<&IdentityPublicKey>,
output_script: CoreScript,
amount: u64,
pooling: Pooling,
Expand All @@ -47,6 +49,7 @@ impl IdentityCreditWithdrawalTransitionMethodsV0 for IdentityCreditWithdrawalTra
) {
0 => Ok(IdentityCreditWithdrawalTransitionV0::try_from_identity(
identity,
withdrawal_key_to_use,
output_script,
amount,
pooling,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::identity::core_script::CoreScript;
use crate::identity::signer::Signer;
#[cfg(feature = "state-transition-signing")]
use crate::identity::Identity;
use crate::identity::IdentityPublicKey;

Check warning on line 7 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/v0/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/v0/mod.rs:7:5 | 7 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 7 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/v0/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/methods/v0/mod.rs:7:5 | 7 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
#[cfg(feature = "state-transition-signing")]
use crate::prelude::{IdentityNonce, UserFeeIncrease};
#[cfg(feature = "state-transition-signing")]
Expand All @@ -20,15 +21,16 @@ pub trait IdentityCreditWithdrawalTransitionMethodsV0 {
#[cfg(feature = "state-transition-signing")]
fn try_from_identity<S: Signer>(
identity: &Identity,
withdrawal_key_to_use: Option<&IdentityPublicKey>,
output_script: CoreScript,
amount: u64,
pooling: Pooling,
core_fee_per_byte: u32,
user_fee_increase: UserFeeIncrease,
signer: S,
nonce: IdentityNonce,
_platform_version: &PlatformVersion,
_version: Option<FeatureVersion>,
platform_version: &PlatformVersion,
version: Option<FeatureVersion>,
) -> Result<StateTransition, ProtocolError>;

/// Get State Transition Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ use crate::identity::accessors::IdentityGettersV0;
use crate::identity::core_script::CoreScript;
#[cfg(feature = "state-transition-signing")]
use crate::identity::signer::Signer;
use crate::identity::IdentityPublicKey;

Check warning on line 7 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/v0/v0_methods.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/v0/v0_methods.rs:7:5 | 7 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 7 in packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/v0/v0_methods.rs

View workflow job for this annotation

GitHub Actions / Rust packages (drive-abci) / Linting

unused import: `crate::identity::IdentityPublicKey`

warning: unused import: `crate::identity::IdentityPublicKey` --> packages/rs-dpp/src/state_transition/state_transitions/identity/identity_credit_withdrawal_transition/v0/v0_methods.rs:7:5 | 7 | use crate::identity::IdentityPublicKey; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#[cfg(feature = "state-transition-signing")]
use crate::identity::{Identity, KeyType, Purpose, SecurityLevel};
#[cfg(feature = "state-transition-signing")]
use crate::prelude::{IdentityNonce, UserFeeIncrease};
#[cfg(feature = "state-transition-signing")]
use crate::ProtocolError;
#[cfg(feature = "state-transition-signing")]
use platform_version::version::{FeatureVersion, PlatformVersion};

use crate::state_transition::identity_credit_withdrawal_transition::methods::IdentityCreditWithdrawalTransitionMethodsV0;
use crate::state_transition::identity_credit_withdrawal_transition::v0::IdentityCreditWithdrawalTransitionV0;
#[cfg(feature = "state-transition-signing")]
use crate::state_transition::{GetDataContractSecurityLevelRequirementFn, StateTransition};
#[cfg(feature = "state-transition-signing")]
use crate::withdrawal::Pooling;
#[cfg(feature = "state-transition-signing")]
use crate::ProtocolError;
#[cfg(feature = "state-transition-signing")]
use platform_version::version::{FeatureVersion, PlatformVersion};

impl IdentityCreditWithdrawalTransitionMethodsV0 for IdentityCreditWithdrawalTransitionV0 {
#[cfg(feature = "state-transition-signing")]
fn try_from_identity<S: Signer>(
identity: &Identity,
withdrawal_key_to_use: Option<&IdentityPublicKey>,
output_script: CoreScript,
amount: u64,
pooling: Pooling,
Expand All @@ -47,17 +48,20 @@ impl IdentityCreditWithdrawalTransitionMethodsV0 for IdentityCreditWithdrawalTra
}
.into();

let identity_public_key = identity
.get_first_public_key_matching(
Purpose::TRANSFER,
SecurityLevel::full_range().into(),
KeyType::all_key_types().into(),
)
.ok_or(
ProtocolError::DesiredKeyWithTypePurposeSecurityLevelMissing(
"no withdrawal public key".to_string(),
),
)?;
let identity_public_key = match withdrawal_key_to_use {
Some(key) => key,
None => identity
.get_first_public_key_matching(
Purpose::TRANSFER,
SecurityLevel::full_range().into(),
KeyType::all_key_types().into(),
)
.ok_or_else(|| {
ProtocolError::DesiredKeyWithTypePurposeSecurityLevelMissing(
"no withdrawal public key".to_string(),
)
})?,
};

transition.sign_external(
identity_public_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::rpc::core::CoreRPCLike;

use crate::execution::validation::state_transition::identity_credit_withdrawal::state::v0::IdentityCreditWithdrawalStateTransitionStateValidationV0;
use crate::execution::validation::state_transition::identity_credit_withdrawal::structure::v0::IdentityCreditWithdrawalStateTransitionStructureValidationV0;

use crate::execution::validation::state_transition::identity_credit_withdrawal::structure::v1::IdentityCreditWithdrawalStateTransitionStructureValidationV1;
use crate::execution::validation::state_transition::processor::v0::{
StateTransitionBasicStructureValidationV0, StateTransitionStateValidationV0,
};
Expand Down Expand Up @@ -67,9 +67,10 @@ impl StateTransitionBasicStructureValidationV0 for IdentityCreditWithdrawalTrans
.basic_structure
{
Some(0) => {
// There is nothing expensive here
self.validate_basic_structure_v0()
// Returns not supported
self.validate_basic_structure_v0(platform_version)
}
Some(1) => self.validate_basic_structure_v1(),
Some(version) => Err(Error::Execution(ExecutionError::UnknownVersionMismatch {
method: "identity credit withdrawal transition: validate_basic_structure"
.to_string(),
Expand Down Expand Up @@ -113,3 +114,98 @@ impl StateTransitionStateValidationV0 for IdentityCreditWithdrawalTransition {
}
}
}

#[cfg(test)]
mod tests {
use crate::config::{PlatformConfig, PlatformTestConfig};
use crate::execution::validation::state_transition::tests::{
fast_forward_to_block, setup_identity,
setup_identity_with_withdrawal_key_and_system_credits,
};
use crate::platform_types::state_transitions_processing_result::StateTransitionExecutionResult;
use crate::test::helpers::setup::TestPlatformBuilder;
use assert_matches::assert_matches;
use dpp::block::block_info::BlockInfo;
use dpp::consensus::basic::BasicError;
use dpp::consensus::ConsensusError;
use dpp::dash_to_credits;
use dpp::identity::core_script::CoreScript;
use dpp::serialization::PlatformSerializable;
use dpp::state_transition::identity_credit_withdrawal_transition::methods::IdentityCreditWithdrawalTransitionMethodsV0;
use dpp::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition;
use dpp::withdrawal::Pooling;
use platform_version::version::PlatformVersion;
use rand::prelude::StdRng;
use rand::{Rng, SeedableRng};

#[test]
fn test_identity_credit_withdrawal_is_disabled_on_release() {
let platform_version = PlatformVersion::first();
let platform_config = PlatformConfig {
testing_configs: PlatformTestConfig {
disable_instant_lock_signature_verification: true,
..Default::default()
},
..Default::default()
};

let mut rng = StdRng::seed_from_u64(567);

let mut platform = TestPlatformBuilder::new()
.with_config(platform_config)
.build_with_mock_rpc()
.set_initial_state_structure();

fast_forward_to_block(&platform, 1_200_000_000, 900, 1); //next epoch

let (identity, signer, _, withdrawal_key) =
setup_identity_with_withdrawal_key_and_system_credits(
&mut platform,
rng.gen(),
dash_to_credits!(0.5),
);

let platform_state = platform.state.load();

let withdrawal_amount = dash_to_credits!(0.1);

let credit_withdrawal_transition = IdentityCreditWithdrawalTransition::try_from_identity(
&identity,
Some(&withdrawal_key),
CoreScript::random_p2pkh(&mut rng),
withdrawal_amount,
Pooling::Never,
1,
0,
signer,
2,
platform_version,
None,
)
.expect("expected a credit withdrawal transition");

let credit_withdrawal_transition_serialized_transition = credit_withdrawal_transition
.serialize_to_bytes()
.expect("expected documents batch serialized state transition");

let transaction = platform.drive.grove.start_transaction();

let processing_result = platform
.platform
.process_raw_state_transitions(
&vec![credit_withdrawal_transition_serialized_transition.clone()],
&platform_state,
&BlockInfo::default(),
&transaction,
platform_version,
)
.expect("expected to process state transition");

assert_matches!(
processing_result.execution_results().as_slice(),
[StateTransitionExecutionResult::UnpaidConsensusError(
ConsensusError::BasicError(BasicError::UnsupportedFeatureError(_))
)]
);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod v0;
pub(crate) mod v1;
Loading

0 comments on commit 1739623

Please sign in to comment.