diff --git a/packages/rs-dpp/src/fee/default_costs/mod.rs b/packages/rs-dpp/src/fee/default_costs/mod.rs index 2d079e76564..698bb03774b 100644 --- a/packages/rs-dpp/src/fee/default_costs/mod.rs +++ b/packages/rs-dpp/src/fee/default_costs/mod.rs @@ -75,6 +75,7 @@ pub enum KnownCostItem { } impl KnownCostItem { + #[inline] pub fn lookup_cost(&self, fee_version: &FeeVersion) -> Credits { match self { KnownCostItem::StorageDiskUsageCreditPerByte => { diff --git a/packages/rs-dpp/src/fee/fee_result/refunds.rs b/packages/rs-dpp/src/fee/fee_result/refunds.rs index fa41f0ae559..ca5e8192be9 100644 --- a/packages/rs-dpp/src/fee/fee_result/refunds.rs +++ b/packages/rs-dpp/src/fee/fee_result/refunds.rs @@ -39,7 +39,7 @@ impl FeeRefunds { storage_removal: I, current_epoch_index: EpochIndex, epochs_per_era: u16, - cached_fee_version: &CachedEpochIndexFeeVersions, + cached_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result where I: IntoIterator, @@ -58,7 +58,7 @@ impl FeeRefunds { // TODO We should use multipliers let credits: Credits = (bytes as Credits) - .checked_mul(Epoch::new(current_epoch_index)?.cost_for_known_cost_item(cached_fee_version, StorageDiskUsageCreditPerByte)) + .checked_mul(Epoch::new(current_epoch_index)?.cost_for_known_cost_item(cached_fee_versions, StorageDiskUsageCreditPerByte)) .ok_or(ProtocolError::Overflow("storage written bytes cost overflow"))?; let (amount, _) = calculate_storage_fee_refund_amount_and_leftovers( diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/add_process_epoch_change_operations/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/add_process_epoch_change_operations/v0/mod.rs index 3e34b5c5822..9c1de256468 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/add_process_epoch_change_operations/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/add_process_epoch_change_operations/v0/mod.rs @@ -171,6 +171,7 @@ mod tests { use crate::platform_types::platform_state::PlatformState; use dpp::block::block_info::BlockInfo; use dpp::fee::epoch::CreditsPerEpoch; + use dpp::prelude::CachedEpochIndexFeeVersions; use drive::drive::defaults::INITIAL_PROTOCOL_VERSION; /// Process and validate an epoch change @@ -211,6 +212,7 @@ mod tests { &BlockInfo::default(), Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); } @@ -285,6 +287,7 @@ mod tests { &BlockInfo::default(), Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/process_block_fees/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/process_block_fees/v0/mod.rs index d6df6c83012..373e97a1c61 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/process_block_fees/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_fee_processing/process_block_fees/v0/mod.rs @@ -36,6 +36,7 @@ use std::option::Option::None; use dpp::block::epoch::Epoch; +use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; use drive::drive::batch::DriveOperation; use drive::drive::Drive; @@ -180,6 +181,7 @@ impl Platform { &block_info.to_block_info(epoch_info.try_into()?), Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), )?; let outcome = processed_block_fees_outcome::v0::ProcessedBlockFeesOutcome { diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs index 938804b9a44..a44cb619102 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs @@ -16,6 +16,7 @@ use drive::drive::batch::IdentityOperationType::AddNewIdentity; use drive::grovedb::Transaction; use std::collections::BTreeMap; use tracing::Level; +use dpp::prelude::CachedEpochIndexFeeVersions; impl Platform where @@ -140,6 +141,7 @@ where block_info, Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), )?; let height = block_info.height; diff --git a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_block_fees_into_pools_operations/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_block_fees_into_pools_operations/v0/mod.rs index e1641df9af8..239cfde5e71 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_block_fees_into_pools_operations/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_block_fees_into_pools_operations/v0/mod.rs @@ -75,6 +75,7 @@ impl Platform { mod tests { use super::*; use dpp::block::block_info::BlockInfo; + use dpp::prelude::CachedEpochIndexFeeVersions; use drive::drive::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use crate::test::helpers::setup::TestPlatformBuilder; @@ -126,6 +127,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); @@ -194,6 +196,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_storage_fee_to_epochs_operations/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_storage_fee_to_epochs_operations/v0/mod.rs index 0085d637088..d56f6d2b10a 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_storage_fee_to_epochs_operations/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_inwards_distribution/add_distribute_storage_fee_to_epochs_operations/v0/mod.rs @@ -84,6 +84,7 @@ mod tests { perpetual_storage_epochs, CreditsPerEpoch, SignedCreditsPerEpoch, GENESIS_EPOCH_INDEX, }; use dpp::fee::Credits; + use dpp::prelude::CachedEpochIndexFeeVersions; use drive::drive::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use drive::drive::batch::DriveOperation; use drive::drive::config::DriveConfig; @@ -196,6 +197,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_distribute_fees_from_oldest_unpaid_epoch_pool_to_proposers_operations/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_distribute_fees_from_oldest_unpaid_epoch_pool_to_proposers_operations/v0/mod.rs index d56373613f3..7d2e1dba6dc 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_distribute_fees_from_oldest_unpaid_epoch_pool_to_proposers_operations/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_distribute_fees_from_oldest_unpaid_epoch_pool_to_proposers_operations/v0/mod.rs @@ -91,6 +91,7 @@ impl Platform { mod tests { use super::*; use dpp::block::block_info::BlockInfo; + use dpp::prelude::CachedEpochIndexFeeVersions; use drive::common::test_utils::identities::create_test_masternode_identities_and_add_them_as_epoch_block_proposers; @@ -203,6 +204,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_epoch_pool_to_proposers_payout_operations/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_epoch_pool_to_proposers_payout_operations/v0/mod.rs index 670f7dc1fe9..cfc195c8772 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_epoch_pool_to_proposers_payout_operations/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/fee_pool_outwards_distribution/add_epoch_pool_to_proposers_payout_operations/v0/mod.rs @@ -152,6 +152,7 @@ impl Platform { &BlockInfo::default(), Some(transaction), platform_version, + &None, )?; batch.push(DriveOperation::GroveDBOpBatch(operations)); @@ -180,6 +181,7 @@ mod tests { use drive::fee_pools::epochs::operations_factory::EpochOperations; use rust_decimal::Decimal; use rust_decimal_macros::dec; + use dpp::prelude::CachedEpochIndexFeeVersions; #[test] fn test_payout_to_proposers() { @@ -281,6 +283,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs index 73795a0aab2..005a539f154 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v0/mod.rs @@ -55,6 +55,7 @@ use drive::drive::object_size_info::{ use drive::query::TransactionArg; use std::borrow::Cow; use std::collections::BTreeMap; +use dpp::prelude::CachedEpochIndexFeeVersions; const DPNS_DASH_TLD_DOCUMENT_ID: [u8; 32] = [ 215, 242, 197, 63, 70, 169, 23, 171, 110, 91, 57, 162, 215, 188, 38, 11, 100, 146, 137, 69, 55, @@ -189,6 +190,7 @@ impl Platform { &block_info, transaction, platform_version, + &CachedEpochIndexFeeVersions::default(), )?; Ok(()) diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs index 99952972dd2..c30deeba244 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs @@ -8,6 +8,7 @@ use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; use dpp::block::block_info::BlockInfo; use dpp::consensus::ConsensusError; +use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; use drive::grovedb::Transaction; @@ -42,6 +43,7 @@ where block_info: &BlockInfo, transaction: &Transaction, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result { match platform_version .drive_abci @@ -55,6 +57,7 @@ where block_info, transaction, platform_version, + previous_fee_versions, ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "execute_event".to_string(), diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v0/mod.rs index e7f03575d9e..dd962308661 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v0/mod.rs @@ -11,6 +11,7 @@ use crate::rpc::core::CoreRPCLike; use dpp::block::block_info::BlockInfo; use dpp::consensus::ConsensusError; use dpp::fee::fee_result::FeeResult; +use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; use drive::drive::identity::update::apply_balance_change_outcome::ApplyBalanceChangeOutcomeV0Methods; use drive::grovedb::Transaction; @@ -48,6 +49,7 @@ where block_info: &BlockInfo, transaction: &Transaction, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result { let maybe_fee_validation_result = match event { ExecutionEvent::PaidFromAssetLock { .. } | ExecutionEvent::Paid { .. } => { @@ -89,6 +91,7 @@ where block_info, Some(transaction), platform_version, + previous_fee_versions, ) .map_err(Error::Drive)?; @@ -138,6 +141,7 @@ where block_info, Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .map_err(Error::Drive)?; @@ -162,6 +166,7 @@ where block_info, Some(transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .map_err(Error::Drive)?; Ok(SuccessfulFreeExecution) diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/process_raw_state_transitions/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/process_raw_state_transitions/v0/mod.rs index a9ba48da712..2ab9e501d6a 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/process_raw_state_transitions/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/process_raw_state_transitions/v0/mod.rs @@ -8,6 +8,7 @@ use dpp::fee::fee_result::FeeResult; use dpp::util::hash::hash_single; use dpp::validation::ConsensusValidationResult; use std::time::Instant; +use dpp::prelude::CachedEpochIndexFeeVersions; use crate::execution::types::execution_event::ExecutionEvent; use crate::execution::types::state_transition_container::v0::{ @@ -70,6 +71,8 @@ where config: &self.config, core_rpc: &self.core_rpc, }; + + let previous_fee_versions = block_platform_state.previous_fee_versions(); let state_transition_container = self.decode_raw_state_transitions(raw_state_transitions, platform_version)?; @@ -115,6 +118,7 @@ where block_info, transaction, platform_version, + previous_fee_versions, ) .unwrap_or_else(error_to_internal_error_execution_result) }) @@ -199,6 +203,7 @@ where block_info: &BlockInfo, transaction: &Transaction, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result> { // State Transition is invalid if !validation_result.is_valid() { @@ -252,6 +257,7 @@ where block_info, transaction, platform_version, + previous_fee_versions, ) .map_err(|error| StateTransitionAwareError { error, @@ -343,6 +349,7 @@ where block_info, transaction, platform_version, + previous_fee_versions, ) .map_err(|error| StateTransitionAwareError { error, diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/validate_fees_of_event/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/validate_fees_of_event/v0/mod.rs index 86b6dd890b8..576f2dbb274 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/validate_fees_of_event/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/validate_fees_of_event/v0/mod.rs @@ -9,7 +9,7 @@ use dpp::consensus::state::identity::IdentityInsufficientBalanceError; use dpp::consensus::state::state_error::StateError; use dpp::fee::fee_result::FeeResult; -use dpp::prelude::ConsensusValidationResult; +use dpp::prelude::{CachedEpochIndexFeeVersions, ConsensusValidationResult}; use dpp::version::PlatformVersion; use drive::grovedb::TransactionArg; @@ -62,6 +62,7 @@ where block_info, transaction, platform_version, + &CachedEpochIndexFeeVersions::default(), ) .map_err(Error::Drive)?; @@ -113,6 +114,7 @@ where block_info, transaction, platform_version, + &CachedEpochIndexFeeVersions::default(), ) .map_err(Error::Drive)?; diff --git a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/dequeue_and_build_unsigned_withdrawal_transactions/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/dequeue_and_build_unsigned_withdrawal_transactions/v0/mod.rs index 9659528dc2b..15865bbf7c8 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/dequeue_and_build_unsigned_withdrawal_transactions/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/dequeue_and_build_unsigned_withdrawal_transactions/v0/mod.rs @@ -25,6 +25,7 @@ use crate::{ rpc::core::CoreRPCLike, }; use dpp::errors::ProtocolError; +use dpp::prelude::CachedEpochIndexFeeVersions; use drive::drive::config::DEFAULT_QUERY_LIMIT; const WITHDRAWAL_TRANSACTIONS_QUERY_LIMIT: u16 = 16; @@ -108,6 +109,7 @@ where block_info, transaction, platform_version, + &CachedEpochIndexFeeVersions::default(), )?; Ok(UnsignedWithdrawalTxs::from_vec( diff --git a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/pool_withdrawals_into_transactions_queue/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/pool_withdrawals_into_transactions_queue/v0/mod.rs index d65b4138f5d..c4f6b9d4ca7 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/pool_withdrawals_into_transactions_queue/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/pool_withdrawals_into_transactions_queue/v0/mod.rs @@ -3,6 +3,7 @@ use dpp::block::block_info::BlockInfo; use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::document::document_methods::DocumentMethodsV0; use dpp::document::{DocumentV0Getters, DocumentV0Setters}; +use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; use drive::drive::identity::withdrawals::WithdrawalTransactionIndexAndBytes; @@ -131,6 +132,7 @@ where block_info, transaction, platform_version, + &CachedEpochIndexFeeVersions::default(), )?; Ok(()) diff --git a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/update_broadcasted_withdrawal_statuses/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/update_broadcasted_withdrawal_statuses/v0/mod.rs index 97b5a922ba9..3fed4d18bba 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/withdrawals/update_broadcasted_withdrawal_statuses/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/withdrawals/update_broadcasted_withdrawal_statuses/v0/mod.rs @@ -9,6 +9,7 @@ use dpp::system_data_contracts::withdrawals_contract::v1::document_types::withdr use dpp::version::PlatformVersion; use itertools::Itertools; use std::collections::HashSet; +use dpp::prelude::CachedEpochIndexFeeVersions; use drive::drive::batch::DriveOperation; use drive::drive::config::DEFAULT_QUERY_LIMIT; @@ -155,6 +156,7 @@ where block_info, transaction.into(), platform_version, + &CachedEpochIndexFeeVersions::default(), )?; Ok(()) diff --git a/packages/rs-drive-abci/src/query/mod.rs b/packages/rs-drive-abci/src/query/mod.rs index c27ec51c7df..8a3c6c8d4c4 100644 --- a/packages/rs-drive-abci/src/query/mod.rs +++ b/packages/rs-drive-abci/src/query/mod.rs @@ -30,6 +30,7 @@ mod tests { use platform_version::version::PlatformVersion; use std::borrow::Cow; use std::sync::Arc; + use dpp::prelude::CachedEpochIndexFeeVersions; pub fn setup_platform<'a>( with_genesis_state: bool, @@ -71,7 +72,7 @@ mod tests { platform .drive - .apply_drive_operations(vec![operation], true, &block_info, None, platform_version) + .apply_drive_operations(vec![operation], true, &block_info, None, platform_version, &CachedEpochIndexFeeVersions::default()) .expect("expected to apply drive operations"); } diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/document.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/document.rs index 99f5cb53889..e77005d6d40 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/document.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/document.rs @@ -12,7 +12,7 @@ use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::DocumentTypeRef; use dpp::data_contract::DataContract; use dpp::document::Document; -use dpp::prelude::Identifier; +use dpp::prelude::{CachedEpochIndexFeeVersions, Identifier}; use dpp::system_data_contracts::withdrawals_contract::v1::document_types::withdrawal; @@ -102,6 +102,7 @@ impl DriveLowLevelOperationConverter for DocumentOperationType<'_> { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result, Error> { match self { DocumentOperationType::AddDocument { diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs index b4040eb7b01..64633194bbd 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs @@ -10,6 +10,7 @@ use dpp::block::block_info::BlockInfo; use grovedb::TransactionArg; use dpp::fee::fee_result::FeeResult; +use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; @@ -39,6 +40,7 @@ impl Drive { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result { match platform_version .drive @@ -52,6 +54,7 @@ impl Drive { block_info, transaction, platform_version, + previous_fee_versions, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "apply_drive_operations".to_string(), diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/v0/mod.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/v0/mod.rs index 033eee50f2f..0d70be08d64 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/apply_drive_operations/v0/mod.rs @@ -17,6 +17,7 @@ use crate::drive::batch::drive_op_batch::finalize_task::{ DriveOperationFinalizationTasks, DriveOperationFinalizeTask, }; use std::collections::HashMap; +use dpp::prelude::CachedEpochIndexFeeVersions; impl Drive { /// Applies a list of high level DriveOperations to the drive, and calculates the fee for them. @@ -43,6 +44,7 @@ impl Drive { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &CachedEpochIndexFeeVersions, ) -> Result { if operations.is_empty() { return Ok(FeeResult::default()); @@ -67,6 +69,7 @@ impl Drive { block_info, transaction, platform_version, + Some(previous_fee_versions), )?); } @@ -84,15 +87,14 @@ impl Drive { for task in finalize_tasks { task.execute(self, platform_version); } - - let cached_fee_versions = self.cache.cached_fee_version.read(); + Drive::calculate_fee( None, Some(cost_operations), &block_info.epoch, self.config.epochs_per_era, platform_version, - &cached_fee_versions, + previous_fee_versions, ) } } diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/mod.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/mod.rs index d3346034dd1..9aa4f611fb1 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/mod.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/mod.rs @@ -9,6 +9,7 @@ use dpp::block::block_info::BlockInfo; use dpp::version::PlatformVersion; use grovedb::TransactionArg; +use dpp::prelude::CachedEpochIndexFeeVersions; impl Drive { /// Convert a batch of drive operations to a batch of grove database operations. @@ -35,6 +36,7 @@ impl Drive { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &Option, ) -> Result { match platform_version .drive @@ -47,6 +49,7 @@ impl Drive { block_info, transaction, platform_version, + previous_fee_versions, ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "convert_drive_operations_to_grove_operations".to_string(), diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/v0/mod.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/v0/mod.rs index fa50fcc4603..5c60d5b354e 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/drive_methods/convert_drive_operations_to_grove_operations/v0/mod.rs @@ -10,6 +10,7 @@ use dpp::version::PlatformVersion; use grovedb::batch::GroveDbOp; use grovedb::TransactionArg; use itertools::Itertools; +use dpp::prelude::CachedEpochIndexFeeVersions; impl Drive { /// Convert a batch of drive operations to a batch of grove database operations. @@ -36,6 +37,7 @@ impl Drive { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &Option, ) -> Result { let ops = drive_batch_operations .into_iter() @@ -46,6 +48,7 @@ impl Drive { block_info, transaction, platform_version, + previous_fee_versions, )?; Ok(LowLevelDriveOperation::grovedb_operations_consume( inner_drive_operations, diff --git a/packages/rs-drive/src/drive/batch/drive_op_batch/mod.rs b/packages/rs-drive/src/drive/batch/drive_op_batch/mod.rs index a7095f2b6f3..9b56cb3c31e 100644 --- a/packages/rs-drive/src/drive/batch/drive_op_batch/mod.rs +++ b/packages/rs-drive/src/drive/batch/drive_op_batch/mod.rs @@ -34,6 +34,7 @@ use crate::drive::batch::drive_op_batch::finalize_task::{ }; use crate::error::drive::DriveError; use std::collections::{BTreeMap, HashMap}; +use dpp::prelude::CachedEpochIndexFeeVersions; /// A converter that will get Drive Operations from High Level Operations pub trait DriveLowLevelOperationConverter { @@ -47,6 +48,7 @@ pub trait DriveLowLevelOperationConverter { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: &Option, ) -> Result, Error>; } @@ -88,6 +90,7 @@ impl DriveLowLevelOperationConverter for DriveOperation<'_> { block_info: &BlockInfo, transaction: TransactionArg, platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, ) -> Result, Error> { match self { DriveOperation::DataContractOperation(contract_operation_type) => { @@ -271,6 +274,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to insert contract and document"); @@ -389,6 +393,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to insert documents"); @@ -500,6 +505,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to insert documents"); @@ -630,6 +636,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to insert documents"); @@ -738,6 +745,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to insert documents"); @@ -798,6 +806,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to update documents"); @@ -951,6 +960,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to insert documents"); @@ -1004,6 +1014,7 @@ mod tests { &BlockInfo::default(), Some(&db_transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("expected to be able to update documents"); diff --git a/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/add_delete_pending_epoch_refunds_except_specified/v0/mod.rs b/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/add_delete_pending_epoch_refunds_except_specified/v0/mod.rs index 410a89920e3..df3c849d1fe 100644 --- a/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/add_delete_pending_epoch_refunds_except_specified/v0/mod.rs +++ b/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/add_delete_pending_epoch_refunds_except_specified/v0/mod.rs @@ -68,6 +68,7 @@ mod tests { use dpp::version::PlatformVersion; use grovedb::batch::Op; + use dpp::prelude::CachedEpochIndexFeeVersions; #[test] fn should_add_delete_operations_v0() { @@ -97,6 +98,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/fetch_and_add_pending_epoch_refunds_to_collection/v0/mod.rs b/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/fetch_and_add_pending_epoch_refunds_to_collection/v0/mod.rs index be751238ed6..b054e1e731d 100644 --- a/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/fetch_and_add_pending_epoch_refunds_to_collection/v0/mod.rs +++ b/packages/rs-drive/src/drive/credit_pools/pending_epoch_refunds/methods/fetch_and_add_pending_epoch_refunds_to_collection/v0/mod.rs @@ -79,6 +79,7 @@ mod tests { use crate::tests::helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; use dpp::fee::epoch::CreditsPerEpoch; + use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; @@ -109,6 +110,7 @@ mod tests { &BlockInfo::default(), Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("should apply batch"); diff --git a/packages/rs-drive/src/drive/fee/calculate_fee/v0/mod.rs b/packages/rs-drive/src/drive/fee/calculate_fee/v0/mod.rs index 928d49d5a1a..badf9284f17 100644 --- a/packages/rs-drive/src/drive/fee/calculate_fee/v0/mod.rs +++ b/packages/rs-drive/src/drive/fee/calculate_fee/v0/mod.rs @@ -37,6 +37,7 @@ impl Drive { drive_operations, epoch, epochs_per_era, + , cached_fee_version, )? { aggregate_fee_result.checked_add_assign(drive_fee_result)?; diff --git a/packages/rs-drive/src/drive/identity/withdrawals/transaction/index/mod.rs b/packages/rs-drive/src/drive/identity/withdrawals/transaction/index/mod.rs index d1882c72d79..2a10a1e1e6b 100644 --- a/packages/rs-drive/src/drive/identity/withdrawals/transaction/index/mod.rs +++ b/packages/rs-drive/src/drive/identity/withdrawals/transaction/index/mod.rs @@ -7,6 +7,7 @@ pub mod fetch_next_withdrawal_transaction_index; mod tests { use dpp::block::block_info::BlockInfo; use dpp::block::epoch::Epoch; + use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; use crate::tests::helpers::setup::setup_drive_with_initial_state_structure; @@ -45,6 +46,7 @@ mod tests { &block_info, Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("to apply drive ops"); diff --git a/packages/rs-drive/src/drive/identity/withdrawals/transaction/queue/mod.rs b/packages/rs-drive/src/drive/identity/withdrawals/transaction/queue/mod.rs index b404dc90883..12958ced8bc 100644 --- a/packages/rs-drive/src/drive/identity/withdrawals/transaction/queue/mod.rs +++ b/packages/rs-drive/src/drive/identity/withdrawals/transaction/queue/mod.rs @@ -14,6 +14,7 @@ mod tests { }; use dpp::block::block_info::BlockInfo; use dpp::block::epoch::Epoch; + use dpp::prelude::CachedEpochIndexFeeVersions; use dpp::version::PlatformVersion; #[test] @@ -52,6 +53,7 @@ mod tests { &block_info, Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("to apply batch"); @@ -73,6 +75,7 @@ mod tests { &block_info, Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("to apply batch"); @@ -96,6 +99,7 @@ mod tests { &block_info, Some(&transaction), platform_version, + &CachedEpochIndexFeeVersions::default(), ) .expect("to apply batch"); diff --git a/packages/rs-drive/src/fee/op.rs b/packages/rs-drive/src/fee/op.rs index 2487011d87d..43cb5133a8a 100644 --- a/packages/rs-drive/src/fee/op.rs +++ b/packages/rs-drive/src/fee/op.rs @@ -21,6 +21,7 @@ use crate::fee::op::LowLevelDriveOperation::{ CalculatedCostOperation, FunctionOperation, GroveOperation, PreCalculatedFeeResult, }; use dpp::block::epoch::Epoch; +use dpp::fee::Credits; use dpp::fee::default_costs::EpochCosts; use dpp::fee::default_costs::KnownCostItem::{ StorageDiskUsageCreditPerByte, StorageLoadCreditPerByte, StorageProcessingCreditPerByte, @@ -29,6 +30,8 @@ use dpp::fee::default_costs::KnownCostItem::{ use dpp::fee::fee_result::refunds::FeeRefunds; use dpp::fee::fee_result::FeeResult; use dpp::prelude::CachedEpochIndexFeeVersions; +use platform_version::version::fee::FeeVersion; +use crate::error::fee::FeeError; /// Base ops #[derive(Debug, Enum)] @@ -141,13 +144,23 @@ impl HashFunction { } } - //todo: put real costs in - fn base_cost(&self, _epoch: &Epoch) -> u64 { + fn block_cost(&self, fee_version: &FeeVersion) -> u64 { match self { - HashFunction::Sha256 => 30, - HashFunction::Sha256_2 => 30, - HashFunction::Blake3 => 30, - HashFunction::Sha256RipeMD160 => 30, + HashFunction::Sha256 => fee_version.hashing.sha256_per_block, + HashFunction::Sha256_2 => fee_version.hashing.sha256_per_block, + HashFunction::Blake3 => fee_version.hashing.blake3_per_block, + HashFunction::Sha256RipeMD160 => fee_version.hashing.sha256_per_block, + } + } + + fn base_cost(&self, fee_version: &FeeVersion) -> u64 { + match self { + HashFunction::Sha256 => fee_version.hashing.single_sha256_base, + // It's normal that the base cost for a sha256 will have a single sha256 base + // But it has an extra block + HashFunction::Sha256_2 => fee_version.hashing.single_sha256_base, + HashFunction::Blake3 => fee_version.hashing.blake3_base, + HashFunction::Sha256RipeMD160 => fee_version.hashing.sha256_ripe_md160_base, } } } @@ -163,8 +176,9 @@ pub struct FunctionOp { impl FunctionOp { /// The cost of the function - fn cost(&self, epoch: &Epoch) -> u64 { - self.rounds as u64 * self.hash.base_cost(epoch) + fn cost(&self, fee_version: &FeeVersion) -> Credits { + let block_cost = (self.rounds as u64).saturating_mul(self.hash.block_cost(fee_version)); + self.hash.base_cost(fee_version).saturating_add(block_cost) } /// Create a new function operation with the following hash knowing the rounds it will take @@ -204,20 +218,23 @@ impl LowLevelDriveOperation { drive_operation: Vec, epoch: &Epoch, epochs_per_era: u16, - cached_fee_version: &CachedEpochIndexFeeVersions, + fee_version: &FeeVersion, + cached_fee_versions: &Option, ) -> Result, Error> { drive_operation .into_iter() .map(|operation| match operation { PreCalculatedFeeResult(f) => Ok(f), FunctionOperation(op) => Ok(FeeResult { - processing_fee: op.cost(epoch), + processing_fee: op.cost(fee_version), ..Default::default() }), _ => { let cost = operation.operation_cost()?; - let storage_fee = cost.storage_cost(epoch, cached_fee_version)?; - let processing_fee = cost.ephemeral_cost(epoch, cached_fee_version)?; + // There is no need for a checked multiply here because added bytes are u64 and + // storage disk usage credit per byte should never be high enough to cause an overflow + let storage_fee = cost.storage_cost.added_bytes as u64 * fee_version.storage.storage_disk_usage_credit_per_byte; + let processing_fee = cost.ephemeral_cost(fee_version)?; let (fee_refunds, removed_bytes_from_system) = match cost.storage_cost.removed_bytes { NoStorageRemoval => (FeeRefunds::default(), 0), @@ -226,6 +243,7 @@ impl LowLevelDriveOperation { (FeeRefunds::default(), amount) } SectionedStorageRemoval(mut removal_per_epoch_by_identifier) => { + let cached_fee_versions = cached_fee_versions.as_ref().ok_or(Error::Drive(DriveError::CorruptedCodeExecution("expected previous epoch index fee versions to be able to offer refunds")))?; let system_amount = removal_per_epoch_by_identifier .remove(&Identifier::default()) .map_or(0, |a| a.values().sum()); @@ -235,7 +253,7 @@ impl LowLevelDriveOperation { removal_per_epoch_by_identifier, epoch.index, epochs_per_era, - cached_fee_version, + cached_fee_versions, )?, system_amount, ) @@ -410,14 +428,7 @@ pub trait DriveCost { /// Ephemeral cost fn ephemeral_cost( &self, - epoch: &Epoch, - cached_fee_version: &CachedEpochIndexFeeVersions, - ) -> Result; - /// Storage cost - fn storage_cost( - &self, - epoch: &Epoch, - cached_fee_version: &CachedEpochIndexFeeVersions, + fee_version: &FeeVersion, ) -> Result; } @@ -425,20 +436,17 @@ impl DriveCost for OperationCost { /// Return the ephemeral cost from the operation fn ephemeral_cost( &self, - epoch: &Epoch, - cached_fee_version: &CachedEpochIndexFeeVersions, - ) -> Result { - //todo: deal with epochs + fee_version: &FeeVersion, + ) -> Result { let OperationCost { seek_count, storage_cost, storage_loaded_bytes, hash_node_calls, } = self; - let epoch_cost_for_processing_credit_per_byte = - epoch.cost_for_known_cost_item(cached_fee_version, StorageProcessingCreditPerByte); + let epoch_cost_for_processing_credit_per_byte = fee_version.storage.storage_processing_credit_per_byte; let seek_cost = (*seek_count as u64) - .checked_mul(epoch.cost_for_known_cost_item(cached_fee_version, StorageSeekCost)) + .checked_mul(fee_version.storage.storage_seek_cost) .ok_or_else(|| get_overflow_error("seek cost overflow"))?; let storage_added_bytes_ephemeral_cost = (storage_cost.added_bytes as u64) .checked_mul(epoch_cost_for_processing_credit_per_byte) @@ -453,12 +461,14 @@ impl DriveCost for OperationCost { // not accessible let storage_loaded_bytes_cost = (*storage_loaded_bytes as u64) .checked_mul( - epoch.cost_for_known_cost_item(cached_fee_version, StorageLoadCreditPerByte), + fee_version.storage.storage_load_credit_per_byte, ) .ok_or_else(|| get_overflow_error("storage loaded cost overflow"))?; + + // There is one block per hash node call + let blake3_total = fee_version.hashing.blake3_base + fee_version.hashing.blake3_per_block; // this can't overflow - let hash_node_cost = - FunctionOp::new_with_round_count(HashFunction::Blake3, *hash_node_calls).cost(epoch); + let hash_node_cost = blake3_total * (*hash_node_calls as u64); seek_cost .checked_add(storage_added_bytes_ephemeral_cost) .and_then(|c| c.checked_add(storage_replaced_bytes_ephemeral_cost)) @@ -467,19 +477,4 @@ impl DriveCost for OperationCost { .and_then(|c| c.checked_add(hash_node_cost)) .ok_or_else(|| get_overflow_error("ephemeral cost addition overflow")) } - - /// Return the storage cost from the operation - fn storage_cost( - &self, - epoch: &Epoch, - cached_fee_version: &CachedEpochIndexFeeVersions, - ) -> Result { - //todo: deal with epochs - let OperationCost { storage_cost, .. } = self; - (storage_cost.added_bytes as u64) - .checked_mul( - epoch.cost_for_known_cost_item(cached_fee_version, StorageDiskUsageCreditPerByte), - ) - .ok_or_else(|| get_overflow_error("storage written bytes cost overflow")) - } } diff --git a/packages/rs-platform-version/src/version/fee/hashing/mod.rs b/packages/rs-platform-version/src/version/fee/hashing/mod.rs index 241d060c535..4f61d69a7f7 100644 --- a/packages/rs-platform-version/src/version/fee/hashing/mod.rs +++ b/packages/rs-platform-version/src/version/fee/hashing/mod.rs @@ -5,10 +5,11 @@ pub mod v1; #[derive(Clone, Debug, Default)] pub struct FeeHashingVersion { - pub double_sha256_base: u64, - pub double_sha256_per_block: u64, pub single_sha256_base: u64, - pub single_sha256_per_block: u64, + pub blake3_base: u64, + pub sha256_ripe_md160_base: u64, + pub sha256_per_block: u64, + pub blake3_per_block: u64, } impl FeeHashingVersion { diff --git a/packages/rs-platform-version/src/version/fee/hashing/v1.rs b/packages/rs-platform-version/src/version/fee/hashing/v1.rs index a29d60858e0..ebce88d17df 100644 --- a/packages/rs-platform-version/src/version/fee/hashing/v1.rs +++ b/packages/rs-platform-version/src/version/fee/hashing/v1.rs @@ -1,8 +1,6 @@ use crate::version::fee::hashing::FeeHashingVersion; pub const FEE_HASHING_VERSION1: FeeHashingVersion = FeeHashingVersion { - double_sha256_base: 500, - double_sha256_per_block: 400, single_sha256_base: 100, - single_sha256_per_block: 400, + sha256_per_block: 400, };