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

refactor: multiplier to version system and tests for refunds #1950

Merged
merged 7 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
864 changes: 15 additions & 849 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ members = [
"packages/simple-signer",
"packages/rs-json-schema-compatibility-validator",
"packages/check-features",
"packages/rs-random-document",
# "packages/rs-random-document",
]

[patch.crates-io]
Expand Down
24 changes: 24 additions & 0 deletions packages/rs-dpp/src/balances/total_credits_balance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::balances::credits::{Creditable, MAX_CREDITS};
use crate::fee::{Credits, SignedCredits};
use crate::ProtocolError;
use std::fmt;

/// The outcome of verifying credits
#[derive(Copy, Clone, Debug)]
Expand All @@ -15,6 +16,29 @@ pub struct TotalCreditsBalance {
pub total_specialized_balances: SignedCredits,
}

impl fmt::Display for TotalCreditsBalance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "TotalCreditsBalance {{")?;
writeln!(
f,
" total_credits_in_platform: {},",
self.total_credits_in_platform
)?;
writeln!(f, " total_in_pools: {},", self.total_in_pools)?;
writeln!(
f,
" total_identity_balances: {},",
self.total_identity_balances
)?;
writeln!(
f,
" total_specialized_balances: {}",
self.total_specialized_balances
)?;
write!(f, "}}")
}
}

impl TotalCreditsBalance {
/// Is the outcome okay? basically do the values match up
/// Errors in case of overflow
Expand Down
17 changes: 16 additions & 1 deletion packages/rs-dpp/src/block/epoch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ProtocolError;
use crate::{InvalidVectorSizeError, ProtocolError};
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -56,3 +56,18 @@ impl TryFrom<EpochIndex> for Epoch {
Self::new(value)
}
}

impl TryFrom<&Vec<u8>> for Epoch {
type Error = ProtocolError;

fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
let key = value.clone().try_into().map_err(|_| {
ProtocolError::InvalidVectorSizeError(InvalidVectorSizeError::new(2, value.len()))
})?;
let index_with_offset = u16::from_be_bytes(key);
let index = index_with_offset
.checked_sub(EPOCH_KEY_OFFSET)
.ok_or(ProtocolError::Overflow("value too low, must have offset"))?;
Ok(Epoch { index, key })
}
}
4 changes: 2 additions & 2 deletions packages/rs-dpp/src/block/extended_epoch_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ impl ExtendedEpochInfoV0Getters for ExtendedEpochInfo {
}
}

fn fee_multiplier(&self) -> f64 {
fn fee_multiplier_permille(&self) -> u64 {
match self {
ExtendedEpochInfo::V0(v0) => v0.fee_multiplier,
ExtendedEpochInfo::V0(v0) => v0.fee_multiplier_permille,
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/rs-dpp/src/block/extended_epoch_info/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct ExtendedEpochInfoV0 {
pub first_block_height: u64,
/// First core block height
pub first_core_block_height: u32,
/// Fee multiplier
pub fee_multiplier: f64,
/// Fee multiplier that you would devise by 1000 to get float value
QuantumExplorer marked this conversation as resolved.
Show resolved Hide resolved
pub fee_multiplier_permille: u64,
/// Protocol version
pub protocol_version: u32,
}
Expand All @@ -34,8 +34,8 @@ pub trait ExtendedEpochInfoV0Getters {
/// Returns the first core block height.
fn first_core_block_height(&self) -> u32;

/// Returns the fee multiplier.
fn fee_multiplier(&self) -> f64;
/// Returns 1000 times the fee multiplier. so 1000 would be 1.
fn fee_multiplier_permille(&self) -> u64;

/// Protocol version
fn protocol_version(&self) -> ProtocolVersion;
Expand Down
1 change: 0 additions & 1 deletion packages/rs-dpp/src/fee/default_costs/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::fee::Credits;

pub const BASE_ST_PROCESSING_FEE: Credits = 10000; // 84000
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
1 change: 1 addition & 0 deletions packages/rs-dpp/src/fee/epoch/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ where
let era_start_epoch_index = start_epoch_index + epochs_per_era * era;

for epoch_index in era_start_epoch_index..era_start_epoch_index + epochs_per_era {
//todo: this can lead to many many calls once we are further along in epochs
map_function(epoch_index, epoch_fee_share)?;

distribution_leftover_credits = distribution_leftover_credits
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/fee/fee_result/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl FeeRefunds {
.map(|(encoded_epoch_index, bytes)| {
let epoch_index : u16 = encoded_epoch_index.try_into().map_err(|_| ProtocolError::Overflow("can't fit u64 epoch index from StorageRemovalPerEpochByIdentifier to u16 EpochIndex"))?;

// TODO We should use multipliers
// TODO Add in multipliers once they have been made

let credits: Credits = (bytes as Credits)
.checked_mul(Epoch::new(current_epoch_index)?.cost_for_known_cost_item(previous_fee_versions, StorageDiskUsageCreditPerByte))
Expand Down
3 changes: 0 additions & 3 deletions packages/rs-dpp/src/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ pub mod epoch;
pub mod fee_result;

pub use crate::balances::credits::{Credits, SignedCredits};

/// Default original fee multiplier
pub const DEFAULT_ORIGINAL_FEE_MULTIPLIER: f64 = 2.0;
4 changes: 2 additions & 2 deletions packages/rs-dpp/src/state_transition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ impl StateTransition {
}

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

/// set a new signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ impl StateTransitionLike for DataContractCreateTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
DataContractCreateTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl StateTransitionLike for DataContractCreateTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl StateTransitionLike for DataContractUpdateTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl StateTransitionLike for DocumentsBatchTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
DocumentsBatchTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl StateTransitionLike for DocumentsBatchTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl StateTransitionLike for IdentityCreateTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
IdentityCreateTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl StateTransitionLike for IdentityCreateTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ impl StateTransitionLike for IdentityCreditTransferTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
IdentityCreditTransferTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl StateTransitionLike for IdentityCreditTransferTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ impl StateTransitionLike for IdentityCreditWithdrawalTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
IdentityCreditWithdrawalTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl StateTransitionLike for IdentityCreditWithdrawalTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl StateTransitionLike for IdentityTopUpTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
IdentityTopUpTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl StateTransitionLike for IdentityTopUpTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl StateTransitionLike for IdentityUpdateTransition {
}
}
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
IdentityUpdateTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl StateTransitionLike for IdentityUpdateTransitionV0 {
self.user_fee_increase
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
self.user_fee_increase = fee_multiplier
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
self.user_fee_increase = user_fee_increase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ impl StateTransitionLike for MasternodeVoteTransition {
}
}

fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease) {
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
match self {
MasternodeVoteTransition::V0(transition) => {
transition.set_user_fee_increase(fee_multiplier)
transition.set_user_fee_increase(user_fee_increase)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait StateTransitionLike:
/// returns the fee multiplier
fn user_fee_increase(&self) -> UserFeeIncrease;
/// set a fee multiplier
fn set_user_fee_increase(&mut self, fee_multiplier: UserFeeIncrease);
fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease);
/// get modified ids list
fn modified_data_ids(&self) -> Vec<Identifier>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,6 @@ where
block_fees_v0.into(),
transaction,
platform_version,
block_execution_context
.block_platform_state()
.previous_fee_versions(),
)?;

tracing::debug!(block_fees = ?processed_block_fees, "block fees are processed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ use std::option::Option::None;

use dpp::block::epoch::Epoch;
use dpp::fee::epoch::{perpetual_storage_epochs, GENESIS_EPOCH_INDEX};
use dpp::fee::DEFAULT_ORIGINAL_FEE_MULTIPLIER;
use dpp::version::PlatformVersion;
use drive::drive::batch::grovedb_op_batch::GroveDbOpBatchV0Methods;
use drive::drive::batch::{DriveOperation, GroveDbOpBatch};
use drive::error;
use drive::grovedb::Transaction;

use crate::error::Error;
Expand Down Expand Up @@ -105,9 +105,16 @@ impl<CoreRPCLike> Platform<CoreRPCLike> {
// init current epoch pool for processing
let current_epoch = Epoch::new(epoch_info.current_epoch_index())?;

let Some(fee_multiplier) = platform_version
.fee_version
.uses_version_fee_multiplier_permille
else {
return Err(Error::Drive(error::drive::DriveError::NotSupported("the fee_multiplier_permille must be set in fees if using add_process_epoch_change_operations_v0").into()));
};

//todo: version
current_epoch.add_init_current_operations(
DEFAULT_ORIGINAL_FEE_MULTIPLIER, // TODO use a data contract to choose the fee multiplier
fee_multiplier, // TODO (feature) use a data contract to choose the fee multiplier
block_info.height(),
block_info.core_chain_locked_height(),
block_info.block_time_ms(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod add_process_epoch_change_operations;
pub mod process_block_fees;

#[cfg(test)]
mod tests;
Loading