From 35926bcc1fef6c3d30d4b7808a832583226fce56 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:31:01 -0500 Subject: [PATCH 01/46] feat(platform)!: introduce protocol version 15 with drive table v10 for storage refund fee history Protocol version 15 is a copy of 14 whose drive table (DRIVE_VERSION_V10) selects calculate_fee v1 and the new identity update slot credit_storage_refunds_to_owners (DRIVE_IDENTITY_METHOD_VERSIONS_V3). Shipped identity tables backfill the slot with None so every shipped protocol version is behaviour-preserving. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../drive_identity_method_versions/mod.rs | 6 + .../drive_identity_method_versions/v1.rs | 1 + .../drive_identity_method_versions/v2.rs | 1 + .../drive_identity_method_versions/v3.rs | 192 ++++++++++++++++++ .../src/version/drive_versions/v10.rs | 21 +- .../rs-platform-version/src/version/v15.rs | 21 +- 6 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs index c55c74dd04e..1f91b861322 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/mod.rs @@ -2,6 +2,7 @@ use versioned_feature_core::{FeatureVersion, OptionalFeatureVersion}; pub mod v1; pub mod v2; +pub mod v3; #[derive(Clone, Debug, Default)] pub struct DriveIdentityMethodVersions { @@ -248,4 +249,9 @@ pub struct DriveIdentityUpdateMethodVersions { /// Rewrites a key with a raised total budget or a later expiry (protocol version 14). Keys /// cannot carry limits before v14, so earlier tables keep the slot `None`. pub update_identity_key_limits: OptionalFeatureVersion, + /// Read by `Drive::credit_storage_refunds_to_owners_operations`: credits each recorded + /// owner of a storage refund and reports the amount whose owner has no balance element + /// so the caller can route it to the current epoch's processing pool. `None` before + /// protocol version 15, where no path settles refunds outside a state transition. + pub credit_storage_refunds_to_owners: OptionalFeatureVersion, } diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs index fd26e1af65b..4bb487f3cde 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs @@ -120,6 +120,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V1: DriveIdentityMethodVersions = remove_from_identity_balance: 0, refresh_identity_key_reference_operations: 0, update_identity_key_limits: None, + credit_storage_refunds_to_owners: None, }, insert: DriveIdentityInsertMethodVersions { add_new_identity: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs index 360c20b0661..ba65d2d03f4 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs @@ -177,6 +177,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V2: DriveIdentityMethodVersions = remove_from_identity_balance: 0, refresh_identity_key_reference_operations: 0, update_identity_key_limits: Some(0), + credit_storage_refunds_to_owners: None, }, insert: DriveIdentityInsertMethodVersions { add_new_identity: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs new file mode 100644 index 00000000000..57b56a38298 --- /dev/null +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs @@ -0,0 +1,192 @@ +use crate::version::drive_versions::drive_identity_method_versions::{ + DriveIdentityContractInfoMethodVersions, DriveIdentityCostEstimationMethodVersions, + DriveIdentityFetchAttributesMethodVersions, DriveIdentityFetchFullIdentityMethodVersions, + DriveIdentityFetchMethodVersions, DriveIdentityFetchPartialIdentityMethodVersions, + DriveIdentityFetchPublicKeyHashesMethodVersions, DriveIdentityInsertMethodVersions, + DriveIdentityKeyHashesToIdentityInsertMethodVersions, DriveIdentityKeysBudgetMethodVersions, + DriveIdentityKeysFetchMethodVersions, DriveIdentityKeysInsertMethodVersions, + DriveIdentityKeysMethodVersions, DriveIdentityKeysProveMethodVersions, + DriveIdentityMethodVersions, DriveIdentityProveMethodVersions, + DriveIdentityUpdateMethodVersions, DriveIdentityWithdrawalDocumentMethodVersions, + DriveIdentityWithdrawalMethodVersions, DriveIdentityWithdrawalTransactionIndexMethodVersions, + DriveIdentityWithdrawalTransactionMethodVersions, + DriveIdentityWithdrawalTransactionQueueMethodVersions, +}; + +/// V3 is protocol version 15's identity-method table. It differs from V2 in +/// one slot: +/// +/// * `update.credit_storage_refunds_to_owners` `None -> Some(0)`: the +/// primitive that credits each recorded owner of a storage refund and +/// reports the amount whose owner has no balance element, so a block +/// lifecycle path (or, later, the state transition refund path) can route +/// that amount to the current epoch's processing pool instead of halting. +/// No key or permission is consulted on any route: a frozen but existing +/// owner is credited, an owner without a balance is settled into the pool. +/// Nothing before v15 settles refunds outside a state transition, so V1 and +/// V2 keep the slot `None`. +pub const DRIVE_IDENTITY_METHOD_VERSIONS_V3: DriveIdentityMethodVersions = + DriveIdentityMethodVersions { + fetch: DriveIdentityFetchMethodVersions { + public_key_hashes: DriveIdentityFetchPublicKeyHashesMethodVersions { + fetch_full_identities_by_unique_public_key_hashes: 0, + fetch_full_identity_by_unique_public_key_hash: 0, + fetch_identity_id_by_unique_public_key_hash: 0, + fetch_identity_ids_by_non_unique_public_key_hash: 0, + fetch_identity_ids_by_unique_public_key_hashes: 0, + fetch_serialized_full_identity_by_unique_public_key_hash: 0, + has_any_of_unique_public_key_hashes: 0, + has_non_unique_public_key_hash: 0, + has_non_unique_public_key_hash_already_for_identity: 0, + has_unique_public_key_hash: 0, + fetch_full_identity_by_non_unique_public_key_hash: 0, + }, + attributes: DriveIdentityFetchAttributesMethodVersions { + revision: 0, + nonce: 0, + identity_contract_nonce: 0, + balance: 0, + balance_include_debt: 0, + negative_balance: 0, + }, + partial_identity: DriveIdentityFetchPartialIdentityMethodVersions { + fetch_identity_revision_with_keys: 0, + fetch_identity_balance_with_keys: 0, + fetch_identity_balance_with_keys_and_revision: 0, + fetch_identity_with_balance: 0, + fetch_identity_keys: 0, + }, + full_identity: DriveIdentityFetchFullIdentityMethodVersions { + fetch_full_identity: Some(0), + fetch_full_identities: Some(0), + }, + }, + prove: DriveIdentityProveMethodVersions { + full_identity: 0, + full_identities: 0, + identity_nonce: 0, + identity_contract_nonce: 0, + identities_contract_keys: 0, + prove_full_identities_by_unique_public_key_hashes: 0, + prove_full_identity_by_unique_public_key_hash: 0, + prove_identity_id_by_unique_public_key_hash: 0, + prove_identity_ids_by_unique_public_key_hashes: 0, + prove_full_identity_by_non_unique_public_key_hash: 0, + prove_full_identity_by_non_unique_public_key_hash_two_proofs: 0, + }, + keys: DriveIdentityKeysMethodVersions { + fetch: DriveIdentityKeysFetchMethodVersions { + fetch_all_current_identity_keys: 0, + fetch_all_identity_keys: 0, + fetch_identities_all_keys: 0, + fetch_identity_keys: 0, + fetch_identities_contract_keys: 0, + }, + prove: DriveIdentityKeysProveMethodVersions { + prove_identities_all_keys: 0, + prove_identity_keys: 0, + }, + insert: DriveIdentityKeysInsertMethodVersions { + create_key_tree_with_keys: 0, + create_new_identity_key_query_trees: 0, + insert_key_searchable_references: 0, + insert_key_to_storage: 0, + insert_new_non_unique_key: 1, + insert_new_unique_key: 1, + replace_key_in_storage: 0, + }, + insert_key_hash_identity_reference: + DriveIdentityKeyHashesToIdentityInsertMethodVersions { + add_estimation_costs_for_insert_non_unique_public_key_hash_reference: 0, + add_estimation_costs_for_insert_unique_public_key_hash_reference: 0, + insert_non_unique_public_key_hash_reference_to_identity: 0, + insert_reference_to_non_unique_key: 0, + insert_reference_to_unique_key: 0, + insert_unique_public_key_hash_reference_to_identity: 0, + }, + budget: DriveIdentityKeysBudgetMethodVersions { + insert_identity_key_budget: Some(0), + fetch_identity_key_remaining_budget: Some(0), + deduct_from_identity_key_budget: Some(0), + add_estimation_costs_for_key_budgets: Some(0), + fetch_identity_keys_remaining_budgets: Some(0), + prove_identity_keys_remaining_budgets: Some(0), + add_to_identity_key_budget: Some(0), + }, + }, + update: DriveIdentityUpdateMethodVersions { + update_identity_revision: 0, + merge_identity_nonce: 0, + update_identity_negative_credit_operation: 0, + initialize_identity_revision: 0, + disable_identity_keys: 1, + re_enable_identity_keys: 0, + add_new_non_unique_keys_to_identity: 0, + add_new_unique_keys_to_identity: 0, + add_new_keys_to_identity: 0, + insert_identity_balance: 0, + initialize_negative_identity_balance: 0, + add_to_identity_balance: 1, // changed in v14: credits that repay an identity's debt are owed to the processing fee pool + add_to_previous_balance: 0, + apply_balance_change_from_fee_to_identity: 1, // changed in v14: routes the debt a balance change repaid to the processing fee pool + remove_from_identity_balance: 0, + refresh_identity_key_reference_operations: 0, + update_identity_key_limits: Some(0), + credit_storage_refunds_to_owners: Some(0), // new in v15: credits recorded refund owners, reports the unrouted amount for the processing pool + }, + insert: DriveIdentityInsertMethodVersions { + add_new_identity: 0, + }, + contract_info: DriveIdentityContractInfoMethodVersions { + add_potential_contract_info_for_contract_bounded_key: 1, + refresh_potential_contract_info_key_references: 1, + merge_identity_contract_nonce: 0, + }, + cost_estimation: DriveIdentityCostEstimationMethodVersions { + for_authentication_keys_security_level_in_key_reference_tree: 0, + for_balances: 0, + for_token_balances: 0, + for_token_total_supply: 0, + for_contract_info: 0, + for_contract_info_group: 0, + for_contract_info_group_keys: 0, + for_contract_info_group_key_purpose: 0, + for_keys_for_identity_id: 0, + for_negative_credit: 0, + for_purpose_in_key_reference_tree: 0, + for_root_key_reference_tree: 0, + for_update_revision: 0, + for_token_identity_infos: 0, + for_token_perpetual_distribution: 0, + for_token_once_per_identity_distribution: 0, + for_token_pre_programmed_distribution: 0, + for_root_token_ms_interval_distribution: 0, + for_token_selling_prices: 0, + for_token_contract_infos: 0, + }, + withdrawals: DriveIdentityWithdrawalMethodVersions { + document: DriveIdentityWithdrawalDocumentMethodVersions { + fetch_oldest_withdrawal_documents_by_status: 0, + find_withdrawal_documents_by_status_and_transaction_indices: 1, + }, + transaction: DriveIdentityWithdrawalTransactionMethodVersions { + index: DriveIdentityWithdrawalTransactionIndexMethodVersions { + fetch_next_withdrawal_transaction_index: 0, + add_update_next_withdrawal_transaction_index_operation: 0, + }, + queue: DriveIdentityWithdrawalTransactionQueueMethodVersions { + add_enqueue_untied_withdrawal_transaction_operations: 0, + dequeue_untied_withdrawal_transactions: 0, + remove_broadcasted_withdrawal_transactions_after_completion_operations: 0, + move_broadcasted_withdrawal_transactions_back_to_queue_operations: 0, + }, + }, + calculate_current_withdrawal_limit: 1, // changed in v14: daily maximum is a percentage of the total credits a day ago plus the credit inflows of the last 25 hours + record_total_credits_history: Some(0), // new in v14: total credits history for the day-lagged daily withdrawal limit + fetch_total_credits_in_platform_a_day_ago: Some(0), // new in v14 + record_credit_inflows: Some(0), // new in v14: credit inflows sum tree for the net daily withdrawal limit + record_core_credit_pool_blocks: Some(0), // new in v14: Core credit pool balances for the Core-anchored withdrawal limit + fetch_core_credit_pool_balances: Some(0), // new in v14 + fetch_in_flight_withdrawal_amount: Some(0), // new in v14 + }, + }; diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 8aef3e6c3c4..408f171d31c 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -1,12 +1,13 @@ use crate::version::drive_versions::drive_contract_method_versions::v5::DRIVE_CONTRACT_METHOD_VERSIONS_V5; use crate::version::drive_versions::drive_document_method_versions::v5::DRIVE_DOCUMENT_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_identity_method_versions::v3::DRIVE_IDENTITY_METHOD_VERSIONS_V3; use crate::version::drive_versions::drive_state_transition_method_versions::v5::DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5; use crate::version::drive_versions::drive_verify_method_versions::v4::DRIVE_VERIFY_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_vote_method_versions::v4::DRIVE_VOTE_METHOD_VERSIONS_V4; use crate::version::drive_versions::v9::DRIVE_VERSION_V9; use crate::version::drive_versions::{ - DriveBatchOperationsMethodVersion, DriveMethodVersions, DrivePlatformSystemMethodVersions, - DriveSystemEstimationCostsMethodVersions, DriveVersion, + DriveBatchOperationsMethodVersion, DriveFeesMethodVersions, DriveMethodVersions, + DrivePlatformSystemMethodVersions, DriveSystemEstimationCostsMethodVersions, DriveVersion, }; /// Drive version 10. @@ -45,11 +46,23 @@ use crate::version::drive_versions::{ /// `stateless_delete_of_non_tree_for_costs` to 1, and /// `platform_system.estimation_costs.for_total_system_credits_update` is 1, so a delete and a /// system credits update describe their layers as they are. Applying a batch is unchanged. +/// * **Fee history required for refunds** — `fees.calculate_fee` 0 -> 1. +/// The v1 generation consults the block's fee history on every fee +/// version number and returns an internal error for an owner-attributed +/// (sectioned) storage removal without that history, where v0 priced +/// fee version number 1 against an empty map. Every shipped schedule +/// shares number 1 and the same storage rates, so the credits are +/// unchanged; only a missing history is now an error instead of a +/// silent fallback to the first-generation rates. +/// * **Recorded-owner refund credits** — `DRIVE_IDENTITY_METHOD_VERSIONS_V3` +/// turns on `update.credit_storage_refunds_to_owners`, the primitive +/// block lifecycle paths use to credit each recorded owner of a refund +/// and report the amount whose owner has no balance element. /// /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { - contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: compiled contract storage + contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers @@ -63,6 +76,8 @@ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { for_total_system_credits_update: 1, // changed in v10: `Misc` described with its subtrees }, }, + fees: DriveFeesMethodVersions { calculate_fee: 1 }, // changed in v10: fee history required and consulted for every storage refund + identity: DRIVE_IDENTITY_METHOD_VERSIONS_V3, // changed in v10: credit_storage_refunds_to_owners primitive for lifecycle refund settlement ..DRIVE_VERSION_V9.methods }, ..DRIVE_VERSION_V9 diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 160493a0855..caf2e17b8c3 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -348,9 +348,28 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// transition pays is unchanged, as applying a batch is generation 1's. Protocol v14 keeps /// `apply_drive_operations` 1 and the estimators' earlier versions, and protocol v13 and /// earlier keep `apply_drive_operations` 0; neither resolves an estimate. +/// +/// 15. **Fee history required for storage refunds**: `DRIVE_VERSION_V10` +/// bumps `fees.calculate_fee` to 1. A storage refund for owner-attributed +/// bytes is priced only with the fee history of the block that removes +/// the bytes; the history is consulted on every fee version number and a +/// missing history is an internal error instead of a silent fallback to +/// the first-generation storage rates. Every shipped schedule shares fee +/// version number 1 and the same storage rates, so refund credits are +/// unchanged for every shipped input; the boundary makes the absence of +/// history an error from this version onward. +/// +/// 16. **Recorded-owner refund credits**: the same drive table turns on +/// `identity.update.credit_storage_refunds_to_owners`, the primitive that +/// credits each recorded owner of a refund without consulting any key or +/// permission and reports the amount whose owner has no balance element, +/// so block lifecycle paths can settle it into the current epoch's +/// processing pool. +/// +/// Everything else matches v14. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, - drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10) and verify_private_document_entries Some(0) a page of a private type's store (note 13); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store, and fetch_private_document_entries and prove_private_document_entries Some(0) read and prove a page of it (note 13); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) + drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); calculate_fee v1 (fee history required for refunds) + identity table v3 (credit_storage_refunds_to_owners); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store (note 13); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) drive_abci: DriveAbciVersion { structs: DRIVE_ABCI_STRUCTURE_VERSIONS_V2, methods: DRIVE_ABCI_METHOD_VERSIONS_V11, // changed: the yes/no poll's tally, record, clean-up and finish hook are Some(0) (note 10); verify_chain_lock_locally 1 rejects a chain lock locally only when Platform holds the quorums active at its signing height, and verify_chain_lock_through_core 1 has Core's verifychainlock check the others before submitchainlock From 9ca1e39095f528a56c55aa3b36c10580ca06a48b Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:38:19 -0500 Subject: [PATCH 02/46] feat(drive)!: require fee history for storage refunds and credit their recorded owners calculate_fee v1 (consume_to_fees_v1) consults the block's fee history for every owner-attributed storage removal and returns CorruptedCodeExecution without one, on every fee version number; v0 stays byte-identical. The new versioned method credit_storage_refunds_to_owners_operations credits each recorded owner that has a balance element without consulting any key or permission and reports the amount whose owner has no balance for the caller to route to the processing pool. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../mod.rs | 77 ++++++++++++++++++ .../v0/mod.rs | 81 +++++++++++++++++++ .../src/drive/identity/update/methods/mod.rs | 1 + .../src/drive/identity/update/structs/mod.rs | 2 + .../storage_refund_credit_outcome/mod.rs | 33 ++++++++ .../rs-drive/src/fees/calculate_fee/mod.rs | 11 ++- .../rs-drive/src/fees/calculate_fee/v1/mod.rs | 54 +++++++++++++ packages/rs-drive/src/fees/op.rs | 73 +++++++++++++++++ 8 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs create mode 100644 packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs create mode 100644 packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs create mode 100644 packages/rs-drive/src/fees/calculate_fee/v1/mod.rs diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs new file mode 100644 index 00000000000..1e107fe2692 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs @@ -0,0 +1,77 @@ +mod v0; + +use crate::drive::identity::update::storage_refund_credit_outcome::StorageRefundCreditOutcome; +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use dpp::fee::fee_result::refunds::FeeRefunds; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Credits storage refunds to their recorded owners and reports what could + /// not be routed. + /// + /// For every owner in `fee_refunds` except `skip_owner`, the owner's + /// per-epoch credits are summed with checked arithmetic. An owner with a + /// balance element is credited through `add_to_identity_balance_operations`; + /// no key, signature or permission is consulted, so a frozen but existing + /// owner receives its bookkeeping refund. An owner without a balance + /// element (the native proxy for a wiped owner) is not credited and its + /// amount is reported as `routed_to_processing_pool`, for the caller to + /// settle into the current epoch's processing pool with one pool write. + /// The primitive records no pending refunds; the caller does, so the block + /// keeps a single pending-refund and pool write per batch. + /// + /// # Parameters + /// + /// * `fee_refunds` - The refunds to settle, per owner and storage epoch. + /// * `skip_owner` - An owner whose refund the caller settles itself (a + /// state transition payer, whose refund folds into its balance change); + /// `None` on lifecycle paths. + /// * `transaction` - The current transaction. + /// * `drive_operations` - The accumulator the balance operations are + /// appended to; the caller applies them. + /// * `platform_version` - The platform version. + /// + /// # Returns + /// + /// * `Ok(StorageRefundCreditOutcome)` - The owners credited and the amount + /// routed to the processing pool. + /// * `Err(Error)` - On overflow, a corrupted balance element, or when the + /// method is not active for the platform version. + pub fn credit_storage_refunds_to_owners_operations( + &self, + fee_refunds: &FeeRefunds, + skip_owner: Option<[u8; 32]>, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result { + match platform_version + .drive + .methods + .identity + .update + .credit_storage_refunds_to_owners + { + Some(0) => self.credit_storage_refunds_to_owners_operations_v0( + fee_refunds, + skip_owner, + transaction, + drive_operations, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "credit_storage_refunds_to_owners_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "credit_storage_refunds_to_owners_operations".to_string(), + known_versions: vec![0], + })), + } + } +} diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs new file mode 100644 index 00000000000..5b932a0e9a1 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -0,0 +1,81 @@ +use crate::drive::identity::update::storage_refund_credit_outcome::StorageRefundCreditOutcome; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::fee::fee_result::refunds::FeeRefunds; +use dpp::fee::Credits; +use dpp::prelude::Identifier; +use dpp::version::PlatformVersion; +use dpp::ProtocolError; +use grovedb::TransactionArg; +use std::collections::BTreeMap; + +impl Drive { + /// Credits each recorded refund owner that has a balance element and + /// reports the rest as routed to the processing pool. See the dispatcher. + #[inline(always)] + pub(super) fn credit_storage_refunds_to_owners_operations_v0( + &self, + fee_refunds: &FeeRefunds, + skip_owner: Option<[u8; 32]>, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result { + let mut credited: BTreeMap = BTreeMap::new(); + let mut routed_to_processing_pool: Credits = 0; + + for (owner_id, credits_per_epoch) in fee_refunds.iter() { + if skip_owner.as_ref() == Some(owner_id) { + continue; + } + + let credits = credits_per_epoch + .values() + .try_fold(0u64, |sum, epoch_credits| sum.checked_add(*epoch_credits)) + .ok_or(ProtocolError::Overflow( + "storage refund credits for one owner overflow", + ))?; + + if credits == 0 { + continue; + } + + // A stateful read: `None` means the balance element does not exist, which + // is the only signal Drive has today that the owner is gone. + let existing_balance = self.fetch_identity_balance_operations( + *owner_id, + true, + transaction, + drive_operations, + platform_version, + )?; + + if existing_balance.is_some() { + let mut estimated_costs_only_with_layer_info = None::; + + drive_operations.extend(self.add_to_identity_balance_operations( + *owner_id, + credits, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?); + + credited.insert(Identifier::from(*owner_id), credits); + } else { + routed_to_processing_pool = routed_to_processing_pool.checked_add(credits).ok_or( + ProtocolError::Overflow( + "storage refund credits routed to the processing pool overflow", + ), + )?; + } + } + + Ok(StorageRefundCreditOutcome { + credited, + routed_to_processing_pool, + }) + } +} diff --git a/packages/rs-drive/src/drive/identity/update/methods/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/mod.rs index c4bccd40633..7761f67037f 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/mod.rs @@ -4,6 +4,7 @@ mod add_new_unique_keys_to_identity; mod add_to_identity_balance; mod add_to_previous_balance; mod apply_balance_change_from_fee_to_identity; +mod credit_storage_refunds_to_owners_operations; #[cfg(test)] mod debt_test_helpers; mod disable_identity_keys; diff --git a/packages/rs-drive/src/drive/identity/update/structs/mod.rs b/packages/rs-drive/src/drive/identity/update/structs/mod.rs index dbdb9a86d2f..71b93a3715f 100644 --- a/packages/rs-drive/src/drive/identity/update/structs/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/structs/mod.rs @@ -2,3 +2,5 @@ pub mod add_to_previous_balance_outcome; /// Applying a balance chance outcome pub mod apply_balance_change_outcome; +/// The outcome of crediting storage refunds to their recorded owners +pub mod storage_refund_credit_outcome; diff --git a/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs b/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs new file mode 100644 index 00000000000..2e42e9bd7b2 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs @@ -0,0 +1,33 @@ +use dpp::fee::Credits; +use dpp::prelude::Identifier; +use std::collections::BTreeMap; + +/// What `Drive::credit_storage_refunds_to_owners_operations` did with a set of +/// storage refunds: which recorded owners were credited and how much could not +/// be routed to any owner. +/// +/// The caller that knows the block's epoch settles `routed_to_processing_pool` +/// into that epoch's processing pool with a single pool write and records the +/// refunds against their storage epochs; the primitive itself never touches +/// the pools. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct StorageRefundCreditOutcome { + /// Credits added to each recorded owner's balance, summed over the epochs + /// the owner's bytes were stored in. + pub credited: BTreeMap, + /// Credits whose recorded owner has no balance element. Until a typed + /// owner encoding exists this is the native proxy for a wiped owner, so + /// the caller moves this amount into the current epoch's processing pool. + pub routed_to_processing_pool: Credits, +} + +impl StorageRefundCreditOutcome { + /// The total credited to owners plus the amount routed to the pool. + pub fn total(&self) -> Option { + self.credited + .values() + .try_fold(self.routed_to_processing_pool, |total, credits| { + total.checked_add(*credits) + }) + } +} diff --git a/packages/rs-drive/src/fees/calculate_fee/mod.rs b/packages/rs-drive/src/fees/calculate_fee/mod.rs index f2a6516a5ed..db7f20f418b 100644 --- a/packages/rs-drive/src/fees/calculate_fee/mod.rs +++ b/packages/rs-drive/src/fees/calculate_fee/mod.rs @@ -9,6 +9,7 @@ use dpp::version::PlatformVersion; use enum_map::EnumMap; mod v0; +mod v1; impl Drive { /// Calculates fees for the given operations. Returns the storage and processing costs. @@ -47,9 +48,17 @@ impl Drive { &platform_version.fee_version, previous_fee_versions, ), + 1 => Self::calculate_fee_v1( + base_operations, + drive_operations, + epoch, + epochs_per_era, + &platform_version.fee_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "Drive::calculate_fee".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs b/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs new file mode 100644 index 00000000000..4201dc023bd --- /dev/null +++ b/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs @@ -0,0 +1,54 @@ +use crate::drive::Drive; +use crate::error::fee::FeeError; +use crate::error::Error; +use crate::fees::op::{BaseOp, LowLevelDriveOperation}; +use dpp::block::epoch::Epoch; +use dpp::fee::fee_result::FeeResult; + +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use enum_map::EnumMap; +use platform_version::version::fee::FeeVersion; + +impl Drive { + /// Calculates fees for the given operations. Returns the storage and processing costs. + /// + /// Generation 1: storage refunds are priced through `consume_to_fees_v1`, + /// which requires and consults the fee history for every owner-attributed + /// storage removal. Selected by drive table v10 (protocol version 15). + #[inline(always)] + pub(crate) fn calculate_fee_v1( + base_operations: Option>, + drive_operations: Option>, + epoch: &Epoch, + epochs_per_era: u16, + fee_version: &FeeVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let mut aggregate_fee_result = FeeResult::default(); + if let Some(base_operations) = base_operations { + for (base_op, count) in base_operations.iter() { + match base_op.cost().checked_mul(*count) { + None => return Err(Error::Fee(FeeError::Overflow("overflow error"))), + Some(cost) => match aggregate_fee_result.processing_fee.checked_add(cost) { + None => return Err(Error::Fee(FeeError::Overflow("overflow error"))), + Some(value) => aggregate_fee_result.processing_fee = value, + }, + } + } + } + + if let Some(drive_operations) = drive_operations { + for drive_fee_result in LowLevelDriveOperation::consume_to_fees_v1( + drive_operations, + epoch, + epochs_per_era, + fee_version, + previous_fee_versions, + )? { + aggregate_fee_result.checked_add_assign(drive_fee_result)?; + } + } + + Ok(aggregate_fee_result) + } +} diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index 3793a5ede9b..ee66e337857 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -462,6 +462,79 @@ impl LowLevelDriveOperation { .collect() } + /// Returns a list of the costs of the Drive operations, pricing every + /// owner-attributed storage removal with the fee history of the block + /// that removes the bytes. + /// + /// This is the generation `Drive::calculate_fee` v1 selects. It differs + /// from `consume_to_fees_v0` in one arm: a `SectionedStorageRemoval` + /// always consults `previous_fee_versions` and returns + /// `CorruptedCodeExecution` when none is given, on every fee version + /// number. v0 priced fee version number 1 against an empty history, so a + /// caller that forgot the history silently refunded at the first + /// generation's storage rates; from protocol version 15 that omission is + /// an error, never a fallback to a schedule. + pub fn consume_to_fees_v1( + drive_operations: Vec, + epoch: &Epoch, + epochs_per_era: u16, + fee_version: &FeeVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result, Error> { + drive_operations + .into_iter() + .map(|operation| match operation { + PreCalculatedFeeResult(f) => Ok(f), + FunctionOperation(op) => Ok(FeeResult { + processing_fee: op.cost(fee_version), + ..Default::default() + }), + _ => { + let cost = operation.operation_cost()?; + // 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), + BasicStorageRemoval(amount) => { + // this is not always considered an error + (FeeRefunds::default(), amount) + } + SectionedStorageRemoval(mut removal_per_epoch_by_identifier) => { + let system_amount = removal_per_epoch_by_identifier + .remove(&Identifier::default()) + .map_or(0, |a| a.values().sum()); + let previous_fee_versions = previous_fee_versions.ok_or( + Error::Drive(DriveError::CorruptedCodeExecution( + "a storage refund needs the fee history of the block that removes the bytes", + )), + )?; + ( + FeeRefunds::from_storage_removal( + removal_per_epoch_by_identifier, + epoch.index, + epochs_per_era, + previous_fee_versions, + )?, + system_amount, + ) + } + }; + Ok(FeeResult { + storage_fee, + processing_fee, + fee_refunds, + removed_bytes_from_system, + lifetime_storage_fees: Default::default(), + }) + } + }) + .collect() + } + /// Returns the cost of this operation pub fn operation_cost(self) -> Result { match self { From 8860f1bfbbe6cd7a85a4693c352fcc4bba4858b6 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:49:28 -0500 Subject: [PATCH 03/46] test(drive): pin fee history handling of calculate_fee v0 and v1 Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../rs-drive/src/fees/calculate_fee/mod.rs | 95 ++++++ packages/rs-drive/src/fees/op.rs | 303 ++++++++++++++++++ 2 files changed, 398 insertions(+) diff --git a/packages/rs-drive/src/fees/calculate_fee/mod.rs b/packages/rs-drive/src/fees/calculate_fee/mod.rs index db7f20f418b..fafa1b185bc 100644 --- a/packages/rs-drive/src/fees/calculate_fee/mod.rs +++ b/packages/rs-drive/src/fees/calculate_fee/mod.rs @@ -64,3 +64,98 @@ impl Drive { } } } + +#[cfg(test)] +mod tests { + use super::*; + use dpp::fee::epoch::DEFAULT_EPOCHS_PER_ERA; + use grovedb_costs::storage_cost::removal::StorageRemovalPerEpochByIdentifier; + use grovedb_costs::storage_cost::removal::StorageRemovedBytes; + use grovedb_costs::storage_cost::StorageCost; + use grovedb_costs::OperationCost; + use platform_version::version::fee::FeeVersion; + use std::collections::BTreeMap; + + fn owner_attributed_removal() -> Vec { + let mut removal = StorageRemovalPerEpochByIdentifier::default(); + removal.entry([7; 32]).or_default().insert(3, 1000); + vec![LowLevelDriveOperation::CalculatedCostOperation( + OperationCost { + seek_count: 1, + storage_cost: StorageCost { + added_bytes: 0, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::SectionedStorageRemoval(removal), + }, + storage_loaded_bytes: 0, + hash_node_calls: 0, + sinsemilla_hash_calls: 0, + }, + )] + } + + /// Both generations through the dispatcher on the same operations: the + /// last frozen protocol version prices an owner-attributed removal without + /// a fee history, the latest refuses it, and with the history both yield + /// the same fee result. + #[test] + fn should_require_the_fee_history_for_storage_refunds_from_protocol_version_15() { + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let platform_version = PlatformVersion::latest(); + let epoch = Epoch::new(5).expect("epoch 5"); + + let frozen_without_history = Drive::calculate_fee( + None, + Some(owner_attributed_removal()), + &epoch, + DEFAULT_EPOCHS_PER_ERA, + frozen_platform_version, + None, + ) + .expect("protocol version 14 prices fee version number 1 without a history"); + assert!( + frozen_without_history.fee_refunds.get(&[7; 32]).is_some(), + "the shipped generation refunds at the first generation's rates" + ); + + let latest_without_history = Drive::calculate_fee( + None, + Some(owner_attributed_removal()), + &epoch, + DEFAULT_EPOCHS_PER_ERA, + platform_version, + None, + ); + assert!( + matches!( + latest_without_history, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "the latest generation refuses a storage refund without the fee history, got {:?}", + latest_without_history + ); + + let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0u16, FeeVersion::first())]); + let frozen_with_history = Drive::calculate_fee( + None, + Some(owner_attributed_removal()), + &epoch, + DEFAULT_EPOCHS_PER_ERA, + frozen_platform_version, + Some(&history), + ) + .expect("protocol version 14 prices with a history"); + let latest_with_history = Drive::calculate_fee( + None, + Some(owner_attributed_removal()), + &epoch, + DEFAULT_EPOCHS_PER_ERA, + platform_version, + Some(&history), + ) + .expect("the latest generation prices with a history"); + + assert_eq!(frozen_with_history, latest_with_history); + assert_eq!(frozen_with_history, frozen_without_history); + } +} diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index ee66e337857..9978e923447 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -3066,4 +3066,307 @@ mod tests { "expected overflow error when summing large components" ); } + + // --------------------------------------------------------------- + // consume_to_fees_v1: fee history required and consulted for + // owner-attributed storage removals + // --------------------------------------------------------------- + + mod storage_refund_fee_history { + use super::*; + use dpp::fee::epoch::distribution::calculate_storage_fee_refund_amount_and_leftovers; + use dpp::fee::epoch::DEFAULT_EPOCHS_PER_ERA; + use grovedb_costs::storage_cost::removal::StorageRemovalPerEpochByIdentifier; + use platform_version::version::fee::storage::v1::FEE_STORAGE_VERSION1; + use platform_version::version::fee::v1::FEE_VERSION1; + use platform_version::version::PLATFORM_VERSIONS; + + const OWNER: [u8; 32] = [7; 32]; + const OTHER_OWNER: [u8; 32] = [9; 32]; + + /// A schedule no protocol version references, with doubled storage + /// rates, so a test can tell "the history was consulted" apart from + /// "the first generation's rates were used". + static SYNTHETIC_FEE_VERSION_2: FeeVersion = FeeVersion { + fee_version_number: 2, + storage: FeeStorageVersion { + storage_disk_usage_credit_per_byte: 2 * FEE_STORAGE_VERSION1 + .storage_disk_usage_credit_per_byte, + ..FEE_STORAGE_VERSION1 + }, + ..FEE_VERSION1 + }; + + /// One removed element whose bytes are attributed per owner and per + /// storage epoch, the shape grovedb reports for an element carrying + /// owner storage flags. + fn sectioned_removal( + bytes_by_owner: &[([u8; 32], &[(u16, u32)])], + ) -> LowLevelDriveOperation { + let mut removal = StorageRemovalPerEpochByIdentifier::default(); + for (owner, bytes_per_epoch) in bytes_by_owner { + let owner_bytes = removal.entry(*owner).or_default(); + for (epoch_index, bytes) in bytes_per_epoch.iter() { + owner_bytes.insert(*epoch_index, *bytes); + } + } + CalculatedCostOperation(OperationCost { + seek_count: 1, + storage_cost: StorageCost { + added_bytes: 0, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::SectionedStorageRemoval(removal), + }, + storage_loaded_bytes: 0, + hash_node_calls: 0, + sinsemilla_hash_calls: 0, + }) + } + + fn basic_removal(bytes: u32) -> LowLevelDriveOperation { + CalculatedCostOperation(OperationCost { + seek_count: 1, + storage_cost: StorageCost { + added_bytes: 0, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::BasicStorageRemoval(bytes), + }, + storage_loaded_bytes: 0, + hash_node_calls: 0, + sinsemilla_hash_calls: 0, + }) + } + + fn epoch(index: u16) -> Epoch { + Epoch::new(index).expect("test epoch index fits") + } + + fn refund_for(fee_results: &[FeeResult], owner: &[u8; 32], storage_epoch: u16) -> Credits { + *fee_results + .iter() + .find_map(|fee_result| fee_result.fee_refunds.get(owner)) + .expect("the owner should be refunded") + .get(&storage_epoch) + .expect("the storage epoch should be refunded") + } + + #[test] + fn should_reject_a_sectioned_removal_without_fee_history_in_v1() { + let operations = vec![sectioned_removal(&[(OWNER, &[(3, 1000)])])]; + + let result = LowLevelDriveOperation::consume_to_fees_v1( + operations, + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ); + + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "v1 must refuse to price an owner-attributed removal without the fee history, got {:?}", + result + ); + + // v0 keeps its shipped shortcut for fee version number 1. + let operations = vec![sectioned_removal(&[(OWNER, &[(3, 1000)])])]; + LowLevelDriveOperation::consume_to_fees_v0( + operations, + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("v0 prices fee version number 1 against an empty history"); + } + + #[test] + fn should_reject_a_removal_of_unowned_flagged_bytes_without_fee_history_in_v1() { + // Bytes flagged with an epoch but no owner land in the system + // bucket; they are never refunded, but the removal is still + // sectioned and the rule is deliberately uniform: every + // sectioned removal carries the history of the removing block. + let operations = vec![sectioned_removal(&[(Identifier::default(), &[(3, 1000)])])]; + + let result = LowLevelDriveOperation::consume_to_fees_v1( + operations, + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ); + + assert!(matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + )); + } + + #[test] + fn should_not_need_fee_history_in_v1_for_unflagged_removals() { + let operations = vec![basic_removal(1000)]; + + let fee_results = LowLevelDriveOperation::consume_to_fees_v1( + operations, + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("unflagged bytes are removed from the system, no refund is priced"); + + assert_eq!(fee_results.len(), 1); + assert_eq!(fee_results[0].removed_bytes_from_system, 1000); + assert_eq!(fee_results[0].fee_refunds, FeeRefunds::default()); + } + + #[test] + fn should_consult_the_fee_history_in_v1_even_for_fee_version_number_one() { + // The history says a doubled schedule has been active since epoch 10. + // The bytes were stored at epoch 12 and are removed at epoch 15 under + // a fee version whose number is 1, the exact case v0 shortcuts. + let history: CachedEpochIndexFeeVersions = + BTreeMap::from([(10u16, &SYNTHETIC_FEE_VERSION_2)]); + let stored_bytes = 1000u32; + let storage_epoch = 12u16; + let removal_epoch = 15u16; + + let v0_results = LowLevelDriveOperation::consume_to_fees_v0( + vec![sectioned_removal(&[( + OWNER, + &[(storage_epoch, stored_bytes)], + )])], + &epoch(removal_epoch), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + Some(&history), + ) + .expect("v0 prices"); + let v1_results = LowLevelDriveOperation::consume_to_fees_v1( + vec![sectioned_removal(&[( + OWNER, + &[(storage_epoch, stored_bytes)], + )])], + &epoch(removal_epoch), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + Some(&history), + ) + .expect("v1 prices with the history"); + + let (expected_v0, _) = calculate_storage_fee_refund_amount_and_leftovers( + stored_bytes as Credits * FEE_STORAGE_VERSION1.storage_disk_usage_credit_per_byte, + storage_epoch, + removal_epoch, + DEFAULT_EPOCHS_PER_ERA, + ) + .expect("refund math"); + let (expected_v1, _) = calculate_storage_fee_refund_amount_and_leftovers( + stored_bytes as Credits + * SYNTHETIC_FEE_VERSION_2 + .storage + .storage_disk_usage_credit_per_byte, + storage_epoch, + removal_epoch, + DEFAULT_EPOCHS_PER_ERA, + ) + .expect("refund math"); + + assert_eq!( + refund_for(&v0_results, &OWNER, storage_epoch), + expected_v0, + "v0 ignores the history for fee version number 1 and prices at the first generation" + ); + assert_eq!( + refund_for(&v1_results, &OWNER, storage_epoch), + expected_v1, + "v1 prices with the schedule the history resolves for the epoch" + ); + assert!(expected_v1 > expected_v0); + } + + #[test] + fn should_credit_the_same_refunds_in_v1_as_in_v0_for_every_shipped_platform_version() { + // Every shipped schedule shares fee version number 1 and the same + // storage rates (pinned below), so for every history the epoch + // change hook can build from the shipped versions, v1's credits + // equal v0's: the boundary changes what a missing history does, + // not what a present one yields. + let removal_epoch = 15u16; + let bytes_by_owner: &[([u8; 32], &[(u16, u32)])] = &[ + (OWNER, &[(0, 900), (3, 1200), (7, 64), (12, 5000)]), + (OTHER_OWNER, &[(5, 31), (11, 2048)]), + (Identifier::default(), &[(2, 700)]), + ]; + + for platform_version in PLATFORM_VERSIONS { + for history_epoch in [0u16, 1, 5, 12, 15] { + let history: CachedEpochIndexFeeVersions = + BTreeMap::from([(history_epoch, &platform_version.fee_version)]); + + let v0_results = LowLevelDriveOperation::consume_to_fees_v0( + vec![sectioned_removal(bytes_by_owner), basic_removal(40)], + &epoch(removal_epoch), + DEFAULT_EPOCHS_PER_ERA, + &platform_version.fee_version, + Some(&history), + ) + .expect("v0 prices"); + let v1_results = LowLevelDriveOperation::consume_to_fees_v1( + vec![sectioned_removal(bytes_by_owner), basic_removal(40)], + &epoch(removal_epoch), + DEFAULT_EPOCHS_PER_ERA, + &platform_version.fee_version, + Some(&history), + ) + .expect("v1 prices"); + + assert_eq!( + v0_results, v1_results, + "protocol version {} with a history entry at epoch {} must refund identically in v0 and v1", + platform_version.protocol_version, history_epoch + ); + assert!( + v1_results[0].fee_refunds.get(&OWNER).is_some(), + "the owner's bytes must be refunded" + ); + assert!( + v1_results[0] + .fee_refunds + .get(&Identifier::default()) + .is_none(), + "system bytes are never refunded" + ); + assert_eq!(v1_results[0].removed_bytes_from_system, 700); + } + } + } + + /// The premise of the equality above. A shipped schedule that kept + /// fee version number 1 but changed its storage rates would make v0 + /// (which prices number 1 at the first generation) and v1 (which + /// prices at the schedule the history resolves) diverge on the same + /// input; such a change needs a new fee version number, which both + /// generations already look up in the history. + #[test] + fn should_keep_every_shipped_schedule_on_the_first_generation_storage_rates() { + for platform_version in PLATFORM_VERSIONS { + assert_eq!( + platform_version.fee_version.fee_version_number, + FeeVersion::first().fee_version_number, + "protocol version {} changed its fee version number", + platform_version.protocol_version + ); + assert_eq!( + platform_version.fee_version.storage, + FeeVersion::first().storage, + "protocol version {} changed its storage rates without a new fee version number", + platform_version.protocol_version + ); + } + } + } } From dc166c9adc808226e729bf96d10535dbecf09fec Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:51:14 -0500 Subject: [PATCH 04/46] test(drive): cover the recorded-owner refund credit primitive Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../v0/mod.rs | 398 ++++++++++++++++++ 1 file changed, 398 insertions(+) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index 5b932a0e9a1..987ed775e69 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -79,3 +79,401 @@ impl Drive { }) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::error::drive::DriveError; + use crate::util::batch::DriveOperation; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::block::epoch::Epoch; + use dpp::fee::epoch::CreditsPerEpoch; + use dpp::identity::accessors::{IdentityGettersV0, IdentitySettersV0}; + use dpp::identity::Identity; + use grovedb::Transaction; + + const IDENTITY_BALANCE: Credits = 10_000_000; + const PROCESSING_POOL_SEED: Credits = 1_000_000; + + fn insert_identity( + drive: &Drive, + seed: u64, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Identity { + let mut identity = Identity::random_identity(3, Some(seed), platform_version) + .expect("expected a random identity"); + identity.set_balance(IDENTITY_BALANCE); + drive + .add_new_identity( + identity.clone(), + false, + &BlockInfo::default(), + true, + Some(transaction), + platform_version, + ) + .expect("expected to insert the identity"); + identity + } + + fn refunds(entries: &[([u8; 32], &[(u16, Credits)])]) -> FeeRefunds { + let mut fee_refunds = FeeRefunds::default(); + for (owner, credits_per_epoch) in entries { + let mut epochs = CreditsPerEpoch::default(); + for (epoch_index, credits) in credits_per_epoch.iter() { + epochs.insert(*epoch_index, *credits); + } + fee_refunds.0.insert(*owner, epochs); + } + fee_refunds + } + + fn apply( + drive: &Drive, + operations: Vec, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) { + drive + .apply_batch_low_level_drive_operations( + None, + Some(transaction), + operations, + &mut vec![], + &platform_version.drive, + ) + .expect("expected to apply the operations"); + } + + fn balance( + drive: &Drive, + identity_id: [u8; 32], + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Option { + drive + .fetch_identity_balance(identity_id, Some(transaction), platform_version) + .expect("expected to fetch the balance") + } + + #[test] + fn should_credit_each_recorded_owner_by_the_sum_of_its_epochs() { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let first = insert_identity(&drive, 1, &transaction, platform_version); + let second = insert_identity(&drive, 2, &transaction, platform_version); + let first_id = first.id().to_buffer(); + let second_id = second.id().to_buffer(); + + let fee_refunds = refunds(&[ + (first_id, &[(0, 300), (4, 700), (9, 1)]), + (second_id, &[(2, 5_000)]), + ]); + + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to credit the owners"); + apply(&drive, operations, &transaction, platform_version); + + assert_eq!( + outcome, + StorageRefundCreditOutcome { + credited: BTreeMap::from([(first.id(), 1_001), (second.id(), 5_000)]), + routed_to_processing_pool: 0, + } + ); + assert_eq!( + balance(&drive, first_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 1_001) + ); + assert_eq!( + balance(&drive, second_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 5_000) + ); + } + + #[test] + fn should_report_refunds_for_an_owner_without_a_balance_instead_of_failing() { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let existing = insert_identity(&drive, 3, &transaction, platform_version); + let existing_id = existing.id().to_buffer(); + let missing_id = [0xAB; 32]; + assert_eq!( + balance(&drive, missing_id, &transaction, platform_version), + None + ); + + let fee_refunds = refunds(&[ + (existing_id, &[(1, 400)]), + (missing_id, &[(1, 250), (3, 50)]), + ]); + + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("an owner without a balance is reported, not an error"); + apply(&drive, operations, &transaction, platform_version); + + assert_eq!( + outcome, + StorageRefundCreditOutcome { + credited: BTreeMap::from([(existing.id(), 400)]), + routed_to_processing_pool: 300, + } + ); + assert_eq!(outcome.total(), Some(700)); + assert_eq!( + balance(&drive, existing_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 400) + ); + assert_eq!( + balance(&drive, missing_id, &transaction, platform_version), + None, + "no balance element is created for the missing owner" + ); + } + + #[test] + fn should_credit_an_owner_whose_keys_are_all_disabled() { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let frozen = insert_identity(&drive, 4, &transaction, platform_version); + let frozen_id = frozen.id().to_buffer(); + let key_ids = frozen.public_keys().keys().copied().collect::>(); + assert_eq!(key_ids.len(), 3); + drive + .disable_identity_keys( + frozen_id, + key_ids, + 1_000, + &BlockInfo::default(), + true, + Some(&transaction), + platform_version, + ) + .expect("expected to disable every key"); + + let fee_refunds = refunds(&[(frozen_id, &[(0, 12_345)])]); + + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("no key or permission is consulted"); + apply(&drive, operations, &transaction, platform_version); + + assert_eq!(outcome.credited, BTreeMap::from([(frozen.id(), 12_345)])); + assert_eq!(outcome.routed_to_processing_pool, 0); + assert_eq!( + balance(&drive, frozen_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 12_345) + ); + } + + #[test] + fn should_skip_the_owner_the_caller_settles_itself() { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let payer = insert_identity(&drive, 5, &transaction, platform_version); + let other = insert_identity(&drive, 6, &transaction, platform_version); + let payer_id = payer.id().to_buffer(); + let other_id = other.id().to_buffer(); + + let fee_refunds = refunds(&[(payer_id, &[(0, 900)]), (other_id, &[(0, 100)])]); + + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + Some(payer_id), + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to credit the other owner"); + apply(&drive, operations, &transaction, platform_version); + + assert_eq!(outcome.credited, BTreeMap::from([(other.id(), 100)])); + assert_eq!(outcome.routed_to_processing_pool, 0); + assert_eq!( + balance(&drive, payer_id, &transaction, platform_version), + Some(IDENTITY_BALANCE), + "the payer's refund folds into its own balance change elsewhere" + ); + assert_eq!( + balance(&drive, other_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 100) + ); + } + + #[test] + fn should_leave_total_credits_balanced_after_the_caller_records_the_pending_refunds() { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + let epoch = Epoch::new(0).expect("epoch 0"); + + let credited_owner = insert_identity(&drive, 7, &transaction, platform_version); + let credited_owner_id = credited_owner.id().to_buffer(); + let missing_owner_id = [0xCD; 32]; + + // Every credit in the system is accounted for before the refunds: two + // identity balances and a seeded processing pool. + let seed_operation = drive + .add_epoch_processing_credits_for_distribution_operation( + &epoch, + PROCESSING_POOL_SEED, + Some(&transaction), + platform_version, + ) + .expect("expected the pool seed operation"); + apply(&drive, vec![seed_operation], &transaction, platform_version); + drive + .add_to_system_credits( + IDENTITY_BALANCE + PROCESSING_POOL_SEED, + Some(&transaction), + platform_version, + ) + .expect("expected to record the system credits"); + assert!(drive + .calculate_total_credits_balance(Some(&transaction), &platform_version.drive) + .expect("expected the balance") + .ok() + .expect("expected a well-formed balance")); + + let fee_refunds = refunds(&[ + (credited_owner_id, &[(0, 100), (1, 50)]), + (missing_owner_id, &[(0, 70)]), + ]); + + // The primitive credits the owner that exists and reports the rest. + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to credit the owners"); + assert_eq!(outcome.routed_to_processing_pool, 70); + + // The caller routes the unrouted amount to the epoch's processing pool + // with one pool write and records every refund against its storage + // epoch, exactly as a lifecycle settlement does. + operations.push( + drive + .add_epoch_processing_credits_for_distribution_operation( + &epoch, + outcome.routed_to_processing_pool, + Some(&transaction), + platform_version, + ) + .expect("expected the pool write"), + ); + apply(&drive, operations, &transaction, platform_version); + + let mut pending_refund_operations: Vec = vec![]; + Drive::add_update_pending_epoch_refunds_operations( + &mut pending_refund_operations, + fee_refunds.sum_per_epoch(), + &platform_version.drive, + ) + .expect("expected the pending refund operations"); + drive + .apply_drive_operations( + pending_refund_operations, + true, + &BlockInfo::default(), + Some(&transaction), + platform_version, + None, + ) + .expect("expected to record the pending refunds"); + + assert_eq!( + balance(&drive, credited_owner_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 150) + ); + assert_eq!( + drive + .get_epoch_processing_credits_for_distribution( + &epoch, + Some(&transaction), + platform_version + ) + .expect("expected the pool balance"), + PROCESSING_POOL_SEED + 70 + ); + assert_eq!( + drive + .fetch_pending_epoch_refunds(Some(&transaction), &platform_version.drive) + .expect("expected the pending refunds"), + CreditsPerEpoch::from_iter([(0, 170), (1, 50)]) + ); + let total = drive + .calculate_total_credits_balance(Some(&transaction), &platform_version.drive) + .expect("expected the balance"); + assert!( + total.ok().expect("expected a well-formed balance"), + "credits moved between the pools and an identity balance must stay conserved: {:?}", + total + ); + } + + #[test] + fn should_not_be_active_before_the_new_drive_table() { + let drive = setup_drive_with_initial_state_structure(None); + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let transaction = drive.grove.start_transaction(); + + let fee_refunds = refunds(&[([1; 32], &[(0, 100)])]); + + let result = drive.credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut vec![], + frozen_platform_version, + ); + + assert!( + matches!( + result, + Err(Error::Drive(DriveError::VersionNotActive { .. })) + ), + "protocol version 14 has no lifecycle refund settlement, got {:?}", + result + ); + } +} From fea0e5782d3789211b4186b6983baba76e26a013 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:54:54 -0500 Subject: [PATCH 05/46] test(drive): close group actions through the production funnel with fee history Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- packages/rs-drive/src/drive/group/mod.rs | 175 ++++++++++++++++++++--- 1 file changed, 159 insertions(+), 16 deletions(-) diff --git a/packages/rs-drive/src/drive/group/mod.rs b/packages/rs-drive/src/drive/group/mod.rs index 9dfb73d09ee..c7973afdbd3 100644 --- a/packages/rs-drive/src/drive/group/mod.rs +++ b/packages/rs-drive/src/drive/group/mod.rs @@ -16,6 +16,10 @@ pub(crate) mod structure; #[cfg(feature = "server")] mod tests { use crate::drive::Drive; + use crate::error::drive::DriveError; + use crate::error::Error; + use crate::util::batch::drive_op_batch::GroupOperationType; + use crate::util::batch::DriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; use dpp::data_contract::accessors::v0::DataContractV0Getters; @@ -27,6 +31,8 @@ mod tests { use dpp::data_contract::group::Group; use dpp::data_contract::v1::DataContractV1; use dpp::data_contract::DataContract; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::fee::fee_result::FeeResult; use dpp::group::action_event::GroupActionEvent; use dpp::group::group_action::v0::GroupActionV0; use dpp::group::group_action::GroupAction; @@ -36,9 +42,52 @@ mod tests { use dpp::identity::Identity; use dpp::serialization::PlatformDeserializableTrusted; use dpp::tokens::token_event::TokenEvent; + use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; use std::collections::BTreeMap; + /// Closing a group action moves signer-flagged items out of the active + /// tree, and pricing that removal needs the fee history of the removing + /// block. Production reaches the closing branch only through + /// `apply_drive_operations`, which forwards the block's history; the + /// tests below use the same funnel. + fn fee_history() -> CachedEpochIndexFeeVersions { + BTreeMap::from([(0, FeeVersion::first())]) + } + + /// Closes `action_id` the way production does: as a group operation + /// applied through `apply_drive_operations` with the fee history. + #[allow(clippy::too_many_arguments)] + fn close_group_action_through_production_funnel( + drive: &Drive, + contract_id: Identifier, + initialize_with_insert_action_info: Option, + action_id: Identifier, + signer_identity_id: Identifier, + signer_power: u32, + platform_version: &PlatformVersion, + ) -> Result { + let history = fee_history(); + drive.apply_drive_operations( + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id, + group_contract_position: 0, + initialize_with_insert_action_info, + action_id, + signer_identity_id, + signer_power, + closes_group_action: true, + }, + )], + true, + &BlockInfo::default(), + None, + platform_version, + Some(&history), + ) + } + /// Helper to create a standard test contract with groups and tokens. fn create_test_contract_with_groups( identity_1_id: Identifier, @@ -575,22 +624,18 @@ mod tests { let platform_version = PlatformVersion::latest(); // Add second signer to bring total power to 3 (meets required_power) - // and close the action - drive - .add_group_action( - contract_id, - 0, - None, // no new action info, existing one will be moved - true, // closes_group_action - action_id, - identity_2_id, - 2, - &BlockInfo::default(), - true, - None, - platform_version, - ) - .expect("expected to close group action"); + // and close the action. Closing moves signer-flagged items, so it goes + // through the production funnel that carries the fee history. + close_group_action_through_production_funnel( + &drive, + contract_id, + None, // no new action info, existing one will be moved + action_id, + identity_2_id, + 2, + platform_version, + ) + .expect("expected to close group action"); // Verify the action is now closed let is_closed = drive @@ -680,6 +725,104 @@ mod tests { ); } + #[test] + fn should_refund_signer_bytes_when_a_group_action_closes() { + let (drive, contract_id, identity_1_id, identity_2_id, action_id) = + setup_drive_with_contract_and_action(); + let platform_version = PlatformVersion::latest(); + + // identity_1 opened the action: its signer sum item and the action + // info are flagged with identity_1 at epoch 0. Closing moves both out + // of the active tree into unflagged closed items, so identity_1 is + // refunded for the removed flagged bytes; identity_2's closing signer + // item is written unflagged and never refundable. + let fee_result = close_group_action_through_production_funnel( + &drive, + contract_id, + None, + action_id, + identity_2_id, + 2, + platform_version, + ) + .expect("expected to close group action"); + + let identity_1_refunds = fee_result + .fee_refunds + .get(identity_1_id.as_bytes()) + .expect("the opening signer's flagged bytes must be refunded"); + assert!( + identity_1_refunds + .get(&0) + .is_some_and(|credits| *credits > 0), + "the refund is recorded against the storage epoch of the moved items: {:?}", + identity_1_refunds + ); + assert!( + fee_result + .fee_refunds + .get(identity_2_id.as_bytes()) + .is_none(), + "the closing signer never stored flagged bytes" + ); + } + + #[test] + fn should_reject_closing_a_group_action_through_the_bare_wrapper_without_fee_history() { + // The bare fee-returning wrapper passes no fee history. Production + // never closes an action through it (the state transition funnel + // forwards the block's history), so from protocol version 15 a + // closing call through the wrapper is a misuse the strict refund rule + // surfaces; the frozen generation keeps pricing at the first + // generation's rates. + let (drive, contract_id, _identity_1_id, identity_2_id, action_id) = + setup_drive_with_contract_and_action(); + let platform_version = PlatformVersion::latest(); + + let result = drive.add_group_action( + contract_id, + 0, + None, + true, + action_id, + identity_2_id, + 2, + &BlockInfo::default(), + true, + None, + platform_version, + ); + + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "closing without fee history must be rejected at the latest version, got {:?}", + result + ); + + let (drive, contract_id, _identity_1_id, identity_2_id, action_id) = + setup_drive_with_contract_and_action(); + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + + drive + .add_group_action( + contract_id, + 0, + None, + true, + action_id, + identity_2_id, + 2, + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + } + #[test] fn should_close_group_action_with_new_action_info() { let drive = setup_drive_with_initial_state_structure(None); From 5c07b349770cacd36c71f1ab64e8dbfa315a6d9e Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:58:45 -0500 Subject: [PATCH 06/46] test(drive): pin the protocol 12 schema strip as the recorded refund exception Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- ...trip_unknown_document_schema_properties.rs | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs b/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs index f1427982d63..dc854c51d74 100644 --- a/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs +++ b/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs @@ -232,3 +232,205 @@ impl Drive { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::util::storage_flags::StorageFlags; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::data_contract::accessors::v0::DataContractV0Getters; + use dpp::identity::accessors::{IdentityGettersV0, IdentitySettersV0}; + use dpp::identity::Identity; + use dpp::platform_value::Value; + use dpp::tests::json_document::json_document_to_contract_with_ids; + use dpp::version::PlatformVersion; + + const OWNER_BALANCE: u64 = 5_000_000; + + /// The protocol 12 schema migration rewrote every stored user contract + /// whose document schemas carried top-level properties the v1 document + /// meta-schema forbids. It kept each element's storage flags and applied + /// the rewrite with a discarded cost vector, so the storage fee share of + /// the stripped bytes was never refunded to the contract owner and no + /// pending epoch refund was recorded. That is what every node executed + /// at the protocol 12 activation, and replay from genesis must reproduce + /// it byte for byte: the stripped byte counts were never recorded, so a + /// retroactive settlement is impossible, and the migration never runs + /// again, so there is nothing at a later version left to correct. The + /// storage refund invariant that applies from protocol version 15 names + /// this migration as its recorded historical exception; this test pins + /// the shipped behaviour so the exception stays exactly what it was. + #[test] + fn should_keep_the_protocol_12_schema_strip_frozen_without_refunding_stripped_bytes() { + let platform_version = PlatformVersion::get(12).expect("protocol version 12"); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let transaction = drive.grove.start_transaction(); + + // The contract owner exists with a balance, so a refund would be + // observable as a balance change. + let mut owner = + Identity::random_identity(2, Some(12), platform_version).expect("expected an identity"); + owner.set_balance(OWNER_BALANCE); + drive + .add_new_identity( + owner.clone(), + false, + &BlockInfo::default(), + true, + Some(&transaction), + platform_version, + ) + .expect("expected to insert the owner"); + drive + .add_to_system_credits(OWNER_BALANCE, Some(&transaction), platform_version) + .expect("expected to record the system credits"); + + // A user contract stored the way protocol version 12 stores it: the + // element carries the owner's storage flags. + let contract = json_document_to_contract_with_ids( + "tests/supporting_files/contract/family/family-contract.json", + None, + Some(owner.id()), + false, + platform_version, + ) + .expect("expected the family contract"); + drive + .insert_contract( + &contract, + BlockInfo::default(), + true, + Some(&transaction), + platform_version, + ) + .expect("expected to insert the contract"); + + let contract_id = contract.id(); + let contract_path = contract_root_path(contract_id.as_slice()); + let stored_element = drive + .grove_get_raw( + (&contract_path).into(), + &[0], + DirectQueryType::StatefulDirectQuery, + Some(&transaction), + &mut vec![], + &platform_version.drive, + ) + .expect("expected to read the contract element") + .expect("expected the contract element to exist"); + let (clean_bytes, flags) = match stored_element { + Element::Item(bytes, flags) => (bytes, flags), + other => panic!("expected an item, got {:?}", other), + }; + let owner_flags = StorageFlags::from_element_flags_ref( + flags + .as_ref() + .expect("a user contract carries storage flags"), + ) + .expect("expected valid flags") + .expect("expected owner flags"); + assert_eq!( + owner_flags.owner_id(), + Some(&owner.id().to_buffer()), + "the contract bytes are attributed to the owner" + ); + + // Rewrite the stored bytes the way a pre-v12 contract could look: + // with a top-level document schema property the v1 meta-schema does + // not allow. The element keeps its flags and grows. + let bincode_config = bincode::config::standard() + .with_big_endian() + .with_no_limit(); + let (mut serialization_format, _): (DataContractInSerializationFormat, usize) = + bincode::borrow_decode_from_slice(&clean_bytes, bincode_config) + .expect("expected to decode the stored contract"); + let person_schema = serialization_format + .document_schemas_mut() + .expect("expected the schemas of a format version 1 contract") + .get_mut("person") + .expect("expected the person document type"); + person_schema + .insert("legacyUnknownProperty".to_string(), Value::Bool(true)) + .expect("expected to add the unknown property"); + let inflated_bytes = bincode::encode_to_vec(&serialization_format, bincode_config) + .expect("expected to encode the inflated contract"); + assert!(inflated_bytes.len() > clean_bytes.len()); + drive + .grove_insert( + (&contract_path).into(), + &[0], + Element::Item(inflated_bytes.clone(), flags.clone()), + Some(&transaction), + None, + &mut vec![], + &platform_version.drive, + ) + .expect("expected to store the inflated contract"); + drive.cache.data_contracts.clear(); + + let pending_refunds_before = drive + .fetch_pending_epoch_refunds(Some(&transaction), &platform_version.drive) + .expect("expected the pending refunds"); + assert!(pending_refunds_before.is_empty()); + + // The migration, exactly as the protocol 12 activation ran it. + drive + .strip_unknown_document_schema_properties(&transaction, &platform_version.drive) + .expect("expected the migration to succeed"); + + let migrated_element = drive + .grove_get_raw( + (&contract_path).into(), + &[0], + DirectQueryType::StatefulDirectQuery, + Some(&transaction), + &mut vec![], + &platform_version.drive, + ) + .expect("expected to read the migrated element") + .expect("expected the migrated element to exist"); + let (migrated_bytes, migrated_flags) = match migrated_element { + Element::Item(bytes, flags) => (bytes, flags), + other => panic!("expected an item, got {:?}", other), + }; + assert!( + migrated_bytes.len() < inflated_bytes.len(), + "the migration strips the unknown property, so the element shrinks" + ); + assert_eq!( + migrated_bytes, clean_bytes, + "the migration restores the bytes the contract had without the property" + ); + assert_eq!( + migrated_flags, flags, + "the element keeps the owner's storage flags byte for byte" + ); + + // The stripped bytes were owner-paid, yet nothing was refunded: the + // owner's balance and the pending epoch refunds are untouched and the + // credit sum stays balanced. + assert_eq!( + drive + .fetch_identity_balance( + owner.id().to_buffer(), + Some(&transaction), + platform_version + ) + .expect("expected the owner's balance"), + Some(OWNER_BALANCE) + ); + assert!(drive + .fetch_pending_epoch_refunds(Some(&transaction), &platform_version.drive) + .expect("expected the pending refunds") + .is_empty()); + let total = drive + .calculate_total_credits_balance(Some(&transaction), &platform_version.drive) + .expect("expected the total credits balance"); + assert!( + total.ok().expect("expected a well-formed balance"), + "no credits move during the migration: {:?}", + total + ); + } +} From b08772487e3230c306a614a5ddc9e5f50a22e586 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 01:58:45 -0500 Subject: [PATCH 07/46] docs(book): describe fee history and refund ownership from protocol version 15 Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- book/src/fees/overview.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/book/src/fees/overview.md b/book/src/fees/overview.md index 2a0ca963e16..26ee71f6fa7 100644 --- a/book/src/fees/overview.md +++ b/book/src/fees/overview.md @@ -485,6 +485,34 @@ out to proposers. There is a **dust limit**: refunds below 32 bytes worth of storage credits are discarded to prevent micro-refund spam. +### Fee history and refund ownership (protocol version 15 onward) + +A refund is priced with the fee history of the block that removes the bytes: +the `previous_fee_versions` map platform state carries, which the epoch change +hook extends whenever the fee version number changes. `Drive::calculate_fee` +v1 (`DRIVE_VERSION_V10`) consults that history for every owner-attributed +storage removal, on every fee version number, and returns an internal error +when a caller passes none. Earlier generations priced fee version number 1 +against an empty history, so a caller that forgot the history silently +refunded at the first generation's storage rates; from protocol version 15 +that omission halts instead of mispricing. Every shipped schedule shares fee +version number 1 and the same storage rates, so the credits themselves are +unchanged for every shipped input. + +Refunds follow the recorded owner in the element's storage flags. +`Drive::credit_storage_refunds_to_owners_operations` credits each owner that +has a balance element without consulting any key or permission, so a frozen +but existing owner still receives its bookkeeping refund, and reports the +amount whose owner has no balance element (the native stand-in for a wiped +owner) for the caller to move into the current epoch's processing pool with a +single pool write. The caller records every refund against its storage epoch +in the pending epoch refunds, so the credit conservation check stays balanced. +Block lifecycle paths that remove owner-attributed bytes settle their refunds +this way in the block that removes them. The protocol 12 schema migration, +which shrank stored contracts without refunding the stripped bytes, ran once +at that activation and is the recorded historical exception; it replays +exactly as executed. + ## Epoch-Based Fee Distribution Fees do not go directly to the block proposer. Instead, they accumulate in @@ -571,6 +599,15 @@ Fee versions are stored in the `FEE_VERSIONS` array and looked up by number. The `uses_version_fee_multiplier_permille` field allows a global scaling factor (permille = divide by 1000; a value of 1000 means no change). +`fee_version_number` keys the persisted fee history that refunds are priced +against. A schedule that changes storage rates needs a new number, because the +refund code resolves the schedule for an epoch through the history and (in +generations before protocol version 15) shortcut number 1 to the first +generation's rates. `FEE_VERSION1` and `FEE_VERSION2` share number 1 because +only a non-storage group changed between them; a test in `rs-drive`'s fee +operation module pins that every shipped schedule keeps the first generation's +storage rates. + ## Key Source Files | File | Contents | From 39cbdb7f892b8822c371eab48b571a64ccbdbca2 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 05:26:09 -0500 Subject: [PATCH 08/46] test(drive): pass the fee history where tests replace epoch-flagged documents Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../insert_contract/v0/tests/ranked_index_e2e_tests.rs | 8 +++++++- packages/rs-drive/src/drive/document/update/mod.rs | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs index 188df96a535..d41b3eb88db 100644 --- a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs +++ b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/ranked_index_e2e_tests.rs @@ -77,10 +77,12 @@ use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::random_document::CreateRandomDocument; use dpp::data_contract::DataContractFactory; use dpp::document::{Document, DocumentV0Getters, DocumentV0Setters}; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::identifier::Identifier; use dpp::platform_value::{platform_value, Value}; use dpp::prelude::DataContract; use dpp::tests::json_document::json_document_to_contract; +use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; use grovedb::element::indexed::AVG_FIXED_POINT_SCALE; use grovedb::Element; @@ -1355,6 +1357,10 @@ fn estimated_and_actual_update_fees( .document_type_for_name(document_type_name) .unwrap_or_else(|_| panic!("{document_type_name} doctype exists")); let storage_flags = Some(Cow::Owned(StorageFlags::SingleEpoch(0))); + // The replaced element carries epoch flags, so pricing its removal needs + // the fee history of the removing block, as every production caller + // passes. + let fee_history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); let run = |apply: bool| { drive @@ -1368,7 +1374,7 @@ fn estimated_and_actual_update_fees( storage_flags.clone(), None, pv, - None, + Some(&fee_history), ) .unwrap_or_else(|e| { panic!("expected the {document_type_name} update (apply={apply}) to succeed: {e}") diff --git a/packages/rs-drive/src/drive/document/update/mod.rs b/packages/rs-drive/src/drive/document/update/mod.rs index 63cccb00bbc..1769d95ac50 100644 --- a/packages/rs-drive/src/drive/document/update/mod.rs +++ b/packages/rs-drive/src/drive/document/update/mod.rs @@ -3484,7 +3484,7 @@ mod tests { storage_flags, None, platform_version, - None, + Some(&EPOCH_CHANGE_FEE_VERSION_TEST), ) .expect("key-changing update on aggregate index must succeed"); From 2c8f0fcaaa8e437da75965a5f8cc56db8d36d789 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 05:30:05 -0500 Subject: [PATCH 09/46] test(drive): name the per-owner refund fixtures for clippy Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../v0/mod.rs | 5 ++++- packages/rs-drive/src/fees/op.rs | 9 +++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index 987ed775e69..c1903e1bacc 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -96,6 +96,9 @@ mod tests { const IDENTITY_BALANCE: Credits = 10_000_000; const PROCESSING_POOL_SEED: Credits = 1_000_000; + /// Refund credits per storage epoch, per owner. + type RefundsByOwner<'a> = [([u8; 32], &'a [(u16, Credits)])]; + fn insert_identity( drive: &Drive, seed: u64, @@ -118,7 +121,7 @@ mod tests { identity } - fn refunds(entries: &[([u8; 32], &[(u16, Credits)])]) -> FeeRefunds { + fn refunds(entries: &RefundsByOwner) -> FeeRefunds { let mut fee_refunds = FeeRefunds::default(); for (owner, credits_per_epoch) in entries { let mut epochs = CreditsPerEpoch::default(); diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index 9978e923447..0328a2312a9 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -3084,6 +3084,9 @@ mod tests { const OWNER: [u8; 32] = [7; 32]; const OTHER_OWNER: [u8; 32] = [9; 32]; + /// Removed bytes per storage epoch, per owner. + type BytesByOwner<'a> = [([u8; 32], &'a [(u16, u32)])]; + /// A schedule no protocol version references, with doubled storage /// rates, so a test can tell "the history was consulted" apart from /// "the first generation's rates were used". @@ -3100,9 +3103,7 @@ mod tests { /// One removed element whose bytes are attributed per owner and per /// storage epoch, the shape grovedb reports for an element carrying /// owner storage flags. - fn sectioned_removal( - bytes_by_owner: &[([u8; 32], &[(u16, u32)])], - ) -> LowLevelDriveOperation { + fn sectioned_removal(bytes_by_owner: &BytesByOwner) -> LowLevelDriveOperation { let mut removal = StorageRemovalPerEpochByIdentifier::default(); for (owner, bytes_per_epoch) in bytes_by_owner { let owner_bytes = removal.entry(*owner).or_default(); @@ -3296,7 +3297,7 @@ mod tests { // equal v0's: the boundary changes what a missing history does, // not what a present one yields. let removal_epoch = 15u16; - let bytes_by_owner: &[([u8; 32], &[(u16, u32)])] = &[ + let bytes_by_owner: &BytesByOwner = &[ (OWNER, &[(0, 900), (3, 1200), (7, 64), (12, 5000)]), (OTHER_OWNER, &[(5, 31), (11, 2048)]), (Identifier::default(), &[(2, 700)]), From 6b0eeb7de3758511ec099bf7bd882644f37338f6 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 15:49:49 -0500 Subject: [PATCH 10/46] fix(drive): report refund credits that repay identity debt for the processing pool add_to_identity_balance_operations clears negative credit before raising a zero balance, and that share never reaches the credit sum trees. The refund primitive now measures it, reports it as repaid_debt beside the unrouted amount, and exposes processing_pool_share() for the caller's single pool write. Tests cover refunds below, equal to and above the outstanding debt with the conservation check, and a positive balance that repays nothing. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../mod.rs | 21 +- .../v0/mod.rs | 279 +++++++++++++++++- .../storage_refund_credit_outcome/mod.rs | 29 +- 3 files changed, 310 insertions(+), 19 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs index 1e107fe2692..2230977b31b 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs @@ -17,12 +17,17 @@ impl Drive { /// per-epoch credits are summed with checked arithmetic. An owner with a /// balance element is credited through `add_to_identity_balance_operations`; /// no key, signature or permission is consulted, so a frozen but existing - /// owner receives its bookkeeping refund. An owner without a balance - /// element (the native proxy for a wiped owner) is not credited and its - /// amount is reported as `routed_to_processing_pool`, for the caller to - /// settle into the current epoch's processing pool with one pool write. - /// The primitive records no pending refunds; the caller does, so the block - /// keeps a single pending-refund and pool write per batch. + /// owner receives its bookkeeping refund. When that owner's balance is + /// zero the helper first clears its negative credit (identity debt), and + /// only the remainder reaches the balance; the cleared debt is reported as + /// `repaid_debt` because debt lives outside the credit sum trees and is + /// processing fee the pools were short of when it was incurred. An owner + /// without a balance element (the native proxy for a wiped owner) is not + /// credited and its amount is reported as `routed_to_processing_pool`. + /// The caller settles `processing_pool_share()` (both amounts) into the + /// current epoch's processing pool with one pool write and records the + /// pending refunds; the primitive does neither, so the block keeps a + /// single pending-refund and pool write per batch. /// /// # Parameters /// @@ -37,8 +42,8 @@ impl Drive { /// /// # Returns /// - /// * `Ok(StorageRefundCreditOutcome)` - The owners credited and the amount - /// routed to the processing pool. + /// * `Ok(StorageRefundCreditOutcome)` - The owners credited, the debt the + /// refunds repaid and the amount routed to the processing pool. /// * `Err(Error)` - On overflow, a corrupted balance element, or when the /// method is not active for the platform version. pub fn credit_storage_refunds_to_owners_operations( diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index c1903e1bacc..03738a878b2 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -1,5 +1,6 @@ use crate::drive::identity::update::storage_refund_credit_outcome::StorageRefundCreditOutcome; use crate::drive::Drive; +use crate::error::drive::DriveError; use crate::error::Error; use crate::fees::op::LowLevelDriveOperation; use crate::util::estimated_layers::EstimatedLayers; @@ -24,6 +25,7 @@ impl Drive { platform_version: &PlatformVersion, ) -> Result { let mut credited: BTreeMap = BTreeMap::new(); + let mut repaid_debt: Credits = 0; let mut routed_to_processing_pool: Credits = 0; for (owner_id, credits_per_epoch) in fee_refunds.iter() { @@ -52,7 +54,27 @@ impl Drive { platform_version, )?; - if existing_balance.is_some() { + if let Some(existing_balance) = existing_balance { + // `add_to_identity_balance_operations` clears negative credit before + // raising a zero balance. That portion never reaches the sum trees, + // so it is measured here for the caller's processing pool write. + let owner_repaid_debt = if existing_balance == 0 { + let debt = self + .fetch_identity_negative_balance_operations( + *owner_id, + true, + transaction, + drive_operations, + platform_version, + )? + .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( + "an identity with a balance element always has a negative credit element", + )))?; + debt.min(credits) + } else { + 0 + }; + let mut estimated_costs_only_with_layer_info = None::; drive_operations.extend(self.add_to_identity_balance_operations( @@ -63,7 +85,17 @@ impl Drive { platform_version, )?); - credited.insert(Identifier::from(*owner_id), credits); + repaid_debt = + repaid_debt + .checked_add(owner_repaid_debt) + .ok_or(ProtocolError::Overflow( + "storage refund credits repaying identity debt overflow", + ))?; + + let reached_balance = credits - owner_repaid_debt; + if reached_balance > 0 { + credited.insert(Identifier::from(*owner_id), reached_balance); + } } else { routed_to_processing_pool = routed_to_processing_pool.checked_add(credits).ok_or( ProtocolError::Overflow( @@ -75,6 +107,7 @@ impl Drive { Ok(StorageRefundCreditOutcome { credited, + repaid_debt, routed_to_processing_pool, }) } @@ -83,7 +116,6 @@ impl Drive { #[cfg(test)] mod tests { use super::*; - use crate::error::drive::DriveError; use crate::util::batch::DriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; @@ -121,6 +153,150 @@ mod tests { identity } + /// An identity with a zero balance and `debt` of negative credit, the + /// state an identity is left in after paying a processing fee it could + /// not fully cover. + fn insert_identity_with_debt( + drive: &Drive, + seed: u64, + debt: Credits, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Identity { + let mut identity = Identity::random_identity(3, Some(seed), platform_version) + .expect("expected a random identity"); + identity.set_balance(0); + drive + .add_new_identity( + identity.clone(), + false, + &BlockInfo::default(), + true, + Some(transaction), + platform_version, + ) + .expect("expected to insert the identity"); + apply( + drive, + vec![ + drive.update_identity_negative_credit_operation_v0(identity.id().to_buffer(), debt) + ], + transaction, + platform_version, + ); + identity + } + + fn debt( + drive: &Drive, + identity_id: [u8; 32], + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Option { + drive + .fetch_identity_negative_balance_operations( + identity_id, + true, + Some(transaction), + &mut vec![], + platform_version, + ) + .expect("expected to fetch the negative credit") + } + + /// Settles a refund for an identity with a zero balance and `debt` of + /// negative credit exactly as a lifecycle caller would (credits, one pool + /// write for the pool share, pending refunds recorded) and returns the + /// outcome, the identity's balance and debt afterwards, and whether the + /// credit sum is balanced. + fn settle_refund_against_debt( + debt_amount: Credits, + refund: Credits, + ) -> ( + StorageRefundCreditOutcome, + Option, + Option, + bool, + ) { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + let epoch = Epoch::new(0).expect("epoch 0"); + + let owner = + insert_identity_with_debt(&drive, 8, debt_amount, &transaction, platform_version); + let owner_id = owner.id().to_buffer(); + + let seed_operation = drive + .add_epoch_processing_credits_for_distribution_operation( + &epoch, + PROCESSING_POOL_SEED, + Some(&transaction), + platform_version, + ) + .expect("expected the pool seed operation"); + apply(&drive, vec![seed_operation], &transaction, platform_version); + drive + .add_to_system_credits(PROCESSING_POOL_SEED, Some(&transaction), platform_version) + .expect("expected to record the system credits"); + + let fee_refunds = refunds(&[(owner_id, &[(0, refund)])]); + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to settle the refund"); + operations.push( + drive + .add_epoch_processing_credits_for_distribution_operation( + &epoch, + outcome + .processing_pool_share() + .expect("expected the pool share"), + Some(&transaction), + platform_version, + ) + .expect("expected the pool write"), + ); + apply(&drive, operations, &transaction, platform_version); + + let mut pending_refund_operations: Vec = vec![]; + Drive::add_update_pending_epoch_refunds_operations( + &mut pending_refund_operations, + fee_refunds.sum_per_epoch(), + &platform_version.drive, + ) + .expect("expected the pending refund operations"); + drive + .apply_drive_operations( + pending_refund_operations, + true, + &BlockInfo::default(), + Some(&transaction), + platform_version, + None, + ) + .expect("expected to record the pending refunds"); + + let balanced = drive + .calculate_total_credits_balance(Some(&transaction), &platform_version.drive) + .expect("expected the balance") + .ok() + .expect("expected a well-formed balance"); + + ( + outcome, + balance(&drive, owner_id, &transaction, platform_version), + debt(&drive, owner_id, &transaction, platform_version), + balanced, + ) + } + fn refunds(entries: &RefundsByOwner) -> FeeRefunds { let mut fee_refunds = FeeRefunds::default(); for (owner, credits_per_epoch) in entries { @@ -193,6 +369,7 @@ mod tests { outcome, StorageRefundCreditOutcome { credited: BTreeMap::from([(first.id(), 1_001), (second.id(), 5_000)]), + repaid_debt: 0, routed_to_processing_pool: 0, } ); @@ -241,6 +418,7 @@ mod tests { outcome, StorageRefundCreditOutcome { credited: BTreeMap::from([(existing.id(), 400)]), + repaid_debt: 0, routed_to_processing_pool: 300, } ); @@ -398,7 +576,9 @@ mod tests { drive .add_epoch_processing_credits_for_distribution_operation( &epoch, - outcome.routed_to_processing_pool, + outcome + .processing_pool_share() + .expect("expected the pool share"), Some(&transaction), platform_version, ) @@ -454,6 +634,97 @@ mod tests { ); } + #[test] + fn should_report_a_refund_below_the_owners_debt_as_repaid_debt() { + let (outcome, balance, debt, balanced) = settle_refund_against_debt(100, 60); + + assert_eq!( + outcome, + StorageRefundCreditOutcome { + credited: BTreeMap::new(), + repaid_debt: 60, + routed_to_processing_pool: 0, + }, + "nothing reaches the balance; the whole refund clears debt" + ); + assert_eq!(balance, Some(0)); + assert_eq!(debt, Some(40)); + assert!(balanced, "the repaid debt went to the processing pool"); + } + + #[test] + fn should_report_a_refund_equal_to_the_owners_debt_as_repaid_debt() { + let (outcome, balance, debt, balanced) = settle_refund_against_debt(100, 100); + + assert_eq!( + outcome, + StorageRefundCreditOutcome { + credited: BTreeMap::new(), + repaid_debt: 100, + routed_to_processing_pool: 0, + } + ); + assert_eq!(balance, Some(0)); + assert_eq!(debt, Some(0)); + assert!(balanced); + } + + #[test] + fn should_split_a_refund_above_the_owners_debt_between_debt_and_balance() { + let (outcome, balance, debt, balanced) = settle_refund_against_debt(100, 150); + + let owner = Identity::random_identity(3, Some(8), PlatformVersion::latest()) + .expect("expected the same random identity") + .id(); + assert_eq!( + outcome, + StorageRefundCreditOutcome { + credited: BTreeMap::from([(owner, 50)]), + repaid_debt: 100, + routed_to_processing_pool: 0, + }, + "only the remainder above the debt is reported as credited" + ); + assert_eq!(outcome.processing_pool_share(), Some(100)); + assert_eq!(outcome.total(), Some(150)); + assert_eq!(balance, Some(50)); + assert_eq!(debt, Some(0)); + assert!(balanced); + } + + #[test] + fn should_not_touch_debt_when_the_owner_has_a_positive_balance() { + // The shipped helper repays debt only from a zero balance; an owner + // that is in debt yet holds a balance cannot exist through the fee + // path, but the primitive must still report what the helper does. + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let owner = insert_identity(&drive, 9, &transaction, platform_version); + let owner_id = owner.id().to_buffer(); + + let fee_refunds = refunds(&[(owner_id, &[(0, 250)])]); + let mut operations = vec![]; + let outcome = drive + .credit_storage_refunds_to_owners_operations( + &fee_refunds, + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to credit the owner"); + apply(&drive, operations, &transaction, platform_version); + + assert_eq!(outcome.repaid_debt, 0); + assert_eq!(outcome.credited, BTreeMap::from([(owner.id(), 250)])); + assert_eq!( + balance(&drive, owner_id, &transaction, platform_version), + Some(IDENTITY_BALANCE + 250) + ); + } + #[test] fn should_not_be_active_before_the_new_drive_table() { let drive = setup_drive_with_initial_state_structure(None); diff --git a/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs b/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs index 2e42e9bd7b2..b80f1d38cab 100644 --- a/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/structs/storage_refund_credit_outcome/mod.rs @@ -3,18 +3,26 @@ use dpp::prelude::Identifier; use std::collections::BTreeMap; /// What `Drive::credit_storage_refunds_to_owners_operations` did with a set of -/// storage refunds: which recorded owners were credited and how much could not -/// be routed to any owner. +/// storage refunds: which recorded owners were credited, how much of the +/// refunds cleared identity debt instead of reaching a balance, and how much +/// could not be routed to any owner. /// -/// The caller that knows the block's epoch settles `routed_to_processing_pool` +/// The caller that knows the block's epoch settles `processing_pool_share()` /// into that epoch's processing pool with a single pool write and records the /// refunds against their storage epochs; the primitive itself never touches /// the pools. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct StorageRefundCreditOutcome { - /// Credits added to each recorded owner's balance, summed over the epochs - /// the owner's bytes were stored in. + /// Credits that reached each recorded owner's balance element, summed + /// over the epochs the owner's bytes were stored in. An owner whose whole + /// refund went into clearing debt has no entry. pub credited: BTreeMap, + /// Credits that cleared an owner's negative credit (identity debt) instead + /// of raising its balance. Debt is processing fee the pools were short of + /// when it was incurred and it lives outside the credit sum trees, so the + /// caller moves this amount into the current epoch's processing pool to + /// keep the credit sum balanced. + pub repaid_debt: Credits, /// Credits whose recorded owner has no balance element. Until a typed /// owner encoding exists this is the native proxy for a wiped owner, so /// the caller moves this amount into the current epoch's processing pool. @@ -22,11 +30,18 @@ pub struct StorageRefundCreditOutcome { } impl StorageRefundCreditOutcome { - /// The total credited to owners plus the amount routed to the pool. + /// What the caller writes into the current epoch's processing pool: the + /// unrouted refunds plus the debt they repaid. + pub fn processing_pool_share(&self) -> Option { + self.routed_to_processing_pool.checked_add(self.repaid_debt) + } + + /// The total settled: credited to balances, repaid as debt, or routed to + /// the pool. pub fn total(&self) -> Option { self.credited .values() - .try_fold(self.routed_to_processing_pool, |total, credits| { + .try_fold(self.processing_pool_share()?, |total, credits| { total.checked_add(*credits) }) } From f6448f2b1f438575086deac2e3924ea7973739ad Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 15:49:49 -0500 Subject: [PATCH 11/46] docs(book): say lifecycle refund settlement is not yet wired at protocol version 15 Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- book/src/fees/overview.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/book/src/fees/overview.md b/book/src/fees/overview.md index 26ee71f6fa7..6d777351dd8 100644 --- a/book/src/fees/overview.md +++ b/book/src/fees/overview.md @@ -502,16 +502,20 @@ unchanged for every shipped input. Refunds follow the recorded owner in the element's storage flags. `Drive::credit_storage_refunds_to_owners_operations` credits each owner that has a balance element without consulting any key or permission, so a frozen -but existing owner still receives its bookkeeping refund, and reports the -amount whose owner has no balance element (the native stand-in for a wiped -owner) for the caller to move into the current epoch's processing pool with a -single pool write. The caller records every refund against its storage epoch -in the pending epoch refunds, so the credit conservation check stays balanced. -Block lifecycle paths that remove owner-attributed bytes settle their refunds -this way in the block that removes them. The protocol 12 schema migration, -which shrank stored contracts without refunding the stripped bytes, ran once -at that activation and is the recorded historical exception; it replays -exactly as executed. +but existing owner still receives its bookkeeping refund. Two shares of a +refund never reach a balance and are reported for the caller instead: the +part that clears an owner's negative credit (identity debt, which lives +outside the credit sum trees) and the part whose owner has no balance element +(the native stand-in for a wiped owner). The caller moves both into the +current epoch's processing pool with a single pool write and records every +refund against its storage epoch in the pending epoch refunds, so the credit +conservation check stays balanced. This primitive is the settlement step +block lifecycle paths that remove owner-attributed bytes are meant to use in +the block that removes them; at protocol version 15 the vote poll end cleanup +does not yet price or settle its refunds, and wiring it up is a separate +change. The protocol 12 schema migration, which shrank stored contracts +without refunding the stripped bytes, ran once at that activation and is the +recorded historical exception; it replays exactly as executed. ## Epoch-Based Fee Distribution From bc113d8bf111781b27c17df973aaa8441468e972 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 16:52:20 -0500 Subject: [PATCH 12/46] docs(book): attribute storage refunds to the recorded owner in the fee overview Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- book/src/fees/overview.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/book/src/fees/overview.md b/book/src/fees/overview.md index 6d777351dd8..781ab05b9f4 100644 --- a/book/src/fees/overview.md +++ b/book/src/fees/overview.md @@ -49,8 +49,9 @@ in `FeeStorageVersion`: | `storage_seek_cost` | 2,000 | Cost of a single disk seek | Storage fees are **refundable**: when data is deleted, a portion of the original -storage fee is returned to the identity that paid it (see [Refunds](#refunds) -below). +storage fee becomes a refund for the owner recorded in the stored bytes' storage +flags, which is not always the identity that paid the fee (see +[Refunds](#refunds) below). The documents of a type that declares a `ttl` (protocol version 14) are the exception: they carry no storage flags and refund nothing, their bytes are priced for the time they live, and From 2fc11c14ca8169b45c2ee5a1dcec06840ac909e6 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sat, 12 Sep 2026 18:53:36 -0500 Subject: [PATCH 13/46] refactor(drive): read each refund owner once when crediting its balance The balance read that decides whether the owner exists now feeds add_to_previous_balance directly, followed by the balance and negative credit update operations, the same shape the payer's own refund uses. The shipped helper reads the negative credit only from a zero balance, so an owner costs one or two stateful reads instead of up to four, and the repaid debt is derived from the helper's outcome instead of a separate read. A test pins the read count per owner. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../mod.rs | 8 +- .../v0/mod.rs | 107 +++++++++++++----- 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs index 2230977b31b..6fe378b6e46 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/mod.rs @@ -15,9 +15,11 @@ impl Drive { /// /// For every owner in `fee_refunds` except `skip_owner`, the owner's /// per-epoch credits are summed with checked arithmetic. An owner with a - /// balance element is credited through `add_to_identity_balance_operations`; - /// no key, signature or permission is consulted, so a frozen but existing - /// owner receives its bookkeeping refund. When that owner's balance is + /// balance element is credited the way a state transition payer's own + /// refund is (one balance read feeding `add_to_previous_balance`, then the + /// balance and negative credit updates); no key, signature or permission + /// is consulted, so a frozen but existing owner receives its bookkeeping + /// refund. When that owner's balance is /// zero the helper first clears its negative credit (identity debt), and /// only the remainder reaches the balance; the cleared debt is reported as /// `repaid_debt` because debt lives outside the credit sum trees and is diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index 03738a878b2..aba13581111 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -1,6 +1,6 @@ +use crate::drive::identity::update::add_to_previous_balance_outcome::AddToPreviousBalanceOutcomeV0Methods; use crate::drive::identity::update::storage_refund_credit_outcome::StorageRefundCreditOutcome; use crate::drive::Drive; -use crate::error::drive::DriveError; use crate::error::Error; use crate::fees::op::LowLevelDriveOperation; use crate::util::estimated_layers::EstimatedLayers; @@ -55,35 +55,48 @@ impl Drive { )?; if let Some(existing_balance) = existing_balance { - // `add_to_identity_balance_operations` clears negative credit before - // raising a zero balance. That portion never reaches the sum trees, - // so it is measured here for the caller's processing pool write. - let owner_repaid_debt = if existing_balance == 0 { - let debt = self - .fetch_identity_negative_balance_operations( - *owner_id, - true, - transaction, - drive_operations, - platform_version, - )? - .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( - "an identity with a balance element always has a negative credit element", - )))?; - debt.min(credits) - } else { - 0 - }; - - let mut estimated_costs_only_with_layer_info = None::; - - drive_operations.extend(self.add_to_identity_balance_operations( + // The same shape as the payer's own credit in + // `apply_balance_change_from_fee_to_identity`: the balance read above + // feeds the shipped helper directly, which reads the negative credit + // itself only when the balance is zero, so nothing is read twice. + let outcome = self.add_to_previous_balance( *owner_id, + existing_balance, credits, - &mut estimated_costs_only_with_layer_info, + true, transaction, + drive_operations, platform_version, - )?); + )?; + + if let Some(new_balance) = outcome.balance_modified() { + drive_operations + .push(self.update_identity_balance_operation_v0(*owner_id, new_balance)?); + } + + if let Some(new_negative_balance) = outcome.negative_credit_balance_modified() { + drive_operations.push(self.update_identity_negative_credit_operation_v0( + *owner_id, + new_negative_balance, + )); + } + + // From a zero balance the helper clears negative credit first and only + // the remainder becomes the new balance; from a positive balance the + // whole refund is added. Whatever did not reach the balance repaid + // debt, which lives outside the sum trees and is reported for the + // caller's processing pool write. + let reached_balance = if existing_balance == 0 { + outcome.balance_modified().unwrap_or(0) + } else { + credits + }; + let owner_repaid_debt = + credits + .checked_sub(reached_balance) + .ok_or(ProtocolError::Overflow( + "a storage refund cannot raise a balance by more than the refund", + ))?; repaid_debt = repaid_debt @@ -92,7 +105,6 @@ impl Drive { "storage refund credits repaying identity debt overflow", ))?; - let reached_balance = credits - owner_repaid_debt; if reached_balance > 0 { credited.insert(Identifier::from(*owner_id), reached_balance); } @@ -116,6 +128,7 @@ impl Drive { #[cfg(test)] mod tests { use super::*; + use crate::error::drive::DriveError; use crate::util::batch::DriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; @@ -725,6 +738,46 @@ mod tests { ); } + #[test] + fn should_read_each_owner_once_before_writing_its_balance() { + // The balance read decides whether the owner exists and then feeds the + // shipped helper, which reads the negative credit only from a zero + // balance: two stateful reads for an owner in debt, one otherwise. + // A second read of either element would show up as an extra cost + // operation and be billed to every refund the caller settles. + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let transaction = drive.grove.start_transaction(); + + let funded = insert_identity(&drive, 10, &transaction, platform_version); + let in_debt = insert_identity_with_debt(&drive, 11, 100, &transaction, platform_version); + + let count_reads = |owner_id: [u8; 32]| { + let mut operations = vec![]; + drive + .credit_storage_refunds_to_owners_operations( + &refunds(&[(owner_id, &[(0, 150)])]), + None, + Some(&transaction), + &mut operations, + platform_version, + ) + .expect("expected to credit the owner"); + operations + .iter() + .filter(|operation| { + matches!( + operation, + LowLevelDriveOperation::CalculatedCostOperation(_) + ) + }) + .count() + }; + + assert_eq!(count_reads(funded.id().to_buffer()), 1); + assert_eq!(count_reads(in_debt.id().to_buffer()), 2); + } + #[test] fn should_not_be_active_before_the_new_drive_table() { let drive = setup_drive_with_initial_state_structure(None); From 346d481a1877f8c47a5c07dd382bf7071234d6b0 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 22 Sep 2026 13:17:47 -0500 Subject: [PATCH 14/46] test(drive): unban, unsuspend and replace suspensions through the production funnel with fee history The contract moderation tests that landed on the base after this branch was cut remove or shrink moderator-flagged entries through the bare fee-returning wrappers, which pass no fee history. From protocol version 15 pricing such a removal without the history is an error, so those calls now go through apply_drive_operations with a history, the funnel production uses; calls that only insert keep using the wrappers. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../src/drive/contract/moderation/tests.rs | 328 ++++++++++++------ 1 file changed, 215 insertions(+), 113 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/moderation/tests.rs b/packages/rs-drive/src/drive/contract/moderation/tests.rs index 611a942210b..326bdee33cc 100644 --- a/packages/rs-drive/src/drive/contract/moderation/tests.rs +++ b/packages/rs-drive/src/drive/contract/moderation/tests.rs @@ -6,6 +6,9 @@ use crate::drive::contract::paths::{ CONTRACT_VERSION_KEY, CONTRACT_WARNINGS_KEY, }; use crate::drive::{Drive, RootTree}; +use crate::error::Error; +use crate::util::batch::drive_op_batch::ContractModerationOperationType; +use crate::util::batch::DriveOperation; use crate::util::grove_operations::DirectQueryType; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; @@ -17,10 +20,14 @@ use dpp::data_contract::config::moderation::{ ContractWarning, }; use dpp::data_contract::DataContract; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::fee::fee_result::FeeResult; use dpp::identifier::Identifier; use dpp::tests::fixtures::get_data_contract_fixture; +use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; use grovedb::Element; +use std::collections::BTreeMap; fn reason(text: &str) -> ContractModerationReason { ContractModerationReason::from_text(text) @@ -90,6 +97,101 @@ fn insert(drive: &Drive, contract: &DataContract, platform_version: &PlatformVer .expect("expected to insert the contract"); } +/// Removing an entry, or replacing one with a shorter reason, frees bytes flagged with the +/// moderator that paid for them, and pricing that removal needs the fee history of the +/// removing block. Production applies moderation through `apply_drive_operations`, which +/// forwards the block's history; the tests below take the same funnel for those calls. +fn fee_history() -> CachedEpochIndexFeeVersions { + BTreeMap::from([(0, FeeVersion::first())]) +} + +fn apply_moderation( + drive: &Drive, + operation: ContractModerationOperationType, + block_info: &BlockInfo, + apply: bool, + platform_version: &PlatformVersion, +) -> Result { + let history = fee_history(); + drive.apply_drive_operations( + vec![DriveOperation::ContractModerationOperation(operation)], + apply, + block_info, + None, + platform_version, + Some(&history), + ) +} + +fn unban( + drive: &Drive, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + platform_version: &PlatformVersion, +) -> Result { + apply_moderation( + drive, + ContractModerationOperationType::RemoveBan { + contract_id, + identity_id, + }, + block_info, + apply, + platform_version, + ) +} + +fn unsuspend( + drive: &Drive, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + platform_version: &PlatformVersion, +) -> Result { + apply_moderation( + drive, + ContractModerationOperationType::RemoveSuspension { + contract_id, + identity_id, + }, + block_info, + apply, + platform_version, + ) +} + +/// Replaces the identity's existing suspension entry. +#[allow(clippy::too_many_arguments)] +fn resuspend( + drive: &Drive, + contract_id: Identifier, + identity_id: Identifier, + until: u64, + reason: &ContractModerationReason, + moderator_id: Identifier, + block_info: &BlockInfo, + apply: bool, + platform_version: &PlatformVersion, +) -> Result { + apply_moderation( + drive, + ContractModerationOperationType::AddSuspension { + contract_id, + identity_id, + until, + reason: reason.clone(), + replaces_existing: true, + moderator_id, + }, + block_info, + apply, + platform_version, + ) +} + fn has_list_tree(drive: &Drive, contract_id: Identifier, key: u8) -> bool { drive .grove_has_raw( @@ -326,16 +428,15 @@ fn should_ban_and_unban_and_prove_the_status_and_the_entries() { }], ); - let fee = drive - .remove_contract_ban( - contract_id, - target, - &BlockInfo::default(), - true, - None, - platform_version, - ) - .expect("expected to unban"); + let fee = unban( + &drive, + contract_id, + target, + &BlockInfo::default(), + true, + platform_version, + ) + .expect("expected to unban"); assert!( fee.fee_refunds .calculate_refunds_amount_for_identity(moderator) @@ -433,16 +534,15 @@ fn should_suspend_replace_and_unsuspend() { }], ); - drive - .remove_contract_suspension( - contract_id, - target, - &BlockInfo::default(), - true, - None, - platform_version, - ) - .expect("expected to unsuspend"); + unsuspend( + &drive, + contract_id, + target, + &BlockInfo::default(), + true, + platform_version, + ) + .expect("expected to unsuspend"); assert_status( &drive, contract_id, @@ -523,48 +623,44 @@ fn should_estimate_before_applying_every_writer() { "suspension storage" ); - let estimated = drive - .remove_contract_suspension( - contract_id, - target, - &block_info, - false, - None, - platform_version, - ) - .expect("expected to estimate an unsuspend"); + let estimated = unsuspend( + &drive, + contract_id, + target, + &block_info, + false, + platform_version, + ) + .expect("expected to estimate an unsuspend"); assert!(estimated.processing_fee > 0); - drive - .remove_contract_suspension( - contract_id, - target, - &block_info, - true, - None, - platform_version, - ) - .expect("expected to unsuspend"); - let estimated = drive - .remove_contract_ban( - contract_id, - target, - &block_info, - false, - None, - platform_version, - ) - .expect("expected to estimate an unban"); + unsuspend( + &drive, + contract_id, + target, + &block_info, + true, + platform_version, + ) + .expect("expected to unsuspend"); + let estimated = unban( + &drive, + contract_id, + target, + &block_info, + false, + platform_version, + ) + .expect("expected to estimate an unban"); assert!(estimated.processing_fee > 0); - drive - .remove_contract_ban( - contract_id, - target, - &block_info, - true, - None, - platform_version, - ) - .expect("expected to unban"); + unban( + &drive, + contract_id, + target, + &block_info, + true, + platform_version, + ) + .expect("expected to unban"); assert_status( &drive, contract_id, @@ -678,24 +774,21 @@ fn should_refund_the_first_moderator_when_another_replaces_the_suspension() { // Another moderator, a later epoch, a reason of the same length. The entry keeps its size, // so the replacement stores nothing new, and the storage stays the first moderator's. let later = BlockInfo::default_with_epoch(Epoch::new(3).expect("epoch 3")); - let fee = drive - .add_contract_suspension( - contract_id, - target, - 20, - &reason("flooding"), - true, - second_moderator, - &later, - true, - None, - platform_version, - ) - .expect("expected to replace the suspension"); + let fee = resuspend( + &drive, + contract_id, + target, + 20, + &reason("flooding"), + second_moderator, + &later, + true, + platform_version, + ) + .expect("expected to replace the suspension"); assert_eq!(fee.storage_fee, 0, "a same-size replacement stores nothing"); - let fee = drive - .remove_contract_suspension(contract_id, target, &later, true, None, platform_version) + let fee = unsuspend(&drive, contract_id, target, &later, true, platform_version) .expect("expected to unsuspend"); assert!( fee.fee_refunds @@ -742,20 +835,18 @@ fn should_bill_the_replacing_moderator_for_a_longer_reason() { // the replacement does not fail on the two owners. let later = BlockInfo::default_with_epoch(Epoch::new(3).expect("epoch 3")); let longer = reason(&"flooding ".repeat(40)); - let second_fee = drive - .add_contract_suspension( - contract_id, - target, - 20, - &longer, - true, - second_moderator, - &later, - true, - None, - platform_version, - ) - .expect("expected to replace the suspension with a longer reason"); + let second_fee = resuspend( + &drive, + contract_id, + target, + 20, + &longer, + second_moderator, + &later, + true, + platform_version, + ) + .expect("expected to replace the suspension with a longer reason"); assert!(second_fee.storage_fee > 0, "the added bytes are stored"); assert!( second_fee.storage_fee > first_fee.storage_fee, @@ -777,8 +868,7 @@ fn should_bill_the_replacing_moderator_for_a_longer_reason() { ); // The entry, and the refund of its removal, passed to the moderator that replaced it. - let fee = drive - .remove_contract_suspension(contract_id, target, &later, true, None, platform_version) + let fee = unsuspend(&drive, contract_id, target, &later, true, platform_version) .expect("expected to unsuspend"); assert!( fee.fee_refunds @@ -823,20 +913,18 @@ fn should_keep_a_shorter_replacement_with_the_first_moderator() { // Another moderator, a later epoch, a shorter reason: nothing is added, the removed bytes // go back to the moderator that paid for them, and the entry stays that moderator's. let later = BlockInfo::default_with_epoch(Epoch::new(3).expect("epoch 3")); - let fee = drive - .add_contract_suspension( - contract_id, - target, - 20, - &reason("flooding"), - true, - second_moderator, - &later, - true, - None, - platform_version, - ) - .expect("expected to replace the suspension with a shorter reason"); + let fee = resuspend( + &drive, + contract_id, + target, + 20, + &reason("flooding"), + second_moderator, + &later, + true, + platform_version, + ) + .expect("expected to replace the suspension with a shorter reason"); assert_eq!(fee.storage_fee, 0, "a shorter replacement stores nothing"); assert!( fee.fee_refunds @@ -856,8 +944,7 @@ fn should_keep_a_shorter_replacement_with_the_first_moderator() { suspended_until(20, "flooding"), ); - let fee = drive - .remove_contract_suspension(contract_id, target, &later, true, None, platform_version) + let fee = unsuspend(&drive, contract_id, target, &later, true, platform_version) .expect("expected to unsuspend"); assert!( fee.fee_refunds @@ -888,20 +975,35 @@ fn should_not_estimate_a_replacement_below_what_it_costs() { let moderator = contract.owner_id(); let target = identity(0x54); let suspend = |until: u64, length: usize, replaces_existing: bool, apply: bool| { - drive - .add_contract_suspension( + if replaces_existing { + resuspend( + &drive, contract_id, target, until, &reason(&"x".repeat(length)), - replaces_existing, moderator, &BlockInfo::default(), apply, - None, platform_version, ) - .expect("expected to suspend") + .expect("expected to replace the suspension") + } else { + drive + .add_contract_suspension( + contract_id, + target, + until, + &reason(&"x".repeat(length)), + false, + moderator, + &BlockInfo::default(), + apply, + None, + platform_version, + ) + .expect("expected to suspend") + } }; suspend(10, from, false, true); From 58d6eefd683c2026dc2457bb5554643e205e612c Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 22 Sep 2026 13:45:05 -0500 Subject: [PATCH 15/46] fix(drive): price ephemeral TTL bytes in calculate_fee v1 as v0 does consume_to_fees_v1 was written before the time-range TTL work added the ephemeral cost arm to v0 and the rebase kept v1 without it, so a TTL'd index write was billed to storage at protocol version 15. v1 now carries the same arm: added bytes bill to processing at the ephemeral rate, storage stays zero, removal is basic and needs no fee history, a sectioned removal is corrupted state. A test pins v1 equal to v0 on ephemeral operations. The TTL twin test's delete of owner-flagged standing index bytes now passes a fee history. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../time_range_index_e2e_tests.rs | 8 +- packages/rs-drive/src/fees/op.rs | 227 +++++++++++++++++- 2 files changed, 233 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs index d406b1c1560..6ab80d600b7 100644 --- a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs +++ b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/time_range_index_e2e_tests.rs @@ -27,9 +27,11 @@ use dpp::data_contract::document_type::{DocumentTypeRef, IndexBucketing}; use dpp::data_contract::DataContractFactory; use dpp::document::serialization_traits::DocumentPlatformConversionMethodsV0; use dpp::document::{Document, DocumentV0, DocumentV0Getters, DocumentV0Setters}; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::fee::fee_result::FeeResult; use dpp::platform_value::{platform_value, Identifier, Value}; use dpp::prelude::DataContract; +use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; use std::borrow::Cow; use std::collections::BTreeMap; @@ -4262,6 +4264,10 @@ fn ttl_index_bytes_bill_to_processing_without_refunds() { ); let doc_id = make_doc().id(); + // The standing twin's index bytes carry owner flags, so pricing their + // removal needs the fee history of the removing block, as every + // production caller passes. + let fee_history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); let delete = |contract: &DataContract| -> FeeResult { drive .delete_document_for_contract( @@ -4272,7 +4278,7 @@ fn ttl_index_bytes_bill_to_processing_without_refunds() { true, None, platform_version, - None, + Some(&fee_history), ) .expect("delete document") }; diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index 0328a2312a9..d549da64f3a 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -467,7 +467,8 @@ impl LowLevelDriveOperation { /// that removes the bytes. /// /// This is the generation `Drive::calculate_fee` v1 selects. It differs - /// from `consume_to_fees_v0` in one arm: a `SectionedStorageRemoval` + /// from `consume_to_fees_v0` in one arm (the ephemeral TTL'd-subtree + /// arm is the same): a `SectionedStorageRemoval` /// always consults `previous_fee_versions` and returns /// `CorruptedCodeExecution` when none is given, on every fee version /// number. v0 priced fee version number 1 against an empty history, so a @@ -489,6 +490,79 @@ impl LowLevelDriveOperation { processing_fee: op.cost(fee_version), ..Default::default() }), + CalculatedEphemeralCostOperation(cost, EphemeralPricing::DocumentTtl { + credit_per_byte, + lifetime_epochs, + }) => { + // The writes of a document whose type declares a `ttl`, + // priced exactly as in v0: each added byte costs the + // price of the document's remaining lifetime, as a + // storage fee paid out over the epochs it has left to + // live. Its elements carry no storage flags and this + // batch refunds nothing, so no fee history is needed: a + // sectioned removal here only counts bytes leaving the + // system. + let storage_fee = (cost.storage_cost.added_bytes as u64) + .checked_mul(credit_per_byte) + .ok_or(Error::Fee(FeeError::Overflow( + "overflow pricing the bytes of a document with a time to live", + )))?; + let processing_fee = cost.ephemeral_cost(fee_version)?; + let lifetime_storage_fees = if storage_fee > 0 { + LifetimeStorageFees::from([(lifetime_epochs, storage_fee)]) + } else { + LifetimeStorageFees::new() + }; + let removed_bytes_from_system = + cost.storage_cost.removed_bytes.total_removed_bytes(); + Ok(FeeResult { + storage_fee, + processing_fee, + fee_refunds: FeeRefunds::default(), + removed_bytes_from_system, + lifetime_storage_fees, + }) + } + CalculatedEphemeralCostOperation(cost, EphemeralPricing::TimeRangeTtl) => { + // TTL'd-subtree bytes: the added bytes bill to + // PROCESSING at the ephemeral rate instead of to + // storage, exactly as in v0. TTL elements carry no + // storage flags, so their removal can only ever be + // basic and needs no fee history; a sectioned removal + // here is a corrupted batch, not a missing history. + let ephemeral_bytes_fee = (cost.storage_cost.added_bytes as u64) + .checked_mul( + fee_version + .storage + .ttl_ephemeral_disk_usage_credit_per_byte, + ) + .ok_or(Error::Fee(FeeError::Overflow( + "overflow pricing ephemeral bytes", + )))?; + let processing_fee = cost + .ephemeral_cost(fee_version)? + .checked_add(ephemeral_bytes_fee) + .ok_or(Error::Fee(FeeError::Overflow( + "overflow adding ephemeral bytes fee", + )))?; + let removed_bytes_from_system = match cost.storage_cost.removed_bytes { + NoStorageRemoval => 0, + BasicStorageRemoval(amount) => amount, + SectionedStorageRemoval(_) => { + return Err(Error::Drive(DriveError::CorruptedCodeExecution( + "TTL'd subtrees carry no storage flags, so an ephemeral \ + batch cannot produce sectioned (refundable) removal", + ))) + } + }; + Ok(FeeResult { + storage_fee: 0, + processing_fee, + fee_refunds: FeeRefunds::default(), + removed_bytes_from_system, + lifetime_storage_fees: Default::default(), + }) + } _ => { let cost = operation.operation_cost()?; // There is no need for a checked multiply here because added bytes are u64 and @@ -3346,6 +3420,157 @@ mod tests { } } + #[test] + fn should_price_ephemeral_bytes_to_processing_without_fee_history_in_v1() { + // TTL'd index bytes bill to processing at the ephemeral rate and + // their removal is never sectioned, so the arm is the same in both + // generations and needs no history even under the strict rule. + let ephemeral = |added: u32, removed: u32| { + LowLevelDriveOperation::CalculatedEphemeralCostOperation( + OperationCost { + seek_count: 2, + storage_cost: StorageCost { + added_bytes: added, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::BasicStorageRemoval(removed), + }, + storage_loaded_bytes: 10, + hash_node_calls: 1, + sinsemilla_hash_calls: 0, + }, + EphemeralPricing::TimeRangeTtl, + ) + }; + + let v0_results = LowLevelDriveOperation::consume_to_fees_v0( + vec![ephemeral(500, 40)], + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("v0 prices ephemeral bytes"); + let v1_results = LowLevelDriveOperation::consume_to_fees_v1( + vec![ephemeral(500, 40)], + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("v1 prices ephemeral bytes without a history"); + + assert_eq!(v0_results, v1_results); + assert_eq!( + v1_results[0].storage_fee, 0, + "TTL'd bytes never bill storage" + ); + assert_eq!( + v1_results[0].processing_fee, + ephemeral(500, 40) + .operation_cost() + .expect("cost") + .ephemeral_cost(&FEE_VERSION1) + .expect("ephemeral cost") + + 500 + * FEE_VERSION1 + .storage + .ttl_ephemeral_disk_usage_credit_per_byte + ); + assert_eq!(v1_results[0].removed_bytes_from_system, 40); + assert_eq!(v1_results[0].fee_refunds, FeeRefunds::default()); + + // A sectioned removal inside an ephemeral batch is corrupted state in + // both generations, with or without a history. + let corrupted = || { + let mut removal = StorageRemovalPerEpochByIdentifier::default(); + removal.entry(OWNER).or_default().insert(3, 100); + LowLevelDriveOperation::CalculatedEphemeralCostOperation( + OperationCost { + seek_count: 0, + storage_cost: StorageCost { + added_bytes: 0, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::SectionedStorageRemoval(removal), + }, + storage_loaded_bytes: 0, + hash_node_calls: 0, + sinsemilla_hash_calls: 0, + }, + EphemeralPricing::TimeRangeTtl, + ) + }; + let history: CachedEpochIndexFeeVersions = + BTreeMap::from([(0u16, FeeVersion::first())]); + for previous_fee_versions in [None, Some(&history)] { + assert!(matches!( + LowLevelDriveOperation::consume_to_fees_v1( + vec![corrupted()], + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + previous_fee_versions, + ), + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + )); + } + } + + #[test] + fn should_price_document_ttl_bytes_as_v0_does_without_fee_history_in_v1() { + // A document with a time to live pays its bytes as a storage fee + // spread over its remaining epochs and refunds nothing, so the + // strict rule does not touch it: even a sectioned removal in its + // batch only counts bytes leaving the system. + let pricing = EphemeralPricing::DocumentTtl { + credit_per_byte: 7, + lifetime_epochs: 3, + }; + let document_ttl = || { + let mut removal = StorageRemovalPerEpochByIdentifier::default(); + removal.entry(OWNER).or_default().insert(3, 100); + LowLevelDriveOperation::CalculatedEphemeralCostOperation( + OperationCost { + seek_count: 2, + storage_cost: StorageCost { + added_bytes: 500, + replaced_bytes: 0, + removed_bytes: StorageRemovedBytes::SectionedStorageRemoval(removal), + }, + storage_loaded_bytes: 10, + hash_node_calls: 1, + sinsemilla_hash_calls: 0, + }, + pricing, + ) + }; + + let v0_results = LowLevelDriveOperation::consume_to_fees_v0( + vec![document_ttl()], + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("v0 prices the bytes of a document with a time to live"); + let v1_results = LowLevelDriveOperation::consume_to_fees_v1( + vec![document_ttl()], + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("v1 prices them without a history"); + + assert_eq!(v0_results, v1_results); + assert_eq!(v1_results[0].storage_fee, 3_500); + assert_eq!( + v1_results[0].lifetime_storage_fees, + LifetimeStorageFees::from([(3, 3_500)]) + ); + assert_eq!(v1_results[0].removed_bytes_from_system, 100); + assert_eq!(v1_results[0].fee_refunds, FeeRefunds::default()); + } + /// The premise of the equality above. A shipped schedule that kept /// fee version number 1 but changed its storage rates would make v0 /// (which prices number 1 at the first generation) and v1 (which From b064de5772fcef234b422acb405cb44fdfe89e0e Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 22 Sep 2026 13:45:05 -0500 Subject: [PATCH 16/46] test(drive): close the structure fixture's group action with fee history and label fixtures with the latest version Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- packages/rs-drive/src/structure/tests.rs | 31 +++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/rs-drive/src/structure/tests.rs b/packages/rs-drive/src/structure/tests.rs index 978dd03afc1..e25475e0201 100644 --- a/packages/rs-drive/src/structure/tests.rs +++ b/packages/rs-drive/src/structure/tests.rs @@ -286,7 +286,7 @@ mod fixtures { use crate::structure::shape::shape_at; use crate::structure::{KeySpec, NodeId}; use crate::util::batch::drive_op_batch::{ - AddressFundsOperationType, ContractFeePotOperationType, + AddressFundsOperationType, ContractFeePotOperationType, GroupOperationType, }; use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use crate::util::batch::ContractModerationOperationType; @@ -305,6 +305,8 @@ mod fixtures { use crate::drive::credit_pools::epochs::paths::EpochProposers; use dpp::block::epoch::Epoch; use dpp::block::finalized_epoch_info::v0::FinalizedEpochInfoV0; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::version::fee::FeeVersion; use dpp::contract_group::{ generate_contract_group_id, ContractGroupMember, ContractGroupMembership, ContractGroupRegistration, @@ -1183,19 +1185,30 @@ mod fixtures { ) .expect("expected to propose the action"); if close { + // Closing moves signer-flagged items, so pricing the removal + // needs the fee history of the removing block; production + // closes actions through `apply_drive_operations`, which + // forwards it. + let fee_history: CachedEpochIndexFeeVersions = + BTreeMap::from([(0, FeeVersion::first())]); drive - .add_group_action( - contract.id(), - 0, - None, + .apply_drive_operations( + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id: contract.id(), + group_contract_position: 0, + initialize_with_insert_action_info: None, + action_id, + signer_identity_id: member_2, + signer_power: 2, + closes_group_action: true, + }, + )], true, - action_id, - member_2, - 2, &BlockInfo::default(), - true, None, platform_version, + Some(&fee_history), ) .expect("expected to close the action"); } From 1576eb42407d17d09c3e35282160d211ee144314 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 23 Sep 2026 01:12:13 -0500 Subject: [PATCH 17/46] fix(drive)!: price a batch before committing the transaction drive owns From protocol version 15 pricing an owner-attributed storage removal without the fee history is an error. The fee-returning entry points that start their own transaction when the caller passes none committed it before pricing, so that error could come back after the write had been persisted. New generations hold the owned transaction until Drive::calculate_fee succeeded and drop it with everything it wrote on an error: apply_drive_operations v2 (finalize tasks still run after the commit), add_group_action v1, and the three moderation writers that can free moderator-flagged bytes (remove_contract_ban, remove_contract_suspension, add_contract_suspension) v1. Drive table v10 selects them through DRIVE_GROUP_METHOD_VERSIONS_V2 and DRIVE_CONTRACT_METHOD_VERSIONS_V5. With a caller transaction nothing changes; shipped generations are byte-identical. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../moderation/add_contract_suspension/mod.rs | 29 +- .../add_contract_suspension/v1/mod.rs | 156 ++++++++++ .../moderation/remove_contract_ban/mod.rs | 21 +- .../moderation/remove_contract_ban/v1/mod.rs | 128 +++++++++ .../remove_contract_suspension/mod.rs | 21 +- .../remove_contract_suspension/v1/mod.rs | 128 +++++++++ .../remove_contract_warnings/mod.rs | 21 +- .../remove_contract_warnings/v1/mod.rs | 128 +++++++++ .../group/insert/add_group_action/mod.rs | 32 ++- .../group/insert/add_group_action/v1/mod.rs | 119 ++++++++ .../apply_drive_operations/v2/mod.rs | 266 +++++++++++++++++- .../drive_contract_method_versions/v5.rs | 18 +- .../drive_group_method_versions/mod.rs | 1 + .../drive_group_method_versions/v2.rs | 40 +++ .../src/version/drive_versions/v10.rs | 14 +- .../rs-platform-version/src/version/v15.rs | 6 + 16 files changed, 1106 insertions(+), 22 deletions(-) create mode 100644 packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/group/insert/add_group_action/v1/mod.rs create mode 100644 packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs diff --git a/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/mod.rs b/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/mod.rs index 595ad598d34..bb072862396 100644 --- a/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/mod.rs +++ b/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -71,9 +72,21 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contract_suspension_v1( + contract_id, + identity_id, + until, + reason, + replaces_existing, + moderator_id, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contract_suspension".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -133,9 +146,21 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contract_suspension_operations_v1( + contract_id, + identity_id, + until, + reason, + replaces_existing, + moderator_id, + block_info, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contract_suspension_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/v1/mod.rs b/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/v1/mod.rs new file mode 100644 index 00000000000..04418e245a6 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/moderation/add_contract_suspension/v1/mod.rs @@ -0,0 +1,156 @@ +use crate::drive::contract::moderation::types::encode_suspension; +use crate::drive::contract::paths::contract_moderation_list_path; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::PathKeyElementInfo::PathFixedSizeKeyRefElement; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::config::moderation::{ContractModerationList, ContractModerationReason}; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::identity::TimestampMillis; +use dpp::version::PlatformVersion; +use grovedb::Element; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, this wrapper + /// persisted the write and then failed. It still passes no fee history, so a call that + /// frees moderator-flagged bytes still fails at protocol version 15; it now fails before + /// anything is written. Production applies moderation through `apply_drive_operations`, + /// which forwards the block's history. With a caller transaction nothing is committed + /// by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contract_suspension_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + until: TimestampMillis, + reason: &ContractModerationReason, + replaces_existing: bool, + moderator_id: Identifier, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.add_contract_suspension_operations_v1( + contract_id, + identity_id, + until, + reason, + replaces_existing, + moderator_id, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + let mut drive_operations: Vec = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + /// A suspension is `until` as eight big-endian bytes, then its reason, under the identity's + /// id, flagged with the moderator's identity so the storage refund on removal goes back to + /// whoever paid. An existing entry is replaced in place; two operations on one key would + /// fail the batch. The replacement brings its own reason, so the entry may change size, and + /// its flags follow GroveDB's flag merge: a longer entry passes, with the refund of its + /// removal, to the moderator that replaced it, who pays for the added bytes; a shorter or + /// an equally long one stays the first moderator's, who is refunded the removed bytes. + /// + /// An estimate prices the replacement as a fresh insert. GroveDB's average-case replace + /// assumes an item keeps its size and would price no storage for a longer reason, which + /// the moderator's balance is then not checked against; the whole entry is an upper bound. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contract_suspension_operations_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + until: TimestampMillis, + reason: &ContractModerationReason, + replaces_existing: bool, + moderator_id: Identifier, + block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, + _transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let estimating = estimated_costs_only_with_layer_info.is_some(); + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Drive::add_estimation_costs_for_contract_moderation_entry( + contract_id.to_buffer(), + ContractModerationList::Suspensions, + estimated_costs_only_with_layer_info, + &platform_version.drive, + )?; + } + + let storage_flags = + StorageFlags::new_single_epoch(block_info.epoch.index, Some(moderator_id.to_buffer())); + + let path_key_element = PathFixedSizeKeyRefElement(( + contract_moderation_list_path( + contract_id.as_slice(), + ContractModerationList::Suspensions, + ), + identity_id.as_slice(), + Element::new_item_with_flags( + encode_suspension(until, reason), + storage_flags.to_some_element_flags(), + ), + )); + + let mut batch_operations: Vec = vec![]; + if replaces_existing && !estimating { + self.batch_replace( + path_key_element, + &mut batch_operations, + &platform_version.drive, + )?; + } else { + self.batch_insert( + path_key_element, + &mut batch_operations, + &platform_version.drive, + )?; + } + + Ok(batch_operations) + } +} diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/mod.rs index c51755740ea..805a34d60a9 100644 --- a/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/mod.rs +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -56,9 +57,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_ban_v1( + contract_id, + identity_id, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_ban".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -105,9 +114,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_ban_operations_v1( + contract_id, + identity_id, + block_info, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_ban_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/v1/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/v1/mod.rs new file mode 100644 index 00000000000..f8fe8795195 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_ban/v1/mod.rs @@ -0,0 +1,128 @@ +use crate::drive::contract::moderation::types::estimated_entry_value_size; +use crate::drive::contract::paths::contract_moderation_list_path; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::grove_operations::BatchDeleteApplyType; +use crate::util::storage_flags::StorageFlags; +use crate::util::type_constants::DEFAULT_HASH_SIZE_U32; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::config::moderation::ContractModerationList; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use grovedb::{MaybeTree, TreeType}; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, this wrapper + /// persisted the write and then failed. It still passes no fee history, so a call that + /// frees moderator-flagged bytes still fails at protocol version 15; it now fails before + /// anything is written. Production applies moderation through `apply_drive_operations`, + /// which forwards the block's history. With a caller transaction nothing is committed + /// by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_ban_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.remove_contract_ban_operations_v1( + contract_id, + identity_id, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + let mut drive_operations: Vec = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + /// Deletes the entry. The storage refund follows the entry's own flags, which name the + /// moderator that wrote it. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_ban_operations_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + _block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let list = ContractModerationList::Banlist; + + let apply_type = if let Some(estimated_costs_only_with_layer_info) = + estimated_costs_only_with_layer_info + { + Drive::add_estimation_costs_for_contract_moderation_entry( + contract_id.to_buffer(), + list, + estimated_costs_only_with_layer_info, + &platform_version.drive, + )?; + BatchDeleteApplyType::StatelessBatchDelete { + in_tree_type: TreeType::NormalTree, + estimated_key_size: DEFAULT_HASH_SIZE_U32, + estimated_value_size: estimated_entry_value_size(list) + + StorageFlags::approximate_size(true, None), + } + } else { + BatchDeleteApplyType::StatefulBatchDelete { + is_known_to_be_subtree_with_sum: Some(MaybeTree::NotTree), + } + }; + + let mut batch_operations: Vec = vec![]; + self.batch_delete( + (&contract_moderation_list_path(contract_id.as_slice(), list)).into(), + identity_id.as_slice(), + apply_type, + transaction, + &mut batch_operations, + &platform_version.drive, + )?; + + Ok(batch_operations) + } +} diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/mod.rs index bc6c235fb2c..98d30c37770 100644 --- a/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/mod.rs +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -56,9 +57,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_suspension_v1( + contract_id, + identity_id, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_suspension".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -105,9 +114,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_suspension_operations_v1( + contract_id, + identity_id, + block_info, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_suspension_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/v1/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/v1/mod.rs new file mode 100644 index 00000000000..0d89b5f288d --- /dev/null +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_suspension/v1/mod.rs @@ -0,0 +1,128 @@ +use crate::drive::contract::moderation::types::estimated_entry_value_size; +use crate::drive::contract::paths::contract_moderation_list_path; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::grove_operations::BatchDeleteApplyType; +use crate::util::storage_flags::StorageFlags; +use crate::util::type_constants::DEFAULT_HASH_SIZE_U32; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::config::moderation::ContractModerationList; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use grovedb::{MaybeTree, TreeType}; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, this wrapper + /// persisted the write and then failed. It still passes no fee history, so a call that + /// frees moderator-flagged bytes still fails at protocol version 15; it now fails before + /// anything is written. Production applies moderation through `apply_drive_operations`, + /// which forwards the block's history. With a caller transaction nothing is committed + /// by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_suspension_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.remove_contract_suspension_operations_v1( + contract_id, + identity_id, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + let mut drive_operations: Vec = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + /// Deletes the entry. The storage refund follows the entry's own flags, which name the + /// moderator that wrote it. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_suspension_operations_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + _block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let list = ContractModerationList::Suspensions; + + let apply_type = if let Some(estimated_costs_only_with_layer_info) = + estimated_costs_only_with_layer_info + { + Drive::add_estimation_costs_for_contract_moderation_entry( + contract_id.to_buffer(), + list, + estimated_costs_only_with_layer_info, + &platform_version.drive, + )?; + BatchDeleteApplyType::StatelessBatchDelete { + in_tree_type: TreeType::NormalTree, + estimated_key_size: DEFAULT_HASH_SIZE_U32, + estimated_value_size: estimated_entry_value_size(list) + + StorageFlags::approximate_size(true, None), + } + } else { + BatchDeleteApplyType::StatefulBatchDelete { + is_known_to_be_subtree_with_sum: Some(MaybeTree::NotTree), + } + }; + + let mut batch_operations: Vec = vec![]; + self.batch_delete( + (&contract_moderation_list_path(contract_id.as_slice(), list)).into(), + identity_id.as_slice(), + apply_type, + transaction, + &mut batch_operations, + &platform_version.drive, + )?; + + Ok(batch_operations) + } +} diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/mod.rs index 60d6b649d6f..d05ddd4159f 100644 --- a/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/mod.rs +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -56,9 +57,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_warnings_v1( + contract_id, + identity_id, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_warnings".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -105,9 +114,17 @@ impl Drive { transaction, platform_version, ), + 1 => self.remove_contract_warnings_operations_v1( + contract_id, + identity_id, + block_info, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "remove_contract_warnings_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/v1/mod.rs b/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/v1/mod.rs new file mode 100644 index 00000000000..54fc35611c7 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/moderation/remove_contract_warnings/v1/mod.rs @@ -0,0 +1,128 @@ +use crate::drive::contract::moderation::types::estimated_entry_value_size; +use crate::drive::contract::paths::contract_moderation_list_path; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::grove_operations::BatchDeleteApplyType; +use crate::util::storage_flags::StorageFlags; +use crate::util::type_constants::DEFAULT_HASH_SIZE_U32; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::config::moderation::ContractModerationList; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use grovedb::{MaybeTree, TreeType}; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, this wrapper + /// persisted the write and then failed. It still passes no fee history, so a call that + /// frees moderator-flagged bytes still fails at protocol version 15; it now fails before + /// anything is written. Production applies moderation through `apply_drive_operations`, + /// which forwards the block's history. With a caller transaction nothing is committed + /// by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_warnings_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.remove_contract_warnings_operations_v1( + contract_id, + identity_id, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + let mut drive_operations: Vec = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + /// Deletes the entry. The storage refund follows the entry's own flags, which name the + /// moderator that wrote it. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn remove_contract_warnings_operations_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + _block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let list = ContractModerationList::Warnings; + + let apply_type = if let Some(estimated_costs_only_with_layer_info) = + estimated_costs_only_with_layer_info + { + Drive::add_estimation_costs_for_contract_moderation_entry( + contract_id.to_buffer(), + list, + estimated_costs_only_with_layer_info, + &platform_version.drive, + )?; + BatchDeleteApplyType::StatelessBatchDelete { + in_tree_type: TreeType::NormalTree, + estimated_key_size: DEFAULT_HASH_SIZE_U32, + estimated_value_size: estimated_entry_value_size(list) + + StorageFlags::approximate_size(true, None), + } + } else { + BatchDeleteApplyType::StatefulBatchDelete { + is_known_to_be_subtree_with_sum: Some(MaybeTree::NotTree), + } + }; + + let mut batch_operations: Vec = vec![]; + self.batch_delete( + (&contract_moderation_list_path(contract_id.as_slice(), list)).into(), + identity_id.as_slice(), + apply_type, + transaction, + &mut batch_operations, + &platform_version.drive, + )?; + + Ok(batch_operations) + } +} diff --git a/packages/rs-drive/src/drive/group/insert/add_group_action/mod.rs b/packages/rs-drive/src/drive/group/insert/add_group_action/mod.rs index e3b01898b81..e246467703d 100644 --- a/packages/rs-drive/src/drive/group/insert/add_group_action/mod.rs +++ b/packages/rs-drive/src/drive/group/insert/add_group_action/mod.rs @@ -14,6 +14,7 @@ use dpp::prelude::Identifier; use grovedb::TransactionArg; mod v0; +mod v1; impl Drive { /// Adds an action to the state @@ -46,9 +47,22 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_group_action_v1( + contract_id, + group_contract_position, + initialize_with_insert_action_info, + closes_group_action, + action_id, + signer_identity_id, + signer_power, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_group_action".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -86,9 +100,23 @@ impl Drive { drive_operations, platform_version, ), + 1 => self.add_group_action_add_to_operations_v1( + contract_id, + group_contract_position, + initialize_with_insert_action_info, + closes_group_action, + action_id, + signer_identity_id, + signer_power, + block_info, + apply, + transaction, + drive_operations, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_group_action_add_to_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/group/insert/add_group_action/v1/mod.rs b/packages/rs-drive/src/drive/group/insert/add_group_action/v1/mod.rs new file mode 100644 index 00000000000..c1f6b60bbe2 --- /dev/null +++ b/packages/rs-drive/src/drive/group/insert/add_group_action/v1/mod.rs @@ -0,0 +1,119 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::group::GroupMemberPower; +use dpp::data_contract::GroupContractPosition; +use dpp::fee::fee_result::FeeResult; +use dpp::group::group_action::GroupAction; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Adds an action to the state and prices it, committing nothing until the price is + /// known. + /// + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, closing an + /// action through this wrapper persisted the closure and then failed. The wrapper still + /// passes no fee history, so such a close still fails at protocol version 15; it now + /// fails before anything is written. Production closes actions through + /// `apply_drive_operations`, which forwards the block's history. With a caller + /// transaction nothing is committed by Drive in either generation. + #[allow(clippy::too_many_arguments)] + pub(super) fn add_group_action_v1( + &self, + contract_id: Identifier, + group_contract_position: GroupContractPosition, + initialize_with_insert_action_info: Option, + closes_group_action: bool, + action_id: Identifier, + signer_identity_id: Identifier, + signer_power: GroupMemberPower, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut drive_operations: Vec = vec![]; + self.add_group_action_add_to_operations_v1( + contract_id, + group_contract_position, + initialize_with_insert_action_info, + closes_group_action, + action_id, + signer_identity_id, + signer_power, + block_info, + apply, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + #[allow(clippy::too_many_arguments)] + /// Adds group creation operations to drive operations + pub(super) fn add_group_action_add_to_operations_v1( + &self, + contract_id: Identifier, + group_contract_position: GroupContractPosition, + initialize_with_insert_action_info: Option, + closes_group_action: bool, + action_id: Identifier, + signer_identity_id: Identifier, + signer_power: GroupMemberPower, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.add_group_action_operations( + contract_id, + group_contract_position, + initialize_with_insert_action_info, + closes_group_action, + action_id, + signer_identity_id, + signer_power, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + drive_operations, + &platform_version.drive, + ) + } +} diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index d541634fbf2..df341386d5f 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -93,7 +93,25 @@ impl Drive { /// descriptions name a shallower one (the credit shielded pool's, at level 3, for /// `ShieldedBalances` at level 4). The estimate does not depend on the order of the batch's operations, and no /// write is priced at a shallower depth than its own description. Applying a batch is - /// generation 1's. + /// generation 1's, but for when an owned transaction commits. + /// + /// It also holds the owned transaction until the batch is priced. Everything generation + /// 1 does carries over: a batch that forfeits storage refunds refunds nobody, every + /// write of one identity balance, fee pot or prefunded specialized balance is merged + /// into one, a batch writing one token balance, supply or shielded pool total twice is + /// refused, a batch moving one document type's `summableOffCountIndex` counters for + /// more than one document is refused, and the debt the batch repaid + /// ([`LowLevelDriveOperation::RepaidIdentityDebt`]) is written to the processing fee + /// pool of the block's epoch after the batch applied, followed by the processing pool + /// returns the batch carries (`SystemOperationType::AddToCurrentEpochProcessingPool`, + /// unbilled), both before the owned transaction commits, so a pricing error drops the + /// pool writes with the rest. From protocol version 15 pricing an owner-attributed + /// storage removal without the fee history is an error; generation 1 committed its + /// owned transaction before pricing, so a caller passing no transaction and no history + /// had its writes persisted and the error returned. Now `Drive::calculate_fee` runs + /// first, a pricing error drops the owned transaction with everything it wrote, and the + /// finalize tasks still run after the commit. With a caller transaction nothing is + /// committed by Drive in either generation. #[inline(always)] pub(crate) fn apply_drive_operations_v2( &self, @@ -267,6 +285,17 @@ impl Drive { &platform_version.drive, )?; } + // Price before committing: a pricing error drops the owned transaction with + // everything it wrote, so nothing is persisted without its fee result. + let fee_result = Drive::calculate_fee( + None, + Some(cost_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { self.commit_transaction(owned_transaction, &platform_version.drive)?; } @@ -281,14 +310,7 @@ impl Drive { } } - Drive::calculate_fee( - None, - Some(cost_operations), - &block_info.epoch, - self.config.epochs_per_era, - platform_version, - previous_fee_versions, - ) + Ok(fee_result) } } @@ -427,3 +449,229 @@ mod tests { assert_eq!(deepest_root_key_level([]), None); } } + +#[cfg(test)] +mod price_before_commit_tests { + use crate::drive::Drive; + use crate::error::drive::DriveError; + use crate::error::Error; + use crate::util::batch::drive_op_batch::GroupOperationType; + use crate::util::batch::DriveOperation; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::data_contract::accessors::v0::DataContractV0Getters; + use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; + use dpp::data_contract::associated_token::token_configuration::TokenConfiguration; + use dpp::data_contract::config::v0::DataContractConfigV0; + use dpp::data_contract::config::DataContractConfig; + use dpp::data_contract::group::v0::GroupV0; + use dpp::data_contract::group::Group; + use dpp::data_contract::v1::DataContractV1; + use dpp::data_contract::DataContract; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::group::action_event::GroupActionEvent; + use dpp::group::group_action::v0::GroupActionV0; + use dpp::group::group_action::GroupAction; + use dpp::group::group_action_status::GroupActionStatus; + use dpp::identifier::Identifier; + use dpp::tokens::token_event::TokenEvent; + use dpp::version::fee::FeeVersion; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + /// A contract with one two-member group and an action the first member opened, + /// so closing the action moves signer-flagged items: the one removal a batch can + /// carry without a document fixture. + fn drive_with_open_group_action( + platform_version: &PlatformVersion, + ) -> (Drive, Identifier, Identifier, Identifier) { + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let member_1 = Identifier::from([1; 32]); + let member_2 = Identifier::from([2; 32]); + let contract = DataContract::V1(DataContractV1 { + id: Identifier::from([3; 32]), + version: 0, + owner_id: member_1, + document_types: Default::default(), + config: DataContractConfig::V0(DataContractConfigV0 { + can_be_deleted: false, + readonly: false, + keeps_history: false, + documents_keep_history_contract_default: false, + documents_mutable_contract_default: false, + documents_can_be_deleted_contract_default: false, + requires_identity_encryption_bounded_key: None, + requires_identity_decryption_bounded_key: None, + }), + schema_defs: None, + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + groups: BTreeMap::from([( + 0, + Group::V0(GroupV0 { + members: [(member_1, 1), (member_2, 2)].into(), + required_power: 3, + }), + )]), + tokens: BTreeMap::from([( + 0, + TokenConfiguration::V0(TokenConfigurationV0::default_most_restrictive()), + )]), + keywords: Vec::new(), + description: None, + }); + let contract_id = contract.id(); + drive + .insert_contract( + &contract, + BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to insert the contract"); + let action_id = Identifier::from([4; 32]); + let action = GroupAction::V0(GroupActionV0 { + contract_id, + proposer_id: member_1, + token_contract_position: 0, + event: GroupActionEvent::TokenEvent(TokenEvent::Mint(100, member_1, None)), + }); + drive + .add_group_action( + contract_id, + 0, + Some(action), + false, + action_id, + member_1, + 1, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to open the action"); + (drive, contract_id, member_2, action_id) + } + + fn close_action( + contract_id: Identifier, + member_2: Identifier, + action_id: Identifier, + ) -> Vec> { + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id, + group_contract_position: 0, + initialize_with_insert_action_info: None, + action_id, + signer_identity_id: member_2, + signer_power: 2, + closes_group_action: true, + }, + )] + } + + fn is_closed( + drive: &Drive, + contract_id: Identifier, + action_id: Identifier, + platform_version: &PlatformVersion, + ) -> bool { + drive + .fetch_action_is_closed( + contract_id, + 0, + action_id, + true, + None, + &mut vec![], + platform_version, + ) + .expect("expected to check if the action is closed") + } + + #[test] + fn should_not_commit_an_owned_transaction_when_the_batch_cannot_be_priced() { + // No caller transaction and no fee history: closing the action frees + // signer-flagged bytes, which the latest generation refuses to price. The + // batch was applied inside an owned transaction that is dropped with the + // error, so the action is still open afterwards. + let platform_version = PlatformVersion::latest(); + let (drive, contract_id, member_2, action_id) = + drive_with_open_group_action(platform_version); + + let result = drive.apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + None, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "pricing a flagged removal without the fee history must fail, got {:?}", + result + ); + assert!( + !is_closed(&drive, contract_id, action_id, platform_version), + "a batch that could not be priced must not be committed" + ); + + // The same batch with the block's history is priced and committed. + let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); + let fee_result = drive + .apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + Some(&history), + ) + .expect("expected to close the action with the fee history"); + assert!(fee_result.fee_refunds.get(&[1; 32]).is_some()); + assert!(is_closed(&drive, contract_id, action_id, platform_version)); + let closed_signers = drive + .fetch_action_signers( + contract_id, + 0, + GroupActionStatus::ActionClosed, + action_id, + None, + platform_version, + ) + .expect("expected the closed signers"); + assert_eq!(closed_signers.len(), 2); + } + + #[test] + fn should_price_and_commit_without_history_under_the_frozen_generation() { + // Protocol version 14 selects generation 1, which commits before pricing and + // prices fee version number 1 without a history: the close persists. + let platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let (drive, contract_id, member_2, action_id) = + drive_with_open_group_action(platform_version); + + drive + .apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + None, + ) + .expect("the frozen generation prices the shipped shortcut without a history"); + assert!(is_closed(&drive, contract_id, action_id, platform_version)); + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs index 957649fd9fe..c40beca0b47 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs @@ -1,9 +1,10 @@ use crate::version::drive_versions::drive_contract_method_versions::v4::DRIVE_CONTRACT_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_contract_method_versions::{ DriveContractApplyMethodVersions, DriveContractMethodVersions, + DriveContractModerationMethodVersions, }; -/// Drive contract methods for protocol v15+. +/// Drive contract methods for protocol version 15. /// /// Relative to [`super::v4::DRIVE_CONTRACT_METHOD_VERSIONS_V4`]: /// @@ -12,11 +13,26 @@ use crate::version::drive_versions::drive_contract_method_versions::{ /// differ from the ones it was stored with in format version 1. v1 writes nothing when the /// two contracts' format version 1 bytes match, where v0 compared the stored bytes alone and /// would have rewritten an unchanged contract as an update. +/// * The four moderation writers that can free moderator-flagged bytes (`remove_contract_ban`, +/// `remove_contract_suspension`, `remove_contract_warnings`, and `add_contract_suspension`, +/// whose replacement of an existing entry may shrink it) are bumped to `1`: when the caller +/// passes no transaction, the fee-returning wrapper writes and prices inside one owned +/// transaction and commits it only once `Drive::calculate_fee` succeeded. Pricing an +/// owner-attributed storage removal without the fee history is an error from this version, +/// and generation 0 committed the write before that error surfaced. The operation builders +/// are unchanged. pub const DRIVE_CONTRACT_METHOD_VERSIONS_V5: DriveContractMethodVersions = DriveContractMethodVersions { apply: DriveContractApplyMethodVersions { apply_contract_with_serialization: 1, ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.apply }, + moderation: DriveContractModerationMethodVersions { + remove_contract_ban: 1, + add_contract_suspension: 1, + remove_contract_suspension: 1, + remove_contract_warnings: 1, + ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.moderation + }, ..DRIVE_CONTRACT_METHOD_VERSIONS_V4 }; diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/mod.rs index 5494feaba84..6abc2f32676 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/mod.rs @@ -1,6 +1,7 @@ use grovedb_version::version::FeatureVersion; pub mod v1; +pub mod v2; #[derive(Clone, Debug, Default)] pub struct DriveGroupMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs new file mode 100644 index 00000000000..3cbd9457df4 --- /dev/null +++ b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs @@ -0,0 +1,40 @@ +use crate::version::drive_versions::drive_group_method_versions::{ + DriveGroupCostEstimationMethodVersions, DriveGroupFetchMethodVersions, + DriveGroupInsertMethodVersions, DriveGroupMethodVersions, DriveGroupProveMethodVersions, +}; + +/// Drive group methods for protocol version 15. +/// +/// Relative to [`super::v1::DRIVE_GROUP_METHOD_VERSIONS_V1`], `insert.add_group_action` is +/// bumped to `1`: when the caller passes no transaction, the fee-returning wrapper writes and +/// prices inside one owned transaction and commits it only once `Drive::calculate_fee` +/// succeeded. Pricing an owner-attributed storage removal without the fee history is an error +/// from this version, and generation 0 committed a closing action before that error surfaced. +/// The operation builders are unchanged. +pub const DRIVE_GROUP_METHOD_VERSIONS_V2: DriveGroupMethodVersions = DriveGroupMethodVersions { + fetch: DriveGroupFetchMethodVersions { + fetch_action_id_signers_power: 0, + fetch_active_action_info: 0, + fetch_action_id_info_keep_serialized: 0, + fetch_action_id_has_signer: 0, + fetch_group_info: 0, + fetch_group_infos: 0, + fetch_action_infos: 0, + fetch_action_signers: 0, + fetch_action_is_closed: 0, + }, + prove: DriveGroupProveMethodVersions { + prove_group_info: 0, + prove_group_infos: 0, + prove_action_infos: 0, + prove_action_signers: 0, + }, + insert: DriveGroupInsertMethodVersions { + add_new_groups: 0, + add_group_action: 1, // changed in v15: the fee-returning wrapper prices before it commits its owned transaction + }, + cost_estimation: DriveGroupCostEstimationMethodVersions { + for_add_group_action: 0, + for_add_group: 0, + }, +}; diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 408f171d31c..66cf1daf03a 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -1,5 +1,6 @@ use crate::version::drive_versions::drive_contract_method_versions::v5::DRIVE_CONTRACT_METHOD_VERSIONS_V5; use crate::version::drive_versions::drive_document_method_versions::v5::DRIVE_DOCUMENT_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_group_method_versions::v2::DRIVE_GROUP_METHOD_VERSIONS_V2; use crate::version::drive_versions::drive_identity_method_versions::v3::DRIVE_IDENTITY_METHOD_VERSIONS_V3; use crate::version::drive_versions::drive_state_transition_method_versions::v5::DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5; use crate::version::drive_versions::drive_verify_method_versions::v4::DRIVE_VERIFY_METHOD_VERSIONS_V4; @@ -58,17 +59,25 @@ use crate::version::drive_versions::{ /// turns on `update.credit_storage_refunds_to_owners`, the primitive /// block lifecycle paths use to credit each recorded owner of a refund /// and report the amount whose owner has no balance element. +/// * **Price before commit** — `batch_operations.apply_drive_operations` +/// 1 -> 2, `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` +/// 0 -> 1) and `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (the four moderation +/// writers that can free flagged bytes, 0 -> 1). Every fee-returning +/// entry point that owns its transaction when the caller passes none +/// now prices the batch before committing, so the missing-history error +/// above never leaves a write persisted without its fee result. With a +/// caller transaction nothing changes. /// /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { - contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled + contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; the moderation removal wrappers price before committing an owned transaction document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read batch_operations: DriveBatchOperationsMethodVersion { - apply_drive_operations: 2, // changed in v10: a batch's estimate is resolved (note 14) + apply_drive_operations: 2, // changed in v10: a batch's estimate is resolved (note 14); the batch is priced before an owned transaction commits ..DRIVE_VERSION_V9.methods.batch_operations }, platform_system: DrivePlatformSystemMethodVersions { @@ -78,6 +87,7 @@ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { }, fees: DriveFeesMethodVersions { calculate_fee: 1 }, // changed in v10: fee history required and consulted for every storage refund identity: DRIVE_IDENTITY_METHOD_VERSIONS_V3, // changed in v10: credit_storage_refunds_to_owners primitive for lifecycle refund settlement + group: DRIVE_GROUP_METHOD_VERSIONS_V2, // changed in v10: add_group_action prices before committing an owned transaction ..DRIVE_VERSION_V9.methods }, ..DRIVE_VERSION_V9 diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index caf2e17b8c3..17f1261e519 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -366,6 +366,12 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// so block lifecycle paths can settle it into the current epoch's /// processing pool. /// +/// 17. **Pricing before commit**: the Drive entry points that own their +/// transaction when a caller passes none (`apply_drive_operations` v2, +/// `add_group_action` v1, the moderation removal wrappers v1) price the +/// batch before committing it, so the error in item 15 never leaves a +/// write persisted without its fee result. +/// /// Everything else matches v14. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, From 5fb247a5ba43029e724eaa37abbf6f5934e8e986 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 23 Sep 2026 01:12:13 -0500 Subject: [PATCH 18/46] test(drive): assert a rejected refund pricing leaves state untouched on both sides of the gate Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../moderation/document_removal_tests.rs | 3 +- .../src/drive/contract/moderation/tests.rs | 201 +++++++++++++++++- packages/rs-drive/src/drive/group/mod.rs | 69 ++++++ 3 files changed, 270 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/moderation/document_removal_tests.rs b/packages/rs-drive/src/drive/contract/moderation/document_removal_tests.rs index 8ac6133ff0a..f257fcd1d68 100644 --- a/packages/rs-drive/src/drive/contract/moderation/document_removal_tests.rs +++ b/packages/rs-drive/src/drive/contract/moderation/document_removal_tests.rs @@ -1304,7 +1304,8 @@ fn should_keep_refunding_a_batch_without_the_forfeiture_before_protocol_version_ .apply_drive_operations, 0 ); - // Generation 1 (protocol version 14) introduced the forfeiture; generation 2 (15) keeps it. + // Protocol version 14 introduced generation 1 and is frozen at it; later versions may + // select later generations, which carry the forfeiture forward. assert_eq!( PlatformVersion::get(14) .expect("expected protocol version 14") diff --git a/packages/rs-drive/src/drive/contract/moderation/tests.rs b/packages/rs-drive/src/drive/contract/moderation/tests.rs index 326bdee33cc..8bd91a95946 100644 --- a/packages/rs-drive/src/drive/contract/moderation/tests.rs +++ b/packages/rs-drive/src/drive/contract/moderation/tests.rs @@ -6,6 +6,7 @@ use crate::drive::contract::paths::{ CONTRACT_VERSION_KEY, CONTRACT_WARNINGS_KEY, }; use crate::drive::{Drive, RootTree}; +use crate::error::drive::DriveError; use crate::error::Error; use crate::util::batch::drive_op_batch::ContractModerationOperationType; use crate::util::batch::DriveOperation; @@ -163,6 +164,26 @@ fn unsuspend( ) } +fn unwarn( + drive: &Drive, + contract_id: Identifier, + identity_id: Identifier, + block_info: &BlockInfo, + apply: bool, + platform_version: &PlatformVersion, +) -> Result { + apply_moderation( + drive, + ContractModerationOperationType::RemoveWarnings { + contract_id, + identity_id, + }, + block_info, + apply, + platform_version, + ) +} + /// Replaces the identity's existing suspension entry. #[allow(clippy::too_many_arguments)] fn resuspend( @@ -1235,8 +1256,7 @@ fn should_warn_accumulate_clear_and_prove_the_status_and_the_entries() { }], ); - let fee = drive - .remove_contract_warnings(contract_id, target, &later, true, None, platform_version) + let fee = unwarn(&drive, contract_id, target, &later, true, platform_version) .expect("expected to clear the warnings"); assert!( fee.fee_refunds @@ -1412,3 +1432,180 @@ fn should_keep_the_documents_on_top_of_the_contract_subtree_and_the_banlist_on_t ); } } + +#[test] +fn should_leave_a_ban_in_place_when_the_bare_wrapper_cannot_price_its_removal() { + // The bare wrapper passes no fee history. Removing a moderator-flagged entry + // needs it from protocol version 15, and the wrapper now prices before it commits + // its owned transaction, so the rejected removal leaves the entry exactly as it + // was. The frozen generation at protocol version 14 still prices and removes it. + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = moderated_contract_keeping(true, true, true); + insert(&drive, &contract, platform_version); + let contract_id = contract.id(); + let moderator = contract.owner_id(); + let target = identity(0x61); + + drive + .add_contract_ban( + contract_id, + target, + &reason("spam"), + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to ban"); + drive + .add_contract_suspension( + contract_id, + target, + 10, + &reason("flooding"), + false, + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to suspend"); + drive + .add_contract_warning( + contract_id, + target, + &[warning(1, "spam")], + false, + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to warn"); + let before = root_hash(&drive, platform_version); + + for result in [ + drive.remove_contract_warnings( + contract_id, + target, + &BlockInfo::default(), + true, + None, + platform_version, + ), + drive.remove_contract_ban( + contract_id, + target, + &BlockInfo::default(), + true, + None, + platform_version, + ), + drive.remove_contract_suspension( + contract_id, + target, + &BlockInfo::default(), + true, + None, + platform_version, + ), + drive.add_contract_suspension( + contract_id, + target, + 20, + &reason("f"), + true, + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ), + ] { + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "freeing flagged bytes without a fee history must be rejected, got {:?}", + result + ); + } + assert_eq!( + root_hash(&drive, platform_version), + before, + "a rejected removal must not persist" + ); + assert_status( + &drive, + contract_id, + target, + &[ + ContractModerationList::Banlist, + ContractModerationList::Suspensions, + ContractModerationList::Warnings, + ], + ContractModerationStatus { + ban: Some(ContractBan { + reason: reason("spam"), + }), + suspension: Some(ContractSuspension { + until: 10, + reason: reason("flooding"), + }), + warnings: vec![warning(1, "spam")], + }, + ); + + // Estimation writes nothing and needs no history at any version. + let estimated = drive + .remove_contract_ban( + contract_id, + target, + &BlockInfo::default(), + false, + None, + platform_version, + ) + .expect("expected to estimate an unban"); + assert!(estimated.processing_fee > 0); + assert_eq!(root_hash(&drive, platform_version), before); + + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let drive = setup_drive_with_initial_state_structure(Some(frozen_platform_version)); + let contract = moderated_contract(true, false); + insert(&drive, &contract, frozen_platform_version); + drive + .add_contract_ban( + contract.id(), + target, + &reason("spam"), + contract.owner_id(), + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("expected to ban"); + drive + .remove_contract_ban( + contract.id(), + target, + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + assert_status( + &drive, + contract.id(), + target, + &[ContractModerationList::Banlist], + ContractModerationStatus::default(), + ); +} diff --git a/packages/rs-drive/src/drive/group/mod.rs b/packages/rs-drive/src/drive/group/mod.rs index c7973afdbd3..5ab181fb192 100644 --- a/packages/rs-drive/src/drive/group/mod.rs +++ b/packages/rs-drive/src/drive/group/mod.rs @@ -801,6 +801,37 @@ mod tests { "closing without fee history must be rejected at the latest version, got {:?}", result ); + // The wrapper owned the transaction, so the rejected close left no trace: + // the action is still active and nothing was moved to the closed tree. + let is_closed = drive + .fetch_action_is_closed( + contract_id, + 0, + action_id, + true, + None, + &mut vec![], + platform_version, + ) + .expect("expected to check if action is closed"); + assert!( + !is_closed, + "a rejected close must not persist: the action is still active" + ); + let closed_signers = drive + .fetch_action_signers( + contract_id, + 0, + GroupActionStatus::ActionClosed, + action_id, + None, + platform_version, + ) + .expect("expected to fetch closed signers"); + assert!( + closed_signers.is_empty(), + "nothing moved to the closed tree" + ); let (drive, contract_id, _identity_1_id, identity_2_id, action_id) = setup_drive_with_contract_and_action(); @@ -823,6 +854,44 @@ mod tests { .expect("protocol version 14 prices the shipped shortcut without a history"); } + #[test] + fn should_commit_the_owned_transaction_when_pricing_succeeds_at_the_latest_version() { + // An opening call frees no flagged bytes, so the wrapper prices it without a + // history at every version; generation 1 must still commit what it wrote. + let (drive, contract_id, _identity_1_id, identity_2_id, action_id) = + setup_drive_with_contract_and_action(); + let platform_version = PlatformVersion::latest(); + + let fee_result = drive + .add_group_action( + contract_id, + 0, + None, + false, + action_id, + identity_2_id, + 2, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to add the second signer"); + assert!(fee_result.processing_fee > 0); + + let signers = drive + .fetch_action_signers( + contract_id, + 0, + GroupActionStatus::ActionActive, + action_id, + None, + platform_version, + ) + .expect("expected to fetch signers"); + assert_eq!(signers.len(), 2, "the write was committed"); + } + #[test] fn should_close_group_action_with_new_action_info() { let drive = setup_drive_with_initial_state_structure(None); From 2b059a30b95f81086727e724773ca98011a38781 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Thu, 24 Sep 2026 11:55:24 -0500 Subject: [PATCH 19/46] fix(drive)!: price document and contract writes before committing the transaction drive owns The six fee-returning document wrappers (add, delete, delete by contract id, update, update by contract id, update with serialization), update_contract and apply_contract_with_serialization committed the transaction they own before pricing, so at protocol version 15 the missing-fee-history error could return with the write persisted. New generations (document wrappers v1, update_contract v3, apply_contract_with_serialization v1) hold the owned transaction until Drive::calculate_fee succeeded; the operation builders, the element writer and the _apply_and_add_to_operations methods production uses are untouched and their dispatchers accept the new numbers. The contract cache keeps its committed path under an owned transaction: contracts are looked up through the caller's transaction and a rewritten copy replaces the global entry only after the commit. DRIVE_DOCUMENT_METHOD_VERSIONS_V5 and DRIVE_CONTRACT_METHOD_VERSIONS_V5 select them in drive table v10. Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../v1/mod.rs | 18 ++- .../contract/update/update_contract/mod.rs | 23 ++- .../contract/update/update_contract/v3/mod.rs | 134 ++++++++++++++++++ .../delete_document_for_contract/mod.rs | 13 +- .../delete_document_for_contract/v1/mod.rs | 67 +++++++++ .../delete_document_for_contract_id/mod.rs | 13 +- .../delete_document_for_contract_id/v1/mod.rs | 88 ++++++++++++ .../insert/add_document_for_contract/mod.rs | 12 +- .../add_document_for_contract/v1/mod.rs | 64 +++++++++ .../update_document_for_contract/mod.rs | 15 +- .../update_document_for_contract/v1/mod.rs | 81 +++++++++++ .../update_document_for_contract_id/mod.rs | 15 +- .../update_document_for_contract_id/v1/mod.rs | 106 ++++++++++++++ .../mod.rs | 16 ++- .../v1/mod.rs | 86 +++++++++++ .../drive_contract_method_versions/v5.rs | 22 +-- .../drive_document_method_versions/v5.rs | 25 +++- .../src/version/drive_versions/v10.rs | 19 +-- .../rs-platform-version/src/version/v15.rs | 8 +- 19 files changed, 793 insertions(+), 32 deletions(-) create mode 100644 packages/rs-drive/src/drive/contract/update/update_contract/v3/mod.rs create mode 100644 packages/rs-drive/src/drive/document/delete/delete_document_for_contract/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/insert/add_document_for_contract/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/update/update_document_for_contract/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/update/update_document_for_contract_id/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/v1/mod.rs diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs index 97b265c580e..7c9d59df2e3 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs @@ -27,7 +27,15 @@ use std::borrow::Cow; impl Drive { /// Applies a contract and returns the fee for applying: generation 0's, with the operations - /// of generation 1 ([`Self::apply_contract_with_serialization_operations_v1`]). + /// of generation 1 ([`Self::apply_contract_with_serialization_operations_v1`]), and one + /// change in when it commits: when the caller supplies no transaction and the operations + /// are applied, the write and its pricing share one owned transaction that is committed + /// only after `Drive::calculate_fee` succeeded. Generation 0 applied the batch (committing + /// it on its own without a caller transaction) and priced it afterwards, so from protocol + /// version 15, where pricing an owner-attributed storage removal without the fee history is + /// an error, a call passing no history would have persisted the write and then failed. It + /// now fails before anything is written. With a caller transaction nothing is committed by + /// Drive in either generation. #[inline(always)] #[allow(clippy::too_many_arguments)] pub(super) fn apply_contract_with_serialization_v1( @@ -40,6 +48,9 @@ impl Drive { transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); let mut cost_operations = vec![]; let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); let batch_operations = self.apply_contract_with_serialization_operations_v1( @@ -61,6 +72,7 @@ impl Drive { )?; cost_operations.push(CalculatedCostOperation(fetch_cost)); + // A pricing error drops the owned transaction with everything it wrote. let fees = Drive::calculate_fee( None, Some(cost_operations), @@ -69,6 +81,10 @@ impl Drive { platform_version, None, )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) } diff --git a/packages/rs-drive/src/drive/contract/update/update_contract/mod.rs b/packages/rs-drive/src/drive/contract/update/update_contract/mod.rs index ed4c54b5d06..3b346ab5d2a 100644 --- a/packages/rs-drive/src/drive/contract/update/update_contract/mod.rs +++ b/packages/rs-drive/src/drive/contract/update/update_contract/mod.rs @@ -1,6 +1,7 @@ mod v0; mod v1; mod v2; +mod v3; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -81,9 +82,17 @@ impl Drive { platform_version, previous_fee_versions, ), + 3 => self.update_contract_v3( + contract, + block_info, + apply, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_contract".to_string(), - known_versions: vec![0, 1, 2], + known_versions: vec![0, 1, 2, 3], received: version, })), } @@ -152,7 +161,9 @@ impl Drive { drive_operations, platform_version, ), - 2 => self.update_contract_element_v2( + // Generation 3 of the wrapper changes only when its owned transaction commits; + // the element writer is generation 2's. + 2 | 3 => self.update_contract_element_v2( contract_element, contract, original_contract, @@ -163,7 +174,7 @@ impl Drive { ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_contract_element".to_string(), - known_versions: vec![0, 1, 2], + known_versions: vec![0, 1, 2, 3], received: version, })), } @@ -237,7 +248,9 @@ impl Drive { drive_operations, platform_version, ), - 2 => self.update_contract_add_operations_v2( + // Generation 3 of the wrapper changes only when its owned transaction commits; + // the batch builder is generation 2's. + 2 | 3 => self.update_contract_add_operations_v2( contract_element, contract, original_contract, @@ -249,7 +262,7 @@ impl Drive { ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_contract_add_operations".to_string(), - known_versions: vec![0, 1, 2], + known_versions: vec![0, 1, 2, 3], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/update/update_contract/v3/mod.rs b/packages/rs-drive/src/drive/contract/update/update_contract/v3/mod.rs new file mode 100644 index 00000000000..ebeaf0f4883 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/update/update_contract/v3/mod.rs @@ -0,0 +1,134 @@ +use crate::drive::contract::serialize_contract_for_storage; +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::config::v0::DataContractConfigGettersV0; +use dpp::data_contract::DataContract; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::fee::fee_result::FeeResult; +use dpp::version::PlatformVersion; +use grovedb::{Element, TransactionArg}; + +impl Drive { + /// Generation 3 differs from generation 2 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written, and the contract cache stays on its committed path: the rewritten copy + /// replaces the global entry only after the commit. With a caller transaction nothing is + /// committed by Drive in either generation and the block cache is used as before. + #[inline(always)] + pub(super) fn update_contract_v3( + &self, + contract: &DataContract, + block_info: BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + if !apply { + return self.insert_contract( + contract, + block_info, + false, + transaction, + platform_version, + ); + } + // The contract cache keys its block-versus-committed behaviour on whether a + // transaction is present. An owned transaction is not block execution, so cache + // reads and writes below stay on the committed path (`caller_transaction`), and + // the rewritten copy is seeded only once the owned transaction has committed. + let caller_transaction = transaction; + let owned_transaction = transaction + .is_none() + .then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + + let mut drive_operations: Vec = vec![]; + + // Stored compiled from protocol version 15 (v15 note 8), as generation 2 stores it + let contract_bytes = serialize_contract_for_storage(contract, platform_version)?; + + // Since we can update the contract by definition it already has storage flags + let storage_flags = Some(StorageFlags::new_single_epoch( + block_info.epoch.index, + Some(contract.owner_id().to_buffer()), + )); + + let contract_element = Element::Item( + contract_bytes, + StorageFlags::map_to_some_element_flags(storage_flags.as_ref()), + ); + + let original_contract_fetch_info = self + .get_contract_with_fetch_info_and_add_to_operations( + contract.id().to_buffer(), + Some(&block_info.epoch), + true, + caller_transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( + "contract should exist", + )))?; + + if original_contract_fetch_info.contract.config().readonly() { + return Err(Error::Drive(DriveError::UpdatingReadOnlyImmutableContract( + "original contract is readonly", + ))); + } + + self.update_contract_element( + contract_element, + contract, + &original_contract_fetch_info.contract, + &block_info, + transaction, + &mut drive_operations, + platform_version, + )?; + + // Update DataContracts cache with the new contract + let updated_contract_fetch_info = self + .fetch_contract_and_add_operations( + contract.id().to_buffer(), + Some(&block_info.epoch), + transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( + "contract should exist", + )))?; + + // A pricing error drops the owned transaction with everything it wrote, and the + // cache never learns of the rewrite. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + // Update DataContracts cache with the new contract + self.cache + .data_contracts + .insert_rewritten(updated_contract_fetch_info, caller_transaction.is_some()); + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/mod.rs index 616d27de6c6..f28fd284ba7 100644 --- a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -59,9 +60,19 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.delete_document_for_contract_v1( + document_id, + contract, + document_type_name, + block_info, + apply, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "delete_document_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/v1/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/v1/mod.rs new file mode 100644 index 00000000000..5b078004ffb --- /dev/null +++ b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract/v1/mod.rs @@ -0,0 +1,67 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::DataContract; +use dpp::fee::fee_result::FeeResult; + +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn delete_document_for_contract_v1( + &self, + document_id: Identifier, + contract: &DataContract, + document_type_name: &str, + block_info: BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut drive_operations: Vec = vec![]; + let estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + self.delete_document_for_contract_apply_and_add_to_operations( + document_id, + contract, + document_type_name, + estimated_costs_only_with_layer_info, + block_info.time_ms, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/mod.rs index 3737d85c356..1a70a431e92 100644 --- a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use grovedb::TransactionArg; @@ -60,9 +61,19 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.delete_document_for_contract_id_v1( + document_id, + contract_id, + document_type_name, + block_info, + apply, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "delete_document_for_contract_id".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/v1/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/v1/mod.rs new file mode 100644 index 00000000000..a97bf605a41 --- /dev/null +++ b/packages/rs-drive/src/drive/document/delete/delete_document_for_contract_id/v1/mod.rs @@ -0,0 +1,88 @@ +use grovedb::TransactionArg; + +use crate::drive::Drive; +use crate::error::document::DocumentError; +use dpp::block::block_info::BlockInfo; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; + +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn delete_document_for_contract_id_v1( + &self, + document_id: Identifier, + contract_id: Identifier, + document_type_name: &str, + block_info: BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + // The contract cache keys its block-versus-committed behaviour on whether a + // transaction is present. An owned transaction is not block execution, so the + // contract is looked up through the caller's (`caller_transaction`) and only the + // document write goes through the owned one. + let caller_transaction = transaction; + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + let mut drive_operations: Vec = vec![]; + let estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let contract_fetch_info = self + .get_contract_with_fetch_info_and_add_to_operations( + contract_id.to_buffer(), + Some(&block_info.epoch), + true, + caller_transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Document(DocumentError::DataContractNotFound))?; + + let contract = &contract_fetch_info.contract; + + self.delete_document_for_contract_apply_and_add_to_operations( + document_id, + contract, + document_type_name, + estimated_costs_only_with_layer_info, + block_info.time_ms, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/mod.rs b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/mod.rs index 0b8f6154b19..bc580abe3f0 100644 --- a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::util::object_size_info::DocumentAndContractInfo; @@ -54,9 +55,18 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.add_document_for_contract_v1( + document_and_contract_info, + override_document, + block_info, + apply, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_document_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/insert/add_document_for_contract/v1/mod.rs b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/v1/mod.rs new file mode 100644 index 00000000000..1a2ded38cc6 --- /dev/null +++ b/packages/rs-drive/src/drive/document/insert/add_document_for_contract/v1/mod.rs @@ -0,0 +1,64 @@ +use crate::drive::Drive; +use crate::util::object_size_info::DocumentAndContractInfo; + +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use dpp::block::block_info::BlockInfo; +use dpp::fee::fee_result::FeeResult; + +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_document_for_contract_v1( + &self, + document_and_contract_info: DocumentAndContractInfo, + override_document: bool, + block_info: BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut drive_operations: Vec = vec![]; + self.add_document_for_contract_apply_and_add_to_operations( + document_and_contract_info, + override_document, + &block_info, + true, + apply, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/update/update_document_for_contract/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_for_contract/mod.rs index 5c3658a7b2f..a549b6dfebe 100644 --- a/packages/rs-drive/src/drive/document/update/update_document_for_contract/mod.rs +++ b/packages/rs-drive/src/drive/document/update/update_document_for_contract/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::util::storage_flags::StorageFlags; @@ -68,9 +69,21 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.update_document_for_contract_v1( + document, + contract, + document_type, + owner_id, + block_info, + apply, + storage_flags, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_document_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/update/update_document_for_contract/v1/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_for_contract/v1/mod.rs new file mode 100644 index 00000000000..484c6e8bb4b --- /dev/null +++ b/packages/rs-drive/src/drive/document/update/update_document_for_contract/v1/mod.rs @@ -0,0 +1,81 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentInfo::DocumentRefInfo; +use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::document_type::DocumentTypeRef; +use dpp::data_contract::DataContract; +use dpp::document::Document; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::fee::fee_result::FeeResult; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use std::borrow::Cow; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn update_document_for_contract_v1( + &self, + document: &Document, + contract: &DataContract, + document_type: DocumentTypeRef, + owner_id: Option<[u8; 32]>, + block_info: BlockInfo, + apply: bool, + storage_flags: Option>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut drive_operations: Vec = vec![]; + let estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let document_info = DocumentRefInfo((document, storage_flags)); + + self.update_document_for_contract_apply_and_add_to_operations( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info, + owner_id, + }, + contract, + document_type, + }, + &block_info, + estimated_costs_only_with_layer_info, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/mod.rs index d6563600f27..88814c194c7 100644 --- a/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/mod.rs +++ b/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::util::storage_flags::StorageFlags; @@ -66,9 +67,21 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.update_document_for_contract_id_v1( + serialized_document, + contract_id, + document_type, + owner_id, + block_info, + apply, + storage_flags, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_document_for_contract_id".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/v1/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/v1/mod.rs new file mode 100644 index 00000000000..823cfa27880 --- /dev/null +++ b/packages/rs-drive/src/drive/document/update/update_document_for_contract_id/v1/mod.rs @@ -0,0 +1,106 @@ +use crate::drive::Drive; +use crate::error::document::DocumentError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentInfo::DocumentRefAndSerialization; +use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::accessors::v0::DataContractV0Getters; + +use dpp::document::serialization_traits::DocumentPlatformConversionMethodsV0; +use dpp::document::Document; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::fee::fee_result::FeeResult; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use std::borrow::Cow; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn update_document_for_contract_id_v1( + &self, + serialized_document: &[u8], + contract_id: [u8; 32], + document_type: &str, + owner_id: Option<[u8; 32]>, + block_info: BlockInfo, + apply: bool, + storage_flags: Option>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + // The contract cache keys its block-versus-committed behaviour on whether a + // transaction is present. An owned transaction is not block execution, so the + // contract is looked up through the caller's (`caller_transaction`) and only the + // document write goes through the owned one. + let caller_transaction = transaction; + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + let mut drive_operations: Vec = vec![]; + let estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let contract_fetch_info = self + .get_contract_with_fetch_info_and_add_to_operations( + contract_id, + Some(&block_info.epoch), + true, + caller_transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Document(DocumentError::DataContractNotFound))?; + + let contract = &contract_fetch_info.contract; + + let document_type = contract.document_type_for_name(document_type)?; + + let document = Document::from_bytes(serialized_document, document_type, platform_version)?; + + let document_info = + DocumentRefAndSerialization((&document, serialized_document, storage_flags)); + + self.update_document_for_contract_apply_and_add_to_operations( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info, + owner_id, + }, + contract, + document_type, + }, + &block_info, + estimated_costs_only_with_layer_info, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/mod.rs index b6f5740684e..bdbfc6e2e4f 100644 --- a/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/mod.rs +++ b/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::util::storage_flags::StorageFlags; @@ -70,9 +71,22 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.update_document_with_serialization_for_contract_v1( + document, + serialized_document, + contract, + document_type_name, + owner_id, + block_info, + apply, + storage_flags, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_document_with_serialization_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/v1/mod.rs b/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/v1/mod.rs new file mode 100644 index 00000000000..f5cc0b7becc --- /dev/null +++ b/packages/rs-drive/src/drive/document/update/update_document_with_serialization_for_contract/v1/mod.rs @@ -0,0 +1,86 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentInfo::DocumentRefAndSerialization; +use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::DataContract; +use dpp::document::Document; +use dpp::fee::fee_result::FeeResult; + +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use std::borrow::Cow; + +impl Drive { + /// Generation 1 differs from the previous one in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Earlier generations applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn update_document_with_serialization_for_contract_v1( + &self, + document: &Document, + serialized_document: &[u8], + contract: &DataContract, + document_type_name: &str, + owner_id: Option<[u8; 32]>, + block_info: BlockInfo, + apply: bool, + storage_flags: Option>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut drive_operations: Vec = vec![]; + let estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let document_type = contract.document_type_for_name(document_type_name)?; + + let document_info = + DocumentRefAndSerialization((document, serialized_document, storage_flags)); + + self.update_document_for_contract_apply_and_add_to_operations( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info, + owner_id, + }, + contract, + document_type, + }, + &block_info, + estimated_costs_only_with_layer_info, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs index c40beca0b47..b2f934a0bb2 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs @@ -1,7 +1,7 @@ use crate::version::drive_versions::drive_contract_method_versions::v4::DRIVE_CONTRACT_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_contract_method_versions::{ DriveContractApplyMethodVersions, DriveContractMethodVersions, - DriveContractModerationMethodVersions, + DriveContractModerationMethodVersions, DriveContractUpdateMethodVersions, }; /// Drive contract methods for protocol version 15. @@ -13,20 +13,26 @@ use crate::version::drive_versions::drive_contract_method_versions::{ /// differ from the ones it was stored with in format version 1. v1 writes nothing when the /// two contracts' format version 1 bytes match, where v0 compared the stored bytes alone and /// would have rewritten an unchanged contract as an update. -/// * The four moderation writers that can free moderator-flagged bytes (`remove_contract_ban`, -/// `remove_contract_suspension`, `remove_contract_warnings`, and `add_contract_suspension`, -/// whose replacement of an existing entry may shrink it) are bumped to `1`: when the caller -/// passes no transaction, the fee-returning wrapper writes and prices inside one owned -/// transaction and commits it only once `Drive::calculate_fee` succeeded. Pricing an +/// * Every fee-returning contract writer that can free owner- or moderator-flagged bytes gets +/// a generation that, when the caller passes no transaction, writes and prices inside one +/// owned transaction and commits it only once `Drive::calculate_fee` succeeded: +/// `update.update_contract` 2 -> 3 (the element writer stays at generation 2), +/// `apply.apply_contract_with_serialization` 1 (the generation above; its operation builder +/// is the compiled-storage one), and the four moderation writers `remove_contract_ban`, +/// `remove_contract_suspension`, `remove_contract_warnings` and `add_contract_suspension` +/// (whose replacement of an existing entry may shrink it) 0 -> 1. Pricing an /// owner-attributed storage removal without the fee history is an error from this version, -/// and generation 0 committed the write before that error surfaced. The operation builders -/// are unchanged. +/// and the earlier generations committed the write before that error surfaced. pub const DRIVE_CONTRACT_METHOD_VERSIONS_V5: DriveContractMethodVersions = DriveContractMethodVersions { apply: DriveContractApplyMethodVersions { apply_contract_with_serialization: 1, ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.apply }, + update: DriveContractUpdateMethodVersions { + update_contract: 3, + ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.update + }, moderation: DriveContractModerationMethodVersions { remove_contract_ban: 1, add_contract_suspension: 1, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs index 7758114f0e1..82b842d9b30 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs @@ -1,7 +1,8 @@ use crate::version::drive_versions::drive_document_method_versions::v4::DRIVE_DOCUMENT_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_document_method_versions::{ DriveDocumentDeleteMethodVersions, DriveDocumentEstimationCostsMethodVersions, - DriveDocumentMethodVersions, + DriveDocumentInsertMethodVersions, DriveDocumentMethodVersions, + DriveDocumentUpdateMethodVersions, }; /// V5 is protocol version 15's document-method table. Relative to V4: @@ -17,13 +18,35 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// the insert's does, not as subtrees. /// * `estimation_costs.stateless_delete_of_non_tree_for_costs` is 1: a delete's estimate prices /// each height of the climb with the layer GroveDB deletes in there, not the one above it. +/// * The six fee-returning wrappers (`add_document_for_contract`, +/// `delete_document_for_contract`, `delete_document_for_contract_id`, +/// `update_document_for_contract`, `update_document_for_contract_id` and +/// `update_document_with_serialization_for_contract`) are bumped to `1`: when the caller +/// passes no transaction they write and price inside one owned transaction and commit it +/// only once `Drive::calculate_fee` succeeded. Pricing an owner-attributed storage +/// removal without the fee history is an error from this version, and generation 0 +/// committed the write before that error surfaced. The operation builders and the +/// `_apply_and_add_to_operations` methods every production caller uses through +/// `apply_drive_operations` are unchanged. pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = DriveDocumentMethodVersions { + update: DriveDocumentUpdateMethodVersions { + update_document_for_contract: 1, + update_document_for_contract_id: 1, + update_document_with_serialization_for_contract: 1, + ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.update + }, + insert: DriveDocumentInsertMethodVersions { + add_document_for_contract: 1, + ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.insert + }, insert_empty_private_document_store_operation: Some(0), // changed: a private document type's store is created with it fetch_private_document_entries: Some(0), // changed: a page of a store's entries can be read prove_private_document_entries: Some(0), // changed: and proved delete: DriveDocumentDeleteMethodVersions { remove_reference_for_index_level_for_contract_operations: 2, // changed: the entries layer is described as references + delete_document_for_contract: 1, + delete_document_for_contract_id: 1, ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.delete }, estimation_costs: DriveDocumentEstimationCostsMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 66cf1daf03a..cd1264c88a7 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -61,18 +61,21 @@ use crate::version::drive_versions::{ /// and report the amount whose owner has no balance element. /// * **Price before commit** — `batch_operations.apply_drive_operations` /// 1 -> 2, `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` -/// 0 -> 1) and `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (the four moderation -/// writers that can free flagged bytes, 0 -> 1). Every fee-returning -/// entry point that owns its transaction when the caller passes none -/// now prices the batch before committing, so the missing-history error -/// above never leaves a write persisted without its fee result. With a -/// caller transaction nothing changes. +/// 0 -> 1), `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (`update_contract` 2 -> 3, +/// `apply_contract_with_serialization` 0 -> 1, the four moderation +/// writers that can free flagged bytes 0 -> 1) and +/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the six fee-returning document +/// wrappers 0 -> 1). Every fee-returning entry point that owns its +/// transaction when the caller passes none now prices the batch before +/// committing, so the missing-history error above never leaves a write +/// persisted without its fee result. With a caller transaction nothing +/// changes. /// /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { - contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; the moderation removal wrappers price before committing an owned transaction - document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates + contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; update_contract v3, apply_contract_with_serialization v1 and the moderation writers price before committing an owned transaction + document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the six fee-returning document wrappers price before committing an owned transaction vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 17f1261e519..2f6bf1194af 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -368,9 +368,11 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// /// 17. **Pricing before commit**: the Drive entry points that own their /// transaction when a caller passes none (`apply_drive_operations` v2, -/// `add_group_action` v1, the moderation removal wrappers v1) price the -/// batch before committing it, so the error in item 15 never leaves a -/// write persisted without its fee result. +/// the six document wrappers v1, `update_contract` v3, +/// `apply_contract_with_serialization` v1, `add_group_action` v1, the +/// moderation writers v1) price the batch before committing it, so the +/// error in item 15 never leaves a write persisted without its fee +/// result. /// /// Everything else matches v14. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { From 11b781da15ad0bfd28abe60644b85b611a4f8b2e Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Thu, 24 Sep 2026 11:55:24 -0500 Subject: [PATCH 20/46] test(drive): assert rejected document and contract pricing leaves state and cache untouched Refs #4675, Refs #4689 Co-Authored-By: Claude Fable 5.1 --- .../document_query/v1/dispatch/chained.rs | 8 +- packages/rs-drive/src/drive/contract/mod.rs | 110 ++++++++++++ .../rs-drive/src/drive/document/delete/mod.rs | 169 +++++++++++++++++- 3 files changed, 285 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive-abci/src/query/document_query/v1/dispatch/chained.rs b/packages/rs-drive-abci/src/query/document_query/v1/dispatch/chained.rs index 1cd6c495b8b..0fed02ec311 100644 --- a/packages/rs-drive-abci/src/query/document_query/v1/dispatch/chained.rs +++ b/packages/rs-drive-abci/src/query/document_query/v1/dispatch/chained.rs @@ -291,11 +291,14 @@ mod tests { use dpp::dashcore::Network; use dpp::data_contract::document_type::random_document::CreateRandomDocument; use dpp::document::{Document, DocumentV0Getters, DocumentV0Setters}; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::identifier::Identifier; use dpp::platform_value::Value; use dpp::tests::json_document::json_document_to_contract; + use dpp::version::fee::FeeVersion; use drive::drive::contract::moderation::types::ContractDocumentRemovalEntry; use drive::util::test_helpers::with_likes_read_whole_through_by_liker; + use std::collections::BTreeMap; const YAPPR_CONTRACT_PATH: &str = "../rs-drive/tests/supporting_files/contract/yappr-likes/yappr-likes-contract.json"; @@ -555,6 +558,9 @@ mod tests { fn should_report_a_deleted_post_of_a_deletable_document_join() { let (platform, state, version, contract) = setup_yappr_state_at(YAPPR_DELETABLE_POSTS_CONTRACT_PATH); + // The post is owner-flagged, so pricing its removal needs the fee history of the + // removing block, as every production caller passes. + let fee_history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); platform .drive .delete_document_for_contract( @@ -565,7 +571,7 @@ mod tests { true, None, version, - None, + Some(&fee_history), ) .expect("expected to delete the post"); diff --git a/packages/rs-drive/src/drive/contract/mod.rs b/packages/rs-drive/src/drive/contract/mod.rs index 33767c67540..55ead038425 100644 --- a/packages/rs-drive/src/drive/contract/mod.rs +++ b/packages/rs-drive/src/drive/contract/mod.rs @@ -90,7 +90,10 @@ mod tests { use crate::drive::identity::key::fetch::{IdentityKeysRequest, KeyIDIdentityPublicKeyPairVec}; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; + use std::collections::BTreeMap; #[allow(dead_code)] #[deprecated(note = "This function is marked as unused.")] @@ -952,6 +955,113 @@ mod tests { ); } + /// A contract update rewrites the owner-flagged contract item, so pricing it without the + /// fee history is rejected from protocol version 15. The wrapper owns its transaction when + /// the caller passes none, so the rejected update leaves the stored contract, the cached + /// copy and the root hash as they were; with the history it commits and the cache follows. + #[test] + fn should_leave_a_contract_in_place_when_the_wrapper_cannot_price_its_update() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + // `insert_contract` flags a mutable contract's item with its owner, so an update + // rewrites owner-attributed bytes. + let contract = json_document_to_contract( + "tests/supporting_files/contract/references/references.json", + false, + platform_version, + ) + .expect("expected to get a contract"); + drive + .insert_contract( + &contract, + BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to insert the contract"); + let contract_id = contract.id().to_buffer(); + let root_hash = |drive: &Drive| { + drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("expected a root hash") + }; + let cached_version = |drive: &Drive| { + drive + .get_cached_contract_with_fetch_info(contract_id, None, &platform_version.drive) + .expect("expected the cache lookup") + .map(|fetch_info| fetch_info.contract.version()) + }; + // Warm the committed cache so a rejected update has a stale copy to leave alone. + drive + .get_contract_with_fetch_info_and_fee(contract_id, None, true, None, platform_version) + .expect("expected to fetch the contract"); + assert_eq!(cached_version(&drive), Some(contract.version())); + let before = root_hash(&drive); + + // The rewrite shrinks the contract item: a smaller `note` schema replaces the + // large one, so owner-flagged bytes are freed and must be priced with the history. + let mut updated = contract.clone(); + updated.set_version(contract.version() + 1); + let note_schema = platform_value!({ + "type": "object", + "properties": { + "abc0": {"type": "string", "maxLength": 63, "position": 0} + }, + "additionalProperties": false, + }); + updated + .set_document_schema("note", note_schema, true, &mut vec![], platform_version) + .expect("should set a document schema"); + + let result = drive.update_contract( + &updated, + BlockInfo::default(), + true, + None, + platform_version, + None, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "rewriting owner-flagged bytes without a fee history must be rejected, got {:?}", + result + ); + assert_eq!( + root_hash(&drive), + before, + "a rejected update must not persist" + ); + assert_eq!( + cached_version(&drive), + Some(contract.version()), + "the cache must not learn of a rewrite that was not committed" + ); + + let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); + drive + .update_contract( + &updated, + BlockInfo::default(), + true, + None, + platform_version, + Some(&history), + ) + .expect("expected to update the contract with the fee history"); + assert_ne!(root_hash(&drive), before, "the update was committed"); + assert_eq!( + cached_version(&drive), + Some(updated.version()), + "the committed rewrite replaced the cached copy" + ); + } + #[test] fn test_update_contract_errors_on_readonly() { let drive = setup_drive_with_initial_state_structure(None); diff --git a/packages/rs-drive/src/drive/document/delete/mod.rs b/packages/rs-drive/src/drive/document/delete/mod.rs index ed79914b353..69e3c615fc9 100644 --- a/packages/rs-drive/src/drive/document/delete/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/mod.rs @@ -62,6 +62,7 @@ mod tests { use crate::config::DriveConfig; use crate::drive::document::tests::setup_dashpay; + use crate::drive::Drive; use crate::error::drive::DriveError; use crate::error::Error; use crate::util::object_size_info::DocumentInfo::DocumentRefInfo; @@ -74,7 +75,7 @@ mod tests { use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::DataContract; use dpp::document::serialization_traits::DocumentPlatformConversionMethodsV0; - use dpp::document::Document; + use dpp::document::{Document, DocumentV0Getters}; use dpp::fee::default_costs::KnownCostItem::StorageDiskUsageCreditPerByte; use dpp::fee::default_costs::{CachedEpochIndexFeeVersions, EpochCosts}; use dpp::identifier::Identifier; @@ -856,6 +857,172 @@ mod tests { .expect("expected to be able to delete the document"); } + /// Deleting an owner-flagged document without the fee history is rejected from + /// protocol version 15, and the public wrapper, owning its transaction when the caller + /// passes none, rejects it before anything is written: the document is still there and + /// the root hash is unchanged. Protocol version 14 still deletes it. + #[test] + fn should_leave_a_document_in_place_when_the_wrapper_cannot_price_its_removal() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + + let contract = setup_contract( + &drive, + "tests/supporting_files/contract/family/family-contract-reduced.json", + None, + None, + None::, + None, + Some(platform_version), + ); + let document_type = contract + .document_type_for_name("person") + .expect("expected to get document type"); + let owner_id = rand::thread_rng().gen::<[u8; 32]>(); + let person_document = json_document_to_document( + "tests/supporting_files/contract/family/person0.json", + Some(owner_id.into()), + document_type, + platform_version, + ) + .expect("expected to get document"); + let storage_flags = Some(Cow::Owned(StorageFlags::SingleEpochOwned(0, owner_id))); + drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo((&person_document, storage_flags)), + owner_id: Some(owner_id), + }, + contract: &contract, + document_type, + }, + false, + BlockInfo::default(), + true, + None, + platform_version, + None, + ) + .expect("expected to insert a document successfully"); + let root_hash = |drive: &Drive| { + drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("expected a root hash") + }; + let before = root_hash(&drive); + + let result = drive.delete_document_for_contract( + person_document.id(), + &contract, + "person", + BlockInfo::default(), + true, + None, + platform_version, + None, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "deleting owner-flagged bytes without a fee history must be rejected, got {:?}", + result + ); + assert_eq!( + root_hash(&drive), + before, + "a rejected delete must not persist" + ); + let query = DriveDocumentQuery::from_sql_expr( + "select * from person where firstName = 'Samuel' order by firstName asc limit 100", + &contract, + Some(&DriveConfig::default()), + platform_version, + ) + .expect("should build query"); + let (results, _, _) = query + .execute_raw_results_no_proof(&drive, None, None, platform_version) + .expect("expected to execute query"); + assert_eq!(results.len(), 1, "the document is still there"); + + // The same delete with the block's history is priced, refunds the owner and commits. + let fee_result = drive + .delete_document_for_contract( + person_document.id(), + &contract, + "person", + BlockInfo::default(), + true, + None, + platform_version, + Some(&EPOCH_CHANGE_FEE_VERSION_TEST), + ) + .expect("expected to delete the document with the fee history"); + assert!(fee_result.fee_refunds.get(&owner_id).is_some()); + let (results, _, _) = query + .execute_raw_results_no_proof(&drive, None, None, platform_version) + .expect("expected to execute query"); + assert!(results.is_empty(), "the delete was committed"); + + // Protocol version 14 prices the shipped shortcut without a history and commits. + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let drive = setup_drive_with_initial_state_structure(Some(frozen_platform_version)); + let contract = setup_contract( + &drive, + "tests/supporting_files/contract/family/family-contract-reduced.json", + None, + None, + None::, + None, + Some(frozen_platform_version), + ); + let document_type = contract + .document_type_for_name("person") + .expect("expected to get document type"); + let person_document = json_document_to_document( + "tests/supporting_files/contract/family/person0.json", + Some(owner_id.into()), + document_type, + frozen_platform_version, + ) + .expect("expected to get document"); + let storage_flags = Some(Cow::Owned(StorageFlags::SingleEpochOwned(0, owner_id))); + drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo((&person_document, storage_flags)), + owner_id: Some(owner_id), + }, + contract: &contract, + document_type, + }, + false, + BlockInfo::default(), + true, + None, + frozen_platform_version, + None, + ) + .expect("expected to insert a document successfully"); + drive + .delete_document_for_contract( + person_document.id(), + &contract, + "person", + BlockInfo::default(), + true, + None, + frozen_platform_version, + None, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + } + #[test] fn test_delete_dashpay_documents() { let drive = setup_drive_with_initial_state_structure(None); From 27628d9712d34d156b1faace930cca9832bcb0b1 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 03:41:35 -0500 Subject: [PATCH 21/46] fix(drive)!: price add_document, index-only deletes and warning replacements before committing Three fee-returning Drive writers still applied their batch, committing the transaction they own when the caller passes none, and priced it afterwards: add_document (whose override of an owner-flagged document can shrink it), the index-only document deletion wrapper (which removes owner-flagged index entries) and add_contract_warning (whose replacement of an existing entry can shrink it). From protocol version 15 pricing an owner-attributed storage removal without the fee history is an error, so a caller passing no history got the error after the write was durable. Each gets a generation 1 that starts the owned transaction itself, applies the batch through it, prices it and commits only once pricing succeeded. The operation builders are copied unchanged; the index-only operations builder stays at generation 0. Protocol version 15's document and contract method tables select the new generations, generation 0 is untouched for protocol version 14 and earlier. Co-Authored-By: Claude Fable 5.1 --- .../moderation/add_contract_warning/mod.rs | 27 ++- .../moderation/add_contract_warning/v1/mod.rs | 155 ++++++++++++++++++ .../mod.rs | 13 +- .../v1/mod.rs | 79 +++++++++ .../drive/document/insert/add_document/mod.rs | 13 +- .../document/insert/add_document/v1/mod.rs | 93 +++++++++++ .../drive_contract_method_versions/v5.rs | 11 +- .../drive_document_method_versions/v5.rs | 7 +- .../src/version/drive_versions/v10.rs | 6 +- .../rs-platform-version/src/version/v15.rs | 2 +- 10 files changed, 391 insertions(+), 15 deletions(-) create mode 100644 packages/rs-drive/src/drive/contract/moderation/add_contract_warning/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs diff --git a/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/mod.rs b/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/mod.rs index 75a940dc5d1..cb5ccdef6d7 100644 --- a/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/mod.rs +++ b/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -70,9 +71,20 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contract_warning_v1( + contract_id, + identity_id, + warnings, + replaces_existing, + moderator_id, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contract_warning".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -128,9 +140,20 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contract_warning_operations_v1( + contract_id, + identity_id, + warnings, + replaces_existing, + moderator_id, + block_info, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contract_warning_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/v1/mod.rs b/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/v1/mod.rs new file mode 100644 index 00000000000..73f48620854 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/moderation/add_contract_warning/v1/mod.rs @@ -0,0 +1,155 @@ +use crate::drive::contract::moderation::types::encode_warnings; +use crate::drive::contract::paths::contract_moderation_list_path; +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::PathKeyElementInfo::PathFixedSizeKeyRefElement; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::config::moderation::{ContractModerationList, ContractWarning}; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; +use dpp::version::PlatformVersion; +use grovedb::Element; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contract_warning_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + warnings: &[ContractWarning], + replaces_existing: bool, + moderator_id: Identifier, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + let batch_operations = self.add_contract_warning_operations_v1( + contract_id, + identity_id, + warnings, + replaces_existing, + moderator_id, + block_info, + &mut estimated_costs_only_with_layer_info, + transaction, + platform_version, + )?; + + let mut drive_operations: Vec = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } + + /// A warning list entry is every warning the identity carries, oldest first, each its + /// block time and its reason (see [`encode_warnings`]), under the identity's id, flagged + /// with the moderator's identity so the storage refund on removal goes back to whoever + /// paid. A warn on an identity that already carries warnings replaces the entry in place + /// with one warning more; two operations on one key would fail the batch. The entry is + /// then longer, so its flags follow GroveDB's flag merge for a grown item: it passes, with + /// the refund of its removal, to the moderator that warned last, who pays for the bytes + /// the warning added. + /// + /// An estimate prices the replacement as a fresh insert. GroveDB's average-case replace + /// assumes an item keeps its size and would price no storage for the added warning, which + /// the moderator's balance is then not checked against; the whole entry is an upper bound. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contract_warning_operations_v1( + &self, + contract_id: Identifier, + identity_id: Identifier, + warnings: &[ContractWarning], + replaces_existing: bool, + moderator_id: Identifier, + block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, + _transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let estimating = estimated_costs_only_with_layer_info.is_some(); + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Drive::add_estimation_costs_for_contract_moderation_entry( + contract_id.to_buffer(), + ContractModerationList::Warnings, + estimated_costs_only_with_layer_info, + &platform_version.drive, + )?; + } + + let storage_flags = + StorageFlags::new_single_epoch(block_info.epoch.index, Some(moderator_id.to_buffer())); + + let path_key_element = PathFixedSizeKeyRefElement(( + contract_moderation_list_path(contract_id.as_slice(), ContractModerationList::Warnings), + identity_id.as_slice(), + Element::new_item_with_flags( + // Every reason was bounded by basic structure validation, so an encoding + // refusal is a code path that lost that guarantee, not a moderator's mistake. + encode_warnings(warnings).map_err(|_| { + Error::Drive(DriveError::CorruptedCodeExecution( + "a warning's reason is longer than a warning list entry can hold", + )) + })?, + storage_flags.to_some_element_flags(), + ), + )); + + let mut batch_operations: Vec = vec![]; + if replaces_existing && !estimating { + self.batch_replace( + path_key_element, + &mut batch_operations, + &platform_version.drive, + )?; + } else { + self.batch_insert( + path_key_element, + &mut batch_operations, + &platform_version.drive, + )?; + } + + Ok(batch_operations) + } +} diff --git a/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/mod.rs index 40afd5dc44d..aa95021d71b 100644 --- a/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -192,9 +193,19 @@ impl Drive { platform_version, previous_fee_versions, ), + 1 => self.delete_index_only_document_for_contract_v1( + document, + contract, + document_type, + block_info, + apply, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "delete_index_only_document_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/v1/mod.rs b/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/v1/mod.rs new file mode 100644 index 00000000000..c0c54bca1bb --- /dev/null +++ b/packages/rs-drive/src/drive/document/delete/delete_index_only_document_for_contract_operations/v1/mod.rs @@ -0,0 +1,79 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::document_type::DocumentTypeRef; +use dpp::data_contract::DataContract; +use dpp::document::Document; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use dpp::fee::fee_result::FeeResult; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 committed its owned transaction and priced afterwards, so from protocol + /// version 15, where pricing an owner-attributed storage removal without the fee history + /// is an error, a call passing no history persisted the write and then failed. It now + /// fails before anything is written. With a caller transaction nothing is committed by + /// Drive in either generation. + #[allow(clippy::too_many_arguments)] + pub(super) fn delete_index_only_document_for_contract_v1( + &self, + document: Document, + contract: &DataContract, + document_type: DocumentTypeRef, + block_info: BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + let mut drive_operations: Vec = vec![]; + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + // With no caller transaction, TTL preparation (direct drainage + // writes) inside the operations builder and the apply below would + // each commit on their own: a tuple failing the row-commitment gate + // after preparation would leave drained buckets committed. Span + // both with one owned transaction. + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let batch_operations = self.delete_index_only_document_for_contract_operations( + document, + contract, + document_type, + None, + &mut estimated_costs_only_with_layer_info, + block_info.time_ms, + transaction, + platform_version, + )?; + + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/insert/add_document/mod.rs b/packages/rs-drive/src/drive/document/insert/add_document/mod.rs index f118660eb6b..406e3a1defc 100644 --- a/packages/rs-drive/src/drive/document/insert/add_document/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/add_document/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::util::object_size_info::OwnedDocumentInfo; @@ -52,9 +53,19 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_document_v1( + owned_document_info, + data_contract_id, + document_type_name, + override_document, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_document".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs b/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs new file mode 100644 index 00000000000..eb10811f52a --- /dev/null +++ b/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs @@ -0,0 +1,93 @@ +use crate::drive::Drive; +use crate::error::document::DocumentError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::fee::fee_result::FeeResult; +use dpp::identifier::Identifier; + +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed. It now fails before anything is + /// written. With a caller transaction nothing is committed by Drive in either + /// generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_document_v1( + &self, + owned_document_info: OwnedDocumentInfo, + data_contract_id: Identifier, + document_type_name: &str, + override_document: bool, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + // The contract cache keys its block-versus-committed behaviour on whether a + // transaction is present. An owned transaction is not block execution, so the + // contract is looked up through the caller's (`caller_transaction`) and only the + // document write goes through the owned one. + let caller_transaction = transaction; + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + + let mut drive_operations: Vec = vec![]; + + let contract_fetch_info = self + .get_contract_with_fetch_info_and_add_to_operations( + data_contract_id.into_buffer(), + Some(&block_info.epoch), + true, + caller_transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Document(DocumentError::DataContractNotFound))?; + + let contract = &contract_fetch_info.contract; + + let document_type = contract.document_type_for_name(document_type_name)?; + + let document_and_contract_info = DocumentAndContractInfo { + owned_document_info, + contract, + document_type, + }; + let mut drive_operations: Vec = vec![]; + self.add_document_for_contract_apply_and_add_to_operations( + document_and_contract_info, + override_document, + block_info, + true, + apply, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs index b2f934a0bb2..6cae7ebbc3b 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs @@ -18,11 +18,11 @@ use crate::version::drive_versions::drive_contract_method_versions::{ /// owned transaction and commits it only once `Drive::calculate_fee` succeeded: /// `update.update_contract` 2 -> 3 (the element writer stays at generation 2), /// `apply.apply_contract_with_serialization` 1 (the generation above; its operation builder -/// is the compiled-storage one), and the four moderation writers `remove_contract_ban`, -/// `remove_contract_suspension`, `remove_contract_warnings` and `add_contract_suspension` -/// (whose replacement of an existing entry may shrink it) 0 -> 1. Pricing an -/// owner-attributed storage removal without the fee history is an error from this version, -/// and the earlier generations committed the write before that error surfaced. +/// is the compiled-storage one), and the five moderation writers `remove_contract_ban`, +/// `remove_contract_suspension`, `remove_contract_warnings`, `add_contract_suspension` and +/// `add_contract_warning` (whose replacement of an existing entry may shrink it) 0 -> 1. +/// Pricing an owner-attributed storage removal without the fee history is an error from this +/// version, and the earlier generations committed the write before that error surfaced. pub const DRIVE_CONTRACT_METHOD_VERSIONS_V5: DriveContractMethodVersions = DriveContractMethodVersions { apply: DriveContractApplyMethodVersions { @@ -38,6 +38,7 @@ pub const DRIVE_CONTRACT_METHOD_VERSIONS_V5: DriveContractMethodVersions = add_contract_suspension: 1, remove_contract_suspension: 1, remove_contract_warnings: 1, + add_contract_warning: 1, ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.moderation }, ..DRIVE_CONTRACT_METHOD_VERSIONS_V4 diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs index 82b842d9b30..958d78f0d42 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs @@ -18,9 +18,10 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// the insert's does, not as subtrees. /// * `estimation_costs.stateless_delete_of_non_tree_for_costs` is 1: a delete's estimate prices /// each height of the climb with the layer GroveDB deletes in there, not the one above it. -/// * The six fee-returning wrappers (`add_document_for_contract`, +/// * The eight fee-returning wrappers (`add_document`, `add_document_for_contract`, /// `delete_document_for_contract`, `delete_document_for_contract_id`, -/// `update_document_for_contract`, `update_document_for_contract_id` and +/// `delete_index_only_document_for_contract`, `update_document_for_contract`, +/// `update_document_for_contract_id` and /// `update_document_with_serialization_for_contract`) are bumped to `1`: when the caller /// passes no transaction they write and price inside one owned transaction and commit it /// only once `Drive::calculate_fee` succeeded. Pricing an owner-attributed storage @@ -37,6 +38,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.update }, insert: DriveDocumentInsertMethodVersions { + add_document: 1, add_document_for_contract: 1, ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.insert }, @@ -47,6 +49,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = remove_reference_for_index_level_for_contract_operations: 2, // changed: the entries layer is described as references delete_document_for_contract: 1, delete_document_for_contract_id: 1, + delete_index_only_document_for_contract: 1, ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.delete }, estimation_costs: DriveDocumentEstimationCostsMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index cd1264c88a7..079b7d3f762 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -62,9 +62,9 @@ use crate::version::drive_versions::{ /// * **Price before commit** — `batch_operations.apply_drive_operations` /// 1 -> 2, `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` /// 0 -> 1), `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (`update_contract` 2 -> 3, -/// `apply_contract_with_serialization` 0 -> 1, the four moderation +/// `apply_contract_with_serialization` 0 -> 1, the five moderation /// writers that can free flagged bytes 0 -> 1) and -/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the six fee-returning document +/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the eight fee-returning document /// wrappers 0 -> 1). Every fee-returning entry point that owns its /// transaction when the caller passes none now prices the batch before /// committing, so the missing-history error above never leaves a write @@ -75,7 +75,7 @@ use crate::version::drive_versions::{ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; update_contract v3, apply_contract_with_serialization v1 and the moderation writers price before committing an owned transaction - document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the six fee-returning document wrappers price before committing an owned transaction + document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the eight fee-returning document wrappers price before committing an owned transaction vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 2f6bf1194af..71754ff38e7 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -368,7 +368,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// /// 17. **Pricing before commit**: the Drive entry points that own their /// transaction when a caller passes none (`apply_drive_operations` v2, -/// the six document wrappers v1, `update_contract` v3, +/// the eight document wrappers v1, `update_contract` v3, /// `apply_contract_with_serialization` v1, `add_group_action` v1, the /// moderation writers v1) price the batch before committing it, so the /// error in item 15 never leaves a write persisted without its fee From 8b4caed77ad7f49c3fec5cd82d75e75330c1f6d4 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 03:41:35 -0500 Subject: [PATCH 22/46] test(drive): assert rejected add_document, index-only delete and warning pricing leaves state untouched Each of the three wrappers gets a test that inserts an owner-flagged element, makes the bare wrapper free flagged bytes without a fee history at the latest version, and checks the missing-history error comes back with the root hash and the stored element unchanged; the same call with the history (or a fresh insert) commits, and protocol version 14 still commits without one. Co-Authored-By: Claude Fable 5.1 --- .../v0/tests/index_only_e2e_tests.rs | 148 ++++++++++++++++++ .../src/drive/contract/moderation/tests.rs | 111 +++++++++++++ .../rs-drive/src/drive/document/insert/mod.rs | 142 ++++++++++++++++- 3 files changed, 400 insertions(+), 1 deletion(-) diff --git a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/index_only_e2e_tests.rs b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/index_only_e2e_tests.rs index 635d32253c9..f9850566e8e 100644 --- a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/index_only_e2e_tests.rs +++ b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/index_only_e2e_tests.rs @@ -3867,3 +3867,151 @@ fn untagged_like_skips_the_hashtag_index_and_prices_lower() { assert_grovedb_is_consistent(&drive); } + +/// Deleting an owner-flagged like frees the owner's bytes, so pricing it without the fee +/// history is rejected from protocol version 15. The wrapper owns its transaction when the +/// caller passes none, so the rejected delete leaves every index entry and the root hash as +/// they were; with the history it refunds the owner and commits, and protocol version 14 +/// still deletes without one. +#[test] +fn should_leave_an_index_only_document_in_place_when_the_wrapper_cannot_price_its_removal() { + use crate::error::drive::DriveError; + use crate::error::Error; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::version::fee::FeeVersion; + use std::borrow::Cow; + use std::collections::BTreeMap; + + let insert_flagged_like = |drive: &Drive, contract: &DataContract, like: &Document, pv| { + let document_type = contract + .document_type_for_name(DOCTYPE) + .expect("like doctype exists"); + drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo(( + like, + Some(Cow::Owned(StorageFlags::SingleEpochOwned(0, OWNER_1))), + )), + owner_id: None, + }, + contract, + document_type, + }, + false, + BlockInfo::default(), + true, + None, + pv, + None, + ) + .expect("insert like with owned flags"); + }; + let root_hash = |drive: &Drive, pv: &PlatformVersion| { + drive + .grove + .root_hash(None, &pv.drive.grove_version) + .unwrap() + .expect("expected a root hash") + }; + + let pv = platform_version(); + let (drive, contract) = setup_likes(); + let document_type = contract + .document_type_for_name(DOCTYPE) + .expect("like doctype exists"); + let like = build_like(&contract, "dash", POST_A, OWNER_1, 1); + insert_flagged_like(&drive, &contract, &like, pv); + let before = root_hash(&drive, pv); + let mut by_post_level = doctype_path(&contract); + by_post_level.push(b"postId".to_vec()); + + let result = drive.delete_index_only_document_for_contract( + like.clone(), + &contract, + document_type, + BlockInfo::default(), + true, + None, + pv, + None, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "deleting owner-flagged entries without a fee history must be rejected, got {:?}", + result + ); + assert_eq!( + root_hash(&drive, pv), + before, + "a rejected delete must not persist" + ); + assert!( + read_grove_element(&drive, &by_post_level, &POST_A).is_some(), + "the like's group is still there" + ); + + let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); + let fee_result = drive + .delete_index_only_document_for_contract( + like, + &contract, + document_type, + BlockInfo::default(), + true, + None, + pv, + Some(&history), + ) + .expect("expected to delete the like with the fee history"); + assert!(fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(Identifier::from(OWNER_1)) + .is_some()); + assert!( + read_grove_element(&drive, &by_post_level, &POST_A).is_none(), + "the delete was committed" + ); + assert_grovedb_is_consistent(&drive); + + // Protocol version 14 prices the shipped shortcut without a history and commits. + let frozen = PlatformVersion::get(14).expect("protocol version 14"); + let drive = setup_drive_with_initial_state_structure(Some(frozen)); + let contract = json_document_to_contract( + "tests/supporting_files/contract/yappr-likes/yappr-likes-contract.json", + false, + frozen, + ) + .expect("expected to parse the yappr-likes contract"); + drive + .apply_contract( + &contract, + BlockInfo::default(), + true, + StorageFlags::optional_default_as_cow(), + None, + frozen, + ) + .expect("expected to apply the yappr-likes contract"); + let document_type = contract + .document_type_for_name(DOCTYPE) + .expect("like doctype exists"); + let like = build_like(&contract, "dash", POST_A, OWNER_1, 1); + insert_flagged_like(&drive, &contract, &like, frozen); + drive + .delete_index_only_document_for_contract( + like, + &contract, + document_type, + BlockInfo::default(), + true, + None, + frozen, + None, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); +} diff --git a/packages/rs-drive/src/drive/contract/moderation/tests.rs b/packages/rs-drive/src/drive/contract/moderation/tests.rs index 8bd91a95946..f21bb5d86ae 100644 --- a/packages/rs-drive/src/drive/contract/moderation/tests.rs +++ b/packages/rs-drive/src/drive/contract/moderation/tests.rs @@ -1609,3 +1609,114 @@ fn should_leave_a_ban_in_place_when_the_bare_wrapper_cannot_price_its_removal() ContractModerationStatus::default(), ); } + +/// Rewriting an identity's warning entry frees the previous entry's moderator-flagged bytes, +/// so pricing it without the fee history is rejected from protocol version 15. The wrapper +/// owns its transaction when the caller passes none, so the rejected rewrite leaves the +/// entry and the root hash as they were; protocol version 14 still rewrites without one. +#[test] +fn should_leave_a_warning_in_place_when_the_bare_wrapper_cannot_price_its_replacement() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = moderated_contract_keeping(false, false, true); + insert(&drive, &contract, platform_version); + let contract_id = contract.id(); + let moderator = contract.owner_id(); + let target = identity(0x71); + // A long first entry, so replacing it with the short one below shrinks the stored bytes + // and frees moderator-flagged storage; a same-size or growing rewrite frees nothing. + let first = warning(1_000, &"first strike ".repeat(8)); + let second = warning(2_000, "cleared"); + let lists = [ContractModerationList::Warnings]; + + drive + .add_contract_warning( + contract_id, + target, + std::slice::from_ref(&first), + false, + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to warn"); + let before = root_hash(&drive, platform_version); + + let result = drive.add_contract_warning( + contract_id, + target, + std::slice::from_ref(&second), + true, + moderator, + &BlockInfo::default(), + true, + None, + platform_version, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "rewriting a flagged entry without a fee history must be rejected, got {:?}", + result + ); + assert_eq!( + root_hash(&drive, platform_version), + before, + "a rejected replacement must not persist" + ); + assert_status( + &drive, + contract_id, + target, + &lists, + warned_with(vec![first.clone()]), + ); + + // The production funnel carries the history and commits the rewrite. + apply_moderation( + &drive, + ContractModerationOperationType::AddWarning { + contract_id, + identity_id: target, + warnings: vec![second.clone()], + replaces_existing: true, + moderator_id: moderator, + }, + &BlockInfo::default(), + true, + platform_version, + ) + .expect("expected to replace the warning with the fee history"); + assert_status( + &drive, + contract_id, + target, + &lists, + warned_with(vec![second.clone()]), + ); + + // Protocol version 14 prices the shipped shortcut without a history and commits. + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let drive = setup_drive_with_initial_state_structure(Some(frozen_platform_version)); + let contract = moderated_contract_keeping(false, false, true); + insert(&drive, &contract, frozen_platform_version); + for (warnings, replaces_existing) in [(vec![first], false), (vec![second], true)] { + drive + .add_contract_warning( + contract.id(), + target, + &warnings, + replaces_existing, + contract.owner_id(), + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + } +} diff --git a/packages/rs-drive/src/drive/document/insert/mod.rs b/packages/rs-drive/src/drive/document/insert/mod.rs index c70972cda78..7df8468c5b7 100644 --- a/packages/rs-drive/src/drive/document/insert/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/mod.rs @@ -51,6 +51,7 @@ mod tests { use rand::{random, Rng}; use crate::drive::document::tests::setup_dashpay; + use crate::drive::Drive; use crate::fees::op::LowLevelDriveOperation; use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; use crate::util::storage_flags::StorageFlags; @@ -68,10 +69,11 @@ mod tests { use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::DataContract; use dpp::document::serialization_traits::DocumentPlatformConversionMethodsV0; - use dpp::document::{Document, DocumentV0Getters}; + use dpp::document::{Document, DocumentV0Getters, DocumentV0Setters}; use dpp::fee::default_costs::KnownCostItem::StorageDiskUsageCreditPerByte; use dpp::fee::default_costs::{CachedEpochIndexFeeVersions, EpochCosts}; use dpp::fee::fee_result::FeeResult; + use dpp::platform_value::{Identifier, Value}; use dpp::tests::json_document::json_document_to_document; use dpp::version::fee::FeeVersion; use dpp::version::PlatformVersion; @@ -1545,4 +1547,142 @@ mod tests { "expected a document-type error, not DataContractNotFound: {err:?}" ); } + + /// Overriding an owner-flagged document rewrites its bytes, so pricing it without the + /// fee history is rejected from protocol version 15. `add_document` owns its + /// transaction when the caller passes none, so the rejected override leaves the stored + /// document and the root hash as they were; with the history it commits, and protocol + /// version 14 still commits without one. + #[test] + fn should_leave_a_document_in_place_when_add_document_cannot_price_its_override() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = setup_contract( + &drive, + "tests/supporting_files/contract/dashpay/dashpay-contract-all-mutable.json", + None, + None, + None::, + None, + Some(platform_version), + ); + let document_type = contract + .document_type_for_name("profile") + .expect("expected to get document type"); + let owner_id = random::<[u8; 32]>(); + let mut profile = json_document_to_document( + "tests/supporting_files/contract/dashpay/profile0.json", + Some(owner_id.into()), + document_type, + platform_version, + ) + .expect("expected to get document"); + // A long display name first, so the override below shrinks the stored bytes and + // frees owner-flagged storage. + profile.set("displayName", Value::Text("a".repeat(24))); + fn owned<'a>(document: &'a Document, owner_id: [u8; 32]) -> OwnedDocumentInfo<'a> { + OwnedDocumentInfo { + document_info: DocumentRefInfo(( + document, + Some(Cow::Owned(StorageFlags::SingleEpochOwned(0, owner_id))), + )), + owner_id: Some(owner_id), + } + } + drive + .add_document( + owned(&profile, owner_id), + contract.id(), + "profile", + false, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to insert the profile"); + let root_hash = |drive: &Drive| { + drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("expected a root hash") + }; + let before = root_hash(&drive); + + // The override shrinks the flagged document, freeing owner-attributed bytes; the + // bare wrapper passes no history. + let mut replaced = profile.clone(); + replaced.set("displayName", Value::Text("a".to_string())); + let result = drive.add_document( + owned(&replaced, owner_id), + contract.id(), + "profile", + true, + &BlockInfo::default(), + true, + None, + platform_version, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "overriding owner-flagged bytes without a fee history must be rejected, got {:?}", + result + ); + assert_eq!( + root_hash(&drive), + before, + "a rejected override must not persist" + ); + + // The insert of a new document frees nothing and commits at the latest version. The + // profile type has a unique owner index, so the second profile needs its own owner. + let second_owner_id = random::<[u8; 32]>(); + let mut second = profile.clone(); + second.set_id(Identifier::from([0x55; 32])); + second.set_owner_id(Identifier::from(second_owner_id)); + drive + .add_document( + owned(&second, second_owner_id), + contract.id(), + "profile", + false, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to insert a second profile"); + assert_ne!(root_hash(&drive), before, "the insert was committed"); + + // Protocol version 14 prices the shipped shortcut without a history and commits. + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let drive = setup_drive_with_initial_state_structure(Some(frozen_platform_version)); + let contract = setup_contract( + &drive, + "tests/supporting_files/contract/dashpay/dashpay-contract-all-mutable.json", + None, + None, + None::, + None, + Some(frozen_platform_version), + ); + for (document, override_document) in [(&profile, false), (&replaced, true)] { + drive + .add_document( + owned(document, owner_id), + contract.id(), + "profile", + override_document, + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + } + } } From e1850c70a52577723dcf7613fbd0f8aab516e1a6 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 03:41:35 -0500 Subject: [PATCH 23/46] refactor(drive): take the repaid debt of a storage refund from the balance helper's outcome The recorded-owner refund primitive reconstructed the repaid part from the helper's balance field, reading it differently for zero and positive starting balances. The helper already reports the repaid debt; use it and derive the balance share by checked subtraction. Co-Authored-By: Claude Fable 5.1 --- .../v0/mod.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index aba13581111..76052d3c634 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -83,19 +83,15 @@ impl Drive { // From a zero balance the helper clears negative credit first and only // the remainder becomes the new balance; from a positive balance the - // whole refund is added. Whatever did not reach the balance repaid - // debt, which lives outside the sum trees and is reported for the - // caller's processing pool write. - let reached_balance = if existing_balance == 0 { - outcome.balance_modified().unwrap_or(0) - } else { + // whole refund is added. The helper reports the part that repaid debt, + // which lives outside the sum trees and is passed on for the caller's + // processing pool write; the rest reached the balance. + let owner_repaid_debt = outcome.repaid_debt(); + let reached_balance = credits - }; - let owner_repaid_debt = - credits - .checked_sub(reached_balance) + .checked_sub(owner_repaid_debt) .ok_or(ProtocolError::Overflow( - "a storage refund cannot raise a balance by more than the refund", + "storage refund debt repayment cannot exceed the refund", ))?; repaid_debt = From 0c25a08ec137b06e2ace482e4947fe0556a10db1 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 05:29:20 -0500 Subject: [PATCH 24/46] fix(drive): price the contract fetch when adding a document by contract id add_document v1 (protocol version 15) inherited generation 0's second operations vector, declared after the contract lookup had recorded its read cost in the first one, so the fee never included the read. The new generation keeps one vector; generation 0 is unchanged. A test estimates the same insert through the contract-reference wrapper and the by-id wrapper: the processing fee is higher by the fetch at the latest version and equal at protocol version 14. Co-Authored-By: Claude Fable 5.1 --- .../document/insert/add_document/v1/mod.rs | 7 +- .../rs-drive/src/drive/document/insert/mod.rs | 84 ++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs b/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs index eb10811f52a..e5f2661f71c 100644 --- a/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/add_document/v1/mod.rs @@ -12,9 +12,11 @@ use dpp::version::PlatformVersion; use grovedb::TransactionArg; impl Drive { - /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// Generation 1 differs from generation 0 in two things. When the caller supplies no /// transaction and the operations are applied, the write and its pricing share one /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// And the contract fetch is priced with the write: generation 0 recorded the fetch in + /// one operations vector and priced another, so its fee never included the read. /// Generation 0 applied the batch (committing it on its own without a caller /// transaction) and priced it afterwards, so from protocol version 15, where pricing an /// owner-attributed storage removal without the fee history is an error, a call passing @@ -65,7 +67,8 @@ impl Drive { contract, document_type, }; - let mut drive_operations: Vec = vec![]; + // The contract fetch above recorded its cost in `drive_operations`; generation 0 + // replaced the vector here and never priced that read. self.add_document_for_contract_apply_and_add_to_operations( document_and_contract_info, override_document, diff --git a/packages/rs-drive/src/drive/document/insert/mod.rs b/packages/rs-drive/src/drive/document/insert/mod.rs index 7df8468c5b7..54bbcc1e42c 100644 --- a/packages/rs-drive/src/drive/document/insert/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/mod.rs @@ -76,7 +76,7 @@ mod tests { use dpp::platform_value::{Identifier, Value}; use dpp::tests::json_document::json_document_to_document; use dpp::version::fee::FeeVersion; - use dpp::version::PlatformVersion; + use dpp::version::{PlatformVersion, LATEST_VERSION}; static EPOCH_CHANGE_FEE_VERSION_TEST: Lazy = Lazy::new(|| BTreeMap::from([(0, FeeVersion::first())])); @@ -1685,4 +1685,86 @@ mod tests { .expect("protocol version 14 prices the shipped shortcut without a history"); } } + + /// `add_document` looks the contract up by id and records that read's cost before + /// building the document operations. Generation 0 replaced its operations vector after + /// the lookup and priced the write alone; generation 1 prices the lookup with it, so its + /// fee exceeds the contract-reference wrapper's by the fetch cost at the latest version + /// and equals it at protocol version 14. + #[test] + fn should_price_the_contract_fetch_when_adding_a_document_by_contract_id() { + for (protocol_version, prices_the_fetch) in [(14, false), (LATEST_VERSION, true)] { + let platform_version = + PlatformVersion::get(protocol_version).expect("expected a platform version"); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = setup_contract( + &drive, + "tests/supporting_files/contract/dashpay/dashpay-contract-all-mutable.json", + None, + None, + None::, + None, + Some(platform_version), + ); + let document_type = contract + .document_type_for_name("profile") + .expect("expected to get document type"); + let owner_id = random::<[u8; 32]>(); + let profile = json_document_to_document( + "tests/supporting_files/contract/dashpay/profile0.json", + Some(owner_id.into()), + document_type, + platform_version, + ) + .expect("expected to get document"); + let owned = || OwnedDocumentInfo { + document_info: DocumentRefInfo((&profile, StorageFlags::optional_default_as_cow())), + owner_id: Some(owner_id), + }; + + // Both estimates price the same write; only the by-id wrapper reads the contract. + let by_reference = drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: owned(), + contract: &contract, + document_type, + }, + false, + BlockInfo::default(), + false, + None, + platform_version, + None, + ) + .expect("expected to estimate the insert by contract reference"); + let by_id = drive + .add_document( + owned(), + contract.id(), + "profile", + false, + &BlockInfo::default(), + false, + None, + platform_version, + ) + .expect("expected to estimate the insert by contract id"); + + assert_eq!(by_id.storage_fee, by_reference.storage_fee); + if prices_the_fetch { + assert!( + by_id.processing_fee > by_reference.processing_fee, + "protocol version {protocol_version} must price the contract read: {} versus {}", + by_id.processing_fee, + by_reference.processing_fee + ); + } else { + assert_eq!( + by_id.processing_fee, by_reference.processing_fee, + "protocol version {protocol_version} keeps pricing the write alone" + ); + } + } + } } From f8a73fdff21e50c56394190d62c2f1637f28d438 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 09:53:58 -0500 Subject: [PATCH 25/46] fix(drive)!: price contested document inserts before committing The two contested-document wrappers applied their batch, committing the transaction they own when the caller passes none, and priced it afterwards. The second contender of a contest resolved without locking moves the poll's end date, which removes the creator-flagged join-window entry, so from protocol version 15 a call passing no fee history got the missing-history error after the write was durable. Each gets a generation 1 that starts the owned transaction itself, applies the batch through it, prices it and commits only once pricing succeeded. The by-id wrapper also keeps one operations vector so the contract read is priced with the write, as add_document v1 does. The operation builders are unchanged. Protocol version 15's document method table selects the new generations; generation 0 is untouched for protocol version 14 and earlier. Co-Authored-By: Claude Fable 5.1 --- .../add_contested_document/mod.rs | 13 ++- .../add_contested_document/v1/mod.rs | 106 ++++++++++++++++++ .../mod.rs | 13 ++- .../v1/mod.rs | 68 +++++++++++ .../drive_document_method_versions/v5.rs | 19 +++- .../src/version/drive_versions/v10.rs | 4 +- .../rs-platform-version/src/version/v15.rs | 2 +- 7 files changed, 214 insertions(+), 11 deletions(-) create mode 100644 packages/rs-drive/src/drive/document/insert_contested/add_contested_document/v1/mod.rs create mode 100644 packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/v1/mod.rs diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/mod.rs index e7b9454ae5f..b8368339ffd 100644 --- a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/mod.rs +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::util::object_size_info::OwnedDocumentInfo; @@ -59,9 +60,19 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contested_document_v1( + owned_document_info, + contested_document_resource_vote_poll, + insert_without_check, + also_insert_vote_poll_stored_info, + block_info, + apply, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contested_document".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/v1/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/v1/mod.rs new file mode 100644 index 00000000000..de667ff569a --- /dev/null +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document/v1/mod.rs @@ -0,0 +1,106 @@ +use crate::drive::Drive; +use crate::error::document::DocumentError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; +use dpp::block::block_info::BlockInfo; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::fee::fee_result::FeeResult; + +use crate::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfo; +use dpp::version::PlatformVersion; +use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::ContestedDocumentVotePollStoredInfo; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in two things. When the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed: the second contender of a contest + /// resolved without locking removes the creator-flagged join-window end-date entry. It + /// now fails before anything is written. And the contract fetch is priced with the + /// write: generation 0 recorded the fetch in one operations vector and priced another. + /// With a caller transaction nothing is committed by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contested_document_v1( + &self, + owned_document_info: OwnedDocumentInfo, + contested_document_resource_vote_poll: ContestedDocumentResourceVotePollWithContractInfo, + insert_without_check: bool, + also_insert_vote_poll_stored_info: Option, + block_info: &BlockInfo, + apply: bool, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + // The contract cache keys its block-versus-committed behaviour on whether a + // transaction is present. An owned transaction is not block execution, so the + // contract is looked up through the caller's (`caller_transaction`) and only the + // document write goes through the owned one. + let caller_transaction = transaction; + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + + let mut drive_operations: Vec = vec![]; + + let contract_fetch_info = self + .get_contract_with_fetch_info_and_add_to_operations( + contested_document_resource_vote_poll + .contract + .id() + .into_buffer(), + Some(&block_info.epoch), + true, + caller_transaction, + &mut drive_operations, + platform_version, + )? + .ok_or(Error::Document(DocumentError::DataContractNotFound))?; + + let contract = &contract_fetch_info.contract; + + let document_type = contract.document_type_for_name( + contested_document_resource_vote_poll + .document_type_name + .as_str(), + )?; + + let document_and_contract_info = DocumentAndContractInfo { + owned_document_info, + contract, + document_type, + }; + // The contract fetch above recorded its cost in `drive_operations`; generation 0 + // replaced the vector here and never priced that read. + self.add_contested_document_for_contract_apply_and_add_to_operations( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + block_info, + true, + apply, + also_insert_vote_poll_stored_info, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } +} diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/mod.rs index ae6ae1395c3..0a962db2b52 100644 --- a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/mod.rs +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::util::object_size_info::DocumentAndContractInfo; @@ -57,9 +58,19 @@ impl Drive { transaction, platform_version, ), + 1 => self.add_contested_document_for_contract_v1( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + block_info, + apply, + also_insert_vote_poll_stored_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contested_document_for_contract".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/v1/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/v1/mod.rs new file mode 100644 index 00000000000..cb3f0f9c21c --- /dev/null +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract/v1/mod.rs @@ -0,0 +1,68 @@ +use crate::drive::Drive; +use crate::util::object_size_info::DocumentAndContractInfo; + +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use dpp::block::block_info::BlockInfo; +use dpp::fee::fee_result::FeeResult; + +use crate::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfo; +use dpp::version::PlatformVersion; +use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::ContestedDocumentVotePollStoredInfo; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 1 differs from generation 0 in one thing: when the caller supplies no + /// transaction and the operations are applied, the write and its pricing share one + /// owned transaction that is committed only after `Drive::calculate_fee` succeeded. + /// Generation 0 applied the batch (committing it on its own without a caller + /// transaction) and priced it afterwards, so from protocol version 15, where pricing an + /// owner-attributed storage removal without the fee history is an error, a call passing + /// no history persisted the write and then failed: the second contender of a contest + /// resolved without locking removes the creator-flagged join-window end-date entry. It + /// now fails before anything is written. With a caller transaction nothing is committed + /// by Drive in either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contested_document_for_contract_v1( + &self, + document_and_contract_info: DocumentAndContractInfo, + contested_document_resource_vote_poll: ContestedDocumentResourceVotePollWithContractInfo, + insert_without_check: bool, + block_info: BlockInfo, + apply: bool, + also_insert_vote_poll_stored_info: Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut drive_operations: Vec = vec![]; + self.add_contested_document_for_contract_apply_and_add_to_operations( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + &block_info, + true, + apply, + also_insert_vote_poll_stored_info, + transaction, + &mut drive_operations, + platform_version, + )?; + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(drive_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + Ok(fees) + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs index 958d78f0d42..44e88c139ef 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs @@ -1,8 +1,8 @@ use crate::version::drive_versions::drive_document_method_versions::v4::DRIVE_DOCUMENT_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_document_method_versions::{ DriveDocumentDeleteMethodVersions, DriveDocumentEstimationCostsMethodVersions, - DriveDocumentInsertMethodVersions, DriveDocumentMethodVersions, - DriveDocumentUpdateMethodVersions, + DriveDocumentInsertContestedMethodVersions, DriveDocumentInsertMethodVersions, + DriveDocumentMethodVersions, DriveDocumentUpdateMethodVersions, }; /// V5 is protocol version 15's document-method table. Relative to V4: @@ -18,7 +18,8 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// the insert's does, not as subtrees. /// * `estimation_costs.stateless_delete_of_non_tree_for_costs` is 1: a delete's estimate prices /// each height of the climb with the layer GroveDB deletes in there, not the one above it. -/// * The eight fee-returning wrappers (`add_document`, `add_document_for_contract`, +/// * The ten fee-returning wrappers (`add_document`, `add_document_for_contract`, +/// `add_contested_document`, `add_contested_document_for_contract`, /// `delete_document_for_contract`, `delete_document_for_contract_id`, /// `delete_index_only_document_for_contract`, `update_document_for_contract`, /// `update_document_for_contract_id` and @@ -26,9 +27,10 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// passes no transaction they write and price inside one owned transaction and commit it /// only once `Drive::calculate_fee` succeeded. Pricing an owner-attributed storage /// removal without the fee history is an error from this version, and generation 0 -/// committed the write before that error surfaced. The operation builders and the -/// `_apply_and_add_to_operations` methods every production caller uses through -/// `apply_drive_operations` are unchanged. +/// committed the write before that error surfaced (a contested insert removes flagged +/// bytes when the second contender of a contest resolved without locking moves its end +/// date). The operation builders and the `_apply_and_add_to_operations` methods every +/// production caller uses through `apply_drive_operations` are unchanged. pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = DriveDocumentMethodVersions { update: DriveDocumentUpdateMethodVersions { @@ -37,6 +39,11 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = update_document_with_serialization_for_contract: 1, ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.update }, + insert_contested: DriveDocumentInsertContestedMethodVersions { + add_contested_document: 1, + add_contested_document_for_contract: 1, + ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.insert_contested + }, insert: DriveDocumentInsertMethodVersions { add_document: 1, add_document_for_contract: 1, diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 079b7d3f762..58ddb174667 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -64,7 +64,7 @@ use crate::version::drive_versions::{ /// 0 -> 1), `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (`update_contract` 2 -> 3, /// `apply_contract_with_serialization` 0 -> 1, the five moderation /// writers that can free flagged bytes 0 -> 1) and -/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the eight fee-returning document +/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the ten fee-returning document /// wrappers 0 -> 1). Every fee-returning entry point that owns its /// transaction when the caller passes none now prices the batch before /// committing, so the missing-history error above never leaves a write @@ -75,7 +75,7 @@ use crate::version::drive_versions::{ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; update_contract v3, apply_contract_with_serialization v1 and the moderation writers price before committing an owned transaction - document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the eight fee-returning document wrappers price before committing an owned transaction + document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the ten fee-returning document wrappers price before committing an owned transaction vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 71754ff38e7..5c22e8291b2 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -368,7 +368,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// /// 17. **Pricing before commit**: the Drive entry points that own their /// transaction when a caller passes none (`apply_drive_operations` v2, -/// the eight document wrappers v1, `update_contract` v3, +/// the ten document wrappers v1, `update_contract` v3, /// `apply_contract_with_serialization` v1, `add_group_action` v1, the /// moderation writers v1) price the batch before committing it, so the /// error in item 15 never leaves a write persisted without its fee From fe715fa1b7876f6fb7bd564343e98c34633993c1 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 09:53:58 -0500 Subject: [PATCH 26/46] test(drive): assert a rejected second contender leaves a no-locking contest untouched Opens a contest resolved without locking with one contender, then joins a second through each bare wrapper without a fee history at the latest version: both are rejected with the root hash, the contenders and the end-date entries unchanged; the production funnel joins with the history and moves the end date to the vote window; protocol version 14 joins without one. A second test opens a contest through both wrappers at every version and checks the by-id fee includes the contract read from protocol version 15. The no-locking DPNS fixture is copied from the drive-abci tests. Co-Authored-By: Claude Fable 5.1 --- .../drive/document/insert_contested/mod.rs | 446 ++++++++++++++++++ ...act-contested-unique-index-no-locking.json | 169 +++++++ 2 files changed, 615 insertions(+) create mode 100644 packages/rs-drive/tests/supporting_files/contract/dpns/dpns-contract-contested-unique-index-no-locking.json diff --git a/packages/rs-drive/src/drive/document/insert_contested/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/mod.rs index 425c37887b4..31e950520a5 100644 --- a/packages/rs-drive/src/drive/document/insert_contested/mod.rs +++ b/packages/rs-drive/src/drive/document/insert_contested/mod.rs @@ -636,4 +636,450 @@ mod tests { } } } + + /// The second contender of a contest resolved without locking moves the poll's end + /// date: the creator-flagged join-window entry is removed and the vote-window entry + /// written, so pricing it without the fee history is rejected from protocol version + /// 15. Both bare wrappers own their transaction when the caller passes none, so the + /// rejected join leaves the root hash, the contenders and the end-date entries as they + /// were; the production funnel joins with the history, and protocol version 14 joins + /// without one. + mod second_contender_without_fee_history { + use super::*; + use crate::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfo; + use crate::drive::Drive; + use crate::error::drive::DriveError; + use crate::error::Error; + use crate::query::vote_poll_vote_state_query::{ + ContestedDocumentVotePollDriveQueryResultType, + ResolvedContestedDocumentVotePollDriveQuery, + }; + use crate::query::VotePollsByEndDateDriveQuery; + use crate::util::batch::drive_op_batch::DocumentOperationType; + use crate::util::batch::DriveOperation; + use crate::util::object_size_info::{ + DataContractInfo, DataContractOwnedResolvedInfo, DocumentTypeInfo, + }; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use crate::util::test_helpers::setup_contract; + use dpp::data_contract::document_type::random_document::{ + CreateRandomDocument, DocumentFieldFillSize, DocumentFieldFillType, + }; + use dpp::data_contract::DataContract; + use dpp::document::{Document, DocumentV0Setters}; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::fee::fee_result::FeeResult; + use dpp::identifier::Identifier; + use dpp::platform_value::{Bytes32, Value}; + use dpp::prelude::TimestampMillis; + use dpp::version::fee::FeeVersion; + use dpp::version::LATEST_VERSION; + use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::ContestedDocumentVotePollStoredInfo; + use rand::rngs::StdRng; + use rand::SeedableRng; + use std::borrow::Cow; + use std::collections::BTreeMap; + + const NO_LOCKING_CONTRACT: &str = + "tests/supporting_files/contract/dpns/dpns-contract-contested-unique-index-no-locking.json"; + + fn quantum_domain_document( + contract: &DataContract, + owner_id: Identifier, + rng: &mut StdRng, + platform_version: &PlatformVersion, + ) -> Document { + let document_type = contract + .document_type_for_name("domain") + .expect("domain should exist on DPNS"); + let mut document = document_type + .random_document_with_params( + owner_id, + Bytes32::random_with_rng(rng), + Some(1), + Some(1), + Some(1), + DocumentFieldFillType::FillIfNotRequired, + DocumentFieldFillSize::MinDocumentFillSize, + rng, + platform_version, + ) + .expect("random document"); + document.set("parentDomainName", "dash".into()); + document.set("normalizedParentDomainName", "dash".into()); + document.set("label", "quantum".into()); + document.set("normalizedLabel", "quantum".into()); + document.set("records.identity", owner_id.into()); + document.set("subdomainRules.allowSubdomains", false.into()); + document + } + + fn vote_poll(contract: &DataContract) -> ContestedDocumentResourceVotePollWithContractInfo { + ContestedDocumentResourceVotePollWithContractInfo { + contract: DataContractOwnedResolvedInfo::OwnedDataContract(contract.clone()), + document_type_name: "domain".to_string(), + index_name: "parentNameAndLabel".to_string(), + index_values: vec![ + Value::Text("dash".to_string()), + Value::Text("quantum".to_string()), + ], + } + } + + fn owned(document: &Document, owner_id: Identifier) -> OwnedDocumentInfo<'_> { + OwnedDocumentInfo { + document_info: DocumentRefInfo(( + document, + Some(Cow::Owned(StorageFlags::SingleEpochOwned( + 0, + owner_id.to_buffer(), + ))), + )), + owner_id: Some(owner_id.to_buffer()), + } + } + + struct Contest { + drive: Drive, + contract: DataContract, + first_owner_id: Identifier, + second_owner_id: Identifier, + second_document: Document, + } + + /// Opens the `dash.quantum` contest with one contender through the production + /// funnel, and prepares the second contender's document. + fn open_contest(platform_version: &PlatformVersion) -> Contest { + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = setup_contract( + &drive, + NO_LOCKING_CONTRACT, + None, + None, + None::, + None, + Some(platform_version), + ); + let mut rng = StdRng::seed_from_u64(0x5EC0_004D); + let first_owner_id = Identifier::from([0x31; 32]); + let second_owner_id = Identifier::from([0x32; 32]); + let first_document = + quantum_domain_document(&contract, first_owner_id, &mut rng, platform_version); + let second_document = + quantum_domain_document(&contract, second_owner_id, &mut rng, platform_version); + drive + .add_contested_document_for_contract( + DocumentAndContractInfo { + owned_document_info: owned(&first_document, first_owner_id), + contract: &contract, + document_type: contract + .document_type_for_name("domain") + .expect("domain should exist on DPNS"), + }, + vote_poll(&contract), + false, + BlockInfo::default(), + true, + Some( + ContestedDocumentVotePollStoredInfo::new( + BlockInfo::default(), + platform_version, + ) + .expect("expected the stored info of a new poll"), + ), + None, + platform_version, + ) + .expect("expected to open the contest"); + Contest { + drive, + contract, + first_owner_id, + second_owner_id, + second_document, + } + } + + fn root_hash(drive: &Drive, platform_version: &PlatformVersion) -> [u8; 32] { + drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .unwrap() + .expect("expected a root hash") + } + + fn end_dates(drive: &Drive, platform_version: &PlatformVersion) -> Vec { + VotePollsByEndDateDriveQuery { + start_time: None, + end_time: None, + limit: None, + offset: None, + order_ascending: true, + } + .execute_no_proof(drive, None, &mut vec![], platform_version) + .expect("expected the end date entries") + .into_iter() + .flat_map(|(time, polls)| polls.into_iter().map(move |_| time)) + .collect() + } + + fn contenders( + drive: &Drive, + contract: &DataContract, + platform_version: &PlatformVersion, + ) -> Vec { + ResolvedContestedDocumentVotePollDriveQuery { + vote_poll: (&vote_poll(contract)).into(), + result_type: ContestedDocumentVotePollDriveQueryResultType::VoteTally, + offset: None, + limit: None, + start_at: None, + allow_include_locked_and_abstaining_vote_tally: false, + } + .execute(drive, None, &mut vec![], platform_version) + .expect("expected the contenders") + .contenders + .into_iter() + .map(|contender| contender.identity_id()) + .collect() + } + + fn fee_history() -> CachedEpochIndexFeeVersions { + BTreeMap::from([(0, FeeVersion::first())]) + } + + fn assert_rejected(result: Result) { + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "moving a creator-flagged end date without a fee history must be rejected, got {:?}", + result + ); + } + + #[test] + fn should_leave_a_no_locking_contest_in_place_when_a_bare_wrapper_cannot_price_the_second_contender( + ) { + let platform_version = PlatformVersion::latest(); + let Contest { + drive, + contract, + first_owner_id, + second_owner_id, + second_document, + } = open_contest(platform_version); + let join_window_end = end_dates(&drive, platform_version); + assert_eq!(join_window_end.len(), 1, "the contest has one end date"); + let before = root_hash(&drive, platform_version); + let document_type = contract + .document_type_for_name("domain") + .expect("domain should exist on DPNS"); + + // The wrapper taking the contract reference. + assert_rejected(drive.add_contested_document_for_contract( + DocumentAndContractInfo { + owned_document_info: owned(&second_document, second_owner_id), + contract: &contract, + document_type, + }, + vote_poll(&contract), + false, + BlockInfo::default(), + true, + None, + None, + platform_version, + )); + assert_eq!(root_hash(&drive, platform_version), before); + + // The wrapper taking the contract id. + assert_rejected(drive.add_contested_document( + owned(&second_document, second_owner_id), + vote_poll(&contract), + false, + None, + &BlockInfo::default(), + true, + None, + platform_version, + )); + assert_eq!( + root_hash(&drive, platform_version), + before, + "a rejected second contender must not persist" + ); + assert_eq!( + contenders(&drive, &contract, platform_version), + vec![first_owner_id], + "the contest still has its first contender only" + ); + assert_eq!( + end_dates(&drive, platform_version), + join_window_end, + "the join-window end date is still the only entry" + ); + + // The production funnel carries the history: the second contender joins and the + // end date moves to the vote window. + let history = fee_history(); + drive + .apply_drive_operations( + vec![DriveOperation::DocumentOperation( + DocumentOperationType::AddContestedDocument { + owned_document_info: owned(&second_document, second_owner_id), + contested_document_resource_vote_poll: vote_poll(&contract), + contract_info: DataContractInfo::BorrowedDataContract(&contract), + document_type_info: DocumentTypeInfo::DocumentTypeNameAsStr("domain"), + insert_without_check: false, + also_insert_vote_poll_stored_info: None, + }, + )], + true, + &BlockInfo::default(), + None, + platform_version, + Some(&history), + ) + .expect("expected the second contender to join with the fee history"); + assert_eq!( + contenders(&drive, &contract, platform_version), + vec![first_owner_id, second_owner_id] + ); + let vote_window_end = end_dates(&drive, platform_version); + assert_eq!( + vote_window_end.len(), + 1, + "the contest still has one end date" + ); + assert!( + vote_window_end[0] > join_window_end[0], + "the end date moved from the join window to the vote window" + ); + + // Protocol version 14 prices the shipped shortcut without a history and commits. + let frozen_platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let Contest { + drive, + contract, + first_owner_id, + second_owner_id, + second_document, + } = open_contest(frozen_platform_version); + drive + .add_contested_document( + owned(&second_document, second_owner_id), + vote_poll(&contract), + false, + None, + &BlockInfo::default(), + true, + None, + frozen_platform_version, + ) + .expect("protocol version 14 prices the shipped shortcut without a history"); + assert_eq!( + contenders(&drive, &contract, frozen_platform_version), + vec![first_owner_id, second_owner_id] + ); + } + + /// The first contender frees nothing, so the bare wrappers price it at every + /// version; the by-id wrapper's fee includes the contract read from protocol + /// version 15 (generation 0 priced the write alone). + #[test] + fn should_open_a_contest_through_the_bare_wrappers_at_every_version() { + for (protocol_version, prices_the_fetch) in [(14, false), (LATEST_VERSION, true)] { + let platform_version = + PlatformVersion::get(protocol_version).expect("expected a platform version"); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = setup_contract( + &drive, + NO_LOCKING_CONTRACT, + None, + None, + None::, + None, + Some(platform_version), + ); + let mut rng = StdRng::seed_from_u64(0x5EC0_004E); + let owner_id = Identifier::from([0x33; 32]); + let document = + quantum_domain_document(&contract, owner_id, &mut rng, platform_version); + let stored_info = || { + Some( + ContestedDocumentVotePollStoredInfo::new( + BlockInfo::default(), + platform_version, + ) + .expect("expected the stored info of a new poll"), + ) + }; + + let by_reference = drive + .add_contested_document_for_contract( + DocumentAndContractInfo { + owned_document_info: owned(&document, owner_id), + contract: &contract, + document_type: contract + .document_type_for_name("domain") + .expect("domain should exist on DPNS"), + }, + vote_poll(&contract), + false, + BlockInfo::default(), + false, + stored_info(), + None, + platform_version, + ) + .expect("expected to estimate the contest by contract reference"); + let by_id = drive + .add_contested_document( + owned(&document, owner_id), + vote_poll(&contract), + false, + stored_info(), + &BlockInfo::default(), + false, + None, + platform_version, + ) + .expect("expected to estimate the contest by contract id"); + assert_eq!(by_id.storage_fee, by_reference.storage_fee); + if prices_the_fetch { + assert!( + by_id.processing_fee > by_reference.processing_fee, + "protocol version {protocol_version} must price the contract read" + ); + } else { + assert_eq!(by_id.processing_fee, by_reference.processing_fee); + } + + let before = root_hash(&drive, platform_version); + drive + .add_contested_document( + owned(&document, owner_id), + vote_poll(&contract), + false, + stored_info(), + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to open the contest by contract id"); + assert_ne!( + root_hash(&drive, platform_version), + before, + "the contest was committed" + ); + assert_eq!( + contenders(&drive, &contract, platform_version), + vec![owner_id] + ); + } + } + } } diff --git a/packages/rs-drive/tests/supporting_files/contract/dpns/dpns-contract-contested-unique-index-no-locking.json b/packages/rs-drive/tests/supporting_files/contract/dpns/dpns-contract-contested-unique-index-no-locking.json new file mode 100644 index 00000000000..db473f41d27 --- /dev/null +++ b/packages/rs-drive/tests/supporting_files/contract/dpns/dpns-contract-contested-unique-index-no-locking.json @@ -0,0 +1,169 @@ +{ + "$formatVersion": "0", + "id": "DWBXe9EXFPHxvbArQgT45uQR5gMmi8dfMpLhR5KSbwnZ", + "ownerId": "2QjL594djCH2NyDsn45vd6yQjEDHupMKo7CEGVTHtQxU", + "version": 1, + "documentSchemas": { + "domain": { + "documentsMutable": false, + "canBeDeleted": true, + "transferable": 1, + "tradeMode": 1, + "type": "object", + "indices": [ + { + "name": "parentNameAndLabel", + "properties": [ + { + "normalizedParentDomainName": "asc" + }, + { + "normalizedLabel": "asc" + } + ], + "unique": true, + "contested": { + "fieldMatches": [ + { + "field": "normalizedLabel", + "regexPattern": "^[a-zA-Z01]{3,19}$" + } + ], + "resolution": 1, + "description": "If the normalized label part of this index is less than 20 characters (all alphabet a-z and 0 and 1) then this index is non unique while contest resolution takes place." + } + }, + { + "name": "identityId", + "nullSearchable": false, + "properties": [ + { + "records.identity": "asc" + } + ] + } + ], + "properties": { + "label": { + "type": "string", + "pattern": "^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$", + "minLength": 3, + "maxLength": 63, + "position": 0, + "description": "Domain label. e.g. 'Bob'." + }, + "normalizedLabel": { + "type": "string", + "pattern": "^[a-hj-km-np-z0-9][a-hj-km-np-z0-9-]{0,61}[a-hj-km-np-z0-9]$", + "maxLength": 63, + "position": 1, + "description": "Domain label converted to lowercase for case-insensitive uniqueness validation. \"o\", \"i\" and \"l\" replaced with \"0\" and \"1\" to mitigate homograph attack. e.g. 'b0b'", + "$comment": "Must be equal to the label in lowercase. \"o\", \"i\" and \"l\" must be replaced with \"0\" and \"1\"." + }, + "parentDomainName": { + "type": "string", + "pattern": "^$|^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$", + "minLength": 0, + "maxLength": 63, + "position": 2, + "description": "A full parent domain name. e.g. 'dash'." + }, + "normalizedParentDomainName": { + "type": "string", + "pattern": "^$|^[a-hj-km-np-z0-9][a-hj-km-np-z0-9-\\.]{0,61}[a-hj-km-np-z0-9]$", + "minLength": 0, + "maxLength": 63, + "position": 3, + "description": "A parent domain name in lowercase for case-insensitive uniqueness validation. \"o\", \"i\" and \"l\" replaced with \"0\" and \"1\" to mitigate homograph attack. e.g. 'dash'", + "$comment": "Must either be equal to an existing domain or empty to create a top level domain. \"o\", \"i\" and \"l\" must be replaced with \"0\" and \"1\". Only the data contract owner can create top level domains." + }, + "preorderSalt": { + "type": "array", + "byteArray": true, + "minItems": 32, + "maxItems": 32, + "position": 4, + "description": "Salt used in the preorder document" + }, + "records": { + "type": "object", + "properties": { + "identity": { + "type": "array", + "byteArray": true, + "minItems": 32, + "maxItems": 32, + "position": 1, + "contentMediaType": "application/x.dash.dpp.identifier", + "description": "Identifier name record that refers to an Identity" + } + }, + "minProperties": 1, + "position": 5, + "additionalProperties": false + }, + "subdomainRules": { + "type": "object", + "properties": { + "allowSubdomains": { + "type": "boolean", + "description": "This option defines who can create subdomains: true - anyone; false - only the domain owner", + "$comment": "Only the domain owner is allowed to create subdomains for non top-level domains", + "position": 0 + } + }, + "position": 6, + "description": "Subdomain rules allow domain owners to define rules for subdomains", + "additionalProperties": false, + "required": [ + "allowSubdomains" + ] + } + }, + "required": [ + "$createdAt", + "$updatedAt", + "$transferredAt", + "label", + "normalizedLabel", + "normalizedParentDomainName", + "preorderSalt", + "records", + "subdomainRules" + ], + "additionalProperties": false, + "$comment": "In order to register a domain you need to create a preorder. The preorder step is needed to prevent man-in-the-middle attacks. normalizedLabel + '.' + normalizedParentDomain must not be longer than 253 chars length as defined by RFC 1035. Domain documents are immutable: modification and deletion are restricted" + }, + "preorder": { + "documentsMutable": false, + "canBeDeleted": true, + "type": "object", + "indices": [ + { + "name": "saltedHash", + "properties": [ + { + "saltedDomainHash": "asc" + } + ], + "unique": true + } + ], + "properties": { + "saltedDomainHash": { + "type": "array", + "byteArray": true, + "minItems": 32, + "maxItems": 32, + "position": 0, + "description": "Double sha-256 of the concatenation of a 32 byte random salt and a normalized domain name" + } + }, + "required": [ + "saltedDomainHash" + ], + "additionalProperties": false, + "$comment": "Preorder documents are immutable: modification and deletion are restricted" + } + } +} \ No newline at end of file From 8e5f847ad1d6f0a2439ac7ea0938ee5e836215c0 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 27 Sep 2026 09:53:58 -0500 Subject: [PATCH 27/46] test(drive): give the refund regression fixtures fixed owner ids The owner ids of the new price-before-commit regressions become persisted keys and storage flags, so a key-dependent failure would exercise different state on every retry. Use fixed, distinct identifiers instead of unseeded random ones. Co-Authored-By: Claude Fable 5.1 --- packages/rs-drive/src/drive/document/delete/mod.rs | 2 +- packages/rs-drive/src/drive/document/insert/mod.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/rs-drive/src/drive/document/delete/mod.rs b/packages/rs-drive/src/drive/document/delete/mod.rs index 69e3c615fc9..e739386d742 100644 --- a/packages/rs-drive/src/drive/document/delete/mod.rs +++ b/packages/rs-drive/src/drive/document/delete/mod.rs @@ -878,7 +878,7 @@ mod tests { let document_type = contract .document_type_for_name("person") .expect("expected to get document type"); - let owner_id = rand::thread_rng().gen::<[u8; 32]>(); + let owner_id = [0x44; 32]; let person_document = json_document_to_document( "tests/supporting_files/contract/family/person0.json", Some(owner_id.into()), diff --git a/packages/rs-drive/src/drive/document/insert/mod.rs b/packages/rs-drive/src/drive/document/insert/mod.rs index 54bbcc1e42c..e2f909b813b 100644 --- a/packages/rs-drive/src/drive/document/insert/mod.rs +++ b/packages/rs-drive/src/drive/document/insert/mod.rs @@ -1569,7 +1569,7 @@ mod tests { let document_type = contract .document_type_for_name("profile") .expect("expected to get document type"); - let owner_id = random::<[u8; 32]>(); + let owner_id = [0x41; 32]; let mut profile = json_document_to_document( "tests/supporting_files/contract/dashpay/profile0.json", Some(owner_id.into()), @@ -1640,7 +1640,7 @@ mod tests { // The insert of a new document frees nothing and commits at the latest version. The // profile type has a unique owner index, so the second profile needs its own owner. - let second_owner_id = random::<[u8; 32]>(); + let second_owner_id = [0x42; 32]; let mut second = profile.clone(); second.set_id(Identifier::from([0x55; 32])); second.set_owner_id(Identifier::from(second_owner_id)); @@ -1709,7 +1709,7 @@ mod tests { let document_type = contract .document_type_for_name("profile") .expect("expected to get document type"); - let owner_id = random::<[u8; 32]>(); + let owner_id = [0x43; 32]; let profile = json_document_to_document( "tests/supporting_files/contract/dashpay/profile0.json", Some(owner_id.into()), From 7a6ecd08ffc2eda634cefbb2d4b9e7c21260aaf0 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 19 Oct 2026 09:38:36 -0500 Subject: [PATCH 28/46] test(drive): pass the fee history in fixtures that remove flagged bytes at the latest version Tests added on the base since the last rebase delete, update or close group actions at PlatformVersion::latest() without a fee history. From protocol version 15 a storage refund for owner-attributed bytes is priced only with the removing block's history, so those fixtures now hit the missing-history error. The rekey migration tests pass the history to their delete and update calls. The group status and group burn proof fixtures close actions through apply_drive_operations with the history, the funnel production uses, because the bare add_group_action wrapper takes no history. Co-Authored-By: Claude Opus 5.5 --- .../rekey_unsigned_integer_index_values.rs | 15 +++++++-- .../v0/status_tests.rs | 31 ++++++++++++----- .../v0/group_burn_tests.rs | 33 ++++++++++++++----- 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/migration/rekey_unsigned_integer_index_values.rs b/packages/rs-drive/src/drive/contract/migration/rekey_unsigned_integer_index_values.rs index 17e38d50150..45157ed9778 100644 --- a/packages/rs-drive/src/drive/contract/migration/rekey_unsigned_integer_index_values.rs +++ b/packages/rs-drive/src/drive/contract/migration/rekey_unsigned_integer_index_values.rs @@ -522,9 +522,11 @@ mod tests { use dpp::data_contract::{DataContract, DataContractFactory}; use dpp::document::serialization_traits::DocumentPlatformConversionMethodsV0; use dpp::document::{Document, DocumentV0, DocumentV0Setters}; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::identifier::Identifier; use dpp::platform_value::string_encoding::Encoding; use dpp::platform_value::{platform_value, Value}; + use dpp::version::fee::FeeVersion; use dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice; use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::ContestedDocumentVotePollStoredInfo; use dpp::voting::vote_polls::VotePoll; @@ -910,6 +912,10 @@ mod tests { // A rewritten entry is found where protocol version 14 looks for it let platform_version = PlatformVersion::latest(); + // The removed entries carry epoch flags, so pricing their refund + // needs the fee history of the removing block, as every production + // caller passes. + let fee_history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); let document_type = contract .document_type_for_name("item") .expect("expected the item type"); @@ -922,7 +928,7 @@ mod tests { true, None, platform_version, - None, + Some(&fee_history), ) .expect("expected to delete a rewritten item"); insert_document( @@ -963,7 +969,7 @@ mod tests { Some(Cow::Owned(StorageFlags::SingleEpoch(0))), None, platform_version, - None, + Some(&fee_history), ) .expect("expected to update a rewritten item"); assert_eq!( @@ -1172,6 +1178,9 @@ mod tests { let platform_version = PlatformVersion::latest(); let grades = |sql: &str| query_values(&drive, &contract, "score", sql, "grade", platform_version); + // The deleted document carries epoch flags, so pricing its refund + // needs the fee history of the removing block. + let fee_history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); assert_eq!(grades("select * from score where grade == 128"), vec![128]); assert_eq!(grades("select * from score where grade == 1"), vec![1]); @@ -1184,7 +1193,7 @@ mod tests { true, None, platform_version, - None, + Some(&fee_history), ) .expect("expected to delete a document written at protocol version 13"); insert_document( diff --git a/packages/rs-drive/src/verify/group/verify_action_signers_total_power/v0/status_tests.rs b/packages/rs-drive/src/verify/group/verify_action_signers_total_power/v0/status_tests.rs index ad2b99bba34..9d396a0ebba 100644 --- a/packages/rs-drive/src/verify/group/verify_action_signers_total_power/v0/status_tests.rs +++ b/packages/rs-drive/src/verify/group/verify_action_signers_total_power/v0/status_tests.rs @@ -2,6 +2,8 @@ use super::*; use crate::drive::group::paths::{ group_active_action_root_path_vec, group_path_vec, GROUP_CLOSED_ACTIONS_KEY, }; +use crate::util::batch::drive_op_batch::GroupOperationType; +use crate::util::batch::DriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; @@ -12,11 +14,13 @@ use dpp::data_contract::group::v0::GroupV0; use dpp::data_contract::group::Group; use dpp::data_contract::v1::DataContractV1; use dpp::data_contract::DataContract; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::group::action_event::GroupActionEvent; use dpp::group::group_action::v0::GroupActionV0; use dpp::group::group_action::GroupAction; use dpp::platform_serialization::PLATFORM_BINCODE_CONFIG; use dpp::tokens::token_event::TokenEvent; +use dpp::version::fee::FeeVersion; use grovedb::operations::proof::{GroveDBProof, ProofBytes}; use grovedb::{MerkProofDecoder, MerkProofNode, MerkProofOp, PathQuery}; use grovedb_merk::proofs::encoding::encode_into; @@ -105,19 +109,30 @@ fn fixture(platform_version: &PlatformVersion, active: bool, closed: bool) -> Dr ) .expect("record proposing signer"); if action_id == CLOSED_ACTION { + // Closing moves signer-flagged items out of the active tree, and + // pricing that removal needs the fee history of the removing + // block, so the close goes through the production funnel that + // carries it. + let fee_history: CachedEpochIndexFeeVersions = + BTreeMap::from([(0, FeeVersion::first())]); drive - .add_group_action( - CONTRACT.into(), - 0, - None, + .apply_drive_operations( + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id: CONTRACT.into(), + group_contract_position: 0, + initialize_with_insert_action_info: None, + action_id: action_id.into(), + signer_identity_id: SECOND_SIGNER.into(), + signer_power: 5, + closes_group_action: true, + }, + )], true, - action_id.into(), - SECOND_SIGNER.into(), - 5, &BlockInfo::default(), - true, None, platform_version, + Some(&fee_history), ) .expect("close action after second signature"); } diff --git a/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/group_burn_tests.rs b/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/group_burn_tests.rs index 82229023160..87cdd5f3ccb 100644 --- a/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/group_burn_tests.rs +++ b/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/group_burn_tests.rs @@ -1,4 +1,6 @@ use super::*; +use crate::util::batch::drive_op_batch::GroupOperationType; +use crate::util::batch::DriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::data_contract::accessors::v1::DataContractV1Getters; use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; @@ -10,6 +12,7 @@ use dpp::data_contract::group::v0::GroupV0; use dpp::data_contract::group::Group; use dpp::data_contract::v1::DataContractV1; use dpp::data_contract::DataContract; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; use dpp::group::action_event::GroupActionEvent; use dpp::group::group_action::v0::GroupActionV0; use dpp::group::group_action::GroupAction; @@ -23,6 +26,7 @@ use dpp::state_transition::batch_transition::token_burn_transition::v0::TokenBur use dpp::state_transition::batch_transition::token_burn_transition::TokenBurnTransition; use dpp::state_transition::batch_transition::{BatchTransition, BatchTransitionV1}; use dpp::tokens::token_event::TokenEvent; +use dpp::version::fee::FeeVersion; use std::collections::BTreeMap; use std::sync::Arc; @@ -176,19 +180,30 @@ impl Fixture { ) .expect("record proposal"); if !signer_is_proposer { + // A closing co-signature moves signer-flagged items out of the + // active tree, and pricing that removal needs the fee history of + // the removing block, so it goes through the production funnel + // that carries it. + let fee_history: CachedEpochIndexFeeVersions = + BTreeMap::from([(0, FeeVersion::first())]); drive - .add_group_action( - CONTRACT.into(), - 0, - None, - closed, - action_id, - COSIGNER.into(), - 5, - &BlockInfo::default(), + .apply_drive_operations( + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id: CONTRACT.into(), + group_contract_position: 0, + initialize_with_insert_action_info: None, + action_id, + signer_identity_id: COSIGNER.into(), + signer_power: 5, + closes_group_action: closed, + }, + )], true, + &BlockInfo::default(), None, platform_version, + Some(&fee_history), ) .expect("record co-signature"); } From 55903405848287c871cb3571ba0da70b760cc806 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 19 Oct 2026 13:03:42 -0500 Subject: [PATCH 29/46] fix(drive)!: price a removal of only system bytes without the fee history From protocol version 15, consume_to_fees_v1 rejected every sectioned storage removal that came without the removing block's fee history. That included removals whose only bytes are flagged with an epoch but no owner. Those bytes go to removed_bytes_from_system and are never refunded, so there is no refund to price. The rule now requires the history only when owner-attributed bytes remain after the system bucket is taken out. A system-only removal returns no refund and reports its bytes as system bytes, as v0 does. A removal that mixes system and owner bytes still requires the history, and a new test pins that. Co-Authored-By: Claude Opus 5.5 --- packages/rs-drive/src/fees/op.rs | 49 +++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index d549da64f3a..8910794ed90 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -468,10 +468,11 @@ impl LowLevelDriveOperation { /// /// This is the generation `Drive::calculate_fee` v1 selects. It differs /// from `consume_to_fees_v0` in one arm (the ephemeral TTL'd-subtree - /// arm is the same): a `SectionedStorageRemoval` - /// always consults `previous_fee_versions` and returns + /// arm is the same): a `SectionedStorageRemoval` with owner-attributed + /// bytes always consults `previous_fee_versions` and returns /// `CorruptedCodeExecution` when none is given, on every fee version - /// number. v0 priced fee version number 1 against an empty history, so a + /// number. A removal of only unowned flagged bytes (the system bucket) + /// prices no refund and needs no history, as in v0. v0 priced fee version number 1 against an empty history, so a /// caller that forgot the history silently refunded at the first /// generation's storage rates; from protocol version 15 that omission is /// an error, never a fallback to a schedule. @@ -581,6 +582,18 @@ impl LowLevelDriveOperation { let system_amount = removal_per_epoch_by_identifier .remove(&Identifier::default()) .map_or(0, |a| a.values().sum()); + // Bytes flagged with an epoch but no owner are system bytes + // and never refunded, so a removal of nothing else prices no + // refund and needs no history. + if removal_per_epoch_by_identifier.is_empty() { + return Ok(FeeResult { + storage_fee, + processing_fee, + fee_refunds: FeeRefunds::default(), + removed_bytes_from_system: system_amount, + lifetime_storage_fees: Default::default(), + }); + } let previous_fee_versions = previous_fee_versions.ok_or( Error::Drive(DriveError::CorruptedCodeExecution( "a storage refund needs the fee history of the block that removes the bytes", @@ -3259,13 +3272,35 @@ mod tests { } #[test] - fn should_reject_a_removal_of_unowned_flagged_bytes_without_fee_history_in_v1() { + fn should_not_need_fee_history_in_v1_for_a_removal_of_only_unowned_flagged_bytes() { // Bytes flagged with an epoch but no owner land in the system - // bucket; they are never refunded, but the removal is still - // sectioned and the rule is deliberately uniform: every - // sectioned removal carries the history of the removing block. + // bucket and are never refunded, so a removal of only those + // bytes prices no refund and needs no history. let operations = vec![sectioned_removal(&[(Identifier::default(), &[(3, 1000)])])]; + let fee_results = LowLevelDriveOperation::consume_to_fees_v1( + operations, + &epoch(5), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + None, + ) + .expect("system bytes are removed without a refund to price"); + + assert_eq!(fee_results.len(), 1); + assert_eq!(fee_results[0].removed_bytes_from_system, 1000); + assert_eq!(fee_results[0].fee_refunds, FeeRefunds::default()); + } + + #[test] + fn should_reject_a_removal_of_unowned_and_owned_flagged_bytes_without_fee_history_in_v1() { + // The owner's share is refunded, so the system share riding + // along does not exempt the removal from the history. + let operations = vec![sectioned_removal(&[ + (Identifier::default(), &[(3, 1000)]), + (OWNER, &[(3, 1000)]), + ])]; + let result = LowLevelDriveOperation::consume_to_fees_v1( operations, &epoch(5), From 74879103cb8b773a477e4a969ef5d5865a8b9e9f Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Mon, 19 Oct 2026 13:03:42 -0500 Subject: [PATCH 30/46] docs(drive): say why the in-place edits to shipped generations are inert apply_drive_operations v1, which only protocol version 14 selects, exposes forfeit_storage_refunds as pub(super) so generation 2 can share it. The body is unchanged. Drive identity method tables v1 and v2 gain the new credit_storage_refunds_to_owners slot as None, which leaves the method inactive at every version they serve. Comments at both edits now give the reason. Co-Authored-By: Claude Opus 5.5 --- .../version/drive_versions/drive_identity_method_versions/v1.rs | 2 +- .../version/drive_versions/drive_identity_method_versions/v2.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs index 4bb487f3cde..8641c91e2dc 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v1.rs @@ -120,7 +120,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V1: DriveIdentityMethodVersions = remove_from_identity_balance: 0, refresh_identity_key_reference_operations: 0, update_identity_key_limits: None, - credit_storage_refunds_to_owners: None, + credit_storage_refunds_to_owners: None, // backfill: the method is new and inactive here (VersionNotActive), nothing at these versions calls it }, insert: DriveIdentityInsertMethodVersions { add_new_identity: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs index ba65d2d03f4..3c165f13853 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v2.rs @@ -177,7 +177,7 @@ pub const DRIVE_IDENTITY_METHOD_VERSIONS_V2: DriveIdentityMethodVersions = remove_from_identity_balance: 0, refresh_identity_key_reference_operations: 0, update_identity_key_limits: Some(0), - credit_storage_refunds_to_owners: None, + credit_storage_refunds_to_owners: None, // backfill: the method is new and inactive here (VersionNotActive), nothing at these versions calls it }, insert: DriveIdentityInsertMethodVersions { add_new_identity: 0, From 29b76e0252d65bfa89f3fbecc56126aec688b0f9 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 20 Oct 2026 02:49:33 -0500 Subject: [PATCH 31/46] refactor(platform): write the protocol 15 drive tables as struct updates DRIVE_VERSION_V10, DRIVE_IDENTITY_METHOD_VERSIONS_V3 and DRIVE_GROUP_METHOD_VERSIONS_V2 were full copies of their predecessors. The book asks new table constants to use struct update syntax, which shows what each table changes and keeps a rebase from leaving stale slots in a copy. Each now names only its changed slots and takes the rest from V9, identity V2 and group V1. Protocol version 15's drive table is unchanged: its Debug output is byte-identical before and after. Co-Authored-By: Claude Opus 5.5 --- .../drive_group_method_versions/v2.rs | 28 +-- .../drive_identity_method_versions/v3.rs | 175 +----------------- 2 files changed, 8 insertions(+), 195 deletions(-) diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs index 3cbd9457df4..bb2dce490b4 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_group_method_versions/v2.rs @@ -1,6 +1,6 @@ +use crate::version::drive_versions::drive_group_method_versions::v1::DRIVE_GROUP_METHOD_VERSIONS_V1; use crate::version::drive_versions::drive_group_method_versions::{ - DriveGroupCostEstimationMethodVersions, DriveGroupFetchMethodVersions, - DriveGroupInsertMethodVersions, DriveGroupMethodVersions, DriveGroupProveMethodVersions, + DriveGroupInsertMethodVersions, DriveGroupMethodVersions, }; /// Drive group methods for protocol version 15. @@ -12,29 +12,9 @@ use crate::version::drive_versions::drive_group_method_versions::{ /// from this version, and generation 0 committed a closing action before that error surfaced. /// The operation builders are unchanged. pub const DRIVE_GROUP_METHOD_VERSIONS_V2: DriveGroupMethodVersions = DriveGroupMethodVersions { - fetch: DriveGroupFetchMethodVersions { - fetch_action_id_signers_power: 0, - fetch_active_action_info: 0, - fetch_action_id_info_keep_serialized: 0, - fetch_action_id_has_signer: 0, - fetch_group_info: 0, - fetch_group_infos: 0, - fetch_action_infos: 0, - fetch_action_signers: 0, - fetch_action_is_closed: 0, - }, - prove: DriveGroupProveMethodVersions { - prove_group_info: 0, - prove_group_infos: 0, - prove_action_infos: 0, - prove_action_signers: 0, - }, insert: DriveGroupInsertMethodVersions { - add_new_groups: 0, add_group_action: 1, // changed in v15: the fee-returning wrapper prices before it commits its owned transaction + ..DRIVE_GROUP_METHOD_VERSIONS_V1.insert }, - cost_estimation: DriveGroupCostEstimationMethodVersions { - for_add_group_action: 0, - for_add_group: 0, - }, + ..DRIVE_GROUP_METHOD_VERSIONS_V1 }; diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs index 57b56a38298..043173a4439 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_identity_method_versions/v3.rs @@ -1,16 +1,6 @@ +use crate::version::drive_versions::drive_identity_method_versions::v2::DRIVE_IDENTITY_METHOD_VERSIONS_V2; use crate::version::drive_versions::drive_identity_method_versions::{ - DriveIdentityContractInfoMethodVersions, DriveIdentityCostEstimationMethodVersions, - DriveIdentityFetchAttributesMethodVersions, DriveIdentityFetchFullIdentityMethodVersions, - DriveIdentityFetchMethodVersions, DriveIdentityFetchPartialIdentityMethodVersions, - DriveIdentityFetchPublicKeyHashesMethodVersions, DriveIdentityInsertMethodVersions, - DriveIdentityKeyHashesToIdentityInsertMethodVersions, DriveIdentityKeysBudgetMethodVersions, - DriveIdentityKeysFetchMethodVersions, DriveIdentityKeysInsertMethodVersions, - DriveIdentityKeysMethodVersions, DriveIdentityKeysProveMethodVersions, - DriveIdentityMethodVersions, DriveIdentityProveMethodVersions, - DriveIdentityUpdateMethodVersions, DriveIdentityWithdrawalDocumentMethodVersions, - DriveIdentityWithdrawalMethodVersions, DriveIdentityWithdrawalTransactionIndexMethodVersions, - DriveIdentityWithdrawalTransactionMethodVersions, - DriveIdentityWithdrawalTransactionQueueMethodVersions, + DriveIdentityMethodVersions, DriveIdentityUpdateMethodVersions, }; /// V3 is protocol version 15's identity-method table. It differs from V2 in @@ -27,166 +17,9 @@ use crate::version::drive_versions::drive_identity_method_versions::{ /// V2 keep the slot `None`. pub const DRIVE_IDENTITY_METHOD_VERSIONS_V3: DriveIdentityMethodVersions = DriveIdentityMethodVersions { - fetch: DriveIdentityFetchMethodVersions { - public_key_hashes: DriveIdentityFetchPublicKeyHashesMethodVersions { - fetch_full_identities_by_unique_public_key_hashes: 0, - fetch_full_identity_by_unique_public_key_hash: 0, - fetch_identity_id_by_unique_public_key_hash: 0, - fetch_identity_ids_by_non_unique_public_key_hash: 0, - fetch_identity_ids_by_unique_public_key_hashes: 0, - fetch_serialized_full_identity_by_unique_public_key_hash: 0, - has_any_of_unique_public_key_hashes: 0, - has_non_unique_public_key_hash: 0, - has_non_unique_public_key_hash_already_for_identity: 0, - has_unique_public_key_hash: 0, - fetch_full_identity_by_non_unique_public_key_hash: 0, - }, - attributes: DriveIdentityFetchAttributesMethodVersions { - revision: 0, - nonce: 0, - identity_contract_nonce: 0, - balance: 0, - balance_include_debt: 0, - negative_balance: 0, - }, - partial_identity: DriveIdentityFetchPartialIdentityMethodVersions { - fetch_identity_revision_with_keys: 0, - fetch_identity_balance_with_keys: 0, - fetch_identity_balance_with_keys_and_revision: 0, - fetch_identity_with_balance: 0, - fetch_identity_keys: 0, - }, - full_identity: DriveIdentityFetchFullIdentityMethodVersions { - fetch_full_identity: Some(0), - fetch_full_identities: Some(0), - }, - }, - prove: DriveIdentityProveMethodVersions { - full_identity: 0, - full_identities: 0, - identity_nonce: 0, - identity_contract_nonce: 0, - identities_contract_keys: 0, - prove_full_identities_by_unique_public_key_hashes: 0, - prove_full_identity_by_unique_public_key_hash: 0, - prove_identity_id_by_unique_public_key_hash: 0, - prove_identity_ids_by_unique_public_key_hashes: 0, - prove_full_identity_by_non_unique_public_key_hash: 0, - prove_full_identity_by_non_unique_public_key_hash_two_proofs: 0, - }, - keys: DriveIdentityKeysMethodVersions { - fetch: DriveIdentityKeysFetchMethodVersions { - fetch_all_current_identity_keys: 0, - fetch_all_identity_keys: 0, - fetch_identities_all_keys: 0, - fetch_identity_keys: 0, - fetch_identities_contract_keys: 0, - }, - prove: DriveIdentityKeysProveMethodVersions { - prove_identities_all_keys: 0, - prove_identity_keys: 0, - }, - insert: DriveIdentityKeysInsertMethodVersions { - create_key_tree_with_keys: 0, - create_new_identity_key_query_trees: 0, - insert_key_searchable_references: 0, - insert_key_to_storage: 0, - insert_new_non_unique_key: 1, - insert_new_unique_key: 1, - replace_key_in_storage: 0, - }, - insert_key_hash_identity_reference: - DriveIdentityKeyHashesToIdentityInsertMethodVersions { - add_estimation_costs_for_insert_non_unique_public_key_hash_reference: 0, - add_estimation_costs_for_insert_unique_public_key_hash_reference: 0, - insert_non_unique_public_key_hash_reference_to_identity: 0, - insert_reference_to_non_unique_key: 0, - insert_reference_to_unique_key: 0, - insert_unique_public_key_hash_reference_to_identity: 0, - }, - budget: DriveIdentityKeysBudgetMethodVersions { - insert_identity_key_budget: Some(0), - fetch_identity_key_remaining_budget: Some(0), - deduct_from_identity_key_budget: Some(0), - add_estimation_costs_for_key_budgets: Some(0), - fetch_identity_keys_remaining_budgets: Some(0), - prove_identity_keys_remaining_budgets: Some(0), - add_to_identity_key_budget: Some(0), - }, - }, update: DriveIdentityUpdateMethodVersions { - update_identity_revision: 0, - merge_identity_nonce: 0, - update_identity_negative_credit_operation: 0, - initialize_identity_revision: 0, - disable_identity_keys: 1, - re_enable_identity_keys: 0, - add_new_non_unique_keys_to_identity: 0, - add_new_unique_keys_to_identity: 0, - add_new_keys_to_identity: 0, - insert_identity_balance: 0, - initialize_negative_identity_balance: 0, - add_to_identity_balance: 1, // changed in v14: credits that repay an identity's debt are owed to the processing fee pool - add_to_previous_balance: 0, - apply_balance_change_from_fee_to_identity: 1, // changed in v14: routes the debt a balance change repaid to the processing fee pool - remove_from_identity_balance: 0, - refresh_identity_key_reference_operations: 0, - update_identity_key_limits: Some(0), credit_storage_refunds_to_owners: Some(0), // new in v15: credits recorded refund owners, reports the unrouted amount for the processing pool + ..DRIVE_IDENTITY_METHOD_VERSIONS_V2.update }, - insert: DriveIdentityInsertMethodVersions { - add_new_identity: 0, - }, - contract_info: DriveIdentityContractInfoMethodVersions { - add_potential_contract_info_for_contract_bounded_key: 1, - refresh_potential_contract_info_key_references: 1, - merge_identity_contract_nonce: 0, - }, - cost_estimation: DriveIdentityCostEstimationMethodVersions { - for_authentication_keys_security_level_in_key_reference_tree: 0, - for_balances: 0, - for_token_balances: 0, - for_token_total_supply: 0, - for_contract_info: 0, - for_contract_info_group: 0, - for_contract_info_group_keys: 0, - for_contract_info_group_key_purpose: 0, - for_keys_for_identity_id: 0, - for_negative_credit: 0, - for_purpose_in_key_reference_tree: 0, - for_root_key_reference_tree: 0, - for_update_revision: 0, - for_token_identity_infos: 0, - for_token_perpetual_distribution: 0, - for_token_once_per_identity_distribution: 0, - for_token_pre_programmed_distribution: 0, - for_root_token_ms_interval_distribution: 0, - for_token_selling_prices: 0, - for_token_contract_infos: 0, - }, - withdrawals: DriveIdentityWithdrawalMethodVersions { - document: DriveIdentityWithdrawalDocumentMethodVersions { - fetch_oldest_withdrawal_documents_by_status: 0, - find_withdrawal_documents_by_status_and_transaction_indices: 1, - }, - transaction: DriveIdentityWithdrawalTransactionMethodVersions { - index: DriveIdentityWithdrawalTransactionIndexMethodVersions { - fetch_next_withdrawal_transaction_index: 0, - add_update_next_withdrawal_transaction_index_operation: 0, - }, - queue: DriveIdentityWithdrawalTransactionQueueMethodVersions { - add_enqueue_untied_withdrawal_transaction_operations: 0, - dequeue_untied_withdrawal_transactions: 0, - remove_broadcasted_withdrawal_transactions_after_completion_operations: 0, - move_broadcasted_withdrawal_transactions_back_to_queue_operations: 0, - }, - }, - calculate_current_withdrawal_limit: 1, // changed in v14: daily maximum is a percentage of the total credits a day ago plus the credit inflows of the last 25 hours - record_total_credits_history: Some(0), // new in v14: total credits history for the day-lagged daily withdrawal limit - fetch_total_credits_in_platform_a_day_ago: Some(0), // new in v14 - record_credit_inflows: Some(0), // new in v14: credit inflows sum tree for the net daily withdrawal limit - record_core_credit_pool_blocks: Some(0), // new in v14: Core credit pool balances for the Core-anchored withdrawal limit - fetch_core_credit_pool_balances: Some(0), // new in v14 - fetch_in_flight_withdrawal_amount: Some(0), // new in v14 - }, + ..DRIVE_IDENTITY_METHOD_VERSIONS_V2 }; From b561115afe83b5dcd9db4a1a1d9e63f7efdc2747 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 20 Oct 2026 02:49:33 -0500 Subject: [PATCH 32/46] refactor(drive): write refund owners' negative credit through the versioned dispatcher credit_storage_refunds_to_owners_operations called update_identity_negative_credit_operation_v0 directly, so a later table selecting another writer would not govern this path. It now goes through update_identity_negative_credit_operation with the platform version. Every table selects 0, so the operations it emits are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../credit_storage_refunds_to_owners_operations/v0/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index 76052d3c634..a58dd1506a8 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -3,7 +3,6 @@ use crate::drive::identity::update::storage_refund_credit_outcome::StorageRefund use crate::drive::Drive; use crate::error::Error; use crate::fees::op::LowLevelDriveOperation; -use crate::util::estimated_layers::EstimatedLayers; use dpp::fee::fee_result::refunds::FeeRefunds; use dpp::fee::Credits; use dpp::prelude::Identifier; @@ -75,10 +74,11 @@ impl Drive { } if let Some(new_negative_balance) = outcome.negative_credit_balance_modified() { - drive_operations.push(self.update_identity_negative_credit_operation_v0( + drive_operations.push(self.update_identity_negative_credit_operation( *owner_id, new_negative_balance, - )); + platform_version, + )?); } // From a zero balance the helper clears negative credit first and only From 37b525fd6269b12e6b11a155e4e77c4caf16bcee Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Tue, 20 Oct 2026 02:49:33 -0500 Subject: [PATCH 33/46] test(drive): strip more than the refund dust limit in the migration freeze test The protocol 12 schema strip freeze test added a boolean unknown property, about 25 bytes. Refunds below 32 bytes per storage epoch are dropped, so an implementation that wrongly refunded the stripped bytes would still pass. The property is now a 64-character string, and the test asserts the contract grows by at least 32 bytes. Co-Authored-By: Claude Opus 5.5 --- .../strip_unknown_document_schema_properties.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs b/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs index dc854c51d74..59dfc1d50a2 100644 --- a/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs +++ b/packages/rs-drive/src/drive/contract/migration/strip_unknown_document_schema_properties.rs @@ -338,7 +338,9 @@ mod tests { // Rewrite the stored bytes the way a pre-v12 contract could look: // with a top-level document schema property the v1 meta-schema does - // not allow. The element keeps its flags and grows. + // not allow. The element keeps its flags and grows by more than the + // 32 bytes below which a removal is never refunded, so an + // implementation that settled the stripped bytes would show here. let bincode_config = bincode::config::standard() .with_big_endian() .with_no_limit(); @@ -351,11 +353,17 @@ mod tests { .get_mut("person") .expect("expected the person document type"); person_schema - .insert("legacyUnknownProperty".to_string(), Value::Bool(true)) + .insert( + "legacyUnknownProperty".to_string(), + Value::Text("x".repeat(64)), + ) .expect("expected to add the unknown property"); let inflated_bytes = bincode::encode_to_vec(&serialization_format, bincode_config) .expect("expected to encode the inflated contract"); - assert!(inflated_bytes.len() > clean_bytes.len()); + assert!( + inflated_bytes.len() - clean_bytes.len() >= 32, + "the stripped bytes must exceed the refund dust limit" + ); drive .grove_insert( (&contract_path).into(), From 8fbf2b2cc040da6156b74a931c28f4c7404819f7 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 06:30:49 -0500 Subject: [PATCH 34/46] refactor(drive): give price-before-commit its own apply contract generation After the rebase onto compiled contract storage (#5579), generation 1 of apply_contract_with_serialization carried two features: #5579's compiled-storage operations and this change's price-before-commit wrapper. The coding conventions give each feature its own implementation module, even inside one unreleased protocol version. Generation 1 is restored to what #5579 wrote. The new generation 2 is that wrapper made price-first, and it builds its operations with generation 1's compiled-storage builder. The operations dispatcher therefore sends 1 and 2 to operations_v1. Protocol version 15's contract table selects 2. Its behaviour is the same as before this commit, and the docs now name generation 2. Co-Authored-By: Claude Opus 5.5 --- .../apply_contract_with_serialization/mod.rs | 18 ++++- .../v1/mod.rs | 18 +---- .../v2/mod.rs | 79 +++++++++++++++++++ .../drive_contract_method_versions/v5.rs | 8 +- .../src/version/drive_versions/v10.rs | 5 +- .../rs-platform-version/src/version/v15.rs | 4 +- 6 files changed, 104 insertions(+), 28 deletions(-) create mode 100644 packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs index 2ed65ff6542..d71b5cf8244 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs @@ -14,6 +14,7 @@ use std::borrow::Cow; mod v0; mod v1; +mod v2; /// Drive contract application methods. impl Drive { @@ -87,9 +88,18 @@ impl Drive { transaction, platform_version, ), + 2 => self.apply_contract_with_serialization_v2( + contract, + contract_serialization, + block_info, + apply, + storage_flags, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "apply_contract_with_serialization".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } @@ -152,7 +162,9 @@ impl Drive { transaction, platform_version, ), - 1 => self.apply_contract_with_serialization_operations_v1( + // Generation 2 changes only when its wrapper commits, so it builds the + // operations as generation 1 does. + 1 | 2 => self.apply_contract_with_serialization_operations_v1( contract, contract_serialization, block_info, @@ -163,7 +175,7 @@ impl Drive { ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "apply_contract_with_serialization_operations".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs index 7c9d59df2e3..97b265c580e 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v1/mod.rs @@ -27,15 +27,7 @@ use std::borrow::Cow; impl Drive { /// Applies a contract and returns the fee for applying: generation 0's, with the operations - /// of generation 1 ([`Self::apply_contract_with_serialization_operations_v1`]), and one - /// change in when it commits: when the caller supplies no transaction and the operations - /// are applied, the write and its pricing share one owned transaction that is committed - /// only after `Drive::calculate_fee` succeeded. Generation 0 applied the batch (committing - /// it on its own without a caller transaction) and priced it afterwards, so from protocol - /// version 15, where pricing an owner-attributed storage removal without the fee history is - /// an error, a call passing no history would have persisted the write and then failed. It - /// now fails before anything is written. With a caller transaction nothing is committed by - /// Drive in either generation. + /// of generation 1 ([`Self::apply_contract_with_serialization_operations_v1`]). #[inline(always)] #[allow(clippy::too_many_arguments)] pub(super) fn apply_contract_with_serialization_v1( @@ -48,9 +40,6 @@ impl Drive { transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result { - let owned_transaction = - (apply && transaction.is_none()).then(|| self.grove.start_transaction()); - let transaction = owned_transaction.as_ref().or(transaction); let mut cost_operations = vec![]; let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); let batch_operations = self.apply_contract_with_serialization_operations_v1( @@ -72,7 +61,6 @@ impl Drive { )?; cost_operations.push(CalculatedCostOperation(fetch_cost)); - // A pricing error drops the owned transaction with everything it wrote. let fees = Drive::calculate_fee( None, Some(cost_operations), @@ -81,10 +69,6 @@ impl Drive { platform_version, None, )?; - if let Some(owned_transaction) = owned_transaction { - self.commit_transaction(owned_transaction, &platform_version.drive)?; - } - Ok(fees) } diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs new file mode 100644 index 00000000000..e26aff17b45 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs @@ -0,0 +1,79 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::fees::op::LowLevelDriveOperation::CalculatedCostOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::storage_flags::StorageFlags; +use dpp::block::block_info::BlockInfo; +use dpp::fee::fee_result::FeeResult; +use dpp::prelude::DataContract; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; +use std::borrow::Cow; + +impl Drive { + /// Applies a contract and returns the fee for applying, pricing it before anything is + /// committed. + /// + /// Generation 2 differs from generation 1 in one thing, when it commits: when the caller + /// supplies no transaction and the operations are applied, the write and its pricing share + /// one owned transaction that is committed only after `Drive::calculate_fee` succeeded. The + /// operations are generation 1's ([`Self::apply_contract_with_serialization_operations_v1`], + /// the compiled-storage builder). Generation 1 applied the batch (committing it on its own + /// without a caller transaction) and priced it afterwards, so from protocol version 15, + /// where pricing an owner-attributed storage removal without the fee history is an error, a + /// call passing no history would have persisted the write and then failed. It now fails + /// before anything is written. With a caller transaction nothing is committed by Drive in + /// either generation. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn apply_contract_with_serialization_v2( + &self, + contract: &DataContract, + contract_serialization: Vec, + block_info: BlockInfo, + apply: bool, + storage_flags: Option>, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(transaction); + let mut cost_operations = vec![]; + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + let batch_operations = self.apply_contract_with_serialization_operations_v1( + contract, + contract_serialization, + &block_info, + &mut estimated_costs_only_with_layer_info, + storage_flags, + transaction, + platform_version, + )?; + let fetch_cost = LowLevelDriveOperation::combine_cost_operations(&batch_operations); + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + &mut cost_operations, + &platform_version.drive, + )?; + cost_operations.push(CalculatedCostOperation(fetch_cost)); + + // A pricing error drops the owned transaction with everything it wrote. + let fees = Drive::calculate_fee( + None, + Some(cost_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + None, + )?; + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + Ok(fees) + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs index 6cae7ebbc3b..39c554b71b3 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_contract_method_versions/v5.rs @@ -8,7 +8,7 @@ use crate::version::drive_versions::drive_contract_method_versions::{ /// /// Relative to [`super::v4::DRIVE_CONTRACT_METHOD_VERSIONS_V4`]: /// -/// * `apply.apply_contract_with_serialization` is bumped to `1`. From protocol version 15 a +/// * `apply.apply_contract_with_serialization` generation 1. From protocol version 15 a /// contract is stored compiled (format version 2), so the bytes of an unchanged contract can /// differ from the ones it was stored with in format version 1. v1 writes nothing when the /// two contracts' format version 1 bytes match, where v0 compared the stored bytes alone and @@ -17,8 +17,8 @@ use crate::version::drive_versions::drive_contract_method_versions::{ /// a generation that, when the caller passes no transaction, writes and prices inside one /// owned transaction and commits it only once `Drive::calculate_fee` succeeded: /// `update.update_contract` 2 -> 3 (the element writer stays at generation 2), -/// `apply.apply_contract_with_serialization` 1 (the generation above; its operation builder -/// is the compiled-storage one), and the five moderation writers `remove_contract_ban`, +/// `apply.apply_contract_with_serialization` 1 -> 2 (generation 1's wrapper made price-first; +/// it builds the operations with generation 1's compiled-storage builder), and the five moderation writers `remove_contract_ban`, /// `remove_contract_suspension`, `remove_contract_warnings`, `add_contract_suspension` and /// `add_contract_warning` (whose replacement of an existing entry may shrink it) 0 -> 1. /// Pricing an owner-attributed storage removal without the fee history is an error from this @@ -26,7 +26,7 @@ use crate::version::drive_versions::drive_contract_method_versions::{ pub const DRIVE_CONTRACT_METHOD_VERSIONS_V5: DriveContractMethodVersions = DriveContractMethodVersions { apply: DriveContractApplyMethodVersions { - apply_contract_with_serialization: 1, + apply_contract_with_serialization: 2, // generation 1's compiled storage, priced before an owned transaction commits ..DRIVE_CONTRACT_METHOD_VERSIONS_V4.apply }, update: DriveContractUpdateMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 58ddb174667..9076304bdf0 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -62,7 +62,8 @@ use crate::version::drive_versions::{ /// * **Price before commit** — `batch_operations.apply_drive_operations` /// 1 -> 2, `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` /// 0 -> 1), `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (`update_contract` 2 -> 3, -/// `apply_contract_with_serialization` 0 -> 1, the five moderation +/// `apply_contract_with_serialization` 0 -> 2 over generation 1's +/// operations, the five moderation /// writers that can free flagged bytes 0 -> 1) and /// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (the ten fee-returning document /// wrappers 0 -> 1). Every fee-returning entry point that owns its @@ -74,7 +75,7 @@ use crate::version::drive_versions::{ /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { - contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled; update_contract v3, apply_contract_with_serialization v1 and the moderation writers price before committing an owned transaction + contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: apply_contract_with_serialization 2 writes nothing for a contract whose format version 1 bytes match the stored one's, as contracts are stored compiled, and prices before committing an owned transaction; update_contract v3 and the moderation writers price before committing too document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates; the ten fee-returning document wrappers price before committing an owned transaction vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 5c22e8291b2..55de99f4298 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -369,7 +369,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// 17. **Pricing before commit**: the Drive entry points that own their /// transaction when a caller passes none (`apply_drive_operations` v2, /// the ten document wrappers v1, `update_contract` v3, -/// `apply_contract_with_serialization` v1, `add_group_action` v1, the +/// `apply_contract_with_serialization` v2, `add_group_action` v1, the /// moderation writers v1) price the batch before committing it, so the /// error in item 15 never leaves a write persisted without its fee /// result. @@ -377,7 +377,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// Everything else matches v14. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, - drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); calculate_fee v1 (fee history required for refunds) + identity table v3 (credit_storage_refunds_to_owners); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store (note 13); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) + drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 2 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8) and prices before it commits; calculate_fee v1 (fee history required for refunds) + identity table v3 (credit_storage_refunds_to_owners); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store (note 13); apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) drive_abci: DriveAbciVersion { structs: DRIVE_ABCI_STRUCTURE_VERSIONS_V2, methods: DRIVE_ABCI_METHOD_VERSIONS_V11, // changed: the yes/no poll's tally, record, clean-up and finish hook are Some(0) (note 10); verify_chain_lock_locally 1 rejects a chain lock locally only when Platform holds the quorums active at its signing height, and verify_chain_lock_through_core 1 has Core's verifychainlock check the others before submitchainlock From f5002128553d717662fd95ba7ee6714765da1cd5 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 08:31:25 -0500 Subject: [PATCH 35/46] refactor(drive): reach the protocol 15 fee generation only through calculate_fee consume_to_fees_v1 was public through the fees::op module, so a caller could use protocol 15's strict fee-history pricing without a platform version selecting it. It is now pub(super), visible to Drive::calculate_fee v1 and the fee tests only, so the drive tables decide which generation prices a batch. The shipped consume_to_fees_v0 stays public. Co-Authored-By: Claude Opus 5.5 --- packages/rs-drive/src/fees/op.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index 8910794ed90..76f13991158 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -476,7 +476,7 @@ impl LowLevelDriveOperation { /// caller that forgot the history silently refunded at the first /// generation's storage rates; from protocol version 15 that omission is /// an error, never a fallback to a schedule. - pub fn consume_to_fees_v1( + pub(super) fn consume_to_fees_v1( drive_operations: Vec, epoch: &Epoch, epochs_per_era: u16, From a6b051d30fdf66c38c56ed04c20555542f9c437d Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 09:58:00 -0500 Subject: [PATCH 36/46] refactor(drive): keep the protocol 15 fee and batch generations behind their dispatchers calculate_fee_v1 and apply_drive_operations_v2 were pub(crate), so any module in rs-drive could call protocol 15's generation without a platform version selecting it. Both are now pub(super), visible only to their dispatchers, which are their only callers. They were the last two leaf generations in this change wider than their dispatcher. The shipped siblings keep their visibility. Co-Authored-By: Claude Opus 5.5 --- packages/rs-drive/src/fees/calculate_fee/v1/mod.rs | 2 +- .../drive_methods/apply_drive_operations/v2/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs b/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs index 4201dc023bd..76d28451dc4 100644 --- a/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs +++ b/packages/rs-drive/src/fees/calculate_fee/v1/mod.rs @@ -16,7 +16,7 @@ impl Drive { /// which requires and consults the fee history for every owner-attributed /// storage removal. Selected by drive table v10 (protocol version 15). #[inline(always)] - pub(crate) fn calculate_fee_v1( + pub(super) fn calculate_fee_v1( base_operations: Option>, drive_operations: Option>, epoch: &Epoch, diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index df341386d5f..1828ebc80d6 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -113,7 +113,7 @@ impl Drive { /// finalize tasks still run after the commit. With a caller transaction nothing is /// committed by Drive in either generation. #[inline(always)] - pub(crate) fn apply_drive_operations_v2( + pub(super) fn apply_drive_operations_v2( &self, operations: Vec, apply: bool, From 9768b757b2a58d14991d4795049f18d2a66255f4 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 11:26:05 -0500 Subject: [PATCH 37/46] docs(drive): record protocol 15 refund fee history and owner credits in the changelog Add an Unreleased entry next to #5579's. It covers the fee history now required for owner-attributed storage refunds at protocol version 15, price-before-commit in Drive's fee-returning entry points, the new Drive::credit_storage_refunds_to_owners_operations API with StorageRefundCreditOutcome, and the DriveIdentityUpdateMethodVersions field that struct literals now need. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608d4d49125..9afe3daf72e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -116,6 +116,23 @@ gains `DataContractCompiledMaxSizeExceededError` and `ProtocolValidationOperation` gains `ContractCompiledSizeValidation`, so struct literals and exhaustive matches need them. See [#5579](https://github.com/dashpay/platform/pull/5579). +- **drive (breaking, protocol version 15):** from protocol version 15 a storage refund + for owner-attributed bytes is priced only with the fee history of the block that + removes the bytes, on every fee version number. A missing history is an internal + error instead of a silent fallback to the first generation's storage rates; a + removal of only system bytes needs none. Every shipped fee schedule shares fee + version number 1 and the same storage rates, so refund credits are unchanged for + shipped inputs. Drive's fee-returning entry points that open their own transaction + when the caller passes none (`apply_drive_operations` and the document, contract, + moderation and group writers) now price a batch before committing it, so a pricing + error leaves nothing written. Rust API: + `Drive::credit_storage_refunds_to_owners_operations` credits each recorded owner of + a refund without consulting keys or permissions, clearing an owner's debt first, and + returns a `StorageRefundCreditOutcome` whose `processing_pool_share()` is the + refunds without an owner balance plus the repaid debt, for the caller's processing + pool write; settling refunds on block lifecycle paths comes in a follow-up. + `DriveIdentityUpdateMethodVersions` gains `credit_storage_refunds_to_owners`, so + struct literals need it. See [#4706](https://github.com/dashpay/platform/pull/4706). ### [5.0.1](https://github.com/dashpay/platform/compare/v5.0.0...v5.0.1) (2026-10-27) From 0203d33a7337bacd740456a9927a9f75e3d9a271 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 13:03:58 -0500 Subject: [PATCH 38/46] test(drive): pin that the genesis epoch's empty fee history prices owner refunds A review proposed rejecting an empty fee history in consume_to_fees_v1 as if it were missing. The platform state's history is empty for the whole genesis epoch: the epoch change hook records a fee version only on epoch changes that are not genesis, so the history never covers epoch 0, and DPP prices the epochs it does not cover at FeeVersion::first(). Rejecting an empty history would halt any chain on its first owner refund in the genesis epoch, including every chain started at protocol version 15. A comment at the guard now says why only an absent history is an error, and a test pins that v1 prices a genesis-epoch owner refund under an empty history exactly as v0 does. The test fails with the proposed change. Co-Authored-By: Claude Opus 5.5 --- packages/rs-drive/src/fees/op.rs | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index 76f13991158..e9f3664d8bc 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -594,6 +594,14 @@ impl LowLevelDriveOperation { lifetime_storage_fees: Default::default(), }); } + // Only an absent history is an error. An empty one is + // the platform state's history throughout the genesis + // epoch: the epoch change hook records a fee version at + // each epoch change but not the genesis one, so the + // history never covers epoch 0 and DPP prices the epochs + // it does not cover at `FeeVersion::first()`. Rejecting + // an empty history would halt a chain on its first + // owner refund in the genesis epoch. let previous_fee_versions = previous_fee_versions.ok_or( Error::Drive(DriveError::CorruptedCodeExecution( "a storage refund needs the fee history of the block that removes the bytes", @@ -3238,6 +3246,39 @@ mod tests { .expect("the storage epoch should be refunded") } + #[test] + fn should_price_an_owner_refund_in_v1_with_the_empty_history_of_the_genesis_epoch() { + // Throughout the genesis epoch the platform state's history is + // empty, because the epoch change hook records nothing at genesis. + // An owner refund there is priced, as v0 prices it, not rejected. + let history: CachedEpochIndexFeeVersions = BTreeMap::new(); + let removal = || vec![sectioned_removal(&[(OWNER, &[(0, 1000)])])]; + + let v1_results = LowLevelDriveOperation::consume_to_fees_v1( + removal(), + &epoch(0), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + Some(&history), + ) + .expect("an empty history is the genesis epoch's history, not a missing one"); + let v0_results = LowLevelDriveOperation::consume_to_fees_v0( + removal(), + &epoch(0), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + Some(&history), + ) + .expect("v0 prices the genesis epoch's empty history"); + + assert_eq!(v1_results, v0_results); + assert_ne!( + v1_results[0].fee_refunds, + FeeRefunds::default(), + "the owner's genesis-epoch bytes are refunded" + ); + } + #[test] fn should_reject_a_sectioned_removal_without_fee_history_in_v1() { let operations = vec![sectioned_removal(&[(OWNER, &[(3, 1000)])])]; From eb6bf5c362a854974f35413997f28b53dc5e30cb Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 18:36:10 -0500 Subject: [PATCH 39/46] fix(drive)!: reject an empty fee history after the genesis epoch consume_to_fees_v1 rejected only an absent history. An empty one priced an owner refund at FeeVersion::first() at any epoch, which is the silent fallback the protocol 15 rule exists to remove. An empty history is valid only in the genesis epoch. The epoch change hook records a fee version at the first epoch change after genesis, and run_block_proposal runs it before the block's state transitions, DAO events and core updates. check_tx reads the epoch and the history from the same committed state. So from epoch 1 on an empty history was lost or never passed, and it is now an error like an absent one. The genesis epoch keeps its empty history, and a new test covers the rejection at epoch 1. Test fixtures that reached later epochs without the hook relied on the empty history. fast_forward_to_block now records the epoch 1 fee version as the hook would. Three token distribution tests that processed transitions with a platform state loaded before their fast-forward now read the state again. Co-Authored-By: Claude Opus 5.5 --- .../token/distribution/pre_programmed.rs | 6 +++ .../src/test/helpers/fast_forward_to_block.rs | 10 ++++ packages/rs-drive/src/fees/op.rs | 48 ++++++++++++++----- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/pre_programmed.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/pre_programmed.rs index 06b241fcd15..164d100d9e9 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/pre_programmed.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/distribution/pre_programmed.rs @@ -52,6 +52,8 @@ mod pre_programmed_distribution { ); fast_forward_to_block(&platform, 100, 40, 42, 1, false); + // The fast-forward records the epoch 1 fee history, so read the state again. + let platform_state = platform.state.load(); let claim_transition = BatchTransition::new_token_claim_transition( token_id, @@ -221,6 +223,8 @@ mod pre_programmed_distribution { ); fast_forward_to_block(&platform, 100, 40, 42, 1, false); + // The fast-forward records the epoch 1 fee history, so read the state again. + let platform_state = platform.state.load(); let claim_transition = BatchTransition::new_token_claim_transition( token_id, @@ -503,6 +507,8 @@ mod pre_programmed_distribution { ); fast_forward_to_block(&platform, 100, 40, 42, 1, false); + // The fast-forward records the epoch 1 fee history, so read the state again. + let platform_state = platform.state.load(); let claim_transition = BatchTransition::new_token_claim_transition( token_id, diff --git a/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs b/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs index e991b965860..99de9047241 100644 --- a/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs +++ b/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs @@ -32,6 +32,16 @@ pub(crate) fn fast_forward_to_block( let protocol_version = platform_state.current_protocol_version_in_consensus(); let platform_version = PlatformVersion::get(protocol_version).unwrap(); + // On a chain the epoch change hook records the fee version at the first epoch + // change after genesis, when the history is still empty. Fast-forwarding past + // genesis skips the hook, so record that entry as the hook would have: from + // protocol version 15 an empty history after the genesis epoch is an error. + if epoch_index > 0 && platform_state.previous_fee_versions().is_empty() { + platform_state + .previous_fee_versions_mut() + .insert(1, &platform_version.fee_version); + } + let block_info = BlockInfo { time_ms, //less than 2 weeks height, diff --git a/packages/rs-drive/src/fees/op.rs b/packages/rs-drive/src/fees/op.rs index e9f3664d8bc..25c262fd07a 100644 --- a/packages/rs-drive/src/fees/op.rs +++ b/packages/rs-drive/src/fees/op.rs @@ -594,19 +594,22 @@ impl LowLevelDriveOperation { lifetime_storage_fees: Default::default(), }); } - // Only an absent history is an error. An empty one is - // the platform state's history throughout the genesis - // epoch: the epoch change hook records a fee version at - // each epoch change but not the genesis one, so the - // history never covers epoch 0 and DPP prices the epochs - // it does not cover at `FeeVersion::first()`. Rejecting - // an empty history would halt a chain on its first - // owner refund in the genesis epoch. - let previous_fee_versions = previous_fee_versions.ok_or( - Error::Drive(DriveError::CorruptedCodeExecution( + // An empty history is the platform state's history + // throughout the genesis epoch: the epoch change hook + // records a fee version at each epoch change but not the + // genesis one, so the history never covers epoch 0 and + // DPP prices the epochs it does not cover at + // `FeeVersion::first()`. Rejecting it there would halt a + // chain on its first owner refund in the genesis epoch. + // From epoch 1 on the hook has recorded a fee version + // before any state transition of that block runs, so an + // empty history there was lost or never passed, and is an + // error like an absent one. + let previous_fee_versions = previous_fee_versions + .filter(|history| epoch.index == 0 || !history.is_empty()) + .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( "a storage refund needs the fee history of the block that removes the bytes", - )), - )?; + )))?; ( FeeRefunds::from_storage_removal( removal_per_epoch_by_identifier, @@ -3279,6 +3282,27 @@ mod tests { ); } + #[test] + fn should_reject_an_owner_refund_in_v1_with_an_empty_history_after_the_genesis_epoch() { + // From epoch 1 on the epoch change hook has recorded a fee version, so + // an empty history is a lost one: it is rejected like an absent one + // instead of pricing the refund at the first schedule. + let history: CachedEpochIndexFeeVersions = BTreeMap::new(); + + let result = LowLevelDriveOperation::consume_to_fees_v1( + vec![sectioned_removal(&[(OWNER, &[(0, 1000)])])], + &epoch(1), + DEFAULT_EPOCHS_PER_ERA, + &FEE_VERSION1, + Some(&history), + ); + + assert!(matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + )); + } + #[test] fn should_reject_a_sectioned_removal_without_fee_history_in_v1() { let operations = vec![sectioned_removal(&[(OWNER, &[(3, 1000)])])]; From b13975440008205aaf586964f7221d0c2c3c2d21 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Wed, 28 Oct 2026 18:36:10 -0500 Subject: [PATCH 40/46] docs(drive): name generation 2 as protocol 15's apply contract generation Two comments still said protocol version 15 selects apply_contract_with_serialization 1: the compiled-storage note in v15.rs (reverted by a rebase) and the system contract cache's note on re-applying contracts. Protocol 15 selects generation 2, which reuses generation 1's compiled-storage operations. Co-Authored-By: Claude Opus 5.5 --- packages/rs-drive/src/cache/system_contracts.rs | 3 ++- packages/rs-platform-version/src/version/v15.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/rs-drive/src/cache/system_contracts.rs b/packages/rs-drive/src/cache/system_contracts.rs index bb83284ad3e..55321889e72 100644 --- a/packages/rs-drive/src/cache/system_contracts.rs +++ b/packages/rs-drive/src/cache/system_contracts.rs @@ -73,7 +73,8 @@ impl SystemDataContracts { /// `perform_events_on_first_block_of_protocol_change`). From protocol version 15 a contract /// stored compiled (format version 2) keeps the meaning it was compiled with, and applying /// it again writes nothing while its format version 1 bytes are unchanged - /// (`apply_contract_with_serialization` 1): a change that compiles an unchanged system + /// (the operations of `apply_contract_with_serialization` 1, which generation 2, the one + /// protocol version 15 selects, reuses): a change that compiles an unchanged system /// contract differently rewrites it with its new compiled bytes, as /// `apply_contract_rebuilding_document_types` does, not by applying it again. /// diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 55de99f4298..7e3781fb2dc 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -175,8 +175,9 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// a transition carrying format version 2 is refused while decoding at every version. The /// 65,000-byte cap still applies to the contract's format version 1 bytes; the compiled /// bytes, about 1.8 times as many, are paid by the byte, and a contract read pays its -/// larger size. `DRIVE_VERSION_V10` selects `apply_contract_with_serialization` 1, which -/// writes nothing when a contract's format version 1 bytes match the stored one's. +/// larger size. `DRIVE_VERSION_V10` selects `apply_contract_with_serialization` 2, whose +/// operations (generation 1's) write nothing when a contract's format version 1 bytes +/// match the stored one's. /// Contracts stored before this version stay in their format until their next write, a /// contract update or a token configuration update, which re-stores them compiled: its /// sender pays the growth, for a token configuration update the group member whose action From 826ac2d9971d94e49218dd82f4d66985cc8b8b12 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Fri, 30 Oct 2026 15:53:13 -0500 Subject: [PATCH 41/46] test(drive-abci): record a changed fee version where fast-forwarding enters it fast_forward_to_block recorded a fee version only when the history was empty, at epoch 1, where a chain's first epoch change after genesis records it. The epoch change hook also records a fee version whose number differs from the last one recorded, at the epoch being entered. The helper now does the same, so a test that fast-forwards across a fee schedule change gets the history a chain would have. The first entry stays at epoch 1, not the target epoch, because a chain records it at its first epoch change and every epoch after that is covered by it. Co-Authored-By: Claude Opus 5.5 --- .../src/test/helpers/fast_forward_to_block.rs | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs b/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs index 99de9047241..0efc158a87a 100644 --- a/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs +++ b/packages/rs-drive-abci/src/test/helpers/fast_forward_to_block.rs @@ -32,14 +32,28 @@ pub(crate) fn fast_forward_to_block( let protocol_version = platform_state.current_protocol_version_in_consensus(); let platform_version = PlatformVersion::get(protocol_version).unwrap(); - // On a chain the epoch change hook records the fee version at the first epoch - // change after genesis, when the history is still empty. Fast-forwarding past - // genesis skips the hook, so record that entry as the hook would have: from - // protocol version 15 an empty history after the genesis epoch is an error. - if epoch_index > 0 && platform_state.previous_fee_versions().is_empty() { - platform_state - .previous_fee_versions_mut() - .insert(1, &platform_version.fee_version); + // On a chain the epoch change hook records the fee version at every epoch change + // after genesis: the first entry at the first one (epoch 1), when the history is + // still empty, and later a fee version whose number differs from the last one + // recorded, at the epoch being entered. Fast-forwarding past genesis skips the + // hook, so record what it would have: from protocol version 15 an empty history + // after the genesis epoch is an error. + if epoch_index > 0 { + let fee_version = &platform_version.fee_version; + let record_at = match platform_state.previous_fee_versions().iter().last() { + None => Some(1), + Some((_, last_fee_version)) + if last_fee_version.fee_version_number != fee_version.fee_version_number => + { + Some(epoch_index) + } + Some(_) => None, + }; + if let Some(record_at) = record_at { + platform_state + .previous_fee_versions_mut() + .insert(record_at, fee_version); + } } let block_info = BlockInfo { From b87d6a327e326d2f2939bdb5560b5b11560e43dd Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 1 Nov 2026 17:25:07 -0600 Subject: [PATCH 42/46] refactor(drive): give price-before-commit its own apply_drive_operations generation After the rebase onto order-independent batch estimation (#5688), generation 2 of apply_drive_operations carried two features: #5688's resolved estimate and this change's price-before-commit wrapper. The coding conventions give each feature its own implementation module, even inside one unreleased protocol version, as was done for apply_contract_with_serialization. Generation 2 is restored to what #5688 wrote. The new generation 3 is generation 2 with the batch priced before an owned transaction commits, and this change's tests move with it. Protocol version 15's drive table selects 3. Its behaviour is the same as before this commit, and the docs and v1's note on the shared forfeit helper now name both generations. Co-Authored-By: Claude Opus 5.5 --- .../apply_drive_operations/mod.rs | 11 +- .../apply_drive_operations/v1/mod.rs | 2 +- .../apply_drive_operations/v2/mod.rs | 268 +------ .../apply_drive_operations/v3/mod.rs | 661 ++++++++++++++++++ .../src/version/drive_versions/v10.rs | 6 +- .../rs-platform-version/src/version/v15.rs | 4 +- 6 files changed, 687 insertions(+), 265 deletions(-) create mode 100644 packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v3/mod.rs diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs index 793199433ef..5ed9912756a 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/mod.rs @@ -1,6 +1,7 @@ mod v0; mod v1; mod v2; +mod v3; use crate::util::batch::DriveOperation; @@ -73,9 +74,17 @@ impl Drive { platform_version, previous_fee_versions, ), + 3 => self.apply_drive_operations_v3( + operations, + apply, + block_info, + transaction, + platform_version, + previous_fee_versions, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "apply_drive_operations".to_string(), - known_versions: vec![0, 1, 2], + known_versions: vec![0, 1, 2, 3], received: version, })), } diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v1/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v1/mod.rs index c73feab43be..a026bab14ea 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v1/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v1/mod.rs @@ -258,7 +258,7 @@ impl Drive { /// operations free bytes: the document's, whoever paid for it, its owner or an earlier one, and /// those of any index subtree the deletion empties, whoever created it. /// -/// `pub(super)` so that generation 2 (protocol version 15) reuses it; the body is generation 1's, +/// `pub(super)` so that generations 2 and 3 (protocol version 15) reuse it; the body is generation 1's, /// unchanged, so protocol version 14 forfeits exactly as before. pub(super) fn forfeit_storage_refunds(cost_operations: &mut [LowLevelDriveOperation]) { for operation in cost_operations.iter_mut() { diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index 1828ebc80d6..d541634fbf2 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -93,27 +93,9 @@ impl Drive { /// descriptions name a shallower one (the credit shielded pool's, at level 3, for /// `ShieldedBalances` at level 4). The estimate does not depend on the order of the batch's operations, and no /// write is priced at a shallower depth than its own description. Applying a batch is - /// generation 1's, but for when an owned transaction commits. - /// - /// It also holds the owned transaction until the batch is priced. Everything generation - /// 1 does carries over: a batch that forfeits storage refunds refunds nobody, every - /// write of one identity balance, fee pot or prefunded specialized balance is merged - /// into one, a batch writing one token balance, supply or shielded pool total twice is - /// refused, a batch moving one document type's `summableOffCountIndex` counters for - /// more than one document is refused, and the debt the batch repaid - /// ([`LowLevelDriveOperation::RepaidIdentityDebt`]) is written to the processing fee - /// pool of the block's epoch after the batch applied, followed by the processing pool - /// returns the batch carries (`SystemOperationType::AddToCurrentEpochProcessingPool`, - /// unbilled), both before the owned transaction commits, so a pricing error drops the - /// pool writes with the rest. From protocol version 15 pricing an owner-attributed - /// storage removal without the fee history is an error; generation 1 committed its - /// owned transaction before pricing, so a caller passing no transaction and no history - /// had its writes persisted and the error returned. Now `Drive::calculate_fee` runs - /// first, a pricing error drops the owned transaction with everything it wrote, and the - /// finalize tasks still run after the commit. With a caller transaction nothing is - /// committed by Drive in either generation. + /// generation 1's. #[inline(always)] - pub(super) fn apply_drive_operations_v2( + pub(crate) fn apply_drive_operations_v2( &self, operations: Vec, apply: bool, @@ -285,17 +267,6 @@ impl Drive { &platform_version.drive, )?; } - // Price before committing: a pricing error drops the owned transaction with - // everything it wrote, so nothing is persisted without its fee result. - let fee_result = Drive::calculate_fee( - None, - Some(cost_operations), - &block_info.epoch, - self.config.epochs_per_era, - platform_version, - previous_fee_versions, - )?; - if let Some(owned_transaction) = owned_transaction { self.commit_transaction(owned_transaction, &platform_version.drive)?; } @@ -310,7 +281,14 @@ impl Drive { } } - Ok(fee_result) + Drive::calculate_fee( + None, + Some(cost_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + ) } } @@ -449,229 +427,3 @@ mod tests { assert_eq!(deepest_root_key_level([]), None); } } - -#[cfg(test)] -mod price_before_commit_tests { - use crate::drive::Drive; - use crate::error::drive::DriveError; - use crate::error::Error; - use crate::util::batch::drive_op_batch::GroupOperationType; - use crate::util::batch::DriveOperation; - use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; - use dpp::block::block_info::BlockInfo; - use dpp::data_contract::accessors::v0::DataContractV0Getters; - use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; - use dpp::data_contract::associated_token::token_configuration::TokenConfiguration; - use dpp::data_contract::config::v0::DataContractConfigV0; - use dpp::data_contract::config::DataContractConfig; - use dpp::data_contract::group::v0::GroupV0; - use dpp::data_contract::group::Group; - use dpp::data_contract::v1::DataContractV1; - use dpp::data_contract::DataContract; - use dpp::fee::default_costs::CachedEpochIndexFeeVersions; - use dpp::group::action_event::GroupActionEvent; - use dpp::group::group_action::v0::GroupActionV0; - use dpp::group::group_action::GroupAction; - use dpp::group::group_action_status::GroupActionStatus; - use dpp::identifier::Identifier; - use dpp::tokens::token_event::TokenEvent; - use dpp::version::fee::FeeVersion; - use dpp::version::PlatformVersion; - use std::collections::BTreeMap; - - /// A contract with one two-member group and an action the first member opened, - /// so closing the action moves signer-flagged items: the one removal a batch can - /// carry without a document fixture. - fn drive_with_open_group_action( - platform_version: &PlatformVersion, - ) -> (Drive, Identifier, Identifier, Identifier) { - let drive = setup_drive_with_initial_state_structure(Some(platform_version)); - let member_1 = Identifier::from([1; 32]); - let member_2 = Identifier::from([2; 32]); - let contract = DataContract::V1(DataContractV1 { - id: Identifier::from([3; 32]), - version: 0, - owner_id: member_1, - document_types: Default::default(), - config: DataContractConfig::V0(DataContractConfigV0 { - can_be_deleted: false, - readonly: false, - keeps_history: false, - documents_keep_history_contract_default: false, - documents_mutable_contract_default: false, - documents_can_be_deleted_contract_default: false, - requires_identity_encryption_bounded_key: None, - requires_identity_decryption_bounded_key: None, - }), - schema_defs: None, - created_at: None, - updated_at: None, - created_at_block_height: None, - updated_at_block_height: None, - created_at_epoch: None, - updated_at_epoch: None, - groups: BTreeMap::from([( - 0, - Group::V0(GroupV0 { - members: [(member_1, 1), (member_2, 2)].into(), - required_power: 3, - }), - )]), - tokens: BTreeMap::from([( - 0, - TokenConfiguration::V0(TokenConfigurationV0::default_most_restrictive()), - )]), - keywords: Vec::new(), - description: None, - }); - let contract_id = contract.id(); - drive - .insert_contract( - &contract, - BlockInfo::default(), - true, - None, - platform_version, - ) - .expect("expected to insert the contract"); - let action_id = Identifier::from([4; 32]); - let action = GroupAction::V0(GroupActionV0 { - contract_id, - proposer_id: member_1, - token_contract_position: 0, - event: GroupActionEvent::TokenEvent(TokenEvent::Mint(100, member_1, None)), - }); - drive - .add_group_action( - contract_id, - 0, - Some(action), - false, - action_id, - member_1, - 1, - &BlockInfo::default(), - true, - None, - platform_version, - ) - .expect("expected to open the action"); - (drive, contract_id, member_2, action_id) - } - - fn close_action( - contract_id: Identifier, - member_2: Identifier, - action_id: Identifier, - ) -> Vec> { - vec![DriveOperation::GroupOperation( - GroupOperationType::AddGroupAction { - contract_id, - group_contract_position: 0, - initialize_with_insert_action_info: None, - action_id, - signer_identity_id: member_2, - signer_power: 2, - closes_group_action: true, - }, - )] - } - - fn is_closed( - drive: &Drive, - contract_id: Identifier, - action_id: Identifier, - platform_version: &PlatformVersion, - ) -> bool { - drive - .fetch_action_is_closed( - contract_id, - 0, - action_id, - true, - None, - &mut vec![], - platform_version, - ) - .expect("expected to check if the action is closed") - } - - #[test] - fn should_not_commit_an_owned_transaction_when_the_batch_cannot_be_priced() { - // No caller transaction and no fee history: closing the action frees - // signer-flagged bytes, which the latest generation refuses to price. The - // batch was applied inside an owned transaction that is dropped with the - // error, so the action is still open afterwards. - let platform_version = PlatformVersion::latest(); - let (drive, contract_id, member_2, action_id) = - drive_with_open_group_action(platform_version); - - let result = drive.apply_drive_operations( - close_action(contract_id, member_2, action_id), - true, - &BlockInfo::default(), - None, - platform_version, - None, - ); - assert!( - matches!( - result, - Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) - ), - "pricing a flagged removal without the fee history must fail, got {:?}", - result - ); - assert!( - !is_closed(&drive, contract_id, action_id, platform_version), - "a batch that could not be priced must not be committed" - ); - - // The same batch with the block's history is priced and committed. - let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); - let fee_result = drive - .apply_drive_operations( - close_action(contract_id, member_2, action_id), - true, - &BlockInfo::default(), - None, - platform_version, - Some(&history), - ) - .expect("expected to close the action with the fee history"); - assert!(fee_result.fee_refunds.get(&[1; 32]).is_some()); - assert!(is_closed(&drive, contract_id, action_id, platform_version)); - let closed_signers = drive - .fetch_action_signers( - contract_id, - 0, - GroupActionStatus::ActionClosed, - action_id, - None, - platform_version, - ) - .expect("expected the closed signers"); - assert_eq!(closed_signers.len(), 2); - } - - #[test] - fn should_price_and_commit_without_history_under_the_frozen_generation() { - // Protocol version 14 selects generation 1, which commits before pricing and - // prices fee version number 1 without a history: the close persists. - let platform_version = PlatformVersion::get(14).expect("protocol version 14"); - let (drive, contract_id, member_2, action_id) = - drive_with_open_group_action(platform_version); - - drive - .apply_drive_operations( - close_action(contract_id, member_2, action_id), - true, - &BlockInfo::default(), - None, - platform_version, - None, - ) - .expect("the frozen generation prices the shipped shortcut without a history"); - assert!(is_closed(&drive, contract_id, action_id, platform_version)); - } -} diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v3/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v3/mod.rs new file mode 100644 index 00000000000..b03f95d5f86 --- /dev/null +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v3/mod.rs @@ -0,0 +1,661 @@ +use crate::util::batch::{DriveOperation, SystemOperationType}; + +use crate::drive::Drive; +use crate::error::{drive::DriveError, fee::FeeError, Error}; +use crate::fees::op::LowLevelDriveOperation; + +use dpp::block::block_info::BlockInfo; +use dpp::fee::fee_result::FeeResult; +use dpp::fee::Credits; + +use grovedb::TransactionArg; + +use dpp::version::PlatformVersion; + +use crate::drive::RootTree; +use crate::util::batch::drive_op_batch::finalize_task::{ + DriveOperationFinalizationTasks, DriveOperationFinalizeTask, +}; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::fee::default_costs::CachedEpochIndexFeeVersions; +use grovedb::batch::key_info::KeyInfo::KnownKey; +use grovedb::batch::KeyInfoPath; + +/// The level of the root tree each root key sits on, as `drive/mod.rs` draws the root tree and +/// `grovedb-structure.json` records it (`layer_shapes.root`, kept in step with a real GroveDB by +/// `structure::tests`). +const ROOT_KEY_LEVELS: [(u8, u32); 18] = [ + (RootTree::DataContractDocuments as u8, 0), + (RootTree::Identities as u8, 1), + (RootTree::Balances as u8, 1), + (RootTree::Tokens as u8, 2), + (RootTree::Pools as u8, 2), + (RootTree::WithdrawalTransactions as u8, 2), + (RootTree::Votes as u8, 2), + (RootTree::NonUniquePublicKeyKeyHashesToIdentities as u8, 3), + (RootTree::UniquePublicKeyHashesToIdentities as u8, 3), + (RootTree::PreFundedSpecializedBalances as u8, 3), + (RootTree::AddressBalances as u8, 3), + (RootTree::SpentAssetLockTransactions as u8, 3), + (RootTree::GroupActions as u8, 3), + (RootTree::Misc as u8, 3), + (RootTree::Versions as u8, 3), + (RootTree::SavedBlockTransactions as u8, 4), + (RootTree::ShieldedBalances as u8, 4), + (RootTree::ContractGroups as u8, 4), +]; + +/// The deepest level of the root tree a root key `operations` write under sits on: the key +/// opening an operation's path, or the key of an operation at the root itself. +fn deepest_root_key_level<'a>( + operations: impl IntoIterator, +) -> Option { + operations + .into_iter() + .filter_map(|operation| match operation { + LowLevelDriveOperation::GroveOperation(operation) + | LowLevelDriveOperation::EphemeralGroveOperation(operation, _) => Some(operation), + _ => None, + }) + .filter_map(|operation| match operation.path.0.first() { + Some(first) => Some(first), + None => operation.key.as_ref(), + }) + .filter_map(|root_key| match root_key { + KnownKey(key) => match key.as_slice() { + [key] => ROOT_KEY_LEVELS + .iter() + .find(|(root_key, _)| root_key == key) + .map(|(_, level)| *level), + _ => None, + }, + _ => None, + }) + .max() +} + +impl Drive { + /// Generation 3 (protocol version 15): generation 2, which resolves a batch's estimate so it + /// does not depend on the order of the batch's operations, but for when an owned + /// transaction commits. + /// + /// It holds the owned transaction until the batch is priced. Everything generation 2 does + /// carries over: the resolved estimate, and from generation 1 a batch that forfeits + /// storage refunds refunds nobody, every write of one identity balance, fee pot or + /// prefunded specialized balance is merged into one, a batch writing one token balance, + /// supply or shielded pool total twice is refused, a batch moving one document type's + /// `summableOffCountIndex` counters for more than one document is refused, and the debt + /// the batch repaid ([`LowLevelDriveOperation::RepaidIdentityDebt`]) is written to the + /// processing fee pool of the block's epoch after the batch applied, followed by the + /// processing pool returns the batch carries + /// (`SystemOperationType::AddToCurrentEpochProcessingPool`, unbilled), both before the + /// owned transaction commits, so a pricing error drops the pool writes with the rest. From + /// protocol version 15 pricing an owner-attributed storage removal without the fee history + /// is an error; generation 2 committed its owned transaction before pricing, so a caller + /// passing no transaction and no history had its writes persisted and the error returned. + /// Now `Drive::calculate_fee` runs first, a pricing error drops the owned transaction with + /// everything it wrote, and the finalize tasks still run after the commit. With a caller + /// transaction nothing is committed by Drive in either generation. + #[inline(always)] + pub(super) fn apply_drive_operations_v3( + &self, + operations: Vec, + apply: bool, + block_info: &BlockInfo, + transaction: TransactionArg, + platform_version: &PlatformVersion, + previous_fee_versions: Option<&CachedEpochIndexFeeVersions>, + ) -> Result { + DriveOperation::refuse_repeated_token_balance_writes(&operations)?; + self.refuse_repeated_counter_moves(&operations, block_info, transaction, platform_version)?; + let mut operations = DriveOperation::merge_balance_writes(operations)?; + let mut processing_pool_credits: Credits = 0; + for operation in operations.extract_if(.., |operation| { + matches!( + operation, + DriveOperation::SystemOperation( + SystemOperationType::AddToCurrentEpochProcessingPool { .. } + ) + ) + }) { + if let DriveOperation::SystemOperation( + SystemOperationType::AddToCurrentEpochProcessingPool { amount }, + ) = operation + { + processing_pool_credits = + processing_pool_credits + .checked_add(amount) + .ok_or(Error::Fee(FeeError::Overflow( + "overflow when accumulating processing pool credits", + )))?; + } + } + if operations.is_empty() && (!apply || processing_pool_credits == 0) { + return Ok(FeeResult::default()); + } + let forfeits_storage_refunds = operations + .iter() + .any(DriveOperation::forfeits_storage_refunds); + // The document operations of a moderator's deletion go in a batch of their own only + // when something else in the batch may free bytes someone is owed. + let separates_forfeited_operations = forfeits_storage_refunds + && operations + .iter() + .any(DriveOperation::refunds_moderation_storage); + // With no caller transaction, TTL preparation (direct drainage + // writes), conversion reads, and the batch apply would each commit + // on their own, so a conversion error after preparation would leave + // drained buckets committed without the write. Span all of it with + // one owned transaction and commit only once the batch applied. + let caller_transaction = transaction; + let owned_transaction = + (apply && transaction.is_none()).then(|| self.grove.start_transaction()); + let transaction = owned_transaction.as_ref().or(caller_transaction); + if apply { + self.prepare_drive_operations_time_range_ttl( + &operations, + block_info, + transaction, + platform_version, + )?; + } + let mut low_level_operations = vec![]; + let mut estimated_costs_only_with_layer_info = (!apply).then(EstimatedLayers::new); + + // The document operations of a moderator's deletion, whose removals refund nobody + let mut forfeited_low_level_operations = vec![]; + + let mut finalize_tasks: Vec = Vec::new(); + + for drive_op in operations { + if let Some(tasks) = drive_op.finalization_tasks(platform_version)? { + finalize_tasks.extend(tasks); + } + + let forfeited = separates_forfeited_operations + && matches!(drive_op, DriveOperation::DocumentOperation(_)); + let mut converted = drive_op.into_low_level_drive_operations_after_ttl_drain( + self, + &mut estimated_costs_only_with_layer_info, + block_info, + transaction, + platform_version, + )?; + if forfeited { + forfeited_low_level_operations.append(&mut converted); + } else { + low_level_operations.append(&mut converted); + } + } + + // A conversion that left no estimate behind would have the batch applied instead of + // estimated, so it is refused. + if !apply && estimated_costs_only_with_layer_info.is_none() { + return Err(Error::Drive(DriveError::CorruptedCodeExecution( + "an estimated operation leaves the estimate's layer descriptions behind", + ))); + } + if let Some(estimated_costs_only_with_layer_info) = + estimated_costs_only_with_layer_info.as_mut() + { + if let Some(level) = deepest_root_key_level( + low_level_operations + .iter() + .chain(forfeited_low_level_operations.iter()), + ) { + estimated_costs_only_with_layer_info + .keep_at_level(&KeyInfoPath::from_known_path([]), level); + } + } + let estimated_costs_only_with_layer_info = estimated_costs_only_with_layer_info + .map(|layers| layers.resolve(platform_version)) + .transpose()?; + + let repaid_identity_debt = + LowLevelDriveOperation::take_repaid_identity_debt(&mut low_level_operations)?; + + let mut cost_operations = vec![]; + + let forfeited_estimated_costs_only_with_layer_info = + if forfeited_low_level_operations.is_empty() { + None + } else { + Some(estimated_costs_only_with_layer_info.clone()) + }; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + low_level_operations, + &mut cost_operations, + &platform_version.drive, + )?; + if let Some(estimated_costs_only_with_layer_info) = + forfeited_estimated_costs_only_with_layer_info + { + let mut forfeited_cost_operations = vec![]; + self.apply_batch_low_level_drive_operations( + estimated_costs_only_with_layer_info, + transaction, + forfeited_low_level_operations, + &mut forfeited_cost_operations, + &platform_version.drive, + )?; + super::v1::forfeit_storage_refunds(&mut forfeited_cost_operations); + cost_operations.append(&mut forfeited_cost_operations); + } else if forfeits_storage_refunds { + // One batch, in which only the document operations free bytes: it forfeits whole. + super::v1::forfeit_storage_refunds(&mut cost_operations); + } + self.apply_repaid_identity_debt_to_processing_pool( + repaid_identity_debt, + &block_info.epoch, + transaction, + platform_version, + )?; + // Read the pool after ordinary writes and debt repayment so neither can overwrite + // the returned allocation. This transfer shares their transaction and is not billed. + if apply && processing_pool_credits > 0 { + let pool_operation = self.add_epoch_processing_credits_for_distribution_operation( + &block_info.epoch, + processing_pool_credits, + transaction, + platform_version, + )?; + self.apply_batch_low_level_drive_operations( + None, + transaction, + vec![pool_operation], + &mut vec![], + &platform_version.drive, + )?; + } + // Price before committing: a pricing error drops the owned transaction with + // everything it wrote, so nothing is persisted without its fee result. + let fee_result = Drive::calculate_fee( + None, + Some(cost_operations), + &block_info.epoch, + self.config.epochs_per_era, + platform_version, + previous_fee_versions, + )?; + + if let Some(owned_transaction) = owned_transaction { + self.commit_transaction(owned_transaction, &platform_version.drive)?; + } + + // Execute drive operation callbacks after updating state. Nothing was written when + // only estimating, so there is nothing to finalize. The tasks read through the + // caller's transaction; an owned one was committed just above, and `caller_transaction` + // is `None` exactly then, so they read committed state. + if apply { + for task in finalize_tasks { + task.execute(self, caller_transaction, platform_version)?; + } + } + + Ok(fee_result) + } +} + +#[cfg(test)] +mod tests { + use super::super::tests::{deep, estimate, mint, shallow, system_credits}; + use super::{deepest_root_key_level, ROOT_KEY_LEVELS}; + use crate::drive::Drive; + use crate::fees::op::LowLevelDriveOperation; + use crate::util::batch::DriveOperation; + use crate::util::estimated_layers::EstimatedLayers; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::version::PlatformVersion; + use grovedb::batch::KeyInfoPath; + use grovedb::Element; + + /// The root layer a single operation's estimate is priced with, lowered into an estimate and + /// resolved as generation 2 resolves a batch's. + fn root_level(drive: &Drive, operation: DriveOperation<'static>) -> u32 { + let platform_version = PlatformVersion::latest(); + let mut estimate = Some(EstimatedLayers::new()); + operation + .into_low_level_drive_operations_after_ttl_drain( + drive, + &mut estimate, + &BlockInfo::default(), + None, + platform_version, + ) + .expect("expected to lower the operation"); + estimate + .expect("expected the estimate") + .resolve(platform_version) + .expect("expected to resolve the estimate") + .get(&KeyInfoPath::from_known_path([])) + .expect("expected the root to be described") + .estimated_layer_count + .estimate_levels() + } + + /// One token mint describes the root twice: its supply estimator at level 3, then its + /// balance estimator at level 2. The resolved estimate prices the mint's root at the + /// supply's level 3. + #[test] + fn should_price_a_composite_operation_by_every_description_of_its_writes() { + let drive = setup_drive_with_initial_state_structure(Some(PlatformVersion::latest())); + assert_eq!(root_level(&drive, mint()), 3); + } + + /// A token mint (whose supply describes the root at depth 3) and a prefunded balance write + /// (the root at depth 3 too, with other key sizes and sum trees): the batch's estimate keeps + /// the nodes GroveDB prices dearer, whichever comes first. + #[test] + fn should_estimate_two_descriptions_of_one_depth_alike_in_either_order() { + let latest = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(latest)); + assert_eq!( + estimate(&drive, vec![mint(), deep()], latest).expect("expected an estimate"), + estimate(&drive, vec![deep(), mint()], latest).expect("expected an estimate") + ); + } + + /// A system credits update and a token mint both describe `Misc`, the mint as holding the + /// token supplies tree, whose new root key GroveDB prices with the subtree sizes: the batch + /// is estimated alike whichever comes first. + #[test] + fn should_estimate_a_layer_holding_items_and_subtrees_in_either_order() { + let latest = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(latest)); + assert_eq!( + estimate(&drive, vec![system_credits(), mint()], latest).expect("expected an estimate"), + estimate(&drive, vec![mint(), system_credits()], latest).expect("expected an estimate") + ); + } + + /// A batch's estimate does not depend on the order of its operations: built either way, the + /// shallow and the deep write are estimated alike, at the deeper root. + #[test] + fn should_estimate_a_batch_alike_in_either_order() { + let latest = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(latest)); + assert_eq!( + estimate(&drive, vec![shallow(), deep()], latest).expect("expected an estimate"), + estimate(&drive, vec![deep(), shallow()], latest).expect("expected an estimate") + ); + } + + /// The levels are the recorded root shape's, every root key included. + #[test] + fn should_take_root_levels_from_the_recorded_root_shape() { + fn walk(node: &serde_json::Value, level: u32, levels: &mut Vec<(u8, u32)>) { + if let Some(hex) = node["hex"].as_str() { + let key = u8::from_str_radix(hex, 16).expect("expected a one-byte root key"); + levels.push((key, level)); + walk(&node["left"], level + 1, levels); + walk(&node["right"], level + 1, levels); + } + } + let structure = std::fs::read_to_string(concat!( + env!("CARGO_MANIFEST_DIR"), + "/grovedb-structure.json" + )) + .expect("expected the recorded structure"); + let structure: serde_json::Value = + serde_json::from_str(&structure).expect("expected valid json"); + let mut recorded = vec![]; + walk(&structure["layer_shapes"]["root"]["tree"], 0, &mut recorded); + recorded.sort_unstable(); + let mut levels = ROOT_KEY_LEVELS.to_vec(); + levels.sort_unstable(); + assert_eq!(levels, recorded); + } + + /// The deepest root key a batch writes under: the key opening an operation's path, or the + /// key of an operation at the root itself; a path under an unknown key counts for nothing. + #[test] + fn should_find_the_deepest_root_key_the_operations_write_under() { + let at = |path: Vec>, key: Vec| { + LowLevelDriveOperation::insert_for_known_path_key_element( + path, + key, + Element::new_item(vec![1]), + ) + }; + let documents = at(vec![vec![64], vec![1]], vec![2]); + let shielded = at(vec![vec![52], vec![1]], vec![2]); + let root_misc = at(vec![], vec![104]); + assert_eq!(deepest_root_key_level([&documents]), Some(0)); + assert_eq!(deepest_root_key_level([&root_misc]), Some(3)); + assert_eq!( + deepest_root_key_level([&documents, &shielded, &root_misc]), + Some(4) + ); + assert_eq!(deepest_root_key_level([&at(vec![vec![7]], vec![1])]), None); + assert_eq!(deepest_root_key_level([]), None); + } +} + +#[cfg(test)] +mod price_before_commit_tests { + use crate::drive::Drive; + use crate::error::drive::DriveError; + use crate::error::Error; + use crate::util::batch::drive_op_batch::GroupOperationType; + use crate::util::batch::DriveOperation; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::data_contract::accessors::v0::DataContractV0Getters; + use dpp::data_contract::associated_token::token_configuration::v0::TokenConfigurationV0; + use dpp::data_contract::associated_token::token_configuration::TokenConfiguration; + use dpp::data_contract::config::v0::DataContractConfigV0; + use dpp::data_contract::config::DataContractConfig; + use dpp::data_contract::group::v0::GroupV0; + use dpp::data_contract::group::Group; + use dpp::data_contract::v1::DataContractV1; + use dpp::data_contract::DataContract; + use dpp::fee::default_costs::CachedEpochIndexFeeVersions; + use dpp::group::action_event::GroupActionEvent; + use dpp::group::group_action::v0::GroupActionV0; + use dpp::group::group_action::GroupAction; + use dpp::group::group_action_status::GroupActionStatus; + use dpp::identifier::Identifier; + use dpp::tokens::token_event::TokenEvent; + use dpp::version::fee::FeeVersion; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + /// A contract with one two-member group and an action the first member opened, + /// so closing the action moves signer-flagged items: the one removal a batch can + /// carry without a document fixture. + fn drive_with_open_group_action( + platform_version: &PlatformVersion, + ) -> (Drive, Identifier, Identifier, Identifier) { + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let member_1 = Identifier::from([1; 32]); + let member_2 = Identifier::from([2; 32]); + let contract = DataContract::V1(DataContractV1 { + id: Identifier::from([3; 32]), + version: 0, + owner_id: member_1, + document_types: Default::default(), + config: DataContractConfig::V0(DataContractConfigV0 { + can_be_deleted: false, + readonly: false, + keeps_history: false, + documents_keep_history_contract_default: false, + documents_mutable_contract_default: false, + documents_can_be_deleted_contract_default: false, + requires_identity_encryption_bounded_key: None, + requires_identity_decryption_bounded_key: None, + }), + schema_defs: None, + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + groups: BTreeMap::from([( + 0, + Group::V0(GroupV0 { + members: [(member_1, 1), (member_2, 2)].into(), + required_power: 3, + }), + )]), + tokens: BTreeMap::from([( + 0, + TokenConfiguration::V0(TokenConfigurationV0::default_most_restrictive()), + )]), + keywords: Vec::new(), + description: None, + }); + let contract_id = contract.id(); + drive + .insert_contract( + &contract, + BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to insert the contract"); + let action_id = Identifier::from([4; 32]); + let action = GroupAction::V0(GroupActionV0 { + contract_id, + proposer_id: member_1, + token_contract_position: 0, + event: GroupActionEvent::TokenEvent(TokenEvent::Mint(100, member_1, None)), + }); + drive + .add_group_action( + contract_id, + 0, + Some(action), + false, + action_id, + member_1, + 1, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to open the action"); + (drive, contract_id, member_2, action_id) + } + + fn close_action( + contract_id: Identifier, + member_2: Identifier, + action_id: Identifier, + ) -> Vec> { + vec![DriveOperation::GroupOperation( + GroupOperationType::AddGroupAction { + contract_id, + group_contract_position: 0, + initialize_with_insert_action_info: None, + action_id, + signer_identity_id: member_2, + signer_power: 2, + closes_group_action: true, + }, + )] + } + + fn is_closed( + drive: &Drive, + contract_id: Identifier, + action_id: Identifier, + platform_version: &PlatformVersion, + ) -> bool { + drive + .fetch_action_is_closed( + contract_id, + 0, + action_id, + true, + None, + &mut vec![], + platform_version, + ) + .expect("expected to check if the action is closed") + } + + #[test] + fn should_not_commit_an_owned_transaction_when_the_batch_cannot_be_priced() { + // No caller transaction and no fee history: closing the action frees + // signer-flagged bytes, which the latest generation refuses to price. The + // batch was applied inside an owned transaction that is dropped with the + // error, so the action is still open afterwards. + let platform_version = PlatformVersion::latest(); + let (drive, contract_id, member_2, action_id) = + drive_with_open_group_action(platform_version); + + let result = drive.apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + None, + ); + assert!( + matches!( + result, + Err(Error::Drive(DriveError::CorruptedCodeExecution(_))) + ), + "pricing a flagged removal without the fee history must fail, got {:?}", + result + ); + assert!( + !is_closed(&drive, contract_id, action_id, platform_version), + "a batch that could not be priced must not be committed" + ); + + // The same batch with the block's history is priced and committed. + let history: CachedEpochIndexFeeVersions = BTreeMap::from([(0, FeeVersion::first())]); + let fee_result = drive + .apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + Some(&history), + ) + .expect("expected to close the action with the fee history"); + assert!(fee_result.fee_refunds.get(&[1; 32]).is_some()); + assert!(is_closed(&drive, contract_id, action_id, platform_version)); + let closed_signers = drive + .fetch_action_signers( + contract_id, + 0, + GroupActionStatus::ActionClosed, + action_id, + None, + platform_version, + ) + .expect("expected the closed signers"); + assert_eq!(closed_signers.len(), 2); + } + + #[test] + fn should_price_and_commit_without_history_under_the_frozen_generation() { + // Protocol version 14 selects generation 1, which commits before pricing and + // prices fee version number 1 without a history: the close persists. + let platform_version = PlatformVersion::get(14).expect("protocol version 14"); + let (drive, contract_id, member_2, action_id) = + drive_with_open_group_action(platform_version); + + drive + .apply_drive_operations( + close_action(contract_id, member_2, action_id), + true, + &BlockInfo::default(), + None, + platform_version, + None, + ) + .expect("the frozen generation prices the shipped shortcut without a history"); + assert!(is_closed(&drive, contract_id, action_id, platform_version)); + } +} diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 9076304bdf0..abf49e4d73d 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -40,7 +40,7 @@ use crate::version::drive_versions::{ /// `DRIVE_VERIFY_METHOD_VERSIONS_V4`, are `Some(0)` (`None` before): a page of the store /// is read, proved and verified. /// * **A batch's fee estimate is resolved** (protocol v15 note 14): `apply_drive_operations` 2 -/// resolves an estimated batch's layer descriptions once the whole batch is lowered, the root +/// (and generation 3, which this table selects and which keeps it) resolves an estimated batch's layer descriptions once the whole batch is lowered, the root /// also kept at the level of the deepest root key the batch writes under, so the estimate no /// longer depends on the order of the batch's operations. `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` /// also sets `remove_reference_for_index_level_for_contract_operations` to 2 and @@ -60,7 +60,7 @@ use crate::version::drive_versions::{ /// block lifecycle paths use to credit each recorded owner of a refund /// and report the amount whose owner has no balance element. /// * **Price before commit** — `batch_operations.apply_drive_operations` -/// 1 -> 2, `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` +/// 3 (generation 2 made price-first), `DRIVE_GROUP_METHOD_VERSIONS_V2` (`insert.add_group_action` /// 0 -> 1), `DRIVE_CONTRACT_METHOD_VERSIONS_V5` (`update_contract` 2 -> 3, /// `apply_contract_with_serialization` 0 -> 2 over generation 1's /// operations, the five moderation @@ -81,7 +81,7 @@ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read batch_operations: DriveBatchOperationsMethodVersion { - apply_drive_operations: 2, // changed in v10: a batch's estimate is resolved (note 14); the batch is priced before an owned transaction commits + apply_drive_operations: 3, // changed in v10: generation 2 resolves a batch's estimate (note 14); generation 3, selected here, also prices the batch before an owned transaction commits ..DRIVE_VERSION_V9.methods.batch_operations }, platform_system: DrivePlatformSystemMethodVersions { diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 7e3781fb2dc..f50572f1796 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -303,7 +303,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// `Some(0)` in `DRIVE_VERIFY_METHOD_VERSIONS_V4`, `None` before). /// /// 14. **A batch's fee estimate no longer depends on the order of its operations**: -/// `apply_drive_operations` 2 (`DRIVE_VERSION_V10`) resolves an estimated batch's layer +/// `apply_drive_operations` 2, and generation 3 that `DRIVE_VERSION_V10` selects, resolve an estimated batch's layer /// descriptions once the whole batch is lowered (`EstimatedLayers::resolve`). Each layer a /// batch's writes describe more than one way, the nested estimators of one operation /// included, is described by the costliest of them (`costliest_layer`): the deepest depth any @@ -368,7 +368,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// processing pool. /// /// 17. **Pricing before commit**: the Drive entry points that own their -/// transaction when a caller passes none (`apply_drive_operations` v2, +/// transaction when a caller passes none (`apply_drive_operations` v3, /// the ten document wrappers v1, `update_contract` v3, /// `apply_contract_with_serialization` v2, `add_group_action` v1, the /// moderation writers v1) price the batch before committing it, so the From 1934b4fe00e85d97f5421b505da5d98c4d36f262 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 1 Nov 2026 18:31:15 -0600 Subject: [PATCH 43/46] docs(platform): drop a stale generation from protocol 15's drive comment The drive slot comment on PLATFORM_V15 named apply_contract_with_serialization 2 and then repeated the compiled storage clause as generation 1. It also said apply_drive_operations 2, though protocol 15 now selects generation 3, which resolves the estimate as generation 2 does and prices the batch before an owned transaction commits. The duplicate clause is removed and the batch generation corrected. Co-Authored-By: Claude Opus 5.5 --- packages/rs-platform-version/src/version/v15.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index f50572f1796..8928a195fff 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -378,7 +378,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// Everything else matches v14. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, - drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 2 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8) and prices before it commits; calculate_fee v1 (fee history required for refunds) + identity table v3 (credit_storage_refunds_to_owners); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store (note 13); apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) + drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 2 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8) and prices before it commits; calculate_fee v1 (fee history required for refunds) + identity table v3 (credit_storage_refunds_to_owners); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store (note 13); apply_drive_operations 3 resolves a batch's estimate as generation 2 does and prices it before an owned transaction commits, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) drive_abci: DriveAbciVersion { structs: DRIVE_ABCI_STRUCTURE_VERSIONS_V2, methods: DRIVE_ABCI_METHOD_VERSIONS_V11, // changed: the yes/no poll's tally, record, clean-up and finish hook are Some(0) (note 10); verify_chain_lock_locally 1 rejects a chain lock locally only when Platform holds the quorums active at its signing height, and verify_chain_lock_through_core 1 has Core's verifychainlock check the others before submitchainlock From a481ffd69224db911dc38c9799e70508b6cdfb86 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 1 Nov 2026 18:58:12 -0600 Subject: [PATCH 44/46] test(drive): seed debt in the positive-balance refund credit test should_not_touch_debt_when_the_owner_has_a_positive_balance promised that an owner in debt while holding a balance keeps its debt, but its owner had no debt, so the test could not tell preserving it from repaying it. The owner now carries outstanding negative credit beside its balance, and the test asserts the debt is unchanged after the refund, alongside the full refund reaching the balance and nothing reported as repaid. Co-Authored-By: Claude Opus 5.5 --- .../v0/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs index a58dd1506a8..91f06d40f78 100644 --- a/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/identity/update/methods/credit_storage_refunds_to_owners_operations/v0/mod.rs @@ -712,6 +712,15 @@ mod tests { let owner = insert_identity(&drive, 9, &transaction, platform_version); let owner_id = owner.id().to_buffer(); + // Outstanding debt beside the positive balance, so repaying it by + // mistake would show in the balance, the outcome and the debt. + let outstanding_debt = 75; + apply( + &drive, + vec![drive.update_identity_negative_credit_operation_v0(owner_id, outstanding_debt)], + &transaction, + platform_version, + ); let fee_refunds = refunds(&[(owner_id, &[(0, 250)])]); let mut operations = vec![]; @@ -732,6 +741,11 @@ mod tests { balance(&drive, owner_id, &transaction, platform_version), Some(IDENTITY_BALANCE + 250) ); + assert_eq!( + debt(&drive, owner_id, &transaction, platform_version), + Some(outstanding_debt), + "the debt beside a positive balance is left as it was" + ); } #[test] From 81731a08f77d2a6fa71b9c430e13b9e436fcf962 Mon Sep 17 00:00:00 2001 From: DCG-Claude Date: Sun, 1 Nov 2026 19:35:21 -0600 Subject: [PATCH 45/46] docs(drive): record the apply contract wrapper's double-counted read cost The apply_contract_with_serialization wrapper pushes the builder's read costs after apply_batch_low_level_drive_operations has already appended them, so its returned processing fee prices the reads twice. Every generation since 0 does this, and no production caller is charged the fee. Generation 2 keeps it because it changes only when it commits. A comment at the push now records the defect and leaves the fix to a later generation of the wrapper. Co-Authored-By: Claude Opus 5.5 --- .../apply/apply_contract_with_serialization/v2/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs index e26aff17b45..c19bfe85f6d 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs @@ -59,6 +59,14 @@ impl Drive { &mut cost_operations, &platform_version.drive, )?; + // Known and kept as generation 1 does it: `apply_batch_low_level_drive_operations` has + // already appended the builder's read costs to `cost_operations`, so this push counts + // them a second time and the returned processing fee prices the reads twice. Every + // generation since 0 does this, and no production caller is charged the fee: genesis and + // the protocol upgrade's system contract re-stores discard it, and state transitions + // apply contracts through `apply_drive_operations`, which prices the builder once. This + // generation changes only when it commits, so dropping the push is left to a later + // generation of this wrapper. cost_operations.push(CalculatedCostOperation(fetch_cost)); // A pricing error drops the owned transaction with everything it wrote. From 851dffb4fb413208b62d8649ddd1653e82f7736f Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 3 Nov 2026 22:20:11 +0700 Subject: [PATCH 46/46] fix(drive): price an applied contract's lookup once in the protocol 15 wrapper `apply_batch_low_level_drive_operations` already hands back the builder's read costs with the batch's, so generation 2 of `apply_contract_with_serialization` no longer pushes their sum a second time. Generation 2 is new on this branch and only protocol version 15 (unreleased) selects it; generations 0 and 1 keep their arithmetic. No production caller is charged this fee (genesis and the protocol upgrades' system contract re-stores discard it, and state transitions apply contracts through `apply_drive_operations`), so no consensus result changes. The new test reapplies an unchanged contract and checks the fee equals its lookup's, and that generation 1 prices the lookup twice. Co-Authored-By: Claude Opus 5.5 --- .../apply_contract_with_serialization/mod.rs | 73 +++++++++++++++++++ .../v2/mod.rs | 41 +++++------ 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs index d71b5cf8244..b20343a1098 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/mod.rs @@ -185,7 +185,9 @@ impl Drive { #[cfg(test)] mod tests { use crate::drive::contract::paths::{contract_keeping_history_root_path, contract_root_path}; + use crate::drive::contract::serialize_contract_for_storage; use crate::drive::Drive; + use crate::fees::op::LowLevelDriveOperation; use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; use dpp::block::block_info::BlockInfo; use dpp::data_contract::accessors::v0::{DataContractV0Getters, DataContractV0Setters}; @@ -296,4 +298,75 @@ mod tests { apply_at(&drive, &contract, 0, PlatformVersion::latest()); assert_eq!(stored_bytes(&drive, &contract)[0], 2); } + + #[test] + fn should_price_the_lookup_of_an_unchanged_contract_once_from_protocol_version_15() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(None); + let contract = fixture_contract(platform_version); + apply_at(&drive, &contract, 0, platform_version); + let stored = stored_bytes(&drive, &contract); + let serialization = serialize_contract_for_storage(&contract, platform_version) + .expect("expected to serialize the contract"); + let block_info = BlockInfo::default(); + + // Applying an unchanged contract only looks it up + let lookup = drive + .apply_contract_with_serialization_operations( + &contract, + serialization.clone(), + &block_info, + &mut None, + None, + None, + platform_version, + ) + .expect("expected the operations"); + assert!(!lookup.is_empty()); + assert!(lookup.iter().all(|operation| matches!( + operation, + LowLevelDriveOperation::CalculatedCostOperation(_) + ))); + let lookup_fee = Drive::calculate_fee( + None, + Some(lookup), + &block_info.epoch, + drive.config.epochs_per_era, + platform_version, + None, + ) + .expect("expected to price the lookup"); + assert!(lookup_fee.processing_fee > 0); + + let fee = drive + .apply_contract_with_serialization( + &contract, + serialization.clone(), + block_info, + true, + None, + None, + platform_version, + ) + .expect("expected to apply the contract"); + assert_eq!(fee, lookup_fee); + assert_eq!(stored_bytes(&drive, &contract), stored); + + // Generation 1 pushed the lookup's cost again after applying the batch + let generation_1_fee = drive + .apply_contract_with_serialization_v1( + &contract, + serialization, + block_info, + true, + None, + None, + platform_version, + ) + .expect("expected to apply the contract"); + assert_eq!( + generation_1_fee.processing_fee, + 2 * lookup_fee.processing_fee + ); + } } diff --git a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs index c19bfe85f6d..3b6195a153c 100644 --- a/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs +++ b/packages/rs-drive/src/drive/contract/apply/apply_contract_with_serialization/v2/mod.rs @@ -1,7 +1,5 @@ use crate::drive::Drive; use crate::error::Error; -use crate::fees::op::LowLevelDriveOperation; -use crate::fees::op::LowLevelDriveOperation::CalculatedCostOperation; use crate::util::estimated_layers::EstimatedLayers; use crate::util::storage_flags::StorageFlags; use dpp::block::block_info::BlockInfo; @@ -15,16 +13,24 @@ impl Drive { /// Applies a contract and returns the fee for applying, pricing it before anything is /// committed. /// - /// Generation 2 differs from generation 1 in one thing, when it commits: when the caller - /// supplies no transaction and the operations are applied, the write and its pricing share - /// one owned transaction that is committed only after `Drive::calculate_fee` succeeded. The - /// operations are generation 1's ([`Self::apply_contract_with_serialization_operations_v1`], - /// the compiled-storage builder). Generation 1 applied the batch (committing it on its own - /// without a caller transaction) and priced it afterwards, so from protocol version 15, - /// where pricing an owner-attributed storage removal without the fee history is an error, a - /// call passing no history would have persisted the write and then failed. It now fails - /// before anything is written. With a caller transaction nothing is committed by Drive in - /// either generation. + /// Generation 2 differs from generation 1 in when it commits and in how often it prices the + /// contract's lookup. The operations are generation 1's + /// ([`Self::apply_contract_with_serialization_operations_v1`], the compiled-storage builder). + /// + /// * When the caller supplies no transaction and the operations are applied, the write and + /// its pricing share one owned transaction that is committed only after + /// `Drive::calculate_fee` succeeded. Generation 1 applied the batch (committing it on its + /// own without a caller transaction) and priced it afterwards, so from protocol version + /// 15, where pricing an owner-attributed storage removal without the fee history is an + /// error, a call passing no history would have persisted the write and then failed. It now + /// fails before anything is written. With a caller transaction nothing is committed by + /// Drive in either generation. + /// * The builder's reads of the stored contract are priced once, as + /// `apply_batch_low_level_drive_operations` hands them back with the batch's costs. + /// Generations 0 and 1 also pushed their sum again, so they price the lookup twice. No + /// production caller is charged this fee: genesis and the protocol upgrades' system + /// contract re-stores discard it, and state transitions apply contracts through + /// `apply_drive_operations`. #[inline(always)] #[allow(clippy::too_many_arguments)] pub(super) fn apply_contract_with_serialization_v2( @@ -51,7 +57,6 @@ impl Drive { transaction, platform_version, )?; - let fetch_cost = LowLevelDriveOperation::combine_cost_operations(&batch_operations); self.apply_batch_low_level_drive_operations( estimated_costs_only_with_layer_info, transaction, @@ -59,16 +64,8 @@ impl Drive { &mut cost_operations, &platform_version.drive, )?; - // Known and kept as generation 1 does it: `apply_batch_low_level_drive_operations` has - // already appended the builder's read costs to `cost_operations`, so this push counts - // them a second time and the returned processing fee prices the reads twice. Every - // generation since 0 does this, and no production caller is charged the fee: genesis and - // the protocol upgrade's system contract re-stores discard it, and state transitions - // apply contracts through `apply_drive_operations`, which prices the builder once. This - // generation changes only when it commits, so dropping the push is left to a later - // generation of this wrapper. - cost_operations.push(CalculatedCostOperation(fetch_cost)); + // The batch's costs already hold the builder's reads, so nothing is added to them. // A pricing error drops the owned transaction with everything it wrote. let fees = Drive::calculate_fee( None,