Skip to content

Commit

Permalink
new system using platform state and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jun 30, 2024
1 parent 3e95442 commit f7512b1
Show file tree
Hide file tree
Showing 32 changed files with 132 additions and 60 deletions.
1 change: 1 addition & 0 deletions packages/rs-dpp/src/fee/default_costs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub enum KnownCostItem {
}

impl KnownCostItem {
#[inline]
pub fn lookup_cost(&self, fee_version: &FeeVersion) -> Credits {
match self {
KnownCostItem::StorageDiskUsageCreditPerByte => {
Expand Down
4 changes: 2 additions & 2 deletions packages/rs-dpp/src/fee/fee_result/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, ProtocolError>
where
I: IntoIterator<Item = ([u8; 32], C)>,
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -211,6 +212,7 @@ mod tests {
&BlockInfo::default(),
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");
}
Expand Down Expand Up @@ -285,6 +287,7 @@ mod tests {
&BlockInfo::default(),
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +181,7 @@ impl<CoreRPCLike> Platform<CoreRPCLike> {
&block_info.to_block_info(epoch_info.try_into()?),
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

let outcome = processed_block_fees_outcome::v0::ProcessedBlockFeesOutcome {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> Platform<C>
where
Expand Down Expand Up @@ -140,6 +141,7 @@ where
block_info,
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

let height = block_info.height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl<C> Platform<C> {
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;
Expand Down Expand Up @@ -126,6 +127,7 @@ mod tests {
&BlockInfo::default(),
Some(&transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down Expand Up @@ -194,6 +196,7 @@ mod tests {
&BlockInfo::default(),
Some(&transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -196,6 +197,7 @@ mod tests {
&BlockInfo::default(),
Some(&transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<C> Platform<C> {
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;

Expand Down Expand Up @@ -203,6 +204,7 @@ mod tests {
&BlockInfo::default(),
Some(&transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<C> Platform<C> {
&BlockInfo::default(),
Some(transaction),
platform_version,
&None,
)?;

batch.push(DriveOperation::GroveDBOpBatch(operations));
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -281,6 +283,7 @@ mod tests {
&BlockInfo::default(),
Some(&transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.expect("should apply batch");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -189,6 +190,7 @@ impl<C> Platform<C> {
&block_info,
transaction,
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,6 +43,7 @@ where
block_info: &BlockInfo,
transaction: &Transaction,
platform_version: &PlatformVersion,
previous_fee_versions: &CachedEpochIndexFeeVersions,
) -> Result<EventExecutionResult, Error> {
match platform_version
.drive_abci
Expand All @@ -55,6 +57,7 @@ where
block_info,
transaction,
platform_version,
previous_fee_versions,
),
version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch {
method: "execute_event".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,7 @@ where
block_info: &BlockInfo,
transaction: &Transaction,
platform_version: &PlatformVersion,
previous_fee_versions: &CachedEpochIndexFeeVersions,
) -> Result<EventExecutionResult, Error> {
let maybe_fee_validation_result = match event {
ExecutionEvent::PaidFromAssetLock { .. } | ExecutionEvent::Paid { .. } => {
Expand Down Expand Up @@ -89,6 +91,7 @@ where
block_info,
Some(transaction),
platform_version,
previous_fee_versions,
)
.map_err(Error::Drive)?;

Expand Down Expand Up @@ -138,6 +141,7 @@ where
block_info,
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.map_err(Error::Drive)?;

Expand All @@ -162,6 +166,7 @@ where
block_info,
Some(transaction),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.map_err(Error::Drive)?;
Ok(SuccessfulFreeExecution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -115,6 +118,7 @@ where
block_info,
transaction,
platform_version,
previous_fee_versions,
)
.unwrap_or_else(error_to_internal_error_execution_result)
})
Expand Down Expand Up @@ -199,6 +203,7 @@ where
block_info: &BlockInfo,
transaction: &Transaction,
platform_version: &PlatformVersion,
previous_fee_versions: &CachedEpochIndexFeeVersions,
) -> Result<StateTransitionExecutionResult, StateTransitionAwareError<'a>> {
// State Transition is invalid
if !validation_result.is_valid() {
Expand Down Expand Up @@ -252,6 +257,7 @@ where
block_info,
transaction,
platform_version,
previous_fee_versions,
)
.map_err(|error| StateTransitionAwareError {
error,
Expand Down Expand Up @@ -343,6 +349,7 @@ where
block_info,
transaction,
platform_version,
previous_fee_versions,
)
.map_err(|error| StateTransitionAwareError {
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +62,7 @@ where
block_info,
transaction,
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.map_err(Error::Drive)?;

Expand Down Expand Up @@ -113,6 +114,7 @@ where
block_info,
transaction,
platform_version,
&CachedEpochIndexFeeVersions::default(),
)
.map_err(Error::Drive)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,7 @@ where
block_info,
transaction,
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

Ok(UnsignedWithdrawalTxs::from_vec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,6 +132,7 @@ where
block_info,
transaction,
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,6 +156,7 @@ where
block_info,
transaction.into(),
platform_version,
&CachedEpochIndexFeeVersions::default(),
)?;

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion packages/rs-drive-abci/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}

Expand Down
3 changes: 2 additions & 1 deletion packages/rs-drive/src/drive/batch/drive_op_batch/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -102,6 +102,7 @@ impl DriveLowLevelOperationConverter for DocumentOperationType<'_> {
block_info: &BlockInfo,
transaction: TransactionArg,
platform_version: &PlatformVersion,
previous_fee_versions: &CachedEpochIndexFeeVersions,
) -> Result<Vec<LowLevelDriveOperation>, Error> {
match self {
DocumentOperationType::AddDocument {
Expand Down
Loading

0 comments on commit f7512b1

Please sign in to comment.