Repository navigation
fix(platform)!: refuse a document write whose sum would overflow as a paid error #5943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v5.1-dev
Are you sure you want to change the base?
Changes from all commits
e4dd8e3
9a537c4
e108f0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use crate::consensus::state::state_error::StateError; | ||
| use crate::consensus::ConsensusError; | ||
| use crate::ProtocolError; | ||
| use bincode::{Decode, DecodeUntrusted, Encode}; | ||
| use platform_serialization_derive::{ | ||
| PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, | ||
| }; | ||
| use platform_value::Identifier; | ||
| use thiserror::Error; | ||
|
|
||
| /// A document write to a data contract would take one of the sums its document types keep (an | ||
| /// index `summable` by a property, a `rangeSummable` index, or `documentsSummable`) past what a | ||
| /// sum holds, a signed 64 bit integer. The write is refused as a paid error from protocol | ||
| /// version 15: the identity contract nonce is bumped and nothing else of the write is stored. | ||
| #[derive( | ||
| Error, | ||
| Debug, | ||
| Clone, | ||
| PartialEq, | ||
| Eq, | ||
| Encode, | ||
| Decode, | ||
| PlatformSerialize, | ||
| PlatformDeserializeTrusted, | ||
| PlatformDeserializeUntrusted, | ||
| DecodeUntrusted, | ||
| )] | ||
| #[error("a document write to contract {contract_id} would take a document sum past the largest value a sum holds")] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💬 Nitpick: Describe underflow in the consensus error message The same aggregate failure is raised when a signed sum falls below source: |
||
| #[platform_serialize(unversioned)] | ||
| pub struct DocumentSumOverflowError { | ||
| /* | ||
|
|
||
| DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION | ||
|
|
||
| */ | ||
| contract_id: Identifier, | ||
| } | ||
|
|
||
| impl DocumentSumOverflowError { | ||
| pub fn new(contract_id: Identifier) -> Self { | ||
| Self { contract_id } | ||
| } | ||
|
|
||
| pub fn contract_id(&self) -> &Identifier { | ||
| &self.contract_id | ||
| } | ||
| } | ||
|
|
||
| impl From<DocumentSumOverflowError> for ConsensusError { | ||
| fn from(err: DocumentSumOverflowError) -> Self { | ||
| Self::StateError(StateError::DocumentSumOverflowError(err)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,14 @@ use crate::rpc::core::CoreRPCLike; | |
| use dpp::address_funds::PlatformAddress; | ||
| use dpp::balances::credits::CreditOperation; | ||
| use dpp::block::block_info::BlockInfo; | ||
| use dpp::consensus::state::document::document_sum_overflow_error::DocumentSumOverflowError; | ||
| use dpp::consensus::ConsensusError; | ||
| use dpp::fee::Credits; | ||
| use dpp::platform_value::Identifier; | ||
| use dpp::version::PlatformVersion; | ||
| use drive::grovedb::Error as GroveError; | ||
| use drive::grovedb::Transaction; | ||
| use drive::util::batch::{ContractModerationOperationType, DriveOperation, IdentityOperationType}; | ||
| use std::collections::BTreeMap; | ||
|
|
||
| impl<C> Platform<C> | ||
|
|
@@ -18,6 +22,22 @@ where | |
| { | ||
| /// Generation 1, except that a gas sponsor who pays is named as the owner of the storage | ||
| /// the batch's documents write, so the refunds of that storage go back to them. | ||
| /// | ||
| /// A document write whose operations would take a document sum past what a sum tree holds | ||
| /// (GroveDB refuses to apply them) is refused as a paid consensus error, | ||
| /// [`DocumentSumOverflowError`]: everything the attempt wrote is rolled back, and the event | ||
| /// is executed again as the nonce bump a refused state validation of the batch leaves | ||
| /// ([`SumOverflowRefusal`]). The signer pays for the bump, the work validation billed, and | ||
| /// the processing of the refused write as fee validation estimates it; no storage. Whether a | ||
| /// write overflows is a function of the state it is applied to, so every node refuses the | ||
| /// same writes. | ||
| /// | ||
| /// The rollback needs a savepoint on the block transaction, which stays on the transaction | ||
| /// when the write applies (RocksDB pops a savepoint only by rolling back to it). The genesis | ||
| /// height rewinds each re-proposal of its block to the one savepoint `init_chain` set, so no | ||
| /// savepoint is set there and a write overflowing a sum at the genesis height stays the | ||
| /// internal error it was. At every other height a savepoint left on the transaction is inert: | ||
| /// the block's transaction is committed or dropped as a whole. | ||
| #[inline(always)] | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub(super) fn execute_event_v2( | ||
|
|
@@ -26,19 +46,171 @@ where | |
| consensus_errors: Vec<ConsensusError>, | ||
| block_info: &BlockInfo, | ||
| transaction: &Transaction, | ||
| address_balances_in_update: Option<&mut BTreeMap<PlatformAddress, CreditOperation>>, | ||
| mut address_balances_in_update: Option<&mut BTreeMap<PlatformAddress, CreditOperation>>, | ||
| block_credit_mints: &mut Credits, | ||
| platform_version: &PlatformVersion, | ||
| ) -> Result<EventExecutionResult, Error> { | ||
| self.execute_event_v1_body( | ||
| // An event that already carries consensus errors is the nonce bump of a refused batch, | ||
| // which writes no document. | ||
| let refusal = if consensus_errors.is_empty() | ||
| && block_info.height != self.config.abci.genesis_height | ||
| { | ||
| SumOverflowRefusal::of(&event) | ||
| } else { | ||
| None | ||
|
Comment on lines
+53
to
+60
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Blocking: Partially invalid batches bypass paid overflow handling
source: |
||
| }; | ||
| let Some(refusal) = refusal else { | ||
| return self.execute_event_v1_body( | ||
| event, | ||
| consensus_errors, | ||
| block_info, | ||
| transaction, | ||
| address_balances_in_update, | ||
| block_credit_mints, | ||
| true, | ||
| platform_version, | ||
| ); | ||
| }; | ||
|
|
||
| transaction.set_savepoint(); | ||
| match self.execute_event_v1_body( | ||
| event, | ||
| consensus_errors, | ||
| block_info, | ||
| transaction, | ||
| address_balances_in_update, | ||
| address_balances_in_update.as_deref_mut(), | ||
| block_credit_mints, | ||
| true, | ||
| platform_version, | ||
| ) | ||
| ) { | ||
| Err(Error::Drive(error)) if error.is_sum_tree_overflow() => { | ||
| // Applying a write may take several GroveDB batches and direct writes (the | ||
| // clean-up of expired time range buckets before them), and only the refused | ||
| // batch is undone by GroveDB. Rolling back to the savepoint undoes the rest, so | ||
| // the state is as if the write had never been attempted. The fee and the | ||
| // balance changes are written only after the operations applied. | ||
| transaction | ||
| .rollback_to_savepoint() | ||
|
Comment on lines
+86
to
+93
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Blocking: Overflow detection is not scoped to the document write The guard converts any source: |
||
| .map_err(|error| Error::Drive(GroveError::StorageError(error).into()))?; | ||
| let SumOverflowRefusal { | ||
| event: refusal_event, | ||
| attempted_operations, | ||
| contract_id, | ||
| } = refusal; | ||
| let attempted_processing_fee = self | ||
| .drive | ||
| .apply_drive_operations( | ||
| attempted_operations, | ||
| false, | ||
| block_info, | ||
| Some(transaction), | ||
| platform_version, | ||
| ) | ||
| .map_err(Error::Drive)? | ||
| .processing_fee; | ||
| self.execute_event_v1_body( | ||
| refusal_event.with_additional_fixed_fee_cost(attempted_processing_fee), | ||
| vec![DocumentSumOverflowError::new(contract_id).into()], | ||
|
Comment on lines
+100
to
+113
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Blocking: The overflow retry misclassifies processing fees as fixed costs
source: |
||
| block_info, | ||
| transaction, | ||
| address_balances_in_update, | ||
| block_credit_mints, | ||
| true, | ||
| platform_version, | ||
| ) | ||
| } | ||
| result => result, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// What an identity-paid document write is executed as when its operations would take a | ||
| /// document sum past what a sum tree holds. | ||
| struct SumOverflowRefusal<'a> { | ||
| /// The write's event with only the operations a refused state validation of the batch | ||
| /// leaves: the identity contract nonce bumps and the removal of a suspension that lapsed. | ||
| /// Nothing else of the write runs, so it moves no balance, owes no action fee and is never | ||
| /// sponsored, like any batch that failed. | ||
| event: ExecutionEvent<'a>, | ||
| /// The operations of the refused write, which price the processing it cost. | ||
| attempted_operations: Vec<DriveOperation<'a>>, | ||
| /// The contract the write is to. | ||
| contract_id: Identifier, | ||
| } | ||
|
|
||
| impl<'a> SumOverflowRefusal<'a> { | ||
| /// The refusal of `event`, when it is an identity-paid write of documents to a contract. | ||
| fn of(event: &ExecutionEvent<'a>) -> Option<Self> { | ||
| let ExecutionEvent::Paid { | ||
| identity, | ||
| operations, | ||
| execution_operations, | ||
| user_fee_increase, | ||
| signing_key_limits, | ||
| .. | ||
| } = event | ||
| else { | ||
| return None; | ||
| }; | ||
| if !operations | ||
| .iter() | ||
| .any(|operation| matches!(operation, DriveOperation::DocumentOperation(_))) | ||
| { | ||
| return None; | ||
| } | ||
| let contract_id = operations.iter().find_map(|operation| match operation { | ||
| DriveOperation::IdentityOperation( | ||
| IdentityOperationType::UpdateIdentityContractNonce { contract_id, .. }, | ||
| ) => Some(Identifier::new(*contract_id)), | ||
| _ => None, | ||
| })?; | ||
|
Comment on lines
+161
to
+166
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: The overflow error can name the wrong contract
source: |
||
| let refused_operations = operations | ||
| .iter() | ||
| .filter(|operation| { | ||
| matches!( | ||
| operation, | ||
| DriveOperation::IdentityOperation( | ||
| IdentityOperationType::UpdateIdentityContractNonce { .. } | ||
| ) | DriveOperation::ContractModerationOperation( | ||
| ContractModerationOperationType::RemoveSuspension { .. } | ||
| ) | ||
| ) | ||
| }) | ||
| .cloned() | ||
| .collect(); | ||
| Some(Self { | ||
| event: ExecutionEvent::Paid { | ||
| identity: identity.clone(), | ||
| removed_balance: None, | ||
| added_to_balance_outputs: None, | ||
| operations: refused_operations, | ||
| execution_operations: execution_operations.clone(), | ||
| additional_fixed_fee_cost: None, | ||
| user_fee_increase: *user_fee_increase, | ||
| signing_key_limits: *signing_key_limits, | ||
| gas_sponsor: None, | ||
| action_fees: vec![], | ||
| }, | ||
|
Comment on lines
+182
to
+193
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Blocking: Sponsored overflow writes can become unpaid internal errors
source: |
||
| attempted_operations: operations.clone(), | ||
|
Comment on lines
+143
to
+194
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: Avoid cloning the complete event on every successful document write
source:
Comment on lines
+167
to
+194
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: Share or verify the refused-batch operation construction
source: |
||
| contract_id, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl ExecutionEvent<'_> { | ||
| /// The event with `fee` added to the processing fee it charges up front. | ||
| fn with_additional_fixed_fee_cost(mut self, fee: Credits) -> Self { | ||
| if let ExecutionEvent::Paid { | ||
| additional_fixed_fee_cost, | ||
| .. | ||
| } = &mut self | ||
| { | ||
| *additional_fixed_fee_cost = Some( | ||
| additional_fixed_fee_cost | ||
| .unwrap_or_default() | ||
| .saturating_add(fee), | ||
| ); | ||
| } | ||
| self | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion: Document the full set of writes that can overflow sums
The implementation prepares a refusal for any event containing a
DocumentOperation, but the documentation names only creates, replacements, and transfers. Deletes, erases, index-only deletes, purchases, price updates, and contested document operations also route through this path; removing a negative value can increase an aggregate pasti64::MAX, and other writes can underflow towardi64::MIN. Broaden this section and the tests to describe all document writes covered by the implementation, or narrow the implementation if the paid error is intentionally limited to the documented operations.source:
muse-spark-1.3-contributor(phase1-reviewer: general, rust-quality)