From 3a594fbcd7eddac3fb73a2e9987f6437c953de9d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Nov 2026 16:23:13 +0700 Subject: [PATCH 1/5] feat(platform)!: append private document entries paid from the shielded pool (PV15) Adds PrivateDocumentCreateFromShieldedPool (state transition type 29), which appends 1 to 16 entries of a private document type's entrySize to the type's store, paying a fixed fee from the credit shielded pool: an Orchard spend bundle whose value balance is exactly the fee, with no identity and no platform signature. The bundle's sighash binds the contract, the type name and every entry. - dpp: the transition, basic structure (entry count, sizes, no duplicates), the sighash preimage, the fee function, the SDK builder, and five consensus errors (10426, 10427, 10428, 40151, 40152). - platform-version: fee group fee_version.private_document (50,000,000 credits and 32 overhead bytes per entry), max_private_document_entries_per_transition 16, the transition's slots, and v15 note 25. - drive: the action, its conversion, a private document append operation behind Drive's 16-byte header, the store check, and the proof of execution. - drive-abci: the transform, the is_allowed gate at protocol version 15, the proof and exact-fee checks, and the pool-paid execution event. - wasm-dpp2 wrapper, wasm-dpp and platform-wallet arms, and book entries. Co-Authored-By: Claude Opus 5.5 --- book/src/contract-keywords.md | 3 +- book/src/fees/shielded-fees.md | 1 + .../src/errors/consensus/basic/basic_error.rs | 38 +- .../duplicate_private_document_entry_error.rs | 63 +++ ...alid_private_document_entry_count_error.rs | 55 ++ ...valid_private_document_entry_size_error.rs | 76 +++ .../errors/consensus/basic/document/mod.rs | 6 + packages/rs-dpp/src/errors/consensus/codes.rs | 5 + .../document_type_not_private_error.rs | 62 +++ .../errors/consensus/state/document/mod.rs | 2 + ...vate_document_entry_size_mismatch_error.rs | 79 +++ .../src/errors/consensus/state/state_error.rs | 29 + packages/rs-dpp/src/shielded/builder/mod.rs | 2 + ...vate_document_create_from_shielded_pool.rs | 162 ++++++ .../compute_minimum_shielded_fee/mod.rs | 71 +++ .../compute_minimum_shielded_fee/v0/mod.rs | 52 ++ packages/rs-dpp/src/shielded/mod.rs | 25 +- packages/rs-dpp/src/shielded/sighash.rs | 137 +++++ packages/rs-dpp/src/state_transition/mod.rs | 52 +- .../src/state_transition/serialization.rs | 6 + .../state_transition_types.rs | 12 +- .../state_transitions/shielded/mod.rs | 1 + .../accessors/mod.rs | 115 ++++ .../accessors/v0/mod.rs | 53 ++ .../methods/mod.rs | 61 +++ .../methods/v0/mod.rs | 35 ++ .../mod.rs | 152 ++++++ ...ate_transition_estimated_fee_validation.rs | 16 + .../state_transition_like.rs | 36 ++ .../state_transition_validation.rs | 17 + .../v0/mod.rs | 105 ++++ .../v0/state_transition_like.rs | 41 ++ .../v0/state_transition_validation.rs | 311 +++++++++++ .../v0/types.rs | 16 + .../v0/v0_methods.rs | 39 ++ .../v0/version.rs | 9 + .../version.rs | 11 + .../execution/types/execution_event/mod.rs | 15 + .../traits/address_balances_and_nonces.rs | 2 + .../processor/traits/address_witnesses.rs | 2 + .../traits/addresses_minimum_balance.rs | 2 + .../processor/traits/basic_structure.rs | 30 ++ .../processor/traits/identity_balance.rs | 1 + .../traits/identity_based_signature.rs | 3 + .../processor/traits/identity_nonces.rs | 2 + .../processor/traits/is_allowed.rs | 21 +- .../processor/traits/shielded_proof.rs | 73 ++- .../processor/traits/state.rs | 6 + .../state_transition/state_transitions/mod.rs | 2 + .../mod.rs | 60 +++ .../tests.rs | 496 ++++++++++++++++++ .../transform_into_action/mod.rs | 1 + .../transform_into_action/v0/mod.rs | 174 ++++++ .../state_transition/transformer/mod.rs | 5 + .../verify_state_transitions.rs | 3 +- .../document/private_document_store/mod.rs | 78 +++ .../document/private_document_store/v0/mod.rs | 37 +- .../prove/prove_state_transition/v0/mod.rs | 17 + .../action_convert_to_operations/mod.rs | 3 + .../shielded/mod.rs | 1 + ...nt_create_from_shielded_pool_transition.rs | 330 ++++++++++++ .../src/state_transition_action/mod.rs | 8 + .../state_transition_action/shielded/mod.rs | 2 + .../mod.rs | 62 +++ .../transformer.rs | 26 + .../v0/mod.rs | 25 + .../v0/transformer.rs | 29 + .../src/util/batch/drive_op_batch/mod.rs | 13 + .../batch/drive_op_batch/private_document.rs | 49 ++ .../v0/mod.rs | 29 + .../dpp_versions/dpp_contract_versions/mod.rs | 21 +- .../mod.rs | 1 + .../v1.rs | 5 + .../v2.rs | 5 + .../v3.rs | 5 + .../drive_abci_validation_versions/mod.rs | 2 + .../drive_abci_validation_versions/v1.rs | 9 + .../drive_abci_validation_versions/v10.rs | 9 + .../drive_abci_validation_versions/v2.rs | 9 + .../drive_abci_validation_versions/v3.rs | 9 + .../drive_abci_validation_versions/v4.rs | 9 + .../drive_abci_validation_versions/v5.rs | 9 + .../drive_abci_validation_versions/v6.rs | 9 + .../drive_abci_validation_versions/v7.rs | 9 + .../drive_abci_validation_versions/v8.rs | 9 + .../drive_abci_validation_versions/v9.rs | 9 + .../drive_document_method_versions/mod.rs | 5 + .../drive_document_method_versions/v1.rs | 1 + .../drive_document_method_versions/v2.rs | 1 + .../drive_document_method_versions/v3.rs | 1 + .../drive_document_method_versions/v4.rs | 1 + .../drive_document_method_versions/v5.rs | 4 +- .../mod.rs | 1 + .../v1.rs | 1 + .../v2.rs | 1 + .../v3.rs | 1 + .../v4.rs | 1 + .../src/version/drive_versions/v10.rs | 4 + .../feature_initial_protocol_versions.rs | 5 + .../src/version/fee/mod.rs | 9 + .../src/version/fee/private_document/mod.rs | 25 + .../src/version/fee/private_document/v1.rs | 18 + .../rs-platform-version/src/version/fee/v1.rs | 3 + .../rs-platform-version/src/version/fee/v2.rs | 3 + .../rs-platform-version/src/version/fee/v4.rs | 5 + .../src/version/mocks/v2_test.rs | 1 + .../src/version/system_limits/mod.rs | 5 + .../src/version/system_limits/v1.rs | 1 + .../src/version/system_limits/v2.rs | 1 + .../src/version/system_limits/v3.rs | 1 + .../src/version/system_limits/v4.rs | 1 + .../src/version/system_limits/v5.rs | 4 +- .../rs-platform-version/src/version/v15.rs | 34 +- .../network/state_transition_summary.rs | 3 +- .../src/errors/consensus/consensus_error.rs | 21 + .../state_transition_factory.rs | 1 + packages/wasm-dpp2/src/lib.rs | 3 +- packages/wasm-dpp2/src/shielded/mod.rs | 2 + ...nt_create_from_shielded_pool_transition.rs | 290 ++++++++++ .../base/state_transition.rs | 6 + 120 files changed, 4125 insertions(+), 31 deletions(-) create mode 100644 packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs create mode 100644 packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_count_error.rs create mode 100644 packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_size_error.rs create mode 100644 packages/rs-dpp/src/errors/consensus/state/document/document_type_not_private_error.rs create mode 100644 packages/rs-dpp/src/errors/consensus/state/document/private_document_entry_size_mismatch_error.rs create mode 100644 packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/v0/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/v0/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_estimated_fee_validation.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_like.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_validation.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_like.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/types.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/v0_methods.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/version.rs create mode 100644 packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/version.rs create mode 100644 packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs create mode 100644 packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs create mode 100644 packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs create mode 100644 packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs create mode 100644 packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs create mode 100644 packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs create mode 100644 packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs create mode 100644 packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs create mode 100644 packages/rs-platform-version/src/version/fee/private_document/mod.rs create mode 100644 packages/rs-platform-version/src/version/fee/private_document/v1.rs create mode 100644 packages/wasm-dpp2/src/shielded/private_document_create_from_shielded_pool_transition.rs diff --git a/book/src/contract-keywords.md b/book/src/contract-keywords.md index b8ace3c169b..0483204e46f 100644 --- a/book/src/contract-keywords.md +++ b/book/src/contract-keywords.md @@ -146,7 +146,7 @@ Every key a contract can write, grouped by where it goes. **Since** is the proto | `rangeCountable`, `rangeSummable`, `rangeAverageable` | boolean | Provable counts, sums or averages over ranges of document ids. | 12 | [Document type range keys](contract-keywords/aggregates.md#document-type-rangecountable-rangesummable-rangeaverageable) | | `indexOnly` | boolean | Documents are never stored whole: the index entries are the rows. | 14 | [indexOnly](contract-keywords/index-only.md#indexonly) · [internals](drive/index-only-document-types.md) | | `entryPayload` | array of 1 to 16 names | On an index-only type, properties carried in each entry's value instead of a key. | 14 | [entryPayload](contract-keywords/index-only.md#entrypayload) | -| `private` | object: `entrySize`, 1 to 4096 | A private type: its documents are encrypted entries of `entrySize` bytes, which the chain never reads. Its `properties` describe the plaintext. It declares only `type`, `properties`, `required`, `additionalProperties`, `minProperties`, `maxProperties`, `dependentRequired`, `$comment`, `description`, the two bounded key keys, and `documentsMutable`, `canBeDeleted` and `documentsKeepHistory` as `false`, each at most once; neither its properties nor `required` nor `dependentRequired` name a system field, no property at any depth declares `refersTo`, `distinctFrom`, `generatedFrom`, `encryptedFor` or `requiredSince`, and the contract's elected moderators may not moderate it. No reference, `countOf` or `sumOf` may name it, an update cannot change it, and a Batch refuses every document transition on it (`DocumentTypePrivateError`, 10425). | 15 | | +| `private` | object: `entrySize`, 1 to 4096 | A private type: its documents are encrypted entries of `entrySize` bytes, which the chain never reads. Its `properties` describe the plaintext. It declares only `type`, `properties`, `required`, `additionalProperties`, `minProperties`, `maxProperties`, `dependentRequired`, `$comment`, `description`, the two bounded key keys, and `documentsMutable`, `canBeDeleted` and `documentsKeepHistory` as `false`, each at most once; neither its properties nor `required` nor `dependentRequired` name a system field, no property at any depth declares `refersTo`, `distinctFrom`, `generatedFrom`, `encryptedFor` or `requiredSince`, and the contract's elected moderators may not moderate it. No reference, `countOf` or `sumOf` may name it, an update cannot change it, and a Batch refuses every document transition on it (`DocumentTypePrivateError`, 10425). Entries are appended only by `PrivateDocumentCreateFromShieldedPool` (type 29), from 1 to 16 distinct entries of exactly `entrySize` bytes, paid from the credit shielded pool. | 15 | | ### Property @@ -333,3 +333,4 @@ The first three limits come from the meta-schema, the rest from protocol version | `max_contested_summed_value_magnitude` | 134217728 (2^27) | the `minimum` and `maximum` of a summed property on a type with a contested index | | `max_expiring_signed_summed_value_magnitude` | 134217728 (2^27) | the `minimum` and `maximum` of a summed property that admits negative values, on a type with a `ttl` | | `max_private_document_entry_size` | 4096 bytes (protocol version 15) | a private type's `private.entrySize` | +| `max_private_document_entries_per_transition` | 16 (protocol version 15) | the entries one `PrivateDocumentCreateFromShieldedPool` appends | diff --git a/book/src/fees/shielded-fees.md b/book/src/fees/shielded-fees.md index d9c2c4f3be4..2caf0ea3637 100644 --- a/book/src/fees/shielded-fees.md +++ b/book/src/fees/shielded-fees.md @@ -45,6 +45,7 @@ The fee is derived differently depending on the shielded transition type: | **IdentityCreateFromShieldedPool** | `total_fee = metered(insert_nullifiers + AddNewIdentity(identity + N keys)) + shielded_verification_fee`, **moved from the new identity's balance** | `value_balance` is a **fixed `denomination`** (a member of the versioned set `{0.1, 0.3, 0.5, 1.0}` DASH) and must equal it EXACTLY. The new identity is created holding the full `denomination`, funded by decrementing the shielded pool by exactly that amount — a move *between* two balance trees (like `Unshield`'s pool→address), so the global system-credit supply is unchanged (**no** `AddToSystemCredits`); the fee is then **moved** from that balance into the fee pools, so the identity ends with `denomination − total_fee`. Unlike the flat pool-paid transitions, the `AddNewIdentity` write grows with the key count, so the cost is **metered** (not a flat carve) — only the ZK compute fee (`compute_shielded_verification_fee`) is added on top, exactly like the transparent `Shield`. The client predicts it offline with `compute_shielded_identity_create_fee(num_actions, num_keys)`; consensus rejects `denomination < total_fee` with `IdentityInsufficientBalanceError`. | | **ShieldFromIdentity** | `fee = metered(storage + processing) + shielded_verification_fee`, paid from the funding identity's balance | Identity balance to pool (protocol version 14). Charged exactly like `Shield`, but on the identity side: the identity signature covers the whole outputs-only bundle, the metered note writes and identity writes go through the standard identity-paid path (`IdentityCreditTransferToAddresses` model), and only the ZK compute fee is added as `additional_fixed_fee_cost`. `user_fee_increase` applies. The identity must hold `amount + fee`; consensus rejects a short balance with `IdentityInsufficientBalanceError`. The pool and the identity are both balance trees, so no system-credit adjustment is emitted. See [Entry-Transition Fees](#entry-transition-fees-shield-shieldfromassetlock-and-shieldfromidentity). | | **IdentityTopUpFromShieldedPool** | `fee = compute_shielded_identity_top_up_fee(num_actions)` = `compute_minimum_shielded_fee(num_actions) + identity_balance_storage_fee`, carved from `value_balance` | Shielded pool to an EXISTING identity's balance (protocol version 14). `value_balance` (the transition's `topUpAmount`) is the gross amount leaving the pool; the identity receives `topUpAmount - fee` and validation requires `topUpAmount >= fee`. Same flat pool-paid model as `Unshield`, with the identity balance write as a flat component built like `Unshield`'s address write but calibrated to its measured cost: the top-up rewrites the existing identity's balance element and its Merk path (320 replaced bytes, 175,320 credits of processing, no storage), folded into one flat figure with headroom like the other shielded components, so `identity_balance_storage_fee = 8 x per_byte_rate` (`SHIELDED_IDENTITY_TOP_UP_BALANCE_STORAGE_BYTES`). The target identity and gross amount are bound into the Orchard sighash; the identity must already exist; no system-credit adjustment. | +| **PrivateDocumentCreateFromShieldedPool** | `fee = value_balance == compute_private_document_create_from_shielded_pool_fee(num_actions, entries, entry_size)` = `compute_minimum_shielded_fee(num_actions) + entries × ((entry_size + entry_overhead_bytes) × per_byte_rate + entry_processing_fee)` | Entries appended to a private document type's store (protocol version 15), with no identity. As for `ShieldedTransfer`, the whole `value_balance` is the fee and must equal it exactly. Each entry pays its own bytes plus `entry_overhead_bytes` (32: Drive's 16-byte header and GroveDB's 1 to 3 bytes of framing, measured the same at every position and chunk power) at the storage rate, and a flat `entry_processing_fee` of 50,000,000 credits (both in `fee_version.private_document`): an entry is downloaded and trial-decrypted by every wallet of the type, so the flat part prices a flood of entries, not the work of storing one. The contract, the type name and every entry are bound into the Orchard sighash. | ### Token shielded pool fees diff --git a/packages/rs-dpp/src/errors/consensus/basic/basic_error.rs b/packages/rs-dpp/src/errors/consensus/basic/basic_error.rs index c6d1b7cf184..b21ad1a639d 100644 --- a/packages/rs-dpp/src/errors/consensus/basic/basic_error.rs +++ b/packages/rs-dpp/src/errors/consensus/basic/basic_error.rs @@ -64,9 +64,10 @@ use crate::consensus::basic::document::{ DocumentPropertyNotDistinctError, DocumentPropertyNotGeneratedError, DocumentReferencePreimageInvalidError, DocumentTransitionsAreAbsentError, DocumentTypePrivateError, DuplicateDocumentTransitionsWithIdsError, - DuplicateDocumentTransitionsWithIndicesError, InconsistentCompoundIndexDataError, - InvalidDocumentTransitionActionError, InvalidDocumentTransitionIdError, - InvalidDocumentTypeError, InvalidEncryptedPropertyShapeError, + DuplicateDocumentTransitionsWithIndicesError, DuplicatePrivateDocumentEntryError, + InconsistentCompoundIndexDataError, InvalidDocumentTransitionActionError, + InvalidDocumentTransitionIdError, InvalidDocumentTypeError, InvalidEncryptedPropertyShapeError, + InvalidPrivateDocumentEntryCountError, InvalidPrivateDocumentEntrySizeError, MaxDocumentsTransitionsExceededError, MissingDataContractIdBasicError, MissingDocumentTransitionActionError, MissingDocumentTransitionTypeError, MissingDocumentTypeError, MissingPositionsInDocumentTypePropertiesError, NonceOutOfBoundsError, @@ -891,6 +892,17 @@ pub enum BasicError { // A Batch document transition on a private document type (protocol version 15). #[error(transparent)] DocumentTypePrivateError(DocumentTypePrivateError), + + // A `PrivateDocumentCreateFromShieldedPool` (protocol version 15) with no entry or too + // many, or an entry that is empty, over the cap or not the size of the first. + #[error(transparent)] + InvalidPrivateDocumentEntryCountError(InvalidPrivateDocumentEntryCountError), + + #[error(transparent)] + InvalidPrivateDocumentEntrySizeError(InvalidPrivateDocumentEntrySizeError), + + #[error(transparent)] + DuplicatePrivateDocumentEntryError(DuplicatePrivateDocumentEntryError), } impl From for ConsensusError { @@ -1137,6 +1149,26 @@ mod tests { )), 210 ); + // A `PrivateDocumentCreateFromShieldedPool`'s entries (protocol version 15): the tail + // of the enum. + assert_eq!( + discriminant_of(BasicError::InvalidPrivateDocumentEntryCountError( + InvalidPrivateDocumentEntryCountError::new(17, 16) + )), + 211 + ); + assert_eq!( + discriminant_of(BasicError::InvalidPrivateDocumentEntrySizeError( + InvalidPrivateDocumentEntrySizeError::new(1, 584, 585, 4096) + )), + 212 + ); + assert_eq!( + discriminant_of(BasicError::DuplicatePrivateDocumentEntryError( + DuplicatePrivateDocumentEntryError::new(0, 3) + )), + 213 + ); } /// The variants that shipped in 4.1 keep the discriminants they were released with, so an diff --git a/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs b/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs new file mode 100644 index 00000000000..66733657a02 --- /dev/null +++ b/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs @@ -0,0 +1,63 @@ +use crate::consensus::basic::BasicError; +use crate::consensus::ConsensusError; +use crate::errors::ProtocolError; +use bincode::{Decode, DecodeUntrusted, Encode}; +use platform_serialization_derive::{ + PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, +}; +use thiserror::Error; + +/// Two entries of a `PrivateDocumentCreateFromShieldedPool` are the same bytes (protocol version +/// 15). A recipient keeps one copy of an entry, so a second copy in the same transition would +/// only be stored for nothing, and Drive appends every entry of a transition in one batch, behind +/// the same header, where two identical appends are the same operation twice. +#[derive( + Error, + Debug, + Clone, + PartialEq, + Eq, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + DecodeUntrusted, +)] +#[error( + "Entry {entry_index} of a private document create is the same as entry {first_index}; \ + every entry of a transition must differ" +)] +#[platform_serialize(unversioned)] +pub struct DuplicatePrivateDocumentEntryError { + /* + + DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION + + */ + first_index: u16, + entry_index: u16, +} + +impl DuplicatePrivateDocumentEntryError { + pub fn new(first_index: u16, entry_index: u16) -> Self { + Self { + first_index, + entry_index, + } + } + + pub fn first_index(&self) -> u16 { + self.first_index + } + + pub fn entry_index(&self) -> u16 { + self.entry_index + } +} + +impl From for ConsensusError { + fn from(err: DuplicatePrivateDocumentEntryError) -> Self { + Self::BasicError(BasicError::DuplicatePrivateDocumentEntryError(err)) + } +} diff --git a/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_count_error.rs b/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_count_error.rs new file mode 100644 index 00000000000..f2fa632054e --- /dev/null +++ b/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_count_error.rs @@ -0,0 +1,55 @@ +use crate::consensus::basic::BasicError; +use crate::consensus::ConsensusError; +use crate::errors::ProtocolError; +use bincode::{Decode, DecodeUntrusted, Encode}; +use platform_serialization_derive::{ + PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, +}; +use thiserror::Error; + +/// A `PrivateDocumentCreateFromShieldedPool` carries no entry, or more entries than +/// `SystemLimits::max_private_document_entries_per_transition` (protocol version 15). +#[derive( + Error, + Debug, + Clone, + PartialEq, + Eq, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + DecodeUntrusted, +)] +#[error("A private document create carries {count} entries; it must carry from 1 to {max_count}")] +#[platform_serialize(unversioned)] +pub struct InvalidPrivateDocumentEntryCountError { + /* + + DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION + + */ + count: u64, + max_count: u16, +} + +impl InvalidPrivateDocumentEntryCountError { + pub fn new(count: u64, max_count: u16) -> Self { + Self { count, max_count } + } + + pub fn count(&self) -> u64 { + self.count + } + + pub fn max_count(&self) -> u16 { + self.max_count + } +} + +impl From for ConsensusError { + fn from(err: InvalidPrivateDocumentEntryCountError) -> Self { + Self::BasicError(BasicError::InvalidPrivateDocumentEntryCountError(err)) + } +} diff --git a/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_size_error.rs b/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_size_error.rs new file mode 100644 index 00000000000..5015c81440b --- /dev/null +++ b/packages/rs-dpp/src/errors/consensus/basic/document/invalid_private_document_entry_size_error.rs @@ -0,0 +1,76 @@ +use crate::consensus::basic::BasicError; +use crate::consensus::ConsensusError; +use crate::errors::ProtocolError; +use bincode::{Decode, DecodeUntrusted, Encode}; +use platform_serialization_derive::{ + PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, +}; +use thiserror::Error; + +/// An entry of a `PrivateDocumentCreateFromShieldedPool` is empty, takes more bytes than +/// `SystemLimits::max_private_document_entry_size`, or takes a different number of bytes than +/// the transition's first entry (protocol version 15). Every entry of a private document type +/// takes exactly the type's `entrySize`, which only the contract states; this is the check that +/// needs no contract. +#[derive( + Error, + Debug, + Clone, + PartialEq, + Eq, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + DecodeUntrusted, +)] +#[error( + "Entry {entry_index} of a private document create takes {size} bytes; every entry must \ + take as many bytes as the first ({first_entry_size}), from 1 to {max_size}" +)] +#[platform_serialize(unversioned)] +pub struct InvalidPrivateDocumentEntrySizeError { + /* + + DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION + + */ + entry_index: u16, + size: u32, + first_entry_size: u32, + max_size: u32, +} + +impl InvalidPrivateDocumentEntrySizeError { + pub fn new(entry_index: u16, size: u32, first_entry_size: u32, max_size: u32) -> Self { + Self { + entry_index, + size, + first_entry_size, + max_size, + } + } + + pub fn entry_index(&self) -> u16 { + self.entry_index + } + + pub fn size(&self) -> u32 { + self.size + } + + pub fn first_entry_size(&self) -> u32 { + self.first_entry_size + } + + pub fn max_size(&self) -> u32 { + self.max_size + } +} + +impl From for ConsensusError { + fn from(err: InvalidPrivateDocumentEntrySizeError) -> Self { + Self::BasicError(BasicError::InvalidPrivateDocumentEntrySizeError(err)) + } +} diff --git a/packages/rs-dpp/src/errors/consensus/basic/document/mod.rs b/packages/rs-dpp/src/errors/consensus/basic/document/mod.rs index f16d06302e0..f75d9657e45 100644 --- a/packages/rs-dpp/src/errors/consensus/basic/document/mod.rs +++ b/packages/rs-dpp/src/errors/consensus/basic/document/mod.rs @@ -11,12 +11,15 @@ mod document_transitions_are_absent_error; mod document_type_private_error; mod duplicate_document_transitions_with_ids_error; mod duplicate_document_transitions_with_indices_error; +mod duplicate_private_document_entry_error; mod identity_contract_nonce_out_of_bounds_error; mod inconsistent_compound_index_data_error; mod invalid_document_transition_action_error; mod invalid_document_transition_id_error; mod invalid_document_type_error; mod invalid_encrypted_property_shape_error; +mod invalid_private_document_entry_count_error; +mod invalid_private_document_entry_size_error; mod max_documents_transitions_exceeded_error; mod missing_data_contract_id_basic_error; mod missing_document_transition_action_error; @@ -37,12 +40,15 @@ pub use document_transitions_are_absent_error::*; pub use document_type_private_error::*; pub use duplicate_document_transitions_with_ids_error::*; pub use duplicate_document_transitions_with_indices_error::*; +pub use duplicate_private_document_entry_error::*; pub use identity_contract_nonce_out_of_bounds_error::*; pub use inconsistent_compound_index_data_error::*; pub use invalid_document_transition_action_error::*; pub use invalid_document_transition_id_error::*; pub use invalid_document_type_error::*; pub use invalid_encrypted_property_shape_error::*; +pub use invalid_private_document_entry_count_error::*; +pub use invalid_private_document_entry_size_error::*; pub use max_documents_transitions_exceeded_error::*; pub use missing_data_contract_id_basic_error::*; pub use missing_document_transition_action_error::*; diff --git a/packages/rs-dpp/src/errors/consensus/codes.rs b/packages/rs-dpp/src/errors/consensus/codes.rs index 5dbda085705..9b3326f5a4c 100644 --- a/packages/rs-dpp/src/errors/consensus/codes.rs +++ b/packages/rs-dpp/src/errors/consensus/codes.rs @@ -178,6 +178,9 @@ impl ErrorWithCode for BasicError { Self::DocumentReferencePreimageInvalidError(_) => 10423, Self::DocumentPropertyNotGeneratedError(_) => 10424, Self::DocumentTypePrivateError(_) => 10425, + Self::InvalidPrivateDocumentEntryCountError(_) => 10426, + Self::InvalidPrivateDocumentEntrySizeError(_) => 10427, + Self::DuplicatePrivateDocumentEntryError(_) => 10428, // Token Errors: 10450-10499 Self::InvalidTokenIdError(_) => 10450, @@ -395,6 +398,8 @@ impl ErrorWithCode for StateError { Self::ReferencedDocumentRuleReadInvalidError(_) => 40148, Self::ReferencedDocumentPrecededError(_) => 40149, Self::ReferencedDocumentTypePrivateError(_) => 40150, + Self::DocumentTypeNotPrivateError(_) => 40151, + Self::PrivateDocumentEntrySizeMismatchError(_) => 40152, // Identity Errors: 40200-40299 Self::IdentityAlreadyExistsError(_) => 40200, diff --git a/packages/rs-dpp/src/errors/consensus/state/document/document_type_not_private_error.rs b/packages/rs-dpp/src/errors/consensus/state/document/document_type_not_private_error.rs new file mode 100644 index 00000000000..a922bde2254 --- /dev/null +++ b/packages/rs-dpp/src/errors/consensus/state/document/document_type_not_private_error.rs @@ -0,0 +1,62 @@ +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 `PrivateDocumentCreateFromShieldedPool` names a document type that is not private +/// (protocol version 15): only a type declaring `private` has a store for entries. +#[derive( + Error, + Debug, + Clone, + PartialEq, + Eq, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + DecodeUntrusted, +)] +#[error( + "Document type \"{document_type_name}\" of data contract {data_contract_id} is not private: \ + only a private document type takes entries" +)] +#[platform_serialize(unversioned)] +pub struct DocumentTypeNotPrivateError { + /* + + DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION + + */ + data_contract_id: Identifier, + document_type_name: String, +} + +impl DocumentTypeNotPrivateError { + pub fn new(data_contract_id: Identifier, document_type_name: String) -> Self { + Self { + data_contract_id, + document_type_name, + } + } + + pub fn data_contract_id(&self) -> Identifier { + self.data_contract_id + } + + pub fn document_type_name(&self) -> &str { + &self.document_type_name + } +} + +impl From for ConsensusError { + fn from(err: DocumentTypeNotPrivateError) -> Self { + Self::StateError(StateError::DocumentTypeNotPrivateError(err)) + } +} diff --git a/packages/rs-dpp/src/errors/consensus/state/document/mod.rs b/packages/rs-dpp/src/errors/consensus/state/document/mod.rs index 22f20966d34..544d7db4ec7 100644 --- a/packages/rs-dpp/src/errors/consensus/state/document/mod.rs +++ b/packages/rs-dpp/src/errors/consensus/state/document/mod.rs @@ -21,8 +21,10 @@ pub mod document_owner_id_mismatch_error; pub mod document_timestamp_window_violation_error; pub mod document_timestamps_are_equal_error; pub mod document_timestamps_mismatch_error; +pub mod document_type_not_private_error; pub mod duplicate_unique_index_error; pub mod invalid_document_revision_error; +pub mod private_document_entry_size_mismatch_error; pub mod referenced_contract_requirement_not_met_error; pub mod referenced_document_list_invalid_error; pub mod referenced_document_lookup_invalid_error; diff --git a/packages/rs-dpp/src/errors/consensus/state/document/private_document_entry_size_mismatch_error.rs b/packages/rs-dpp/src/errors/consensus/state/document/private_document_entry_size_mismatch_error.rs new file mode 100644 index 00000000000..fe4d0fde718 --- /dev/null +++ b/packages/rs-dpp/src/errors/consensus/state/document/private_document_entry_size_mismatch_error.rs @@ -0,0 +1,79 @@ +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; + +/// The entries of a `PrivateDocumentCreateFromShieldedPool` do not take the `entrySize` the +/// private document type declares (protocol version 15). +#[derive( + Error, + Debug, + Clone, + PartialEq, + Eq, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + DecodeUntrusted, +)] +#[error( + "Entries of private document type \"{document_type_name}\" of data contract \ + {data_contract_id} take {entry_size} bytes, but this transition's take {size}" +)] +#[platform_serialize(unversioned)] +pub struct PrivateDocumentEntrySizeMismatchError { + /* + + DO NOT CHANGE ORDER OF FIELDS WITHOUT INTRODUCING OF NEW VERSION + + */ + data_contract_id: Identifier, + document_type_name: String, + entry_size: u32, + size: u32, +} + +impl PrivateDocumentEntrySizeMismatchError { + pub fn new( + data_contract_id: Identifier, + document_type_name: String, + entry_size: u32, + size: u32, + ) -> Self { + Self { + data_contract_id, + document_type_name, + entry_size, + size, + } + } + + pub fn data_contract_id(&self) -> Identifier { + self.data_contract_id + } + + pub fn document_type_name(&self) -> &str { + &self.document_type_name + } + + pub fn entry_size(&self) -> u32 { + self.entry_size + } + + pub fn size(&self) -> u32 { + self.size + } +} + +impl From for ConsensusError { + fn from(err: PrivateDocumentEntrySizeMismatchError) -> Self { + Self::StateError(StateError::PrivateDocumentEntrySizeMismatchError(err)) + } +} diff --git a/packages/rs-dpp/src/errors/consensus/state/state_error.rs b/packages/rs-dpp/src/errors/consensus/state/state_error.rs index d93b48ff2d3..2d07f7c7f9d 100644 --- a/packages/rs-dpp/src/errors/consensus/state/state_error.rs +++ b/packages/rs-dpp/src/errors/consensus/state/state_error.rs @@ -84,6 +84,8 @@ use crate::consensus::state::document::referenced_document_removed_error::Refere use crate::consensus::state::document::referenced_document_type_moderated_error::ReferencedDocumentTypeModeratedError; use crate::consensus::state::document::referenced_document_type_index_only_error::ReferencedDocumentTypeIndexOnlyError; use crate::consensus::state::document::referenced_document_type_private_error::ReferencedDocumentTypePrivateError; +use crate::consensus::state::document::document_type_not_private_error::DocumentTypeNotPrivateError; +use crate::consensus::state::document::private_document_entry_size_mismatch_error::PrivateDocumentEntrySizeMismatchError; use crate::consensus::state::document::document_delete_constraint_violated_error::DocumentDeleteConstraintViolatedError; use crate::consensus::state::document::referenced_document_rule_read_invalid_error::ReferencedDocumentRuleReadInvalidError; use crate::consensus::state::document::referenced_document_preceded_error::ReferencedDocumentPrecededError; @@ -761,6 +763,14 @@ pub enum StateError { // A document reference names a private document type (protocol version 15). #[error(transparent)] ReferencedDocumentTypePrivateError(ReferencedDocumentTypePrivateError), + + // A `PrivateDocumentCreateFromShieldedPool` (protocol version 15) names a type that is not + // private, or carries entries that do not take the type's `entrySize`. + #[error(transparent)] + DocumentTypeNotPrivateError(DocumentTypeNotPrivateError), + + #[error(transparent)] + PrivateDocumentEntrySizeMismatchError(PrivateDocumentEntrySizeMismatchError), } impl From for ConsensusError { @@ -1721,5 +1731,24 @@ mod tests { )), 178 ); + // A `PrivateDocumentCreateFromShieldedPool`'s target (protocol version 15): the tail of + // the enum. + assert_eq!( + discriminant_of(StateError::DocumentTypeNotPrivateError( + DocumentTypeNotPrivateError::new(identity_id, "contactRequest".to_string()) + )), + 179 + ); + assert_eq!( + discriminant_of(StateError::PrivateDocumentEntrySizeMismatchError( + PrivateDocumentEntrySizeMismatchError::new( + identity_id, + "privateContactRequest".to_string(), + 585, + 584, + ) + )), + 180 + ); } } diff --git a/packages/rs-dpp/src/shielded/builder/mod.rs b/packages/rs-dpp/src/shielded/builder/mod.rs index 3651ecbf12b..86a165614a0 100644 --- a/packages/rs-dpp/src/shielded/builder/mod.rs +++ b/packages/rs-dpp/src/shielded/builder/mod.rs @@ -32,6 +32,7 @@ mod document_token_payment; mod identity_create_from_shielded_pool; mod identity_top_up_from_shielded_pool; +mod private_document_create_from_shielded_pool; mod shield; mod shield_from_asset_lock; mod shield_from_identity; @@ -53,6 +54,7 @@ pub use identity_create_from_shielded_pool::{ build_identity_create_from_shielded_pool_transition, IdentityCreateFromShieldedPoolBuildResult, }; pub use identity_top_up_from_shielded_pool::build_identity_top_up_from_shielded_pool_transition; +pub use private_document_create_from_shielded_pool::build_private_document_create_from_shielded_pool_transition; pub use shield_from_asset_lock::build_shield_from_asset_lock_transition; #[cfg(feature = "core_key_wallet")] pub use shield_from_asset_lock::build_shield_from_asset_lock_transition_with_signer; diff --git a/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs new file mode 100644 index 00000000000..bb7489606f3 --- /dev/null +++ b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs @@ -0,0 +1,162 @@ +use grovedb_commitment_tree::{Anchor, FullViewingKey, SpendAuthorizingKey}; + +use crate::address_funds::OrchardAddress; +use crate::fee::Credits; +use crate::shielded::{ + compute_private_document_create_from_shielded_pool_fee, + private_document_create_from_shielded_pool_extra_sighash_data, +}; +use crate::state_transition::private_document_create_from_shielded_pool_transition::methods::PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0; +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::state_transition::StateTransition; +use crate::ProtocolError; +use platform_value::{BinaryData, Identifier}; +use platform_version::version::PlatformVersion; + +use super::{build_spend_bundle, serialize_authorized_bundle, OrchardProver, SpendableNote}; + +/// Build a `PrivateDocumentCreateFromShieldedPool` transition (protocol version 15): append +/// `entries` to the private document type `document_type_name` of `data_contract_id`, paying the +/// fee from `spends` and sending the change back to `change_address`. +/// +/// The fee is not a parameter: the bundle's value balance is the fee, and consensus pins it to +/// exactly `compute_private_document_create_from_shielded_pool_fee` for the bundle's action +/// count and the entries' number and size, so there is nothing for the caller to choose. The +/// fee is priced at the size of the first entry; every entry must take the type's `entrySize`, +/// which consensus checks. The contract, the type name and every entry are bound into the +/// bundle's sighash. Returns the built transition together with the fee it pays. +#[allow(clippy::too_many_arguments)] +pub fn build_private_document_create_from_shielded_pool_transition( + spends: Vec, + data_contract_id: Identifier, + document_type_name: String, + entries: Vec, + change_address: &OrchardAddress, + fvk: &FullViewingKey, + ask: &SpendAuthorizingKey, + anchor: Anchor, + prover: &P, + platform_version: &PlatformVersion, +) -> Result<(StateTransition, Credits), ProtocolError> { + let entry_size = entries.first().map(BinaryData::len).ok_or_else(|| { + ProtocolError::ShieldedBuildError("a private document create needs an entry".to_string()) + })?; + let entry_size = u32::try_from(entry_size).map_err(|_| { + ProtocolError::ShieldedBuildError(format!( + "a private document entry of {entry_size} bytes is too large" + )) + })?; + + let total_spent = spends + .iter() + .try_fold(0u64, |total, spend| { + total.checked_add(spend.note.value().inner()) + }) + .ok_or_else(|| { + ProtocolError::ShieldedBuildError("the spent notes' total overflows u64".to_string()) + })?; + + // The bundle holds the spends and one change output, padded to at least two actions. + let num_actions = spends.len().max(2); + let fee = compute_private_document_create_from_shielded_pool_fee( + num_actions, + entries.len(), + entry_size, + platform_version, + )?; + if fee > total_spent { + return Err(ProtocolError::ShieldedBuildError(format!( + "private document create fee {fee} exceeds total spendable value {total_spent}" + ))); + } + let change_amount = total_spent - fee; + + let extra_sighash_data = private_document_create_from_shielded_pool_extra_sighash_data( + &data_contract_id.to_buffer(), + &document_type_name, + &entries, + platform_version, + )?; + + let bundle = build_spend_bundle( + spends, + change_address, + change_amount, + [0u8; 36], + fvk, + ask, + anchor, + prover, + &extra_sighash_data, + )?; + + let sb = serialize_authorized_bundle(&bundle); + + let state_transition = PrivateDocumentCreateFromShieldedPoolTransition::try_from_bundle( + data_contract_id, + document_type_name, + entries, + sb.actions, + sb.value_balance as u64, + sb.anchor, + sb.proof, + sb.binding_signature, + platform_version, + )?; + Ok((state_transition, fee)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shielded::builder::test_helpers::{ + test_orchard_address, test_spendable_note, TestProver, + }; + + fn keys() -> (FullViewingKey, SpendAuthorizingKey) { + let sk = grovedb_commitment_tree::SpendingKey::from_bytes([42u8; 32]) + .expect("valid spending key bytes"); + (FullViewingKey::from(&sk), SpendAuthorizingKey::from(&sk)) + } + + #[test] + fn should_refuse_to_build_without_an_entry() { + let (fvk, ask) = keys(); + let err = build_private_document_create_from_shielded_pool_transition( + vec![test_spendable_note(1_000_000_000)], + Identifier::from([1u8; 32]), + "privateContactRequest".to_string(), + vec![], + &test_orchard_address(), + &fvk, + &ask, + Anchor::empty_tree(), + &TestProver, + PlatformVersion::latest(), + ) + .expect_err("an empty entry list has no size to price"); + assert!(err.to_string().contains("needs an entry"), "{err}"); + } + + #[test] + fn should_refuse_to_build_when_the_notes_do_not_cover_the_fee() { + let (fvk, ask) = keys(); + let err = build_private_document_create_from_shielded_pool_transition( + vec![test_spendable_note(100)], + Identifier::from([1u8; 32]), + "privateContactRequest".to_string(), + vec![BinaryData::new(vec![7u8; 585])], + &test_orchard_address(), + &fvk, + &ask, + Anchor::empty_tree(), + &TestProver, + PlatformVersion::latest(), + ) + .expect_err("100 credits do not cover the fee"); + assert!( + err.to_string().contains("exceeds total spendable value"), + "{err}" + ); + } +} diff --git a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs index e24db54c77f..f6f21549a57 100644 --- a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs +++ b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs @@ -306,10 +306,81 @@ pub fn compute_token_purchase_from_shielded_pool_fee( ) } +/// The fee of a `PrivateDocumentCreateFromShieldedPool` (protocol version 15): the bundle's +/// [`compute_minimum_shielded_fee`] plus, for each entry, its bytes and the per-entry overhead +/// of `fee_version.private_document` at the storage rate, and that group's flat entry fee. +/// +/// The bundle's value balance must equal this exactly: there is no recipient to take an +/// excess, and an unusual fee would mark the sender. It depends only on the action count, the +/// entry count and the entry size, so a wallet computes it before building the bundle; the SDK +/// builder, the minimum-fee validation and the transformer all use it. +/// +/// # Parameters +/// - `num_actions` — number of Orchard actions in the bundle +/// - `entry_count` — number of entries the transition appends +/// - `entry_size` — the bytes each entry takes, the type's `entrySize` +/// - `platform_version` — protocol version (determines the formula version and fee constants) +pub fn compute_private_document_create_from_shielded_pool_fee( + num_actions: usize, + entry_count: usize, + entry_size: u32, + platform_version: &PlatformVersion, +) -> Result { + match platform_version.dpp.methods.compute_minimum_shielded_fee { + 0 => v0::compute_private_document_create_from_shielded_pool_fee_v0( + num_actions, + entry_count, + entry_size, + platform_version, + ), + version => Err(ProtocolError::UnknownVersionMismatch { + method: "compute_private_document_create_from_shielded_pool_fee".to_string(), + known_versions: vec![0], + received: version, + }), + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn should_price_each_private_document_entry_at_its_bytes_and_the_flat_entry_fee() { + let platform_version = PlatformVersion::latest(); + let storage = &platform_version.fee_version.storage; + let private_document = &platform_version.fee_version.private_document; + let per_byte = + storage.storage_disk_usage_credit_per_byte + storage.storage_processing_credit_per_byte; + let base = compute_minimum_shielded_fee(2, platform_version).expect("base fee"); + let entry = (585 + private_document.entry_overhead_bytes) * per_byte + + private_document.entry_processing_fee; + for entry_count in [1, 2, 16] { + assert_eq!( + compute_private_document_create_from_shielded_pool_fee( + 2, + entry_count, + 585, + platform_version + ) + .expect("private document create fee"), + base + entry_count as u64 * entry + ); + } + assert_eq!(private_document.entry_processing_fee, 50_000_000); + } + + #[test] + fn should_refuse_a_private_document_fee_that_overflows() { + assert!(compute_private_document_create_from_shielded_pool_fee( + 2, + usize::MAX, + u32::MAX, + PlatformVersion::latest() + ) + .is_err()); + } + #[test] fn should_preserve_token_purchase_fee_when_identity_shield_floor_changes() { let platform_version = PlatformVersion::latest(); diff --git a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/v0/mod.rs b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/v0/mod.rs index 5fad93d87d7..df1526d1786 100644 --- a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/v0/mod.rs +++ b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/v0/mod.rs @@ -364,6 +364,58 @@ pub fn compute_token_pool_paid_shielded_fee_v0( )) } +/// v0 of the `PrivateDocumentCreateFromShieldedPool` fee formula: +/// +/// `fee = compute_minimum_shielded_fee_v0(num_actions) +/// + entry_count × ((entry_size + entry_overhead_bytes) × (disk + processing) credits/byte +/// + entry_processing_fee)` +/// +/// with `entry_overhead_bytes` and `entry_processing_fee` from `fee_version.private_document`. +/// The overhead covers Drive's 16-byte header and what GroveDB's append adds per entry, so the +/// fee covers the store's metered storage at every position and for every chunk power a store +/// can take, and `execute_event/v0`'s `storage = min(real_storage, fee)` split never zeroes the +/// proposer's share. All arithmetic is checked. +pub fn compute_private_document_create_from_shielded_pool_fee_v0( + num_actions: usize, + entry_count: usize, + entry_size: u32, + platform_version: &PlatformVersion, +) -> Result { + let storage = &platform_version.fee_version.storage; + let private_document = &platform_version.fee_version.private_document; + + let base_fee = compute_minimum_shielded_fee_v0(num_actions, platform_version)?; + + let per_byte_rate = storage + .storage_disk_usage_credit_per_byte + .checked_add(storage.storage_processing_credit_per_byte) + .ok_or(ProtocolError::Overflow( + "shielded storage per-byte rate overflow", + ))?; + let entry_bytes = (entry_size as u64) + .checked_add(private_document.entry_overhead_bytes) + .ok_or(ProtocolError::Overflow( + "private document entry bytes overflow", + ))?; + let entry_fee = entry_bytes + .checked_mul(per_byte_rate) + .and_then(|storage_fee| storage_fee.checked_add(private_document.entry_processing_fee)) + .ok_or(ProtocolError::Overflow( + "private document entry fee overflow", + ))?; + let entries_fee = + (entry_count as u64) + .checked_mul(entry_fee) + .ok_or(ProtocolError::Overflow( + "private document entries fee overflow", + ))?; + base_fee + .checked_add(entries_fee) + .ok_or(ProtocolError::Overflow( + "private document create fee overflow", + )) +} + #[cfg(test)] mod tests { use super::*; diff --git a/packages/rs-dpp/src/shielded/mod.rs b/packages/rs-dpp/src/shielded/mod.rs index 288a2e7a7b7..60805aa4a42 100644 --- a/packages/rs-dpp/src/shielded/mod.rs +++ b/packages/rs-dpp/src/shielded/mod.rs @@ -18,11 +18,11 @@ use serde::{Deserialize, Serialize}; // Re-exported so the public path stays `dpp::shielded::compute_minimum_shielded_fee` (the // module and the function share a name but live in different namespaces). pub use compute_minimum_shielded_fee::{ - compute_minimum_shielded_fee, compute_shielded_identity_balance_write_fee, - compute_shielded_identity_create_fee, compute_shielded_identity_top_up_fee, - compute_shielded_unshield_fee, compute_shielded_verification_fee, - compute_shielded_withdrawal_fee, compute_token_pool_paid_shielded_fee, - compute_token_purchase_from_shielded_pool_fee, + compute_minimum_shielded_fee, compute_private_document_create_from_shielded_pool_fee, + compute_shielded_identity_balance_write_fee, compute_shielded_identity_create_fee, + compute_shielded_identity_top_up_fee, compute_shielded_unshield_fee, + compute_shielded_verification_fee, compute_shielded_withdrawal_fee, + compute_token_pool_paid_shielded_fee, compute_token_purchase_from_shielded_pool_fee, compute_token_shielded_transfer_with_shielded_fee_fee, compute_token_unshield_with_shielded_fee_fee, }; @@ -70,7 +70,9 @@ pub use sighash::{ identity_create_from_shielded_extra_sighash_data, identity_create_from_shielded_extra_sighash_data_v0, identity_top_up_from_shielded_extra_sighash_data, - identity_top_up_from_shielded_extra_sighash_data_v0, shield_extra_sighash_data, + identity_top_up_from_shielded_extra_sighash_data_v0, + private_document_create_from_shielded_pool_extra_sighash_data, + private_document_create_from_shielded_pool_extra_sighash_data_v0, shield_extra_sighash_data, shield_from_asset_lock_extra_sighash_data, shield_from_identity_extra_sighash_data, shielded_withdrawal_extra_sighash_data, shielded_withdrawal_extra_sighash_data_v0, token_burn_from_pool_extra_sighash_data, token_burn_from_pool_extra_sighash_data_v0, @@ -84,11 +86,12 @@ pub use sighash::{ token_unshield_extra_sighash_data, token_unshield_extra_sighash_data_v0, token_unshield_with_shielded_fee_extra_sighash_data, token_unshield_with_shielded_fee_extra_sighash_data_v0, unshield_extra_sighash_data, - unshield_extra_sighash_data_v0, SHIELD_BUNDLE_TAG, SHIELD_FROM_ASSET_LOCK_BUNDLE_TAG, - SHIELD_FROM_IDENTITY_BUNDLE_TAG, TOKEN_CLAIM_TO_POOL_BUNDLE_TAG, - TOKEN_DIRECT_PURCHASE_TO_POOL_BUNDLE_TAG, TOKEN_MINT_TO_POOL_BUNDLE_TAG, - TOKEN_PURCHASE_FROM_SHIELDED_POOL_TYPE, TOKEN_SHIELDED_TRANSFER_WITH_SHIELDED_FEE_TYPE, - TOKEN_SHIELD_BUNDLE_TAG, TOKEN_UNSHIELD_WITH_SHIELDED_FEE_TYPE, + unshield_extra_sighash_data_v0, PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE, + SHIELD_BUNDLE_TAG, SHIELD_FROM_ASSET_LOCK_BUNDLE_TAG, SHIELD_FROM_IDENTITY_BUNDLE_TAG, + TOKEN_CLAIM_TO_POOL_BUNDLE_TAG, TOKEN_DIRECT_PURCHASE_TO_POOL_BUNDLE_TAG, + TOKEN_MINT_TO_POOL_BUNDLE_TAG, TOKEN_PURCHASE_FROM_SHIELDED_POOL_TYPE, + TOKEN_SHIELDED_TRANSFER_WITH_SHIELDED_FEE_TYPE, TOKEN_SHIELD_BUNDLE_TAG, + TOKEN_UNSHIELD_WITH_SHIELDED_FEE_TYPE, }; /// Calibrated effective storage-byte cost of the Core withdrawal document a diff --git a/packages/rs-dpp/src/shielded/sighash.rs b/packages/rs-dpp/src/shielded/sighash.rs index 4bf46574a32..40d9e10e722 100644 --- a/packages/rs-dpp/src/shielded/sighash.rs +++ b/packages/rs-dpp/src/shielded/sighash.rs @@ -18,6 +18,7 @@ use crate::state_transition::public_key_in_creation::accessors::IdentityPublicKe use crate::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; use crate::withdrawal::Pooling; use crate::ProtocolError; +use platform_value::BinaryData; use platform_version::version::PlatformVersion; use sha2::{Digest, Sha256}; use std::collections::BTreeMap; @@ -32,6 +33,9 @@ pub const TOKEN_SHIELDED_TRANSFER_WITH_SHIELDED_FEE_TYPE: u8 = 26; pub const TOKEN_UNSHIELD_WITH_SHIELDED_FEE_TYPE: u8 = 27; /// The state transition type byte the token bundle of a `TokenPurchaseFromShieldedPool` commits to. pub const TOKEN_PURCHASE_FROM_SHIELDED_POOL_TYPE: u8 = 28; +/// The state transition type byte the bundle of a `PrivateDocumentCreateFromShieldedPool` +/// commits to. +pub const PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE: u8 = 29; /// Domain tag an outputs-only token pool bundle commits to. Unlike the three constants above /// these are not `StateTransitionType` bytes — every one of these bundles rides inside a batch @@ -583,6 +587,59 @@ pub fn token_purchase_from_shielded_pool_extra_sighash_data_v0( data } +/// Builds the transparent `extra_data` bound into a `PrivateDocumentCreateFromShieldedPool`'s +/// platform sighash: the target contract, the private document type and every entry, so the +/// bundle pays for these entries of this type and nothing else. Changing any byte of the target +/// or of any entry, or the entries' number or order, invalidates the bundle. +/// +/// The signing (SDK builder) and verifying (consensus) sides both call this function. +pub fn private_document_create_from_shielded_pool_extra_sighash_data( + data_contract_id: &[u8; 32], + document_type_name: &str, + entries: &[BinaryData], + platform_version: &PlatformVersion, +) -> Result, ProtocolError> { + match platform_version.dpp.methods.shielded_extra_sighash_data { + 0 => Ok( + private_document_create_from_shielded_pool_extra_sighash_data_v0( + data_contract_id, + document_type_name, + entries, + ), + ), + version => Err(ProtocolError::UnknownVersionMismatch { + method: "private_document_create_from_shielded_pool_extra_sighash_data".to_string(), + known_versions: vec![0], + received: version, + }), + } +} + +/// Version 0 layout: `state transition type (1, = 29) || SHA-256(data_contract_id (32) || +/// type name length (4, LE) || type name || entry count (4, LE) || for each entry, in order: +/// its length (4, LE) || the entry)`, 33 bytes. Every variable-length part carries its length, +/// so no two targets or entry lists share a preimage. Frozen. +pub fn private_document_create_from_shielded_pool_extra_sighash_data_v0( + data_contract_id: &[u8; 32], + document_type_name: &str, + entries: &[BinaryData], +) -> Vec { + let mut hasher = Sha256::new(); + hasher.update(data_contract_id); + hasher.update((document_type_name.len() as u32).to_le_bytes()); + hasher.update(document_type_name.as_bytes()); + hasher.update((entries.len() as u32).to_le_bytes()); + for entry in entries { + hasher.update((entry.len() as u32).to_le_bytes()); + hasher.update(entry.as_slice()); + } + let digest: [u8; 32] = hasher.finalize().into(); + let mut data = Vec::with_capacity(1 + 32); + data.push(PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE); + data.extend_from_slice(&digest); + data +} + /// Extra sighash data of the credit pool fee bundle of an identity-less token pool transition: /// the state transition type, the token id and a digest of the token bundle's actions, so the /// fee bundle can only ever pay for that exact token bundle. @@ -1416,6 +1473,86 @@ mod tests { } } + mod private_document_create_from_shielded_pool { + use super::*; + use platform_value::BinaryData; + + fn data(contract: [u8; 32], type_name: &str, entries: &[BinaryData]) -> Vec { + private_document_create_from_shielded_pool_extra_sighash_data( + &contract, + type_name, + entries, + PlatformVersion::latest(), + ) + .expect("version 0") + } + + #[test] + fn should_lead_with_type_29_and_hash_the_length_prefixed_target_and_entries() { + let entries = vec![ + BinaryData::new(vec![1, 2, 3]), + BinaryData::new(vec![4, 5, 6]), + ]; + let mut preimage = vec![9u8; 32]; + preimage.extend_from_slice(&4u32.to_le_bytes()); + preimage.extend_from_slice(b"note"); + preimage.extend_from_slice(&2u32.to_le_bytes()); + for entry in &entries { + preimage.extend_from_slice(&3u32.to_le_bytes()); + preimage.extend_from_slice(entry.as_slice()); + } + let mut expected = vec![PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE]; + expected.extend_from_slice(&Sha256::digest(&preimage)); + assert_eq!(data([9; 32], "note", &entries), expected); + assert_eq!(expected[0], 29); + assert_eq!(expected.len(), 33); + } + + #[test] + fn should_change_with_the_contract_the_type_an_entry_byte_and_the_entries_order_and_split() + { + let a = BinaryData::new(vec![1, 2]); + let b = BinaryData::new(vec![3, 4]); + let base = data([9; 32], "note", &[a.clone(), b.clone()]); + assert_ne!(base, data([8; 32], "note", &[a.clone(), b.clone()])); + assert_ne!(base, data([9; 32], "notes", &[a.clone(), b.clone()])); + assert_ne!( + base, + data([9; 32], "note", &[a.clone(), BinaryData::new(vec![3, 5])]) + ); + assert_ne!(base, data([9; 32], "note", &[b.clone(), a.clone()])); + // The same bytes split into a different number of entries. + assert_ne!( + base, + data([9; 32], "note", &[BinaryData::new(vec![1, 2, 3, 4])]) + ); + // The type name's length prefix keeps it apart from the entries. + assert_ne!( + data([9; 32], "ab", &[BinaryData::new(vec![1])]), + data([9; 32], "a", &[BinaryData::new(vec![b'b', 1])]) + ); + } + + #[test] + fn should_not_collide_with_an_outputs_only_bundle_tag() { + assert!( + StateTransitionType::try_from(PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE) + .is_ok_and(|t| t == StateTransitionType::PrivateDocumentCreateFromShieldedPool) + ); + for tag in [ + TOKEN_SHIELD_BUNDLE_TAG, + TOKEN_MINT_TO_POOL_BUNDLE_TAG, + TOKEN_CLAIM_TO_POOL_BUNDLE_TAG, + TOKEN_DIRECT_PURCHASE_TO_POOL_BUNDLE_TAG, + SHIELD_BUNDLE_TAG, + SHIELD_FROM_IDENTITY_BUNDLE_TAG, + SHIELD_FROM_ASSET_LOCK_BUNDLE_TAG, + ] { + assert_ne!(tag, PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_TYPE); + } + } + } + mod credit_pool_outputs_only { use super::*; use crate::identity::state_transition::asset_lock_proof::chain::ChainAssetLockProof; diff --git a/packages/rs-dpp/src/state_transition/mod.rs b/packages/rs-dpp/src/state_transition/mod.rs index 0772907f04e..df55059a7b5 100644 --- a/packages/rs-dpp/src/state_transition/mod.rs +++ b/packages/rs-dpp/src/state_transition/mod.rs @@ -19,7 +19,10 @@ use dashcore::signer::double_sha; use platform_serialization_derive::{ PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, PlatformSignable, }; -use platform_version::version::feature_initial_protocol_versions::YES_NO_VOTE_POLL_INITIAL_PROTOCOL_VERSION; +use platform_version::version::feature_initial_protocol_versions::{ + PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, + YES_NO_VOTE_POLL_INITIAL_PROTOCOL_VERSION, +}; use platform_version::version::{PlatformVersion, ProtocolVersion, ALL_VERSIONS, LATEST_VERSION}; #[cfg(any( @@ -165,6 +168,10 @@ use crate::state_transition::identity_update_transition::{ use crate::state_transition::masternode_vote_transition::accessors::MasternodeVoteTransitionAccessorsV0; use crate::state_transition::masternode_vote_transition::MasternodeVoteTransition; use crate::state_transition::masternode_vote_transition::MasternodeVoteTransitionSignable; +use crate::state_transition::private_document_create_from_shielded_pool_transition::{ + PrivateDocumentCreateFromShieldedPoolTransition, + PrivateDocumentCreateFromShieldedPoolTransitionSignable, +}; use crate::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; use crate::state_transition::shield_from_asset_lock_transition::{ ShieldFromAssetLockTransition, ShieldFromAssetLockTransitionSignable, @@ -231,6 +238,7 @@ macro_rules! call_method { StateTransition::TokenShieldedTransferWithShieldedFee(st) => st.$method($args), StateTransition::TokenUnshieldWithShieldedFee(st) => st.$method($args), StateTransition::TokenPurchaseFromShieldedPool(st) => st.$method($args), + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => st.$method($args), StateTransition::ShieldFromAssetLock(st) => st.$method($args), StateTransition::ShieldedWithdrawal(st) => st.$method($args), StateTransition::IdentityCreateFromShieldedPool(st) => st.$method($args), @@ -264,6 +272,7 @@ macro_rules! call_method { StateTransition::TokenShieldedTransferWithShieldedFee(st) => st.$method(), StateTransition::TokenUnshieldWithShieldedFee(st) => st.$method(), StateTransition::TokenPurchaseFromShieldedPool(st) => st.$method(), + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => st.$method(), StateTransition::ShieldFromAssetLock(st) => st.$method(), StateTransition::ShieldedWithdrawal(st) => st.$method(), StateTransition::IdentityCreateFromShieldedPool(st) => st.$method(), @@ -300,6 +309,7 @@ macro_rules! call_getter_method_identity_signed { StateTransition::TokenShieldedTransferWithShieldedFee(_) => None, StateTransition::TokenUnshieldWithShieldedFee(_) => None, StateTransition::TokenPurchaseFromShieldedPool(_) => None, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => None, StateTransition::ShieldFromAssetLock(_) => None, StateTransition::ShieldedWithdrawal(_) => None, StateTransition::IdentityCreateFromShieldedPool(_) => None, @@ -333,6 +343,7 @@ macro_rules! call_getter_method_identity_signed { StateTransition::TokenShieldedTransferWithShieldedFee(_) => None, StateTransition::TokenUnshieldWithShieldedFee(_) => None, StateTransition::TokenPurchaseFromShieldedPool(_) => None, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => None, StateTransition::ShieldFromAssetLock(_) => None, StateTransition::ShieldedWithdrawal(_) => None, StateTransition::IdentityCreateFromShieldedPool(_) => None, @@ -369,6 +380,7 @@ macro_rules! call_method_identity_signed { StateTransition::TokenShieldedTransferWithShieldedFee(_) => {} StateTransition::TokenUnshieldWithShieldedFee(_) => {} StateTransition::TokenPurchaseFromShieldedPool(_) => {} + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => {} StateTransition::ShieldFromAssetLock(_) => {} StateTransition::ShieldedWithdrawal(_) => {} StateTransition::IdentityCreateFromShieldedPool(_) => {} @@ -402,6 +414,7 @@ macro_rules! call_method_identity_signed { StateTransition::TokenShieldedTransferWithShieldedFee(_) => {} StateTransition::TokenUnshieldWithShieldedFee(_) => {} StateTransition::TokenPurchaseFromShieldedPool(_) => {} + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => {} StateTransition::ShieldFromAssetLock(_) => {} StateTransition::ShieldedWithdrawal(_) => {} StateTransition::IdentityCreateFromShieldedPool(_) => {} @@ -467,6 +480,9 @@ macro_rules! call_errorable_method_identity_signed { StateTransition::TokenPurchaseFromShieldedPool(_) => Err(ProtocolError::CorruptedCodeExecution( "token purchase from shielded pool transition can not be called for identity signing".to_string(), )), + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => Err(ProtocolError::CorruptedCodeExecution( + "private document create from shielded pool transition can not be called for identity signing".to_string(), + )), StateTransition::ShieldFromAssetLock(_) => Err(ProtocolError::CorruptedCodeExecution( "shield from asset lock transition can not be called for identity signing".to_string(), )), @@ -534,6 +550,9 @@ macro_rules! call_errorable_method_identity_signed { StateTransition::TokenPurchaseFromShieldedPool(_) => Err(ProtocolError::CorruptedCodeExecution( "token purchase from shielded pool transition can not be called for identity signing".to_string(), )), + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => Err(ProtocolError::CorruptedCodeExecution( + "private document create from shielded pool transition can not be called for identity signing".to_string(), + )), StateTransition::ShieldFromAssetLock(_) => Err(ProtocolError::CorruptedCodeExecution( "shield from asset lock transition can not be called for identity signing".to_string(), )), @@ -646,6 +665,7 @@ pub enum StateTransition { TokenShieldedTransferWithShieldedFee(TokenShieldedTransferWithShieldedFeeTransition), TokenUnshieldWithShieldedFee(TokenUnshieldWithShieldedFeeTransition), TokenPurchaseFromShieldedPool(TokenPurchaseFromShieldedPoolTransition), + PrivateDocumentCreateFromShieldedPool(PrivateDocumentCreateFromShieldedPoolTransition), } #[cfg(all(feature = "json-conversion", feature = "serde-conversion"))] @@ -993,6 +1013,14 @@ mod json_convertible_tests { "tokenPurchaseFromShieldedPool", ); } + #[test] + fn umbrella_private_document_create_from_shielded_pool() { + let inner = crate::state_transition::private_document_create_from_shielded_pool_transition::json_convertible_tests::fixture(); + assert_umbrella_round_trip( + StateTransition::PrivateDocumentCreateFromShieldedPool(inner), + "privateDocumentCreateFromShieldedPool", + ); + } #[test] fn umbrella_shield_from_identity() { @@ -1288,6 +1316,9 @@ impl StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) => 14..=LATEST_VERSION, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => { + PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION..=LATEST_VERSION + } } } @@ -1306,6 +1337,7 @@ impl StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) ) } @@ -1439,6 +1471,9 @@ impl StateTransition { } Self::TokenUnshieldWithShieldedFee(_) => "TokenUnshieldWithShieldedFee".to_string(), Self::TokenPurchaseFromShieldedPool(_) => "TokenPurchaseFromShieldedPool".to_string(), + Self::PrivateDocumentCreateFromShieldedPool(_) => { + "PrivateDocumentCreateFromShieldedPool".to_string() + } Self::ShieldFromAssetLock(_) => "ShieldFromAssetLock".to_string(), Self::ShieldedWithdrawal(_) => "ShieldedWithdrawal".to_string(), Self::IdentityCreateFromShieldedPool(_) => "IdentityCreateFromShieldedPool".to_string(), @@ -1474,6 +1509,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => None, StateTransition::TokenUnshieldWithShieldedFee(_) => None, StateTransition::TokenPurchaseFromShieldedPool(_) => None, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => None, StateTransition::ShieldFromAssetLock(st) => Some(st.signature()), StateTransition::ShieldedWithdrawal(_) => None, StateTransition::IdentityCreateFromShieldedPool(_) => None, @@ -1495,6 +1531,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => 0, StateTransition::TokenUnshieldWithShieldedFee(_) => 0, StateTransition::TokenPurchaseFromShieldedPool(_) => 0, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => 0, StateTransition::ShieldFromAssetLock(_) => 0, StateTransition::ShieldedWithdrawal(_) => 0, StateTransition::IdentityCreateFromShieldedPool(_) => 0, @@ -1532,6 +1569,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => 0, StateTransition::TokenUnshieldWithShieldedFee(_) => 0, StateTransition::TokenPurchaseFromShieldedPool(_) => 0, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => 0, StateTransition::ShieldedWithdrawal(_) => 0, StateTransition::IdentityCreateFromShieldedPool(_) => 0, StateTransition::ShieldFromIdentity(st) => st.user_fee_increase(), @@ -1607,6 +1645,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => None, StateTransition::TokenUnshieldWithShieldedFee(_) => None, StateTransition::TokenPurchaseFromShieldedPool(_) => None, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => None, StateTransition::ShieldFromAssetLock(_) => None, StateTransition::ShieldedWithdrawal(_) => None, StateTransition::IdentityCreateFromShieldedPool(_) => None, @@ -1642,6 +1681,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => None, StateTransition::TokenUnshieldWithShieldedFee(_) => None, StateTransition::TokenPurchaseFromShieldedPool(_) => None, + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => None, StateTransition::ShieldFromAssetLock(_) => None, StateTransition::ShieldedWithdrawal(_) => None, StateTransition::IdentityCreateFromShieldedPool(_) => None, @@ -1725,6 +1765,7 @@ impl StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::IdentityTopUpFromShieldedPool(_) => false, StateTransition::ShieldFromIdentity(st) => { st.set_signature(signature); @@ -1792,6 +1833,7 @@ impl StateTransition { StateTransition::TokenShieldedTransferWithShieldedFee(_) => {} StateTransition::TokenUnshieldWithShieldedFee(_) => {} StateTransition::TokenPurchaseFromShieldedPool(_) => {} + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => {} StateTransition::ShieldedWithdrawal(_) => {} StateTransition::IdentityCreateFromShieldedPool(_) => {} StateTransition::ShieldFromIdentity(st) => st.set_user_fee_increase(user_fee_increase), @@ -1991,6 +2033,11 @@ impl StateTransition { "token purchase from shielded pool transition can not be called for identity signing".to_string(), )) } + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => { + return Err(ProtocolError::CorruptedCodeExecution( + "private document create from shielded pool transition can not be called for identity signing".to_string(), + )) + } StateTransition::ShieldFromAssetLock(_) => { return Err(ProtocolError::CorruptedCodeExecution( "shield from asset lock transition can not be called for identity signing" @@ -2506,6 +2553,9 @@ impl StateTransitionStructureValidation for StateTransition { StateTransition::TokenPurchaseFromShieldedPool(transition) => { transition.validate_structure(platform_version) } + StateTransition::PrivateDocumentCreateFromShieldedPool(transition) => { + transition.validate_structure(platform_version) + } StateTransition::ShieldFromAssetLock(transition) => { transition.validate_structure(platform_version) } diff --git a/packages/rs-dpp/src/state_transition/serialization.rs b/packages/rs-dpp/src/state_transition/serialization.rs index d27693bd06b..52abae112d6 100644 --- a/packages/rs-dpp/src/state_transition/serialization.rs +++ b/packages/rs-dpp/src/state_transition/serialization.rs @@ -138,6 +138,12 @@ impl StateTransition { )? .into() } + StateTransitionType::PrivateDocumentCreateFromShieldedPool => { + PrivateDocumentCreateFromShieldedPoolTransition::deserialize_from_bytes_untrusted_exact( + bytes, + )? + .into() + } StateTransitionType::IdentityKeyLimitsUpdate => { IdentityKeyLimitsUpdateTransition::deserialize_from_bytes_untrusted_exact(bytes)? .into() diff --git a/packages/rs-dpp/src/state_transition/state_transition_types.rs b/packages/rs-dpp/src/state_transition/state_transition_types.rs index 36245d0eb52..56dc89943b4 100644 --- a/packages/rs-dpp/src/state_transition/state_transition_types.rs +++ b/packages/rs-dpp/src/state_transition/state_transition_types.rs @@ -50,6 +50,7 @@ pub enum StateTransitionType { TokenShieldedTransferWithShieldedFee = 26, TokenUnshieldWithShieldedFee = 27, TokenPurchaseFromShieldedPool = 28, + PrivateDocumentCreateFromShieldedPool = 29, } impl std::fmt::Display for StateTransitionType { @@ -161,6 +162,10 @@ mod tests { StateTransitionType::TokenPurchaseFromShieldedPool, "TokenPurchaseFromShieldedPool", ), + ( + StateTransitionType::PrivateDocumentCreateFromShieldedPool, + "PrivateDocumentCreateFromShieldedPool", + ), ]; for (variant, expected) in cases { assert_eq!( @@ -207,6 +212,10 @@ mod tests { ), (27, StateTransitionType::TokenUnshieldWithShieldedFee), (28, StateTransitionType::TokenPurchaseFromShieldedPool), + ( + 29, + StateTransitionType::PrivateDocumentCreateFromShieldedPool, + ), ]; for (val, expected) in pairs { let result = StateTransitionType::try_from(val).unwrap(); @@ -216,7 +225,7 @@ mod tests { #[test] fn test_try_from_u8_invalid() { - assert!(StateTransitionType::try_from(29u8).is_err()); + assert!(StateTransitionType::try_from(30u8).is_err()); assert!(StateTransitionType::try_from(255u8).is_err()); } @@ -252,6 +261,7 @@ mod tests { StateTransitionType::TokenShieldedTransferWithShieldedFee, StateTransitionType::TokenUnshieldWithShieldedFee, StateTransitionType::TokenPurchaseFromShieldedPool, + StateTransitionType::PrivateDocumentCreateFromShieldedPool, ]; for variant in all_variants { let val: u8 = variant.into(); diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/mod.rs index fbeb5e3b420..34b1bc20147 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/shielded/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/mod.rs @@ -1,6 +1,7 @@ pub mod common_validation; pub mod identity_create_from_shielded_pool_transition; pub mod identity_top_up_from_shielded_pool_transition; +pub mod private_document_create_from_shielded_pool_transition; pub mod shield_from_asset_lock_transition; pub mod shield_from_identity_transition; pub mod shield_transition; diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/mod.rs new file mode 100644 index 00000000000..d97198eda12 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/mod.rs @@ -0,0 +1,115 @@ +mod v0; + +pub use v0::*; + +use crate::shielded::SerializedAction; +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use platform_value::{BinaryData, Identifier}; + +impl PrivateDocumentCreateFromShieldedPoolTransitionAccessorsV0 + for PrivateDocumentCreateFromShieldedPoolTransition +{ + fn data_contract_id(&self) -> Identifier { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.data_contract_id, + } + } + + fn set_data_contract_id(&mut self, data_contract_id: Identifier) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + v0.data_contract_id = data_contract_id + } + } + } + + fn document_type_name(&self) -> &str { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => &v0.document_type_name, + } + } + + fn set_document_type_name(&mut self, document_type_name: String) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + v0.document_type_name = document_type_name + } + } + } + + fn entries(&self) -> &[BinaryData] { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => &v0.entries, + } + } + + fn set_entries(&mut self, entries: Vec) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.entries = entries, + } + } + + fn actions(&self) -> &[SerializedAction] { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => &v0.actions, + } + } + + fn set_actions(&mut self, actions: Vec) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.actions = actions, + } + } + + fn value_balance(&self) -> u64 { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.value_balance, + } + } + + fn set_value_balance(&mut self, value_balance: u64) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + v0.value_balance = value_balance + } + } + } + + fn anchor(&self) -> [u8; 32] { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.anchor, + } + } + + fn set_anchor(&mut self, anchor: [u8; 32]) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.anchor = anchor, + } + } + + fn proof(&self) -> &[u8] { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => &v0.proof, + } + } + + fn set_proof(&mut self, proof: Vec) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.proof = proof, + } + } + + fn binding_signature(&self) -> [u8; 64] { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.binding_signature, + } + } + + fn set_binding_signature(&mut self, binding_signature: [u8; 64]) { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + v0.binding_signature = binding_signature + } + } + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/v0/mod.rs new file mode 100644 index 00000000000..aa848ffcc44 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/accessors/v0/mod.rs @@ -0,0 +1,53 @@ +use crate::shielded::SerializedAction; +use platform_value::{BinaryData, Identifier}; + +/// Accessors for the fields of a `PrivateDocumentCreateFromShieldedPoolTransition`. +pub trait PrivateDocumentCreateFromShieldedPoolTransitionAccessorsV0 { + /// The contract that defines the private document type. + fn data_contract_id(&self) -> Identifier; + /// Set the contract. + fn set_data_contract_id(&mut self, data_contract_id: Identifier); + + /// The private document type the entries are appended to. + fn document_type_name(&self) -> &str; + /// Set the document type name. + fn set_document_type_name(&mut self, document_type_name: String); + + /// The entries, in the order they are appended. + fn entries(&self) -> &[BinaryData]; + /// Replace the entries. + fn set_entries(&mut self, entries: Vec); + + /// Get the serialized Orchard actions (spend/output pairs). + fn actions(&self) -> &[SerializedAction]; + /// Replace the serialized Orchard actions. + fn set_actions(&mut self, actions: Vec); + + /// Credits leaving the credit pool: the bundle's value balance, which is the fee. + fn value_balance(&self) -> u64; + /// Set the value balance. + fn set_value_balance(&mut self, value_balance: u64); + + /// Get the Orchard anchor (Sinsemilla root of the note commitment tree). + fn anchor(&self) -> [u8; 32]; + /// Set the Orchard anchor. + fn set_anchor(&mut self, anchor: [u8; 32]); + + /// Get the Halo2 proof bytes. + fn proof(&self) -> &[u8]; + /// Set the Halo2 proof bytes. + fn set_proof(&mut self, proof: Vec); + + /// Get the RedPallas binding signature. + fn binding_signature(&self) -> [u8; 64]; + /// Set the RedPallas binding signature. + fn set_binding_signature(&mut self, binding_signature: [u8; 64]); + + /// Extract nullifier bytes from each action. + fn nullifiers>(&self) -> Vec { + self.actions() + .iter() + .map(|a| N::from(a.nullifier)) + .collect() + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/mod.rs new file mode 100644 index 00000000000..d591f314da3 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/mod.rs @@ -0,0 +1,61 @@ +mod v0; + +pub use v0::*; + +#[cfg(feature = "state-transition-signing")] +use crate::shielded::SerializedAction; +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +#[cfg(feature = "state-transition-signing")] +use crate::{ + state_transition::{ + private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0, + StateTransition, + }, + ProtocolError, +}; +#[cfg(feature = "state-transition-signing")] +use platform_value::{BinaryData, Identifier}; +#[cfg(feature = "state-transition-signing")] +use platform_version::version::PlatformVersion; + +impl PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0 + for PrivateDocumentCreateFromShieldedPoolTransition +{ + #[cfg(feature = "state-transition-signing")] + fn try_from_bundle( + data_contract_id: Identifier, + document_type_name: String, + entries: Vec, + actions: Vec, + value_balance: u64, + anchor: [u8; 32], + proof: Vec, + binding_signature: [u8; 64], + platform_version: &PlatformVersion, + ) -> Result { + match platform_version + .dpp + .state_transition_serialization_versions + .private_document_create_from_shielded_pool_state_transition + .default_current_version + { + 0 => PrivateDocumentCreateFromShieldedPoolTransitionV0::try_from_bundle( + data_contract_id, + document_type_name, + entries, + actions, + value_balance, + anchor, + proof, + binding_signature, + platform_version, + ), + version => Err(ProtocolError::UnknownVersionMismatch { + method: "PrivateDocumentCreateFromShieldedPoolTransition::try_from_bundle" + .to_string(), + known_versions: vec![0], + received: version, + }), + } + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/v0/mod.rs new file mode 100644 index 00000000000..e6b7a876288 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/methods/v0/mod.rs @@ -0,0 +1,35 @@ +#[cfg(feature = "state-transition-signing")] +use crate::shielded::SerializedAction; +use crate::state_transition::StateTransitionType; +#[cfg(feature = "state-transition-signing")] +use crate::{state_transition::StateTransition, ProtocolError}; +#[cfg(feature = "state-transition-signing")] +use platform_value::{BinaryData, Identifier}; +#[cfg(feature = "state-transition-signing")] +use platform_version::version::PlatformVersion; + +pub trait PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0 { + /// Build the transition from an already proven and signed Orchard spend bundle. The + /// bundle's binding signature must have been computed over the platform sighash that binds + /// the contract, the type name and the entries (see + /// `private_document_create_from_shielded_pool_extra_sighash_data`), and its value balance + /// must be the fee (`compute_private_document_create_from_shielded_pool_fee`). + #[cfg(feature = "state-transition-signing")] + #[allow(clippy::too_many_arguments)] + fn try_from_bundle( + data_contract_id: Identifier, + document_type_name: String, + entries: Vec, + actions: Vec, + value_balance: u64, + anchor: [u8; 32], + proof: Vec, + binding_signature: [u8; 64], + platform_version: &PlatformVersion, + ) -> Result; + + /// Get State Transition Type + fn get_type() -> StateTransitionType { + StateTransitionType::PrivateDocumentCreateFromShieldedPool + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/mod.rs new file mode 100644 index 00000000000..870284b8642 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/mod.rs @@ -0,0 +1,152 @@ +pub mod accessors; +pub mod methods; +mod state_transition_estimated_fee_validation; +mod state_transition_like; +mod state_transition_validation; +pub mod v0; +mod version; + +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0Signable; +use crate::state_transition::StateTransitionFieldTypes; + +pub type PrivateDocumentCreateFromShieldedPoolTransitionLatest = + PrivateDocumentCreateFromShieldedPoolTransitionV0; + +use crate::identity::state_transition::OptionallyAssetLockProved; +#[cfg(feature = "json-conversion")] +use crate::serialization::JsonConvertible; +#[cfg(feature = "value-conversion")] +use crate::serialization::ValueConvertible; +use crate::ProtocolError; +use bincode::{Decode, DecodeUntrusted, Encode}; +use derive_more::From; +use platform_serialization_derive::{ + PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, PlatformSignable, +}; +use platform_versioning::PlatformVersioned; +#[cfg(feature = "serde-conversion")] +use serde::{Deserialize, Serialize}; + +/// Appends entries to a private document type's store, paid from the credit shielded pool +/// (protocol version 15). +/// +/// The entries are opaque bytes of the type's `entrySize` that the chain never reads. The fee +/// is paid by an Orchard spend bundle in the credit pool whose value balance is exactly the +/// fee, so no identity signs and nothing names the sender. The target contract, type and every +/// entry are bound into the bundle's sighash, so a relay can not move the payment to other +/// entries. +#[derive( + Debug, + Clone, + Encode, + Decode, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + PlatformSerialize, + PlatformSignable, + PlatformVersioned, + From, + PartialEq, + DecodeUntrusted, +)] +#[cfg_attr( + feature = "serde-conversion", + derive(Serialize, Deserialize), + serde(tag = "$formatVersion") +)] +#[cfg_attr( + all(feature = "json-conversion", feature = "serde-conversion"), + derive(JsonConvertible) +)] +#[cfg_attr(feature = "value-conversion", derive(ValueConvertible))] +#[platform_serialize(unversioned)] //versioned directly, no need to use platform_version +#[platform_version_path_bounds( + "dpp.state_transition_serialization_versions.private_document_create_from_shielded_pool_state_transition" +)] +pub enum PrivateDocumentCreateFromShieldedPoolTransition { + #[cfg_attr(feature = "serde-conversion", serde(rename = "0"))] + V0(PrivateDocumentCreateFromShieldedPoolTransitionV0), +} + +impl OptionallyAssetLockProved for PrivateDocumentCreateFromShieldedPoolTransition {} + +impl StateTransitionFieldTypes for PrivateDocumentCreateFromShieldedPoolTransition { + fn signature_property_paths() -> Vec<&'static str> { + vec![] + } + + fn identifiers_property_paths() -> Vec<&'static str> { + vec![] + } + + fn binary_property_paths() -> Vec<&'static str> { + vec![] + } +} + +#[cfg(all( + test, + feature = "json-conversion", + feature = "value-conversion", + feature = "serde-conversion" +))] +pub(crate) mod json_convertible_tests { + use super::*; + use crate::shielded::SerializedAction; + use platform_value::{BinaryData, Identifier}; + + fn fixture_action() -> SerializedAction { + SerializedAction { + nullifier: [0x11; 32], + rk: [0x22; 32], + cmx: [0x33; 32], + encrypted_note: vec![0x44; 216], + cv_net: [0x55; 32], + spend_auth_sig: [0x66; 64], + } + } + + pub(crate) fn fixture() -> PrivateDocumentCreateFromShieldedPoolTransition { + PrivateDocumentCreateFromShieldedPoolTransition::V0( + PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id: Identifier::new([0x10; 32]), + document_type_name: "privateContactRequest".to_string(), + entries: vec![ + BinaryData::new(vec![0xab; 24]), + BinaryData::new(vec![0xcd; 24]), + ], + actions: vec![fixture_action()], + value_balance: 131_000_007, + anchor: [0x77; 32], + proof: vec![0x88; 192], + binding_signature: [0x99; 64], + }, + ) + } + + #[test] + fn should_round_trip_through_json() { + use crate::serialization::JsonConvertible; + let original = fixture(); + let json = original.to_json().expect("to_json"); + assert_eq!(json["$formatVersion"], "0"); + assert_eq!(json["documentTypeName"], "privateContactRequest"); + assert_eq!(json["valueBalance"], 131_000_007); + // Entries are base64 strings in JSON, as every other byte field is. + assert!(json["entries"][0].is_string(), "{json}"); + let recovered = + PrivateDocumentCreateFromShieldedPoolTransition::from_json(json).expect("from_json"); + assert_eq!(original, recovered); + } + + #[test] + fn should_round_trip_through_a_value() { + use crate::serialization::ValueConvertible; + let original = fixture(); + let value = original.to_object().expect("to_object"); + let recovered = PrivateDocumentCreateFromShieldedPoolTransition::from_object(value) + .expect("from_object"); + assert_eq!(original, recovered); + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_estimated_fee_validation.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_estimated_fee_validation.rs new file mode 100644 index 00000000000..6f46da05465 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_estimated_fee_validation.rs @@ -0,0 +1,16 @@ +use crate::fee::Credits; +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::state_transition::StateTransitionEstimatedFeeValidation; +use crate::ProtocolError; +use platform_version::version::PlatformVersion; + +impl StateTransitionEstimatedFeeValidation for PrivateDocumentCreateFromShieldedPoolTransition { + /// Pool-paid: the fee is the bundle's value balance, which the shielded minimum-fee + /// validation holds to the exact fee, not an identity balance floor. + fn calculate_min_required_fee( + &self, + _platform_version: &PlatformVersion, + ) -> Result { + Ok(0) + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_like.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_like.rs new file mode 100644 index 00000000000..2c1f522ffcb --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_like.rs @@ -0,0 +1,36 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::state_transition::{StateTransitionLike, StateTransitionType}; +use crate::version::FeatureVersion; +use platform_value::Identifier; + +impl StateTransitionLike for PrivateDocumentCreateFromShieldedPoolTransition { + fn modified_data_ids(&self) -> Vec { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(transition) => { + transition.modified_data_ids() + } + } + } + + fn state_transition_protocol_version(&self) -> FeatureVersion { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(_) => 0, + } + } + + fn state_transition_type(&self) -> StateTransitionType { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(transition) => { + transition.state_transition_type() + } + } + } + + fn unique_identifiers(&self) -> Vec { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(transition) => { + transition.unique_identifiers() + } + } + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_validation.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_validation.rs new file mode 100644 index 00000000000..148960ef0ea --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/state_transition_validation.rs @@ -0,0 +1,17 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::state_transition::StateTransitionStructureValidation; +use crate::validation::SimpleConsensusValidationResult; +use platform_version::version::PlatformVersion; + +impl StateTransitionStructureValidation for PrivateDocumentCreateFromShieldedPoolTransition { + fn validate_structure( + &self, + platform_version: &PlatformVersion, + ) -> SimpleConsensusValidationResult { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + v0.validate_structure(platform_version) + } + } + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs new file mode 100644 index 00000000000..9916195209f --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs @@ -0,0 +1,105 @@ +mod state_transition_like; +mod state_transition_validation; +mod types; +pub(super) mod v0_methods; +mod version; + +use crate::prelude::Identifier; +use crate::shielded::SerializedAction; +use crate::ProtocolError; +use bincode::{Decode, DecodeUntrusted, Encode}; +use platform_serialization_derive::{ + PlatformDeserializeTrusted, PlatformDeserializeUntrusted, PlatformSerialize, PlatformSignable, +}; +use platform_value::BinaryData; +#[cfg(feature = "serde-conversion")] +use serde::{Deserialize, Serialize}; + +/// Entries appended to a private document type's store, paid from the credit shielded pool, +/// version 0. +/// +/// No platform signature: the Orchard proof and spend-auth signatures authorize the payment, +/// and the contract, the type name and every entry are committed into the Orchard binding +/// sighash (`private_document_create_from_shielded_pool_extra_sighash_data`), so the payment +/// can not be moved to other entries. +#[cfg_attr(feature = "json-conversion", crate::serialization::json_safe_fields)] +#[derive( + Debug, + Clone, + Encode, + Decode, + PlatformSerialize, + PlatformDeserializeTrusted, + PlatformDeserializeUntrusted, + PlatformSignable, + PartialEq, + DecodeUntrusted, +)] +#[cfg_attr( + feature = "serde-conversion", + derive(Serialize, Deserialize), + serde(rename_all = "camelCase") +)] +#[platform_serialize(unversioned)] +pub struct PrivateDocumentCreateFromShieldedPoolTransitionV0 { + /// The contract that defines the private document type. + pub data_contract_id: Identifier, + /// The private document type the entries are appended to. + pub document_type_name: String, + /// The entries, appended in this order. Each takes exactly the type's `entrySize`. + pub entries: Vec, + /// Orchard actions (spend-output pairs) of the credit pool bundle that pays the fee. + pub actions: Vec, + /// Credits leaving the credit pool, the bundle's value balance: exactly the fee + /// (`compute_private_document_create_from_shielded_pool_fee`). + pub value_balance: u64, + /// Sinsemilla root of the note commitment tree the bundle was built against. + pub anchor: [u8; 32], + /// Halo 2 proof of the bundle. + pub proof: Vec, + /// RedPallas binding signature of the bundle. + #[cfg_attr( + all(feature = "serde-conversion", not(feature = "json-conversion")), + serde(with = "crate::serialization::serde_bytes") + )] + pub binding_signature: [u8; 64], +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::serialization::{PlatformDeserializableUntrusted, PlatformSerializable}; + + #[test] + fn should_round_trip_through_platform_serialization() { + let transition = PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id: Identifier::from([7u8; 32]), + document_type_name: "privateContactRequest".to_string(), + entries: vec![ + BinaryData::new(vec![1u8; 585]), + BinaryData::new(vec![2u8; 585]), + ], + actions: vec![SerializedAction { + nullifier: [1u8; 32], + rk: [2u8; 32], + cmx: [3u8; 32], + encrypted_note: vec![4u8; 216], + cv_net: [5u8; 32], + spend_auth_sig: [6u8; 64], + }], + value_balance: 1000u64, + anchor: [7u8; 32], + proof: vec![8u8; 100], + binding_signature: [9u8; 64], + }; + let serialized = transition + .serialize_to_bytes() + .expect("expected to serialize"); + let deserialized = + PrivateDocumentCreateFromShieldedPoolTransitionV0::deserialize_from_bytes_untrusted( + serialized.as_slice(), + ) + .expect("expected to deserialize"); + assert_eq!(transition, deserialized); + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_like.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_like.rs new file mode 100644 index 00000000000..932a3930ea9 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_like.rs @@ -0,0 +1,41 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::{ + prelude::Identifier, + state_transition::{StateTransitionLike, StateTransitionType}, +}; + +use crate::state_transition::StateTransition; +use crate::version::FeatureVersion; + +impl From for StateTransition { + fn from(value: PrivateDocumentCreateFromShieldedPoolTransitionV0) -> Self { + let transition: PrivateDocumentCreateFromShieldedPoolTransition = value.into(); + transition.into() + } +} + +impl StateTransitionLike for PrivateDocumentCreateFromShieldedPoolTransitionV0 { + fn state_transition_protocol_version(&self) -> FeatureVersion { + 0 + } + + fn state_transition_type(&self) -> StateTransitionType { + StateTransitionType::PrivateDocumentCreateFromShieldedPool + } + + /// No identity or document is modified: the entries are appended to the type's store, + /// which takes any number of them. + fn modified_data_ids(&self) -> Vec { + vec![] + } + + /// Unique by spent nullifier, as every pool-paid transition: two transitions spending the + /// same note can never both be valid. + fn unique_identifiers(&self) -> Vec { + self.actions + .iter() + .map(|action| hex::encode(action.nullifier)) + .collect() + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs new file mode 100644 index 00000000000..8d211d52192 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs @@ -0,0 +1,311 @@ +use crate::consensus::basic::document::{ + DuplicatePrivateDocumentEntryError, InvalidPrivateDocumentEntryCountError, + InvalidPrivateDocumentEntrySizeError, +}; +use crate::consensus::basic::state_transition::ShieldedInvalidValueBalanceError; +use crate::consensus::basic::BasicError; +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use crate::state_transition::state_transitions::shielded::common_validation::{ + validate_actions_count, validate_anchor_not_zero, validate_encrypted_note_sizes, + validate_proof_not_empty, +}; +use crate::state_transition::StateTransitionStructureValidation; +use crate::validation::SimpleConsensusValidationResult; +use platform_version::version::PlatformVersion; +use std::collections::BTreeMap; + +impl StateTransitionStructureValidation for PrivateDocumentCreateFromShieldedPoolTransitionV0 { + /// Checks what needs no state: from 1 to `max_private_document_entries_per_transition` + /// entries, each as long as the first and from 1 to `max_private_document_entry_size` + /// bytes, no two the same, then the bundle's actions, value balance, proof and anchor. Whether the entries + /// take the type's `entrySize` needs the contract, so the transform checks it. + fn validate_structure( + &self, + platform_version: &PlatformVersion, + ) -> SimpleConsensusValidationResult { + let limits = &platform_version.system_limits; + // Both limits are `None` before protocol version 15, where the transition is refused + // before its structure is validated; refusing every entry there keeps it so. + let max_count = limits + .max_private_document_entries_per_transition + .unwrap_or(0); + let max_size = limits.max_private_document_entry_size.unwrap_or(0); + + if self.entries.is_empty() || self.entries.len() > max_count as usize { + return SimpleConsensusValidationResult::new_with_error( + InvalidPrivateDocumentEntryCountError::new(self.entries.len() as u64, max_count) + .into(), + ); + } + + let first_entry_size = self.entries[0].len(); + for (entry_index, entry) in self.entries.iter().enumerate() { + let size = entry.len(); + if size == 0 || size != first_entry_size || size > max_size as usize { + return SimpleConsensusValidationResult::new_with_error( + InvalidPrivateDocumentEntrySizeError::new( + // At most max_count (a u16) entries reach this loop. + entry_index as u16, + size.min(u32::MAX as usize) as u32, + first_entry_size.min(u32::MAX as usize) as u32, + max_size, + ) + .into(), + ); + } + } + + let mut first_index_of_entry = BTreeMap::new(); + for (entry_index, entry) in self.entries.iter().enumerate() { + if let Some(first_index) = + first_index_of_entry.insert(entry.as_slice(), entry_index as u16) + { + return SimpleConsensusValidationResult::new_with_error( + DuplicatePrivateDocumentEntryError::new(first_index, entry_index as u16).into(), + ); + } + } + + let result = validate_actions_count(&self.actions, limits.max_shielded_transition_actions); + if !result.is_valid() { + return result; + } + + let result = validate_encrypted_note_sizes(&self.actions); + if !result.is_valid() { + return result; + } + + if self.value_balance == 0 { + return SimpleConsensusValidationResult::new_with_error( + BasicError::ShieldedInvalidValueBalanceError( + ShieldedInvalidValueBalanceError::new( + "private document create value balance must be greater than zero" + .to_string(), + ), + ) + .into(), + ); + } + + if self.value_balance > i64::MAX as u64 { + return SimpleConsensusValidationResult::new_with_error( + BasicError::ShieldedInvalidValueBalanceError( + ShieldedInvalidValueBalanceError::new( + "private document create value balance exceeds maximum allowed value" + .to_string(), + ), + ) + .into(), + ); + } + + let result = validate_proof_not_empty(&self.proof); + if !result.is_valid() { + return result; + } + + validate_anchor_not_zero(&self.anchor) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::consensus::ConsensusError; + use crate::shielded::SerializedAction; + use assert_matches::assert_matches; + use platform_value::{BinaryData, Identifier}; + + fn action() -> SerializedAction { + SerializedAction { + nullifier: [1u8; 32], + rk: [2u8; 32], + cmx: [3u8; 32], + encrypted_note: vec![4u8; 216], + cv_net: [5u8; 32], + spend_auth_sig: [6u8; 64], + } + } + + fn valid() -> PrivateDocumentCreateFromShieldedPoolTransitionV0 { + PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id: Identifier::from([1u8; 32]), + document_type_name: "privateContactRequest".to_string(), + entries: vec![ + BinaryData::new(vec![7u8; 585]), + BinaryData::new(vec![8u8; 585]), + ], + actions: vec![action()], + value_balance: 1_000, + anchor: [7u8; 32], + proof: vec![8u8; 100], + binding_signature: [9u8; 64], + } + } + + fn entry_count_error( + t: &PrivateDocumentCreateFromShieldedPoolTransitionV0, + ) -> Option<(u64, u16)> { + match t + .validate_structure(PlatformVersion::latest()) + .errors + .as_slice() + { + [ConsensusError::BasicError(BasicError::InvalidPrivateDocumentEntryCountError(e))] => { + Some((e.count(), e.max_count())) + } + _ => None, + } + } + + fn entry_size_error( + t: &PrivateDocumentCreateFromShieldedPoolTransitionV0, + ) -> Option<(u16, u32)> { + match t + .validate_structure(PlatformVersion::latest()) + .errors + .as_slice() + { + [ConsensusError::BasicError(BasicError::InvalidPrivateDocumentEntrySizeError(e))] => { + Some((e.entry_index(), e.size())) + } + _ => None, + } + } + + #[test] + fn should_accept_a_well_formed_transition() { + let result = valid().validate_structure(PlatformVersion::latest()); + assert!(result.is_valid(), "{:?}", result.errors); + } + + #[test] + fn should_accept_the_most_entries_of_the_largest_size() { + let limits = &PlatformVersion::latest().system_limits; + let mut t = valid(); + t.entries = (0..limits.max_private_document_entries_per_transition.unwrap()) + .map(|i| { + BinaryData::new(vec![ + i as u8; + limits.max_private_document_entry_size.unwrap() as usize + ]) + }) + .collect(); + let result = t.validate_structure(PlatformVersion::latest()); + assert!(result.is_valid(), "{:?}", result.errors); + } + + #[test] + fn should_refuse_no_entry_and_one_entry_too_many() { + let max = PlatformVersion::latest() + .system_limits + .max_private_document_entries_per_transition + .unwrap(); + let mut t = valid(); + t.entries = vec![]; + assert_eq!(entry_count_error(&t), Some((0, max))); + t.entries = (0..=max) + .map(|i| BinaryData::new(vec![i as u8; 585])) + .collect(); + assert_eq!(entry_count_error(&t), Some((max as u64 + 1, max))); + } + + #[test] + fn should_refuse_an_empty_entry_an_entry_over_the_cap_and_entries_of_two_sizes() { + let max_size = PlatformVersion::latest() + .system_limits + .max_private_document_entry_size + .unwrap() as usize; + let mut t = valid(); + t.entries = vec![BinaryData::new(vec![])]; + assert_eq!(entry_size_error(&t), Some((0, 0))); + t.entries = vec![BinaryData::new(vec![1u8; max_size + 1])]; + assert_eq!(entry_size_error(&t), Some((0, max_size as u32 + 1))); + t.entries = vec![ + BinaryData::new(vec![1u8; 585]), + BinaryData::new(vec![2u8; 585]), + BinaryData::new(vec![1u8; 584]), + ]; + assert_eq!(entry_size_error(&t), Some((2, 584))); + } + + #[test] + fn should_refuse_two_entries_that_are_the_same_bytes() { + let mut t = valid(); + t.entries = vec![ + BinaryData::new(vec![1u8; 585]), + BinaryData::new(vec![2u8; 585]), + BinaryData::new(vec![1u8; 585]), + ]; + match t + .validate_structure(PlatformVersion::latest()) + .errors + .as_slice() + { + [ConsensusError::BasicError(BasicError::DuplicatePrivateDocumentEntryError(e))] => { + assert_eq!((e.first_index(), e.entry_index()), (0, 2)); + } + errors => panic!("expected a duplicate entry error, got {errors:?}"), + } + } + + #[test] + fn should_refuse_every_entry_before_protocol_version_15() { + let t = valid(); + let result = t.validate_structure(PlatformVersion::get(14).expect("protocol version 14")); + assert_matches!( + result.errors.as_slice(), + [ConsensusError::BasicError( + BasicError::InvalidPrivateDocumentEntryCountError(_) + )] + ); + } + + #[test] + fn should_refuse_no_actions_a_zero_or_oversized_value_balance_an_empty_proof_and_a_zero_anchor() + { + let mut t = valid(); + t.actions = vec![]; + assert_matches!( + t.validate_structure(PlatformVersion::latest()) + .errors + .as_slice(), + [ConsensusError::BasicError( + BasicError::ShieldedNoActionsError(_) + )] + ); + for value_balance in [0, i64::MAX as u64 + 1] { + let mut t = valid(); + t.value_balance = value_balance; + assert_matches!( + t.validate_structure(PlatformVersion::latest()) + .errors + .as_slice(), + [ConsensusError::BasicError( + BasicError::ShieldedInvalidValueBalanceError(_) + )] + ); + } + let mut t = valid(); + t.proof = vec![]; + assert_matches!( + t.validate_structure(PlatformVersion::latest()) + .errors + .as_slice(), + [ConsensusError::BasicError( + BasicError::ShieldedEmptyProofError(_) + )] + ); + let mut t = valid(); + t.anchor = [0u8; 32]; + assert_matches!( + t.validate_structure(PlatformVersion::latest()) + .errors + .as_slice(), + [ConsensusError::BasicError( + BasicError::ShieldedZeroAnchorError(_) + )] + ); + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/types.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/types.rs new file mode 100644 index 00000000000..9ba69a0dc59 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/types.rs @@ -0,0 +1,16 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use crate::state_transition::StateTransitionFieldTypes; + +impl StateTransitionFieldTypes for PrivateDocumentCreateFromShieldedPoolTransitionV0 { + fn signature_property_paths() -> Vec<&'static str> { + vec![] + } + + fn identifiers_property_paths() -> Vec<&'static str> { + vec![] + } + + fn binary_property_paths() -> Vec<&'static str> { + vec![] + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/v0_methods.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/v0_methods.rs new file mode 100644 index 00000000000..2f95facbf0b --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/v0_methods.rs @@ -0,0 +1,39 @@ +#[cfg(feature = "state-transition-signing")] +use crate::shielded::SerializedAction; +use crate::state_transition::private_document_create_from_shielded_pool_transition::methods::PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0; +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +#[cfg(feature = "state-transition-signing")] +use crate::{state_transition::StateTransition, ProtocolError}; +#[cfg(feature = "state-transition-signing")] +use platform_value::{BinaryData, Identifier}; +#[cfg(feature = "state-transition-signing")] +use platform_version::version::PlatformVersion; + +impl PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0 + for PrivateDocumentCreateFromShieldedPoolTransitionV0 +{ + #[cfg(feature = "state-transition-signing")] + fn try_from_bundle( + data_contract_id: Identifier, + document_type_name: String, + entries: Vec, + actions: Vec, + value_balance: u64, + anchor: [u8; 32], + proof: Vec, + binding_signature: [u8; 64], + _platform_version: &PlatformVersion, + ) -> Result { + let transition = PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id, + document_type_name, + entries, + actions, + value_balance, + anchor, + proof, + binding_signature, + }; + Ok(transition.into()) + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/version.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/version.rs new file mode 100644 index 00000000000..2f4f9722273 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/version.rs @@ -0,0 +1,9 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use crate::state_transition::FeatureVersioned; +use crate::version::FeatureVersion; + +impl FeatureVersioned for PrivateDocumentCreateFromShieldedPoolTransitionV0 { + fn feature_version(&self) -> FeatureVersion { + 0 + } +} diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/version.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/version.rs new file mode 100644 index 00000000000..42bb8a69f89 --- /dev/null +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/version.rs @@ -0,0 +1,11 @@ +use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use crate::state_transition::FeatureVersioned; +use crate::version::FeatureVersion; + +impl FeatureVersioned for PrivateDocumentCreateFromShieldedPoolTransition { + fn feature_version(&self) -> FeatureVersion { + match self { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.feature_version(), + } + } +} diff --git a/packages/rs-drive-abci/src/execution/types/execution_event/mod.rs b/packages/rs-drive-abci/src/execution/types/execution_event/mod.rs index b5e0da71100..6d33f4ed4c7 100644 --- a/packages/rs-drive-abci/src/execution/types/execution_event/mod.rs +++ b/packages/rs-drive-abci/src/execution/types/execution_event/mod.rs @@ -644,6 +644,21 @@ impl ExecutionEvent<'_> { chargeable_failure: false, }) } + StateTransitionAction::PrivateDocumentCreateFromShieldedPoolAction( + ref private_document_action, + ) => { + // Pool-paid exactly like ShieldedTransfer: the whole value balance is the fee, + // routed to the fee pools. Nothing is credited outside the pool. + let fee_amount = private_document_action.fee_amount(); + let operations = + action.into_high_level_drive_operations(epoch, platform_version)?; + Ok(ExecutionEvent::PaidFromShieldedPool { + operations, + fees_to_add_to_pool: fee_amount, + added_to_balance_outputs: None, + chargeable_failure: false, + }) + } StateTransitionAction::IdentityTopUpFromShieldedPoolAction(ref top_up_action) => { // Pool-paid exactly like Unshield: the flat fee is carved from the value // balance and routed to the fee pools; the identity receives the rest. diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_balances_and_nonces.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_balances_and_nonces.rs index c4fb2b6e494..a5deb6a00e1 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_balances_and_nonces.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_balances_and_nonces.rs @@ -187,6 +187,7 @@ impl StateTransitionAddressBalancesAndNoncesValidation for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -264,6 +265,7 @@ impl StateTransitionAddressBalancesAndNoncesValidation for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_witnesses.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_witnesses.rs index 1f3a5b3465e..c3dc54460ae 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_witnesses.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/address_witnesses.rs @@ -113,6 +113,7 @@ impl StateTransitionAddressWitnessValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -239,6 +240,7 @@ impl StateTransitionHasAddressWitnessValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/addresses_minimum_balance.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/addresses_minimum_balance.rs index 9c6e1c819c9..9a503e64d8b 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/addresses_minimum_balance.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/addresses_minimum_balance.rs @@ -81,6 +81,7 @@ impl StateTransitionAddressesMinimumBalanceValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -122,6 +123,7 @@ impl StateTransitionAddressesMinimumBalanceValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/basic_structure.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/basic_structure.rs index ba917d078d6..d2a1531a5cd 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/basic_structure.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/basic_structure.rs @@ -453,6 +453,28 @@ impl StateTransitionBasicStructureValidationV0 for StateTransition { })), } } + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => { + match platform_version + .drive_abci + .validation_and_processing + .state_transitions + .private_document_create_from_shielded_pool_state_transition + .basic_structure + { + Some(0) => Ok(st.validate_structure(platform_version)), + Some(version) => { + Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: "private document create from shielded pool transition: validate_basic_structure".to_string(), + known_versions: vec![0], + received: version, + })) + } + None => Err(Error::Execution(ExecutionError::VersionNotActive { + method: "private document create from shielded pool transition: validate_basic_structure".to_string(), + known_versions: vec![0], + })), + } + } StateTransition::ShieldFromAssetLock(st) => { match platform_version .drive_abci @@ -631,6 +653,13 @@ impl StateTransitionBasicStructureValidationV0 for StateTransition { .token_purchase_from_shielded_pool_state_transition .basic_structure .is_some(), + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => platform_version + .drive_abci + .validation_and_processing + .state_transitions + .private_document_create_from_shielded_pool_state_transition + .basic_structure + .is_some(), StateTransition::ShieldFromAssetLock(_) => platform_version .drive_abci .validation_and_processing @@ -1024,6 +1053,7 @@ mod tests { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldedWithdrawal(_) | StateTransition::IdentityCreateFromShieldedPool(_) => false, diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_balance.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_balance.rs index 14bed334f3f..202f949e4be 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_balance.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_balance.rs @@ -156,6 +156,7 @@ impl StateTransitionIdentityBalanceValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_based_signature.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_based_signature.rs index 6087ff3275d..ff61ddd4867 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_based_signature.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_based_signature.rs @@ -141,6 +141,7 @@ impl StateTransitionIdentityBasedSignatureValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -188,6 +189,7 @@ impl StateTransitionIdentityBasedSignatureValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -225,6 +227,7 @@ impl StateTransitionIdentityBasedSignatureValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_nonces.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_nonces.rs index a23db96866c..3a598671a9d 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_nonces.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/identity_nonces.rs @@ -149,6 +149,7 @@ impl StateTransitionIdentityNonceValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -211,6 +212,7 @@ impl StateTransitionHasIdentityNonceValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs index 7bf11d9ae27..9fc1c20a620 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs @@ -22,6 +22,7 @@ use dpp::version::feature_initial_protocol_versions::{ CONTRACT_USER_MODERATION_INITIAL_PROTOCOL_VERSION, IDENTITY_KEY_LIMITS_UPDATE_INITIAL_PROTOCOL_VERSION, IDENTITY_TOP_UP_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, + PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, SHIELD_FROM_IDENTITY_INITIAL_PROTOCOL_VERSION, TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION, TOKEN_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, }; @@ -54,6 +55,7 @@ impl StateTransitionIsAllowedValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) @@ -259,6 +261,22 @@ impl StateTransitionIsAllowedValidationV0 for StateTransition { ])) } } + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => { + if platform_version.protocol_version + >= PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION + { + Ok(ConsensusValidationResult::new()) + } else { + Ok(ConsensusValidationResult::new_with_errors(vec![ + StateTransitionNotActiveError::new( + self.state_transition_type().to_string(), + platform_version.protocol_version, + PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION, + ) + .into(), + ])) + } + } StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) => { @@ -612,7 +630,8 @@ mod tests { | StateTransition::ContractFeeClaim(_) | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) - | StateTransition::TokenPurchaseFromShieldedPool(_) => Some(true), + | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) => Some(true), StateTransition::IdentityCreate(_) | StateTransition::IdentityTopUp(_) | StateTransition::IdentityCreditWithdrawal(_) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/shielded_proof.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/shielded_proof.rs index 6be3f347c16..04f4defceca 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/shielded_proof.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/shielded_proof.rs @@ -21,6 +21,7 @@ use dpp::state_transition::public_key_in_creation::accessors::IdentityPublicKeyI use dpp::state_transition::public_key_in_creation::IdentityPublicKeyInCreation; use dpp::state_transition::state_transitions::shielded::identity_create_from_shielded_pool_transition::IdentityCreateFromShieldedPoolTransition; use dpp::state_transition::identity_top_up_from_shielded_pool_transition::IdentityTopUpFromShieldedPoolTransition; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; use dpp::state_transition::token_purchase_from_shielded_pool_transition::TokenPurchaseFromShieldedPoolTransition; use dpp::state_transition::token_shielded_transfer_with_shielded_fee_transition::TokenShieldedTransferWithShieldedFeeTransition; use dpp::state_transition::token_unshield_with_shielded_fee_transition::TokenUnshieldWithShieldedFeeTransition; @@ -30,7 +31,8 @@ use dpp::state_transition::batch_transition::batched_transition::token_transitio TokenTransition, TokenTransitionV0Methods, }; use dpp::shielded::{ - shield_extra_sighash_data, shield_from_identity_extra_sighash_data, TOKEN_PURCHASE_FROM_SHIELDED_POOL_TYPE, TOKEN_SHIELDED_TRANSFER_WITH_SHIELDED_FEE_TYPE, TOKEN_UNSHIELD_WITH_SHIELDED_FEE_TYPE, compute_token_purchase_from_shielded_pool_fee, compute_token_shielded_transfer_with_shielded_fee_fee, compute_token_unshield_with_shielded_fee_fee, document_token_payment_extra_sighash_data, token_burn_from_pool_extra_sighash_data, token_pool_fee_bundle_extra_sighash_data, token_pool_output_only_extra_sighash_data, token_purchase_from_shielded_pool_extra_sighash_data, token_shielded_transfer_extra_sighash_data, token_shielded_transfer_with_shielded_fee_extra_sighash_data, token_unshield_extra_sighash_data, token_unshield_with_shielded_fee_extra_sighash_data, + compute_private_document_create_from_shielded_pool_fee, + private_document_create_from_shielded_pool_extra_sighash_data, shield_extra_sighash_data, shield_from_identity_extra_sighash_data, TOKEN_PURCHASE_FROM_SHIELDED_POOL_TYPE, TOKEN_SHIELDED_TRANSFER_WITH_SHIELDED_FEE_TYPE, TOKEN_UNSHIELD_WITH_SHIELDED_FEE_TYPE, compute_token_purchase_from_shielded_pool_fee, compute_token_shielded_transfer_with_shielded_fee_fee, compute_token_unshield_with_shielded_fee_fee, document_token_payment_extra_sighash_data, token_burn_from_pool_extra_sighash_data, token_pool_fee_bundle_extra_sighash_data, token_pool_output_only_extra_sighash_data, token_purchase_from_shielded_pool_extra_sighash_data, token_shielded_transfer_extra_sighash_data, token_shielded_transfer_with_shielded_fee_extra_sighash_data, token_unshield_extra_sighash_data, token_unshield_with_shielded_fee_extra_sighash_data, }; use dpp::state_transition::batch_transition::batched_transition::BatchedTransitionRef; use dpp::state_transition::batch_transition::batched_transition::token_transition_action_type::TokenTransitionActionType; @@ -157,6 +159,7 @@ impl StateTransitionHasShieldedProofValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) ) } @@ -207,6 +210,9 @@ impl StateTransitionHasShieldedProofValidationV0 for StateTransition { StateTransition::IdentityCreateFromShieldedPool(st) => match st { IdentityCreateFromShieldedPoolTransition::V0(v0) => v0.actions.len(), }, + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => match st { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => v0.actions.len(), + }, StateTransition::ShieldFromAssetLock(st) => match st { dpp::state_transition::shield_from_asset_lock_transition::ShieldFromAssetLockTransition::V0(v0) => { v0.actions.len() @@ -293,6 +299,7 @@ impl StateTransitionHasShieldedProofValidationV0 for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) ) } } @@ -372,6 +379,14 @@ enum ShieldedMinFeeKind { TokenPurchase { token_actions: usize, }, + /// `compute_private_document_create_from_shielded_pool_fee` — the bundle plus, per entry, its + /// bytes and the flat entry fee; `validated_amount` is the bundle's value balance, which is + /// the fee. Priced at the first entry's size: basic structure holds every entry to it, and + /// the transform to the type's `entrySize`. + PrivateDocumentCreate { + entry_count: usize, + entry_size: u32, + }, } impl StateTransitionShieldedMinimumFeeValidationV0 for StateTransition { @@ -512,6 +527,27 @@ impl StateTransitionShieldedMinimumFeeValidationV0 for StateTransition { ShieldedMinFeeKind::TokenPurchase { token_actions: v0.token_actions.len() }, ), }, + // Added in place: protocol version 14 selects this generation too, but refuses a + // `PrivateDocumentCreateFromShieldedPool` before it gets here (its + // `active_version_range` and `is_allowed` start it at 15). + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => match st { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => ( + v0.value_balance as i64, + v0.actions.len(), + 0, + u64::MAX, + true, + ShieldedMinFeeKind::PrivateDocumentCreate { + entry_count: v0.entries.len(), + // Basic structure caps an entry at a u32 size. + entry_size: v0 + .entries + .first() + .map(|entry| entry.len().min(u32::MAX as usize) as u32) + .unwrap_or(0), + }, + ), + }, // Other transitions don't go through shielded fee validation. _ => return Ok(SimpleConsensusValidationResult::new()), }; @@ -599,6 +635,15 @@ impl StateTransitionShieldedMinimumFeeValidationV0 for StateTransition { platform_version, )? } + ShieldedMinFeeKind::PrivateDocumentCreate { + entry_count, + entry_size, + } => compute_private_document_create_from_shielded_pool_fee( + num_actions, + entry_count, + entry_size, + platform_version, + )?, }; if (validated_amount as u64) < minimum_shielded_fee { @@ -1426,6 +1471,32 @@ fn validate_shielded_proof_v1( ) } }, + // Added in place: protocol version 14 selects this generation too, but + // refuses a `PrivateDocumentCreateFromShieldedPool` before it gets here + // (`active_version_range` and `is_allowed` start it at 15), so the arm is + // unreachable there. The bundle pays exactly the fee, so its value balance + // is the whole amount leaving the pool, and its sighash binds the target and + // every entry. + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => match st { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + let extra_sighash_data = + private_document_create_from_shielded_pool_extra_sighash_data( + &v0.data_contract_id.to_buffer(), + &v0.document_type_name, + &v0.entries, + platform_version, + )?; + reconstruct_and_verify_bundle( + &v0.actions, + FLAGS_SPENDS_AND_OUTPUTS, + v0.value_balance as i64, + &v0.anchor, + v0.proof.as_slice(), + &v0.binding_signature, + &extra_sighash_data, + ) + } + }, StateTransition::ShieldedWithdrawal(st) => match st { dpp::state_transition::shielded_withdrawal_transition::ShieldedWithdrawalTransition::V0(v0) => { let extra_sighash_data = diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/state.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/state.rs index d2d49f24cdc..764ef18f7e1 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/state.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/state.rs @@ -249,6 +249,11 @@ impl StateTransitionStateValidation for StateTransition { "token purchase from shielded pool should not have state validation", ))) } + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => { + Err(Error::Execution(ExecutionError::CorruptedCodeExecution( + "private document create from shielded pool should not have state validation", + ))) + } StateTransition::ShieldFromAssetLock(_) => { Err(Error::Execution(ExecutionError::CorruptedCodeExecution( "shield from asset lock should not have state validation", @@ -334,6 +339,7 @@ impl StateTransitionStateValidation for StateTransition { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::Unshield(_) | StateTransition::ShieldFromAssetLock(_) | StateTransition::ShieldedWithdrawal(_) => false, diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs index 1854fa60e93..8f7d7e9b241 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs @@ -57,6 +57,8 @@ mod identity_top_up_from_addresses; pub mod identity_create_from_shielded_pool; /// Identity top up from shielded pool (pool to an existing identity) pub mod identity_top_up_from_shielded_pool; +/// Module for the private document create from shielded pool transition (protocol version 15) +pub mod private_document_create_from_shielded_pool; /// Module for shield transition validation pub mod shield; /// Module for shield from asset lock transition validation diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/mod.rs new file mode 100644 index 00000000000..33da18c604f --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/mod.rs @@ -0,0 +1,60 @@ +mod transform_into_action; + +#[cfg(test)] +mod tests; + +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use dpp::validation::ConsensusValidationResult; +use drive::grovedb::TransactionArg; +use drive::state_transition_action::StateTransitionAction; + +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::execution::validation::state_transition::private_document_create_from_shielded_pool::transform_into_action::v0::PrivateDocumentCreateFromShieldedPoolStateTransitionTransformIntoActionValidationV0; +use crate::platform_types::platform::PlatformRef; +use crate::platform_types::platform_state::PlatformStateV0Methods; +use crate::rpc::core::CoreRPCLike; + +/// A trait to transform into an action for the private document create from shielded pool +/// transition +pub trait StateTransitionPrivateDocumentCreateFromShieldedPoolTransitionActionTransformer { + /// Transform into an action for the private document create from shielded pool transition + fn transform_into_action_for_private_document_create_from_shielded_pool_transition< + C: CoreRPCLike, + >( + &self, + platform: &PlatformRef, + tx: TransactionArg, + ) -> Result, Error>; +} + +impl StateTransitionPrivateDocumentCreateFromShieldedPoolTransitionActionTransformer + for PrivateDocumentCreateFromShieldedPoolTransition +{ + fn transform_into_action_for_private_document_create_from_shielded_pool_transition< + C: CoreRPCLike, + >( + &self, + platform: &PlatformRef, + tx: TransactionArg, + ) -> Result, Error> { + let platform_version = platform.state.current_platform_version()?; + + match platform_version + .drive_abci + .validation_and_processing + .state_transitions + .private_document_create_from_shielded_pool_state_transition + .transform_into_action + { + 0 => self.transform_into_action_v0(platform.drive, tx, platform_version), + version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { + method: + "private document create from shielded pool transition: transform_into_action" + .to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs new file mode 100644 index 00000000000..5893783a94b --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs @@ -0,0 +1,496 @@ +use crate::execution::validation::state_transition::state_transitions::shielded_common::compute_platform_sighash; +use crate::execution::validation::state_transition::state_transitions::test_helpers::{ + create_dummy_serialized_action, get_proving_key, insert_anchor_into_state, + insert_dummy_encrypted_notes, process_transition, serialize_authorized_bundle_i64, + set_pool_total_balance, setup_platform, setup_platform_at_protocol_version, +}; +use crate::execution::validation::state_transition::state_transitions::tests::process_state_transitions; +use crate::platform_types::state_transitions_processing_result::StateTransitionExecutionResult; +use crate::rpc::core::MockCoreRPCLike; +use crate::test::helpers::setup::TempPlatform; +use assert_matches::assert_matches; +use dpp::block::block_info::BlockInfo; +use dpp::consensus::basic::BasicError; +use dpp::consensus::state::state_error::StateError; +use dpp::consensus::ConsensusError; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::conversion::value::v0::DataContractValueConversionMethodsV0; +use dpp::data_contract::DataContract; +use dpp::platform_value::string_encoding::Encoding; +use dpp::platform_value::{BinaryData, Identifier}; +use dpp::serialization::PlatformSerializable; +use dpp::shielded::{ + compute_private_document_create_from_shielded_pool_fee, + private_document_create_from_shielded_pool_extra_sighash_data_v0, SerializedAction, +}; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use dpp::state_transition::proof_result::StateTransitionProofResult; +use dpp::state_transition::StateTransition; +use drive::drive::Drive; +use grovedb_commitment_tree::{ + Builder, BundleType, ClientMemoryCommitmentTree, DashMemo, ExtractedNoteCommitment, + FullViewingKey, Note, NoteValue, Position, RandomSeed, Retention, Rho, Scope, + SpendAuthorizingKey, SpendingKey, +}; +use platform_version::version::PlatformVersion; +use rand::rngs::StdRng; +use rand::SeedableRng; +use serde_json::json; + +const NOTE_VALUE: u64 = 5_000_000_000; +const CONTRACT_ID: [u8; 32] = [7; 32]; +const ENTRY_SIZE: u32 = 24; +/// A bundle with one spend and one change output carries two actions. +const BUNDLE_ACTIONS: usize = 2; + +fn entries(count: u8, size: u32) -> Vec { + (0..count) + .map(|i| BinaryData::new(vec![i + 1; size as usize])) + .collect() +} + +fn fee(entries: &[BinaryData]) -> u64 { + compute_private_document_create_from_shielded_pool_fee( + BUNDLE_ACTIONS, + entries.len(), + entries.first().map(|e| e.len() as u32).unwrap_or(0), + PlatformVersion::latest(), + ) + .expect("fee") +} + +fn transition( + data_contract_id: Identifier, + document_type_name: &str, + entries: Vec, + actions: Vec, + value_balance: u64, + anchor: [u8; 32], + proof: Vec, + binding_signature: [u8; 64], +) -> StateTransition { + PrivateDocumentCreateFromShieldedPoolTransition::V0( + PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id, + document_type_name: document_type_name.to_string(), + entries, + actions, + value_balance, + anchor, + proof, + binding_signature, + }, + ) + .into() +} + +/// A transition whose bundle is never proven: for the checks that run before the proof. +fn dummy_transition(entries: Vec, value_balance: u64) -> StateTransition { + transition( + Identifier::from(CONTRACT_ID), + "secret", + entries, + vec![create_dummy_serialized_action()], + value_balance, + [42u8; 32], + vec![7u8; 100], + [0u8; 64], + ) +} + +/// A transition with a real bundle (one note of `NOTE_VALUE` in, its change out) paying the +/// fee for `entries`, whose sighash binds `data_contract_id`, `document_type_name` and the +/// entries. Returns it with its anchor. +fn proven_transition( + data_contract_id: Identifier, + document_type_name: &str, + entries: Vec, +) -> (StateTransition, [u8; 32]) { + let fee = fee(&entries); + let mut rng = StdRng::seed_from_u64(0); + let pk = get_proving_key(); + + let sk = SpendingKey::from_bytes([0u8; 32]).unwrap(); + let fvk = FullViewingKey::from(&sk); + let recipient = fvk.address_at(0u32, Scope::External); + let ask = SpendAuthorizingKey::from(&sk); + + let mut rho_bytes = [0u8; 32]; + rho_bytes[0] = 1; + let rho = Rho::from_bytes(&rho_bytes).unwrap(); + let rseed = RandomSeed::from_bytes([42u8; 32], &rho).unwrap(); + let note = Note::from_parts(recipient, NoteValue::from_raw(NOTE_VALUE), rho, rseed).unwrap(); + + let cmx = ExtractedNoteCommitment::from(note.commitment()); + let mut tree = ClientMemoryCommitmentTree::new(100); + tree.append(cmx.to_bytes(), Retention::Marked).unwrap(); + tree.checkpoint(0u32).unwrap(); + let anchor = tree.anchor().unwrap(); + let merkle_path = tree.witness(Position::from(0u64), 0).unwrap().unwrap(); + + let mut builder = Builder::::new(BundleType::DEFAULT, anchor); + builder.add_spend(fvk.clone(), note, merkle_path).unwrap(); + builder + .add_output( + None, + recipient, + NoteValue::from_raw(NOTE_VALUE - fee), + [0u8; 36], + ) + .unwrap(); + let (unauthorized, _) = builder.build::(&mut rng).unwrap().unwrap(); + + let extra_sighash_data = private_document_create_from_shielded_pool_extra_sighash_data_v0( + &data_contract_id.to_buffer(), + document_type_name, + &entries, + ); + let bundle_commitment: [u8; 32] = unauthorized.commitment().into(); + let sighash = compute_platform_sighash(&bundle_commitment, &extra_sighash_data); + let proven = unauthorized.create_proof(pk, &mut rng).unwrap(); + let bundle = proven.apply_signatures(rng, sighash, &[ask]).unwrap(); + + let (actions, value_balance, anchor, proof, binding_signature) = + serialize_authorized_bundle_i64(&bundle); + assert_eq!(actions.len(), BUNDLE_ACTIONS); + assert_eq!(value_balance as u64, fee, "the bundle pays exactly the fee"); + ( + transition( + data_contract_id, + document_type_name, + entries, + actions, + value_balance as u64, + anchor, + proof, + binding_signature, + ), + anchor, + ) +} + +/// Registers the contract holding the private `secret` type of `ENTRY_SIZE`-byte entries and +/// the public `note` type. +fn register_contract(platform: &TempPlatform) -> DataContract { + let platform_version = PlatformVersion::latest(); + let contract = DataContract::from_value( + dpp::platform_value::to_value(json!({ + "$formatVersion": "1", + "id": Identifier::from(CONTRACT_ID).to_string(Encoding::Base58), + "ownerId": Identifier::from([8; 32]).to_string(Encoding::Base58), + "version": 1, + "documentSchemas": { + "secret": { + "type": "object", + "private": { "entrySize": ENTRY_SIZE }, + "documentsMutable": false, + "canBeDeleted": false, + "properties": { + "accountReference": { + "type": "integer", "minimum": 0, "maximum": 4294967295u64, + "position": 0 + } + }, + "additionalProperties": false + }, + "note": { + "type": "object", + "properties": { + "text": { "type": "string", "maxLength": 63, "position": 0 } + }, + "additionalProperties": false + } + } + })) + .expect("the contract converts"), + true, + platform_version, + ) + .expect("the contract registers"); + platform + .drive + .apply_contract( + &contract, + BlockInfo::default(), + true, + None, + None, + platform_version, + ) + .expect("expected to register the contract"); + contract +} + +/// Seeds the pool the spend needs: enough notes for the minimum-notes floor, the bundle's +/// anchor and the pool's total balance, booked into system credits. +fn seed_pool(platform: &TempPlatform, anchor: &[u8; 32]) { + insert_dummy_encrypted_notes(platform, 250); + insert_anchor_into_state(platform, anchor); + set_pool_total_balance(platform, NOTE_VALUE); +} + +fn single_unpaid_error( + platform: &TempPlatform, + st: StateTransition, +) -> ConsensusError { + let result = process_transition(platform, st, PlatformVersion::latest()); + match result.execution_results().as_slice() { + [StateTransitionExecutionResult::UnpaidConsensusError(error)] => error.clone(), + results => panic!("expected one unpaid refusal, got {results:?}"), + } +} + +// ========================================== +// Success, fee, conservation, proof +// ========================================== + +#[test] +fn should_append_the_entries_behind_the_block_header_and_pay_exactly_the_fee() { + let platform_version = PlatformVersion::latest(); + let platform = setup_platform(); + let contract = register_contract(&platform); + let entries = entries(3, ENTRY_SIZE); + let (st, anchor) = proven_transition(contract.id(), "secret", entries.clone()); + seed_pool(&platform, &anchor); + + let credits_before = platform + .drive + .calculate_total_credits_balance(None, &platform_version.drive) + .expect("total credits before"); + assert!( + credits_before.ok().expect("no overflow"), + "fixture must start balanced" + ); + + { + let guard_bytes = st.serialize_to_bytes().expect("serialize for guard"); + crate::test::helpers::state_mutation_guard::assert_check_tx_valid_at_all_levels( + &platform, + &guard_bytes, + "private document create from shielded pool", + ); + } + + let block_info = BlockInfo { + height: 7, + time_ms: 1_700_000_000_000, + ..Default::default() + }; + let platform_state = platform.state.load(); + let (fee_results, _) = process_state_transitions(&platform, &[st], block_info, &platform_state); + let fee = fee(&entries); + assert_eq!( + fee_results[0].total_base_fee(), + fee, + "the pool-paid fee must be booked exactly" + ); + + let credits_after = platform + .drive + .calculate_total_credits_balance(None, &platform_version.drive) + .expect("total credits after"); + assert!( + credits_after.ok().expect("no overflow"), + "credits must stay balanced: {credits_after}" + ); + assert_eq!( + credits_after.total_credits_in_platform, credits_before.total_credits_in_platform, + "paying the fee from the pool must not mint or burn system credits" + ); + assert_eq!( + credits_before.total_in_shielded_balances - credits_after.total_in_shielded_balances, + fee as i64, + "the pool must lose exactly the fee" + ); + + let secret = contract + .document_type_for_name("secret") + .expect("the secret type"); + let page = platform + .drive + .fetch_private_document_entries(CONTRACT_ID, secret, 0, None, platform_version) + .expect("expected to read the store"); + assert_eq!(page.total_count, 3); + for (i, (stored, entry)) in page.entries.iter().zip(&entries).enumerate() { + assert_eq!(stored.position, i as u64); + assert_eq!(stored.block_height, 7); + assert_eq!(stored.block_time_ms, 1_700_000_000_000); + assert_eq!(stored.data, entry.to_vec()); + } +} + +#[test] +fn should_prove_the_spent_nullifiers_as_the_affected_state() { + let platform_version = PlatformVersion::latest(); + let platform = setup_platform(); + let contract = register_contract(&platform); + let (st, anchor) = proven_transition(contract.id(), "secret", entries(1, ENTRY_SIZE)); + seed_pool(&platform, &anchor); + let StateTransition::PrivateDocumentCreateFromShieldedPool( + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0), + ) = &st + else { + panic!("expected a private document create"); + }; + let expected_nullifiers: Vec> = + v0.actions.iter().map(|a| a.nullifier.to_vec()).collect(); + + let platform_state = platform.state.load(); + process_state_transitions( + &platform, + &[st.clone()], + BlockInfo::default(), + &platform_state, + ); + + let proof_bytes = platform + .drive + .prove_state_transition(&st, None, platform_version) + .expect("prove") + .into_data() + .expect("proof data"); + let (root_hash, outcome) = Drive::verify_state_transition_was_executed_with_proof( + &st, + &BlockInfo::default(), + &proof_bytes, + &|_| Ok(None), + platform_version, + ) + .expect("verify"); + assert_ne!(root_hash, [0u8; 32]); + // The spent nullifiers name the notes paying the fee, not the entries, so a competing create + // spending the same notes for other entries leaves the same evidence. + assert!( + !outcome.is_execution_proved(), + "a private document create proof must be classified as affected state, got {outcome:?}" + ); + let result = outcome.into_result(); + let StateTransitionProofResult::VerifiedShieldedNullifiers(statuses) = result else { + panic!("expected VerifiedShieldedNullifiers, got {result:?}"); + }; + assert_eq!( + statuses + .iter() + .map(|(nf, _)| nf.clone()) + .collect::>(), + expected_nullifiers + ); + assert!(statuses.iter().all(|(_, spent)| *spent)); +} + +// ========================================== +// Refusals, all unpaid +// ========================================== + +#[test] +fn should_refuse_the_transition_before_protocol_version_15() { + let platform = setup_platform_at_protocol_version(14); + let entries = entries(1, ENTRY_SIZE); + let value_balance = fee(&entries); + let result = process_transition( + &platform, + dummy_transition(entries, value_balance), + PlatformVersion::get(14).expect("protocol version 14"), + ); + assert_matches!( + result.execution_results().as_slice(), + [StateTransitionExecutionResult::UnpaidConsensusError(ConsensusError::BasicError( + BasicError::StateTransitionNotActiveError(error) + ))] if error.state_transition_type().starts_with("PrivateDocumentCreateFromShieldedPool") + ); +} + +#[test] +fn should_refuse_a_value_balance_below_or_above_the_fee() { + let platform = setup_platform(); + let entries = entries(2, ENTRY_SIZE); + // The fee check reads the action count: the dummy bundle has one action. + let exact = compute_private_document_create_from_shielded_pool_fee( + 1, + 2, + ENTRY_SIZE, + PlatformVersion::latest(), + ) + .expect("fee"); + assert_matches!( + single_unpaid_error(&platform, dummy_transition(entries.clone(), exact - 1)), + ConsensusError::StateError(StateError::InsufficientShieldedFeeError(_)) + ); + assert_matches!( + single_unpaid_error(&platform, dummy_transition(entries, exact + 1)), + ConsensusError::BasicError(BasicError::ShieldedInvalidValueBalanceError(_)) + ); +} + +#[test] +fn should_refuse_two_entries_that_are_the_same_before_the_proof() { + let platform = setup_platform(); + let entries = vec![ + BinaryData::new(vec![1u8; ENTRY_SIZE as usize]), + BinaryData::new(vec![1u8; ENTRY_SIZE as usize]), + ]; + let value_balance = fee(&entries); + assert_matches!( + single_unpaid_error(&platform, dummy_transition(entries, value_balance)), + ConsensusError::BasicError(BasicError::DuplicatePrivateDocumentEntryError(_)) + ); +} + +#[test] +fn should_refuse_entries_changed_after_the_bundle_was_signed() { + let platform = setup_platform(); + let contract = register_contract(&platform); + let (mut st, anchor) = proven_transition(contract.id(), "secret", entries(2, ENTRY_SIZE)); + seed_pool(&platform, &anchor); + let StateTransition::PrivateDocumentCreateFromShieldedPool( + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0), + ) = &mut st + else { + panic!("expected a private document create"); + }; + let mut changed = v0.entries[1].to_vec(); + changed[0] ^= 1; + v0.entries[1] = BinaryData::new(changed); + assert_matches!( + single_unpaid_error(&platform, st), + ConsensusError::StateError(StateError::InvalidShieldedProofError(_)) + ); +} + +#[test] +fn should_refuse_a_target_that_is_no_private_type_taking_the_entries() { + let platform = setup_platform(); + let contract = register_contract(&platform); + + let (st, anchor) = proven_transition( + Identifier::from([9u8; 32]), + "secret", + entries(1, ENTRY_SIZE), + ); + seed_pool(&platform, &anchor); + assert_matches!( + single_unpaid_error(&platform, st), + ConsensusError::StateError(StateError::DataContractNotFoundError(_)) + ); + + let (st, _) = proven_transition(contract.id(), "missing", entries(1, ENTRY_SIZE)); + assert_matches!( + single_unpaid_error(&platform, st), + ConsensusError::BasicError(BasicError::InvalidDocumentTypeError(_)) + ); + + let (st, _) = proven_transition(contract.id(), "note", entries(1, ENTRY_SIZE)); + assert_matches!( + single_unpaid_error(&platform, st), + ConsensusError::StateError(StateError::DocumentTypeNotPrivateError(e)) + if e.document_type_name() == "note" + ); + + let (st, _) = proven_transition(contract.id(), "secret", entries(1, ENTRY_SIZE + 1)); + assert_matches!( + single_unpaid_error(&platform, st), + ConsensusError::StateError(StateError::PrivateDocumentEntrySizeMismatchError(e)) + if e.entry_size() == ENTRY_SIZE && e.size() == ENTRY_SIZE + 1 + ); +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/mod.rs new file mode 100644 index 00000000000..9a1925de7fc --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/mod.rs @@ -0,0 +1 @@ +pub(crate) mod v0; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs new file mode 100644 index 00000000000..3e0434a9c8c --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs @@ -0,0 +1,174 @@ +use crate::error::execution::ExecutionError; +use crate::error::Error; +use crate::execution::validation::state_transition::state_transitions::shielded_common::{ + read_pool_total_balance, validate_anchor_exists, validate_minimum_pool_notes, + validate_nullifiers, +}; +use dpp::consensus::basic::document::InvalidDocumentTypeError; +use dpp::consensus::state::data_contract::data_contract_not_found_error::DataContractNotFoundError; +use dpp::consensus::state::document::document_type_not_private_error::DocumentTypeNotPrivateError; +use dpp::consensus::state::document::private_document_entry_size_mismatch_error::PrivateDocumentEntrySizeMismatchError; +use dpp::consensus::state::shielded::invalid_shielded_proof_error::InvalidShieldedProofError; +use dpp::consensus::state::state_error::StateError; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::document_type::accessors::DocumentTypeV2Getters; +use dpp::prelude::ConsensusValidationResult; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use dpp::version::PlatformVersion; +use drive::drive::Drive; +use drive::grovedb::TransactionArg; +use drive::state_transition_action::shielded::private_document_create_from_shielded_pool::PrivateDocumentCreateFromShieldedPoolTransitionAction; +use drive::state_transition_action::StateTransitionAction; + +pub(in crate::execution::validation::state_transition::state_transitions::private_document_create_from_shielded_pool) trait PrivateDocumentCreateFromShieldedPoolStateTransitionTransformIntoActionValidationV0 +{ + fn transform_into_action_v0( + &self, + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error>; +} + +impl PrivateDocumentCreateFromShieldedPoolStateTransitionTransformIntoActionValidationV0 + for PrivateDocumentCreateFromShieldedPoolTransition +{ + /// The Orchard proof and the exact fee have already been checked by the processor. Every + /// failure here is refused without a fee, as every check runs before execution. The pool + /// checks come first, as `ShieldedTransfer`'s (the pool notes floor, the anchor, unspent + /// nullifiers, the pool balance), so junk with a made-up anchor costs no contract read. + /// Then the target: the contract and the type must exist, the type must be private, the + /// entries must take its `entrySize`, and its store must be there to take them, so the + /// append in execution cannot fail. + fn transform_into_action_v0( + &self, + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) = self; + + let nullifiers: Vec<[u8; 32]> = v0.actions.iter().map(|a| a.nullifier).collect(); + + let mut drive_operations = vec![]; + let current_total_balance = + read_pool_total_balance(drive, transaction, &mut drive_operations, platform_version)?; + + if let Some(consensus_error) = validate_minimum_pool_notes( + drive, + transaction, + &mut drive_operations, + platform_version, + )? { + return Ok(consensus_error); + } + + if let Some(consensus_error) = validate_anchor_exists( + drive, + &v0.anchor, + transaction, + &mut drive_operations, + platform_version, + )? { + return Ok(consensus_error); + } + + if let Some(consensus_error) = validate_nullifiers( + drive, + &nullifiers, + transaction, + &mut drive_operations, + platform_version, + )? { + return Ok(consensus_error); + } + + if current_total_balance < v0.value_balance { + return Ok(ConsensusValidationResult::new_with_error( + StateError::InvalidShieldedProofError(InvalidShieldedProofError::new(format!( + "shielded pool has insufficient balance: pool has {} but the private \ + document create's fee is {}", + current_total_balance, v0.value_balance + ))) + .into(), + )); + } + + let Some(contract_fetch_info) = drive.get_contract_with_fetch_info( + v0.data_contract_id.to_buffer(), + false, + transaction, + platform_version, + )? + else { + return Ok(ConsensusValidationResult::new_with_error( + DataContractNotFoundError::new(v0.data_contract_id).into(), + )); + }; + let Ok(document_type) = contract_fetch_info + .contract + .document_type_for_name(&v0.document_type_name) + else { + return Ok(ConsensusValidationResult::new_with_error( + InvalidDocumentTypeError::new(v0.document_type_name.clone(), v0.data_contract_id) + .into(), + )); + }; + let Some(private_document) = document_type.private_document() else { + return Ok(ConsensusValidationResult::new_with_error( + DocumentTypeNotPrivateError::new( + v0.data_contract_id, + v0.document_type_name.clone(), + ) + .into(), + )); + }; + let entry_size = private_document.entry_size(); + // Basic structure holds every entry to the first one's size, and to a u32. + let size = v0 + .entries + .first() + .map(|entry| entry.len().min(u32::MAX as usize) as u32) + .unwrap_or(0); + if size != entry_size { + return Ok(ConsensusValidationResult::new_with_error( + PrivateDocumentEntrySizeMismatchError::new( + v0.data_contract_id, + v0.document_type_name.clone(), + entry_size, + size, + ) + .into(), + )); + } + + // Every path that registers a private type creates its store; one missing, or of + // another entry size, is corrupted state, which no transition is executed against. + if !drive.private_document_store_takes_entries( + &v0.data_contract_id.to_buffer(), + &v0.document_type_name, + entry_size, + transaction, + platform_version, + )? { + return Err(Error::Execution(ExecutionError::CorruptedDriveResponse( + format!( + "the private document type \"{}\" of contract {} has no store taking entries \ + of {} bytes", + v0.document_type_name, v0.data_contract_id, entry_size + ), + ))); + } + + // The minimum fee validation held the value balance to the fee for these entries at + // the first entry's size, which is the type's `entrySize`, so the whole value balance + // is the fee. + let result = PrivateDocumentCreateFromShieldedPoolTransitionAction::try_from_transition( + self, + current_total_balance, + v0.value_balance, + ); + + Ok(result.map(|action| action.into())) + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/transformer/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/transformer/mod.rs index 680ce0c40e1..8031b923e5b 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/transformer/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/transformer/mod.rs @@ -9,6 +9,7 @@ use crate::execution::validation::state_transition::identity_create_from_address use crate::execution::validation::state_transition::identity_create_from_shielded_pool::StateTransitionIdentityCreateFromShieldedPoolTransitionActionTransformer; use crate::execution::validation::state_transition::identity_top_up::StateTransitionIdentityTopUpTransitionActionTransformer; use crate::execution::validation::state_transition::identity_top_up_from_shielded_pool::StateTransitionIdentityTopUpFromShieldedPoolTransitionActionTransformer; +use crate::execution::validation::state_transition::private_document_create_from_shielded_pool::StateTransitionPrivateDocumentCreateFromShieldedPoolTransitionActionTransformer; use crate::execution::validation::state_transition::shield::StateTransitionShieldTransitionActionTransformer; use crate::execution::validation::state_transition::shield_from_asset_lock::StateTransitionShieldFromAssetLockTransitionActionTransformer; use crate::execution::validation::state_transition::shielded_transfer::StateTransitionShieldedTransferTransitionActionTransformer; @@ -370,6 +371,10 @@ impl StateTransitionActionTransformer for StateTransition { .transform_into_action_for_token_purchase_from_shielded_pool_transition( platform, tx, ), + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => st + .transform_into_action_for_private_document_create_from_shielded_pool_transition( + platform, tx, + ), StateTransition::ShieldFromAssetLock(st) => { let signable_bytes = self.signable_bytes()?; st.transform_into_action_for_shield_from_asset_lock_transition( diff --git a/packages/rs-drive-abci/tests/strategy_tests/verify_state_transitions.rs b/packages/rs-drive-abci/tests/strategy_tests/verify_state_transitions.rs index 6fdb9ebb1af..73f294ec144 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/verify_state_transitions.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/verify_state_transitions.rs @@ -1379,7 +1379,8 @@ pub(crate) fn verify_state_transitions_were_or_were_not_executed( | StateTransitionAction::IdentityTopUpFromShieldedPoolAction(_) | StateTransitionAction::TokenShieldedTransferWithShieldedFeeAction(_) | StateTransitionAction::TokenUnshieldWithShieldedFeeAction(_) - | StateTransitionAction::TokenPurchaseFromShieldedPoolAction(_) => { + | StateTransitionAction::TokenPurchaseFromShieldedPoolAction(_) + | StateTransitionAction::PrivateDocumentCreateFromShieldedPoolAction(_) => { // The strategy harness does not generate shielded transitions (no shielded // `OperationType`), so their proof-verification roundtrip isn't exercised here. // IdentityCreateFromShieldedPool's strict prove/verify is covered by the unit diff --git a/packages/rs-drive/src/drive/document/private_document_store/mod.rs b/packages/rs-drive/src/drive/document/private_document_store/mod.rs index ee2f7e33cb6..0d9bbb502cf 100644 --- a/packages/rs-drive/src/drive/document/private_document_store/mod.rs +++ b/packages/rs-drive/src/drive/document/private_document_store/mod.rs @@ -3,6 +3,8 @@ //! from the shielded pool. They live in a grovedb `PrivateDocumentStore` at the type's key //! `[0]`, where every other type keeps its primary-key tree, and the type has no index trees. +#[cfg(feature = "server")] +use crate::drive::document::paths::contract_document_type_path; #[cfg(feature = "server")] use crate::drive::Drive; use crate::error::drive::DriveError; @@ -14,12 +16,16 @@ use crate::fees::op::LowLevelDriveOperation; use crate::util::grove_operations::DirectQueryType; #[cfg(feature = "server")] use crate::util::storage_flags::StorageFlags; +#[cfg(feature = "server")] +use dpp::block::block_info::BlockInfo; use dpp::data_contract::document_type::accessors::{DocumentTypeV0Getters, DocumentTypeV2Getters}; use dpp::data_contract::document_type::{DocumentTypeRef, PrivateDocumentConfig}; use dpp::version::PlatformVersion; #[cfg(feature = "server")] use grovedb::{Element, TransactionArg}; #[cfg(feature = "server")] +use v0::append_private_document_entries_operations_v0; +#[cfg(feature = "server")] use v0::insert_empty_private_document_store_operation_v0; use v0::private_document_store_chunk_power_v0; @@ -195,6 +201,40 @@ impl Drive { )))), } } + + /// Whether the store of the private type `document_type_name` of `contract_id` takes + /// entries of `entry_size` bytes: the element at the type's key `[0]` is a private document + /// store whose committed stored entry size is Drive's header and `entry_size`. A + /// `PrivateDocumentCreateFromShieldedPool` asks before execution, where an append to a + /// missing store, or one of another size, would fail. The read is not billed: the + /// transition pays a fixed fee. + pub fn private_document_store_takes_entries( + &self, + contract_id: &[u8; 32], + document_type_name: &str, + entry_size: u32, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let type_path = contract_document_type_path(contract_id, document_type_name); + let element = self.grove_get_raw_optional( + type_path.as_slice().into(), + &[PRIVATE_DOCUMENT_STORE_KEY], + DirectQueryType::StatefulDirectQuery, + transaction, + &mut vec![], + &platform_version.drive, + )?; + let Some(stored_entry_size) = entry_size.checked_add(PRIVATE_DOCUMENT_ENTRY_HEADER_SIZE) + else { + return Ok(false); + }; + Ok(matches!( + element.as_ref().map(Element::underlying), + Some(Element::PrivateDocumentStore(_, committed_entry_size, _, _)) + if *committed_entry_size == stored_entry_size + )) + } } /// The chunk power of a private type's store whose stored entries take `stored_entry_size` @@ -258,6 +298,44 @@ pub fn insert_empty_private_document_store_operation( } } +/// The operations appending `entries`, in order, to the store of the private type +/// `document_type_name` of `contract_id`, each after Drive's header: the height and then the +/// time in milliseconds of `block_info` (`encode_stored_private_document_entry`). Every entry +/// must take the type's `entrySize`; GroveDB refuses an entry of another size, so the caller +/// checks it before execution. Only a protocol version admitting private types selects a +/// version of it. +#[cfg(feature = "server")] +pub fn append_private_document_entries_operations( + contract_id: &[u8; 32], + document_type_name: &str, + entries: &[Vec], + block_info: &BlockInfo, + platform_version: &PlatformVersion, +) -> Result, Error> { + match platform_version + .drive + .methods + .document + .append_private_document_entries_operations + { + Some(0) => Ok(append_private_document_entries_operations_v0( + contract_id, + document_type_name, + entries, + block_info, + )), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "append_private_document_entries_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "append_private_document_entries_operations".to_string(), + known_versions: vec![0], + })), + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs b/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs index 0f48acf1879..cdecb29165c 100644 --- a/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs +++ b/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs @@ -1,6 +1,9 @@ #[cfg(feature = "server")] +use crate::drive::document::paths::contract_document_type_path_vec; +#[cfg(feature = "server")] use crate::drive::document::private_document_store::{ - private_document_stored_entry_size, PRIVATE_DOCUMENT_STORE_KEY, + encode_stored_private_document_entry, private_document_stored_entry_size, + PRIVATE_DOCUMENT_STORE_KEY, }; use crate::error::drive::DriveError; use crate::error::Error; @@ -9,9 +12,13 @@ use crate::fees::op::LowLevelDriveOperation; #[cfg(feature = "server")] use crate::util::storage_flags::StorageFlags; #[cfg(feature = "server")] +use dpp::block::block_info::BlockInfo; +#[cfg(feature = "server")] use dpp::data_contract::document_type::PrivateDocumentConfig; use dpp::version::PlatformVersion; #[cfg(feature = "server")] +use grovedb::batch::QualifiedGroveDbOp; +#[cfg(feature = "server")] use grovedb::Element; /// The chunk power of a store whose stored entries take `stored_entry_size` bytes: the largest @@ -62,3 +69,31 @@ pub(super) fn insert_empty_private_document_store_operation_v0( store, )) } + +/// One GroveDB append per entry, into the store at the type's key `[0]`, each entry after Drive's +/// header. The entries land at consecutive positions in this order. +#[cfg(feature = "server")] +pub(super) fn append_private_document_entries_operations_v0( + contract_id: &[u8; 32], + document_type_name: &str, + entries: &[Vec], + block_info: &BlockInfo, +) -> Vec { + let mut store_path = contract_document_type_path_vec(contract_id, document_type_name); + store_path.push(vec![PRIVATE_DOCUMENT_STORE_KEY]); + entries + .iter() + .map(|entry| { + LowLevelDriveOperation::GroveOperation( + QualifiedGroveDbOp::private_document_store_insert_op( + store_path.clone(), + encode_stored_private_document_entry( + block_info.height, + block_info.time_ms, + entry, + ), + ), + ) + }) + .collect() +} diff --git a/packages/rs-drive/src/prove/prove_state_transition/v0/mod.rs b/packages/rs-drive/src/prove/prove_state_transition/v0/mod.rs index 0d543b2550b..6dcc86658d6 100644 --- a/packages/rs-drive/src/prove/prove_state_transition/v0/mod.rs +++ b/packages/rs-drive/src/prove/prove_state_transition/v0/mod.rs @@ -619,6 +619,23 @@ impl Drive { grovedb::SizedQuery::new(query, Some(count), None), ) } + // The bundle's spent nullifiers, as a shielded transfer's. The entries are found by + // scanning the store: the sender knows them. + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => { + use crate::drive::shielded::paths::shielded_credit_pool_nullifiers_path_vec; + use dpp::state_transition::private_document_create_from_shielded_pool_transition::accessors::PrivateDocumentCreateFromShieldedPoolTransitionAccessorsV0; + + let nullifier_keys: Vec> = st.nullifiers(); + let count = nullifier_keys.len() as u16; + + let mut query = grovedb::Query::new(); + query.insert_keys(nullifier_keys); + + PathQuery::new( + shielded_credit_pool_nullifiers_path_vec(), + grovedb::SizedQuery::new(query, Some(count), None), + ) + } StateTransition::Shield(st) => { Drive::balances_for_clear_addresses_query(st.inputs().keys()) } diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/mod.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/mod.rs index 2355587538f..a5578043910 100644 --- a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/mod.rs +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/mod.rs @@ -137,6 +137,9 @@ impl DriveHighLevelOperationConverter for StateTransitionAction { StateTransitionAction::TokenPurchaseFromShieldedPoolAction(action) => { action.into_high_level_drive_operations(epoch, platform_version) } + StateTransitionAction::PrivateDocumentCreateFromShieldedPoolAction(action) => { + action.into_high_level_drive_operations(epoch, platform_version) + } StateTransitionAction::ShieldedTransferAction(shielded_transfer_action) => { shielded_transfer_action.into_high_level_drive_operations(epoch, platform_version) } diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/mod.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/mod.rs index 8402f09d04a..8477662df9a 100644 --- a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/mod.rs +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/mod.rs @@ -1,5 +1,6 @@ mod identity_create_from_shielded_pool_transition; mod identity_top_up_from_shielded_pool_transition; +mod private_document_create_from_shielded_pool_transition; mod shield_from_asset_lock_transition; mod shield_from_identity_transition; mod shield_transition; diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs new file mode 100644 index 00000000000..02b832f855a --- /dev/null +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs @@ -0,0 +1,330 @@ +use super::{insert_notes, insert_nullifiers, update_balance}; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::state_transition_action::action_convert_to_operations::DriveHighLevelOperationConverter; +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::PrivateDocumentCreateFromShieldedPoolTransitionAction; +use crate::util::batch::drive_op_batch::PrivateDocumentOperationType; +use crate::util::batch::DriveOperation; +use dpp::block::epoch::Epoch; +use dpp::version::PlatformVersion; + +impl DriveHighLevelOperationConverter for PrivateDocumentCreateFromShieldedPoolTransitionAction { + fn into_high_level_drive_operations<'a>( + self, + _epoch: &Epoch, + platform_version: &PlatformVersion, + ) -> Result>, Error> { + match platform_version + .drive + .methods + .state_transitions + .convert_to_high_level_operations + .private_document_create_from_shielded_pool_transition + { + 0 => match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(v0) => { + let mut ops: Vec> = Vec::new(); + + // 1. The spent nullifiers, known not to exist after validation + insert_nullifiers(&mut ops, &v0.notes); + + // 2. The change notes + insert_notes(&mut ops, &v0.notes); + + // 3. The pool loses the fee, the whole value balance, to the fee pools + let new_total_balance = v0 + .current_total_balance + .checked_sub(v0.fee_amount) + .ok_or_else(|| { + Error::Drive(DriveError::CorruptedDriveState( + "shielded pool total balance underflow when subtracting the \ + private document create fee" + .to_string(), + )) + })?; + update_balance(&mut ops, new_total_balance); + + // 4. The entries, in order. The transformer read the store and checked + // every entry takes the type's entry size, so the append cannot fail. + ops.push(DriveOperation::PrivateDocumentOperation( + PrivateDocumentOperationType::AppendEntries { + contract_id: v0.data_contract_id.to_buffer(), + document_type_name: v0.document_type_name, + entries: v0.entries, + }, + )); + + Ok(ops) + } + }, + version => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "PrivateDocumentCreateFromShieldedPoolTransitionAction::into_high_level_drive_operations" + .to_string(), + known_versions: vec![0], + received: version, + })), + } + } +} + +#[cfg(test)] +mod tests { + use super::super::fee_floor_support; + use super::*; + use crate::drive::document::private_document_store::PRIVATE_DOCUMENT_STORE_KEY; + use crate::drive::{Drive, RootTree}; + use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; + use crate::state_transition_action::shielded::ShieldedActionNote; + use crate::util::batch::drive_op_batch::ShieldedPoolOperationType; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::data_contract::conversion::value::v0::DataContractValueConversionMethodsV0; + use dpp::data_contract::DataContract; + use dpp::platform_value::string_encoding::Encoding; + use dpp::platform_value::Identifier; + use dpp::shielded::compute_private_document_create_from_shielded_pool_fee; + use grovedb::TransactionArg; + use serde_json::json; + + const CONTRACT_ID: [u8; 32] = [7; 32]; + + fn note(i: u8) -> ShieldedActionNote { + ShieldedActionNote { + nullifier: [i; 32], + cmx: [i.wrapping_add(100); 32], + cv_net: [0x33; 32], + encrypted_note: vec![0x44; 216], + } + } + + fn action( + notes: Vec, + entries: Vec>, + fee_amount: u64, + current_total_balance: u64, + ) -> PrivateDocumentCreateFromShieldedPoolTransitionAction { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0( + PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { + data_contract_id: Identifier::from(CONTRACT_ID), + document_type_name: "secret".to_string(), + entries, + notes, + anchor: [9; 32], + fee_amount, + current_total_balance, + }, + ) + } + + fn ops( + action: PrivateDocumentCreateFromShieldedPoolTransitionAction, + ) -> Result>, Error> { + action.into_high_level_drive_operations(&Epoch::new(0).unwrap(), PlatformVersion::latest()) + } + + #[test] + fn should_record_nullifiers_and_change_notes_lower_the_pool_and_append_the_entries_last() { + let entries = vec![vec![1u8; 24], vec![2u8; 24]]; + let ops = + ops(action(vec![note(1), note(2)], entries.clone(), 500, 10_000)).expect("operations"); + // InsertNullifiers + 2 InsertNote + UpdateTotalBalance + AppendEntries + assert_eq!(ops.len(), 5); + assert!(matches!( + &ops[0], + DriveOperation::ShieldedPoolOperation(ShieldedPoolOperationType::InsertNullifiers { nullifiers }) + if nullifiers == &vec![[1u8; 32], [2u8; 32]] + )); + assert!(matches!( + &ops[3], + DriveOperation::ShieldedPoolOperation(ShieldedPoolOperationType::UpdateTotalBalance { + new_total_balance: 9_500 + }) + )); + match &ops[4] { + DriveOperation::PrivateDocumentOperation( + PrivateDocumentOperationType::AppendEntries { + contract_id, + document_type_name, + entries: appended, + }, + ) => { + assert_eq!(contract_id, &CONTRACT_ID); + assert_eq!(document_type_name, "secret"); + assert_eq!(appended, &entries); + } + other => panic!("expected AppendEntries, got {other:?}"), + } + } + + #[test] + fn should_refuse_a_fee_over_the_pool_total() { + assert!(ops(action(vec![note(1)], vec![vec![1u8; 24]], 10_001, 10_000)).is_err()); + } + + /// A drive holding a contract with the private `secret` type of `entry_size`-byte entries. + fn drive_with_private_type(entry_size: u32) -> Drive { + let drive = setup_drive_with_initial_state_structure(None); + let platform_version = PlatformVersion::latest(); + let contract = DataContract::from_value( + dpp::platform_value::to_value(json!({ + "$formatVersion": "1", + "id": Identifier::from(CONTRACT_ID).to_string(Encoding::Base58), + "ownerId": Identifier::from([8; 32]).to_string(Encoding::Base58), + "version": 1, + "documentSchemas": { + "secret": { + "type": "object", + "private": { "entrySize": entry_size }, + "documentsMutable": false, + "canBeDeleted": false, + "properties": { + "accountReference": { + "type": "integer", "minimum": 0, "maximum": 4294967295u64, + "position": 0 + } + }, + "additionalProperties": false + } + } + })) + .expect("the contract converts"), + true, + platform_version, + ) + .expect("the contract registers"); + drive + .apply_contract( + &contract, + BlockInfo::default(), + true, + None, + None, + platform_version, + ) + .expect("expected to register the contract"); + drive + } + + fn store_count(drive: &Drive, transaction: TransactionArg) -> u64 { + let type_path: [&[u8]; 4] = [ + &[RootTree::DataContractDocuments as u8], + &CONTRACT_ID, + &[1], + b"secret", + ]; + drive + .grove + .private_document_store_count( + type_path.as_slice(), + &[PRIVATE_DOCUMENT_STORE_KEY], + transaction, + &PlatformVersion::latest().drive.grove_version, + ) + .unwrap() + .expect("the store's count") + } + + /// The fixed fee of a private document create, which the pool pays, covers what its + /// operations meter, and its entries' storage share covers the entries' metered storage, so + /// the pool-paid split (`storage = min(metered storage, fee)`) never zeroes the proposer's + /// share. Checked for an entry size at every chunk power a store can take (11 down to 6), + /// for one entry and for the most a transition carries, over appends that cross a chunk's + /// compaction. + #[test] + fn should_charge_a_fixed_fee_covering_the_metered_cost_at_every_chunk_power() { + let platform_version = PlatformVersion::latest(); + let storage = &platform_version.fee_version.storage; + let private_document = &platform_version.fee_version.private_document; + let per_byte = + storage.storage_disk_usage_credit_per_byte + storage.storage_processing_credit_per_byte; + let max_entries = platform_version + .system_limits + .max_private_document_entries_per_transition + .expect("protocol version 15 admits private document creates") + as usize; + let mut nonce = 0u8; + let mut note_index = 0u32; + for entry_size in [1u32, 200, 400, 585, 1500, 3000, 4096] { + let drive = drive_with_private_type(entry_size); + let transaction = drive.grove.start_transaction(); + let entry_storage_allowance = + (entry_size as u64 + private_document.entry_overhead_bytes) * per_byte; + // 70 appends: at entry size 4096 (chunk power 6, 64 entries to a chunk) they cross a + // chunk's compaction. GroveDB charges every append of a store the same, the + // compacting one included, so the smaller sizes' appends meter what any of theirs + // does. + let mut appended = 0u64; + for entry_count in [1, max_entries, max_entries, 1, 1] { + nonce = nonce.wrapping_add(1); + // Distinct entries, as basic structure requires. + let entries: Vec> = (0..entry_count) + .map(|i| { + let mut entry = vec![nonce; entry_size as usize]; + entry[0] = i as u8; + entry + }) + .collect(); + let fee = compute_private_document_create_from_shielded_pool_fee( + 2, + entry_count, + entry_size, + platform_version, + ) + .expect("fee"); + note_index += 2; + let operations = ops(action( + vec![ + fee_floor_support::note(note_index), + fee_floor_support::note(note_index + 1), + ], + entries.clone(), + fee, + u64::MAX / 2, + )) + .expect("operations"); + let metered = drive + .apply_drive_operations( + operations, + true, + &BlockInfo::default(), + Some(&transaction), + platform_version, + None, + ) + .expect("apply"); + assert!( + fee >= metered.total_base_fee(), + "entry size {entry_size}, {entry_count} entries: fixed fee {fee} must cover \ + the metered {}", + metered.total_base_fee() + ); + // The entries alone, metered on their own append. + let entries_metered = drive + .apply_drive_operations( + vec![DriveOperation::PrivateDocumentOperation( + PrivateDocumentOperationType::AppendEntries { + contract_id: CONTRACT_ID, + document_type_name: "secret".to_string(), + entries, + }, + )], + true, + &BlockInfo::default(), + Some(&transaction), + platform_version, + None, + ) + .expect("apply the entries"); + assert!( + entry_count as u64 * entry_storage_allowance >= entries_metered.storage_fee, + "entry size {entry_size}, {entry_count} entries: the entries' storage \ + allowance {} must cover their metered storage {}", + entry_count as u64 * entry_storage_allowance, + entries_metered.storage_fee + ); + appended += 2 * entry_count as u64; + } + assert_eq!(store_count(&drive, Some(&transaction)), appended); + } + } +} diff --git a/packages/rs-drive/src/state_transition_action/mod.rs b/packages/rs-drive/src/state_transition_action/mod.rs index a65dc122083..e61a2195030 100644 --- a/packages/rs-drive/src/state_transition_action/mod.rs +++ b/packages/rs-drive/src/state_transition_action/mod.rs @@ -34,6 +34,7 @@ use crate::state_transition_action::identity::identity_update::IdentityUpdateTra use crate::state_transition_action::identity::masternode_vote::MasternodeVoteTransitionAction; use crate::state_transition_action::shielded::identity_create_from_shielded_pool::IdentityCreateFromShieldedPoolTransitionAction; use crate::state_transition_action::shielded::identity_top_up_from_shielded_pool::IdentityTopUpFromShieldedPoolTransitionAction; +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::PrivateDocumentCreateFromShieldedPoolTransitionAction; use crate::state_transition_action::shielded::shield::ShieldTransitionAction; use crate::state_transition_action::shielded::shield_from_asset_lock::ShieldFromAssetLockTransitionAction; use crate::state_transition_action::shielded::shield_from_identity::ShieldFromIdentityTransitionAction; @@ -136,6 +137,10 @@ pub enum StateTransitionAction { TokenUnshieldWithShieldedFeeAction(TokenUnshieldWithShieldedFeeTransitionAction), /// tokens bought out of the credit pool and minted into the token's shielded pool TokenPurchaseFromShieldedPoolAction(TokenPurchaseFromShieldedPoolTransitionAction), + /// entries appended to a private document type's store, paid from the credit pool + PrivateDocumentCreateFromShieldedPoolAction( + PrivateDocumentCreateFromShieldedPoolTransitionAction, + ), } impl StateTransitionAction { @@ -213,6 +218,9 @@ impl StateTransitionAction { | StateTransitionAction::TokenPurchaseFromShieldedPoolAction(_) => { UserFeeIncrease::default() // 0 (fee is locked by Orchard binding signatures) } + StateTransitionAction::PrivateDocumentCreateFromShieldedPoolAction(_) => { + UserFeeIncrease::default() // 0 (fee is locked by Orchard binding signature) + } } } } diff --git a/packages/rs-drive/src/state_transition_action/shielded/mod.rs b/packages/rs-drive/src/state_transition_action/shielded/mod.rs index 6fde72ed8ea..1146c4b185c 100644 --- a/packages/rs-drive/src/state_transition_action/shielded/mod.rs +++ b/packages/rs-drive/src/state_transition_action/shielded/mod.rs @@ -2,6 +2,8 @@ pub mod identity_create_from_shielded_pool; /// IdentityTopUpFromShieldedPool transition action pub mod identity_top_up_from_shielded_pool; +/// PrivateDocumentCreateFromShieldedPool transition action +pub mod private_document_create_from_shielded_pool; /// Shield transition action pub mod shield; /// Shield from asset lock transition action diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs new file mode 100644 index 00000000000..09f663a91ad --- /dev/null +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs @@ -0,0 +1,62 @@ +/// transformer +pub mod transformer; +/// v0 +pub mod v0; + +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; +use crate::state_transition_action::shielded::ShieldedActionNote; +use derive_more::From; +use dpp::fee::Credits; +use dpp::platform_value::Identifier; + +/// Private document create from shielded pool transition action +#[derive(Debug, Clone, From)] +pub enum PrivateDocumentCreateFromShieldedPoolTransitionAction { + /// v0 + V0(PrivateDocumentCreateFromShieldedPoolTransitionActionV0), +} + +impl PrivateDocumentCreateFromShieldedPoolTransitionAction { + /// The contract that defines the private document type + pub fn data_contract_id(&self) -> Identifier { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => t.data_contract_id, + } + } + /// The private document type the entries are appended to + pub fn document_type_name(&self) -> &str { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => &t.document_type_name, + } + } + /// The entries, in the order they are appended + pub fn entries(&self) -> &[Vec] { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => &t.entries, + } + } + /// Notes (spent nullifiers plus change outputs) + pub fn notes(&self) -> &[ShieldedActionNote] { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => &t.notes, + } + } + /// The anchor the spend was proven against + pub fn anchor(&self) -> &[u8; 32] { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => &t.anchor, + } + } + /// The fee, the whole amount leaving the pool, routed to the fee pools + pub fn fee_amount(&self) -> Credits { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => t.fee_amount, + } + } + /// Pool total balance read at transform time + pub fn current_total_balance(&self) -> Credits { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => t.current_total_balance, + } + } +} diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs new file mode 100644 index 00000000000..b31982acef6 --- /dev/null +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs @@ -0,0 +1,26 @@ +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::PrivateDocumentCreateFromShieldedPoolTransitionAction; +use dpp::fee::Credits; +use dpp::prelude::ConsensusValidationResult; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; + +impl PrivateDocumentCreateFromShieldedPoolTransitionAction { + /// Transforms the state transition into an action, once the transformer has checked the + /// pool, the target type and the entries' size against state + pub fn try_from_transition( + value: &PrivateDocumentCreateFromShieldedPoolTransition, + current_total_balance: Credits, + fee_amount: Credits, + ) -> ConsensusValidationResult { + match value { + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { + PrivateDocumentCreateFromShieldedPoolTransitionActionV0::try_from_transition( + v0, + current_total_balance, + fee_amount, + ) + .map(|action| action.into()) + } + } + } +} diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs new file mode 100644 index 00000000000..5463da10796 --- /dev/null +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs @@ -0,0 +1,25 @@ +mod transformer; + +use crate::state_transition_action::shielded::ShieldedActionNote; +use dpp::fee::Credits; +use dpp::platform_value::Identifier; + +/// Entries appended to a private document type's store, paid from the credit shielded pool, +/// version 0. +#[derive(Debug, Clone)] +pub struct PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { + /// The contract that defines the private document type + pub data_contract_id: Identifier, + /// The private document type the entries are appended to + pub document_type_name: String, + /// The entries, in the order they are appended, each the type's `entrySize` + pub entries: Vec>, + /// Notes built 1:1 from the on-wire Orchard actions + pub notes: Vec, + /// The anchor the spend was proven against + pub anchor: [u8; 32], + /// The fee, the bundle's whole value balance, routed to the fee pools + pub fee_amount: Credits, + /// Pool total balance read at transform time + pub current_total_balance: Credits, +} diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs new file mode 100644 index 00000000000..d53ad932cd9 --- /dev/null +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs @@ -0,0 +1,29 @@ +use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; +use crate::state_transition_action::shielded::ShieldedActionNote; +use dpp::fee::Credits; +use dpp::prelude::ConsensusValidationResult; +use dpp::state_transition::state_transitions::shielded::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; + +impl PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { + /// Builds the v0 action from the v0 transition, the pool total, and the fee + pub fn try_from_transition( + value: &PrivateDocumentCreateFromShieldedPoolTransitionV0, + current_total_balance: Credits, + fee_amount: Credits, + ) -> ConsensusValidationResult { + let notes: Vec = + value.actions.iter().map(ShieldedActionNote::from).collect(); + + ConsensusValidationResult::new_with_data( + PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { + data_contract_id: value.data_contract_id, + document_type_name: value.document_type_name.clone(), + entries: value.entries.iter().map(|entry| entry.to_vec()).collect(), + notes, + anchor: value.anchor, + fee_amount, + current_total_balance, + }, + ) + } +} diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/mod.rs index b9d3d4c1051..da87c27a5ca 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/mod.rs @@ -9,6 +9,7 @@ pub(crate) mod finalize_task; mod group; mod identity; mod prefunded_specialized_balance; +mod private_document; mod shielded; mod system; mod token; @@ -38,6 +39,7 @@ pub use document::UpdateOperationInfo; pub use group::GroupOperationType; pub use identity::IdentityOperationType; pub use prefunded_specialized_balance::PrefundedSpecializedBalanceOperationType; +pub use private_document::PrivateDocumentOperationType; pub use shielded::ShieldedPoolOperationType; pub use system::SystemOperationType; pub use token::TokenOperationType; @@ -117,6 +119,8 @@ pub enum DriveOperation<'a> { AddressFundsOperation(AddressFundsOperationType), /// A shielded pool operation ShieldedPoolOperation(ShieldedPoolOperationType), + /// An operation on the store of a private document type + PrivateDocumentOperation(PrivateDocumentOperationType), /// A vote poll operation: the opening of a yes/no vote poll VotePollOperation(VotePollOperationType), /// A single low level groveDB operation @@ -197,6 +201,15 @@ impl DriveLowLevelOperationConverter for DriveOperation<'_> { platform_version, ) } + DriveOperation::PrivateDocumentOperation(private_document_operation_type) => { + private_document_operation_type.into_low_level_drive_operations( + drive, + estimated_costs_only_with_layer_info, + block_info, + transaction, + platform_version, + ) + } DriveOperation::VotePollOperation(vote_poll_operation_type) => vote_poll_operation_type .into_low_level_drive_operations( drive, diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs b/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs new file mode 100644 index 00000000000..d53e97c166e --- /dev/null +++ b/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs @@ -0,0 +1,49 @@ +use crate::drive::document::private_document_store::append_private_document_entries_operations; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::block::block_info::BlockInfo; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +/// Operations on the store of a private document type (protocol version 15) +#[derive(Clone, Debug)] +pub enum PrivateDocumentOperationType { + /// Append entries to the store of a private document type, in order, each after Drive's + /// header: the block's height and time. + AppendEntries { + /// The contract that defines the private document type + contract_id: [u8; 32], + /// The private document type + document_type_name: String, + /// The entries, each exactly the type's `entrySize` + entries: Vec>, + }, +} + +impl DriveLowLevelOperationConverter for PrivateDocumentOperationType { + fn into_low_level_drive_operations( + self, + _drive: &Drive, + _estimated_costs_only_with_layer_info: &mut Option, + block_info: &BlockInfo, + _transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match self { + PrivateDocumentOperationType::AppendEntries { + contract_id, + document_type_name, + entries, + } => append_private_document_entries_operations( + &contract_id, + &document_type_name, + &entries, + block_info, + platform_version, + ), + } + } +} diff --git a/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs b/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs index c0ab82e2d34..d7d57d828db 100644 --- a/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs +++ b/packages/rs-drive/src/verify/state_transition/verify_state_transition_was_executed_with_proof/v0/mod.rs @@ -2162,6 +2162,31 @@ impl Drive { Ok((root_hash, VerifiedShieldedNullifiers(statuses))) } + StateTransition::PrivateDocumentCreateFromShieldedPool(st) => { + use dpp::state_transition::private_document_create_from_shielded_pool_transition::accessors::PrivateDocumentCreateFromShieldedPoolTransitionAccessorsV0; + use dpp::state_transition::proof_result::StateTransitionProofResult::VerifiedShieldedNullifiers; + + let nullifier_keys: Vec> = st.nullifiers(); + + let (root_hash, statuses) = Drive::verify_shielded_nullifiers( + proof, + &nullifier_keys, + false, + platform_version, + )?; + + // All nullifiers must be marked as spent + for (nf, is_spent) in &statuses { + if !is_spent { + return Err(Error::Proof(ProofError::IncorrectProof(format!( + "nullifier {} was not found as spent in the proof", + hex::encode(nf) + )))); + } + } + + Ok((root_hash, VerifiedShieldedNullifiers(statuses))) + } StateTransition::ShieldedWithdrawal(st) => { use crate::drive::shielded::paths::shielded_credit_pool_nullifiers_path_vec; use dpp::data_contracts::withdrawals_contract; @@ -3254,6 +3279,10 @@ impl Drive { // Balance and pool snapshots at the proof's block: not bindable to one transition. StateTransition::TokenUnshieldWithShieldedFee(_) => false, StateTransition::TokenPurchaseFromShieldedPool(_) => false, + // Only the resulting state: the spent nullifiers name the notes paying the fee, not + // the entries, so a competing create spending the same notes for other entries + // leaves the same evidence. + StateTransition::PrivateDocumentCreateFromShieldedPool(_) => false, }; Ok(binds) diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs index f0e2ec445bf..0757e59807f 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs @@ -285,9 +285,11 @@ mod tests { } /// Drive creates a private type's store through `insert_empty_private_document_store_operation`, - /// sized from the private document store limits, and reads, proves and verifies a page of - /// it through `fetch_private_document_entries`, `prove_private_document_entries` and - /// `verify_private_document_entries`: a protocol version parsing private types without + /// sized from the private document store limits, reads, proves and verifies a page of it + /// through `fetch_private_document_entries`, `prove_private_document_entries` and + /// `verify_private_document_entries`, and appends a private document create's entries, + /// at most `max_private_document_entries_per_transition` of them, through + /// `append_private_document_entries_operations`: a protocol version parsing private types without /// them could not register or read one, and one setting them where no type is private /// states a rule nothing reads. #[test] @@ -346,6 +348,19 @@ mod tests { .prove_private_document_entries .is_some(), ), + ( + "append_private_document_entries_operations", + platform_version + .drive + .methods + .document + .append_private_document_entries_operations + .is_some(), + ), + ( + "max_private_document_entries_per_transition", + limits.max_private_document_entries_per_transition.is_some(), + ), ( "verify_private_document_entries", platform_version diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/mod.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/mod.rs index 530d1ce0e71..93461710789 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/mod.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/mod.rs @@ -54,6 +54,7 @@ pub struct DPPStateTransitionSerializationVersions { pub token_shielded_transfer_with_shielded_fee_state_transition: FeatureVersionBounds, pub token_unshield_with_shielded_fee_state_transition: FeatureVersionBounds, pub token_purchase_from_shielded_pool_state_transition: FeatureVersionBounds, + pub private_document_create_from_shielded_pool_state_transition: FeatureVersionBounds, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v1.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v1.rs index 8d0841dc9be..a0dbfb79fc9 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v1.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v1.rs @@ -204,4 +204,9 @@ pub const STATE_TRANSITION_SERIALIZATION_VERSIONS_V1: DPPStateTransitionSerializ max_version: 0, default_current_version: 0, }, + private_document_create_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, }; diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v2.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v2.rs index 14b740316a5..0b2dbb148e2 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v2.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v2.rs @@ -204,4 +204,9 @@ pub const STATE_TRANSITION_SERIALIZATION_VERSIONS_V2: DPPStateTransitionSerializ max_version: 0, default_current_version: 0, }, + private_document_create_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, }; diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v3.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v3.rs index 4a437149a7c..f96754f6579 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v3.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_state_transition_serialization_versions/v3.rs @@ -223,4 +223,9 @@ pub const STATE_TRANSITION_SERIALIZATION_VERSIONS_V3: DPPStateTransitionSerializ max_version: 0, default_current_version: 0, }, + private_document_create_from_shielded_pool_state_transition: FeatureVersionBounds { + min_version: 0, + max_version: 0, + default_current_version: 0, + }, }; diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/mod.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/mod.rs index 8af14768f5e..3f2a0e083c2 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/mod.rs @@ -170,6 +170,8 @@ pub struct DriveAbciStateTransitionValidationVersions { DriveAbciStateTransitionValidationVersion, pub token_purchase_from_shielded_pool_state_transition: DriveAbciStateTransitionValidationVersion, + pub private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v1.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v1.rs index a4d3f23fd29..48d526658a3 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v1.rs @@ -342,6 +342,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V1: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 0, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs index 23fed710b80..ea0765cc6de 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs @@ -443,6 +443,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V10: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: Some(0), + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v2.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v2.rs index 432bbaa06ec..8b05b02e8aa 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v2.rs @@ -342,6 +342,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V2: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 0, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v3.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v3.rs index a568addcbc8..6fa1e0b8ff3 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v3.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v3.rs @@ -342,6 +342,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V3: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 0, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v4.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v4.rs index f5c45d3f19c..18de98b3f8b 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v4.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v4.rs @@ -345,6 +345,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V4: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, // <---- changed this has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v5.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v5.rs index dbd541308a2..b51550463b0 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v5.rs @@ -346,6 +346,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V5: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v6.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v6.rs index a1b57e3297e..baf9dea15d5 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v6.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v6.rs @@ -349,6 +349,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V6: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v7.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v7.rs index cf6c289f4ed..96ea9ba6999 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v7.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v7.rs @@ -343,6 +343,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V7: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v8.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v8.rs index 633dc8c016f..30c6b50ea64 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v8.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v8.rs @@ -397,6 +397,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V8: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v9.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v9.rs index b31dea6dff2..39725f600a3 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v9.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v9.rs @@ -393,6 +393,15 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V9: DriveAbciValidationVersions = state: 0, transform_into_action: 0, }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: None, + advanced_structure: None, + identity_signatures: None, + nonce: None, + state: 0, + transform_into_action: 0, + }, }, has_nonce_validation: 1, has_address_witness_validation: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/mod.rs index 35deb9762cc..cbd815cb9ef 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/mod.rs @@ -37,6 +37,11 @@ pub struct DriveDocumentMethodVersions { /// `verify_private_document_entries` checks. `Some(0)` from /// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` (protocol version 15), `None` before. pub prove_private_document_entries: OptionalFeatureVersion, + /// `append_private_document_entries_operations`: the operations a + /// `PrivateDocumentCreateFromShieldedPool` appends its entries with, each after Drive's + /// header (the block's height and time). `Some(0)` from `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` + /// (protocol version 15), `None` before. + pub append_private_document_entries_operations: OptionalFeatureVersion, } /// Drive methods of document expiry: the expirations tree under `Misc` that indexes every diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v1.rs index 8193461a1f4..f75c5277222 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v1.rs @@ -93,6 +93,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V1: DriveDocumentMethodVersions = insert_empty_private_document_store_operation: None, // no private document type before protocol version 15 fetch_private_document_entries: None, // no private document type before protocol version 15 prove_private_document_entries: None, + append_private_document_entries_operations: None, // no private document type before protocol version 15 expiration: DriveDocumentExpirationMethodVersions { insert_document_ttl_trees: 0, add_document_expiration_operations: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v2.rs index d5e51cc9f31..6d7a5711d5c 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v2.rs @@ -127,6 +127,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V2: DriveDocumentMethodVersions = insert_empty_private_document_store_operation: None, // no private document type before protocol version 15 fetch_private_document_entries: None, // no private document type before protocol version 15 prove_private_document_entries: None, + append_private_document_entries_operations: None, // no private document type before protocol version 15 expiration: DriveDocumentExpirationMethodVersions { insert_document_ttl_trees: 0, add_document_expiration_operations: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v3.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v3.rs index ef1dba68dec..6e66106a5ff 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v3.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v3.rs @@ -124,6 +124,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V3: DriveDocumentMethodVersions = insert_empty_private_document_store_operation: None, // no private document type before protocol version 15 fetch_private_document_entries: None, // no private document type before protocol version 15 prove_private_document_entries: None, + append_private_document_entries_operations: None, // no private document type before protocol version 15 expiration: DriveDocumentExpirationMethodVersions { insert_document_ttl_trees: 0, add_document_expiration_operations: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs index 5109e826e6e..7a98b33664c 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v4.rs @@ -194,6 +194,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V4: DriveDocumentMethodVersions = insert_empty_private_document_store_operation: None, // no private document type before protocol version 15 fetch_private_document_entries: None, // no private document type before protocol version 15 prove_private_document_entries: None, + append_private_document_entries_operations: None, // no private document type before protocol version 15 expiration: DriveDocumentExpirationMethodVersions { insert_document_ttl_trees: 0, add_document_expiration_operations: 0, 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..a5f32ea84d2 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 @@ -11,7 +11,8 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// the type's private document store at its key `[0]`, sized from the private document store /// limits of `SYSTEM_LIMITS_V5`. `fetch_private_document_entries` and /// `prove_private_document_entries` are `Some(0)` too (`None` before): a page of a store's -/// entries can be read and proved. +/// entries can be read and proved, and `append_private_document_entries_operations` is +/// `Some(0)` (`None` before): a `PrivateDocumentCreateFromShieldedPool` appends entries to one. /// * `delete.remove_reference_for_index_level_for_contract_operations` is 2: a delete's estimate /// describes an index's entries for a value (its `[0]` layer) as the references they are, as /// the insert's does, not as subtrees. @@ -22,6 +23,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = 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 + append_private_document_entries_operations: Some(0), // changed: a PrivateDocumentCreateFromShieldedPool appends entries to a store delete: DriveDocumentDeleteMethodVersions { remove_reference_for_index_level_for_contract_operations: 2, // changed: the entries layer is described as references ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.delete diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/mod.rs index f26baf11a7a..24d185ca160 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/mod.rs @@ -85,6 +85,7 @@ pub struct DriveStateTransitionActionConvertToHighLevelOperationsMethodVersions pub token_shielded_transfer_with_shielded_fee_transition: FeatureVersion, pub token_unshield_with_shielded_fee_transition: FeatureVersion, pub token_purchase_from_shielded_pool_transition: FeatureVersion, + pub private_document_create_from_shielded_pool_transition: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v1.rs index 255f358114f..0a0bab12866 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v1.rs @@ -72,6 +72,7 @@ pub const DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V1: DriveStateTransitionMethodV token_shielded_transfer_with_shielded_fee_transition: 0, token_unshield_with_shielded_fee_transition: 0, token_purchase_from_shielded_pool_transition: 0, + private_document_create_from_shielded_pool_transition: 0, }, document_from_action: DriveDocumentFromActionVersions { document_from_create_transition_action: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v2.rs index 352cae7e299..5af37c28480 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v2.rs @@ -73,6 +73,7 @@ pub const DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V2: DriveStateTransitionMethodV token_shielded_transfer_with_shielded_fee_transition: 0, token_unshield_with_shielded_fee_transition: 0, token_purchase_from_shielded_pool_transition: 0, + private_document_create_from_shielded_pool_transition: 0, }, document_from_action: DriveDocumentFromActionVersions { document_from_create_transition_action: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v3.rs b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v3.rs index 864760ebb28..8dbd1d13b93 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v3.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v3.rs @@ -77,6 +77,7 @@ pub const DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V3: DriveStateTransitionMethodV token_shielded_transfer_with_shielded_fee_transition: 0, token_unshield_with_shielded_fee_transition: 0, token_purchase_from_shielded_pool_transition: 0, + private_document_create_from_shielded_pool_transition: 0, }, document_from_action: DriveDocumentFromActionVersions { document_from_create_transition_action: 0, diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v4.rs b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v4.rs index 2b3e1d20f6c..78aa8efd67d 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v4.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_state_transition_method_versions/v4.rs @@ -80,6 +80,7 @@ pub const DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V4: DriveStateTransitionMethodV token_shielded_transfer_with_shielded_fee_transition: 0, token_unshield_with_shielded_fee_transition: 0, token_purchase_from_shielded_pool_transition: 0, + private_document_create_from_shielded_pool_transition: 0, }, document_from_action: DriveDocumentFromActionVersions { document_from_create_transition_action: 1, // changed 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 281f858302f..e2a020e45a7 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -64,6 +64,10 @@ use crate::version::drive_versions::{ /// `prove_private_document_entries` there, and `verify_private_document_entries` in /// `DRIVE_VERIFY_METHOD_VERSIONS_V4`, are `Some(0)` (`None` before): a page of the store /// is read, proved and verified. +/// * **Private document entries** (protocol v15 note 25): `DRIVE_DOCUMENT_METHOD_VERSIONS_V5` +/// sets `append_private_document_entries_operations` to `Some(0)` (`None` before): a +/// `PrivateDocumentCreateFromShieldedPool` appends its entries to the store, each behind +/// Drive's 16-byte header. /// * **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 /// also kept at the level of the deepest root key the batch writes under, so the estimate no diff --git a/packages/rs-platform-version/src/version/feature_initial_protocol_versions.rs b/packages/rs-platform-version/src/version/feature_initial_protocol_versions.rs index e8424621a68..fd91614fd9a 100644 --- a/packages/rs-platform-version/src/version/feature_initial_protocol_versions.rs +++ b/packages/rs-platform-version/src/version/feature_initial_protocol_versions.rs @@ -45,3 +45,8 @@ pub const YES_NO_VOTE_POLL_INITIAL_PROTOCOL_VERSION: ProtocolVersion = 15; /// exist and lookups must report it absent. Creating one of its proposals opens a yes/no vote /// poll. pub const DECISION_PROPOSALS_CONTRACT_INITIAL_PROTOCOL_VERSION: ProtocolVersion = 15; +/// Private document entries activate with protocol version 15: a +/// `PrivateDocumentCreateFromShieldedPool`, which appends entries to a private document type's +/// store paying from the credit shielded pool, is not active before it, as software that +/// predates it cannot decode one. +pub const PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION: ProtocolVersion = 15; diff --git a/packages/rs-platform-version/src/version/fee/mod.rs b/packages/rs-platform-version/src/version/fee/mod.rs index fcacff7b8df..231cdfa8a01 100644 --- a/packages/rs-platform-version/src/version/fee/mod.rs +++ b/packages/rs-platform-version/src/version/fee/mod.rs @@ -8,6 +8,8 @@ use crate::version::fee::document_ttl::v1::FEE_DOCUMENT_TTL_VERSION1; use crate::version::fee::document_ttl::FeeDocumentTtlVersion; use crate::version::fee::hashing::v1::FEE_HASHING_VERSION1; use crate::version::fee::hashing::{FeeHashingVersion, FeeHashingVersionBeforeVersion11}; +use crate::version::fee::private_document::v1::FEE_PRIVATE_DOCUMENT_VERSION1; +use crate::version::fee::private_document::FeePrivateDocumentVersion; use crate::version::fee::processing::{ FeeProcessingVersion, FeeProcessingVersionFieldsBeforeVersion1Point4, }; @@ -27,6 +29,7 @@ mod data_contract_validation; pub mod document_reference; pub mod document_ttl; mod hashing; +pub mod private_document; mod processing; pub mod signature; pub mod state_transition_min_fees; @@ -76,6 +79,10 @@ pub struct FeeVersion { /// protocol version 14). Like `document_ttl`, this field touches no stored format; /// `FeeVersionFieldsBeforeVersion4` must not gain it. pub document_reference: FeeDocumentReferenceVersion, + /// The entries a `PrivateDocumentCreateFromShieldedPool` appends (protocol version 15). + /// Fee versions are stored by number only, so this group does not touch any stored + /// format; `FeeVersionFieldsBeforeVersion4` must not gain it. + pub private_document: FeePrivateDocumentVersion, } impl FeeVersion { @@ -181,6 +188,8 @@ impl From for FeeVersion { document_ttl: FEE_DOCUMENT_TTL_VERSION1, // Nor do they parse `chooseFirst`; the group is unread there too. document_reference: FEE_DOCUMENT_REFERENCE_VERSION1, + // Nor do they admit a private document entry; the group is unread there too. + private_document: FEE_PRIVATE_DOCUMENT_VERSION1, } } } diff --git a/packages/rs-platform-version/src/version/fee/private_document/mod.rs b/packages/rs-platform-version/src/version/fee/private_document/mod.rs new file mode 100644 index 00000000000..160e4daf78e --- /dev/null +++ b/packages/rs-platform-version/src/version/fee/private_document/mod.rs @@ -0,0 +1,25 @@ +use bincode::{Decode, Encode}; + +pub mod v1; + +/// Fees of an entry a `PrivateDocumentCreateFromShieldedPool` appends to a private document +/// type's store (protocol version 15). +/// +/// The transition pays a fixed fee out of the credit shielded pool, so it cannot be metered: +/// its value balance must equal the fee exactly, and a wallet computes it before building the +/// bundle. On top of the bundle's shielded fee, each entry pays its own bytes and +/// `entry_overhead_bytes` at the storage rate, plus `entry_processing_fee`. +/// +/// Every earlier schedule carries the same group; it is unread there because the transition +/// is refused before protocol version 15. +#[derive(Clone, Debug, Encode, Decode, Default, PartialEq, Eq)] +pub struct FeePrivateDocumentVersion { + /// The flat credits each entry pays on top of its storage. It prices the scan every wallet + /// of the type makes of the entry, more than the work of storing it, so a flood of entries + /// costs what it makes others download. + pub entry_processing_fee: u64, + /// The bytes each entry is charged for beyond its own: Drive's 16-byte header, and what + /// GroveDB's append into the store adds per entry, the compaction of a full chunk shared out + /// among its entries, for every chunk power a store can take. + pub entry_overhead_bytes: u64, +} diff --git a/packages/rs-platform-version/src/version/fee/private_document/v1.rs b/packages/rs-platform-version/src/version/fee/private_document/v1.rs new file mode 100644 index 00000000000..ea24af8b385 --- /dev/null +++ b/packages/rs-platform-version/src/version/fee/private_document/v1.rs @@ -0,0 +1,18 @@ +use crate::version::fee::private_document::FeePrivateDocumentVersion; + +/// Introduced with protocol version 15 (5.1), the first version that admits +/// `PrivateDocumentCreateFromShieldedPool`; every fee schedule references it, and none before +/// 15 reads it. +/// +/// An entry pays 50,000,000 credits (0.0005 DASH) on top of its storage, so a million entries +/// a day, which every wallet of the type downloads and scans, cost 500 DASH a day in this fee +/// alone. +/// +/// GroveDB's append into a store meters the entry, Drive's 16-byte header and 1 to 3 bytes of +/// its own per entry, the same at every position (the compacting append included) for every +/// chunk power; 32 bytes cover them with margin. Drive's +/// `should_charge_a_fixed_fee_covering_the_metered_cost_at_every_chunk_power` holds both to it. +pub const FEE_PRIVATE_DOCUMENT_VERSION1: FeePrivateDocumentVersion = FeePrivateDocumentVersion { + entry_processing_fee: 50_000_000, + entry_overhead_bytes: 32, +}; diff --git a/packages/rs-platform-version/src/version/fee/v1.rs b/packages/rs-platform-version/src/version/fee/v1.rs index eb9c2613847..2afa351e761 100644 --- a/packages/rs-platform-version/src/version/fee/v1.rs +++ b/packages/rs-platform-version/src/version/fee/v1.rs @@ -3,6 +3,7 @@ use crate::version::fee::data_contract_validation::v1::FEE_DATA_CONTRACT_VALIDAT use crate::version::fee::document_reference::v1::FEE_DOCUMENT_REFERENCE_VERSION1; use crate::version::fee::document_ttl::v1::FEE_DOCUMENT_TTL_VERSION1; use crate::version::fee::hashing::v1::FEE_HASHING_VERSION1; +use crate::version::fee::private_document::v1::FEE_PRIVATE_DOCUMENT_VERSION1; use crate::version::fee::processing::v1::FEE_PROCESSING_VERSION1; use crate::version::fee::signature::v1::FEE_SIGNATURE_VERSION1; use crate::version::fee::state_transition_min_fees::v1::STATE_TRANSITION_MIN_FEES_VERSION1; @@ -25,4 +26,6 @@ pub const FEE_VERSION1: FeeVersion = FeeVersion { document_ttl: FEE_DOCUMENT_TTL_VERSION1, // Unread before protocol version 14: `chooseFirst` does not parse there. document_reference: FEE_DOCUMENT_REFERENCE_VERSION1, + // Unread before protocol version 15: no private document entry is admitted there. + private_document: FEE_PRIVATE_DOCUMENT_VERSION1, }; diff --git a/packages/rs-platform-version/src/version/fee/v2.rs b/packages/rs-platform-version/src/version/fee/v2.rs index 38e1ffea37b..d993623939d 100644 --- a/packages/rs-platform-version/src/version/fee/v2.rs +++ b/packages/rs-platform-version/src/version/fee/v2.rs @@ -3,6 +3,7 @@ use crate::version::fee::data_contract_validation::v1::FEE_DATA_CONTRACT_VALIDAT use crate::version::fee::document_reference::v1::FEE_DOCUMENT_REFERENCE_VERSION1; use crate::version::fee::document_ttl::v1::FEE_DOCUMENT_TTL_VERSION1; use crate::version::fee::hashing::v1::FEE_HASHING_VERSION1; +use crate::version::fee::private_document::v1::FEE_PRIVATE_DOCUMENT_VERSION1; use crate::version::fee::processing::v1::FEE_PROCESSING_VERSION1; use crate::version::fee::signature::v1::FEE_SIGNATURE_VERSION1; use crate::version::fee::state_transition_min_fees::v1::STATE_TRANSITION_MIN_FEES_VERSION1; @@ -26,4 +27,6 @@ pub const FEE_VERSION2: FeeVersion = FeeVersion { document_ttl: FEE_DOCUMENT_TTL_VERSION1, // Unread before protocol version 14: `chooseFirst` does not parse there. document_reference: FEE_DOCUMENT_REFERENCE_VERSION1, + // Unread before protocol version 15: no private document entry is admitted there. + private_document: FEE_PRIVATE_DOCUMENT_VERSION1, }; diff --git a/packages/rs-platform-version/src/version/fee/v4.rs b/packages/rs-platform-version/src/version/fee/v4.rs index ae926cadc20..ea7aad0d7e3 100644 --- a/packages/rs-platform-version/src/version/fee/v4.rs +++ b/packages/rs-platform-version/src/version/fee/v4.rs @@ -1,3 +1,4 @@ +use crate::version::fee::private_document::v1::FEE_PRIVATE_DOCUMENT_VERSION1; use crate::version::fee::v3::FEE_VERSION3; use crate::version::fee::vote_resolution_fund_fees::v3::VOTE_RESOLUTION_FUND_FEES_VERSION3; use crate::version::fee::FeeVersion; @@ -7,5 +8,9 @@ pub const FEE_VERSION4: FeeVersion = FeeVersion { // Decision proposals (new in protocol version 15) prefund their poll's votes. Storage rates // are unchanged, so the fee_version_number inherited from FEE_VERSION3 stays. vote_resolution_fund_fees: VOTE_RESOLUTION_FUND_FEES_VERSION3, + // Each private document entry (new in protocol version 15) pays its storage and a flat + // 50,000,000 credits; the earlier schedules carry the same group, unread before 15, so the + // table is spelled out here where it takes effect. + private_document: FEE_PRIVATE_DOCUMENT_VERSION1, ..FEE_VERSION3 }; diff --git a/packages/rs-platform-version/src/version/mocks/v2_test.rs b/packages/rs-platform-version/src/version/mocks/v2_test.rs index ef67c313fd9..b7999175f90 100644 --- a/packages/rs-platform-version/src/version/mocks/v2_test.rs +++ b/packages/rs-platform-version/src/version/mocks/v2_test.rs @@ -610,6 +610,7 @@ pub const TEST_PLATFORM_V2: PlatformVersion = PlatformVersion { private_document_store_target_chunk_bytes: None, private_document_store_min_chunk_power: None, private_document_store_max_chunk_power: None, + max_private_document_entries_per_transition: None, max_references_per_document: 256, max_reference_operands: 4, max_reference_expression_depth: 4, diff --git a/packages/rs-platform-version/src/version/system_limits/mod.rs b/packages/rs-platform-version/src/version/system_limits/mod.rs index 500ae9835f1..154e507895b 100644 --- a/packages/rs-platform-version/src/version/system_limits/mod.rs +++ b/packages/rs-platform-version/src/version/system_limits/mod.rs @@ -59,6 +59,11 @@ pub struct SystemLimits { /// At most 15: a page of a store reads up to a whole chunk, and a page reads at most /// u16::MAX entries (Drive's `should_keep_a_whole_chunk_within_one_page`). pub private_document_store_max_chunk_power: Option, + /// Most entries one `PrivateDocumentCreateFromShieldedPool` transition may append to a + /// private document type's store (protocol version 15): 16 from `SYSTEM_LIMITS_V5`, `None` + /// in the earlier tables, which refuse the transition before validating it. Read by the + /// transition's basic structure 0. + pub max_private_document_entries_per_transition: Option, /// Maximum number of references one document of a document type may carry, counted at /// contract registration or update from the type's `refersTo` declarations: one for each /// property that declares one (an identifier, or a key id carrying a key reference), one diff --git a/packages/rs-platform-version/src/version/system_limits/v1.rs b/packages/rs-platform-version/src/version/system_limits/v1.rs index dde60327d5e..dedcc28fc5a 100644 --- a/packages/rs-platform-version/src/version/system_limits/v1.rs +++ b/packages/rs-platform-version/src/version/system_limits/v1.rs @@ -13,6 +13,7 @@ pub const SYSTEM_LIMITS_V1: SystemLimits = SystemLimits { private_document_store_target_chunk_bytes: None, private_document_store_min_chunk_power: None, private_document_store_max_chunk_power: None, + max_private_document_entries_per_transition: None, max_references_per_document: 256, max_reference_operands: 4, max_reference_expression_depth: 4, diff --git a/packages/rs-platform-version/src/version/system_limits/v2.rs b/packages/rs-platform-version/src/version/system_limits/v2.rs index b5f33c7160b..eabb93e7285 100644 --- a/packages/rs-platform-version/src/version/system_limits/v2.rs +++ b/packages/rs-platform-version/src/version/system_limits/v2.rs @@ -19,6 +19,7 @@ pub const SYSTEM_LIMITS_V2: SystemLimits = SystemLimits { private_document_store_target_chunk_bytes: None, private_document_store_min_chunk_power: None, private_document_store_max_chunk_power: None, + max_private_document_entries_per_transition: None, max_references_per_document: 256, max_reference_operands: 4, max_reference_expression_depth: 4, diff --git a/packages/rs-platform-version/src/version/system_limits/v3.rs b/packages/rs-platform-version/src/version/system_limits/v3.rs index d318a1f1e55..802e21da2f5 100644 --- a/packages/rs-platform-version/src/version/system_limits/v3.rs +++ b/packages/rs-platform-version/src/version/system_limits/v3.rs @@ -21,6 +21,7 @@ pub const SYSTEM_LIMITS_V3: SystemLimits = SystemLimits { private_document_store_target_chunk_bytes: None, private_document_store_min_chunk_power: None, private_document_store_max_chunk_power: None, + max_private_document_entries_per_transition: None, max_references_per_document: 256, max_reference_operands: 4, max_reference_expression_depth: 4, diff --git a/packages/rs-platform-version/src/version/system_limits/v4.rs b/packages/rs-platform-version/src/version/system_limits/v4.rs index af4dfdebad2..87b81fbe198 100644 --- a/packages/rs-platform-version/src/version/system_limits/v4.rs +++ b/packages/rs-platform-version/src/version/system_limits/v4.rs @@ -142,6 +142,7 @@ pub const SYSTEM_LIMITS_V4: SystemLimits = SystemLimits { private_document_store_target_chunk_bytes: None, private_document_store_min_chunk_power: None, private_document_store_max_chunk_power: None, + max_private_document_entries_per_transition: None, max_references_per_document: 256, // refersTo (new in v14): contract registration caps the references one document carries, a typed array of references counting its maxItems max_reference_operands: 4, // refersTo anyOf / allOf (new in v14): contract registration caps the operands one list holds max_reference_expression_depth: 4, // refersTo anyOf / allOf (new in v14): contract registration caps how deep they nest diff --git a/packages/rs-platform-version/src/version/system_limits/v5.rs b/packages/rs-platform-version/src/version/system_limits/v5.rs index f8ddf0a466d..8bade129770 100644 --- a/packages/rs-platform-version/src/version/system_limits/v5.rs +++ b/packages/rs-platform-version/src/version/system_limits/v5.rs @@ -13,12 +13,14 @@ use crate::version::system_limits::SystemLimits; /// declaring a larger `entrySize`. A private type's store keeps a compacted chunk near /// 512 KiB (`private_document_store_target_chunk_bytes`) at a chunk power from 4 to 11 /// (`private_document_store_min_chunk_power`, `private_document_store_max_chunk_power`), all -/// `None` before. +/// `None` before. A `PrivateDocumentCreateFromShieldedPool` appends at most 16 entries +/// (`max_private_document_entries_per_transition`, `None` before). pub const SYSTEM_LIMITS_V5: SystemLimits = SystemLimits { max_compiled_contract_size: Some(1_048_576), // changed: contracts stored compiled take at most 1 MiB max_private_document_entry_size: Some(4096), // changed: a private document type's entries take at most 4096 bytes private_document_store_target_chunk_bytes: Some(512 * 1024), // changed: a private type's store keeps a compacted chunk near 512 KiB private_document_store_min_chunk_power: Some(4), // changed: and its chunk power is at least 4 private_document_store_max_chunk_power: Some(11), // changed: and at most 11 + max_private_document_entries_per_transition: Some(16), // changed: a PrivateDocumentCreateFromShieldedPool appends at most 16 entries ..SYSTEM_LIMITS_V4 }; diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 6a754bfd357..dd21cdfdcbe 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -505,9 +505,37 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// profile create and replace triggers, which made the identity, writer and type byte /// checks and refused with `DataTriggerConditionError` (40500). Protocol v14 keeps /// DashPay v2 and bindings 2, and earlier versions the contract and bindings they had. +/// +/// 25. **Private document entries**: a new state transition, +/// `PrivateDocumentCreateFromShieldedPool` (type 29), appends entries to the store of a +/// private document type (note 13), paying a fixed fee from the credit shielded pool: an +/// Orchard spend bundle whose value balance is exactly the fee, with no identity and no +/// platform signature. Its sighash binds the contract, the type name and every entry +/// (`private_document_create_from_shielded_pool_extra_sighash_data`, led by the type byte +/// 29). Basic structure 0 refuses, unpaid, a transition carrying no entry or more than +/// `max_private_document_entries_per_transition` (16 in `SYSTEM_LIMITS_V5`, +/// `InvalidPrivateDocumentEntryCountError`, 10426), an entry that is empty, over +/// `max_private_document_entry_size` or not the first entry's size +/// (`InvalidPrivateDocumentEntrySizeError`, 10427), and two entries that are the same bytes +/// (`DuplicatePrivateDocumentEntryError`, 10428). The value balance must equal +/// `compute_private_document_create_from_shielded_pool_fee`: the bundle's shielded fee +/// plus, per entry, its bytes and 32 overhead bytes at the storage rate and a flat +/// 50,000,000 credits (`FEE_PRIVATE_DOCUMENT_VERSION1` in `FEE_VERSION4`). After the pool +/// checks a shielded transfer makes, the transform refuses, unpaid, a contract that does not +/// exist, a type it does not have (`InvalidDocumentTypeError`), a type that is not private +/// (`DocumentTypeNotPrivateError`, 40151) and entries of another size than the type's +/// `entrySize` (`PrivateDocumentEntrySizeMismatchError`, 40152). Execution records the +/// nullifiers and the change notes, lowers the pool by the fee and appends each entry +/// behind Drive's 16-byte header (`append_private_document_entries_operations` `Some(0)` in +/// `DRIVE_DOCUMENT_METHOD_VERSIONS_V5`, `None` before). The proof of execution proves the +/// spent nullifiers. The transition is not active before this version +/// (`PRIVATE_DOCUMENT_CREATE_FROM_SHIELDED_POOL_INITIAL_PROTOCOL_VERSION`), as software that +/// predates it cannot decode one: `validate_shielded_proof` 1 and +/// `validate_minimum_shielded_fee` 0, which protocol v14 selects too, gain an arm for it +/// that is unreachable there. 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); create_initial_state_structure 5 adds the token marketplace escrow trees; calculate_total_tokens_balance 2 counts escrowed tokens; the escrow primitives; the identity balance, token balance, prefunded specialized balance and contract fee pot estimations (for_balances 1, for_token_balances 1, estimated_cost_for_prefunded_specialized_balance_update 2, add_estimation_costs_for_contract_fee_pot_update 1) keep a deeper layer description (note 17); fetch_core_fork_activation_height and store_core_fork_activation_height Some(0) keep the Core fork activations (note 18) + 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); create_initial_state_structure 5 adds the token marketplace escrow trees; calculate_total_tokens_balance 2 counts escrowed tokens; the escrow primitives; the identity balance, token balance, prefunded specialized balance and contract fee pot estimations (for_balances 1, for_token_balances 1, estimated_cost_for_prefunded_specialized_balance_update 2, add_estimation_costs_for_contract_fee_pot_update 1) keep a deeper layer description (note 17); fetch_core_fork_activation_height and store_core_fork_activation_height Some(0) keep the Core fork activations (note 18); document method versions v5: append_private_document_entries_operations Some(0) appends a private document create's entries (note 25) 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; consensus_params_update 2 installs the full evidence policy (note 16); update_core_info 1 records the chain locked height at which Core's V24 is first seen active (note 18); build_untied_withdrawal_transactions_from_documents 1 emits asset unlock payload version 2 once V24 is recorded as active (note 20); update_voter_identity 1 re-enables the voter key of the address a masternode returns to (note 22) @@ -533,8 +561,8 @@ pub const PLATFORM_V15: PlatformVersion = PlatformVersion { factory_versions: DPP_FACTORY_VERSIONS_V1, }, system_data_contracts: SYSTEM_DATA_CONTRACT_VERSIONS_V4, // changed: DPNS v3 checks a name's create with keywords (note 3); the decision proposals contract (note 12); the token market contract is selected (note 17); DashPay v3 states what its data triggers checked (note 24) - fee_version: FEE_VERSION4, // changed: decision proposals prefund their poll with 0.5 Dash (note 12) - system_limits: SYSTEM_LIMITS_V5, // changed: max_compiled_contract_size caps a contract stored compiled at 1 MiB (note 8); max_private_document_entry_size caps a private type's entries at 4096 bytes, and the private document store limits size its store (note 13) + fee_version: FEE_VERSION4, // changed: decision proposals prefund their poll with 0.5 Dash (note 12); a private document entry pays its storage and 50,000,000 credits (note 25) + system_limits: SYSTEM_LIMITS_V5, // changed: max_compiled_contract_size caps a contract stored compiled at 1 MiB (note 8); max_private_document_entry_size caps a private type's entries at 4096 bytes, and the private document store limits size its store (note 13); max_private_document_entries_per_transition caps a private document create at 16 entries (note 25) consensus: ConsensusVersions { tenderdash_consensus_version: 1, evidence: Some(ConsensusEvidenceParams { diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/state_transition_summary.rs b/packages/rs-platform-wallet/src/wallet/identity/network/state_transition_summary.rs index d97da1c9594..be0e36d27c8 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/state_transition_summary.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/state_transition_summary.rs @@ -364,7 +364,8 @@ fn carries_signature(transition: &StateTransition) -> bool { | StateTransition::ContractFeeClaim(_) | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) - | StateTransition::TokenPurchaseFromShieldedPool(_) => false, + | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) => false, }; has_witness || transition.signature().is_some_and(|sig| !sig.is_empty()) } diff --git a/packages/wasm-dpp/src/errors/consensus/consensus_error.rs b/packages/wasm-dpp/src/errors/consensus/consensus_error.rs index 2a5af4a1035..ac96756afa9 100644 --- a/packages/wasm-dpp/src/errors/consensus/consensus_error.rs +++ b/packages/wasm-dpp/src/errors/consensus/consensus_error.rs @@ -84,6 +84,12 @@ use dpp::consensus::state::document::referenced_document_removed_error::Referenc use dpp::consensus::state::document::referenced_document_type_moderated_error::ReferencedDocumentTypeModeratedError; use dpp::consensus::state::document::referenced_document_type_index_only_error::ReferencedDocumentTypeIndexOnlyError; use dpp::consensus::state::document::referenced_document_type_private_error::ReferencedDocumentTypePrivateError; +use dpp::consensus::state::document::document_type_not_private_error::DocumentTypeNotPrivateError; +use dpp::consensus::state::document::private_document_entry_size_mismatch_error::PrivateDocumentEntrySizeMismatchError; +use dpp::consensus::basic::document::{ + DuplicatePrivateDocumentEntryError, InvalidPrivateDocumentEntryCountError, + InvalidPrivateDocumentEntrySizeError, +}; use dpp::consensus::state::document::document_delete_constraint_violated_error::DocumentDeleteConstraintViolatedError; use dpp::consensus::state::document::referenced_document_rule_read_invalid_error::ReferencedDocumentRuleReadInvalidError; use dpp::consensus::state::document::referenced_document_preceded_error::ReferencedDocumentPrecededError; @@ -823,6 +829,12 @@ pub fn from_state_error(state_error: &StateError) -> JsValue { StateError::ReferencedDocumentTypePrivateError(e) => { generic_consensus_error!(ReferencedDocumentTypePrivateError, e).into() } + StateError::DocumentTypeNotPrivateError(e) => { + generic_consensus_error!(DocumentTypeNotPrivateError, e).into() + } + StateError::PrivateDocumentEntrySizeMismatchError(e) => { + generic_consensus_error!(PrivateDocumentEntrySizeMismatchError, e).into() + } } } @@ -1451,6 +1463,15 @@ fn from_basic_error(basic_error: &BasicError) -> JsValue { BasicError::DocumentTypePrivateError(e) => { generic_consensus_error!(DocumentTypePrivateError, e).into() } + BasicError::InvalidPrivateDocumentEntryCountError(e) => { + generic_consensus_error!(InvalidPrivateDocumentEntryCountError, e).into() + } + BasicError::InvalidPrivateDocumentEntrySizeError(e) => { + generic_consensus_error!(InvalidPrivateDocumentEntrySizeError, e).into() + } + BasicError::DuplicatePrivateDocumentEntryError(e) => { + generic_consensus_error!(DuplicatePrivateDocumentEntryError, e).into() + } } } diff --git a/packages/wasm-dpp/src/state_transition/state_transition_factory.rs b/packages/wasm-dpp/src/state_transition/state_transition_factory.rs index dcb4826864e..dd3b399e697 100644 --- a/packages/wasm-dpp/src/state_transition/state_transition_factory.rs +++ b/packages/wasm-dpp/src/state_transition/state_transition_factory.rs @@ -89,6 +89,7 @@ impl StateTransitionFactoryWasm { | StateTransition::TokenShieldedTransferWithShieldedFee(_) | StateTransition::TokenUnshieldWithShieldedFee(_) | StateTransition::TokenPurchaseFromShieldedPool(_) + | StateTransition::PrivateDocumentCreateFromShieldedPool(_) | StateTransition::IdentityTopUpFromShieldedPool(_) => Err(JsValue::from_str( "shielded transitions are not yet supported in wasm-dpp StateTransitionFactory", )), diff --git a/packages/wasm-dpp2/src/lib.rs b/packages/wasm-dpp2/src/lib.rs index b81bffb664a..fdb2ce8ecbd 100644 --- a/packages/wasm-dpp2/src/lib.rs +++ b/packages/wasm-dpp2/src/lib.rs @@ -72,7 +72,8 @@ pub use platform_address::{ outputs_to_optional_btree_map, }; pub use shielded::{ - AddressWitnessWasm, IdentityTopUpFromShieldedPoolTransitionWasm, SerializedOrchardActionWasm, + AddressWitnessWasm, IdentityTopUpFromShieldedPoolTransitionWasm, + PrivateDocumentCreateFromShieldedPoolTransitionWasm, SerializedOrchardActionWasm, ShieldFromAssetLockTransitionWasm, ShieldFromIdentityTransitionWasm, ShieldTransitionWasm, ShieldedTransferTransitionWasm, ShieldedWithdrawalTransitionWasm, TokenPurchaseFromShieldedPoolTransitionWasm, diff --git a/packages/wasm-dpp2/src/shielded/mod.rs b/packages/wasm-dpp2/src/shielded/mod.rs index bae01e5ebfb..d7f402ac0cf 100644 --- a/packages/wasm-dpp2/src/shielded/mod.rs +++ b/packages/wasm-dpp2/src/shielded/mod.rs @@ -2,6 +2,7 @@ pub mod address_witness; pub mod identity_create_from_shielded_pool_transition; pub mod identity_top_up_from_shielded_pool_transition; pub mod orchard_action; +pub mod private_document_create_from_shielded_pool_transition; pub mod shield_from_asset_lock_transition; pub mod shield_from_identity_transition; pub mod shield_transition; @@ -16,6 +17,7 @@ pub use address_witness::{AddressWitnessWasm, input_witnesses_from_js_options}; pub use identity_create_from_shielded_pool_transition::IdentityCreateFromShieldedPoolTransitionWasm; pub use identity_top_up_from_shielded_pool_transition::IdentityTopUpFromShieldedPoolTransitionWasm; pub use orchard_action::{SerializedOrchardActionWasm, actions_from_js_options}; +pub use private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransitionWasm; pub use shield_from_asset_lock_transition::ShieldFromAssetLockTransitionWasm; pub use shield_from_identity_transition::ShieldFromIdentityTransitionWasm; pub use shield_transition::ShieldTransitionWasm; diff --git a/packages/wasm-dpp2/src/shielded/private_document_create_from_shielded_pool_transition.rs b/packages/wasm-dpp2/src/shielded/private_document_create_from_shielded_pool_transition.rs new file mode 100644 index 00000000000..47a7841d3a1 --- /dev/null +++ b/packages/wasm-dpp2/src/shielded/private_document_create_from_shielded_pool_transition.rs @@ -0,0 +1,290 @@ +use crate::error::{WasmDppError, WasmDppResult}; +use crate::identifier::IdentifierWasm; +use crate::shielded::orchard_action::{SerializedOrchardActionWasm, actions_from_js_options}; +use crate::state_transitions::StateTransitionWasm; +use crate::utils::try_vec_to_fixed_bytes; +use crate::{impl_wasm_conversions_inner, impl_wasm_type_info}; +use dpp::platform_value::BinaryData; +use dpp::platform_value::string_encoding::Encoding::{Base64, Hex}; +use dpp::platform_value::string_encoding::{decode, encode}; +use dpp::serialization::{PlatformDeserializableUntrusted, PlatformSerializable}; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::accessors::PrivateDocumentCreateFromShieldedPoolTransitionAccessorsV0; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use dpp::state_transition::{StateTransition, StateTransitionLike}; +use js_sys::{Array, Uint8Array}; +use serde::{Deserialize, Serialize}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(typescript_custom_section)] +const TS_TYPES: &str = r#" +/** + * Options for constructing a PrivateDocumentCreateFromShieldedPoolTransition (entries appended to + * a private document type's store, paid from the credit shielded pool). The bundle is an Orchard + * spend whose value balance is the fee and whose binding signature commits to the contract, the + * type name and every entry; there is no platform signature. + */ +export interface PrivateDocumentCreateFromShieldedPoolTransitionOptions { + dataContractId: IdentifierLike; + documentTypeName: string; + entries: Uint8Array[]; + actions: SerializedOrchardAction[]; + valueBalance: bigint; + anchor: Uint8Array; + proof: Uint8Array; + bindingSignature: Uint8Array; +} + +export interface PrivateDocumentCreateFromShieldedPoolTransitionObject { + $formatVersion: string; + dataContractId: Uint8Array; + documentTypeName: string; + entries: Uint8Array[]; + actions: SerializedOrchardActionObject[]; + valueBalance: bigint; + anchor: Uint8Array; + proof: Uint8Array; + bindingSignature: Uint8Array; +} + +export interface PrivateDocumentCreateFromShieldedPoolTransitionJSON { + $formatVersion: string; + dataContractId: string; + documentTypeName: string; + entries: string[]; + actions: SerializedOrchardActionJSON[]; + valueBalance: number | string; + anchor: string; + proof: string; + bindingSignature: string; +} +"#; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "PrivateDocumentCreateFromShieldedPoolTransitionOptions")] + pub type PrivateDocumentCreateFromShieldedPoolTransitionOptionsJs; + + #[wasm_bindgen(typescript_type = "PrivateDocumentCreateFromShieldedPoolTransitionObject")] + pub type PrivateDocumentCreateFromShieldedPoolTransitionObjectJs; + + #[wasm_bindgen(typescript_type = "PrivateDocumentCreateFromShieldedPoolTransitionJSON")] + pub type PrivateDocumentCreateFromShieldedPoolTransitionJSONJs; +} + +/// Non-WASM-instance fields extracted from the constructor options via serde. +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct PrivateDocumentCreateFromShieldedPoolTransitionSimpleFields { + document_type_name: String, + /// Each a `Uint8Array`, which the serde-wasm-bindgen fork reads as bytes. + entries: Vec>, + value_balance: u64, + anchor: Vec, + proof: Vec, + binding_signature: Vec, +} + +#[derive(Clone, Serialize, Deserialize)] +#[serde(transparent)] +#[wasm_bindgen(js_name = PrivateDocumentCreateFromShieldedPoolTransition)] +pub struct PrivateDocumentCreateFromShieldedPoolTransitionWasm( + PrivateDocumentCreateFromShieldedPoolTransition, +); + +impl From + for PrivateDocumentCreateFromShieldedPoolTransitionWasm +{ + fn from(v: PrivateDocumentCreateFromShieldedPoolTransition) -> Self { + PrivateDocumentCreateFromShieldedPoolTransitionWasm(v) + } +} + +impl From + for PrivateDocumentCreateFromShieldedPoolTransition +{ + fn from(v: PrivateDocumentCreateFromShieldedPoolTransitionWasm) -> Self { + v.0 + } +} + +#[wasm_bindgen(js_class = PrivateDocumentCreateFromShieldedPoolTransition)] +impl PrivateDocumentCreateFromShieldedPoolTransitionWasm { + #[wasm_bindgen(constructor)] + pub fn new( + options: PrivateDocumentCreateFromShieldedPoolTransitionOptionsJs, + ) -> WasmDppResult { + let js_opts: &JsValue = options.as_ref(); + + let data_contract_id: IdentifierWasm = + crate::utils::try_from_options(&options, "dataContractId")?; + let actions = actions_from_js_options(js_opts, "actions")?; + + let fields: PrivateDocumentCreateFromShieldedPoolTransitionSimpleFields = + serde_wasm_bindgen::from_value(options.into()) + .map_err(|e| WasmDppError::invalid_argument(e.to_string()))?; + + let anchor: [u8; 32] = try_vec_to_fixed_bytes(fields.anchor, "anchor")?; + let binding_signature: [u8; 64] = + try_vec_to_fixed_bytes(fields.binding_signature, "bindingSignature")?; + + Ok(PrivateDocumentCreateFromShieldedPoolTransitionWasm( + PrivateDocumentCreateFromShieldedPoolTransition::V0( + PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id: data_contract_id.into(), + document_type_name: fields.document_type_name, + entries: fields.entries.into_iter().map(BinaryData::new).collect(), + actions: actions.into_iter().map(Into::into).collect(), + value_balance: fields.value_balance, + anchor, + proof: fields.proof, + binding_signature, + }, + ), + )) + } + + /// The contract that defines the private document type. Read-only, like every field the + /// Orchard binding signature commits to: a wrapper that changed it would produce a + /// transition consensus must reject. + #[wasm_bindgen(getter = "dataContractId")] + pub fn data_contract_id(&self) -> IdentifierWasm { + self.0.data_contract_id().into() + } + + /// The private document type the entries are appended to. + #[wasm_bindgen(getter = "documentTypeName")] + pub fn document_type_name(&self) -> String { + self.0.document_type_name().to_string() + } + + /// The entries, in the order they are appended. + #[wasm_bindgen(getter = "entries")] + pub fn entries(&self) -> Array { + self.0 + .entries() + .iter() + .map(|entry| Uint8Array::from(entry.as_slice())) + .collect() + } + + /// Returns the serialized Orchard actions. + #[wasm_bindgen(getter = "actions")] + pub fn actions(&self) -> Vec { + self.0 + .actions() + .iter() + .cloned() + .map(SerializedOrchardActionWasm::from) + .collect() + } + + /// Credits leaving the credit pool: the bundle's value balance, which is the fee. + #[wasm_bindgen(getter = "valueBalance")] + pub fn value_balance(&self) -> u64 { + self.0.value_balance() + } + + #[wasm_bindgen(getter = "anchor")] + pub fn anchor(&self) -> Vec { + self.0.anchor().to_vec() + } + + #[wasm_bindgen(getter = "proof")] + pub fn proof(&self) -> Vec { + self.0.proof().to_vec() + } + + #[wasm_bindgen(getter = "bindingSignature")] + pub fn binding_signature(&self) -> Vec { + self.0.binding_signature().to_vec() + } + + #[wasm_bindgen(js_name = getModifiedDataIds)] + pub fn modified_data_ids(&self) -> Vec { + self.0 + .modified_data_ids() + .into_iter() + .map(IdentifierWasm::from) + .collect() + } + + #[wasm_bindgen(js_name = toBytes)] + pub fn to_bytes(&self) -> WasmDppResult> { + Ok(PlatformSerializable::serialize_to_bytes( + &StateTransition::PrivateDocumentCreateFromShieldedPool(self.0.clone()), + )?) + } + + #[wasm_bindgen(js_name = "toHex")] + pub fn to_hex(&self) -> WasmDppResult { + Ok(encode(self.to_bytes()?.as_slice(), Hex)) + } + + #[wasm_bindgen(js_name = "toBase64")] + pub fn to_base64(&self) -> WasmDppResult { + Ok(encode(self.to_bytes()?.as_slice(), Base64)) + } + + #[wasm_bindgen(js_name = fromBytes)] + pub fn from_bytes( + bytes: Vec, + ) -> WasmDppResult { + let st = StateTransition::deserialize_from_bytes_untrusted(&bytes)?; + match st { + StateTransition::PrivateDocumentCreateFromShieldedPool(inner) => Ok(inner.into()), + _ => Err(WasmDppError::invalid_argument( + "Invalid state transition type: expected PrivateDocumentCreateFromShieldedPool", + )), + } + } + + #[wasm_bindgen(js_name = "fromHex")] + pub fn from_hex( + hex: String, + ) -> WasmDppResult { + let bytes = + decode(hex.as_str(), Hex).map_err(|e| WasmDppError::serialization(e.to_string()))?; + PrivateDocumentCreateFromShieldedPoolTransitionWasm::from_bytes(bytes) + } + + #[wasm_bindgen(js_name = "fromBase64")] + pub fn from_base64( + base64: String, + ) -> WasmDppResult { + let bytes = decode(base64.as_str(), Base64) + .map_err(|e| WasmDppError::serialization(e.to_string()))?; + PrivateDocumentCreateFromShieldedPoolTransitionWasm::from_bytes(bytes) + } + + #[wasm_bindgen(js_name = toStateTransition)] + pub fn to_state_transition(&self) -> StateTransitionWasm { + StateTransition::PrivateDocumentCreateFromShieldedPool(self.0.clone()).into() + } + + #[wasm_bindgen(js_name = "fromStateTransition")] + pub fn from_state_transition( + st: &StateTransitionWasm, + ) -> WasmDppResult { + let rs_st: StateTransition = st.clone().into(); + match rs_st { + StateTransition::PrivateDocumentCreateFromShieldedPool(inner) => Ok(inner.into()), + _ => Err(WasmDppError::invalid_argument( + "Invalid state transition type: expected PrivateDocumentCreateFromShieldedPool", + )), + } + } +} + +impl_wasm_conversions_inner!( + PrivateDocumentCreateFromShieldedPoolTransitionWasm, + PrivateDocumentCreateFromShieldedPoolTransition, + PrivateDocumentCreateFromShieldedPoolTransition, + PrivateDocumentCreateFromShieldedPoolTransitionObjectJs, + PrivateDocumentCreateFromShieldedPoolTransitionJSONJs +); + +impl_wasm_type_info!( + PrivateDocumentCreateFromShieldedPoolTransitionWasm, + PrivateDocumentCreateFromShieldedPoolTransition +); diff --git a/packages/wasm-dpp2/src/state_transitions/base/state_transition.rs b/packages/wasm-dpp2/src/state_transitions/base/state_transition.rs index f0de69d545f..836e4044bca 100644 --- a/packages/wasm-dpp2/src/state_transitions/base/state_transition.rs +++ b/packages/wasm-dpp2/src/state_transitions/base/state_transition.rs @@ -369,6 +369,7 @@ impl StateTransitionWasm { TokenShieldedTransferWithShieldedFee(_) => 26, TokenUnshieldWithShieldedFee(_) => 27, TokenPurchaseFromShieldedPool(_) => 28, + PrivateDocumentCreateFromShieldedPool(_) => 29, } } @@ -466,6 +467,7 @@ impl StateTransitionWasm { | TokenShieldedTransferWithShieldedFee(_) | TokenUnshieldWithShieldedFee(_) | TokenPurchaseFromShieldedPool(_) + | PrivateDocumentCreateFromShieldedPool(_) | IdentityTopUpFromShieldedPool(_) => None, } } @@ -502,6 +504,7 @@ impl StateTransitionWasm { | TokenShieldedTransferWithShieldedFee(_) | TokenUnshieldWithShieldedFee(_) | TokenPurchaseFromShieldedPool(_) + | PrivateDocumentCreateFromShieldedPool(_) | IdentityTopUpFromShieldedPool(_) => None, } } @@ -678,6 +681,7 @@ impl StateTransitionWasm { | TokenShieldedTransferWithShieldedFee(_) | TokenUnshieldWithShieldedFee(_) | TokenPurchaseFromShieldedPool(_) + | PrivateDocumentCreateFromShieldedPool(_) | IdentityTopUpFromShieldedPool(_) => { return Err(WasmDppError::invalid_argument( "Cannot set owner for shielded transition", @@ -776,6 +780,7 @@ impl StateTransitionWasm { | TokenShieldedTransferWithShieldedFee(_) | TokenUnshieldWithShieldedFee(_) | TokenPurchaseFromShieldedPool(_) + | PrivateDocumentCreateFromShieldedPool(_) | IdentityTopUpFromShieldedPool(_) => { return Err(WasmDppError::invalid_argument( "Cannot set identity contract nonce for shielded transition", @@ -901,6 +906,7 @@ impl StateTransitionWasm { | TokenShieldedTransferWithShieldedFee(_) | TokenUnshieldWithShieldedFee(_) | TokenPurchaseFromShieldedPool(_) + | PrivateDocumentCreateFromShieldedPool(_) | IdentityTopUpFromShieldedPool(_) => { return Err(WasmDppError::invalid_argument( "Cannot set identity nonce for shielded transition", From c71f7afd1e0171ef2869478b244edb65f91f5d1c Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Nov 2026 17:29:38 +0700 Subject: [PATCH 2/5] fix(platform): meter private document creates in DAPI, estimate their appends, and check entries in the builder - rs-dapi counts a PrivateDocumentCreateFromShieldedPool's Orchard actions, so a source whose type 29 proofs keep failing spends its per-source budget. - The private document append describes its layers when Drive estimates a batch: the contract's document types, the type's tree and the store at the chunk power it committed, which the transform now reads with the store check and the action carries with the contract. Estimated before applied across a chunk's compaction, the entries' estimate covers what they meter. - The entry checks of basic structure are one function, validate_private_document_create_entries, which the SDK builder runs before it proves anything. - Tests: the builder's transition through block processing, the builder's refusals before proving, and a wasm-dpp2 spec for the new wrapper. Co-Authored-By: Claude Opus 5.5 --- .../shielded_proof_failure_budget.rs | 48 ++++ ...vate_document_create_from_shielded_pool.rs | 92 +++++-- .../v0/mod.rs | 1 + .../v0/state_transition_validation.rs | 102 ++++---- .../tests.rs | 98 +++++++- .../transform_into_action/v0/mod.rs | 9 +- .../document/private_document_store/mod.rs | 72 ++++-- .../document/private_document_store/v0/mod.rs | 80 ++++++- ...nt_create_from_shielded_pool_transition.rs | 226 ++++++++++++------ .../mod.rs | 11 +- .../transformer.rs | 6 + .../v0/mod.rs | 7 +- .../v0/transformer.rs | 10 +- .../batch/drive_op_batch/private_document.rs | 15 +- ...ntCreateFromShieldedPoolTransition.spec.ts | 150 ++++++++++++ 15 files changed, 751 insertions(+), 176 deletions(-) create mode 100644 packages/wasm-dpp2/tests/unit/PrivateDocumentCreateFromShieldedPoolTransition.spec.ts diff --git a/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs b/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs index 375b76c7c66..92c50e9eb2b 100644 --- a/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs +++ b/packages/rs-dapi/src/services/platform_service/shielded_proof_failure_budget.rs @@ -22,6 +22,7 @@ use dpp::serialization::PlatformDeserializableUntrusted; use dpp::state_transition::StateTransition; use dpp::state_transition::identity_create_from_shielded_pool_transition::IdentityCreateFromShieldedPoolTransition; use dpp::state_transition::identity_top_up_from_shielded_pool_transition::IdentityTopUpFromShieldedPoolTransition; +use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; use dpp::state_transition::shield_from_asset_lock_transition::ShieldFromAssetLockTransition; use dpp::state_transition::shield_from_identity_transition::ShieldFromIdentityTransition; use dpp::state_transition::shield_transition::ShieldTransition; @@ -136,6 +137,9 @@ pub(super) fn orchard_action_count(state_transition_bytes: &[u8]) -> usize { StateTransition::IdentityTopUpFromShieldedPool( IdentityTopUpFromShieldedPoolTransition::V0(v0), ) => v0.actions.len(), + StateTransition::PrivateDocumentCreateFromShieldedPool( + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0), + ) => v0.actions.len(), _ => 0, } } @@ -325,8 +329,10 @@ mod tests { use dpp::consensus::codes::ErrorWithCode; use dpp::consensus::state::shielded::invalid_shielded_proof_error::InvalidShieldedProofError; use dpp::consensus::state::state_error::StateError; + use dpp::platform_value::{BinaryData, Identifier}; use dpp::serialization::PlatformSerializable; use dpp::shielded::SerializedAction; + use dpp::state_transition::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; use dpp::state_transition::shielded_transfer_transition::v0::ShieldedTransferTransitionV0; use std::net::Ipv6Addr; @@ -585,4 +591,46 @@ mod tests { assert_eq!(orchard_action_count(&bytes), 3); assert_eq!(orchard_action_count(&[0xff, 0x00]), 0); } + + /// A private document create is verified unpaid like the other pool-paid spends, so a + /// source whose type 29 proofs keep failing spends its allowance like any other. + #[test] + fn should_meter_a_private_document_create_and_refuse_a_source_whose_proofs_keep_failing() { + let create = StateTransition::PrivateDocumentCreateFromShieldedPool( + PrivateDocumentCreateFromShieldedPoolTransition::V0( + PrivateDocumentCreateFromShieldedPoolTransitionV0 { + data_contract_id: Identifier::from([9; 32]), + document_type_name: "privateContactRequest".to_string(), + entries: vec![BinaryData::new(vec![1; 585])], + actions: vec![ + SerializedAction { + nullifier: [1; 32], + rk: [2; 32], + cmx: [3; 32], + encrypted_note: vec![4; 216], + cv_net: [5; 32], + spend_auth_sig: [6; 64], + }; + 2 + ], + value_balance: 1, + anchor: [7; 32], + proof: vec![0; 100], + binding_signature: [0; 64], + }, + ), + ); + let actions = orchard_action_count(&create.serialize_to_bytes().unwrap()); + assert_eq!(actions, 2); + + let budget = budget(); + let now = Instant::now(); + broadcast(&budget, SOURCE, actions, true, now).unwrap(); + broadcast(&budget, SOURCE, actions, true, now).unwrap(); + assert_eq!( + broadcast(&budget, SOURCE, actions, true, now), + Err(ACTION_DRAIN_INTERVAL * 2), + "a third failing create waits until one bundle has drained" + ); + } } diff --git a/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs index bb7489606f3..f52d23be604 100644 --- a/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs +++ b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs @@ -7,6 +7,7 @@ use crate::shielded::{ private_document_create_from_shielded_pool_extra_sighash_data, }; use crate::state_transition::private_document_create_from_shielded_pool_transition::methods::PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0; +use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::validate_private_document_create_entries; use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; use crate::state_transition::StateTransition; use crate::ProtocolError; @@ -22,9 +23,11 @@ use super::{build_spend_bundle, serialize_authorized_bundle, OrchardProver, Spen /// The fee is not a parameter: the bundle's value balance is the fee, and consensus pins it to /// exactly `compute_private_document_create_from_shielded_pool_fee` for the bundle's action /// count and the entries' number and size, so there is nothing for the caller to choose. The -/// fee is priced at the size of the first entry; every entry must take the type's `entrySize`, -/// which consensus checks. The contract, the type name and every entry are bound into the -/// bundle's sighash. Returns the built transition together with the fee it pays. +/// entries must pass the checks basic structure makes (from 1 to 16, of one size within the cap, +/// no two the same), which run before anything is proved; that they take the type's `entrySize` +/// is for the caller to know, as consensus checks it against the contract. The contract, the type +/// name and every entry are bound into the bundle's sighash. Returns the built transition together +/// with the fee it pays. #[allow(clippy::too_many_arguments)] pub fn build_private_document_create_from_shielded_pool_transition( spends: Vec, @@ -38,14 +41,16 @@ pub fn build_private_document_create_from_shielded_pool_transition Result<(StateTransition, Credits), ProtocolError> { - let entry_size = entries.first().map(BinaryData::len).ok_or_else(|| { - ProtocolError::ShieldedBuildError("a private document create needs an entry".to_string()) - })?; - let entry_size = u32::try_from(entry_size).map_err(|_| { - ProtocolError::ShieldedBuildError(format!( - "a private document entry of {entry_size} bytes is too large" - )) - })?; + // The entries consensus refuses are refused here, before any proving. + if let Some(error) = validate_private_document_create_entries(&entries, platform_version) + .errors + .into_iter() + .next() + { + return Err(ProtocolError::ShieldedBuildError(error.to_string())); + } + // Every entry now takes the first one's size, at most `max_private_document_entry_size`. + let entry_size = entries.first().map(|entry| entry.len() as u32).unwrap_or(0); let total_spent = spends .iter() @@ -112,6 +117,7 @@ mod tests { use crate::shielded::builder::test_helpers::{ test_orchard_address, test_spendable_note, TestProver, }; + use crate::shielded::builder::OrchardProver; fn keys() -> (FullViewingKey, SpendAuthorizingKey) { let sk = grovedb_commitment_tree::SpendingKey::from_bytes([42u8; 32]) @@ -119,23 +125,71 @@ mod tests { (FullViewingKey::from(&sk), SpendAuthorizingKey::from(&sk)) } - #[test] - fn should_refuse_to_build_without_an_entry() { + /// A prover that fails the test if a build ever reaches the proof. + struct UnreachableProver; + + impl OrchardProver for UnreachableProver { + fn proving_key(&self) -> &grovedb_commitment_tree::ProvingKey { + panic!("the build must refuse the entries before proving") + } + } + + fn build_with(entries: Vec) -> Result<(StateTransition, Credits), ProtocolError> { let (fvk, ask) = keys(); - let err = build_private_document_create_from_shielded_pool_transition( - vec![test_spendable_note(1_000_000_000)], + build_private_document_create_from_shielded_pool_transition( + vec![test_spendable_note(1_000_000_000_000)], Identifier::from([1u8; 32]), "privateContactRequest".to_string(), - vec![], + entries, &test_orchard_address(), &fvk, &ask, Anchor::empty_tree(), - &TestProver, + &UnreachableProver, PlatformVersion::latest(), ) - .expect_err("an empty entry list has no size to price"); - assert!(err.to_string().contains("needs an entry"), "{err}"); + } + + #[test] + fn should_refuse_entries_consensus_refuses_before_proving() { + let limits = &PlatformVersion::latest().system_limits; + let max_count = limits.max_private_document_entries_per_transition.unwrap(); + let max_size = limits.max_private_document_entry_size.unwrap() as usize; + let cases: Vec<(&str, Vec)> = vec![ + ("no entry", vec![]), + ( + "one entry too many", + (0..=max_count) + .map(|i| BinaryData::new(vec![i as u8; 585])) + .collect(), + ), + ("an empty entry", vec![BinaryData::new(vec![])]), + ( + "an entry over the cap", + vec![BinaryData::new(vec![1u8; max_size + 1])], + ), + ( + "entries of two sizes", + vec![ + BinaryData::new(vec![1u8; 585]), + BinaryData::new(vec![2u8; 584]), + ], + ), + ( + "two identical entries", + vec![ + BinaryData::new(vec![1u8; 585]), + BinaryData::new(vec![1u8; 585]), + ], + ), + ]; + for (case, entries) in cases { + let err = build_with(entries).expect_err(case); + assert!( + matches!(err, ProtocolError::ShieldedBuildError(_)), + "{case}: {err}" + ); + } } #[test] diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs index 9916195209f..0195cd889c6 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/mod.rs @@ -1,5 +1,6 @@ mod state_transition_like; mod state_transition_validation; +pub use state_transition_validation::validate_private_document_create_entries; mod types; pub(super) mod v0_methods; mod version; diff --git a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs index 8d211d52192..0f01d1de243 100644 --- a/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs +++ b/packages/rs-dpp/src/state_transition/state_transitions/shielded/private_document_create_from_shielded_pool_transition/v0/state_transition_validation.rs @@ -11,60 +11,76 @@ use crate::state_transition::state_transitions::shielded::common_validation::{ }; use crate::state_transition::StateTransitionStructureValidation; use crate::validation::SimpleConsensusValidationResult; +use platform_value::BinaryData; use platform_version::version::PlatformVersion; use std::collections::BTreeMap; -impl StateTransitionStructureValidation for PrivateDocumentCreateFromShieldedPoolTransitionV0 { - /// Checks what needs no state: from 1 to `max_private_document_entries_per_transition` - /// entries, each as long as the first and from 1 to `max_private_document_entry_size` - /// bytes, no two the same, then the bundle's actions, value balance, proof and anchor. Whether the entries - /// take the type's `entrySize` needs the contract, so the transform checks it. - fn validate_structure( - &self, - platform_version: &PlatformVersion, - ) -> SimpleConsensusValidationResult { - let limits = &platform_version.system_limits; - // Both limits are `None` before protocol version 15, where the transition is refused - // before its structure is validated; refusing every entry there keeps it so. - let max_count = limits - .max_private_document_entries_per_transition - .unwrap_or(0); - let max_size = limits.max_private_document_entry_size.unwrap_or(0); +/// The checks a private document create's entries take without state, before the bundle's: from +/// 1 to `max_private_document_entries_per_transition` entries, each as long as the first and from +/// 1 to `max_private_document_entry_size` bytes, no two the same. Whether they take the type's +/// `entrySize` needs the contract, so the transform checks it. Basic structure runs it, and the +/// SDK builder runs it before proving, so a wallet never proves a bundle for entries consensus +/// refuses. +pub fn validate_private_document_create_entries( + entries: &[BinaryData], + platform_version: &PlatformVersion, +) -> SimpleConsensusValidationResult { + let limits = &platform_version.system_limits; + // Both limits are `None` before protocol version 15, where the transition is refused + // before its structure is validated; refusing every entry there keeps it so. + let max_count = limits + .max_private_document_entries_per_transition + .unwrap_or(0); + let max_size = limits.max_private_document_entry_size.unwrap_or(0); - if self.entries.is_empty() || self.entries.len() > max_count as usize { + if entries.is_empty() || entries.len() > max_count as usize { + return SimpleConsensusValidationResult::new_with_error( + InvalidPrivateDocumentEntryCountError::new(entries.len() as u64, max_count).into(), + ); + } + + let first_entry_size = entries[0].len(); + for (entry_index, entry) in entries.iter().enumerate() { + let size = entry.len(); + if size == 0 || size != first_entry_size || size > max_size as usize { return SimpleConsensusValidationResult::new_with_error( - InvalidPrivateDocumentEntryCountError::new(self.entries.len() as u64, max_count) - .into(), + InvalidPrivateDocumentEntrySizeError::new( + // At most max_count (a u16) entries reach this loop. + entry_index as u16, + size.min(u32::MAX as usize) as u32, + first_entry_size.min(u32::MAX as usize) as u32, + max_size, + ) + .into(), ); } + } - let first_entry_size = self.entries[0].len(); - for (entry_index, entry) in self.entries.iter().enumerate() { - let size = entry.len(); - if size == 0 || size != first_entry_size || size > max_size as usize { - return SimpleConsensusValidationResult::new_with_error( - InvalidPrivateDocumentEntrySizeError::new( - // At most max_count (a u16) entries reach this loop. - entry_index as u16, - size.min(u32::MAX as usize) as u32, - first_entry_size.min(u32::MAX as usize) as u32, - max_size, - ) - .into(), - ); - } + let mut first_index_of_entry = BTreeMap::new(); + for (entry_index, entry) in entries.iter().enumerate() { + if let Some(first_index) = first_index_of_entry.insert(entry.as_slice(), entry_index as u16) + { + return SimpleConsensusValidationResult::new_with_error( + DuplicatePrivateDocumentEntryError::new(first_index, entry_index as u16).into(), + ); } + } - let mut first_index_of_entry = BTreeMap::new(); - for (entry_index, entry) in self.entries.iter().enumerate() { - if let Some(first_index) = - first_index_of_entry.insert(entry.as_slice(), entry_index as u16) - { - return SimpleConsensusValidationResult::new_with_error( - DuplicatePrivateDocumentEntryError::new(first_index, entry_index as u16).into(), - ); - } + SimpleConsensusValidationResult::new() +} + +impl StateTransitionStructureValidation for PrivateDocumentCreateFromShieldedPoolTransitionV0 { + /// Checks what needs no state: the entries (`validate_private_document_create_entries`), + /// then the bundle's actions, value balance, proof and anchor. + fn validate_structure( + &self, + platform_version: &PlatformVersion, + ) -> SimpleConsensusValidationResult { + let result = validate_private_document_create_entries(&self.entries, platform_version); + if !result.is_valid() { + return result; } + let limits = &platform_version.system_limits; let result = validate_actions_count(&self.actions, limits.max_shielded_transition_actions); if !result.is_valid() { diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs index 5893783a94b..445c81ccc77 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/tests.rs @@ -9,6 +9,7 @@ use crate::platform_types::state_transitions_processing_result::StateTransitionE use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::TempPlatform; use assert_matches::assert_matches; +use dpp::address_funds::OrchardAddress; use dpp::block::block_info::BlockInfo; use dpp::consensus::basic::BasicError; use dpp::consensus::state::state_error::StateError; @@ -19,6 +20,9 @@ use dpp::data_contract::DataContract; use dpp::platform_value::string_encoding::Encoding; use dpp::platform_value::{BinaryData, Identifier}; use dpp::serialization::PlatformSerializable; +use dpp::shielded::builder::{ + build_private_document_create_from_shielded_pool_transition, OrchardProver, SpendableNote, +}; use dpp::shielded::{ compute_private_document_create_from_shielded_pool_fee, private_document_create_from_shielded_pool_extra_sighash_data_v0, SerializedAction, @@ -30,7 +34,7 @@ use dpp::state_transition::StateTransition; use drive::drive::Drive; use grovedb_commitment_tree::{ Builder, BundleType, ClientMemoryCommitmentTree, DashMemo, ExtractedNoteCommitment, - FullViewingKey, Note, NoteValue, Position, RandomSeed, Retention, Rho, Scope, + FullViewingKey, Note, NoteValue, Position, ProvingKey, RandomSeed, Retention, Rho, Scope, SpendAuthorizingKey, SpendingKey, }; use platform_version::version::PlatformVersion; @@ -379,6 +383,98 @@ fn should_prove_the_spent_nullifiers_as_the_affected_state() { assert!(statuses.iter().all(|(_, spent)| *spent)); } +struct Prover; + +impl OrchardProver for Prover { + fn proving_key(&self) -> &ProvingKey { + get_proving_key() + } +} + +/// The SDK builder's transition goes through block processing as is: the fee it returns is the +/// bundle's value balance and the fee for the bundle's actual action count, and its entries are +/// appended. +#[test] +fn should_execute_the_transition_the_sdk_builder_builds() { + let platform_version = PlatformVersion::latest(); + let platform = setup_platform(); + let contract = register_contract(&platform); + + let sk = SpendingKey::from_bytes([0u8; 32]).unwrap(); + let fvk = FullViewingKey::from(&sk); + let ask = SpendAuthorizingKey::from(&sk); + let address = fvk.address_at(0u32, Scope::External); + let mut rho_bytes = [0u8; 32]; + rho_bytes[0] = 1; + let rho = Rho::from_bytes(&rho_bytes).unwrap(); + let rseed = RandomSeed::from_bytes([42u8; 32], &rho).unwrap(); + let note = Note::from_parts(address, NoteValue::from_raw(NOTE_VALUE), rho, rseed).unwrap(); + let cmx = ExtractedNoteCommitment::from(note.commitment()); + let mut tree = ClientMemoryCommitmentTree::new(100); + tree.append(cmx.to_bytes(), Retention::Marked).unwrap(); + tree.checkpoint(0u32).unwrap(); + let anchor = tree.anchor().unwrap(); + let merkle_path = tree.witness(Position::from(0u64), 0).unwrap().unwrap(); + let change_address = OrchardAddress::from_raw_bytes(&address.to_raw_address_bytes()) + .expect("valid orchard address bytes"); + + let entries = entries(2, ENTRY_SIZE); + let (st, fee) = build_private_document_create_from_shielded_pool_transition( + vec![SpendableNote { note, merkle_path }], + contract.id(), + "secret".to_string(), + entries.clone(), + &change_address, + &fvk, + &ask, + anchor, + &Prover, + platform_version, + ) + .expect("the builder builds the transition"); + let StateTransition::PrivateDocumentCreateFromShieldedPool( + PrivateDocumentCreateFromShieldedPoolTransition::V0(v0), + ) = &st + else { + panic!("expected a private document create"); + }; + assert_eq!(v0.value_balance, fee, "the value balance is the fee"); + assert_eq!( + fee, + compute_private_document_create_from_shielded_pool_fee( + v0.actions.len(), + 2, + ENTRY_SIZE, + platform_version, + ) + .expect("fee"), + "the fee is the one for the bundle's actual action count" + ); + seed_pool(&platform, &anchor.to_bytes()); + + let platform_state = platform.state.load(); + let (fee_results, _) = + process_state_transitions(&platform, &[st], BlockInfo::default(), &platform_state); + assert_eq!(fee_results[0].total_base_fee(), fee); + let secret = contract + .document_type_for_name("secret") + .expect("the secret type"); + let page = platform + .drive + .fetch_private_document_entries(CONTRACT_ID, secret, 0, None, platform_version) + .expect("expected to read the store"); + assert_eq!( + page.entries + .iter() + .map(|stored| stored.data.clone()) + .collect::>(), + entries + .iter() + .map(|entry| entry.to_vec()) + .collect::>() + ); +} + // ========================================== // Refusals, all unpaid // ========================================== diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs index 3e0434a9c8c..79ca212584d 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/private_document_create_from_shielded_pool/transform_into_action/v0/mod.rs @@ -144,13 +144,14 @@ impl PrivateDocumentCreateFromShieldedPoolStateTransitionTransformIntoActionVali // Every path that registers a private type creates its store; one missing, or of // another entry size, is corrupted state, which no transition is executed against. - if !drive.private_document_store_takes_entries( + let Some(store_chunk_power) = drive.private_document_store_chunk_power_taking_entries( &v0.data_contract_id.to_buffer(), &v0.document_type_name, entry_size, transaction, platform_version, - )? { + )? + else { return Err(Error::Execution(ExecutionError::CorruptedDriveResponse( format!( "the private document type \"{}\" of contract {} has no store taking entries \ @@ -158,13 +159,15 @@ impl PrivateDocumentCreateFromShieldedPoolStateTransitionTransformIntoActionVali v0.document_type_name, v0.data_contract_id, entry_size ), ))); - } + }; // The minimum fee validation held the value balance to the fee for these entries at // the first entry's size, which is the type's `entrySize`, so the whole value balance // is the fee. let result = PrivateDocumentCreateFromShieldedPoolTransitionAction::try_from_transition( self, + contract_fetch_info.clone(), + store_chunk_power, current_total_balance, v0.value_balance, ); diff --git a/packages/rs-drive/src/drive/document/private_document_store/mod.rs b/packages/rs-drive/src/drive/document/private_document_store/mod.rs index 0d9bbb502cf..23b0c98eb5b 100644 --- a/packages/rs-drive/src/drive/document/private_document_store/mod.rs +++ b/packages/rs-drive/src/drive/document/private_document_store/mod.rs @@ -13,6 +13,8 @@ use crate::error::Error; #[cfg(feature = "server")] use crate::fees::op::LowLevelDriveOperation; #[cfg(feature = "server")] +use crate::util::estimated_layers::EstimatedLayers; +#[cfg(feature = "server")] use crate::util::grove_operations::DirectQueryType; #[cfg(feature = "server")] use crate::util::storage_flags::StorageFlags; @@ -20,6 +22,8 @@ use crate::util::storage_flags::StorageFlags; use dpp::block::block_info::BlockInfo; use dpp::data_contract::document_type::accessors::{DocumentTypeV0Getters, DocumentTypeV2Getters}; use dpp::data_contract::document_type::{DocumentTypeRef, PrivateDocumentConfig}; +#[cfg(feature = "server")] +use dpp::data_contract::DataContract; use dpp::version::PlatformVersion; #[cfg(feature = "server")] use grovedb::{Element, TransactionArg}; @@ -202,20 +206,21 @@ impl Drive { } } - /// Whether the store of the private type `document_type_name` of `contract_id` takes - /// entries of `entry_size` bytes: the element at the type's key `[0]` is a private document - /// store whose committed stored entry size is Drive's header and `entry_size`. A - /// `PrivateDocumentCreateFromShieldedPool` asks before execution, where an append to a - /// missing store, or one of another size, would fail. The read is not billed: the - /// transition pays a fixed fee. - pub fn private_document_store_takes_entries( + /// The chunk power the store of the private type `document_type_name` of `contract_id` + /// committed, if it takes entries of `entry_size` bytes: the element at the type's key `[0]` + /// is a private document store whose committed stored entry size is Drive's header and + /// `entry_size`. `None` otherwise. A `PrivateDocumentCreateFromShieldedPool` asks before + /// execution, where an append to a missing store, or one of another size, would fail, and + /// its append describes the store at this chunk power when it is estimated. The read is not + /// billed: the transition pays a fixed fee. + pub fn private_document_store_chunk_power_taking_entries( &self, contract_id: &[u8; 32], document_type_name: &str, entry_size: u32, transaction: TransactionArg, platform_version: &PlatformVersion, - ) -> Result { + ) -> Result, Error> { let type_path = contract_document_type_path(contract_id, document_type_name); let element = self.grove_get_raw_optional( type_path.as_slice().into(), @@ -227,13 +232,16 @@ impl Drive { )?; let Some(stored_entry_size) = entry_size.checked_add(PRIVATE_DOCUMENT_ENTRY_HEADER_SIZE) else { - return Ok(false); + return Ok(None); }; - Ok(matches!( - element.as_ref().map(Element::underlying), - Some(Element::PrivateDocumentStore(_, committed_entry_size, _, _)) - if *committed_entry_size == stored_entry_size - )) + Ok(match element.as_ref().map(Element::underlying) { + Some(Element::PrivateDocumentStore(_, committed_entry_size, chunk_power, _)) + if *committed_entry_size == stored_entry_size => + { + Some(*chunk_power) + } + _ => None, + }) } } @@ -299,17 +307,32 @@ pub fn insert_empty_private_document_store_operation( } /// The operations appending `entries`, in order, to the store of the private type -/// `document_type_name` of `contract_id`, each after Drive's header: the height and then the -/// time in milliseconds of `block_info` (`encode_stored_private_document_entry`). Every entry -/// must take the type's `entrySize`; GroveDB refuses an entry of another size, so the caller -/// checks it before execution. Only a protocol version admitting private types selects a -/// version of it. +/// `document_type_name` of `contract`, each after Drive's header: the height and then the time +/// in milliseconds of `block_info` (`encode_stored_private_document_entry`). Every entry must +/// take the type's `entrySize`; GroveDB refuses an entry of another size, so the caller checks +/// it before execution. When the batch is estimated, it describes the layers down to the store, +/// the store at `chunk_power`, the chunk power the store committed. Only a protocol version +/// admitting private types selects a version of it. +/// +/// # Parameters +/// - `contract`: the contract defining the private type +/// - `document_type_name`: the private type +/// - `entries`: the client's entries, appended in this order +/// - `chunk_power`: the chunk power the type's store committed +/// - `block_info`: the block whose height and time head each entry +/// - `estimated_costs_only_with_layer_info`: the layer descriptions of an estimated batch +/// - `platform_version`: the protocol version +/// +/// # Returns +/// One GroveDB append per entry. #[cfg(feature = "server")] pub fn append_private_document_entries_operations( - contract_id: &[u8; 32], + contract: &DataContract, document_type_name: &str, entries: &[Vec], + chunk_power: u8, block_info: &BlockInfo, + estimated_costs_only_with_layer_info: &mut Option, platform_version: &PlatformVersion, ) -> Result, Error> { match platform_version @@ -318,12 +341,15 @@ pub fn append_private_document_entries_operations( .document .append_private_document_entries_operations { - Some(0) => Ok(append_private_document_entries_operations_v0( - contract_id, + Some(0) => append_private_document_entries_operations_v0( + contract, document_type_name, entries, + chunk_power, block_info, - )), + estimated_costs_only_with_layer_info, + platform_version, + ), Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "append_private_document_entries_operations".to_string(), known_versions: vec![0], diff --git a/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs b/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs index cdecb29165c..81146b836d2 100644 --- a/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs +++ b/packages/rs-drive/src/drive/document/private_document_store/v0/mod.rs @@ -3,23 +3,43 @@ use crate::drive::document::paths::contract_document_type_path_vec; #[cfg(feature = "server")] use crate::drive::document::private_document_store::{ encode_stored_private_document_entry, private_document_stored_entry_size, - PRIVATE_DOCUMENT_STORE_KEY, + PRIVATE_DOCUMENT_ENTRY_HEADER_SIZE, PRIVATE_DOCUMENT_STORE_KEY, }; +#[cfg(feature = "server")] +use crate::drive::Drive; use crate::error::drive::DriveError; use crate::error::Error; #[cfg(feature = "server")] use crate::fees::op::LowLevelDriveOperation; #[cfg(feature = "server")] +use crate::util::estimated_layers::EstimatedLayers; +#[cfg(feature = "server")] use crate::util::storage_flags::StorageFlags; #[cfg(feature = "server")] use dpp::block::block_info::BlockInfo; #[cfg(feature = "server")] +use dpp::data_contract::accessors::v0::DataContractV0Getters; +#[cfg(feature = "server")] +use dpp::data_contract::config::v0::DataContractConfigGettersV0; +#[cfg(feature = "server")] use dpp::data_contract::document_type::PrivateDocumentConfig; +#[cfg(feature = "server")] +use dpp::data_contract::DataContract; use dpp::version::PlatformVersion; #[cfg(feature = "server")] +use grovedb::batch::KeyInfoPath; +#[cfg(feature = "server")] use grovedb::batch::QualifiedGroveDbOp; #[cfg(feature = "server")] use grovedb::Element; +#[cfg(feature = "server")] +use grovedb::EstimatedLayerCount::EstimatedLevel; +#[cfg(feature = "server")] +use grovedb::EstimatedLayerSizes::{AllItems, AllSubtrees}; +#[cfg(feature = "server")] +use grovedb::EstimatedSumTrees::NoSumTrees; +#[cfg(feature = "server")] +use grovedb::{EstimatedLayerInformation, TreeType}; /// The chunk power of a store whose stored entries take `stored_entry_size` bytes: the largest /// that keeps a compacted chunk near the target of `platform_version`'s system limits, @@ -71,17 +91,63 @@ pub(super) fn insert_empty_private_document_store_operation_v0( } /// One GroveDB append per entry, into the store at the type's key `[0]`, each entry after Drive's -/// header. The entries land at consecutive positions in this order. +/// header. The entries land at consecutive positions in this order. An estimated batch gets the +/// layers down to the contract's document types (`add_estimation_costs_for_levels_up_to_contract_document_type_excluded`), +/// the type's tree, which holds the store alone, and the store's own layer at its committed +/// chunk power, which GroveDB's estimator requires. #[cfg(feature = "server")] pub(super) fn append_private_document_entries_operations_v0( - contract_id: &[u8; 32], + contract: &DataContract, document_type_name: &str, entries: &[Vec], + chunk_power: u8, block_info: &BlockInfo, -) -> Vec { - let mut store_path = contract_document_type_path_vec(contract_id, document_type_name); + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, +) -> Result, Error> { + let contract_id = contract.id().to_buffer(); + if let Some(estimated_costs) = estimated_costs_only_with_layer_info { + Drive::add_estimation_costs_for_levels_up_to_contract_document_type_excluded( + contract, + estimated_costs, + &platform_version.drive, + )?; + // The store carries the contract's storage flags, as the type's tree does. + let storage_flags = contract + .config() + .can_be_deleted() + .then(|| StorageFlags::approximate_size(true, None)); + estimated_costs.insert( + KeyInfoPath::from_known_owned_path(contract_document_type_path_vec( + &contract_id, + document_type_name, + )), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + // A private type's tree holds its store and nothing else. + estimated_layer_count: EstimatedLevel(0, false), + estimated_layer_sizes: AllSubtrees(1, NoSumTrees, storage_flags), + }, + ); + let stored_entry_size = entries + .first() + .map(|entry| entry.len() as u32) + .unwrap_or(0) + .saturating_add(PRIVATE_DOCUMENT_ENTRY_HEADER_SIZE); + let mut store_path = contract_document_type_path_vec(&contract_id, document_type_name); + store_path.push(vec![PRIVATE_DOCUMENT_STORE_KEY]); + estimated_costs.insert( + KeyInfoPath::from_known_owned_path(store_path), + EstimatedLayerInformation { + tree_type: TreeType::PrivateDocumentStore(chunk_power), + estimated_layer_count: EstimatedLevel(0, false), + estimated_layer_sizes: AllItems(8, stored_entry_size, None), + }, + ); + } + let mut store_path = contract_document_type_path_vec(&contract_id, document_type_name); store_path.push(vec![PRIVATE_DOCUMENT_STORE_KEY]); - entries + Ok(entries .iter() .map(|entry| { LowLevelDriveOperation::GroveOperation( @@ -95,5 +161,5 @@ pub(super) fn append_private_document_entries_operations_v0( ), ) }) - .collect() + .collect()) } diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs index 02b832f855a..a4d092302ce 100644 --- a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs @@ -48,9 +48,10 @@ impl DriveHighLevelOperationConverter for PrivateDocumentCreateFromShieldedPoolT // every entry takes the type's entry size, so the append cannot fail. ops.push(DriveOperation::PrivateDocumentOperation( PrivateDocumentOperationType::AppendEntries { - contract_id: v0.data_contract_id.to_buffer(), + contract: v0.data_contract, document_type_name: v0.document_type_name, entries: v0.entries, + chunk_power: v0.store_chunk_power, }, )); @@ -71,6 +72,7 @@ impl DriveHighLevelOperationConverter for PrivateDocumentCreateFromShieldedPoolT mod tests { use super::super::fee_floor_support; use super::*; + use crate::drive::contract::DataContractFetchInfo; use crate::drive::document::private_document_store::PRIVATE_DOCUMENT_STORE_KEY; use crate::drive::{Drive, RootTree}; use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; @@ -78,13 +80,16 @@ mod tests { use crate::util::batch::drive_op_batch::ShieldedPoolOperationType; 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::conversion::value::v0::DataContractValueConversionMethodsV0; use dpp::data_contract::DataContract; use dpp::platform_value::string_encoding::Encoding; use dpp::platform_value::Identifier; use dpp::shielded::compute_private_document_create_from_shielded_pool_fee; use grovedb::TransactionArg; + use grovedb_costs::OperationCost; use serde_json::json; + use std::sync::Arc; const CONTRACT_ID: [u8; 32] = [7; 32]; @@ -97,7 +102,46 @@ mod tests { } } + /// The contract holding the private `secret` type of `entry_size`-byte entries. + fn contract(entry_size: u32) -> Arc { + let contract = DataContract::from_value( + dpp::platform_value::to_value(json!({ + "$formatVersion": "1", + "id": Identifier::from(CONTRACT_ID).to_string(Encoding::Base58), + "ownerId": Identifier::from([8; 32]).to_string(Encoding::Base58), + "version": 1, + "documentSchemas": { + "secret": { + "type": "object", + "private": { "entrySize": entry_size }, + "documentsMutable": false, + "canBeDeleted": false, + "properties": { + "accountReference": { + "type": "integer", "minimum": 0, "maximum": 4294967295u64, + "position": 0 + } + }, + "additionalProperties": false + } + } + })) + .expect("the contract converts"), + true, + PlatformVersion::latest(), + ) + .expect("the contract registers"); + Arc::new(DataContractFetchInfo { + contract, + storage_flags: None, + cost: OperationCost::default(), + fee: None, + }) + } + fn action( + contract: Arc, + store_chunk_power: u8, notes: Vec, entries: Vec>, fee_amount: u64, @@ -105,9 +149,10 @@ mod tests { ) -> PrivateDocumentCreateFromShieldedPoolTransitionAction { PrivateDocumentCreateFromShieldedPoolTransitionAction::V0( PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { - data_contract_id: Identifier::from(CONTRACT_ID), + data_contract: contract, document_type_name: "secret".to_string(), entries, + store_chunk_power, notes, anchor: [9; 32], fee_amount, @@ -125,8 +170,15 @@ mod tests { #[test] fn should_record_nullifiers_and_change_notes_lower_the_pool_and_append_the_entries_last() { let entries = vec![vec![1u8; 24], vec![2u8; 24]]; - let ops = - ops(action(vec![note(1), note(2)], entries.clone(), 500, 10_000)).expect("operations"); + let ops = ops(action( + contract(24), + 11, + vec![note(1), note(2)], + entries.clone(), + 500, + 10_000, + )) + .expect("operations"); // InsertNullifiers + 2 InsertNote + UpdateTotalBalance + AppendEntries assert_eq!(ops.len(), 5); assert!(matches!( @@ -143,14 +195,16 @@ mod tests { match &ops[4] { DriveOperation::PrivateDocumentOperation( PrivateDocumentOperationType::AppendEntries { - contract_id, + contract, document_type_name, entries: appended, + chunk_power, }, ) => { - assert_eq!(contract_id, &CONTRACT_ID); + assert_eq!(contract.contract.id().to_buffer(), CONTRACT_ID); assert_eq!(document_type_name, "secret"); assert_eq!(appended, &entries); + assert_eq!(*chunk_power, 11); } other => panic!("expected AppendEntries, got {other:?}"), } @@ -158,43 +212,26 @@ mod tests { #[test] fn should_refuse_a_fee_over_the_pool_total() { - assert!(ops(action(vec![note(1)], vec![vec![1u8; 24]], 10_001, 10_000)).is_err()); + assert!(ops(action( + contract(24), + 11, + vec![note(1)], + vec![vec![1u8; 24]], + 10_001, + 10_000 + )) + .is_err()); } - /// A drive holding a contract with the private `secret` type of `entry_size`-byte entries. - fn drive_with_private_type(entry_size: u32) -> Drive { + /// A drive holding the contract with the private `secret` type of `entry_size`-byte entries, + /// and the chunk power its store committed. + fn drive_with_private_type(entry_size: u32) -> (Drive, Arc, u8) { let drive = setup_drive_with_initial_state_structure(None); let platform_version = PlatformVersion::latest(); - let contract = DataContract::from_value( - dpp::platform_value::to_value(json!({ - "$formatVersion": "1", - "id": Identifier::from(CONTRACT_ID).to_string(Encoding::Base58), - "ownerId": Identifier::from([8; 32]).to_string(Encoding::Base58), - "version": 1, - "documentSchemas": { - "secret": { - "type": "object", - "private": { "entrySize": entry_size }, - "documentsMutable": false, - "canBeDeleted": false, - "properties": { - "accountReference": { - "type": "integer", "minimum": 0, "maximum": 4294967295u64, - "position": 0 - } - }, - "additionalProperties": false - } - } - })) - .expect("the contract converts"), - true, - platform_version, - ) - .expect("the contract registers"); + let contract = contract(entry_size); drive .apply_contract( - &contract, + &contract.contract, BlockInfo::default(), true, None, @@ -202,7 +239,17 @@ mod tests { platform_version, ) .expect("expected to register the contract"); - drive + let chunk_power = drive + .private_document_store_chunk_power_taking_entries( + &CONTRACT_ID, + "secret", + entry_size, + None, + platform_version, + ) + .expect("expected to read the store") + .expect("the store takes the type's entries"); + (drive, contract, chunk_power) } fn store_count(drive: &Drive, transaction: TransactionArg) -> u64 { @@ -224,12 +271,37 @@ mod tests { .expect("the store's count") } + #[test] + fn should_read_the_chunk_power_of_a_store_taking_the_entries_and_none_otherwise() { + let (drive, _, chunk_power) = drive_with_private_type(4096); + // 4096 + 16 bytes to an entry keeps 64 of them near the 512 KiB target. + assert_eq!(chunk_power, 6); + let platform_version = PlatformVersion::latest(); + for (type_name, entry_size) in [("secret", 4095), ("missing", 4096)] { + assert_eq!( + drive + .private_document_store_chunk_power_taking_entries( + &CONTRACT_ID, + type_name, + entry_size, + None, + platform_version, + ) + .expect("expected to read"), + None, + "{type_name} with entries of {entry_size} bytes" + ); + } + } + /// The fixed fee of a private document create, which the pool pays, covers what its /// operations meter, and its entries' storage share covers the entries' metered storage, so /// the pool-paid split (`storage = min(metered storage, fee)`) never zeroes the proposer's /// share. Checked for an entry size at every chunk power a store can take (11 down to 6), /// for one entry and for the most a transition carries, over appends that cross a chunk's - /// compaction. + /// compaction. Every batch is estimated before it is applied: the estimate succeeds, with the + /// store described at its committed chunk power, and the entries' estimate covers what their + /// append meters. #[test] fn should_charge_a_fixed_fee_covering_the_metered_cost_at_every_chunk_power() { let platform_version = PlatformVersion::latest(); @@ -245,7 +317,7 @@ mod tests { let mut nonce = 0u8; let mut note_index = 0u32; for entry_size in [1u32, 200, 400, 585, 1500, 3000, 4096] { - let drive = drive_with_private_type(entry_size); + let (drive, contract, chunk_power) = drive_with_private_type(entry_size); let transaction = drive.grove.start_transaction(); let entry_storage_allowance = (entry_size as u64 + private_document.entry_overhead_bytes) * per_byte; @@ -272,49 +344,61 @@ mod tests { ) .expect("fee"); note_index += 2; - let operations = ops(action( - vec![ - fee_floor_support::note(note_index), - fee_floor_support::note(note_index + 1), - ], - entries.clone(), - fee, - u64::MAX / 2, - )) - .expect("operations"); - let metered = drive - .apply_drive_operations( + let transition_operations = || { + ops(action( + contract.clone(), + chunk_power, + vec![ + fee_floor_support::note(note_index), + fee_floor_support::note(note_index + 1), + ], + entries.clone(), + fee, + u64::MAX / 2, + )) + .expect("operations") + }; + let apply = |operations, apply| { + drive.apply_drive_operations( operations, - true, + apply, &BlockInfo::default(), Some(&transaction), platform_version, None, ) - .expect("apply"); + }; + apply(transition_operations(), false).unwrap_or_else(|e| { + panic!("entry size {entry_size}: the transition must estimate: {e}") + }); + let metered = apply(transition_operations(), true).expect("apply"); assert!( fee >= metered.total_base_fee(), "entry size {entry_size}, {entry_count} entries: fixed fee {fee} must cover \ the metered {}", metered.total_base_fee() ); - // The entries alone, metered on their own append. - let entries_metered = drive - .apply_drive_operations( - vec![DriveOperation::PrivateDocumentOperation( - PrivateDocumentOperationType::AppendEntries { - contract_id: CONTRACT_ID, - document_type_name: "secret".to_string(), - entries, - }, - )], - true, - &BlockInfo::default(), - Some(&transaction), - platform_version, - None, - ) - .expect("apply the entries"); + // The entries alone, estimated and then metered on their own append. + let entries_operations = || { + vec![DriveOperation::PrivateDocumentOperation( + PrivateDocumentOperationType::AppendEntries { + contract: contract.clone(), + document_type_name: "secret".to_string(), + entries: entries.clone(), + chunk_power, + }, + )] + }; + let entries_estimated = apply(entries_operations(), false).unwrap_or_else(|e| { + panic!("entry size {entry_size}: the entries must estimate: {e}") + }); + let entries_metered = apply(entries_operations(), true).expect("apply the entries"); + assert!( + entries_estimated.storage_fee >= entries_metered.storage_fee + && entries_estimated.total_base_fee() >= entries_metered.total_base_fee(), + "entry size {entry_size}, {entry_count} entries: the estimate {entries_estimated:?} \ + must cover the metered {entries_metered:?}" + ); assert!( entry_count as u64 * entry_storage_allowance >= entries_metered.storage_fee, "entry size {entry_size}, {entry_count} entries: the entries' storage \ diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs index 09f663a91ad..879e5cdfae8 100644 --- a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/mod.rs @@ -6,6 +6,7 @@ pub mod v0; use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; use crate::state_transition_action::shielded::ShieldedActionNote; use derive_more::From; +use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::fee::Credits; use dpp::platform_value::Identifier; @@ -20,7 +21,15 @@ impl PrivateDocumentCreateFromShieldedPoolTransitionAction { /// The contract that defines the private document type pub fn data_contract_id(&self) -> Identifier { match self { - PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => t.data_contract_id, + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => { + t.data_contract.contract.id() + } + } + } + /// The chunk power the type's store committed + pub fn store_chunk_power(&self) -> u8 { + match self { + PrivateDocumentCreateFromShieldedPoolTransitionAction::V0(t) => t.store_chunk_power, } } /// The private document type the entries are appended to diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs index b31982acef6..04ef67a3a3f 100644 --- a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/transformer.rs @@ -1,14 +1,18 @@ +use crate::drive::contract::DataContractFetchInfo; use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::PrivateDocumentCreateFromShieldedPoolTransitionAction; use dpp::fee::Credits; use dpp::prelude::ConsensusValidationResult; use dpp::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; +use std::sync::Arc; impl PrivateDocumentCreateFromShieldedPoolTransitionAction { /// Transforms the state transition into an action, once the transformer has checked the /// pool, the target type and the entries' size against state pub fn try_from_transition( value: &PrivateDocumentCreateFromShieldedPoolTransition, + data_contract: Arc, + store_chunk_power: u8, current_total_balance: Credits, fee_amount: Credits, ) -> ConsensusValidationResult { @@ -16,6 +20,8 @@ impl PrivateDocumentCreateFromShieldedPoolTransitionAction { PrivateDocumentCreateFromShieldedPoolTransition::V0(v0) => { PrivateDocumentCreateFromShieldedPoolTransitionActionV0::try_from_transition( v0, + data_contract, + store_chunk_power, current_total_balance, fee_amount, ) diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs index 5463da10796..245c85cc5bc 100644 --- a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/mod.rs @@ -1,19 +1,22 @@ mod transformer; +use crate::drive::contract::DataContractFetchInfo; use crate::state_transition_action::shielded::ShieldedActionNote; use dpp::fee::Credits; -use dpp::platform_value::Identifier; +use std::sync::Arc; /// Entries appended to a private document type's store, paid from the credit shielded pool, /// version 0. #[derive(Debug, Clone)] pub struct PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { /// The contract that defines the private document type - pub data_contract_id: Identifier, + pub data_contract: Arc, /// The private document type the entries are appended to pub document_type_name: String, /// The entries, in the order they are appended, each the type's `entrySize` pub entries: Vec>, + /// The chunk power the type's store committed, read at transform time + pub store_chunk_power: u8, /// Notes built 1:1 from the on-wire Orchard actions pub notes: Vec, /// The anchor the spend was proven against diff --git a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs index d53ad932cd9..fee1a1f8ac4 100644 --- a/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs +++ b/packages/rs-drive/src/state_transition_action/shielded/private_document_create_from_shielded_pool/v0/transformer.rs @@ -1,13 +1,18 @@ use crate::state_transition_action::shielded::private_document_create_from_shielded_pool::v0::PrivateDocumentCreateFromShieldedPoolTransitionActionV0; +use crate::drive::contract::DataContractFetchInfo; use crate::state_transition_action::shielded::ShieldedActionNote; use dpp::fee::Credits; use dpp::prelude::ConsensusValidationResult; use dpp::state_transition::state_transitions::shielded::private_document_create_from_shielded_pool_transition::v0::PrivateDocumentCreateFromShieldedPoolTransitionV0; +use std::sync::Arc; impl PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { - /// Builds the v0 action from the v0 transition, the pool total, and the fee + /// Builds the v0 action from the v0 transition, the contract and the store's chunk power + /// the transform read, the pool total, and the fee pub fn try_from_transition( value: &PrivateDocumentCreateFromShieldedPoolTransitionV0, + data_contract: Arc, + store_chunk_power: u8, current_total_balance: Credits, fee_amount: Credits, ) -> ConsensusValidationResult { @@ -16,9 +21,10 @@ impl PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { ConsensusValidationResult::new_with_data( PrivateDocumentCreateFromShieldedPoolTransitionActionV0 { - data_contract_id: value.data_contract_id, + data_contract, document_type_name: value.document_type_name.clone(), entries: value.entries.iter().map(|entry| entry.to_vec()).collect(), + store_chunk_power, notes, anchor: value.anchor, fee_amount, diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs b/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs index d53e97c166e..e19065d4e1a 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/private_document.rs @@ -1,3 +1,4 @@ +use crate::drive::contract::DataContractFetchInfo; use crate::drive::document::private_document_store::append_private_document_entries_operations; use crate::drive::Drive; use crate::error::Error; @@ -7,6 +8,7 @@ use crate::util::estimated_layers::EstimatedLayers; use dpp::block::block_info::BlockInfo; use dpp::version::PlatformVersion; use grovedb::TransactionArg; +use std::sync::Arc; /// Operations on the store of a private document type (protocol version 15) #[derive(Clone, Debug)] @@ -15,11 +17,13 @@ pub enum PrivateDocumentOperationType { /// header: the block's height and time. AppendEntries { /// The contract that defines the private document type - contract_id: [u8; 32], + contract: Arc, /// The private document type document_type_name: String, /// The entries, each exactly the type's `entrySize` entries: Vec>, + /// The chunk power the type's store committed, which an estimate describes it at + chunk_power: u8, }, } @@ -27,21 +31,24 @@ impl DriveLowLevelOperationConverter for PrivateDocumentOperationType { fn into_low_level_drive_operations( self, _drive: &Drive, - _estimated_costs_only_with_layer_info: &mut Option, + estimated_costs_only_with_layer_info: &mut Option, block_info: &BlockInfo, _transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { match self { PrivateDocumentOperationType::AppendEntries { - contract_id, + contract, document_type_name, entries, + chunk_power, } => append_private_document_entries_operations( - &contract_id, + &contract.contract, &document_type_name, &entries, + chunk_power, block_info, + estimated_costs_only_with_layer_info, platform_version, ), } diff --git a/packages/wasm-dpp2/tests/unit/PrivateDocumentCreateFromShieldedPoolTransition.spec.ts b/packages/wasm-dpp2/tests/unit/PrivateDocumentCreateFromShieldedPoolTransition.spec.ts new file mode 100644 index 00000000000..faf95e3371c --- /dev/null +++ b/packages/wasm-dpp2/tests/unit/PrivateDocumentCreateFromShieldedPoolTransition.spec.ts @@ -0,0 +1,150 @@ +import { expect } from './helpers/chai.ts'; +import { initWasm, wasm } from '../../dist/dpp.compressed.js'; +import { + fakeOrchardAction, + ZERO_ANCHOR, + ZERO_BINDING_SIG, + ZERO_PROOF, +} from './helpers/shielded.ts'; + +before(async () => { + await initWasm(); +}); + +describe('PrivateDocumentCreateFromShieldedPoolTransition', () => { + const dataContractId = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec'; + // Above Number.MAX_SAFE_INTEGER, so JSON has to carry it as a string. + const valueBalance = BigInt('9007199254740993'); + const firstEntry = new Uint8Array(24).fill(1); + const secondEntry = new Uint8Array(24).fill(2); + + function createTransition() { + return new wasm.PrivateDocumentCreateFromShieldedPoolTransition({ + dataContractId, + documentTypeName: 'privateContactRequest', + entries: [firstEntry, secondEntry], + actions: [fakeOrchardAction()], + valueBalance, + anchor: ZERO_ANCHOR, + proof: ZERO_PROOF, + bindingSignature: ZERO_BINDING_SIG, + }); + } + + describe('constructor()', () => { + it('should construct with required fields', () => { + expect(createTransition()).to.be.an.instanceof( + wasm.PrivateDocumentCreateFromShieldedPoolTransition, + ); + }); + + it('should reject an anchor of the wrong length', () => { + expect(() => new wasm.PrivateDocumentCreateFromShieldedPoolTransition({ + dataContractId, + documentTypeName: 'privateContactRequest', + entries: [firstEntry], + actions: [fakeOrchardAction()], + valueBalance, + anchor: new Uint8Array(31), + proof: ZERO_PROOF, + bindingSignature: ZERO_BINDING_SIG, + })).to.throw(); + }); + }); + + describe('getters', () => { + it('should return the target, the entries in order and the value balance', () => { + const t = createTransition(); + expect(t.dataContractId).to.be.an.instanceof(wasm.Identifier); + expect(t.dataContractId.toString()).to.equal(dataContractId); + expect(t.documentTypeName).to.equal('privateContactRequest'); + expect(t.entries).to.have.lengthOf(2); + expect(Buffer.from(t.entries[0])).to.deep.equal(Buffer.from(firstEntry)); + expect(Buffer.from(t.entries[1])).to.deep.equal(Buffer.from(secondEntry)); + expect(t.actions[0]).to.be.an.instanceof(wasm.SerializedOrchardAction); + expect(t.valueBalance).to.equal(valueBalance); + }); + + it('should copy the entries in and out', () => { + const entry = new Uint8Array(24).fill(7); + const t = new wasm.PrivateDocumentCreateFromShieldedPoolTransition({ + dataContractId, + documentTypeName: 'privateContactRequest', + entries: [entry], + actions: [fakeOrchardAction()], + valueBalance, + anchor: ZERO_ANCHOR, + proof: ZERO_PROOF, + bindingSignature: ZERO_BINDING_SIG, + }); + entry.fill(0); + t.entries[0].fill(0); + expect(Buffer.from(t.entries[0])).to.deep.equal(Buffer.from(new Uint8Array(24).fill(7))); + }); + }); + + describe('toObject() / fromObject()', () => { + it('should emit every property with bytes as Uint8Array and round-trip', () => { + const t = createTransition(); + const obj = t.toObject(); + expect(obj.$formatVersion).to.equal('0'); + expect(obj.dataContractId).to.be.instanceOf(Uint8Array); + expect(obj.dataContractId.length).to.equal(32); + expect(obj.documentTypeName).to.equal('privateContactRequest'); + expect(obj.entries).to.be.an('array').with.lengthOf(2); + expect(obj.entries[0]).to.be.instanceOf(Uint8Array); + expect(Buffer.from(obj.entries[1])).to.deep.equal(Buffer.from(secondEntry)); + expect(obj.actions).to.be.an('array').with.lengthOf(1); + expect(obj.valueBalance).to.equal(valueBalance); + expect(obj.anchor).to.be.instanceOf(Uint8Array); + expect(obj.proof).to.be.instanceOf(Uint8Array); + expect(obj.bindingSignature).to.be.instanceOf(Uint8Array); + + const restored = wasm.PrivateDocumentCreateFromShieldedPoolTransition.fromObject(obj); + expect(Buffer.from(restored.toBytes())).to.deep.equal(Buffer.from(t.toBytes())); + }); + }); + + describe('toJSON() / fromJSON()', () => { + it('should emit every property with bytes as base64 and round-trip', () => { + const t = createTransition(); + const json = t.toJSON(); + expect(json.$formatVersion).to.equal('0'); + expect(json.dataContractId).to.equal(dataContractId); + expect(json.documentTypeName).to.equal('privateContactRequest'); + expect(json.entries).to.deep.equal([ + Buffer.from(firstEntry).toString('base64'), + Buffer.from(secondEntry).toString('base64'), + ]); + expect(json.actions).to.be.an('array').with.lengthOf(1); + expect(json.valueBalance).to.equal(valueBalance.toString()); + expect(json.anchor).to.be.a('string').with.lengthOf(44); + expect(json.bindingSignature).to.be.a('string').with.lengthOf(88); + + const restored = wasm.PrivateDocumentCreateFromShieldedPoolTransition.fromJSON(json); + expect(Buffer.from(restored.toBytes())).to.deep.equal(Buffer.from(t.toBytes())); + }); + }); + + describe('toBytes() / fromBytes()', () => { + it('should round-trip via bytes, base64 and hex', () => { + const T = wasm.PrivateDocumentCreateFromShieldedPoolTransition; + const t = createTransition(); + const bytes = t.toBytes(); + expect(Buffer.from(T.fromBytes(bytes).toBytes())).to.deep.equal(Buffer.from(bytes)); + expect(Buffer.from(T.fromBase64(t.toBase64()).toBytes())).to.deep.equal(Buffer.from(bytes)); + expect(Buffer.from(T.fromHex(t.toHex()).toBytes())).to.deep.equal(Buffer.from(bytes)); + }); + }); + + describe('toStateTransition()', () => { + it('should convert to the umbrella wrapper with type 29 and back, keeping the bytes', () => { + const t = createTransition(); + const st = t.toStateTransition(); + expect(st.actionTypeNumber).to.equal(29); + expect(Buffer.from(st.toBytes())).to.deep.equal(Buffer.from(t.toBytes())); + const restored = wasm.PrivateDocumentCreateFromShieldedPoolTransition.fromStateTransition(st); + expect(Buffer.from(restored.toBytes())).to.deep.equal(Buffer.from(t.toBytes())); + }); + }); +}); From ed3a35e169a880b1d7b81a27c82b622a679dba1b Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Nov 2026 18:13:07 +0700 Subject: [PATCH 3/5] fix(platform): refuse an oversized private document create before proving, and activate its basic structure at PV15 only - The SDK builder refuses a transition whose entries and actions already exceed max_state_transition_size before it proves anything, and checks the built transition's size, proof included, before returning it. - The builder tests spend a note with the anchor its witness proves, so a build that skipped the entry checks would reach the prover (a control test shows it does), and each refusal is held to the exact entry error. - The transition's basic_structure slot is None in every validation table before DRIVE_ABCI_VALIDATION_VERSIONS_V11, which sets Some(0); the cross-table test holds it to where private types parse. Co-Authored-By: Claude Opus 5.5 --- ...vate_document_create_from_shielded_pool.rs | 118 ++++++++++++++++-- .../dpp_versions/dpp_contract_versions/mod.rs | 13 +- .../drive_abci_validation_versions/v10.rs | 2 +- .../drive_abci_validation_versions/v11.rs | 11 ++ 4 files changed, 130 insertions(+), 14 deletions(-) diff --git a/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs index f52d23be604..a6c86092981 100644 --- a/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs +++ b/packages/rs-dpp/src/shielded/builder/private_document_create_from_shielded_pool.rs @@ -2,6 +2,8 @@ use grovedb_commitment_tree::{Anchor, FullViewingKey, SpendAuthorizingKey}; use crate::address_funds::OrchardAddress; use crate::fee::Credits; +use crate::serialization::PlatformSerializable; +use crate::shielded::SerializedAction; use crate::shielded::{ compute_private_document_create_from_shielded_pool_fee, private_document_create_from_shielded_pool_extra_sighash_data, @@ -9,13 +11,25 @@ use crate::shielded::{ use crate::state_transition::private_document_create_from_shielded_pool_transition::methods::PrivateDocumentCreateFromShieldedPoolTransitionMethodsV0; use crate::state_transition::private_document_create_from_shielded_pool_transition::v0::validate_private_document_create_entries; use crate::state_transition::private_document_create_from_shielded_pool_transition::PrivateDocumentCreateFromShieldedPoolTransition; -use crate::state_transition::StateTransition; +use crate::state_transition::state_transitions::shielded::common_validation::ENCRYPTED_NOTE_SIZE; +use crate::state_transition::{validate_state_transition_size, StateTransition}; use crate::ProtocolError; use platform_value::{BinaryData, Identifier}; use platform_version::version::PlatformVersion; use super::{build_spend_bundle, serialize_authorized_bundle, OrchardProver, SpendableNote}; +/// Refuses `state_transition` if its serialized bytes are over the protocol's +/// `max_state_transition_size`, as every node and DAPI would. +fn check_size( + state_transition: &StateTransition, + platform_version: &PlatformVersion, +) -> Result<(), ProtocolError> { + let size = state_transition.serialize_to_bytes()?.len() as u64; + validate_state_transition_size(size, platform_version) + .map_err(|error| ProtocolError::ShieldedBuildError(error.to_string())) +} + /// Build a `PrivateDocumentCreateFromShieldedPool` transition (protocol version 15): append /// `entries` to the private document type `document_type_name` of `data_contract_id`, paying the /// fee from `spends` and sending the change back to `change_address`. @@ -25,9 +39,11 @@ use super::{build_spend_bundle, serialize_authorized_bundle, OrchardProver, Spen /// count and the entries' number and size, so there is nothing for the caller to choose. The /// entries must pass the checks basic structure makes (from 1 to 16, of one size within the cap, /// no two the same), which run before anything is proved; that they take the type's `entrySize` -/// is for the caller to know, as consensus checks it against the contract. The contract, the type -/// name and every entry are bound into the bundle's sighash. Returns the built transition together -/// with the fee it pays. +/// is for the caller to know, as consensus checks it against the contract. The transition must +/// fit `max_state_transition_size`: one whose entries and actions alone do not is refused before +/// proving, and the built one is checked again with its proof. The contract, the type name and +/// every entry are bound into the bundle's sighash. Returns the built transition together with +/// the fee it pays. #[allow(clippy::too_many_arguments)] pub fn build_private_document_create_from_shielded_pool_transition( spends: Vec, @@ -63,6 +79,30 @@ pub fn build_private_document_create_from_shielded_pool_transition (FullViewingKey, SpendAuthorizingKey) { let sk = grovedb_commitment_tree::SpendingKey::from_bytes([42u8; 32]) @@ -134,25 +177,44 @@ mod tests { } } + /// A funded note and the anchor its witness proves it under, so a build gets past + /// `add_spend` and reaches the prover unless the entries are refused first. + fn spend_with_its_anchor() -> (SpendableNote, Anchor) { + let spend = test_spendable_note(1_000_000_000_000); + let anchor = spend + .merkle_path + .root(ExtractedNoteCommitment::from(spend.note.commitment())); + (spend, anchor) + } + fn build_with(entries: Vec) -> Result<(StateTransition, Credits), ProtocolError> { let (fvk, ask) = keys(); + let (spend, anchor) = spend_with_its_anchor(); build_private_document_create_from_shielded_pool_transition( - vec![test_spendable_note(1_000_000_000_000)], + vec![spend], Identifier::from([1u8; 32]), "privateContactRequest".to_string(), entries, &test_orchard_address(), &fvk, &ask, - Anchor::empty_tree(), + anchor, &UnreachableProver, PlatformVersion::latest(), ) } + /// The control for the refusals below: with valid entries the fixture reaches the prover. + #[test] + #[should_panic(expected = "the build must refuse the entries before proving")] + fn should_reach_the_prover_with_entries_consensus_accepts() { + let _ = build_with(vec![BinaryData::new(vec![1u8; 585])]); + } + #[test] fn should_refuse_entries_consensus_refuses_before_proving() { - let limits = &PlatformVersion::latest().system_limits; + let platform_version = PlatformVersion::latest(); + let limits = &platform_version.system_limits; let max_count = limits.max_private_document_entries_per_transition.unwrap(); let max_size = limits.max_private_document_entry_size.unwrap() as usize; let cases: Vec<(&str, Vec)> = vec![ @@ -184,11 +246,43 @@ mod tests { ), ]; for (case, entries) in cases { - let err = build_with(entries).expect_err(case); - assert!( - matches!(err, ProtocolError::ShieldedBuildError(_)), - "{case}: {err}" - ); + let expected = validate_private_document_create_entries(&entries, platform_version) + .errors + .into_iter() + .next() + .unwrap_or_else(|| panic!("{case}: basic structure refuses it")) + .to_string(); + match build_with(entries) { + Err(ProtocolError::ShieldedBuildError(message)) => { + assert_eq!(message, expected, "{case}") + } + other => panic!("{case}: expected the entry refusal, got {other:?}"), + } + } + } + + /// Sixteen entries of the largest size are valid entries, but 64 KiB is far over the + /// transition size limit, so the build is refused before proving. + #[test] + fn should_refuse_a_transition_over_the_size_limit_before_proving() { + let platform_version = PlatformVersion::latest(); + let limits = &platform_version.system_limits; + let entries: Vec = + (0..limits.max_private_document_entries_per_transition.unwrap()) + .map(|i| { + BinaryData::new(vec![ + i as u8; + limits.max_private_document_entry_size.unwrap() as usize + ]) + }) + .collect(); + assert!(validate_private_document_create_entries(&entries, platform_version).is_valid()); + match build_with(entries) { + Err(ProtocolError::ShieldedBuildError(message)) => assert!( + message.contains(&format!("maximum {}", limits.max_state_transition_size)), + "{message}" + ), + other => panic!("expected the size refusal, got {other:?}"), } } diff --git a/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs b/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs index 0757e59807f..2b67fe1762a 100644 --- a/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs +++ b/packages/rs-platform-version/src/version/dpp_versions/dpp_contract_versions/mod.rs @@ -289,7 +289,8 @@ mod tests { /// through `fetch_private_document_entries`, `prove_private_document_entries` and /// `verify_private_document_entries`, and appends a private document create's entries, /// at most `max_private_document_entries_per_transition` of them, through - /// `append_private_document_entries_operations`: a protocol version parsing private types without + /// `append_private_document_entries_operations`, after the transition's basic structure: a + /// protocol version parsing private types without /// them could not register or read one, and one setting them where no type is private /// states a rule nothing reads. #[test] @@ -361,6 +362,16 @@ mod tests { "max_private_document_entries_per_transition", limits.max_private_document_entries_per_transition.is_some(), ), + ( + "private_document_create_from_shielded_pool basic_structure", + platform_version + .drive_abci + .validation_and_processing + .state_transitions + .private_document_create_from_shielded_pool_state_transition + .basic_structure + .is_some(), + ), ( "verify_private_document_entries", platform_version diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs index ea0765cc6de..ae45ad2be05 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v10.rs @@ -445,7 +445,7 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V10: DriveAbciValidationVersions = }, private_document_create_from_shielded_pool_state_transition: DriveAbciStateTransitionValidationVersion { - basic_structure: Some(0), + basic_structure: None, // no private document create before protocol version 15 advanced_structure: None, identity_signatures: None, nonce: None, diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs index c1190c7adcc..b2a81decc65 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs @@ -55,6 +55,10 @@ use super::{ /// conversion 1 deletes the commitment from it without reading it again. Any other lookup reads /// as v0's does. /// +/// - the `PrivateDocumentCreateFromShieldedPool` (type 29) `basic_structure`, `Some(0)` rather +/// than `None`: the transition, new at protocol version 15, has its entries, value balance, +/// proof and anchor checked. +/// /// v10 remains unchanged for `PROTOCOL_VERSION_14` chain replay. pub const DRIVE_ABCI_VALIDATION_VERSIONS_V11: DriveAbciValidationVersions = DriveAbciValidationVersions { @@ -104,6 +108,13 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V11: DriveAbciValidationVersions = .state_transitions .batch_state_transition }, + private_document_create_from_shielded_pool_state_transition: + DriveAbciStateTransitionValidationVersion { + basic_structure: Some(0), // a private document create (new in v15) has a basic structure + ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 + .state_transitions + .private_document_create_from_shielded_pool_state_transition + }, ..DRIVE_ABCI_VALIDATION_VERSIONS_V10.state_transitions }, ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 From 7ce672b76f922e7c9354cf79de1a657e369582cf Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Nov 2026 20:15:49 +0700 Subject: [PATCH 4/5] build(platform)!: bump GroveDB to the merge of #1032 and run GROVE_V5 at protocol version 15 GroveDB 12adbd35 brings GROVE_V5 (dashpay/grovedb#1028), moves the store range reads, proofs and creation estimates from GROVE_V4 to it (dashpay/grovedb#1029), and lets a batch carry the same keyless append more than once under it (dashpay/grovedb#1032). DRIVE_VERSION_V10, which only protocol version 15 selects, now runs GROVE_V5 (note 26 of v15.rs). Protocol version 14 keeps GROVE_V4, which the bump leaves as GroveDB 6.1.1 shipped it. QualifiedGroveDbOp::verify_consistency_of_operations now takes the GroveDB version and returns a Result. Drive's two batch appliers and GroveDbOpBatch::verify_consistency_of_operations pass the drive version's; GROVE_V1 to GROVE_V4 count every operation for repeats, so the check refuses what it did through protocol version 14. A Drive test appends two identical entries in one batch. The duplicate entry error's doc no longer gives GroveDB's refusal as its reason: basic structure keeps refusing identical entries, as copies a recipient drops. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 +++++++++---------- Cargo.toml | 18 +++++------ .../duplicate_private_document_entry_error.rs | 3 +- ...nt_create_from_shielded_pool_transition.rs | 27 ++++++++++++++++ .../src/util/batch/grovedb_op_batch/mod.rs | 17 +++++++--- .../v0/mod.rs | 7 ++-- .../v0/mod.rs | 7 ++-- .../tests/drive_storage_ops_coverage.rs | 5 ++- .../src/version/drive_versions/v10.rs | 12 +++++++ .../rs-platform-version/src/version/v15.rs | 16 +++++++++- 10 files changed, 106 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c59ed9fc84..dcedf6fa136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3237,7 +3237,7 @@ dependencies = [ [[package]] name = "grovedb" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "axum 0.8.9", "blake3", @@ -3296,7 +3296,7 @@ dependencies = [ [[package]] name = "grovedb-bulk-append-tree" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-bincode", @@ -3314,7 +3314,7 @@ dependencies = [ [[package]] name = "grovedb-commitment-tree" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-bulk-append-tree", @@ -3331,7 +3331,7 @@ dependencies = [ [[package]] name = "grovedb-costs" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "integer-encoding", "intmap", @@ -3341,7 +3341,7 @@ dependencies = [ [[package]] name = "grovedb-dense-fixed-sized-merkle-tree" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-bincode", @@ -3355,7 +3355,7 @@ dependencies = [ [[package]] name = "grovedb-element" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "grovedb-bincode", "grovedb-bincode-derive", @@ -3371,7 +3371,7 @@ dependencies = [ [[package]] name = "grovedb-epoch-based-storage-flags" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "grovedb-costs", "hex", @@ -3383,7 +3383,7 @@ dependencies = [ [[package]] name = "grovedb-merk" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "byteorder", @@ -3409,7 +3409,7 @@ dependencies = [ [[package]] name = "grovedb-merkle-mountain-range" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-bincode", @@ -3422,7 +3422,7 @@ dependencies = [ [[package]] name = "grovedb-path" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "hex", ] @@ -3430,7 +3430,7 @@ dependencies = [ [[package]] name = "grovedb-private-document-store" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-bulk-append-tree", @@ -3443,7 +3443,7 @@ dependencies = [ [[package]] name = "grovedb-query" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "byteorder", "ed", @@ -3459,7 +3459,7 @@ dependencies = [ [[package]] name = "grovedb-storage" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "blake3", "grovedb-costs", @@ -3478,7 +3478,7 @@ dependencies = [ [[package]] name = "grovedb-version" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "thiserror 2.0.18", "versioned-feature-core", @@ -3487,7 +3487,7 @@ dependencies = [ [[package]] name = "grovedb-visualize" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "hex", "itertools 0.14.0", @@ -3496,7 +3496,7 @@ dependencies = [ [[package]] name = "grovedbg-types" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=9b19035f51905b156e8a628c10b71b85797ffa21#9b19035f51905b156e8a628c10b71b85797ffa21" +source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" dependencies = [ "serde", "serde_with 3.21.0", diff --git a/Cargo.toml b/Cargo.toml index 393766eb6a9..6da85829926 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,15 +61,15 @@ members = [ [workspace.dependencies] secp256k1 = { version = "=0.33.1", features = ["rand", "serde", "recovery"] } bincode = { package = "grovedb-bincode", version = "=2.1.0" } -grovedb = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21", default-features = false } -grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-merk = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21", default-features = false } -grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "9b19035f51905b156e8a628c10b71b85797ffa21" } +grovedb = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c", default-features = false } +grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-merk = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c", default-features = false } +grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } +grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "12adbd357522a03efad4ae609499c93fe8cc3e2c" } dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "c9aeac0d0dcbd4b91d780a70d61b9bb693199ca4" } dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "c9aeac0d0dcbd4b91d780a70d61b9bb693199ca4" } dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "c9aeac0d0dcbd4b91d780a70d61b9bb693199ca4" } diff --git a/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs b/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs index 66733657a02..437675d9542 100644 --- a/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs +++ b/packages/rs-dpp/src/errors/consensus/basic/document/duplicate_private_document_entry_error.rs @@ -9,8 +9,7 @@ use thiserror::Error; /// Two entries of a `PrivateDocumentCreateFromShieldedPool` are the same bytes (protocol version /// 15). A recipient keeps one copy of an entry, so a second copy in the same transition would -/// only be stored for nothing, and Drive appends every entry of a transition in one batch, behind -/// the same header, where two identical appends are the same operation twice. +/// only be stored, and scanned by every wallet of the type, for nothing. #[derive( Error, Debug, diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs index a4d092302ce..1f85911d3d6 100644 --- a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/shielded/private_document_create_from_shielded_pool_transition.rs @@ -411,4 +411,31 @@ mod tests { assert_eq!(store_count(&drive, Some(&transaction)), appended); } } + + /// Basic structure refuses two identical entries in one transition, but the store takes + /// them: from protocol version 15, GroveDB V5 applies repeated appends of the same value to + /// one store in one batch, which earlier GroveDB versions refuse as the same operation twice. + #[test] + fn should_append_two_identical_entries_in_one_batch() { + let platform_version = PlatformVersion::latest(); + let (drive, contract, chunk_power) = drive_with_private_type(24); + drive + .apply_drive_operations( + vec![DriveOperation::PrivateDocumentOperation( + PrivateDocumentOperationType::AppendEntries { + contract, + document_type_name: "secret".to_string(), + entries: vec![vec![5u8; 24], vec![5u8; 24]], + chunk_power, + }, + )], + true, + &BlockInfo::default(), + None, + platform_version, + None, + ) + .expect("expected to append both entries"); + assert_eq!(store_count(&drive, None), 2); + } } diff --git a/packages/rs-drive/src/util/batch/grovedb_op_batch/mod.rs b/packages/rs-drive/src/util/batch/grovedb_op_batch/mod.rs index 06b93cc50bc..bb778d2599f 100644 --- a/packages/rs-drive/src/util/batch/grovedb_op_batch/mod.rs +++ b/packages/rs-drive/src/util/batch/grovedb_op_batch/mod.rs @@ -22,6 +22,7 @@ use grovedb::batch::{ }; use grovedb::operations::proof::util::hex_to_ascii; use grovedb::{Element, TreeType}; +use grovedb_version::version::GroveVersion; use std::borrow::Cow; use std::fmt; @@ -457,8 +458,11 @@ pub trait GroveDbOpBatchV0Methods { /// Adds an `Insert` operation with an element to a list of GroveDB ops. fn add_insert(&mut self, path: Vec>, key: Vec, element: Element); - /// Verify consistency of operations - fn verify_consistency_of_operations(&self) -> GroveDbOpConsistencyResults; + /// Verify consistency of operations, by the rules of `grove_version` + fn verify_consistency_of_operations( + &self, + grove_version: &GroveVersion, + ) -> Result; /// Check if the batch contains a specific path and key. /// @@ -626,9 +630,12 @@ impl GroveDbOpBatchV0Methods for GroveDbOpBatch { ) } - /// Verify consistency of operations - fn verify_consistency_of_operations(&self) -> GroveDbOpConsistencyResults { - QualifiedGroveDbOp::verify_consistency_of_operations(&self.operations) + /// Verify consistency of operations, by the rules of `grove_version` + fn verify_consistency_of_operations( + &self, + grove_version: &GroveVersion, + ) -> Result { + QualifiedGroveDbOp::verify_consistency_of_operations(&self.operations, grove_version) } /// Check if the batch contains a specific path and key. diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v0/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v0/mod.rs index e44000512d7..35b7b05e957 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v0/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v0/mod.rs @@ -32,8 +32,11 @@ impl Drive { // } if self.config.batching_consistency_verification { - let consistency_results = - QualifiedGroveDbOp::verify_consistency_of_operations(&ops.operations); + let consistency_results = QualifiedGroveDbOp::verify_consistency_of_operations( + &ops.operations, + &drive_version.grove_version, + ) + .map_err(Error::from)?; if !consistency_results.is_empty() { tracing::error!( ?consistency_results, diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_partial_batch_with_add_costs/v0/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_partial_batch_with_add_costs/v0/mod.rs index a736817ab8d..b32807379e3 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_partial_batch_with_add_costs/v0/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_partial_batch_with_add_costs/v0/mod.rs @@ -35,8 +35,11 @@ impl Drive { } // println!("batch {:#?}", ops); if self.config.batching_consistency_verification { - let consistency_results = - QualifiedGroveDbOp::verify_consistency_of_operations(&ops.operations); + let consistency_results = QualifiedGroveDbOp::verify_consistency_of_operations( + &ops.operations, + &drive_version.grove_version, + ) + .map_err(Error::from)?; if !consistency_results.is_empty() { // println!("consistency_results {:#?}", consistency_results); return Err(Error::Drive(DriveError::GroveDBInsertion( diff --git a/packages/rs-drive/tests/drive_storage_ops_coverage.rs b/packages/rs-drive/tests/drive_storage_ops_coverage.rs index 64e71329a63..33a8fc3d5cc 100644 --- a/packages/rs-drive/tests/drive_storage_ops_coverage.rs +++ b/packages/rs-drive/tests/drive_storage_ops_coverage.rs @@ -960,6 +960,7 @@ mod grovedb_op_batch_tests { use drive::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use grovedb::batch::{GroveOp, QualifiedGroveDbOp}; use grovedb::{Element, TreeType}; + use platform_version::version::PlatformVersion; #[test] fn new_batch_is_empty() { @@ -1384,7 +1385,9 @@ mod grovedb_op_batch_tests { #[test] fn verify_consistency_of_empty_batch() { let batch = GroveDbOpBatch::new(); - let results = batch.verify_consistency_of_operations(); + let results = batch + .verify_consistency_of_operations(&PlatformVersion::latest().drive.grove_version) + .expect("expected to check the batch"); // An empty batch should have no inconsistencies assert!( results.is_empty(), 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 e2a020e45a7..911a5dfe6b0 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -17,6 +17,7 @@ use crate::version::drive_versions::{ DrivePlatformSystemMethodVersions, DrivePrefundedSpecializedMethodVersions, DriveSystemEstimationCostsMethodVersions, DriveVersion, }; +use grovedb_version::version::v5::GROVE_V5; /// Drive version 10, introduced in protocol v15: /// @@ -80,6 +81,12 @@ use crate::version::drive_versions::{ /// `store_core_fork_activation_height` are `Some(0)` (`None` before): Drive keeps, in `Misc`, /// the chain locked Core height at which Platform first saw each tracked Core fork active. /// +/// * **GroveDB V5** (protocol v15 note 26): `grove_version` is `GROVE_V5` (`GROVE_V4` before). +/// GroveDB reads and proves a private document store's entries by position range only under +/// V5 (`private_document_store.get_range` and `range_proof`), prices a store's creation as it +/// applies it (`merk_insert_element` 1 in both estimators), and lets a batch carry the same +/// keyless append more than once (`apply_batch.keyless_append_repeats`). +/// /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { @@ -123,5 +130,10 @@ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { }, ..DRIVE_VERSION_V9.methods }, + // changed in v10: GROVE_V5 (protocol v15 note 26), whose gates protocol v15 needs: a private + // document store's entries are read and proved by position range, a store's creation is + // estimated as it is applied, and a batch may repeat a keyless append. GROVE_V4, which v9 + // keeps, refuses all three. + grove_version: GROVE_V5, ..DRIVE_VERSION_V9 }; diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index dd21cdfdcbe..7c7670b6dba 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -533,9 +533,23 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// predates it cannot decode one: `validate_shielded_proof` 1 and /// `validate_minimum_shielded_fee` 0, which protocol v14 selects too, gain an arm for it /// that is unreachable there. +/// +/// 26. **GroveDB V5**: `DRIVE_VERSION_V10` selects `GROVE_V5` (protocol v14 keeps `GROVE_V4`, +/// which GroveDB 6.1.1 shipped). Three of its gates differ from V4, and protocol v15 needs +/// each: a private document store's entries are read and proved by position range, which +/// `getPrivateDocumentEntries` (note 13) is built on (`private_document_store.get_range` +/// and `range_proof` 1); creating a store is estimated as it is applied, its existence check +/// and the hashes deriving its empty root included (`merk_insert_element` 1 in the average +/// and worst case estimators), so a contract create or update adding a private type is +/// estimated no lower than it costs; and a batch may carry the same keyless append more +/// than once (`apply_batch.keyless_append_repeats` 1), which the batch consistency check +/// Drive runs for estimates and partial batches used to refuse. No other GroveDB behaviour +/// changes. Private document types and their store exist only from this version, so the +/// first two reach no earlier state; a batch repeating a keyless append arises nowhere in +/// Platform (notes are unique, and note 25 refuses identical entries). 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); create_initial_state_structure 5 adds the token marketplace escrow trees; calculate_total_tokens_balance 2 counts escrowed tokens; the escrow primitives; the identity balance, token balance, prefunded specialized balance and contract fee pot estimations (for_balances 1, for_token_balances 1, estimated_cost_for_prefunded_specialized_balance_update 2, add_estimation_costs_for_contract_fee_pot_update 1) keep a deeper layer description (note 17); fetch_core_fork_activation_height and store_core_fork_activation_height Some(0) keep the Core fork activations (note 18); document method versions v5: append_private_document_entries_operations Some(0) appends a private document create's entries (note 25) + 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); create_initial_state_structure 5 adds the token marketplace escrow trees; calculate_total_tokens_balance 2 counts escrowed tokens; the escrow primitives; the identity balance, token balance, prefunded specialized balance and contract fee pot estimations (for_balances 1, for_token_balances 1, estimated_cost_for_prefunded_specialized_balance_update 2, add_estimation_costs_for_contract_fee_pot_update 1) keep a deeper layer description (note 17); fetch_core_fork_activation_height and store_core_fork_activation_height Some(0) keep the Core fork activations (note 18); document method versions v5: append_private_document_entries_operations Some(0) appends a private document create's entries (note 25); grove_version GROVE_V5 (note 26) 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; consensus_params_update 2 installs the full evidence policy (note 16); update_core_info 1 records the chain locked height at which Core's V24 is first seen active (note 18); build_untied_withdrawal_transactions_from_documents 1 emits asset unlock payload version 2 once V24 is recorded as active (note 20); update_voter_identity 1 re-enables the voter key of the address a masternode returns to (note 22) From b472eb6186b02858285c6e2ab7d745c990dc5025 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 7 Nov 2026 21:06:28 +0700 Subject: [PATCH 5/5] feat(platform)!: price a private document entry's flat fee at 10,000,000 credits FEE_PRIVATE_DOCUMENT_VERSION1's entry_processing_fee goes from 50,000,000 to 10,000,000 credits (0.0001 DASH). Every transition already pays the shielded fee of its bundle, so the flat part only has to price the scan every wallet of the type makes of an entry. An entry still pays its bytes and 32 overhead bytes at the storage rate. A DashPay contact request entry (585 bytes) now costs 26,905,800 credits on top of the bundle's shielded fee. Protocol version 15 is unreleased, so the table is edited in place. Co-Authored-By: Claude Opus 5.5 --- book/src/fees/shielded-fees.md | 2 +- .../src/shielded/compute_minimum_shielded_fee/mod.rs | 2 +- .../src/version/fee/private_document/v1.rs | 8 ++++---- packages/rs-platform-version/src/version/fee/v4.rs | 2 +- packages/rs-platform-version/src/version/v15.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/book/src/fees/shielded-fees.md b/book/src/fees/shielded-fees.md index 2caf0ea3637..c612dcebe1f 100644 --- a/book/src/fees/shielded-fees.md +++ b/book/src/fees/shielded-fees.md @@ -45,7 +45,7 @@ The fee is derived differently depending on the shielded transition type: | **IdentityCreateFromShieldedPool** | `total_fee = metered(insert_nullifiers + AddNewIdentity(identity + N keys)) + shielded_verification_fee`, **moved from the new identity's balance** | `value_balance` is a **fixed `denomination`** (a member of the versioned set `{0.1, 0.3, 0.5, 1.0}` DASH) and must equal it EXACTLY. The new identity is created holding the full `denomination`, funded by decrementing the shielded pool by exactly that amount — a move *between* two balance trees (like `Unshield`'s pool→address), so the global system-credit supply is unchanged (**no** `AddToSystemCredits`); the fee is then **moved** from that balance into the fee pools, so the identity ends with `denomination − total_fee`. Unlike the flat pool-paid transitions, the `AddNewIdentity` write grows with the key count, so the cost is **metered** (not a flat carve) — only the ZK compute fee (`compute_shielded_verification_fee`) is added on top, exactly like the transparent `Shield`. The client predicts it offline with `compute_shielded_identity_create_fee(num_actions, num_keys)`; consensus rejects `denomination < total_fee` with `IdentityInsufficientBalanceError`. | | **ShieldFromIdentity** | `fee = metered(storage + processing) + shielded_verification_fee`, paid from the funding identity's balance | Identity balance to pool (protocol version 14). Charged exactly like `Shield`, but on the identity side: the identity signature covers the whole outputs-only bundle, the metered note writes and identity writes go through the standard identity-paid path (`IdentityCreditTransferToAddresses` model), and only the ZK compute fee is added as `additional_fixed_fee_cost`. `user_fee_increase` applies. The identity must hold `amount + fee`; consensus rejects a short balance with `IdentityInsufficientBalanceError`. The pool and the identity are both balance trees, so no system-credit adjustment is emitted. See [Entry-Transition Fees](#entry-transition-fees-shield-shieldfromassetlock-and-shieldfromidentity). | | **IdentityTopUpFromShieldedPool** | `fee = compute_shielded_identity_top_up_fee(num_actions)` = `compute_minimum_shielded_fee(num_actions) + identity_balance_storage_fee`, carved from `value_balance` | Shielded pool to an EXISTING identity's balance (protocol version 14). `value_balance` (the transition's `topUpAmount`) is the gross amount leaving the pool; the identity receives `topUpAmount - fee` and validation requires `topUpAmount >= fee`. Same flat pool-paid model as `Unshield`, with the identity balance write as a flat component built like `Unshield`'s address write but calibrated to its measured cost: the top-up rewrites the existing identity's balance element and its Merk path (320 replaced bytes, 175,320 credits of processing, no storage), folded into one flat figure with headroom like the other shielded components, so `identity_balance_storage_fee = 8 x per_byte_rate` (`SHIELDED_IDENTITY_TOP_UP_BALANCE_STORAGE_BYTES`). The target identity and gross amount are bound into the Orchard sighash; the identity must already exist; no system-credit adjustment. | -| **PrivateDocumentCreateFromShieldedPool** | `fee = value_balance == compute_private_document_create_from_shielded_pool_fee(num_actions, entries, entry_size)` = `compute_minimum_shielded_fee(num_actions) + entries × ((entry_size + entry_overhead_bytes) × per_byte_rate + entry_processing_fee)` | Entries appended to a private document type's store (protocol version 15), with no identity. As for `ShieldedTransfer`, the whole `value_balance` is the fee and must equal it exactly. Each entry pays its own bytes plus `entry_overhead_bytes` (32: Drive's 16-byte header and GroveDB's 1 to 3 bytes of framing, measured the same at every position and chunk power) at the storage rate, and a flat `entry_processing_fee` of 50,000,000 credits (both in `fee_version.private_document`): an entry is downloaded and trial-decrypted by every wallet of the type, so the flat part prices a flood of entries, not the work of storing one. The contract, the type name and every entry are bound into the Orchard sighash. | +| **PrivateDocumentCreateFromShieldedPool** | `fee = value_balance == compute_private_document_create_from_shielded_pool_fee(num_actions, entries, entry_size)` = `compute_minimum_shielded_fee(num_actions) + entries × ((entry_size + entry_overhead_bytes) × per_byte_rate + entry_processing_fee)` | Entries appended to a private document type's store (protocol version 15), with no identity. As for `ShieldedTransfer`, the whole `value_balance` is the fee and must equal it exactly. Each entry pays its own bytes plus `entry_overhead_bytes` (32: Drive's 16-byte header and GroveDB's 1 to 3 bytes of framing, measured the same at every position and chunk power) at the storage rate, and a flat `entry_processing_fee` of 10,000,000 credits (both in `fee_version.private_document`): an entry is downloaded and trial-decrypted by every wallet of the type, so the flat part prices a flood of entries, not the work of storing one. The contract, the type name and every entry are bound into the Orchard sighash. | ### Token shielded pool fees diff --git a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs index f6f21549a57..a2a1e2b288f 100644 --- a/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs +++ b/packages/rs-dpp/src/shielded/compute_minimum_shielded_fee/mod.rs @@ -367,7 +367,7 @@ mod tests { base + entry_count as u64 * entry ); } - assert_eq!(private_document.entry_processing_fee, 50_000_000); + assert_eq!(private_document.entry_processing_fee, 10_000_000); } #[test] diff --git a/packages/rs-platform-version/src/version/fee/private_document/v1.rs b/packages/rs-platform-version/src/version/fee/private_document/v1.rs index ea24af8b385..b95877bed2e 100644 --- a/packages/rs-platform-version/src/version/fee/private_document/v1.rs +++ b/packages/rs-platform-version/src/version/fee/private_document/v1.rs @@ -4,15 +4,15 @@ use crate::version::fee::private_document::FeePrivateDocumentVersion; /// `PrivateDocumentCreateFromShieldedPool`; every fee schedule references it, and none before /// 15 reads it. /// -/// An entry pays 50,000,000 credits (0.0005 DASH) on top of its storage, so a million entries -/// a day, which every wallet of the type downloads and scans, cost 500 DASH a day in this fee -/// alone. +/// An entry pays 10,000,000 credits (0.0001 DASH) on top of its storage, so a million entries +/// a day, which every wallet of the type downloads and scans, cost 100 DASH a day in this fee +/// alone, beside the shielded fee every transition pays. /// /// GroveDB's append into a store meters the entry, Drive's 16-byte header and 1 to 3 bytes of /// its own per entry, the same at every position (the compacting append included) for every /// chunk power; 32 bytes cover them with margin. Drive's /// `should_charge_a_fixed_fee_covering_the_metered_cost_at_every_chunk_power` holds both to it. pub const FEE_PRIVATE_DOCUMENT_VERSION1: FeePrivateDocumentVersion = FeePrivateDocumentVersion { - entry_processing_fee: 50_000_000, + entry_processing_fee: 10_000_000, entry_overhead_bytes: 32, }; diff --git a/packages/rs-platform-version/src/version/fee/v4.rs b/packages/rs-platform-version/src/version/fee/v4.rs index ea7aad0d7e3..c0f2ca26427 100644 --- a/packages/rs-platform-version/src/version/fee/v4.rs +++ b/packages/rs-platform-version/src/version/fee/v4.rs @@ -9,7 +9,7 @@ pub const FEE_VERSION4: FeeVersion = FeeVersion { // are unchanged, so the fee_version_number inherited from FEE_VERSION3 stays. vote_resolution_fund_fees: VOTE_RESOLUTION_FUND_FEES_VERSION3, // Each private document entry (new in protocol version 15) pays its storage and a flat - // 50,000,000 credits; the earlier schedules carry the same group, unread before 15, so the + // 10,000,000 credits; the earlier schedules carry the same group, unread before 15, so the // table is spelled out here where it takes effect. private_document: FEE_PRIVATE_DOCUMENT_VERSION1, ..FEE_VERSION3 diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 7c7670b6dba..255733a8cc3 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -520,7 +520,7 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// (`DuplicatePrivateDocumentEntryError`, 10428). The value balance must equal /// `compute_private_document_create_from_shielded_pool_fee`: the bundle's shielded fee /// plus, per entry, its bytes and 32 overhead bytes at the storage rate and a flat -/// 50,000,000 credits (`FEE_PRIVATE_DOCUMENT_VERSION1` in `FEE_VERSION4`). After the pool +/// 10,000,000 credits (`FEE_PRIVATE_DOCUMENT_VERSION1` in `FEE_VERSION4`). After the pool /// checks a shielded transfer makes, the transform refuses, unpaid, a contract that does not /// exist, a type it does not have (`InvalidDocumentTypeError`), a type that is not private /// (`DocumentTypeNotPrivateError`, 40151) and entries of another size than the type's @@ -575,7 +575,7 @@ pub const PLATFORM_V15: PlatformVersion = PlatformVersion { factory_versions: DPP_FACTORY_VERSIONS_V1, }, system_data_contracts: SYSTEM_DATA_CONTRACT_VERSIONS_V4, // changed: DPNS v3 checks a name's create with keywords (note 3); the decision proposals contract (note 12); the token market contract is selected (note 17); DashPay v3 states what its data triggers checked (note 24) - fee_version: FEE_VERSION4, // changed: decision proposals prefund their poll with 0.5 Dash (note 12); a private document entry pays its storage and 50,000,000 credits (note 25) + fee_version: FEE_VERSION4, // changed: decision proposals prefund their poll with 0.5 Dash (note 12); a private document entry pays its storage and 10,000,000 credits (note 25) system_limits: SYSTEM_LIMITS_V5, // changed: max_compiled_contract_size caps a contract stored compiled at 1 MiB (note 8); max_private_document_entry_size caps a private type's entries at 4096 bytes, and the private document store limits size its store (note 13); max_private_document_entries_per_transition caps a private document create at 16 entries (note 25) consensus: ConsensusVersions { tenderdash_consensus_version: 1,