Skip to content

Commit

Permalink
feat(platform)!: state transition fee increase and priorities (#1750)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Mar 8, 2024
1 parent 22137c7 commit 8030d3d
Show file tree
Hide file tree
Showing 106 changed files with 841 additions and 71 deletions.
1 change: 1 addition & 0 deletions packages/rs-dpp/src/document/document_factory/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl DocumentFactoryV0 {
Ok(DocumentsBatchTransitionV0 {
owner_id,
transitions,
user_fee_increase: 0,
signature_public_key_id: 0,
signature: Default::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl SpecializedDocumentFactoryV0 {
Ok(DocumentsBatchTransitionV0 {
owner_id,
transitions,
user_fee_increase: 0,
signature_public_key_id: 0,
signature: Default::default(),
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/fee/default_costs/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fee::Credits;

pub const BASE_ST_PROCESSING_FEE: Credits = 10000; // 84000
pub const FEE_MULTIPLIER: Credits = 2;
pub const DEFAULT_SYSTEM_FEE_MULTIPLIER: Credits = 2;
pub const DEFAULT_USER_TIP: Credits = 0;
pub const STORAGE_CREDIT_PER_BYTE: Credits = 5000;
pub const PROCESSING_CREDIT_PER_BYTE: Credits = 12;
Expand Down
16 changes: 16 additions & 0 deletions packages/rs-dpp/src/fee/fee_result/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::consensus::fee::fee_error::FeeError;
use crate::fee::fee_result::refunds::FeeRefunds;
use crate::fee::fee_result::BalanceChange::{AddToBalance, NoBalanceChange, RemoveFromBalance};
use crate::fee::Credits;
use crate::prelude::UserFeeIncrease;
use crate::ProtocolError;
use platform_value::Identifier;
use std::cmp::Ordering;
Expand Down Expand Up @@ -192,6 +193,21 @@ impl FeeResult {
removed_bytes_from_system: 0,
}
}

/// Apply a fee multiplier to a fee result
pub fn apply_user_fee_increase(&mut self, add_fee_percentage_multiplier: UserFeeIncrease) {
let additional_processing_fee = (self.processing_fee as u128)
.saturating_mul(add_fee_percentage_multiplier as u128)
.saturating_div(100);
if additional_processing_fee > u64::MAX as u128 {
self.processing_fee = u64::MAX;
} else {
self.processing_fee = self
.processing_fee
.saturating_add(additional_processing_fee as u64);
}
}

/// Convenience method to get total fee
pub fn total_base_fee(&self) -> Credits {
self.storage_fee + self.processing_fee
Expand Down
4 changes: 4 additions & 0 deletions packages/rs-dpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub mod prelude {
pub type TimestampMillis = u64;
pub type Revision = u64;
pub type IdentityNonce = u64;

/// UserFeeIncrease is the additional percentage of the processing fee.
/// A 1 here means we pay 1% more in processing fees. A 100 means we pay 100% more.
pub type UserFeeIncrease = u16;
}

pub use bincode;
Expand Down
12 changes: 11 additions & 1 deletion packages/rs-dpp/src/state_transition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use crate::identity::state_transition::OptionallyAssetLockProved;
))]
use crate::identity::{IdentityPublicKey, KeyType, Purpose};

Check warning on line 64 in packages/rs-dpp/src/state_transition/mod.rs

View workflow job for this annotation

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

unused import: `Purpose`

warning: unused import: `Purpose` --> packages/rs-dpp/src/state_transition/mod.rs:64:51 | 64 | use crate::identity::{IdentityPublicKey, KeyType, Purpose}; | ^^^^^^^
use crate::identity::{KeyID, SecurityLevel};
use crate::prelude::AssetLockProof;
use crate::prelude::{AssetLockProof, UserFeeIncrease};
pub use state_transitions::*;

use crate::serialization::Signable;
Expand Down Expand Up @@ -315,6 +315,11 @@ impl StateTransition {
call_method!(self, signature)
}

/// returns the fee_increase additional percentage multiplier, it affects only processing costs
pub fn user_fee_increase(&self) -> UserFeeIncrease {
call_method!(self, user_fee_increase)
}

/// The transaction id is a single hash of the data with the signature
pub fn transaction_id(&self) -> Result<[u8; 32], ProtocolError> {
Ok(hash_single(
Expand Down Expand Up @@ -347,6 +352,11 @@ impl StateTransition {
call_method!(self, set_signature, signature)
}

/// set fee multiplier
pub fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
call_method!(self, set_user_fee_increase, fee_multiplier)
}

/// set a new signature
pub fn set_signature_public_key_id(&mut self, public_key_id: KeyID) {
call_method_identity_signed!(self, set_signature_public_key_id, public_key_id)
Expand Down
5 changes: 5 additions & 0 deletions packages/rs-dpp/src/state_transition/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod tests {
let identity_topup_transition = IdentityTopUpTransitionV0 {
asset_lock_proof: AssetLockProof::Instant(asset_lock_proof),
identity_id: identity.id(),
user_fee_increase: 0,
signature: [1u8; 65].to_vec().into(),
};
let state_transition: StateTransition = identity_topup_transition.into();
Expand Down Expand Up @@ -125,6 +126,7 @@ mod tests {
add_public_keys: add_public_keys_in_creation,
disable_public_keys: vec![],
public_keys_disabled_at: None,
user_fee_increase: 0,
};

let key_signable_bytes = identity_update_transition
Expand Down Expand Up @@ -192,6 +194,7 @@ mod tests {
add_public_keys: add_public_keys_in_creation,
disable_public_keys: vec![3, 4, 5],
public_keys_disabled_at: Some(15),
user_fee_increase: 0,
};

let key_signable_bytes = identity_update_transition
Expand Down Expand Up @@ -246,6 +249,7 @@ mod tests {
pooling: Pooling::Standard,
output_script: CoreScript::from_bytes((0..23).collect::<Vec<u8>>()),
nonce: 1,
user_fee_increase: 0,
signature_public_key_id: 0,
signature: [1u8; 65].to_vec().into(),
};
Expand Down Expand Up @@ -296,6 +300,7 @@ mod tests {
.data_contract_owned()
.try_into_platform_versioned(platform_version)
.expect("expected a data contract"),
user_fee_increase: 0,
signature_public_key_id: 0,
signature: [1u8; 65].to_vec().into(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod property_names {
pub const STATE_TRANSITION_PROTOCOL_VERSION: &str = "$version";
pub const IDENTITY_CONTRACT_NONCE: &str = "identityContractNonce";
pub const IDENTITY_NONCE: &str = "identityNonce";

pub const USER_FEE_INCREASE: &str = "userFeeIncrease";
pub const SIGNATURE_PUBLIC_KEY_ID: &str = "signaturePublicKeyId";
pub const SIGNATURE: &str = "signature";
pub const TRANSITION_TYPE: &str = "type";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::UserFeeIncrease;
use crate::state_transition::data_contract_create_transition::DataContractCreateTransition;
use crate::state_transition::{StateTransitionLike, StateTransitionType};
use crate::version::FeatureVersion;
Expand Down Expand Up @@ -35,6 +36,21 @@ impl StateTransitionLike for DataContractCreateTransition {
}
}

/// returns the fee multiplier
fn user_fee_increase(&self) -> UserFeeIncrease {
match self {
DataContractCreateTransition::V0(transition) => transition.user_fee_increase(),
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
match self {
DataContractCreateTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
}
}
}

fn set_signature_bytes(&mut self, signature: Vec<u8>) {
match self {
DataContractCreateTransition::V0(transition) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{data_contract::DataContract, identity::KeyID, ProtocolError};

use crate::data_contract::created_data_contract::CreatedDataContract;
use crate::data_contract::serialized_version::DataContractInSerializationFormat;
use crate::prelude::IdentityNonce;
use crate::prelude::{IdentityNonce, UserFeeIncrease};
use crate::state_transition::data_contract_create_transition::DataContractCreateTransition;
use bincode::{Decode, Encode};
use platform_version::{TryFromPlatformVersioned, TryIntoPlatformVersioned};
Expand All @@ -37,6 +37,7 @@ use crate::version::PlatformVersion;
pub struct DataContractCreateTransitionV0 {
pub data_contract: DataContractInSerializationFormat,
pub identity_nonce: IdentityNonce,
pub user_fee_increase: UserFeeIncrease,
#[platform_signable(exclude_from_sig_hash)]
pub signature_public_key_id: KeyID,
#[platform_signable(exclude_from_sig_hash)]
Expand Down Expand Up @@ -67,6 +68,7 @@ impl TryFromPlatformVersioned<DataContract> for DataContractCreateTransitionV0 {
Ok(DataContractCreateTransitionV0 {
data_contract: value.try_into_platform_versioned(platform_version)?,
identity_nonce: Default::default(),
user_fee_increase: 0,
signature_public_key_id: 0,
signature: Default::default(),
})
Expand All @@ -84,6 +86,7 @@ impl TryFromPlatformVersioned<CreatedDataContract> for DataContractCreateTransit
Ok(DataContractCreateTransitionV0 {
data_contract: data_contract.try_into_platform_versioned(platform_version)?,
identity_nonce,
user_fee_increase: 0,
signature_public_key_id: 0,
signature: Default::default(),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use platform_value::BinaryData;

use crate::prelude::UserFeeIncrease;
use crate::{
prelude::Identifier,
state_transition::{StateTransitionLike, StateTransitionType},
Expand Down Expand Up @@ -48,4 +49,12 @@ impl StateTransitionLike for DataContractCreateTransitionV0 {
self.data_contract.id()
)]
}

fn user_fee_increase(&self) -> UserFeeIncrease {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl DataContractCreateTransitionMethodsV0 for DataContractCreateTransitionV0 {
let transition = DataContractCreateTransition::V0(DataContractCreateTransitionV0 {
data_contract: data_contract.try_into_platform_versioned(platform_version)?,
identity_nonce,
user_fee_increase: 0,
signature_public_key_id: key_id,
signature: Default::default(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::data_contract::conversion::value::v0::DataContractValueConversionMeth
use crate::state_transition::{StateTransitionFieldTypes, StateTransitionValueConvert};
use crate::state_transition::data_contract_create_transition::{DataContractCreateTransitionV0};
use crate::state_transition::data_contract_create_transition::fields::*;
use crate::state_transition::state_transitions::common_fields::property_names::USER_FEE_INCREASE;
use crate::state_transition::state_transitions::contract::data_contract_create_transition::fields::{BINARY_FIELDS, IDENTIFIER_FIELDS, U32_FIELDS};

impl<'a> StateTransitionValueConvert<'a> for DataContractCreateTransitionV0 {
Expand Down Expand Up @@ -41,6 +42,10 @@ impl<'a> StateTransitionValueConvert<'a> for DataContractCreateTransitionV0 {
platform_version,
)?
.try_into_platform_versioned(platform_version)?,
user_fee_increase: raw_object
.get_optional_integer(USER_FEE_INCREASE)
.map_err(ProtocolError::ValueError)?
.unwrap_or_default(),
})
}

Expand Down Expand Up @@ -71,6 +76,10 @@ impl<'a> StateTransitionValueConvert<'a> for DataContractCreateTransitionV0 {
platform_version,
)?
.try_into_platform_versioned(platform_version)?,
user_fee_increase: raw_value_map
.remove_optional_integer(USER_FEE_INCREASE)
.map_err(ProtocolError::ValueError)?
.unwrap_or_default(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::state_transition::StateTransition;
use crate::version::FeatureVersion;
use crate::ProtocolError;

use crate::prelude::IdentityNonce;
use crate::prelude::{IdentityNonce, UserFeeIncrease};
use platform_version::version::PlatformVersion;

impl DataContractUpdateTransitionMethodsV0 for DataContractUpdateTransition {
Expand All @@ -21,6 +21,7 @@ impl DataContractUpdateTransitionMethodsV0 for DataContractUpdateTransition {
identity: &PartialIdentity,
key_id: KeyID,
identity_contract_nonce: IdentityNonce,
user_fee_increase: UserFeeIncrease,
signer: &S,
platform_version: &PlatformVersion,
feature_version: Option<FeatureVersion>,
Expand All @@ -37,6 +38,7 @@ impl DataContractUpdateTransitionMethodsV0 for DataContractUpdateTransition {
identity,
key_id,
identity_contract_nonce,
user_fee_increase,
signer,
platform_version,
feature_version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::data_contract::DataContract;
use crate::identity::signer::Signer;
use crate::identity::{KeyID, PartialIdentity};

use crate::prelude::IdentityNonce;
use crate::prelude::{IdentityNonce, UserFeeIncrease};
use crate::state_transition::StateTransition;
use crate::version::FeatureVersion;
use crate::ProtocolError;
Expand All @@ -27,6 +27,7 @@ pub trait DataContractUpdateTransitionMethodsV0 {
identity: &PartialIdentity,
key_id: KeyID,
identity_contract_nonce: IdentityNonce,
user_fee_increase: UserFeeIncrease,
signer: &S,
platform_version: &PlatformVersion,
feature_version: Option<FeatureVersion>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::prelude::UserFeeIncrease;
use crate::state_transition::data_contract_update_transition::DataContractUpdateTransition;
use crate::state_transition::{StateTransitionLike, StateTransitionType};
use crate::version::FeatureVersion;
Expand Down Expand Up @@ -54,4 +55,19 @@ impl StateTransitionLike for DataContractUpdateTransition {
DataContractUpdateTransition::V0(transition) => transition.unique_identifiers(),
}
}

/// returns the fee increase multiplier
fn user_fee_increase(&self) -> UserFeeIncrease {
match self {
DataContractUpdateTransition::V0(transition) => transition.user_fee_increase(),
}
}
/// set a fee increase multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
DataContractUpdateTransition::V0(transition) => {
transition.set_user_fee_increase(user_fee_increase)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use platform_version::version::PlatformVersion;
use platform_version::{TryFromPlatformVersioned, TryIntoPlatformVersioned};

use crate::data_contract::serialized_version::DataContractInSerializationFormat;
use crate::prelude::IdentityNonce;
use crate::prelude::{IdentityNonce, UserFeeIncrease};
use crate::state_transition::data_contract_update_transition::DataContractUpdateTransition;
use crate::state_transition::StateTransition;
use crate::{data_contract::DataContract, identity::KeyID, ProtocolError};
Expand All @@ -37,6 +37,7 @@ pub struct DataContractUpdateTransitionV0 {
)]
pub identity_contract_nonce: IdentityNonce,
pub data_contract: DataContractInSerializationFormat,
pub user_fee_increase: UserFeeIncrease,
#[platform_signable(exclude_from_sig_hash)]
pub signature_public_key_id: KeyID,
#[platform_signable(exclude_from_sig_hash)]
Expand All @@ -53,6 +54,7 @@ impl TryFromPlatformVersioned<(DataContract, IdentityNonce)> for DataContractUpd
Ok(DataContractUpdateTransitionV0 {
identity_contract_nonce: value.1,
data_contract: value.0.try_into_platform_versioned(platform_version)?,
user_fee_increase: 0,
signature_public_key_id: 0,
signature: Default::default(),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use platform_value::BinaryData;

use crate::prelude::UserFeeIncrease;
use crate::{
prelude::Identifier,
state_transition::{StateTransitionLike, StateTransitionType},
Expand Down Expand Up @@ -49,4 +50,12 @@ impl StateTransitionLike for DataContractUpdateTransitionV0 {
self.identity_contract_nonce
)]
}

fn user_fee_increase(&self) -> UserFeeIncrease {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
}
}
Loading

0 comments on commit 8030d3d

Please sign in to comment.