From 38ec572ffb882d86989bcf90b57c064857030bd0 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 00:33:13 +0700 Subject: [PATCH 1/6] chore: bump GroveDB past the flags handover and count tree offset work Moves the pin from 12adbd35 to 45e2d84f, bringing in dashpay/grovedb#1025 and dashpay/grovedb#1033. Both are gated on GROVE_V5, which only protocol version 15 selects. The new struct fields keep today's behaviour: `offset: None` on the Query literals and `allow_replacement_accounting: false` on the BatchApplyOptions literals. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 +++++++++---------- Cargo.toml | 18 +++++------ .../query/shielded/encrypted_notes/v0/mod.rs | 1 + .../v0/mod.rs | 1 + .../v0/mod.rs | 1 + .../grove_batch_operations_costs/v0/mod.rs | 1 + .../verify_shielded_encrypted_notes/v0/mod.rs | 2 ++ 7 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c903b1a44ec..389510f9340 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" dependencies = [ "byteorder", "ed", @@ -3459,7 +3459,7 @@ dependencies = [ [[package]] name = "grovedb-storage" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" 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=12adbd357522a03efad4ae609499c93fe8cc3e2c#12adbd357522a03efad4ae609499c93fe8cc3e2c" +source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" dependencies = [ "serde", "serde_with 3.21.0", diff --git a/Cargo.toml b/Cargo.toml index 6da85829926..814ffeba878 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 = "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" } +grovedb = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c", default-features = false } +grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-merk = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c", default-features = false } +grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } 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-drive-abci/src/query/shielded/encrypted_notes/v0/mod.rs b/packages/rs-drive-abci/src/query/shielded/encrypted_notes/v0/mod.rs index 31485ba8348..ef0c9dd87aa 100644 --- a/packages/rs-drive-abci/src/query/shielded/encrypted_notes/v0/mod.rs +++ b/packages/rs-drive-abci/src/query/shielded/encrypted_notes/v0/mod.rs @@ -92,6 +92,7 @@ impl Platform { query: Query { read_mode: None, limit: None, + offset: None, items: vec![QueryItem::Key(vec![SHIELDED_NOTES_KEY])], default_subquery_branch: SubqueryBranch { subquery_path: None, 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 35b7b05e957..baf9070b52c 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 @@ -71,6 +71,7 @@ impl Drive { disable_operation_consistency_check: !self.config.batching_consistency_verification, base_root_storage_is_free: true, batch_pause_height: None, + allow_replacement_accounting: false, }), |cost, old_flags, new_flags| { StorageFlags::update_element_flags(cost, old_flags, new_flags) 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 b32807379e3..f3ad2d1f531 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 @@ -56,6 +56,7 @@ impl Drive { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + allow_replacement_accounting: false, }), |cost, old_flags, new_flags| { // if there were no flags before then the new flags are used diff --git a/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs index 42dec3e28cf..fa19668c8c2 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs @@ -29,6 +29,7 @@ impl Drive { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + allow_replacement_accounting: false, }), |_, _, _| Ok(false), |_, _, _| Err(GroveError::InternalError("not implemented".to_string())), diff --git a/packages/rs-drive/src/verify/shielded/verify_shielded_encrypted_notes/v0/mod.rs b/packages/rs-drive/src/verify/shielded/verify_shielded_encrypted_notes/v0/mod.rs index 033dcb99047..08972c37da3 100644 --- a/packages/rs-drive/src/verify/shielded/verify_shielded_encrypted_notes/v0/mod.rs +++ b/packages/rs-drive/src/verify/shielded/verify_shielded_encrypted_notes/v0/mod.rs @@ -111,6 +111,7 @@ impl Drive { query: Query { read_mode: None, limit: None, + offset: None, items: vec![QueryItem::Key(vec![SHIELDED_NOTES_KEY])], default_subquery_branch: SubqueryBranch { subquery_path: None, @@ -300,6 +301,7 @@ mod tests { query: Query { read_mode: None, limit: None, + offset: None, items: vec![QueryItem::Key(vec![SHIELDED_NOTES_KEY])], default_subquery_branch: SubqueryBranch { subquery_path: None, From d779aa2a79f495ab08e4331e6147b418e0fef570 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 00:52:55 +0700 Subject: [PATCH 2/6] fix(platform)!: hand stored bytes to the owner a write names (PV15) From protocol version 15 the owner a write names owns the element afterwards, at any size. A transfer or purchase keeps the stored bytes and moves them to the new owner, including the references of the indexes it leaves unchanged. A gas sponsor who pays is named as the owner of what the batch writes, and an owner change with a sponsoring contract owner on either side refunds the old owner in full and charges the payer in full. The flags update runs per element and sees only flags, so the batch tells it who the sponsoring contract owners are. Replaces the sponsor-held approach of #5238. Co-Authored-By: Claude Opus 5.5 --- book/src/contract-keywords/deletion.md | 4 +- .../contract-keywords/moderator-abilities.md | 4 +- book/src/contract-keywords/token-cost.md | 5 +- book/src/data-model/contract-moderation.md | 4 +- book/src/fees/overview.md | 36 +- .../execute_event/mod.rs | 12 +- .../execute_event/v1/mod.rs | 37 +- .../execute_event/v2/mod.rs | 44 + .../batch/tests/document/action_fees.rs | 17 +- .../batch/tests/document/gas_sponsorship.rs | 768 ++++++++++++++++-- .../mod.rs | 14 +- .../v1/mod.rs | 46 +- .../v2/mod.rs | 41 + .../mod.rs | 12 +- .../v1/mod.rs | 41 +- .../v2/mod.rs | 32 + ...kfill_historical_credit_pool_nullifiers.rs | 3 +- .../tests.rs | 6 +- .../src/state_transition_action/batch/mod.rs | 81 +- .../src/util/batch/drive_op_batch/document.rs | 24 + .../apply_drive_operations/v2/mod.rs | 4 +- .../src/util/batch/drive_op_batch/mod.rs | 15 + .../src/util/batch/grovedb_op_batch/mod.rs | 14 +- .../grove_apply_batch_with_add_costs/mod.rs | 10 +- .../v0/mod.rs | 42 +- .../v1/mod.rs | 153 ++++ .../grove_apply_operation/v0/mod.rs | 5 +- .../util/object_size_info/document_info.rs | 17 + .../mod.rs | 23 + .../v1/mod.rs | 6 +- .../drive_abci_method_versions/v11.rs | 6 +- .../drive_document_method_versions/v5.rs | 15 +- .../drive_grove_method_versions/mod.rs | 1 + .../drive_grove_method_versions/v2.rs | 17 + .../src/version/drive_versions/v10.rs | 5 + .../rs-platform-version/src/version/v15.rs | 12 + 36 files changed, 1458 insertions(+), 118 deletions(-) create mode 100644 packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v2/mod.rs create mode 100644 packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v2/mod.rs create mode 100644 packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v2/mod.rs create mode 100644 packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs create mode 100644 packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs diff --git a/book/src/contract-keywords/deletion.md b/book/src/contract-keywords/deletion.md index 098d338564d..ae64dd11628 100644 --- a/book/src/contract-keywords/deletion.md +++ b/book/src/contract-keywords/deletion.md @@ -35,7 +35,7 @@ A commenter may take a comment down at any time. Since `true` is the usual defau ### How it works - The owner deletes a document with a delete transition that names its id. Anyone else is refused (`DocumentOwnerIdMismatchError`, 40102), and a document that does not exist is `DocumentNotFoundError` (40101). -- The owner is refunded the part of the document's storage fee that has not yet been paid out to past epochs. A document of a type with a `ttl` refunds nothing. See [Refunds](../fees/overview.md#refunds). +- The owner is refunded the part of the document's storage fee that has not yet been paid out to past epochs. Storage the contract owner paid for as the document's [gas sponsor](token-cost.md#who-pays-the-gas-gasfeespaidby) is refunded to them instead. A document of a type with a `ttl` refunds nothing. See [Refunds](../fees/overview.md#refunds). - A delete may carry a token cost or an action fee, like any document action. See [Token Costs](token-cost.md) and [Action Fees](action-fees.md). - An identity that is banned or suspended on a moderated contract may still delete its own documents. See [Contract Moderation](../data-model/contract-moderation.md#the-model). On a type set to `false` it can retract them instead, when the type declares [`retractedWhen`](#retractedwhen). - `false` binds only the owner. The contract's moderators, when the type allows them, and the platform, when the type has a `ttl`, still delete such documents. @@ -443,7 +443,7 @@ Whether the owner of a document a moderator deletes is refunded its storage. By ### How it works -- With `true`, the owner is refunded as for their own deletion: the part of the storage fee not yet paid out to past epochs. The refund goes to the owner, not to the moderator, who still pays for the transition and the record. A document of a type with a `ttl` refunds nothing either way. +- With `true`, the storage is refunded as for the owner's own deletion: the part of the storage fee not yet paid out to past epochs, to the owner, or to the contract owner for storage they paid for as the document's [gas sponsor](token-cost.md#who-pays-the-gas-gasfeespaidby). The deleting moderator gets none of it as moderator, and still pays for the transition and the record. A document of a type with a `ttl` refunds nothing either way. - With `false`, the credits stay in the storage pools they were paid into. ### Rules at registration diff --git a/book/src/contract-keywords/moderator-abilities.md b/book/src/contract-keywords/moderator-abilities.md index 0fa1b20a808..84e87a3c8e2 100644 --- a/book/src/contract-keywords/moderator-abilities.md +++ b/book/src/contract-keywords/moderator-abilities.md @@ -17,7 +17,7 @@ The keys: | `delete` | The moderators delete documents of the type. By default each deletion leaves a removal record and can be restored within a week; with `deleteKeepsRecord: false` it leaves none and is final. | [Deletion](deletion.md#moderatorabilitiesdelete) | | `deleteWithin` | Limits `delete` to so many seconds after a document's last change. | [Deletion](deletion.md#moderatorabilitiesdeletewithin) | | `deleteKeepsRecord` | Whether a deletion leaves a removal record, and so can be restored. Default `true`. | [Deletion](deletion.md#moderatorabilitiesdeletekeepsrecord) | -| `deleteRefundsOwner` | Whether the deleted document's owner is refunded its storage. Default `false`. | [Deletion](deletion.md#moderatorabilitiesdeleterefundsowner) | +| `deleteRefundsOwner` | Whether the deleted document's storage is refunded to whoever it names: its owner, or the contract owner for storage they paid for as its gas sponsor. Default `false`. | [Deletion](deletion.md#moderatorabilitiesdeleterefundsowner) | | `deleteSettled` | Past `deleteWithin`, the seated team of an elected contract deletes a document together: one member proposes, and so many approve, the leader among them only when the rule says so (`leader: true`), and a member the leader added only for documents created after its addition unless the rule says otherwise (`approversPredateDocument: false`). | [Deletion](deletion.md#moderatorabilitiesdeletesettled) | | `deleteKeepsFields` | The fields of a deleted document whose values stay public in its removal record, such as a post's hashtag. | [Deletion](deletion.md#moderatorabilitiesdeletekeepsfields) | | `changeFields` | The listed properties are written only by the moderators. | [below](#changefields) | @@ -72,7 +72,7 @@ A user files a report and can never edit it. A report starts with no `status`: t - The document records who wrote the fields last, and when: [`$moderatedAt` and `$moderatedBy`](system-properties.md#moderatedat-and-moderatedby), which an index of the type may name to find what the moderators handled. - The document is updated in place. Every other property stays as it was, `$updatedAt` among them, so a change never opens a [`deleteWithin`](deletion.md#moderatorabilitiesdeletewithin) window again. `$revision` goes up by one, so a replace its owner built on the earlier revision is refused (`InvalidDocumentRevisionError`, 40106) rather than writing over the change. - References are not checked again: no field a moderator writes holds one or is read by one (see the rules below). A report whose post a moderator has already deleted can still be marked handled. Nor is a `propertyConstraints` rule reading a referenced document judged: registration keeps such a rule from reading anything the change can break. -- The moderator pays for the transition and the bytes it adds. Storage the change frees, or index entries it moves, are refunded to the document's owner, who paid for them. Nothing the type prices is charged: a moderator's change is no action of the owner's. +- The moderator pays for the transition and the bytes it adds. Storage the change frees, or index entries it moves, are refunded to whoever paid for them: the document's owner, or the contract owner for a document whose storage they hold as its [gas sponsor](token-cost.md#who-pays-the-gas-gasfeespaidby). A change to such a document returns it to its owner, so the contract owner is refunded for all of it and the moderator pays for all of it. Nothing the type prices is charged: a moderator's change is no action of the owner's. - A change that changes nothing, every field already holding the value named, is refused (10905): it would bump `$revision` for nothing. - A change needs a listed reason from a seated team, but does not count toward the team's action share: changes of one document have no bound, as deletions of the content that exists do, so counting them would let a member farm the share. - The proof of the change is the document as it now stands, holding the values the change set. diff --git a/book/src/contract-keywords/token-cost.md b/book/src/contract-keywords/token-cost.md index 6cb759f8567..b9ef2efdba7 100644 --- a/book/src/contract-keywords/token-cost.md +++ b/book/src/contract-keywords/token-cost.md @@ -106,7 +106,10 @@ From protocol version 14 the contract owner can pay the gas (the storage and pro - A batch has one payer. Transitions of one batch that resolve to different payers are refused (`InconsistentGasFeesPaidByInBatchError`, 40130); a token transition in the batch is never sponsored, so it counts as the signer paying. - When the contract owner's balance does not cover the gas (and any action fees they would owe), a batch that insisted is refused and charged nothing (`GasSponsorInsufficientBalanceError`, 40222), and a batch that only preferred falls back to the signer. - A transition that fails validation is never sponsored: its signer pays for the work that ran. -- Storage refunds still go to the document's owner, whoever paid for the storage. Each token the contract owner hands out is therefore worth up to the storage fee of the largest document the type allows, so a type that offers to pay should bound its documents' size (`maxLength`, `maxItems`, [maxBytes](max-bytes.md)) and price the action to match. +- Storage refunds go to whoever paid for the storage. From protocol version 15 a sponsored action records the contract owner as the owner of the storage it writes, a contested document's vote poll entries included, so deleting or shrinking the document later refunds the contract owner, not the document's owner. A document of a type with a `ttl` is stored without storage flags and refunds nobody, sponsored or not. +- The contract owner still pays the storage fee of each document a sponsored action writes, up to the largest the type allows, and gets back only the part not yet paid out to past epochs, only when the document is deleted or shrinks. A type that offers to pay should bound its documents' size (`maxLength`, `maxItems`, [maxBytes](max-bytes.md)) and price the action to match. +- When a write changes who holds stored data and the contract owner is on either side, the handover is full: the previous holder is refunded for all of it and whoever pays is charged for all of it. That covers a document owner replacing a document the contract owner holds, a sponsored action on a document its owner paid for, and a transfer or purchase of a document the contract owner holds. +- An ordinary transfer or purchase between two other identities keeps the stored bytes: they move to the new owner, index entries included, and nobody is refunded for them. - A sponsor also pays any [action fee](action-fees.md) the action charges. - A sponsor never pays the transition's `userFeeIncrease`: the signer chose it and pays the processing fee it adds, and a batch whose signer cannot is refused and charged nothing (`IdentityInsufficientBalanceError`, 40210). diff --git a/book/src/data-model/contract-moderation.md b/book/src/data-model/contract-moderation.md index 29011ca6acd..c00c6f61954 100644 --- a/book/src/data-model/contract-moderation.md +++ b/book/src/data-model/contract-moderation.md @@ -180,7 +180,7 @@ ContractUserModerationAction::RestoreDocument { It names no identity and no id: the document is inside the bytes, and its id and owner with it. The transform checks, in order and each refusal paid: the document type exists (10406) and carries `moderatorAbilities.delete` (41115, only such a type keeps records), the signer is the owner or a moderator (41101, whoever deleted), the bytes decode under the type (a basic decoding error, never an execution error: the bytes are the signer's), the document has a removal record (`ContractDocumentRemovalNotFoundError`, 41119) that is not marked restored (`ContractDocumentAlreadyRestoredError`, 41122: the document is live), block time is within `SystemLimits::contract_document_restore_window_ms` of the removal, a week, the last millisecond included (`DocumentRestoreWindowElapsedError`, 41120), the bytes hash to what the record holds (`DocumentRestoreHashMismatchError`, 41121: the document as it was, not an edit of it), and no other document of the type holds a value of one of its unique indexes (`DuplicateUniqueIndexError`, 40105: the value was free while the document was gone, and someone may have taken it), and the type's `propertyConstraints` rules that read a `countOf` or `sumOf` total still hold (`DocumentPropertyConstraintViolatedError`, 10422: other writes may have moved a total while the document was gone), judged with the totals as they will be once it is back (`read_property_constraint_aggregates_for_restore`) and `$old.` reading the restored document itself (`validate_property_constraints_for_system_change`, `SystemChange::Restore`). A rule reading a referenced document is not judged: a restore checks no reference, and registration keeps such a rule from reading any total on a type whose deletions keep records. The record read is billed, and so is each total; the hash pins everything else, so the owner, the revision, the timestamps, the references, the schema and the rules reading no total are not checked again. Whether the document's owner is banned is not asked either: a ban stops writes, it never removed documents. -The action carries the contract, the decoded document and the record marked restored by the signer at the block's time, so Drive puts the document back through the ordinary insert (`DocumentOperationType::AddDocument`, every index and aggregate of the type included) and replaces the record in place, without reading again. The restored document's storage flags name its owner, as they did before the deletion: the signer pays for the bytes, and the refund of a later deletion is the owner's, as it always was. Nothing the document type prices is charged, neither its creation token cost nor its `actionFees` creation fee, and no fee agreement is asked: a moderator undoes a moderation, it does not create content. The restore is proved by the record, now marked restored and holding the hash of the bytes the transition carried; the verifier reads the document's id out of those bytes under the contract's document type, so it needs the contract, which the SDKs register with their context provider before broadcasting. +The action carries the contract, the decoded document and the record marked restored by the signer at the block's time, so Drive puts the document back through the ordinary insert (`DocumentOperationType::AddDocument`, every index and aggregate of the type included) and replaces the record in place, without reading again. The restored document's storage flags name its owner: the signer pays for the bytes, and the refund of a later deletion is the owner's. A gas sponsor who held the deleted document's storage was settled by the deletion (forfeited, or refunded to them with `deleteRefundsOwner`), so the restore is new storage they have no claim on. Nothing the document type prices is charged, neither its creation token cost nor its `actionFees` creation fee, and no fee agreement is asked: a moderator undoes a moderation, it does not create content. The restore is proved by the record, now marked restored and holding the hash of the bytes the transition carried; the verifier reads the document's id out of those bytes under the contract's document type, so it needs the contract, which the SDKs register with their context provider before broadcasting. Two consequences follow from the document coming back byte for byte. Its `$updatedAt` does not move, so a type with `moderatorAbilities.deleteWithin` may have settled it while it was gone: no moderator deletes it again alone until its author edits it, though the author still can, and on a type that sets `deleteSettled` the seated team can, by a fresh proposal (the action that deleted it ran and is closed). A deletion the team approved together is never restored: it is the only removal made after the document settled, since a moderator alone deletes up to the settling time and the team only after it, so the restore transform compares the removal time with the settling time of the document the bytes hash to (no read), and refuses the restore of a later removal, paid (`SettledDeletionNotRestorableError`, 41209), so no single moderator undoes what the leader and the members agreed on. And a type with a contested index can not carry `moderatorAbilities.delete` at all (`InvalidContractStructure`, 10231): a contested index only takes a document through a vote, which no restore can go through, so such a deletion could never be undone. The bytes are decoded under the type as the contract holds it when the restore is processed, and the hash is of the document serialized under the type as it was when the document was deleted: a contract update that changes the type's layout inside the window (a property added, say) leaves the record unrestorable, since bytes that decode under the new layout cannot hash to what the record holds. A client therefore serializes under the contract's current version, as Drive does, and keeps the document rather than the bytes. @@ -260,7 +260,7 @@ ContractUserModerationAction::ChangeDocumentFields { Basic structure refuses one that names no field or a system property (`InvalidContractModerationDocumentFieldsError`, 10905, unpaid). The transform (`transform_document_fields_change_v0`) checks, in order and each refusal paid: the document type exists (10406), every field is one it keeps for its moderators (`DocumentFieldNotChangeableByModeratorsError`, 41123), the signer moderates the contract (41101) and a seated team holds `changeDocumentFields` on the type (41201) and names a listed reason (41203), the document exists (40101) and has not expired (40140). It then builds the changed document: the stored one with each field set or removed, compared as a replace compares (`Value::equal_underlying_data`, so an integer sent at another width than the stored one is no change), `$revision` one higher (`OverflowError`, 10700, at `u64::MAX`), `$moderatedAt` the block's time and `$moderatedBy` the signer, everything else as stored, `$updatedAt` and its heights included. That document is judged as a replace judges one: `validate_document_properties` (the schema and `propertyConstraints`, with the `countOf` and `sumOf` totals read as they will be once it is stored, `read_property_constraint_aggregates_for_moderator_change`), `distinctFrom` and the shapes of `encryptedFor` properties, then, when a changed field is in a unique index, `Drive::validate_moderated_document_uniqueness`, the restore's check generalized to a changed document (only the changed fields are checked, and the document's own entries do not clash). Of the `propertyConstraints` rules, only those reading a field the change moves (directly or through `$old.`, which reads the stored document), `$revision` or a total are judged (`DocumentSystemValues::changed_fields`, `PropertyConstraint::reads_changed_fields`): every other rule reads only what the owner last wrote, and gave its verdict then. A rule reading a referenced document is not judged either: the change checks no reference, and registration keeps such a rule from reading anything the change can break. Whoever owns the document is no protection: the fields are the moderators'. Nothing the document type prices is charged. -The action carries the contract and the changed document, and Drive stores it with `DocumentOperationType::UpdateDocument`, the replace's update, every index and aggregate of the type included. The document's storage flags name its owner, as a replace's do: the moderator pays for the bytes the change adds, and what the change frees (a smaller value, an index entry that moved) is refunded to the owner, who paid for it. A seated team's member does not have the change counted toward the team's action share: the changes of one document have no bound, and counting them would let a member farm it. A change that changes nothing is refused (10905) rather than written. `$updatedAt` does not move, so a change never opens a `moderatorAbilities.deleteWithin` window again: a moderator can not keep a post deletable by touching it. +The action carries the contract and the changed document, and Drive stores it with `DocumentOperationType::UpdateDocument`, the replace's update, every index and aggregate of the type included. The document's storage flags name its owner, as a replace's do: the moderator pays for the bytes the change adds, and what the change frees (a smaller value, an index entry that moved) is refunded to whoever paid for it, the owner, or the contract owner for a document whose storage they hold as its gas sponsor (the document then returns to its owner: the contract owner is refunded for all of it and the moderator pays for all of it). A seated team's member does not have the change counted toward the team's action share: the changes of one document have no bound, and counting them would let a member farm it. A change that changes nothing is refused (10905) rather than written. `$updatedAt` does not move, so a change never opens a `moderatorAbilities.deleteWithin` window again: a moderator can not keep a post deletable by touching it. The stamp is two system properties of document serialization format 3 (bits 9 and 10 of its time field flags): absent on a document no moderator has written, set only here and by the batch transformer, which stamps a moderator's create or replace of such a field (an action it did not judge is written unstamped), carried over by a replace that leaves the fields alone and by every transfer, purchase and price update, and put back by a restore with the rest of the bytes. A type that keeps fields for its moderators may index either, never in a unique index (`apply_moderator_abilities`, 10231; the core index check admits the two names from parser generation 3, `ParserGeneration::admit_moderation_stamp_indexes`). See [System Properties](../contract-keywords/system-properties.md#moderatedat-and-moderatedby). diff --git a/book/src/fees/overview.md b/book/src/fees/overview.md index 30afc2e6ef3..4cad5419b21 100644 --- a/book/src/fees/overview.md +++ b/book/src/fees/overview.md @@ -237,13 +237,35 @@ budget) has to cover, or the batch is refused unpaid (`IdentityInsufficientBalanceError`, 40210). A batch that fails validation is never sponsored: its signer pays for the work that ran, and a request the document type does not offer is a paid rejection (`GasFeesPaidByNotAllowedError`, -40129). Storage refunds still go to the document's owner, whoever paid the -storage: a sponsored document refunds its owner when it is deleted or replaced -by a smaller one, even when the sponsor pays for that transition too. Each -token the sponsor hands out is therefore worth up to the storage fee of the -largest document the type allows, so a document type that offers sponsorship -should bound its documents' size (`maxLength`, `maxItems`) and price the -action accordingly. +40129). + +Storage refunds go to whoever paid for the storage. From protocol version 15, +execution names the sponsor it charges as the owner in the storage flags of +everything the batch's documents write (`record_gas_sponsor_as_storage_owner`; +the owner's 32 bytes are there either way and the estimate reads no owner, so +no fee changes), and a contest's end date entries name whoever the document's +flags name. A sponsored document therefore refunds the sponsor when it is +deleted or shrinks. A document of a type with a `ttl` is stored without storage +flags and refunds nobody, sponsored or not. The sponsor still pays the storage +fee of every document it sponsors, up to the largest the type allows, and gets +back only the part not yet paid out to past epochs, so a document type that +offers sponsorship should bound its documents' size (`maxLength`, `maxItems`, +`maxBytes`) and price the action accordingly. + +From protocol version 15 the owner a write names owns the element afterwards, +at any size. What happens to the bytes depends on who is involved: + +- When the old or new owner is the owner of a contract whose document type the + batch updates offers sponsorship, the handover is full: the old owner is + refunded for the whole element and the payer charged for all of it. Otherwise + a document's owner could keep storage the sponsor paid for, by growing the + document or by transferring it to a second identity and deleting it there. + This does not change the estimate: a document update is already estimated as + the insertion of everything it writes. +- Any other owner change, an ordinary transfer or purchase, keeps the stored + bytes and moves them to the new owner, the references of the indexes it + leaves unchanged included. Nobody is refunded for them, and the new owner is + the one refunded when they are freed later. The signer's minimum balance pre-check runs before the contracts are loaded; its v1 asks a batch that requests sponsorship for its principal only diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs index bbb548414bb..9e3d4034c06 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::error::execution::ExecutionError; use crate::error::Error; @@ -80,9 +81,18 @@ where block_credit_mints, platform_version, ), + 2 => self.execute_event_v2( + event, + consensus_errors, + block_info, + transaction, + address_balances_in_update, + block_credit_mints, + platform_version, + ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "execute_event".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v1/mod.rs index ba324f73dc8..c27f0661fd4 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v1/mod.rs @@ -20,7 +20,9 @@ use dpp::fee::Credits; use dpp::version::PlatformVersion; use drive::drive::identity::update::apply_balance_change_outcome::ApplyBalanceChangeOutcomeV0Methods; use drive::grovedb::Transaction; -use drive::state_transition_action::batch::{action_fee_operations, action_fees_total}; +use drive::state_transition_action::batch::{ + action_fee_operations, action_fees_total, record_gas_sponsor_as_storage_owner, +}; use drive::util::batch::DriveOperation; use std::collections::BTreeMap; @@ -28,6 +30,31 @@ impl Platform where C: CoreRPCLike, { + /// Generation 1: a sponsor's batch stores what it writes under the owner the documents name. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn execute_event_v1( + &self, + event: ExecutionEvent, + consensus_errors: Vec, + block_info: &BlockInfo, + transaction: &Transaction, + address_balances_in_update: Option<&mut BTreeMap>, + block_credit_mints: &mut Credits, + platform_version: &PlatformVersion, + ) -> Result { + self.execute_event_v1_body( + event, + consensus_errors, + block_info, + transaction, + address_balances_in_update, + block_credit_mints, + false, + platform_version, + ) + } + /// v1 executes a `Paid` event whose signing key carries a budget, whose batch names a gas /// sponsor, or whose batch owes document action fees, all recorded on the event from protocol /// version 14. Every other event, including one signed by a key that only expires, is @@ -56,7 +83,7 @@ where /// like the balance change, and never changes what is stored. #[inline(always)] #[allow(clippy::too_many_arguments)] - pub(super) fn execute_event_v1( + pub(super) fn execute_event_v1_body( &self, mut event: ExecutionEvent, mut consensus_errors: Vec, @@ -64,6 +91,7 @@ where transaction: &Transaction, address_balances_in_update: Option<&mut BTreeMap>, block_credit_mints: &mut Credits, + sponsor_owns_what_it_writes: bool, platform_version: &PlatformVersion, ) -> Result { // The sponsor pays for a valid batch only; fee validation below must judge the same @@ -150,6 +178,11 @@ where // credits that were not there. let mut operations = operations; operations.extend(action_fee_operations(payer_id, &action_fees)?); + if sponsor_owns_what_it_writes { + if let Some(gas_sponsor) = paying_sponsor { + record_gas_sponsor_as_storage_owner(&mut operations, gas_sponsor.identity_id); + } + } let action_fees_owed_by_identity = if paying_sponsor.is_some() { 0 } else { diff --git a/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v2/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v2/mod.rs new file mode 100644 index 00000000000..43d33c8588c --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/state_transition_processing/execute_event/v2/mod.rs @@ -0,0 +1,44 @@ +use crate::error::Error; +use crate::execution::types::execution_event::ExecutionEvent; +use crate::platform_types::event_execution_result::EventExecutionResult; +use crate::platform_types::platform::Platform; +use crate::rpc::core::CoreRPCLike; +use dpp::address_funds::PlatformAddress; +use dpp::balances::credits::CreditOperation; +use dpp::block::block_info::BlockInfo; +use dpp::consensus::ConsensusError; +use dpp::fee::Credits; +use dpp::version::PlatformVersion; +use drive::grovedb::Transaction; +use std::collections::BTreeMap; + +impl Platform +where + C: CoreRPCLike, +{ + /// Generation 1, except that a gas sponsor who pays is named as the owner of the storage + /// the batch's documents write, so the refunds of that storage go back to them. + #[inline(always)] + #[allow(clippy::too_many_arguments)] + pub(super) fn execute_event_v2( + &self, + event: ExecutionEvent, + consensus_errors: Vec, + block_info: &BlockInfo, + transaction: &Transaction, + address_balances_in_update: Option<&mut BTreeMap>, + block_credit_mints: &mut Credits, + platform_version: &PlatformVersion, + ) -> Result { + self.execute_event_v1_body( + event, + consensus_errors, + block_info, + transaction, + address_balances_in_update, + block_credit_mints, + true, + platform_version, + ) + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/action_fees.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/action_fees.rs index dcf0c7408ab..42d2456db48 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/action_fees.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/action_fees.rs @@ -6,8 +6,8 @@ //! sum of all credits changes by the gas alone. use super::gas_sponsorship::gas_sponsorship_tests::{ - balance_of, process_alone, total_fee, Sponsorship, GAS_SPONSOR_INSUFFICIENT_BALANCE, - IDENTITY_INSUFFICIENT_BALANCE, + balance_of, credits_in_trees_of, process_alone, total_fee, Sponsorship, + GAS_SPONSOR_INSUFFICIENT_BALANCE, IDENTITY_INSUFFICIENT_BALANCE, }; use super::*; @@ -1238,19 +1238,6 @@ mod action_fee_tests { (pot(ContractFeePot::Owner), pot(ContractFeePot::Moderators)) } - fn credits_in_trees_of( - platform: &TempPlatform, - tx: &Transaction, - platform_version: &PlatformVersion, - ) -> Credits { - platform - .drive - .calculate_total_credits_balance(Some(tx), &platform_version.drive) - .expect("expected to sum the credits") - .total_in_trees() - .expect("expected the credits to add up") - } - /// The agreement to what `document_type` declares for `action`, at the fee multiplier of /// the fee schedule fn agreeing_to( diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs index e17c4d3b877..db7b65340e8 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs @@ -32,12 +32,13 @@ pub(crate) mod gas_sponsorship_tests { }; use dpp::data_contract::DataContract; use dpp::data_contract::schema::DataContractSchemaMethodsV0; + use dpp::data_contract::document_type::methods::DocumentTypeV0Methods; use dpp::document::Document; use dpp::identity::accessors::IdentitySettersV0; use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dpp::identity::{Identity, IdentityPublicKey}; - use dpp::prelude::{Identifier, Revision, UserFeeIncrease}; use dpp::platform_value::platform_value; + use dpp::prelude::{Identifier, IdentityNonce, Revision, UserFeeIncrease}; use dpp::state_transition::batch_transition::batched_transition::document_transition_action_type::DocumentTransitionActionType; use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; use dpp::state_transition::StateTransition; @@ -48,6 +49,10 @@ pub(crate) mod gas_sponsorship_tests { }; use dpp::tokens::token_payment_info::v0::TokenPaymentInfoV0; use dpp::tokens::token_payment_info::TokenPaymentInfo; + use dpp::document::DocumentV0Setters; + use drive::drive::RootTree; + use drive::query::DriveDocumentQuery; + use drive::util::storage_flags::StorageFlags; use drive::util::test_helpers::setup_contract; use simple_signer::signer::SimpleSigner; @@ -292,22 +297,7 @@ pub(crate) mod gas_sponsorship_tests { /// The user's deletion of the card they created, without any token payment info async fn card_deletion_without_token_payment(&self) -> StateTransition { - let (document, _) = self.card_of(&self.user); - BatchTransition::new_document_deletion_transition_from_document( - document, - self.contract - .document_type_for_name("card") - .expect("expected the card document type"), - &self.user_key, - 3, - 0, - None, - &self.user_signer, - self.platform_version, - None, - ) - .await - .expect("expected a batch transition") + self.card_deletion_paying(3, None).await } /// The contract owner's own card creation, asking `requested` for the gas @@ -324,28 +314,61 @@ pub(crate) mod gas_sponsorship_tests { .await } + /// A payment of at most `maximum` of the game's second token, which pays for replacing, + /// deleting and transferring a card, asking `requested` for the gas + fn game_token_payment(maximum: TokenAmount, requested: GasFeesPaidBy) -> TokenPaymentInfo { + TokenPaymentInfo::V0(TokenPaymentInfoV0 { + payment_token_contract_id: None, + token_contract_position: 1, + minimum_token_cost: None, + maximum_token_cost: Some(maximum), + gas_fees_paid_by: requested, + }) + } + /// The user's replacement of the card they created, at `revision`, preferring that the /// contract owner pays the gas async fn card_replacement(&self, revision: Revision) -> StateTransition { let (mut document, _) = self.card_of(&self.user); document.set("attack", 5.into()); document.set_revision(Some(revision)); + self.card_replacement_of(document, 3, GasFeesPaidBy::PreferContractOwner) + .await + } + + /// The user's replacement of the card they created, at `revision` and identity contract + /// `nonce`, giving it a description of `description_length` characters and asking + /// `requested` for the gas + async fn card_replacement_describing( + &self, + revision: Revision, + nonce: IdentityNonce, + description_length: usize, + requested: GasFeesPaidBy, + ) -> StateTransition { + let (mut document, _) = self.card_of(&self.user); + document.set("description", "d".repeat(description_length).into()); + document.set_revision(Some(revision)); + self.card_replacement_of(document, nonce, requested).await + } + /// The user's replacement of their card by `document`, at identity contract `nonce`, + /// paying its token and asking `requested` for the gas + async fn card_replacement_of( + &self, + document: Document, + nonce: IdentityNonce, + requested: GasFeesPaidBy, + ) -> StateTransition { BatchTransition::new_document_replacement_transition_from_document( document, self.contract .document_type_for_name("card") .expect("expected the card document type"), &self.user_key, - 3, + nonce, 0, - Some(TokenPaymentInfo::V0(TokenPaymentInfoV0 { - payment_token_contract_id: None, - token_contract_position: 1, - minimum_token_cost: None, - maximum_token_cost: Some(2), - gas_fees_paid_by: GasFeesPaidBy::PreferContractOwner, - })), + Some(Self::game_token_payment(2, requested)), &self.user_signer, self.platform_version, None, @@ -354,6 +377,160 @@ pub(crate) mod gas_sponsorship_tests { .expect("expected a batch transition") } + /// The user's deletion of the card they created, at identity contract `nonce`, paying + /// its token and the gas themselves: the card game offers no sponsorship of a deletion + async fn card_deletion(&self, nonce: IdentityNonce) -> StateTransition { + self.card_deletion_paying( + nonce, + Some(Self::game_token_payment(1, GasFeesPaidBy::DocumentOwner)), + ) + .await + } + + /// The user's deletion of the card they created, at identity contract `nonce`, with + /// `token_payment_info` + async fn card_deletion_paying( + &self, + nonce: IdentityNonce, + token_payment_info: Option, + ) -> StateTransition { + let (document, _) = self.card_of(&self.user); + BatchTransition::new_document_deletion_transition_from_document( + document, + self.contract + .document_type_for_name("card") + .expect("expected the card document type"), + &self.user_key, + nonce, + 0, + token_payment_info, + &self.user_signer, + self.platform_version, + None, + ) + .await + .expect("expected a batch transition") + } + + /// Gives the user `amount` of the game's second token, which pays for replacing and + /// deleting a card + fn give_the_user_game_tokens(&self, amount: TokenAmount) { + let token_id: Identifier = calculate_token_id(self.contract.id().as_bytes(), 1).into(); + add_tokens_to_identity(&self.platform, token_id, self.user.id(), amount); + } + + /// Processes `transition` in a block of its own and commits it + fn process_and_commit( + &self, + transition: &StateTransition, + ) -> StateTransitionExecutionResult { + self.process_in_and_commit(transition, &BlockInfo::default()) + } + + /// `process_and_commit` in the block `block_info` describes + fn process_in_and_commit( + &self, + transition: &StateTransition, + block_info: &BlockInfo, + ) -> StateTransitionExecutionResult { + let tx = self.platform.drive.grove.start_transaction(); + let result = self.process_in(transition, block_info, &tx); + self.platform + .drive + .grove + .commit_transaction(tx) + .unwrap() + .expect("expected to commit the transition"); + result + } + + /// The identity the committed storage flags of the user's card name as its storage owner + fn stored_card_storage_owner(&self) -> Option { + let (card, _) = self.card_of(&self.user); + let query = DriveDocumentQuery::new_primary_key_single_item_query( + &self.contract, + self.contract + .document_type_for_name("card") + .expect("expected the card document type"), + card.id(), + ); + let mut documents = self + .platform + .drive + .query_documents_with_flags( + query, + None, + false, + None, + Some(self.platform_version.protocol_version), + ) + .expect("expected to query the card") + .documents_owned(); + let (_, storage_flags) = documents.pop().expect("expected the card to be stored"); + storage_flags + .and_then(|storage_flags| storage_flags.owner_id().copied()) + .map(Identifier::from) + } + + /// The owner the card's entry in its `attack` index names + fn stored_attack_index_reference_owner(&self) -> Option { + let (card, _) = self.card_of(&self.user); + let card_type = self + .contract + .document_type_for_name("card") + .expect("expected the card document type"); + let attack = card_type + .serialize_value_for_key( + "attack", + card.get("attack").expect("expected an attack"), + self.platform_version, + ) + .expect("expected the attack to serialize"); + let path: Vec> = vec![ + vec![RootTree::DataContractDocuments as u8], + self.contract.id().to_vec(), + vec![1], + b"card".to_vec(), + b"attack".to_vec(), + attack, + vec![0], + ]; + let element = self + .platform + .drive + .grove + .get( + path.iter() + .map(Vec::as_slice) + .collect::>() + .as_slice(), + card.id().as_bytes(), + None, + &self.platform_version.drive.grove_version, + ) + .unwrap() + .expect("expected the card's index entry"); + StorageFlags::from_element_flags_ref(element.get_flags().as_ref()?) + .expect("expected flags that decode") + .and_then(|flags| flags.owner_id().copied()) + .map(Identifier::from) + } + + /// The committed credits held in the trees + fn committed_credits_in_trees(&self) -> Credits { + let tx = self.platform.drive.grove.start_transaction(); + credits_in_trees_of(&self.platform, &tx, self.platform_version) + } + + /// The committed balances of the user and of the contract owner + fn committed_credits(&self) -> (Credits, Credits) { + let tx = self.platform.drive.grove.start_transaction(); + ( + self.credits(&self.user, &tx), + self.credits(&self.contract_owner, &tx), + ) + } + /// The card `creator` creates, always the same one, with the entropy of its id pub(crate) fn card_of(&self, creator: &Identity) -> (Document, Bytes32) { let mut rng = StdRng::seed_from_u64(433); @@ -622,6 +799,20 @@ pub(crate) mod gas_sponsorship_tests { .expect("expected a balance") } + /// The credits held in the trees in `tx` + pub(crate) fn credits_in_trees_of( + platform: &TempPlatform, + tx: &drive::grovedb::Transaction, + platform_version: &PlatformVersion, + ) -> Credits { + platform + .drive + .calculate_total_credits_balance(Some(tx), &platform_version.drive) + .expect("expected to sum the credits") + .total_in_trees() + .expect("expected the credits to add up") + } + pub(crate) fn total_fee(result: &StateTransitionExecutionResult) -> Credits { match result { SuccessfulExecution { fee_result, .. } => fee_result.total_base_fee(), @@ -966,19 +1157,13 @@ pub(crate) mod gas_sponsorship_tests { // and on a recheck, while its validation mode stays what it is. let setup = Sponsorship::new(GasFeesPaidBy::ContractOwner, dash_to_credits!(0.1), 0, 15); // Replacing a card costs 2 of the game's second token - let gas_token_id: Identifier = calculate_token_id(setup.contract.id().as_bytes(), 1).into(); - add_tokens_to_identity(&setup.platform, gas_token_id, setup.user.id(), 5); + setup.give_the_user_game_tokens(5); let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; - let tx = setup.platform.drive.grove.start_transaction(); - assert_matches!(setup.process(&creation, &tx), SuccessfulExecution { .. }); - setup - .platform - .drive - .grove - .commit_transaction(tx) - .unwrap() - .expect("expected to commit the creation"); + assert_matches!( + setup.process_and_commit(&creation), + SuccessfulExecution { .. } + ); for (revision, expected) in [(2, Vec::::new()), (3, vec![INVALID_DOCUMENT_REVISION])] { let replacement = setup.card_replacement(revision).await; @@ -1288,6 +1473,491 @@ pub(crate) mod gas_sponsorship_tests { } } + // ---------- Storage refunds ---------- + + /// What processing `transition` in the block `block_info` describes and committing it + /// refunded the user and the contract owner. Checks that whoever paid was charged the fee + /// less their own refund, that the other was credited theirs, and that only the fee less + /// the refunds left the credits held in the trees. + fn settle( + setup: &Sponsorship, + transition: &StateTransition, + block_info: &BlockInfo, + paid_by_the_contract_owner: bool, + ) -> (Option, Option) { + let (user_credits, contract_owner_credits) = setup.committed_credits(); + let credits_in_trees = setup.committed_credits_in_trees(); + + let result = setup.process_in_and_commit(transition, block_info); + + let SuccessfulExecution { fee_result, .. } = &result else { + panic!("expected the transition to execute, got {result:?}"); + }; + let fee = fee_result.total_base_fee(); + let refunded_to_user = fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.user.id()); + let refunded_to_contract_owner = fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.contract_owner.id()); + let (user_fee, contract_owner_fee) = if paid_by_the_contract_owner { + (0, fee) + } else { + (fee, 0) + }; + assert_eq!( + setup.committed_credits(), + ( + user_credits - user_fee + refunded_to_user.unwrap_or_default(), + contract_owner_credits - contract_owner_fee + + refunded_to_contract_owner.unwrap_or_default(), + ), + "the payer is charged the fee, and each is credited their refund" + ); + assert_eq!( + setup.committed_credits_in_trees(), + credits_in_trees - fee + + refunded_to_user.unwrap_or_default() + + refunded_to_contract_owner.unwrap_or_default(), + "only the fee less the refunds leaves the trees" + ); + (refunded_to_user, refunded_to_contract_owner) + } + + #[tokio::test] + async fn should_refund_a_sponsored_cards_storage_to_the_contract_owner() { + // The contract owner paid for the card's storage, so its refund is theirs: a user who + // deletes the card pays for the deletion and gets nothing back. + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + let deletion = setup.card_deletion(3).await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &deletion, &block, false); + + assert_eq!(refunded_to_user, None, "the user is refunded nothing"); + assert!( + refunded_to_contract_owner.is_some_and(|refund| refund > 0), + "the contract owner is refunded the storage they paid for" + ); + } + + #[tokio::test] + async fn should_refund_the_contract_owner_in_full_when_the_user_replaces_a_sponsored_card() { + // Writing the card at the user's own expense names the user, so the handover is full: + // the contract owner gets back what they paid for and the user pays for the whole card. + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + let replacement = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &replacement, &block, false); + assert_eq!(refunded_to_user, None); + assert!(refunded_to_contract_owner.is_some_and(|refund| refund > 0)); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + + let deletion = setup.card_deletion(4).await; + let (refunded_to_user, _) = settle(&setup, &deletion, &block, false); + assert!( + refunded_to_user.is_some_and(|refund| refund > 0), + "the user is refunded the card they paid for" + ); + } + + #[tokio::test] + async fn should_refund_the_user_more_than_the_contract_owner_after_replacing_a_sponsored_card() + { + // After a full handover the card and its index entries are the user's. All that stays + // the contract owner's are the index trees their sponsored creation made. + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + let (mut document, _) = setup.card_of(&setup.user); + document.set("attack", 5.into()); + document.set_revision(Some(2)); + let replacement = setup + .card_replacement_of(document, 3, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &replacement, &block, false); + assert_eq!(refunded_to_user, None); + assert!(refunded_to_contract_owner.is_some_and(|refund| refund > 0)); + + let deletion = setup.card_deletion(4).await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &deletion, &block, false); + assert!( + refunded_to_user.unwrap_or_default() > refunded_to_contract_owner.unwrap_or_default(), + "the user holds the card, got {refunded_to_user:?} and {refunded_to_contract_owner:?}" + ); + } + + #[tokio::test] + async fn should_refund_the_contract_owner_when_a_sponsored_card_is_transferred() { + // The contract owner paid for this card, so a transfer is a full handover and not the + // keep-the-bytes move of an ordinary transfer: otherwise the user could transfer it to + // a second identity, delete it there and keep the contract owner's refund. + let mut setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + let (recipient, _, _) = setup_identity(&mut setup.platform, 571, dash_to_credits!(0.1)); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + assert_eq!( + setup.stored_card_storage_owner(), + Some(setup.contract_owner.id()) + ); + + let (mut card, _) = setup.card_of(&setup.user); + card.increment_revision() + .expect("expected the revision to increment"); + let transfer = BatchTransition::new_document_transfer_transition_from_document( + card, + setup + .contract + .document_type_for_name("card") + .expect("expected the card document type"), + recipient.id(), + &setup.user_key, + 3, + 0, + Some(Sponsorship::game_token_payment( + 1, + GasFeesPaidBy::DocumentOwner, + )), + &setup.user_signer, + setup.platform_version, + None, + ) + .await + .expect("expected a batch transition"); + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &transfer, &block, false); + + assert_eq!(refunded_to_user, None, "the user is refunded nothing"); + assert!( + refunded_to_contract_owner.is_some_and(|refund| refund > 0), + "the contract owner gets back what they paid for" + ); + assert_eq!(setup.stored_card_storage_owner(), Some(recipient.id())); + } + + /// The card game with a card type that keeps history, its creation and replacement + /// offering the contract owner's gas + fn history_card_game() -> Sponsorship { + let setup = Sponsorship::build_customized( + PlatformVersion::latest(), + GasFeesPaidBy::ContractOwner, + false, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + None, + |contract| { + let mut schema = contract + .document_type_for_name("card") + .expect("expected the card document type") + .schema() + .clone(); + schema + .set_value("documentsKeepHistory", true.into()) + .expect("expected to keep history"); + schema + .set_value("canBeDeleted", false.into()) + .expect("expected to refuse deletion"); + contract + .set_document_schema( + "card", + schema, + false, + &mut vec![], + PlatformVersion::latest(), + ) + .expect("expected the card type to keep history"); + }, + ); + assert!(setup + .contract + .document_type_for_name("card") + .expect("expected the card document type") + .documents_keep_history()); + setup + } + + #[tokio::test] + async fn should_hand_a_sponsored_history_version_to_the_user_who_rewrites_it_in_its_block() { + // A type keeping history stores each version under the block's time, so a second + // update in the same block rewrites the version the first wrote. The user's growth + // refunds the contract owner, and the version is the user's from then on. + let setup = history_card_game(); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + let growth = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = settle(&setup, &growth, &block, false); + assert_eq!(refunded_to_user, None); + assert!(refunded_to_contract_owner.is_some_and(|refund| refund > 0)); + + let shrink = setup + .card_replacement_describing(3, 4, 10, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = settle(&setup, &shrink, &block, false); + assert!( + refunded_to_user.is_some_and(|refund| refund > 0), + "the user is refunded what the shrink frees of the version they now hold" + ); + assert_eq!(refunded_to_contract_owner, None); + } + + #[tokio::test] + async fn should_estimate_a_handed_over_history_rewrite_at_no_less_than_it_costs() { + // The estimate reads nothing stored, so it prices the update as a whole insertion, + // which covers a full handover. + let setup = history_card_game(); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + let growth = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::DocumentOwner) + .await; + let result = setup.process_and_commit(&growth); + + let SuccessfulExecution { + estimated_fees: Some(estimated_fees), + fee_result, + .. + } = &result + else { + panic!("expected an execution with an estimated fee, got {result:?}"); + }; + assert!( + estimated_fees.processing_fee >= fee_result.processing_fee, + "estimated {} below the {} the rewrite cost", + estimated_fees.processing_fee, + fee_result.processing_fee + ); + assert!(estimated_fees.total_base_fee() >= fee_result.total_base_fee()); + } + + #[tokio::test] + async fn should_leave_a_history_version_the_user_paid_for_with_the_user_in_a_later_block() { + // A version written in a later block than the sponsored one is a new element, paid for + // by the user: it names the user, so the user's shrink of it in that block refunds the + // user. The contract owner's version stays stored, and theirs. + let setup = history_card_game(); + setup.give_the_user_game_tokens(5); + let creation_block = BlockInfo::default(); + let later_block = BlockInfo { + time_ms: 1_000, + height: 1, + ..Default::default() + }; + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &creation_block, true); + + let growth = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, _) = settle(&setup, &growth, &later_block, false); + assert_eq!(refunded_to_user, None); + + let shrink = setup + .card_replacement_describing(3, 4, 10, GasFeesPaidBy::DocumentOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &shrink, &later_block, false); + + assert!( + refunded_to_user.is_some_and(|refund| refund > 0), + "the user is refunded what the shrink frees of the version they paid for" + ); + assert_eq!(refunded_to_contract_owner, None); + } + + #[tokio::test] + async fn should_refund_the_user_in_full_when_the_contract_owner_sponsors_a_replacement() { + // The sponsor pays for the whole card and holds it afterwards, and the user gets back + // what they had paid for. + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &block, false); + + let replacement = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::ContractOwner) + .await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &replacement, &block, true); + assert!(refunded_to_user.is_some_and(|refund| refund > 0)); + assert_eq!(refunded_to_contract_owner, None); + assert_eq!( + setup.stored_card_storage_owner(), + Some(setup.contract_owner.id()) + ); + + let deletion = setup.card_deletion(4).await; + let (_, refunded_to_contract_owner) = settle(&setup, &deletion, &block, false); + assert!( + refunded_to_contract_owner.is_some_and(|refund| refund > 0), + "the contract owner is refunded the card" + ); + } + + /// The storage fee estimated for the user's own growing replacement of a card they paid + /// for, on a game whose card type offers `offered` + async fn estimated_storage_fee_of_a_replacement(offered: GasFeesPaidBy) -> Credits { + let setup = Sponsorship::new(offered, dash_to_credits!(0.1), dash_to_credits!(0.1), 15); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &block, false); + + let replacement = setup + .card_replacement_describing(2, 3, 200, GasFeesPaidBy::DocumentOwner) + .await; + let result = setup.process_and_commit(&replacement); + let SuccessfulExecution { + estimated_fees: Some(estimated_fees), + .. + } = &result + else { + panic!("expected an execution with an estimated fee, got {result:?}"); + }; + estimated_fees.storage_fee + } + + #[tokio::test] + async fn should_estimate_a_possible_full_handover_as_any_other_update() { + // A document update is estimated as the insertion of everything it writes, which covers + // a full handover, so offering sponsorship must not change the estimate. + let offering = estimated_storage_fee_of_a_replacement(GasFeesPaidBy::ContractOwner).await; + let not_offering = + estimated_storage_fee_of_a_replacement(GasFeesPaidBy::DocumentOwner).await; + assert_eq!(offering, not_offering); + } + + #[tokio::test] + async fn should_move_a_transferred_cards_stored_bytes_to_the_recipient() { + // The user paid for this card, so nobody involved is a sponsor: the recipient takes the + // stored bytes as they are, with nobody refunded, and the index entries the transfer + // leaves unchanged move with them. + let mut setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + let (recipient, _, _) = setup_identity(&mut setup.platform, 572, dash_to_credits!(0.1)); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &block, false); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + + let (mut card, _) = setup.card_of(&setup.user); + card.increment_revision() + .expect("expected the revision to increment"); + let transfer = BatchTransition::new_document_transfer_transition_from_document( + card, + setup + .contract + .document_type_for_name("card") + .expect("expected the card document type"), + recipient.id(), + &setup.user_key, + 3, + 0, + Some(Sponsorship::game_token_payment( + 1, + GasFeesPaidBy::DocumentOwner, + )), + &setup.user_signer, + setup.platform_version, + None, + ) + .await + .expect("expected a batch transition"); + let (_, refunded_to_contract_owner) = settle(&setup, &transfer, &block, false); + + assert_eq!(refunded_to_contract_owner, None); + assert_eq!(setup.stored_card_storage_owner(), Some(recipient.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(recipient.id()), + "the index entries the transfer leaves unchanged move too" + ); + } + + #[tokio::test] + async fn should_refund_the_user_who_paid_when_the_contract_owner_could_not() { + // A preference the contract owner cannot honour falls back to the user, who then paid + // for the storage and is refunded it. + let setup = Sponsorship::new(GasFeesPaidBy::ContractOwner, 0, dash_to_credits!(0.1), 15); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup + .card_creation(GasFeesPaidBy::PreferContractOwner) + .await; + settle(&setup, &creation, &block, false); + + let deletion = setup.card_deletion(3).await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &deletion, &block, false); + + assert!( + refunded_to_user.is_some_and(|refund| refund > 0), + "the user is refunded the storage they paid for" + ); + assert_eq!(refunded_to_contract_owner, None); + } + // ---------- Optional token costs ---------- #[tokio::test] @@ -1405,15 +2075,10 @@ pub(crate) mod gas_sponsorship_tests { 0, ); let creation = setup.card_creation_without_token_payment().await; - let tx = setup.platform.drive.grove.start_transaction(); - assert_matches!(setup.process(&creation, &tx), SuccessfulExecution { .. }); - setup - .platform - .drive - .grove - .commit_transaction(tx) - .unwrap() - .expect("expected to commit the creation"); + assert_matches!( + setup.process_and_commit(&creation), + SuccessfulExecution { .. } + ); let deletion = setup.card_deletion_without_token_payment().await; assert_eq!(setup.check_tx(&deletion), Vec::::new()); @@ -1433,15 +2098,10 @@ pub(crate) mod gas_sponsorship_tests { 15, ); let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; - let tx = setup.platform.drive.grove.start_transaction(); - assert_matches!(setup.process(&creation, &tx), SuccessfulExecution { .. }); - setup - .platform - .drive - .grove - .commit_transaction(tx) - .unwrap() - .expect("expected to commit the creation"); + assert_matches!( + setup.process_and_commit(&creation), + SuccessfulExecution { .. } + ); let deletion = setup.card_deletion_without_token_payment().await; assert_eq!( setup.check_tx(&deletion), diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/mod.rs index 125856de4d6..6c525d14c0d 100644 --- a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/mod.rs +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -58,9 +59,20 @@ impl Drive { transaction, platform_version, ), + 2 => self.add_contested_document_for_contract_operations_v2( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + block_info, + also_insert_vote_poll_stored_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_contested_document_for_contract_operations".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v1/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v1/mod.rs index cb436494fde..9663552b56b 100644 --- a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v1/mod.rs +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v1/mod.rs @@ -16,7 +16,7 @@ use crate::query::GroveError; use crate::util::estimated_layers::EstimatedLayers; use crate::util::grove_operations::QueryTarget::QueryTargetValue; use crate::util::grove_operations::{BatchDeleteUpTreeApplyType, DirectQueryType}; -use crate::util::object_size_info::DocumentAndContractInfo; +use crate::util::object_size_info::{DocumentAndContractInfo, DocumentInfoV0Methods}; use dpp::block::block_info::BlockInfo; use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::ContestedIndexResolution; @@ -31,6 +31,33 @@ use grovedb::batch::KeyInfoPath; use grovedb::{MaybeTree, TransactionArg, TreeType}; impl Drive { + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contested_document_for_contract_operations_v1( + &self, + document_and_contract_info: DocumentAndContractInfo, + contested_document_resource_vote_poll: ContestedDocumentResourceVotePollWithContractInfo, + insert_without_check: bool, + block_info: &BlockInfo, + also_insert_vote_poll_stored_info: Option, + previous_batch_operations: &mut Option<&mut Vec>, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + self.add_contested_document_for_contract_operations_v1_body( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + block_info, + also_insert_vote_poll_stored_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + false, + transaction, + platform_version, + ) + } + /// Gathers the operations to add a contested document to a contract. /// /// Version 1 (protocol version 14) reads the contested index's resolution. A contest @@ -43,7 +70,7 @@ impl Drive { /// the generic windows of the version tables. #[inline(always)] #[allow(clippy::too_many_arguments)] - pub(super) fn add_contested_document_for_contract_operations_v1( + pub(super) fn add_contested_document_for_contract_operations_v1_body( &self, document_and_contract_info: DocumentAndContractInfo, contested_document_resource_vote_poll: ContestedDocumentResourceVotePollWithContractInfo, @@ -52,6 +79,7 @@ impl Drive { also_insert_vote_poll_stored_info: Option, previous_batch_operations: &mut Option<&mut Vec>, estimated_costs_only_with_layer_info: &mut Option, + end_date_entries_name_storage_owner: bool, transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { @@ -94,7 +122,19 @@ impl Drive { platform_version, )?; - let owner_id = document_and_contract_info.owned_document_info.owner_id; + // A poll's end date entries are paid for with the document, so from generation 2 they + // name whoever its storage flags name: a sponsor who paid is refunded when a second + // contender's arrival moves the end date. + let owner_id = if end_date_entries_name_storage_owner { + document_and_contract_info + .owned_document_info + .document_info + .get_storage_flags_ref() + .and_then(|storage_flags| storage_flags.owner_id().copied()) + .or(document_and_contract_info.owned_document_info.owner_id) + } else { + document_and_contract_info.owned_document_info.owner_id + }; if !contest_already_existed { if let Some(vote_poll_stored_start_info) = also_insert_vote_poll_stored_info { diff --git a/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v2/mod.rs b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v2/mod.rs new file mode 100644 index 00000000000..f72c491730f --- /dev/null +++ b/packages/rs-drive/src/drive/document/insert_contested/add_contested_document_for_contract_operations/v2/mod.rs @@ -0,0 +1,41 @@ +use crate::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfo; +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentAndContractInfo; +use dpp::block::block_info::BlockInfo; +use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::ContestedDocumentVotePollStoredInfo; +use grovedb::TransactionArg; +use platform_version::version::PlatformVersion; + +impl Drive { + /// Generation 1, except that a poll's end date entries name whoever the document's storage + /// flags name. + #[allow(clippy::too_many_arguments)] + pub(super) fn add_contested_document_for_contract_operations_v2( + &self, + document_and_contract_info: DocumentAndContractInfo, + contested_document_resource_vote_poll: ContestedDocumentResourceVotePollWithContractInfo, + insert_without_check: bool, + block_info: &BlockInfo, + also_insert_vote_poll_stored_info: Option, + previous_batch_operations: &mut Option<&mut Vec>, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + self.add_contested_document_for_contract_operations_v1_body( + document_and_contract_info, + contested_document_resource_vote_poll, + insert_without_check, + block_info, + also_insert_vote_poll_stored_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + true, + transaction, + platform_version, + ) + } +} diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/mod.rs index 5a6d4e397cd..e374a1ceab6 100644 --- a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/mod.rs +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::drive::Drive; use crate::util::estimated_layers::EstimatedLayers; @@ -100,9 +101,18 @@ impl Drive { transaction, platform_version, ), + // v2 moves the references of unchanged indexes to a new owner. + 2 => self.update_document_for_contract_operations_v2( + document_and_contract_info, + block_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + transaction, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_document_for_contract_operations".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs index 1c46f23d9ac..b390c354565 100644 --- a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs @@ -123,6 +123,28 @@ fn reference_tree_type_for_index( } impl Drive { + /// Generation 1: the references of the indexes an update leaves unchanged stay with the + /// owner they name, unless the document has no storage flags. + pub(in crate::drive::document::update) fn update_document_for_contract_operations_v1( + &self, + document_and_contract_info: DocumentAndContractInfo, + block_info: &BlockInfo, + previous_batch_operations: &mut Option<&mut Vec>, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + self.update_document_for_contract_operations_v1_body( + document_and_contract_info, + block_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + false, + transaction, + platform_version, + ) + } + /// Gathers operations for updating a document. /// /// v1 (platform v14+) applies the shared-prefix aggregate fix to the @@ -168,12 +190,14 @@ impl Drive { /// /// The `[0]` reference-bucket tree-type dispatch and everything /// else match v0. - pub(in crate::drive::document::update) fn update_document_for_contract_operations_v1( + #[allow(clippy::too_many_arguments)] + pub(super) fn update_document_for_contract_operations_v1_body( &self, document_and_contract_info: DocumentAndContractInfo, block_info: &BlockInfo, previous_batch_operations: &mut Option<&mut Vec>, estimated_costs_only_with_layer_info: &mut Option, + references_follow_the_owner: bool, transaction: TransactionArg, platform_version: &PlatformVersion, ) -> Result, Error> { @@ -380,6 +404,15 @@ impl Drive { ))); }; + // An update naming a new owner moves the references of the indexes it leaves unchanged + // too. An untrusted refresh writes the stored flags back, so those are refreshed as + // trusted: what they point at is unchanged. + let references_take_new_owner = references_follow_the_owner + && old_document_info + .get_storage_flags_ref() + .and_then(|flags| flags.owner_id()) + != storage_flags.and_then(|flags| flags.owner_id()); + let mut batch_insertion_cache: HashSet>> = HashSet::new(); // Pre-built tree of every index path in the doctype. Walking // this in parallel with each `index.properties` chain below @@ -554,6 +587,7 @@ impl Drive { current_index_level, &index_document_reference, index_storage_flags, + references_take_new_owner, &mut batch_insertion_cache, previous_batch_operations, index_batch_operations, @@ -1089,7 +1123,7 @@ impl Drive { // as a change, and an index skipping both was passed by above) // We can only trust the reference content has not changed if there are no storage flags - let trust_refresh_reference = storage_flags.is_none(); + let trust_refresh_reference = storage_flags.is_none() || references_take_new_owner; // unique indexes will be stored under key "0" // non unique indices should have a tree at key "0" that has all elements based off of primary key @@ -1241,6 +1275,7 @@ impl Drive { top_index_level: &IndexLevel, index_document_reference: &Element, storage_flags: Option<&StorageFlags>, + references_take_new_owner: bool, batch_insertion_cache: &mut HashSet>>, previous_batch_operations: &mut Option<&mut Vec>, batch_operations: &mut Vec, @@ -1433,7 +1468,7 @@ impl Drive { refresh_path, refresh_key, index_document_reference.clone(), - storage_flags.is_none(), + storage_flags.is_none() || references_take_new_owner, batch_operations, drive_version, )?; diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v2/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v2/mod.rs new file mode 100644 index 00000000000..38228f11f6c --- /dev/null +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v2/mod.rs @@ -0,0 +1,32 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentAndContractInfo; +use dpp::block::block_info::BlockInfo; +use grovedb::TransactionArg; +use platform_version::version::PlatformVersion; + +impl Drive { + /// Generation 1, except that an update naming a new owner rewrites the references of the + /// indexes it leaves unchanged with that owner, so all of the document's bytes move. + pub(in crate::drive::document::update) fn update_document_for_contract_operations_v2( + &self, + document_and_contract_info: DocumentAndContractInfo, + block_info: &BlockInfo, + previous_batch_operations: &mut Option<&mut Vec>, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + self.update_document_for_contract_operations_v1_body( + document_and_contract_info, + block_info, + previous_batch_operations, + estimated_costs_only_with_layer_info, + true, + transaction, + platform_version, + ) + } +} diff --git a/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers.rs b/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers.rs index ace265f6ef6..8dd3ff20060 100644 --- a/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers.rs +++ b/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers.rs @@ -6,6 +6,7 @@ use crate::drive::shielded::paths::{ use crate::drive::{Drive, RootTree}; use crate::error::drive::DriveError; use crate::error::Error; +use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use crate::util::batch::GroveDbOpBatch; use dpp::version::PlatformVersion; use grovedb::batch::QualifiedGroveDbOp; @@ -97,7 +98,7 @@ impl Drive { }, |batch| { self.grove_apply_batch( - GroveDbOpBatch { operations: batch }, + GroveDbOpBatch::from_operations(batch), false, Some(transaction), &platform_version.drive, diff --git a/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers/tests.rs b/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers/tests.rs index f6d6a8328c3..e583abb16e6 100644 --- a/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers/tests.rs +++ b/packages/rs-drive/src/drive/shielded/migration/backfill_historical_credit_pool_nullifiers/tests.rs @@ -154,7 +154,7 @@ fn should_union_full_and_partial_pages_preserving_existing_items_and_note_state( assert!(batch.len() <= 2048); batches.set(batches.get() + 1); drive.grove_apply_batch( - GroveDbOpBatch { operations: batch }, + GroveDbOpBatch::from_operations(batch), false, Some(&tx), &pv.drive, @@ -362,7 +362,7 @@ fn should_drop_partial_writes_on_failure_and_retry_to_the_same_root() { } batches += 1; drive.grove_apply_batch( - GroveDbOpBatch { operations: batch }, + GroveDbOpBatch::from_operations(batch), false, Some(&tx), &pv.drive, @@ -422,7 +422,7 @@ fn should_abort_a_corrupt_later_page_after_a_nonempty_batch() { |batch| { batches.set(batches.get() + 1); drive.grove_apply_batch( - GroveDbOpBatch { operations: batch }, + GroveDbOpBatch::from_operations(batch), false, Some(&tx), &pv.drive, diff --git a/packages/rs-drive/src/state_transition_action/batch/mod.rs b/packages/rs-drive/src/state_transition_action/batch/mod.rs index 60a434dea33..590550e26d2 100644 --- a/packages/rs-drive/src/state_transition_action/batch/mod.rs +++ b/packages/rs-drive/src/state_transition_action/batch/mod.rs @@ -3,7 +3,9 @@ use crate::state_transition_action::batch::v0::BatchTransitionActionV0; use crate::state_transition_action::contract::moderators_pot_settlement::ModeratorsPotSettlement; use derive_more::From; use dpp::data_contract::accessors::v0::DataContractV0Getters; -use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; +use dpp::data_contract::document_type::accessors::{DocumentTypeV0Getters, DocumentTypeV1Getters}; +use dpp::data_contract::document_type::DocumentTypeRef; +use dpp::tokens::gas_fees_paid_by::GasFeesPaidBy; use crate::drive::contract_groups::types::ContractGroupMembershipsForContract; use dpp::fee::fee_result::FeeResult; use dpp::consensus::ConsensusError; @@ -18,7 +20,9 @@ use dpp::data_contract::document_type::action_fees::{ ActionFeePricing, ContractFeePot, DocumentActionFee, FEE_MULTIPLIER_PERMILLE_BASE, }; use dpp::prelude::FeeMultiplier; -use crate::util::batch::drive_op_batch::{ContractFeePotOperationType, IdentityOperationType}; +use crate::util::batch::drive_op_batch::{ + ContractFeePotOperationType, DocumentOperationType, IdentityOperationType, +}; use crate::util::batch::DriveOperation; use crate::state_transition_action::batch::batched_transition::document_transition::document_base_transition_action::DocumentBaseTransitionActionAccessorsV0; @@ -172,6 +176,79 @@ pub fn action_fee_operations( Ok(operations) } +/// Names the gas sponsor as the owner of the storage the batch's documents write (protocol +/// version 15), once fee validation settled that the sponsor pays: storage refunds go to whoever +/// paid for the storage, and a document's would otherwise go to its owner, who could delete it +/// and keep what the sponsor paid. +/// +/// Deletes name no owner (their refunds go to the owners recorded on what they remove), history +/// entries, which are never deleted, refund nobody, and the withdrawal and multi-document +/// operations are never part of a batch. A document of a type with a `ttl` is stored without +/// flags and refunds nobody. +/// +/// The estimate needs no owner, and an owner takes no more bytes than any other, so this +/// changes no fee. +pub fn record_gas_sponsor_as_storage_owner( + operations: &mut [DriveOperation], + gas_sponsor_id: Identifier, +) { + for operation in operations { + let DriveOperation::DocumentOperation(document_operation) = operation else { + continue; + }; + match document_operation { + DocumentOperationType::AddDocument { + owned_document_info, + .. + } + | DocumentOperationType::AddContestedDocument { + owned_document_info, + .. + } + | DocumentOperationType::UpdateDocument { + owned_document_info, + .. + } + | DocumentOperationType::AddDocumentAndDeleteConsumed { + owned_document_info, + .. + } + | DocumentOperationType::AddDocumentAndDeleteReadConsumed { + owned_document_info, + .. + } => owned_document_info + .document_info + .set_storage_flags_owner(gas_sponsor_id.to_buffer()), + DocumentOperationType::DeleteDocument { .. } + | DocumentOperationType::ForceDeleteDocument { .. } + | DocumentOperationType::ForceDeleteReadDocument { .. } + | DocumentOperationType::DeleteIndexOnlyDocument { .. } + | DocumentOperationType::AddWithdrawalDocument { .. } + | DocumentOperationType::MultipleDocumentOperationsForSameContractDocumentType { + .. + } + | DocumentOperationType::DocumentHistory { .. } => {} + } + } +} + +/// Whether a document type's token costs let the contract owner pay the gas of some action on +/// its documents: only then can the contract owner hold the storage of a document someone else +/// owns as its gas sponsor. +pub(crate) fn document_type_offers_gas_sponsorship(document_type: DocumentTypeRef) -> bool { + [ + document_type.document_creation_token_cost(), + document_type.document_replacement_token_cost(), + document_type.document_deletion_token_cost(), + document_type.document_transfer_token_cost(), + document_type.document_update_price_token_cost(), + document_type.document_purchase_token_cost(), + ] + .into_iter() + .flatten() + .any(|token_cost| token_cost.gas_fees_paid_by != GasFeesPaidBy::DocumentOwner) +} + /// Who pays the gas of a whole batch, as `GasFeesPaidBy::resolve` names it for each of its /// transitions #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/document.rs b/packages/rs-drive/src/util/batch/drive_op_batch/document.rs index 7af06f1371d..16fe3bb864e 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/document.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/document.rs @@ -2,6 +2,7 @@ use crate::util::estimated_layers::EstimatedLayers; use crate::drive::Drive; use crate::error::Error; use crate::fees::op::LowLevelDriveOperation; +use crate::state_transition_action::batch::document_type_offers_gas_sponsorship; use crate::state_transition_action::batch::batched_transition::document_transition::document_create_transition_action::ConsumedDocument; use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter; use crate::util::object_size_info::DocumentInfo::{DocumentRefAndSerialization, DocumentRefInfo}; @@ -270,6 +271,29 @@ impl DriveLowLevelOperationConverter for DocumentOperationType<'_> { } impl DocumentOperationType<'_> { + /// The owner of this update's contract, when its document type offers gas sponsorship: + /// the identity that may hold the storage of a document someone else owns. Batches always + /// carry the resolved contract, so an update that names only its id has no sponsor here. + pub(crate) fn storage_sponsor_of_updated_document(&self) -> Result, Error> { + let Self::UpdateDocument { + contract_info, + document_type_info, + .. + } = self + else { + return Ok(None); + }; + let contract = match contract_info { + DataContractInfo::DataContractFetchInfo(fetch_info) => &fetch_info.contract, + DataContractInfo::BorrowedDataContract(contract) => *contract, + DataContractInfo::OwnedDataContract(contract) => contract, + DataContractInfo::DataContractId(_) => return Ok(None), + }; + let document_type = document_type_info.clone().resolve(contract)?; + Ok(document_type_offers_gas_sponsorship(document_type) + .then(|| contract.owner_id().to_buffer())) + } + /// Run all direct TTL cleanup before any operation in this batch is built. pub(crate) fn prepare_time_range_ttl( &self, diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index 3b7b69f625c..15be4da6bda 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -132,6 +132,7 @@ impl Drive { let forfeits_storage_refunds = operations .iter() .any(DriveOperation::forfeits_storage_refunds); + let full_handover_owners = DriveOperation::full_handover_owners(&operations)?; // The document operations of a moderator's deletion go in a batch of their own only // when something else in the batch may free bytes someone is owed. let separates_forfeited_operations = forfeits_storage_refunds @@ -218,10 +219,11 @@ impl Drive { } else { Some(estimated_costs_only_with_layer_info.clone()) }; - self.apply_batch_low_level_drive_operations( + self.apply_batch_low_level_drive_operations_with_full_handover_owners( estimated_costs_only_with_layer_info, transaction, low_level_operations, + full_handover_owners, &mut cost_operations, &platform_version.drive, )?; 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 5c790574069..261abd18316 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 @@ -282,6 +282,21 @@ impl DriveLowLevelOperationConverter for DriveOperation<'_> { } impl DriveOperation<'_> { + /// The identities for which an owner change in this batch is a full handover: the owners + /// of the contracts offering gas sponsorship on the documents the batch updates (protocol + /// version 15). A batch naming none is estimated and applied as it always was. + pub(crate) fn full_handover_owners( + operations: &[DriveOperation], + ) -> Result, Error> { + let mut owners = BTreeSet::new(); + for operation in operations { + if let DriveOperation::DocumentOperation(document_operation) = operation { + owners.extend(document_operation.storage_sponsor_of_updated_document()?); + } + } + Ok(owners) + } + /// Whether the batch this operation is in refunds nobody for the storage its document /// operations remove: see [`ContractModerationOperationType::ForfeitStorageRefunds`]. pub fn forfeits_storage_refunds(&self) -> bool { 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 bb778d2599f..963e7e92f87 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 @@ -24,6 +24,7 @@ use grovedb::operations::proof::util::hex_to_ascii; use grovedb::{Element, TreeType}; use grovedb_version::version::GroveVersion; use std::borrow::Cow; +use std::collections::BTreeSet; use std::fmt; /// A batch of GroveDB operations as a vector. @@ -32,6 +33,10 @@ use std::fmt; pub struct GroveDbOpBatch { /// Operations pub(crate) operations: Vec, + /// Identities for which an owner change is a full handover rather than a change of the + /// stored bytes' owner: the owners of the contracts offering gas sponsorship on what the + /// batch updates. Empty before protocol version 15. + pub(crate) full_handover_owners: BTreeSet<[u8; 32]>, } #[derive(Debug, PartialEq, Eq, Copy, Clone)] @@ -512,9 +517,7 @@ pub trait GroveDbOpBatchV0Methods { impl GroveDbOpBatchV0Methods for GroveDbOpBatch { /// Creates a new empty batch of GroveDB operations. fn new() -> Self { - GroveDbOpBatch { - operations: Vec::new(), - } + GroveDbOpBatch::default() } /// Gets the number of operations from a list of GroveDB ops. @@ -544,7 +547,10 @@ impl GroveDbOpBatchV0Methods for GroveDbOpBatch { /// Puts a list of GroveDB operations into a batch. fn from_operations(operations: Vec) -> Self { - GroveDbOpBatch { operations } + GroveDbOpBatch { + operations, + ..Default::default() + } } /// Adds an `Insert` operation with an empty tree at the specified path and key to a list of GroveDB ops. diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/mod.rs index ccea6a9242a..ea1549856b6 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::util::batch::GroveDbOpBatch; @@ -40,9 +41,16 @@ impl Drive { drive_operations, drive_version, ), + 1 => self.grove_apply_batch_with_add_costs_v1( + ops, + validate, + transaction, + drive_operations, + drive_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "grove_apply_batch_with_add_costs".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } 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 baf9070b52c..916bfd9cb59 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 @@ -8,7 +8,9 @@ use crate::util::batch::GroveDbOpBatch; use crate::util::grove_operations::push_drive_operation_result; use crate::util::storage_flags::StorageFlags; use grovedb::batch::{BatchApplyOptions, QualifiedGroveDbOp}; -use grovedb::TransactionArg; +use grovedb::{ElementFlags, TransactionArg}; +use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; +use grovedb_costs::storage_cost::StorageCost; use platform_version::version::drive_versions::DriveVersion; impl Drive { @@ -20,6 +22,37 @@ impl Drive { transaction: TransactionArg, drive_operations: &mut Vec, drive_version: &DriveVersion, + ) -> Result<(), Error> { + self.grove_apply_batch_with_flags_update( + ops, + validate, + false, + |cost, old_flags, new_flags| { + StorageFlags::update_element_flags(cost, old_flags, new_flags) + .map_err(|e| GroveError::JustInTimeElementFlagsClientError(e.to_string())) + }, + transaction, + drive_operations, + drive_version, + ) + } + + /// Applies the batch with the given flags update and passes the costs to + /// `push_drive_operation_result`. + #[allow(clippy::too_many_arguments)] + pub(super) fn grove_apply_batch_with_flags_update>( + &self, + ops: GroveDbOpBatch, + validate: bool, + allow_replacement_accounting: bool, + update_element_flags: impl FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + transaction: TransactionArg, + drive_operations: &mut Vec, + drive_version: &DriveVersion, ) -> Result<(), Error> { if ops.is_empty() { return Err(Error::Drive(DriveError::BatchIsEmpty( @@ -71,12 +104,9 @@ impl Drive { disable_operation_consistency_check: !self.config.batching_consistency_verification, base_root_storage_is_free: true, batch_pause_height: None, - allow_replacement_accounting: false, + allow_replacement_accounting, }), - |cost, old_flags, new_flags| { - StorageFlags::update_element_flags(cost, old_flags, new_flags) - .map_err(|e| GroveError::JustInTimeElementFlagsClientError(e.to_string())) - }, + update_element_flags, |flags, removed_key_bytes, removed_value_bytes| { StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) .map_err(|e| GroveError::SplitRemovalBytesClientError(e.to_string())) diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs new file mode 100644 index 00000000000..e87b94f7e1e --- /dev/null +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs @@ -0,0 +1,153 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::query::GroveError; +use crate::util::batch::GroveDbOpBatch; +use crate::util::storage_flags::StorageFlags; +use grovedb::{ElementFlags, TransactionArg}; +use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; +use grovedb_costs::storage_cost::StorageCost; +use grovedb_epoch_based_storage_flags::error::StorageFlagsError; +use platform_version::version::drive_versions::DriveVersion; +use std::collections::BTreeSet; + +impl Drive { + /// Generation 0 with a flags update that follows the owner the written flags name: see + /// [`update_element_flags_for_owner_change`]. + pub(super) fn grove_apply_batch_with_add_costs_v1( + &self, + ops: GroveDbOpBatch, + validate: bool, + transaction: TransactionArg, + drive_operations: &mut Vec, + drive_version: &DriveVersion, + ) -> Result<(), Error> { + let full_handover_owners = ops.full_handover_owners.clone(); + self.grove_apply_batch_with_flags_update( + ops, + validate, + !full_handover_owners.is_empty(), + |cost, old_flags, new_flags| { + update_element_flags_for_owner_change( + cost, + old_flags, + new_flags, + &full_handover_owners, + ) + .map_err(|e| GroveError::JustInTimeElementFlagsClientError(e.to_string())) + }, + transaction, + drive_operations, + drive_version, + ) + } +} + +/// The flags update of a stored element whose new flags may name a new owner. +/// +/// An owner change involving one of `full_handover_owners` refunds the old owner for the whole +/// element and charges the payer for all of it, so a document's owner cannot keep storage a +/// gas sponsor paid for, nor a sponsor what the owner paid for. Any other owner change keeps +/// the stored bytes and moves them to the new owner, as a transfer or a purchase does. +fn update_element_flags_for_owner_change( + cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags, + full_handover_owners: &BTreeSet<[u8; 32]>, +) -> Result { + let names_full_handover_owner = |flags: &ElementFlags| -> Result { + Ok( + StorageFlags::from_element_flags_ref(flags)?.is_some_and(|flags| { + flags + .owner_id() + .is_some_and(|owner| full_handover_owners.contains(owner)) + }), + ) + }; + let is_full_handover = !full_handover_owners.is_empty() + && (names_full_handover_owner(new_flags)? + || old_flags + .as_ref() + .map(names_full_handover_owner) + .transpose()? + .unwrap_or(false)); + if is_full_handover { + StorageFlags::update_element_flags_replacing_on_owner_change(cost, old_flags, new_flags) + } else { + StorageFlags::update_element_flags_with_written_owner(cost, old_flags, new_flags) + .map(ElementFlagsUpdate::from) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use grovedb_costs::storage_cost::removal::StorageRemovedBytes::NoStorageRemoval; + + const OLD_OWNER: [u8; 32] = [1; 32]; + const NEW_OWNER: [u8; 32] = [2; 32]; + const SPONSOR: [u8; 32] = [3; 32]; + + fn flags(epoch: u16, owner: [u8; 32]) -> ElementFlags { + StorageFlags::new_single_epoch(epoch, Some(owner)).to_element_flags() + } + + fn update(old_owner: [u8; 32], new_owner: [u8; 32]) -> (ElementFlagsUpdate, StorageFlags) { + let same_size = StorageCost { + added_bytes: 0, + replaced_bytes: 10, + removed_bytes: NoStorageRemoval, + }; + let mut new_flags = flags(2, new_owner); + let answer = update_element_flags_for_owner_change( + &same_size, + Some(flags(1, old_owner)), + &mut new_flags, + &BTreeSet::from([SPONSOR]), + ) + .expect("expected the flags to update"); + let written = StorageFlags::from_element_flags_ref(&new_flags) + .expect("expected flags that decode") + .expect("expected flags"); + (answer, written) + } + + #[test] + fn should_keep_the_stored_bytes_and_change_their_owner_when_no_sponsor_is_involved() { + let (answer, written) = update(OLD_OWNER, NEW_OWNER); + + assert_eq!(answer, ElementFlagsUpdate::Changed); + assert_eq!(written.owner_id(), Some(&NEW_OWNER)); + assert_eq!( + written.base_epoch(), + &1, + "the stored bytes keep their epoch" + ); + } + + #[test] + fn should_account_a_replacement_when_the_old_owner_is_a_sponsor() { + let (answer, written) = update(SPONSOR, NEW_OWNER); + + assert_eq!(answer, ElementFlagsUpdate::AccountAsReplacement); + assert_eq!(written.owner_id(), Some(&NEW_OWNER)); + assert_eq!(written.base_epoch(), &2, "the new owner's bytes start now"); + } + + #[test] + fn should_account_a_replacement_when_the_new_owner_is_a_sponsor() { + let (answer, written) = update(OLD_OWNER, SPONSOR); + + assert_eq!(answer, ElementFlagsUpdate::AccountAsReplacement); + assert_eq!(written.owner_id(), Some(&SPONSOR)); + } + + #[test] + fn should_not_account_a_replacement_when_a_sponsor_keeps_its_element() { + let (answer, written) = update(SPONSOR, SPONSOR); + + assert_ne!(answer, ElementFlagsUpdate::AccountAsReplacement); + assert_eq!(written.owner_id(), Some(&SPONSOR)); + assert_eq!(written.base_epoch(), &1); + } +} diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_operation/v0/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_operation/v0/mod.rs index 9bb4ab95c48..974f4379284 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_operation/v0/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_operation/v0/mod.rs @@ -1,5 +1,6 @@ use crate::drive::Drive; use crate::error::Error; +use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use crate::util::batch::GroveDbOpBatch; use dpp::version::drive_versions::DriveVersion; use grovedb::batch::QualifiedGroveDbOp; @@ -15,9 +16,7 @@ impl Drive { drive_version: &DriveVersion, ) -> Result<(), Error> { self.grove_apply_batch_with_add_costs( - GroveDbOpBatch { - operations: vec![operation], - }, + GroveDbOpBatch::from_operations(vec![operation]), validate, transaction, &mut vec![], diff --git a/packages/rs-drive/src/util/object_size_info/document_info.rs b/packages/rs-drive/src/util/object_size_info/document_info.rs index 56b5f999386..2c78ed2218b 100644 --- a/packages/rs-drive/src/util/object_size_info/document_info.rs +++ b/packages/rs-drive/src/util/object_size_info/document_info.rs @@ -98,6 +98,23 @@ fn indexed_property_type<'a>( } impl DocumentInfo<'_> { + /// Names `owner_id` as the owner of the storage this document writes, the identity its + /// storage refunds go to. Storage flags without an owner refund nobody and stay that way, + /// as does a document without flags or a worst-case size. The owner takes no more bytes + /// than any other, so the write costs the same. + pub fn set_storage_flags_owner(&mut self, owner_id: [u8; 32]) { + let storage_flags = match self { + DocumentInfo::DocumentOwnedInfo((_, storage_flags)) + | DocumentInfo::DocumentRefInfo((_, storage_flags)) + | DocumentInfo::DocumentRefAndSerialization((_, _, storage_flags)) + | DocumentInfo::DocumentAndSerialization((_, _, storage_flags)) => storage_flags, + DocumentInfo::DocumentEstimatedAverageSize(_) => return, + }; + if let Some(storage_flags) = storage_flags { + storage_flags.to_mut().set_owner_id(owner_id); + } + } + /// The same document without storage flags: how the document of a type declaring a /// `ttl` is written, since such a document refunds nothing. A worst-case size passes /// through unchanged. diff --git a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs index 5e7d869297d..73b74a32b8c 100644 --- a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs +++ b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs @@ -9,6 +9,7 @@ use crate::util::estimated_layers::EstimatedLayers; use dpp::version::drive_versions::DriveVersion; use grovedb::TransactionArg; +use std::collections::BTreeSet; impl Drive { /// Applies a batch of Drive operations to groveDB depending on the drive version. @@ -38,6 +39,27 @@ impl Drive { batch_operations: Vec, drive_operations: &mut Vec, drive_version: &DriveVersion, + ) -> Result<(), Error> { + self.apply_batch_low_level_drive_operations_with_full_handover_owners( + estimated_costs_only_with_layer_info, + transaction, + batch_operations, + BTreeSet::new(), + drive_operations, + drive_version, + ) + } + + /// [`Self::apply_batch_low_level_drive_operations`], for a batch where an owner change + /// involving one of `full_handover_owners` is a full handover (protocol version 15). + pub(crate) fn apply_batch_low_level_drive_operations_with_full_handover_owners( + &self, + estimated_costs_only_with_layer_info: Option, + transaction: TransactionArg, + batch_operations: Vec, + full_handover_owners: BTreeSet<[u8; 32]>, + drive_operations: &mut Vec, + drive_version: &DriveVersion, ) -> Result<(), Error> { match drive_version .methods @@ -65,6 +87,7 @@ impl Drive { estimated_costs_only_with_layer_info, transaction, batch_operations, + full_handover_owners, drive_operations, drive_version, ) diff --git a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs index 34c30403eee..fa2ed632c93 100644 --- a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs +++ b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs @@ -7,6 +7,7 @@ use crate::util::batch::grovedb_op_batch::GroveDbOpBatchV0Methods; use crate::util::estimated_layers::EstimatedLayers; use dpp::version::drive_versions::DriveVersion; use grovedb::TransactionArg; +use std::collections::BTreeSet; impl Drive { /// Applies a batch of Drive operations to groveDB. @@ -20,14 +21,17 @@ impl Drive { estimated_costs_only_with_layer_info: Option, transaction: TransactionArg, mut batch_operations: Vec, + full_handover_owners: BTreeSet<[u8; 32]>, drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { coalesce_current_key_alias_operations(&mut batch_operations); - let (grove_db_operations, ephemeral_batches, mut other_operations) = + let (mut grove_db_operations, ephemeral_batches, mut other_operations) = LowLevelDriveOperation::grovedb_operations_batch_consume_split_ephemeral( batch_operations, ); + // Ephemeral operations carry no storage flags, so only the ordinary batch needs them. + grove_db_operations.full_handover_owners = full_handover_owners; // Ephemeral operations (a `timeRange` index's TTL'd sub-levels, the // writes of a document whose type declares a `ttl`) apply as their // own batch per pricing rule, so each batch's cost is known diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v11.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v11.rs index ffa5923ec3e..93e7ea6435d 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v11.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v11.rs @@ -4,7 +4,7 @@ use super::{ DriveAbciCoreChainLockMethodVersionsAndConstants, DriveAbciEngineMethodVersions, DriveAbciIdentityCreditWithdrawalMethodVersions, DriveAbciMasternodeIdentitiesUpdatesMethodVersions, DriveAbciMethodVersions, - DriveAbciVotingMethodVersions, + DriveAbciStateTransitionProcessingMethodVersions, DriveAbciVotingMethodVersions, }; /// Dash Core's DKG schedule (`llmq/params.h`) of the non-rotating quorum types, the ones chain @@ -153,5 +153,9 @@ pub const DRIVE_ABCI_METHOD_VERSIONS_V11: DriveAbciMethodVersions = DriveAbciMet build_untied_withdrawal_transactions_from_documents: 1, // changed in v15: asset unlock payload version 2 once V24 is recorded as active ..DRIVE_ABCI_METHOD_VERSIONS_V10.withdrawals }, + state_transition_processing: DriveAbciStateTransitionProcessingMethodVersions { + execute_event: 2, // changed in v15: a paying gas sponsor owns the storage its batch writes + ..DRIVE_ABCI_METHOD_VERSIONS_V10.state_transition_processing + }, ..DRIVE_ABCI_METHOD_VERSIONS_V10 }; 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 a5f32ea84d2..e96bbf31043 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs @@ -1,7 +1,8 @@ use crate::version::drive_versions::drive_document_method_versions::v4::DRIVE_DOCUMENT_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_document_method_versions::{ DriveDocumentDeleteMethodVersions, DriveDocumentEstimationCostsMethodVersions, - DriveDocumentMethodVersions, + DriveDocumentInsertContestedMethodVersions, DriveDocumentMethodVersions, + DriveDocumentUpdateMethodVersions, }; /// V5 is protocol version 15's document-method table. Relative to V4: @@ -18,6 +19,10 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// the insert's does, not as subtrees. /// * `estimation_costs.stateless_delete_of_non_tree_for_costs` is 1: a delete's estimate prices /// each height of the climb with the layer GroveDB deletes in there, not the one above it. +/// * `update.update_document_for_contract_operations` is 2: when an update names a new owner, +/// the references of the indexes it leaves unchanged are rewritten with that owner. +/// * `insert_contested.add_contested_document_for_contract_operations` is 2: a poll's end date +/// entries name whoever the document's storage flags name. pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = DriveDocumentMethodVersions { insert_empty_private_document_store_operation: Some(0), // changed: a private document type's store is created with it @@ -32,5 +37,13 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = stateless_delete_of_non_tree_for_costs: 1, // changed: each height gets the layer priced there ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.estimation_costs }, + update: DriveDocumentUpdateMethodVersions { + update_document_for_contract_operations: 2, // changed: unchanged index references follow a new owner + ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.update + }, + insert_contested: DriveDocumentInsertContestedMethodVersions { + add_contested_document_for_contract_operations: 2, // changed: end date entries name the storage owner + ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.insert_contested + }, ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4 }; diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/mod.rs index c41823323b5..cf943258cf1 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/mod.rs @@ -1,6 +1,7 @@ use versioned_feature_core::FeatureVersion; pub mod v1; +pub mod v2; #[derive(Clone, Debug, Default)] pub struct DriveGroveMethodVersions { diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs new file mode 100644 index 00000000000..7f377b3474f --- /dev/null +++ b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs @@ -0,0 +1,17 @@ +use crate::version::drive_versions::drive_grove_method_versions::v1::DRIVE_GROVE_METHOD_VERSIONS_V1; +use crate::version::drive_versions::drive_grove_method_versions::{ + DriveGroveApplyMethodVersions, DriveGroveMethodVersions, +}; + +/// V2 is protocol version 15's GroveDB method table. Relative to V1, +/// `apply.grove_apply_batch` is 1: an element keeps its stored bytes under the owner the +/// written flags name, and a batch naming a gas sponsor accounts that sponsor's owner changes +/// as replacements (see protocol version 15's notes). Estimates do not change: a document +/// update is already estimated as the insertion of everything it writes. +pub const DRIVE_GROVE_METHOD_VERSIONS_V2: DriveGroveMethodVersions = DriveGroveMethodVersions { + apply: DriveGroveApplyMethodVersions { + grove_apply_batch: 1, // changed: the flags update follows the written owner + ..DRIVE_GROVE_METHOD_VERSIONS_V1.apply + }, + ..DRIVE_GROVE_METHOD_VERSIONS_V1 +}; 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 19cf3009495..4a321bcd4fb 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -3,6 +3,7 @@ use crate::version::drive_versions::drive_contract_method_versions::{ DriveContractFeePotMethodVersions, DriveContractMethodVersions, }; use crate::version::drive_versions::drive_document_method_versions::v5::DRIVE_DOCUMENT_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_grove_method_versions::v2::DRIVE_GROVE_METHOD_VERSIONS_V2; use crate::version::drive_versions::drive_identity_method_versions::v2::DRIVE_IDENTITY_METHOD_VERSIONS_V2; use crate::version::drive_versions::drive_identity_method_versions::{ DriveIdentityCostEstimationMethodVersions, DriveIdentityMethodVersions, @@ -91,6 +92,9 @@ use grovedb_version::version::v5::GROVE_V5; /// 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`). /// +/// * **Storage handover** (protocol v15 note 28): `grove_methods` is +/// `DRIVE_GROVE_METHOD_VERSIONS_V2`. +/// /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { @@ -140,5 +144,6 @@ pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { // 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, + grove_methods: DRIVE_GROVE_METHOD_VERSIONS_V2, // changed in v10: the flags update follows the written owner ..DRIVE_VERSION_V9 }; diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index aa794085509..30e8b4d2ef5 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -567,6 +567,18 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// block's rate. Every schedule so far has the same storage rate, so no refund changes. A /// removal from an earlier epoch pays once per such epoch for reading its record, the only /// fee this changes; generation 0 reads nothing. +/// +/// 28. **Storage handover**: when a write names a new owner on a stored element, the owner it +/// names owns the element afterwards at any size (`DRIVE_VERSION_V10`, +/// `DRIVE_GROVE_METHOD_VERSIONS_V2`). A transfer or a purchase keeps the stored bytes and +/// moves them to the new owner, the references of the indexes it leaves unchanged included +/// (`update_document_for_contract_operations` 2). A gas sponsor who pays is named as the +/// owner of what the batch writes (`execute_event` 2), and when the old or new owner is +/// the owner of a contract whose document type the batch updates offers sponsorship, the +/// old owner is refunded for the whole element and the payer charged for all of it, so a +/// document's owner cannot keep storage the sponsor paid for. A poll's end date entries +/// name whoever the document's flags name +/// (`add_contested_document_for_contract_operations` 2). pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, drive: DRIVE_VERSION_V10, // changed: calculate_fee 1 prices a refund at the storing epoch's rate (note 27); 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) From 704d3f03bf86d12512cac1c8d5243d3e41abc77b Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 01:41:18 +0700 Subject: [PATCH 3/6] chore: move the GroveDB pin past allow_replacement_accounting Moves the pin to fa92511c, which removes that batch option and bundles SQLite 3.54 through rusqlite 0.41. Only one SQLite can be linked, so the wallet crates move to rusqlite 0.41 and refinery 0.10, whose migrations support it. That also picks up the fix for RUSTSEC-2026-0459. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 88 ++++++++++--------- Cargo.toml | 18 ++-- .../v0/mod.rs | 1 - .../v0/mod.rs | 1 - .../grove_batch_operations_costs/v0/mod.rs | 1 - .../rs-platform-wallet-storage/Cargo.toml | 6 +- .../integration/sqlite_buffer_semantics.rs | 6 +- .../integration/sqlite_load_reconstruction.rs | 13 ++- packages/rs-platform-wallet/Cargo.toml | 4 +- 9 files changed, 71 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 389510f9340..e2fd994e2b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1273,7 +1273,7 @@ version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "faf9468729b8cbcea668e36183cb69d317348c2e08e994829fb56ebfdfbaac34" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -2756,7 +2756,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3237,7 +3237,7 @@ dependencies = [ [[package]] name = "grovedb" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" dependencies = [ "byteorder", "ed", @@ -3459,7 +3459,7 @@ dependencies = [ [[package]] name = "grovedb-storage" version = "6.1.1" -source = "git+https://github.com/dashpay/grovedb?rev=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" 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=45e2d84fb9a7f30d1221a432613948a98184a12c#45e2d84fb9a7f30d1221a432613948a98184a12c" +source = "git+https://github.com/dashpay/grovedb?rev=fa92511cb7cdc19f5977d64f20f93107f62844b1#fa92511cb7cdc19f5977d64f20f93107f62844b1" dependencies = [ "serde", "serde_with 3.21.0", @@ -3627,14 +3627,17 @@ name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash 0.2.0", +] [[package]] name = "hashlink" -version = "0.11.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824e001ac4f3012dd16a264bec811403a67ca9deb6c102fc5049b32c4574b35f" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" dependencies = [ - "hashbrown 0.16.1", + "hashbrown 0.17.1", ] [[package]] @@ -3908,7 +3911,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.5.10", + "socket2 0.6.4", "system-configuration", "tokio", "tower-service", @@ -4152,7 +4155,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -4529,9 +4532,9 @@ dependencies = [ [[package]] name = "libsqlite3-sys" -version = "0.36.0" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95b4103cffefa72eb8428cb6b47d6627161e51c2739fc5e3b734584157bc642a" +checksum = "14a5f01b87b92705d07b433c06f96cbc764637d81a0bffb5734ce17cf0f9a2b3" dependencies = [ "cc", "pkg-config", @@ -4939,7 +4942,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -5086,7 +5089,7 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.5.0", "proc-macro2", "quote", "syn 2.0.117", @@ -5811,7 +5814,7 @@ version = "0.14.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" dependencies = [ - "heck 0.4.1", + "heck 0.5.0", "itertools 0.14.0", "log", "multimap", @@ -5962,7 +5965,7 @@ dependencies = [ "quinn-udp", "rustc-hash 2.1.2", "rustls", - "socket2 0.5.10", + "socket2 0.6.4", "thiserror 2.0.18", "tokio", "tracing", @@ -6001,7 +6004,7 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2 0.6.4", "tracing", "windows-sys 0.59.0", ] @@ -6224,9 +6227,9 @@ dependencies = [ [[package]] name = "refinery" -version = "0.9.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e2a344cdb48871e27addeafbbaffab8828cc12cec2b9041119e9bea0c0f551a" +checksum = "8e48a123dfddb4e33b888a233e3dd8f428d0dadbe8a2302177fcad504b9fb0e3" dependencies = [ "refinery-core", "refinery-macros", @@ -6234,11 +6237,10 @@ dependencies = [ [[package]] name = "refinery-core" -version = "0.9.2" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24eeafd893124f29183dd6afa9137a27e7bef59250223b8b660005279c60aea4" +checksum = "07f0a6b2d031aec7903f7ce81c723bbe7d3d8667441012b2a4388af0f3a70ef3" dependencies = [ - "async-trait", "cfg-if", "log", "regex", @@ -6252,9 +6254,9 @@ dependencies = [ [[package]] name = "refinery-macros" -version = "0.9.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a90cea6d11a9a4e8a85a884b6305461004101b28ca65dd35ec028e009a898e16" +checksum = "623c53cdc704ababffe5361be8cb726f874fcd4e97b945741aa69b15d0406f36" dependencies = [ "proc-macro2", "quote", @@ -6724,9 +6726,9 @@ dependencies = [ [[package]] name = "rusqlite" -version = "0.38.0" +version = "0.41.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1c93dd1c9683b438c392c492109cb702b8090b2bfc8fed6f6e4eb4523f17af3" +checksum = "da9bba6f3506135e120a6586df05be18b48d18896600646a4bec8f993d91a594" dependencies = [ "bitflags 2.13.0", "fallible-iterator", @@ -6814,7 +6816,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -6873,7 +6875,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -7704,7 +7706,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix 1.1.4", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -9149,7 +9151,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 814ffeba878..ae9ae03aa37 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 = "45e2d84fb9a7f30d1221a432613948a98184a12c", default-features = false } -grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-merk = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c", default-features = false } -grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } -grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "45e2d84fb9a7f30d1221a432613948a98184a12c" } +grovedb = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1", default-features = false } +grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-merk = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1", default-features = false } +grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-query = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } +grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "fa92511cb7cdc19f5977d64f20f93107f62844b1" } 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-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 baf9070b52c..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 @@ -71,7 +71,6 @@ impl Drive { disable_operation_consistency_check: !self.config.batching_consistency_verification, base_root_storage_is_free: true, batch_pause_height: None, - allow_replacement_accounting: false, }), |cost, old_flags, new_flags| { StorageFlags::update_element_flags(cost, old_flags, new_flags) 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 f3ad2d1f531..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 @@ -56,7 +56,6 @@ impl Drive { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, - allow_replacement_accounting: false, }), |cost, old_flags, new_flags| { // if there were no flags before then the new flags are used diff --git a/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs index fa19668c8c2..42dec3e28cf 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_batch_operations_costs/v0/mod.rs @@ -29,7 +29,6 @@ impl Drive { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, - allow_replacement_accounting: false, }), |_, _, _| Ok(false), |_, _, _| Err(GroveError::InternalError("not implemented".to_string())), diff --git a/packages/rs-platform-wallet-storage/Cargo.toml b/packages/rs-platform-wallet-storage/Cargo.toml index 69f3ca9aefc..6877f8ba404 100644 --- a/packages/rs-platform-wallet-storage/Cargo.toml +++ b/packages/rs-platform-wallet-storage/Cargo.toml @@ -48,7 +48,7 @@ dash-sdk = { path = "../rs-sdk", default-features = false, features = [ "dashpay-contract", "dpns-contract", ], optional = true } -rusqlite = { version = "0.38", features = [ +rusqlite = { version = "0.41", features = [ "bundled", "backup", "blob", @@ -56,12 +56,12 @@ rusqlite = { version = "0.38", features = [ "limits", "trace", ], optional = true } -refinery = { version = "=0.9.2", default-features = false, features = [ +refinery = { version = "=0.10.0", default-features = false, features = [ "rusqlite", ], optional = true } # The migration adapter uses refinery's supported synchronous driver traits. # Match refinery's driver traits and applied-history timestamp types. -refinery-core = { version = "=0.9.2", default-features = false, optional = true } +refinery-core = { version = "=0.10.1", default-features = false, optional = true } time = { version = "=0.3.49", features = ["parsing"], optional = true } # bincode 2 is required directly: we encode `dpp::IdentityPublicKey` # (which derives bincode 2 `Encode`/`Decode`) and decode diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_buffer_semantics.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_buffer_semantics.rs index af151578e6a..72c89318136 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_buffer_semantics.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_buffer_semantics.rs @@ -459,7 +459,7 @@ fn tc023_one_flush_is_one_transaction() { let commits = Arc::new(AtomicUsize::new(0)); { let commits_clone = Arc::clone(&commits); - let conn = persister.lock_conn_for_test(); + let mut conn = persister.lock_conn_for_test(); conn.commit_hook(Some(move || { commits_clone.fetch_add(1, Ordering::SeqCst); false @@ -471,7 +471,7 @@ fn tc023_one_flush_is_one_transaction() { // Remove the hook so the persister is reusable elsewhere. { - let conn = persister.lock_conn_for_test(); + let mut conn = persister.lock_conn_for_test(); conn.commit_hook(None:: bool>) .expect("remove commit hook"); } @@ -515,7 +515,7 @@ fn install_commit_counter( use std::sync::Arc; let counter = Arc::new(std::sync::atomic::AtomicUsize::new(0)); let counter_clone = Arc::clone(&counter); - let conn = persister.lock_conn_for_test(); + let mut conn = persister.lock_conn_for_test(); conn.commit_hook(Some(move || { counter_clone.fetch_add(1, Ordering::SeqCst); false diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_load_reconstruction.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_load_reconstruction.rs index b473251becb..51343313cb8 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_load_reconstruction.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_load_reconstruction.rs @@ -1488,19 +1488,24 @@ fn tc_p4_012_load_query_count_bounded() { // Hold the conn briefly to install the trace, then drop the // guard before calling load() (load takes its own lock). { - let conn = persister.lock_conn_for_test(); + let mut conn = persister.lock_conn_for_test(); conn.trace_v2( rusqlite::trace::TraceEventCodes::SQLITE_TRACE_STMT, Some(cb), - ); + ) + .expect("install trace"); } COUNTER.store(0, Ordering::Relaxed); persister.load().expect("load"); let n = COUNTER.load(Ordering::Relaxed); // Disable trace so other tests don't accidentally inherit it. { - let conn = persister.lock_conn_for_test(); - conn.trace_v2(rusqlite::trace::TraceEventCodes::SQLITE_TRACE_STMT, None); + let mut conn = persister.lock_conn_for_test(); + conn.trace_v2( + rusqlite::trace::TraceEventCodes::SQLITE_TRACE_STMT, + None::)>, + ) + .expect("remove trace"); } n } diff --git a/packages/rs-platform-wallet/Cargo.toml b/packages/rs-platform-wallet/Cargo.toml index 7da2bb5ce6c..b47c5662fa4 100644 --- a/packages/rs-platform-wallet/Cargo.toml +++ b/packages/rs-platform-wallet/Cargo.toml @@ -59,7 +59,7 @@ grovedb-commitment-tree = { workspace = true, optional = true } # WAL + synchronous=NORMAL pragmas before handing the connection to # `ClientPersistentCommitmentTree`. Version locked to match the rev grovedb # is built against (its `Cargo.lock` resolves rusqlite at this major). -rusqlite = { version = "0.38", optional = true } +rusqlite = { version = "0.41", optional = true } zip32 = { version = "0.2.0", default-features = false, optional = true } # `StreamExt`/`pin_mut!` to consume the SDK's `sync_shielded_notes_stream` # in the interleaved fetch/tree-append consumer (shielded sync only). @@ -108,7 +108,7 @@ rayon = "1.10" # Used by `shielded_tree_append_bench.rs` to open SQLite with tuned PRAGMAs # (WAL vs DELETE, sync mode) — confirms whether journal mode is what's # making the simulator path so slow. -rusqlite = "0.38" +rusqlite = "0.41" static_assertions = "1.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } # `test-util` brings `tokio::time::pause` / `tokio::test(start_paused = true)` From 7a7e310ffb702e18c57a97f926e301a88317a8d3 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 02:20:39 +0700 Subject: [PATCH 4/6] fix(drive): follow the written owner only in batches updating documents Applying the rule to every batch at protocol version 15 moved a contract suspension to the moderator replacing it. Other batches keep the old merge. Also compares a chained page's merged query with the merge of grove version 4: from version 5 that merge refuses a global limit over subqueries. Co-Authored-By: Claude Opus 5.5 --- book/src/fees/overview.md | 6 +++-- .../v0/tests/chained_query_e2e_tests.rs | 16 +++++++++---- .../apply_drive_operations/v2/mod.rs | 6 ++--- .../src/util/batch/drive_op_batch/mod.rs | 21 ++++++++++------- .../src/util/batch/grovedb_op_batch/mod.rs | 10 ++++---- .../v1/mod.rs | 23 +++++++++++-------- .../mod.rs | 14 +++++------ .../v1/mod.rs | 4 ++-- .../drive_grove_method_versions/v2.rs | 6 ++--- .../rs-platform-version/src/version/v15.rs | 4 ++-- 10 files changed, 65 insertions(+), 45 deletions(-) diff --git a/book/src/fees/overview.md b/book/src/fees/overview.md index 4cad5419b21..c0ba80481db 100644 --- a/book/src/fees/overview.md +++ b/book/src/fees/overview.md @@ -252,8 +252,10 @@ back only the part not yet paid out to past epochs, so a document type that offers sponsorship should bound its documents' size (`maxLength`, `maxItems`, `maxBytes`) and price the action accordingly. -From protocol version 15 the owner a write names owns the element afterwards, -at any size. What happens to the bytes depends on who is involved: +From protocol version 15 the owner a document update names owns the element +afterwards, at any size. Other writes, such as a moderator replacing a contract +suspension, keep the old rules. What happens to the bytes depends on who is +involved: - When the old or new owner is the owner of a contract whose document type the batch updates offers sponsorship, the handover is full: the old owner is diff --git a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/chained_query_e2e_tests.rs b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/chained_query_e2e_tests.rs index c1c4247894b..f5ab4542779 100644 --- a/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/chained_query_e2e_tests.rs +++ b/packages/rs-drive/src/drive/contract/insert/insert_contract/v0/tests/chained_query_e2e_tests.rs @@ -40,6 +40,7 @@ use dpp::platform_value::{Identifier, Value}; use dpp::prelude::DataContract; use dpp::tests::json_document::json_document_to_contract; use grovedb::{GroveDb, PathQuery}; +use grovedb_version::version::{v4::GROVE_V4, GroveVersion}; use std::collections::BTreeMap; const POST_A: [u8; 32] = [0xA1; 32]; @@ -1323,8 +1324,10 @@ fn chained_posts_tipped_by<'a>( /// For a page with rows, the merged query the proof covers is the one the /// merge built when the inner half still carried its limit as a global -/// one: grovedb's merge lifts a global limit into the same per-instance -/// cap, so such a page proves to the same bytes as before. +/// one: before grove version 5, grovedb's merge lifted a global limit into +/// the same per-instance cap, so such a page proves to the same bytes. From +/// version 5 that merge refuses a global limit over subqueries, so the old +/// form is only built at version 4. fn assert_merged_query_matches_the_lifted_global_limit( chained: &DriveDocumentQuery, join_values: &[Identifier], @@ -1341,11 +1344,14 @@ fn assert_merged_query_matches_the_lifted_global_limit( lifted[0].query.limit.is_some(), "the plain lowering carries the limit as a global one" ); - let merge = |path_queries: &[PathQuery]| { - PathQuery::merge(path_queries.iter().collect(), &pv.drive.grove_version) + let merge = |path_queries: &[PathQuery], grove_version: &GroveVersion| { + PathQuery::merge(path_queries.iter().collect(), grove_version) .expect("the components merge") }; - assert_eq!(merge(&components), merge(&lifted)); + assert_eq!( + merge(&components, &pv.drive.grove_version), + merge(&lifted, &GROVE_V4) + ); } /// Reads `chained` without and with a proof, verifies the proof, checks diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index 15be4da6bda..6a3378ea352 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -132,7 +132,7 @@ impl Drive { let forfeits_storage_refunds = operations .iter() .any(DriveOperation::forfeits_storage_refunds); - let full_handover_owners = DriveOperation::full_handover_owners(&operations)?; + let owner_handover = DriveOperation::owner_handover(&operations)?; // The document operations of a moderator's deletion go in a batch of their own only // when something else in the batch may free bytes someone is owed. let separates_forfeited_operations = forfeits_storage_refunds @@ -219,11 +219,11 @@ impl Drive { } else { Some(estimated_costs_only_with_layer_info.clone()) }; - self.apply_batch_low_level_drive_operations_with_full_handover_owners( + self.apply_batch_low_level_drive_operations_with_owner_handover( estimated_costs_only_with_layer_info, transaction, low_level_operations, - full_handover_owners, + owner_handover, &mut cost_operations, &platform_version.drive, )?; 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 261abd18316..43bcb1ac3ee 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 @@ -282,16 +282,21 @@ impl DriveLowLevelOperationConverter for DriveOperation<'_> { } impl DriveOperation<'_> { - /// The identities for which an owner change in this batch is a full handover: the owners - /// of the contracts offering gas sponsorship on the documents the batch updates (protocol - /// version 15). A batch naming none is estimated and applied as it always was. - pub(crate) fn full_handover_owners( + /// The batch's owner handover (protocol version 15): `None` for a batch that updates no + /// document, and otherwise the owners of the contracts offering gas sponsorship on the + /// documents it updates, for whom an owner change is a full handover. + pub(crate) fn owner_handover( operations: &[DriveOperation], - ) -> Result, Error> { - let mut owners = BTreeSet::new(); + ) -> Result>, Error> { + let mut owners = None; for operation in operations { - if let DriveOperation::DocumentOperation(document_operation) = operation { - owners.extend(document_operation.storage_sponsor_of_updated_document()?); + if let DriveOperation::DocumentOperation( + document_operation @ DocumentOperationType::UpdateDocument { .. }, + ) = operation + { + owners + .get_or_insert_with(BTreeSet::new) + .extend(document_operation.storage_sponsor_of_updated_document()?); } } Ok(owners) 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 963e7e92f87..c282361591f 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 @@ -33,10 +33,12 @@ use std::fmt; pub struct GroveDbOpBatch { /// Operations pub(crate) operations: Vec, - /// Identities for which an owner change is a full handover rather than a change of the - /// stored bytes' owner: the owners of the contracts offering gas sponsorship on what the - /// batch updates. Empty before protocol version 15. - pub(crate) full_handover_owners: BTreeSet<[u8; 32]>, + /// How the batch's flags update treats a new owner, set on a batch that updates documents + /// from protocol version 15. `None` merges flags as they always were. Otherwise the owner + /// a write names owns the element afterwards, and an owner change involving one of these + /// identities, the owners of contracts offering gas sponsorship on what the batch + /// updates, is a full handover. + pub(crate) owner_handover: Option>, } #[derive(Debug, PartialEq, Eq, Copy, Clone)] diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs index 4314bdc3f7e..f0ecf84ed42 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs @@ -12,8 +12,9 @@ use platform_version::version::drive_versions::DriveVersion; use std::collections::BTreeSet; impl Drive { - /// Generation 0 with a flags update that follows the owner the written flags name: see - /// [`update_element_flags_for_owner_change`]. + /// Generation 0, except that a batch with an owner handover follows the owner the written + /// flags name: see [`update_element_flags_for_owner_change`]. Any other batch merges flags + /// as generation 0 does. pub(super) fn grove_apply_batch_with_add_costs_v1( &self, ops: GroveDbOpBatch, @@ -22,17 +23,21 @@ impl Drive { drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { - let full_handover_owners = ops.full_handover_owners.clone(); + let owner_handover = ops.owner_handover.clone(); self.grove_apply_batch_with_flags_update( ops, validate, |cost, old_flags, new_flags| { - update_element_flags_for_owner_change( - cost, - old_flags, - new_flags, - &full_handover_owners, - ) + match &owner_handover { + Some(full_handover_owners) => update_element_flags_for_owner_change( + cost, + old_flags, + new_flags, + full_handover_owners, + ), + None => StorageFlags::update_element_flags(cost, old_flags, new_flags) + .map(ElementFlagsUpdate::from), + } .map_err(|e| GroveError::JustInTimeElementFlagsClientError(e.to_string())) }, transaction, diff --git a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs index 73b74a32b8c..6f3c896025e 100644 --- a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs +++ b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/mod.rs @@ -40,24 +40,24 @@ impl Drive { drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { - self.apply_batch_low_level_drive_operations_with_full_handover_owners( + self.apply_batch_low_level_drive_operations_with_owner_handover( estimated_costs_only_with_layer_info, transaction, batch_operations, - BTreeSet::new(), + None, drive_operations, drive_version, ) } - /// [`Self::apply_batch_low_level_drive_operations`], for a batch where an owner change - /// involving one of `full_handover_owners` is a full handover (protocol version 15). - pub(crate) fn apply_batch_low_level_drive_operations_with_full_handover_owners( + /// [`Self::apply_batch_low_level_drive_operations`], with the batch's `owner_handover` + /// (see [`GroveDbOpBatch`](crate::util::batch::GroveDbOpBatch)). + pub(crate) fn apply_batch_low_level_drive_operations_with_owner_handover( &self, estimated_costs_only_with_layer_info: Option, transaction: TransactionArg, batch_operations: Vec, - full_handover_owners: BTreeSet<[u8; 32]>, + owner_handover: Option>, drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { @@ -87,7 +87,7 @@ impl Drive { estimated_costs_only_with_layer_info, transaction, batch_operations, - full_handover_owners, + owner_handover, drive_operations, drive_version, ) diff --git a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs index fa2ed632c93..531520e257c 100644 --- a/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs +++ b/packages/rs-drive/src/util/operations/apply_batch_low_level_drive_operations/v1/mod.rs @@ -21,7 +21,7 @@ impl Drive { estimated_costs_only_with_layer_info: Option, transaction: TransactionArg, mut batch_operations: Vec, - full_handover_owners: BTreeSet<[u8; 32]>, + owner_handover: Option>, drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { @@ -31,7 +31,7 @@ impl Drive { batch_operations, ); // Ephemeral operations carry no storage flags, so only the ordinary batch needs them. - grove_db_operations.full_handover_owners = full_handover_owners; + grove_db_operations.owner_handover = owner_handover; // Ephemeral operations (a `timeRange` index's TTL'd sub-levels, the // writes of a document whose type declares a `ttl`) apply as their // own batch per pricing rule, so each batch's cost is known diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs index 7f377b3474f..4323ada7368 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_grove_method_versions/v2.rs @@ -4,9 +4,9 @@ use crate::version::drive_versions::drive_grove_method_versions::{ }; /// V2 is protocol version 15's GroveDB method table. Relative to V1, -/// `apply.grove_apply_batch` is 1: an element keeps its stored bytes under the owner the -/// written flags name, and a batch naming a gas sponsor accounts that sponsor's owner changes -/// as replacements (see protocol version 15's notes). Estimates do not change: a document +/// `apply.grove_apply_batch` is 1: in a batch updating documents, an element keeps its stored +/// bytes under the owner the written flags name, and a gas sponsor's owner changes are +/// accounted as replacements (see protocol version 15's notes). Estimates do not change: a document /// update is already estimated as the insertion of everything it writes. pub const DRIVE_GROVE_METHOD_VERSIONS_V2: DriveGroveMethodVersions = DriveGroveMethodVersions { apply: DriveGroveApplyMethodVersions { diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 30e8b4d2ef5..b298667a62e 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -568,8 +568,8 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// removal from an earlier epoch pays once per such epoch for reading its record, the only /// fee this changes; generation 0 reads nothing. /// -/// 28. **Storage handover**: when a write names a new owner on a stored element, the owner it -/// names owns the element afterwards at any size (`DRIVE_VERSION_V10`, +/// 28. **Storage handover**: when a batch updating documents names a new owner on a stored +/// element, the owner it names owns the element afterwards at any size (`DRIVE_VERSION_V10`, /// `DRIVE_GROVE_METHOD_VERSIONS_V2`). A transfer or a purchase keeps the stored bytes and /// moves them to the new owner, the references of the indexes it leaves unchanged included /// (`update_document_for_contract_operations` 2). A gas sponsor who pays is named as the From 8dfa3a11faaf45a2c84f0323726fdfb41a30a79a Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 05:44:25 +0700 Subject: [PATCH 5/6] fix(drive-abci): leave a document's storage with its holder when a moderator changes it A moderator's document field change was written in the name of the document's owner. From protocol version 15 a document a gas sponsor paid for is held by the contract owner, so that write was an owner change: the contract owner was refunded, the moderator was charged for the whole document, and its owner could then delete it and collect the refund. The contract user moderation `transform_into_action` 1 (protocol version 15 only) reads the stored document with its storage flags, and the action carries the owner they name. The change is written in that identity's name, so the moderator pays for the bytes the change adds and the document stays with whoever holds it. In place, and neutral before protocol version 15: the conversion names the document's owner when the action carries no holder, which is every action generation 0 builds; the field change transform takes a flag that generation 0 leaves off; the state validation runs the transformation the version tables select, which is generation 0 in every earlier table. Co-Authored-By: Claude --- .../state_transitions/batch/mod.rs | 2 + .../batch/state/v0/fetch_documents.rs | 65 ++++++ .../batch/tests/document/gas_sponsorship.rs | 206 ++++++++++++++++++ .../contract_user_moderation/mod.rs | 21 +- .../contract_user_moderation/state/mod.rs | 1 + .../contract_user_moderation/state/v0/mod.rs | 114 +++++++--- .../contract_user_moderation/state/v1/mod.rs | 104 +++++++++ .../contract_user_moderation_transition.rs | 79 ++++++- .../contract_user_moderation/v0/mod.rs | 6 + .../drive_abci_validation_versions/v11.rs | 14 ++ .../rs-platform-version/src/version/v15.rs | 8 +- 11 files changed, 576 insertions(+), 44 deletions(-) create mode 100644 packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v1/mod.rs diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/mod.rs index d46e09a61df..20d2fad4500 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/mod.rs @@ -10,6 +10,8 @@ mod transformer; // A moderator's document deletion (`contract_user_moderation`) reads the document the way a // document's own deletion does, billed the same. pub(in crate::execution::validation::state_transition) use state::v0::fetch_documents::fetch_document_with_id; +// A moderator's field change (protocol version 15) also reads who holds the document's storage. +pub(in crate::execution::validation::state_transition) use state::v0::fetch_documents::fetch_document_with_id_and_storage_flags; // Check tx reserves, for a create it does not validate the state of, what the block's // `chooseFirst` check may bill it: its two hashes and the competitor scan's budget. pub(in crate::execution::validation::state_transition) use action_validation::document::document_reference_validation::v0::choose_first_reservation; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/state/v0/fetch_documents.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/state/v0/fetch_documents.rs index aba579397bb..33981ecea5c 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/state/v0/fetch_documents.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/state/v0/fetch_documents.rs @@ -20,12 +20,14 @@ use dpp::validation::ConsensusValidationResult; use dpp::version::PlatformVersion; use drive::drive::document::query::query_contested_documents_storage::QueryContestedDocumentsOutcomeV0Methods; use drive::drive::document::query::QueryDocumentsOutcomeV0Methods; +use drive::drive::document::query::QueryDocumentsWithFlagsOutcomeV0Methods; use drive::drive::Drive; use drive::grovedb::TransactionArg; use drive::query::drive_contested_document_query::{ DriveContestedDocumentQuery, PrimaryContestedInternalClauses, }; use drive::query::{DriveDocumentQuery, InternalClauses, OrderClause, WhereClause, WhereOperator}; +use drive::util::storage_flags::StorageFlags; use std::collections::BTreeMap; // ============================================================================ @@ -412,6 +414,69 @@ fn fetch_document_with_id_v1( } } +// ============================================================================ +// fetch_document_with_id_and_storage_flags +// ============================================================================ + +/// `fetch_document_with_id` for a caller that also needs the storage flags of the document's +/// stored element, which name who holds its storage: the same query by `$id`, read through +/// `query_documents_with_flags` and billed the same way. +/// +/// Only protocol version 15 code calls it (a moderator's field change, which is written in +/// the name of whoever holds the document), so it has one generation. +#[allow(clippy::too_many_arguments)] +pub(crate) fn fetch_document_with_id_and_storage_flags( + drive: &Drive, + contract: &DataContract, + document_type: DocumentTypeRef, + id: Identifier, + epoch: &Epoch, + execution_context: &mut StateTransitionExecutionContext, + transaction: TransactionArg, + platform_version: &PlatformVersion, +) -> Result)>, Error> { + let drive_query = DriveDocumentQuery { + contract, + document_type, + internal_clauses: InternalClauses { + primary_key_in_clause: None, + primary_key_equal_clause: Some(WhereClause { + field: "$id".to_string(), + operator: WhereOperator::Equal, + value: Value::Identifier(id.to_buffer()), + }), + in_clauses: Vec::new(), + range_clause: None, + equal_clauses: Default::default(), + }, + offset: None, + limit: Some(1), + order_by: Default::default(), + start_at: None, + start_at_included: false, + block_time_ms: None, + resolved_time_ranges: vec![], + sub_queries: vec![], + }; + + let documents_outcome = drive.query_documents_with_flags( + drive_query, + Some(epoch), + false, + transaction, + Some(platform_version.protocol_version), + )?; + execution_context.add_operation(ValidationOperation::PrecalculatedOperation(FeeResult { + storage_fee: 0, + processing_fee: documents_outcome.cost(), + fee_refunds: Default::default(), + removed_bytes_from_system: 0, + lifetime_storage_fees: Default::default(), + })); + + Ok(documents_outcome.documents_owned().into_iter().next()) +} + // ============================================================================ // fetch_document_through_lookup // ============================================================================ diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs index db7b65340e8..cb9b9a8ba6f 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs @@ -24,6 +24,9 @@ pub(crate) mod gas_sponsorship_tests { use dpp::consensus::codes::ErrorWithCode; use dpp::data_contract::accessors::v0::DataContractV0Setters; use dpp::data_contract::accessors::v1::DataContractV1Setters; + use dpp::data_contract::config::moderation::{ + ContractModerationConfig, ContractModerationReason, ContractModerators, + }; use dpp::data_contract::document_type::accessors::{ DocumentTypeV0MutGetters, DocumentTypeV1Getters, DocumentTypeV1Setters, }; @@ -41,6 +44,10 @@ pub(crate) mod gas_sponsorship_tests { use dpp::prelude::{Identifier, IdentityNonce, Revision, UserFeeIncrease}; use dpp::state_transition::batch_transition::batched_transition::document_transition_action_type::DocumentTransitionActionType; use dpp::state_transition::batch_transition::methods::StateTransitionCreationOptions; + use dpp::state_transition::contract_user_moderation_transition::methods::ContractUserModerationTransitionMethodsV0; + use dpp::state_transition::contract_user_moderation_transition::{ + ContractUserModerationAction, ContractUserModerationTransition, + }; use dpp::state_transition::StateTransition; use dpp::tokens::calculate_token_id; use dpp::tokens::gas_fees_paid_by::GasFeesPaidBy; @@ -55,6 +62,7 @@ pub(crate) mod gas_sponsorship_tests { use drive::util::storage_flags::StorageFlags; use drive::util::test_helpers::setup_contract; use simple_signer::signer::SimpleSigner; + use std::collections::BTreeMap; const GAS_FEES_PAID_BY_NOT_ALLOWED: u32 = 40129; pub(crate) const GAS_SPONSOR_INSUFFICIENT_BALANCE: u32 = 40222; @@ -1935,6 +1943,204 @@ pub(crate) mod gas_sponsorship_tests { ); } + /// The card game with a `grade` on its cards that only moderators write, and the moderator + /// its contract appoints: an identity that is neither the contract owner nor the user + fn moderated_card_game() -> (Sponsorship, Identity, SimpleSigner) { + let (moderator, moderator_signer, _) = + setup_identity_without_adding_it(771, dash_to_credits!(0.1)); + let moderator_id = moderator.id(); + let setup = Sponsorship::build_customized( + PlatformVersion::latest(), + GasFeesPaidBy::ContractOwner, + false, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + None, + |contract| { + contract.set_config(contract.config().clone().with_moderation(Some( + ContractModerationConfig { + banlist: false, + suspensions: false, + warnings: false, + moderators: ContractModerators::AppointedModerators( + [moderator_id].into_iter().collect(), + ), + }, + ))); + let mut schema = contract + .document_type_for_name("card") + .expect("expected the card document type") + .schema() + .clone(); + schema + .set_value( + "moderatorAbilities", + platform_value!({ "changeFields": ["grade"] }), + ) + .expect("expected to let moderators change the grade"); + schema + .get_mut("properties") + .expect("expected to get the properties") + .expect("expected the card type to have properties") + .set_value( + "grade", + platform_value!({ + "type": "integer", + "minimum": 0, + "maximum": 10, + "position": 7, + }), + ) + .expect("expected to add the grade"); + contract + .set_document_schema( + "card", + schema, + false, + &mut vec![], + PlatformVersion::latest(), + ) + .expect("expected the card type to take a grade"); + }, + ); + setup + .platform + .drive + .add_new_identity( + moderator.clone(), + false, + &BlockInfo::default(), + true, + None, + setup.platform_version, + ) + .expect("expected to add the moderator"); + (setup, moderator, moderator_signer) + } + + /// Processes the moderator's grading of the user's card and commits it. Returns what it + /// refunded the user and the contract owner, after checking that the moderator paid the + /// fee and that nobody else was charged. + async fn grade_the_card( + setup: &Sponsorship, + moderator: &Identity, + moderator_signer: &SimpleSigner, + ) -> (Option, Option) { + let (card, _) = setup.card_of(&setup.user); + let grading = ContractUserModerationTransition::try_from_identity_with_signer( + moderator, + &1, + setup.contract.id(), + ContractUserModerationAction::ChangeDocumentFields { + document_type_name: "card".to_string(), + document_id: card.id(), + fields: BTreeMap::from([("grade".to_string(), Value::U64(9))]), + reason: ContractModerationReason::from_text("graded"), + }, + 1, + 0, + moderator_signer, + setup.platform_version, + None, + ) + .await + .expect("expected to build the moderation transition"); + + let credits = setup.committed_credits(); + let moderator_credits = { + let tx = setup.platform.drive.grove.start_transaction(); + setup.credits(moderator, &tx) + }; + let result = setup.process_and_commit(&grading); + let SuccessfulExecution { fee_result, .. } = &result else { + panic!("expected the moderator's change to execute, got {result:?}"); + }; + let refunded_to_user = fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.user.id()); + let refunded_to_contract_owner = fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.contract_owner.id()); + assert_eq!( + setup.committed_credits(), + ( + credits.0 + refunded_to_user.unwrap_or_default(), + credits.1 + refunded_to_contract_owner.unwrap_or_default(), + ), + "neither the user nor the contract owner is charged for a moderator's change" + ); + let tx = setup.platform.drive.grove.start_transaction(); + assert_eq!( + setup.credits(moderator, &tx), + moderator_credits - fee_result.total_base_fee(), + "the moderator pays for their change" + ); + (refunded_to_user, refunded_to_contract_owner) + } + + #[tokio::test] + async fn should_leave_a_sponsored_card_with_the_contract_owner_when_a_moderator_changes_it() { + // A moderator pays for what their change adds and owns nothing: the card stays with the + // contract owner who paid for it. Naming the card's owner instead would refund the + // contract owner, bill the moderator the whole card, and leave it for the user to + // delete and collect. + let (setup, moderator, moderator_signer) = moderated_card_game(); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + assert_eq!( + setup.stored_card_storage_owner(), + Some(setup.contract_owner.id()) + ); + + let (refunded_to_user, refunded_to_contract_owner) = + grade_the_card(&setup, &moderator, &moderator_signer).await; + assert_eq!(refunded_to_user, None); + assert_eq!( + refunded_to_contract_owner, None, + "the contract owner keeps the card, so nothing of it is refunded" + ); + assert_eq!( + setup.stored_card_storage_owner(), + Some(setup.contract_owner.id()) + ); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.contract_owner.id()) + ); + + let deletion = setup.card_deletion(3).await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &deletion, &block, false); + assert_eq!(refunded_to_user, None, "the user is refunded nothing"); + assert!( + refunded_to_contract_owner.is_some_and(|refund| refund > 0), + "the contract owner is refunded the card they paid for" + ); + } + + #[tokio::test] + async fn should_leave_a_card_its_user_paid_for_with_the_user_when_a_moderator_changes_it() { + let (setup, moderator, moderator_signer) = moderated_card_game(); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &block, false); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + + let (refunded_to_user, refunded_to_contract_owner) = + grade_the_card(&setup, &moderator, &moderator_signer).await; + assert_eq!(refunded_to_user, None); + assert_eq!(refunded_to_contract_owner, None); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + } + #[tokio::test] async fn should_refund_the_user_who_paid_when_the_contract_owner_could_not() { // A preference the contract owner cannot honour falls back to the user, who then paid diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/mod.rs index 3a10bc33919..b8156559c4f 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/mod.rs @@ -26,6 +26,7 @@ use crate::rpc::core::CoreRPCLike; use crate::execution::validation::state_transition::contract_user_moderation::basic_structure::v0::ContractUserModerationStateTransitionStructureValidationV0; use crate::execution::validation::state_transition::contract_user_moderation::state::v0::ContractUserModerationStateTransitionStateValidationV0; +use crate::execution::validation::state_transition::contract_user_moderation::state::v1::ContractUserModerationStateTransitionStateValidationV1; use crate::execution::validation::state_transition::processor::basic_structure::StateTransitionBasicStructureValidationV0; use crate::execution::validation::state_transition::processor::state::StateTransitionStateValidation; use crate::execution::validation::state_transition::transformer::StateTransitionActionTransformer; @@ -60,9 +61,16 @@ impl StateTransitionActionTransformer for ContractUserModerationTransition { tx, platform_version, ), + 1 => self.transform_into_action_v1( + platform, + block_info, + execution_context, + tx, + platform_version, + ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "contract user moderation transition: transform_into_action".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -101,7 +109,7 @@ impl StateTransitionStateValidation for ContractUserModerationTransition { &self, _action: Option, platform: &PlatformRef, - _validation_mode: ValidationMode, + validation_mode: ValidationMode, block_info: &BlockInfo, execution_context: &mut StateTransitionExecutionContext, tx: TransactionArg, @@ -115,13 +123,16 @@ impl StateTransitionStateValidation for ContractUserModerationTransition { .state { // The state validation is the transformation: the contract and the target's - // status are read and checked while the action that carries them is built. - 0 => self.transform_into_action_v0( + // status are read and checked while the action that carries them is built. So it + // runs the generation of the transformation the version tables select, which at + // every protocol version before 15 is generation 0. + 0 => self.transform_into_action( platform, block_info, + &None, + validation_mode, execution_context, tx, - platform_version, ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "contract user moderation transition: validate_state".to_string(), diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/mod.rs index 9a1925de7fc..008be12cc67 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/mod.rs @@ -1 +1,2 @@ pub(crate) mod v0; +pub(crate) mod v1; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v0/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v0/mod.rs index 0b96711388d..a43f6d018fc 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v0/mod.rs @@ -13,7 +13,8 @@ use crate::execution::validation::state_transition::common::seated_moderation_ch use crate::execution::validation::state_transition::common::validate_document_not_expired::validate_document_not_expired; use crate::execution::validation::state_transition::common::validate_identity_exists::validate_identity_exists; use crate::execution::validation::state_transition::state_transitions::batch::{ - fetch_document_with_id, read_property_constraint_aggregates_for_moderator_change, + fetch_document_with_id, fetch_document_with_id_and_storage_flags, + read_property_constraint_aggregates_for_moderator_change, read_property_constraint_aggregates_for_restore, }; use crate::platform_types::platform::PlatformRef; @@ -132,25 +133,14 @@ impl ContractUserModerationStateTransitionStateValidationV0 for ContractUserMode let moderator_id = self.owner_id(); let action = self.action(); - let (contract_fetch_fee, maybe_contract_fetch_info) = - platform.drive.get_contract_with_fetch_info_and_fee( - contract_id.to_buffer(), - Some(&block_info.epoch), - false, - tx, - platform_version, - )?; - // The read is billed from the fee this call returns, whether the contract was pulled - // from disk, was in the cache or does not exist. The fee a cached fetch info carries is - // only there when that entry was built with an epoch, which differs from node to node, - // so billing it would make the fee, and the app hash, depend on the cache. - let contract_fetch_fee = - contract_fetch_fee.ok_or(Error::Execution(ExecutionError::CorruptedCodeExecution( - "fee must exist for the contract fetch of a contract user moderation transition", - )))?; - execution_context.add_operation(ValidationOperation::PrecalculatedOperation( - contract_fetch_fee, - )); + let maybe_contract_fetch_info = fetch_moderated_contract( + contract_id, + platform, + block_info, + execution_context, + tx, + platform_version, + )?; let bump_action = || { StateTransitionAction::BumpIdentityDataContractNonceAction( @@ -252,6 +242,7 @@ impl ContractUserModerationStateTransitionStateValidationV0 for ContractUserMode document_type_name, *document_id, fields, + false, execution_context, tx, platform_version, @@ -401,6 +392,37 @@ impl ContractUserModerationStateTransitionStateValidationV0 for ContractUserMode } } +/// Reads the moderated contract and bills the read, for every generation of the transformation. +pub(super) fn fetch_moderated_contract( + contract_id: Identifier, + platform: &PlatformRef, + block_info: &BlockInfo, + execution_context: &mut StateTransitionExecutionContext, + tx: TransactionArg, + platform_version: &PlatformVersion, +) -> Result>, Error> { + let (contract_fetch_fee, maybe_contract_fetch_info) = + platform.drive.get_contract_with_fetch_info_and_fee( + contract_id.to_buffer(), + Some(&block_info.epoch), + false, + tx, + platform_version, + )?; + // The read is billed from the fee this call returns, whether the contract was pulled + // from disk, was in the cache or does not exist. The fee a cached fetch info carries is + // only there when that entry was built with an epoch, which differs from node to node, + // so billing it would make the fee, and the app hash, depend on the cache. + let contract_fetch_fee = + contract_fetch_fee.ok_or(Error::Execution(ExecutionError::CorruptedCodeExecution( + "fee must exist for the contract fetch of a contract user moderation transition", + )))?; + execution_context.add_operation(ValidationOperation::PrecalculatedOperation( + contract_fetch_fee, + )); + Ok(maybe_contract_fetch_info) +} + /// A document deletion: the document type exists and says moderators may delete its /// documents, the signer moderates the contract (see [`Moderators`]: a seated team must also /// hold `deleteDocuments` on the type), the document exists, its owner is not protected, and it @@ -1382,8 +1404,14 @@ fn document_deletion_context( /// among it, so that Drive stores it without reading again. A moderator's change is not the /// owner's modification: the window a type gives its moderators to delete a document is not /// opened again by it. +/// +/// `keeps_storage_holder` is what generation 1 of the transformation asks for and generation 0 +/// does not: the read of the stored document also returns who its storage flags name, and the +/// action carries that identity, so that Drive writes the change in its name and the storage +/// stays where it is. Without it the read is generation 0's and the action names nobody, which +/// writes the change in the name of the document's owner. #[allow(clippy::too_many_arguments)] -fn transform_document_fields_change_v0( +pub(super) fn transform_document_fields_change_v0( transition: &ContractUserModerationTransition, platform: &PlatformRef, block_info: &BlockInfo, @@ -1391,6 +1419,7 @@ fn transform_document_fields_change_v0( document_type_name: &str, document_id: Identifier, fields: &BTreeMap, + keeps_storage_holder: bool, execution_context: &mut StateTransitionExecutionContext, tx: TransactionArg, platform_version: &PlatformVersion, @@ -1485,17 +1514,37 @@ fn transform_document_fields_change_v0( return refuse(error); } - let Some(stored) = fetch_document_with_id( - platform.drive, - contract, - document_type, - document_id, - &block_info.epoch, - execution_context, - tx, - platform_version, - )? - else { + let stored = if keeps_storage_holder { + fetch_document_with_id_and_storage_flags( + platform.drive, + contract, + document_type, + document_id, + &block_info.epoch, + execution_context, + tx, + platform_version, + )? + .map(|(document, storage_flags)| { + let storage_holder_id = storage_flags + .and_then(|flags| flags.owner_id().copied()) + .map(Identifier::from); + (document, storage_holder_id) + }) + } else { + fetch_document_with_id( + platform.drive, + contract, + document_type, + document_id, + &block_info.epoch, + execution_context, + tx, + platform_version, + )? + .map(|document| (document, None)) + }; + let Some((stored, storage_holder_id)) = stored else { return refuse(ConsensusError::StateError( StateError::DocumentNotFoundError(DocumentNotFoundError::new(document_id)), )); @@ -1648,6 +1697,7 @@ fn transform_document_fields_change_v0( ContractDocumentChangeContext { data_contract_fetch_info: Arc::clone(contract_fetch_info), document: changed, + storage_holder_id, }, ); let moderation_action = moderators.count_for_signer( diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v1/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v1/mod.rs new file mode 100644 index 00000000000..6fe205bc6ae --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/state/v1/mod.rs @@ -0,0 +1,104 @@ +use crate::error::Error; +use crate::execution::types::state_transition_execution_context::StateTransitionExecutionContext; +use crate::execution::validation::state_transition::state_transitions::contract_user_moderation::state::v0::{ + fetch_moderated_contract, transform_document_fields_change_v0, + ContractUserModerationStateTransitionStateValidationV0, +}; +use crate::platform_types::platform::PlatformRef; +use crate::rpc::core::CoreRPCLike; +use dpp::block::block_info::BlockInfo; +use dpp::consensus::basic::document::DataContractNotPresentError; +use dpp::prelude::ConsensusValidationResult; +use dpp::state_transition::contract_user_moderation_transition::accessors::ContractUserModerationTransitionAccessorsV0; +use dpp::state_transition::contract_user_moderation_transition::{ + ContractUserModerationAction, ContractUserModerationTransition, +}; +use dpp::version::PlatformVersion; +use drive::grovedb::TransactionArg; +use drive::state_transition_action::system::bump_identity_data_contract_nonce_action::BumpIdentityDataContractNonceAction; +use drive::state_transition_action::StateTransitionAction; + +pub(in crate::execution::validation::state_transition::state_transitions::contract_user_moderation) trait ContractUserModerationStateTransitionStateValidationV1 +{ + fn transform_into_action_v1( + &self, + platform: &PlatformRef, + block_info: &BlockInfo, + execution_context: &mut StateTransitionExecutionContext, + tx: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error>; +} + +impl ContractUserModerationStateTransitionStateValidationV1 for ContractUserModerationTransition { + /// Version 1 (protocol version 15) transforms a document field change so that the change + /// leaves the document's storage with whoever holds it. The read of the stored document + /// also returns who its storage flags name, and the action carries that identity for Drive + /// to write the change in its name. + /// + /// Version 0 writes the change in the name of the document's owner. From protocol version + /// 15 a document a gas sponsor paid for is held by the contract owner, and a write naming + /// another owner hands such a document over: the contract owner would be refunded and the + /// moderator charged for the whole document, which its owner could then delete and collect. + /// + /// Every other action transforms through version 0, unchanged. + fn transform_into_action_v1( + &self, + platform: &PlatformRef, + block_info: &BlockInfo, + execution_context: &mut StateTransitionExecutionContext, + tx: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let ContractUserModerationAction::ChangeDocumentFields { + document_type_name, + document_id, + fields, + .. + } = self.action() + else { + return self.transform_into_action_v0( + platform, + block_info, + execution_context, + tx, + platform_version, + ); + }; + + // The contract is read, and its absence refused, as version 0 does for every action. + let contract_id = self.data_contract_id(); + let Some(contract_fetch_info) = fetch_moderated_contract( + contract_id, + platform, + block_info, + execution_context, + tx, + platform_version, + )? + else { + return Ok(ConsensusValidationResult::new_with_data_and_errors( + StateTransitionAction::BumpIdentityDataContractNonceAction( + BumpIdentityDataContractNonceAction::from_borrowed_contract_user_moderation_transition( + self, + ), + ), + vec![DataContractNotPresentError::new(contract_id).into()], + )); + }; + + transform_document_fields_change_v0( + self, + platform, + block_info, + &contract_fetch_info, + document_type_name, + *document_id, + fields, + true, + execution_context, + tx, + platform_version, + ) + } +} diff --git a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/contract/contract_user_moderation_transition.rs b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/contract/contract_user_moderation_transition.rs index 5b775a33cac..8e9fa55a7dc 100644 --- a/packages/rs-drive/src/state_transition_action/action_convert_to_operations/contract/contract_user_moderation_transition.rs +++ b/packages/rs-drive/src/state_transition_action/action_convert_to_operations/contract/contract_user_moderation_transition.rs @@ -317,17 +317,23 @@ impl DriveHighLevelOperationConverter for ContractUserModerationTransitionAction let ContractDocumentChangeContext { data_contract_fetch_info, document, + storage_holder_id, } = document_change .ok_or(Error::Drive(DriveError::CorruptedCodeExecution( "a document field change action must carry what its validation built", )))?; // The document is updated the way a replace updates it, every index and - // aggregate of its type included. Its storage flags name its owner, as a - // replace's do: the moderator pays for the bytes the change adds, and a - // refund of the document's storage stays the owner's, as it always was. + // aggregate of its type included. Its storage flags name whoever holds + // its storage: the holder the validation read (protocol version 15 on), + // or else its owner, as a replace's do. So the change hands the document + // to nobody: the moderator pays for the bytes the change adds, and a + // refund of the document's storage stays with whoever it was due to. The + // document's owner still keys its `$ownerId` index paths. let owner_id = document.owner_id(); - let storage_flags = - StorageFlags::new_single_epoch(epoch.index, Some(owner_id.to_buffer())); + let storage_flags = StorageFlags::new_single_epoch( + epoch.index, + Some(storage_holder_id.unwrap_or(owner_id).to_buffer()), + ); operations.push(DocumentOperation(DocumentOperationType::UpdateDocument { owned_document_info: OwnedDocumentInfo { document_info: DocumentOwnedInfo(( @@ -460,8 +466,12 @@ fn removal_record_size(removal: &ContractDocumentRemoval) -> Result #[cfg(test)] mod tests { use super::*; + use crate::drive::contract::DataContractFetchInfo; use dpp::data_contract::config::moderation::ContractModerationReason; + use dpp::document::{Document, DocumentV0}; use dpp::platform_value::Identifier; + use std::collections::BTreeMap; + use std::sync::Arc; fn action( action: ContractUserModerationAction, @@ -549,6 +559,65 @@ mod tests { )); } + #[test] + fn should_write_a_field_change_in_the_name_of_the_storage_holder_or_else_the_owner() { + let platform_version = PlatformVersion::latest(); + let epoch = Epoch::new(3).expect("epoch"); + let owner = Identifier::from([0xCC; 32]); + let holder = Identifier::from([0xDD; 32]); + + // Who the written storage flags name, and who keys the `$ownerId` index paths. + let written_for = |storage_holder_id: Option| { + let mut change = action( + ContractUserModerationAction::ChangeDocumentFields { + document_type_name: "domain".to_string(), + document_id: Identifier::from([0x77; 32]), + fields: BTreeMap::new(), + reason: ContractModerationReason::from_text("handled"), + }, + false, + ); + let ContractUserModerationTransitionAction::V0(v0) = &mut change; + v0.document_change = Some(ContractDocumentChangeContext { + data_contract_fetch_info: Arc::new(DataContractFetchInfo::dpns_contract_fixture( + platform_version.protocol_version, + )), + document: Document::V0(DocumentV0 { + id: Identifier::from([0x77; 32]), + owner_id: owner, + ..Default::default() + }), + storage_holder_id, + }); + let ops = change + .into_high_level_drive_operations(&epoch, platform_version) + .expect("operations"); + let Some(DocumentOperation(DocumentOperationType::UpdateDocument { + owned_document_info: + OwnedDocumentInfo { + document_info: DocumentOwnedInfo((_, Some(storage_flags))), + owner_id: index_owner_id, + }, + .. + })) = ops.into_iter().last() + else { + panic!("a field change ends with the document's update"); + }; + (storage_flags.owner_id().copied(), index_owner_id) + }; + + // The holder the validation read is named, so the storage stays with it. + assert_eq!( + written_for(Some(holder)), + (Some(holder.to_buffer()), Some(owner.to_buffer())) + ); + // Without one (every version before protocol version 15) the owner is named, as before. + assert_eq!( + written_for(None), + (Some(owner.to_buffer()), Some(owner.to_buffer())) + ); + } + #[test] fn should_bump_the_contract_nonce_then_edit_the_list() { let platform_version = PlatformVersion::latest(); diff --git a/packages/rs-drive/src/state_transition_action/contract/contract_user_moderation/v0/mod.rs b/packages/rs-drive/src/state_transition_action/contract/contract_user_moderation/v0/mod.rs index 0b0e64dbab8..faaef830f8e 100644 --- a/packages/rs-drive/src/state_transition_action/contract/contract_user_moderation/v0/mod.rs +++ b/packages/rs-drive/src/state_transition_action/contract/contract_user_moderation/v0/mod.rs @@ -136,4 +136,10 @@ pub struct ContractDocumentChangeContext { /// the document as it will be stored: the stored one with the fields the transition sets, /// its revision one higher, everything else as it was pub document: Document, + /// who holds the stored document's storage, when the validation read it: the owner its + /// storage flags name, which is the contract owner for a document a gas sponsor paid for. + /// The change is written in that identity's name, so the storage stays with it. `None` + /// writes it in the name of the document's owner, as every change before protocol + /// version 15 is written, and as one is when the stored flags name nobody + pub storage_holder_id: Option, } 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 b2a81decc65..152b44a6ea1 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 @@ -54,6 +54,14 @@ use super::{ /// (`query_documents_with_flags`) and keeps that read in the `ConsumedDocument`, so the create /// conversion 1 deletes the commitment from it without reading it again. Any other lookup reads /// as v0's does. +/// - the contract user moderation `transform_into_action`, 1 rather than 0: a moderator's +/// document field change reads the stored document with its stored element's storage flags +/// (`query_documents_with_flags`), and the action carries the owner they name, so that the +/// change is written in that identity's name and the document's storage stays with whoever +/// holds it. v0 writes the change in the name of the document's owner, which from protocol +/// version 15 would hand a document a gas sponsor paid for over to its owner at the +/// moderator's cost. Every other moderation action transforms as v0's does. The state +/// validation, which is the transformation, runs the same generation. /// /// - the `PrivateDocumentCreateFromShieldedPool` (type 29) `basic_structure`, `Some(0)` rather /// than `None`: the transition, new at protocol version 15, has its entries, value balance, @@ -82,6 +90,12 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V11: DriveAbciValidationVersions = .state_transitions .masternode_vote_state_transition }, + contract_user_moderation_state_transition: DriveAbciStateTransitionValidationVersion { + transform_into_action: 1, // a moderator's field change reads who holds the document's storage + ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 + .state_transitions + .contract_user_moderation_state_transition + }, contract_update_state_transition: DriveAbciStateTransitionValidationVersion { transform_into_action: 1, // check_tx holds the update to the compiled size cap ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index b298667a62e..6c8dd077561 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -578,14 +578,18 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// old owner is refunded for the whole element and the payer charged for all of it, so a /// document's owner cannot keep storage the sponsor paid for. A poll's end date entries /// name whoever the document's flags name -/// (`add_contested_document_for_contract_operations` 2). +/// (`add_contested_document_for_contract_operations` 2). A moderator's document field +/// change hands the document to nobody: it is written in the name of whoever the stored +/// document's flags name (contract user moderation `transform_into_action` 1, +/// `DRIVE_ABCI_VALIDATION_VERSIONS_V11`), so the moderator pays for the bytes the change +/// adds and a document a sponsor paid for stays the sponsor's. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, drive: DRIVE_VERSION_V10, // changed: calculate_fee 1 prices a refund at the storing epoch's rate (note 27); 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) - validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V11, // changed: identity update advanced_structure 1 refuses unpaid an identity update whose added key's proof of possession does not verify (note 2); create_domain_data_trigger 2 (note 3); contract update transform_into_action 1 holds an update to the compiled size cap in check_tx (note 8); batch advanced_structure 2 refuses a token config update on a read-only contract (note 9); masternode vote transform_into_action 1 looks up a masternode's previous answer on a yes/no poll (note 10); document_reference_validation 1 keeps what a consumed commitment's lookup read (note 11); document create structure_validation 2 holds a decision proposal to its fund, rule and end date (note 12); data trigger bindings 3 refuse a replace or a delete of a token market order (note 17) and drop the DashPay bindings (note 24); document create state_validation 3 keeps a create outside a contest off the values of a live one its index values do not decide (note 19); identity create basic_structure 1 checks the identity id before the asset lock proof and advanced_structure 2 no longer does (note 23) + validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V11, // changed: identity update advanced_structure 1 refuses unpaid an identity update whose added key's proof of possession does not verify (note 2); create_domain_data_trigger 2 (note 3); contract update transform_into_action 1 holds an update to the compiled size cap in check_tx (note 8); batch advanced_structure 2 refuses a token config update on a read-only contract (note 9); masternode vote transform_into_action 1 looks up a masternode's previous answer on a yes/no poll (note 10); document_reference_validation 1 keeps what a consumed commitment's lookup read (note 11); document create structure_validation 2 holds a decision proposal to its fund, rule and end date (note 12); data trigger bindings 3 refuse a replace or a delete of a token market order (note 17) and drop the DashPay bindings (note 24); document create state_validation 3 keeps a create outside a contest off the values of a live one its index values do not decide (note 19); identity create basic_structure 1 checks the identity id before the asset lock proof and advanced_structure 2 no longer does (note 23); contract user moderation transform_into_action 1 writes a moderator's field change in the name of whoever holds the document's storage (note 28) withdrawal_constants: DRIVE_ABCI_WITHDRAWAL_CONSTANTS_V3, query: DRIVE_ABCI_QUERY_VERSIONS_V2, checkpoints: DRIVE_ABCI_CHECKPOINT_PARAMETERS_V1, From 3c1762ce5ced7366718447164cd932f9db8e877c Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Tue, 10 Nov 2026 06:35:13 +0700 Subject: [PATCH 6/6] fix(drive): hand storage over in document updates applied outside a batch At protocol version 15 a document update applied on its own, or as one of a document type's operations, now follows the same owner handover as an update in a batch of drive operations. An update naming only its contract's id resolves the contract for it. Tests: the index entry reader followed the reference to the card, so it now reads the entry itself. Adds protocol version 14 regressions, the callback's insert, ownerless and empty-set branches, and a check that a payer is refunded once for a card that was handed over. Co-Authored-By: Claude --- .../batch/tests/document/gas_sponsorship.rs | 554 ++++++++++++++++-- .../mod.rs | 11 +- .../v0/mod.rs | 28 +- .../v1/mod.rs | 41 ++ .../v1/mod.rs | 5 + .../src/state_transition_action/batch/mod.rs | 14 +- .../src/util/batch/drive_op_batch/document.rs | 91 ++- .../apply_drive_operations/v2/mod.rs | 3 +- .../src/util/batch/drive_op_batch/mod.rs | 21 +- .../v1/mod.rs | 80 ++- .../drive_document_method_versions/v5.rs | 3 + 11 files changed, 758 insertions(+), 93 deletions(-) create mode 100644 packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v1/mod.rs diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs index cb9b9a8ba6f..758b2211889 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/gas_sponsorship.rs @@ -57,11 +57,20 @@ pub(crate) mod gas_sponsorship_tests { use dpp::tokens::token_payment_info::v0::TokenPaymentInfoV0; use dpp::tokens::token_payment_info::TokenPaymentInfo; use dpp::document::DocumentV0Setters; + use dpp::fee::epoch::{DEFAULT_EPOCHS_PER_ERA, PERPETUAL_STORAGE_ERAS}; + use dpp::fee::fee_result::FeeResult; use drive::drive::RootTree; use drive::query::DriveDocumentQuery; + use drive::util::batch::drive_op_batch::{ + DocumentOperation, DocumentOperationsForContractDocumentType, UpdateOperationInfo, + }; + use drive::util::batch::{DocumentOperationType, DriveOperation}; + use drive::util::object_size_info::DocumentInfo::DocumentRefInfo; + use drive::util::object_size_info::{DataContractInfo, DocumentTypeInfo, OwnedDocumentInfo}; use drive::util::storage_flags::StorageFlags; use drive::util::test_helpers::setup_contract; use simple_signer::signer::SimpleSigner; + use std::borrow::Cow; use std::collections::BTreeMap; const GAS_FEES_PAID_BY_NOT_ALLOWED: u32 = 40129; @@ -454,6 +463,14 @@ pub(crate) mod gas_sponsorship_tests { /// The identity the committed storage flags of the user's card name as its storage owner fn stored_card_storage_owner(&self) -> Option { + let (_, storage_flags) = self.stored_card(); + storage_flags + .and_then(|storage_flags| storage_flags.owner_id().copied()) + .map(Identifier::from) + } + + /// The user's card as it is stored, with its storage flags + fn stored_card(&self) -> (Document, Option) { let (card, _) = self.card_of(&self.user); let query = DriveDocumentQuery::new_primary_key_single_item_query( &self.contract, @@ -474,10 +491,31 @@ pub(crate) mod gas_sponsorship_tests { ) .expect("expected to query the card") .documents_owned(); - let (_, storage_flags) = documents.pop().expect("expected the card to be stored"); - storage_flags - .and_then(|storage_flags| storage_flags.owner_id().copied()) - .map(Identifier::from) + documents.pop().expect("expected the card to be stored") + } + + /// The user's transfer of the card they created to `recipient`, at identity contract + /// nonce 3, paying its token and the gas themselves + async fn card_transfer(&self, recipient: Identifier) -> StateTransition { + let (mut card, _) = self.card_of(&self.user); + card.increment_revision() + .expect("expected the revision to increment"); + BatchTransition::new_document_transfer_transition_from_document( + card, + self.contract + .document_type_for_name("card") + .expect("expected the card document type"), + recipient, + &self.user_key, + 3, + 0, + Some(Self::game_token_payment(1, GasFeesPaidBy::DocumentOwner)), + &self.user_signer, + self.platform_version, + None, + ) + .await + .expect("expected a batch transition") } /// The owner the card's entry in its `attack` index names @@ -503,21 +541,25 @@ pub(crate) mod gas_sponsorship_tests { attack, vec![0], ]; + // Read raw: `get` follows the reference to the card, and would answer with the + // card's flags let element = self .platform .drive .grove - .get( + .get_raw( path.iter() .map(Vec::as_slice) .collect::>() - .as_slice(), + .as_slice() + .into(), card.id().as_bytes(), None, &self.platform_version.drive.grove_version, ) .unwrap() .expect("expected the card's index entry"); + assert!(element.is_reference(), "expected the index entry itself"); StorageFlags::from_element_flags_ref(element.get_flags().as_ref()?) .expect("expected flags that decode") .and_then(|flags| flags.owner_id().copied()) @@ -1581,6 +1623,11 @@ pub(crate) mod gas_sponsorship_tests { assert_eq!(refunded_to_user, None); assert!(refunded_to_contract_owner.is_some_and(|refund| refund > 0)); assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()), + "the index entries the replacement leaves unchanged are handed over too" + ); let deletion = setup.card_deletion(4).await; let (refunded_to_user, _) = settle(&setup, &deletion, &block, false); @@ -1647,29 +1694,7 @@ pub(crate) mod gas_sponsorship_tests { Some(setup.contract_owner.id()) ); - let (mut card, _) = setup.card_of(&setup.user); - card.increment_revision() - .expect("expected the revision to increment"); - let transfer = BatchTransition::new_document_transfer_transition_from_document( - card, - setup - .contract - .document_type_for_name("card") - .expect("expected the card document type"), - recipient.id(), - &setup.user_key, - 3, - 0, - Some(Sponsorship::game_token_payment( - 1, - GasFeesPaidBy::DocumentOwner, - )), - &setup.user_signer, - setup.platform_version, - None, - ) - .await - .expect("expected a batch transition"); + let transfer = setup.card_transfer(recipient.id()).await; let (refunded_to_user, refunded_to_contract_owner) = settle(&setup, &transfer, &block, false); @@ -1845,6 +1870,11 @@ pub(crate) mod gas_sponsorship_tests { setup.stored_card_storage_owner(), Some(setup.contract_owner.id()) ); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.contract_owner.id()), + "the index entries the replacement leaves unchanged are handed over too" + ); let deletion = setup.card_deletion(4).await; let (_, refunded_to_contract_owner) = settle(&setup, &deletion, &block, false); @@ -1909,29 +1939,7 @@ pub(crate) mod gas_sponsorship_tests { Some(setup.user.id()) ); - let (mut card, _) = setup.card_of(&setup.user); - card.increment_revision() - .expect("expected the revision to increment"); - let transfer = BatchTransition::new_document_transfer_transition_from_document( - card, - setup - .contract - .document_type_for_name("card") - .expect("expected the card document type"), - recipient.id(), - &setup.user_key, - 3, - 0, - Some(Sponsorship::game_token_payment( - 1, - GasFeesPaidBy::DocumentOwner, - )), - &setup.user_signer, - setup.platform_version, - None, - ) - .await - .expect("expected a batch transition"); + let transfer = setup.card_transfer(recipient.id()).await; let (_, refunded_to_contract_owner) = settle(&setup, &transfer, &block, false); assert_eq!(refunded_to_contract_owner, None); @@ -1943,6 +1951,448 @@ pub(crate) mod gas_sponsorship_tests { ); } + /// What the user and the contract owner are each refunded, in total, by a card's creation, + /// its replacement giving it a description, and the user's deletion of it. `creation` and + /// `replacement` say whom each asks for the gas; without a `replacement` the card is + /// deleted as it was created. + async fn refunds_over_a_cards_life( + creation: GasFeesPaidBy, + replacement: Option, + ) -> (Credits, Credits) { + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let mut refunds = vec![settle( + &setup, + &setup.card_creation(creation).await, + &block, + creation == GasFeesPaidBy::ContractOwner, + )]; + let mut nonce = 3; + if let Some(replacement) = replacement { + let transition = setup + .card_replacement_describing(2, nonce, 200, replacement) + .await; + refunds.push(settle( + &setup, + &transition, + &block, + replacement == GasFeesPaidBy::ContractOwner, + )); + nonce += 1; + } + refunds.push(settle( + &setup, + &setup.card_deletion(nonce).await, + &block, + false, + )); + refunds + .into_iter() + .fold((0, 0), |(user, contract_owner), refunded| { + ( + user + refunded.0.unwrap_or_default(), + contract_owner + refunded.1.unwrap_or_default(), + ) + }) + } + + /// Checks that a refund paid out `in_two_parts` is the refund paid out `at_once`. A refund + /// rounds each epoch's share of the storage window down, so two parts can be up to a + /// credit per epoch short of the whole, which is far less than one stored byte is worth. + fn assert_same_refund_in_two_parts(in_two_parts: Credits, at_once: Credits) { + let rounding = Credits::from(PERPETUAL_STORAGE_ERAS * DEFAULT_EPOCHS_PER_ERA); + assert!( + in_two_parts <= at_once && at_once - in_two_parts <= rounding, + "{in_two_parts} refunded in two parts, {at_once} at once" + ); + } + + #[tokio::test] + async fn should_refund_a_first_payer_once_for_all_of_a_card_that_was_handed_over() { + // Whoever paid for a card's creation is refunded all of it exactly once, whether the + // card is deleted as it was created or handed over in full first: the handover refunds + // the card and its index entries, the deletion the rest. + let (_, contract_owner_alone) = + refunds_over_a_cards_life(GasFeesPaidBy::ContractOwner, None).await; + let (user_alone, _) = refunds_over_a_cards_life(GasFeesPaidBy::DocumentOwner, None).await; + assert!(contract_owner_alone > 0); + assert_eq!( + user_alone, contract_owner_alone, + "the same card is stored whoever pays for it" + ); + + // The user takes a sponsored card over, and is refunded only what they then paid for: + // the card with its description + let (user_after_taking_over, contract_owner_handing_over) = refunds_over_a_cards_life( + GasFeesPaidBy::ContractOwner, + Some(GasFeesPaidBy::DocumentOwner), + ) + .await; + assert_same_refund_in_two_parts(contract_owner_handing_over, contract_owner_alone); + assert!(user_after_taking_over > 0); + + // The reverse: the contract owner takes the user's card over + let (user_handing_over, contract_owner_after_taking_over) = refunds_over_a_cards_life( + GasFeesPaidBy::DocumentOwner, + Some(GasFeesPaidBy::ContractOwner), + ) + .await; + assert_same_refund_in_two_parts(user_handing_over, user_alone); + assert_eq!( + contract_owner_after_taking_over, user_after_taking_over, + "either taker holds the same bytes afterwards" + ); + } + + // ---------- Protocol version 14, which names the document's owner whoever paid ---------- + + fn protocol_version_14() -> &'static PlatformVersion { + PlatformVersion::get(14).expect("expected protocol version 14 to exist") + } + + #[tokio::test] + async fn should_name_the_user_as_holder_of_a_sponsored_card_at_protocol_version_14() { + // The contract owner pays the gas, and the storage flags name the document's owner as + // they always did: a paying sponsor is named from protocol version 15. + let setup = Sponsorship::build( + protocol_version_14(), + GasFeesPaidBy::ContractOwner, + false, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + None, + ); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &block, true); + + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + + let deletion = setup.card_deletion(3).await; + let (refunded_to_user, refunded_to_contract_owner) = + settle(&setup, &deletion, &block, false); + assert!(refunded_to_user.is_some_and(|refund| refund > 0)); + assert_eq!(refunded_to_contract_owner, None); + } + + #[tokio::test] + async fn should_leave_a_transferred_card_with_its_first_holder_at_protocol_version_14() { + // A transfer rewrites the card at the size it had, which up to protocol version 14 + // keeps the stored owner: the card and the index entries the transfer leaves unchanged + // stay with the user. Version 15 moves them to the recipient. + let mut setup = Sponsorship::build( + protocol_version_14(), + GasFeesPaidBy::ContractOwner, + false, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + None, + ); + let (recipient, _, _) = setup_identity(&mut setup.platform, 573, dash_to_credits!(0.1)); + setup.give_the_user_game_tokens(5); + let block = BlockInfo::default(); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &block, false); + + let transfer = setup.card_transfer(recipient.id()).await; + settle(&setup, &transfer, &block, false); + + assert_eq!(setup.stored_card().0.owner_id(), recipient.id()); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + } + + // ---------- Updates Drive applies outside a state transition's batch ---------- + + /// The card game with the card a sponsored creation left the contract owner holding + async fn game_with_a_sponsored_card() -> Sponsorship { + let setup = Sponsorship::new( + GasFeesPaidBy::ContractOwner, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + ); + let creation = setup.card_creation(GasFeesPaidBy::ContractOwner).await; + settle(&setup, &creation, &BlockInfo::default(), true); + assert_eq!( + setup.stored_card_storage_owner(), + Some(setup.contract_owner.id()) + ); + setup + } + + /// Storage flags naming `holder`, as an update in epoch 0 writes them + fn flags_naming<'a>(holder: Identifier) -> Option> { + Some(Cow::Owned(StorageFlags::new_single_epoch( + 0, + Some(holder.to_buffer()), + ))) + } + + /// Applies `update`, the only operation of its batch + fn apply_alone( + setup: &Sponsorship, + update: DocumentOperationType, + platform_version: &PlatformVersion, + ) -> FeeResult { + setup + .platform + .drive + .apply_drive_operations( + vec![DriveOperation::DocumentOperation(update)], + true, + &BlockInfo::default(), + None, + platform_version, + ) + .expect("expected the update to apply") + } + + /// Checks that an update written in the user's name handed the sponsored card over in + /// full: the contract owner is refunded, and the card and the index entry the update left + /// unchanged are the user's. + fn assert_handed_over_in_full_to_the_user(setup: &Sponsorship, fee_result: &FeeResult) { + assert!( + fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.contract_owner.id()) + .is_some_and(|refund| refund > 0), + "the contract owner gets back what they paid for" + ); + assert_eq!( + fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.user.id()), + None + ); + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + } + + #[tokio::test] + async fn should_hand_a_sponsored_card_over_in_full_when_drive_updates_it_on_its_own() { + // `update_document_for_contract` applies a batch of its own, which follows the rule of + // a state transition's batch. + let setup = game_with_a_sponsored_card().await; + let (card, _) = setup.stored_card(); + + let fee_result = setup + .platform + .drive + .update_document_for_contract( + &card, + &setup.contract, + setup + .contract + .document_type_for_name("card") + .expect("expected the card document type"), + Some(setup.user.id().to_buffer()), + BlockInfo::default(), + true, + flags_naming(setup.user.id()), + None, + setup.platform_version, + ) + .expect("expected the card to update"); + + assert_handed_over_in_full_to_the_user(&setup, &fee_result); + } + + #[tokio::test] + async fn should_hand_a_sponsored_card_over_in_full_when_its_update_names_only_the_contract_id() + { + // The batch reads the contract to learn that its owner may hold the card. + let setup = game_with_a_sponsored_card().await; + let (card, _) = setup.stored_card(); + + let fee_result = apply_alone( + &setup, + DocumentOperationType::UpdateDocument { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo((&card, flags_naming(setup.user.id()))), + owner_id: Some(setup.user.id().to_buffer()), + }, + contract_info: DataContractInfo::DataContractId(setup.contract.id()), + document_type_info: DocumentTypeInfo::DocumentTypeNameAsStr("card"), + }, + setup.platform_version, + ); + + assert_handed_over_in_full_to_the_user(&setup, &fee_result); + } + + #[tokio::test] + async fn should_hand_a_sponsored_card_over_in_full_when_its_update_is_one_of_a_document_type() { + let setup = game_with_a_sponsored_card().await; + let (card, _) = setup.stored_card(); + + let fee_result = apply_alone( + &setup, + DocumentOperationType::MultipleDocumentOperationsForSameContractDocumentType { + document_operations: DocumentOperationsForContractDocumentType { + operations: vec![DocumentOperation::UpdateOperation(UpdateOperationInfo { + document: &card, + serialized_document: None, + owner_id: Some(setup.user.id().to_buffer()), + storage_flags: flags_naming(setup.user.id()), + })], + contract: &setup.contract, + document_type: setup + .contract + .document_type_for_name("card") + .expect("expected the card document type"), + }, + }, + setup.platform_version, + ); + + assert_handed_over_in_full_to_the_user(&setup, &fee_result); + } + + /// Rewrites the user's card as it is stored, changing only what `change` does, with flags + /// naming `holder`, as Drive running `platform_version` does on its own + fn rewrite_card_for( + setup: &Sponsorship, + holder: Identifier, + change: impl FnOnce(&mut Document), + platform_version: &PlatformVersion, + ) -> FeeResult { + let (mut card, _) = setup.stored_card(); + change(&mut card); + setup + .platform + .drive + .update_document_for_contract( + &card, + &setup.contract, + setup + .contract + .document_type_for_name("card") + .expect("expected the card document type"), + Some(setup.user.id().to_buffer()), + BlockInfo::default(), + true, + flags_naming(holder), + None, + platform_version, + ) + .expect("expected the card to update") + } + + /// A card game without sponsorship on a chain running `platform_version`, with the card + /// the user created and paid for + async fn game_with_the_users_own_card( + platform_version: &'static PlatformVersion, + ) -> Sponsorship { + let setup = Sponsorship::build( + platform_version, + GasFeesPaidBy::DocumentOwner, + false, + dash_to_credits!(0.1), + dash_to_credits!(0.1), + 15, + None, + ); + let creation = setup.card_creation(GasFeesPaidBy::DocumentOwner).await; + settle(&setup, &creation, &BlockInfo::default(), false); + setup + } + + #[tokio::test] + async fn should_move_a_cards_stored_bytes_to_the_holder_a_drive_update_names() { + // No sponsor is involved, so the named holder takes the stored bytes of the card and + // of its unchanged index entries as they are, and nobody is refunded. + let setup = game_with_the_users_own_card(PlatformVersion::latest()).await; + let holder = Identifier::from([9; 32]); + + let fee_result = rewrite_card_for(&setup, holder, |_| {}, setup.platform_version); + + assert_eq!( + fee_result + .fee_refunds + .calculate_refunds_amount_for_identity(setup.user.id()), + None + ); + assert_eq!(setup.stored_card_storage_owner(), Some(holder)); + assert_eq!(setup.stored_attack_index_reference_owner(), Some(holder)); + } + + #[tokio::test] + async fn should_leave_a_card_with_its_first_holder_when_drive_updates_it_at_protocol_version_14( + ) { + let setup = game_with_the_users_own_card(protocol_version_14()).await; + + rewrite_card_for( + &setup, + Identifier::from([9; 32]), + |_| {}, + setup.platform_version, + ); + + assert_eq!(setup.stored_card_storage_owner(), Some(setup.user.id())); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + } + + #[tokio::test] + async fn should_leave_index_entries_an_earlier_version_split_from_their_card_with_their_payer() + { + // Up to protocol version 14 a card that grows under a new holder goes to that holder + // while its unchanged index entries stay with whoever paid for them. Version 15 moves + // index entries when the card changes hands, so an update keeping the card's holder + // leaves such entries where they are, and the next change of holder moves them. + let setup = game_with_the_users_own_card(protocol_version_14()).await; + let second_holder = Identifier::from([9; 32]); + let third_holder = Identifier::from([8; 32]); + rewrite_card_for( + &setup, + second_holder, + |card| card.set("description", "d".repeat(200).into()), + protocol_version_14(), + ); + assert_eq!(setup.stored_card_storage_owner(), Some(second_holder)); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()) + ); + + rewrite_card_for(&setup, second_holder, |_| {}, PlatformVersion::latest()); + assert_eq!(setup.stored_card_storage_owner(), Some(second_holder)); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(setup.user.id()), + "the user paid for the entry, and the card did not change hands" + ); + + rewrite_card_for(&setup, third_holder, |_| {}, PlatformVersion::latest()); + assert_eq!(setup.stored_card_storage_owner(), Some(third_holder)); + assert_eq!( + setup.stored_attack_index_reference_owner(), + Some(third_holder) + ); + } + /// The card game with a `grade` on its cards that only moderators write, and the moderator /// its contract appoints: an identity that is neither the contract owner nor the user fn moderated_card_game() -> (Sponsorship, Identity, SimpleSigner) { diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/mod.rs index 2e1a90243b6..cf439c93ecf 100644 --- a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/mod.rs +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -49,9 +50,17 @@ impl Drive { drive_operations, platform_version, ), + 1 => self.update_document_for_contract_apply_and_add_to_operations_v1( + document_and_contract_info, + block_info, + estimated_costs_only_with_layer_info, + transaction, + drive_operations, + platform_version, + ), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "update_document_for_contract_apply_and_add_to_operations".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v0/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v0/mod.rs index 993d27e5f6e..b27297dc000 100644 --- a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v0/mod.rs +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v0/mod.rs @@ -6,14 +6,39 @@ use crate::util::object_size_info::DocumentAndContractInfo; use dpp::block::block_info::BlockInfo; use dpp::version::PlatformVersion; use grovedb::TransactionArg; +use std::collections::BTreeSet; impl Drive { /// Updates a document. pub(in crate::drive::document::update) fn update_document_for_contract_apply_and_add_to_operations_v0( + &self, + document_and_contract_info: DocumentAndContractInfo, + block_info: &BlockInfo, + estimated_costs_only_with_layer_info: Option, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + self.update_document_for_contract_apply_and_add_to_operations_v0_body( + document_and_contract_info, + block_info, + estimated_costs_only_with_layer_info, + None, + transaction, + drive_operations, + platform_version, + ) + } + + /// Generation 0, applying its batch with `owner_handover` (see + /// [`GroveDbOpBatch`](crate::util::batch::GroveDbOpBatch)): `None` is generation 0 itself. + #[allow(clippy::too_many_arguments)] + pub(in crate::drive::document::update) fn update_document_for_contract_apply_and_add_to_operations_v0_body( &self, document_and_contract_info: DocumentAndContractInfo, block_info: &BlockInfo, mut estimated_costs_only_with_layer_info: Option, + owner_handover: Option>, transaction: TransactionArg, drive_operations: &mut Vec, platform_version: &PlatformVersion, @@ -33,10 +58,11 @@ impl Drive { transaction, platform_version, )?; - self.apply_batch_low_level_drive_operations( + self.apply_batch_low_level_drive_operations_with_owner_handover( estimated_costs_only_with_layer_info, transaction, batch_operations, + owner_handover, drive_operations, &platform_version.drive, )?; diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v1/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v1/mod.rs new file mode 100644 index 00000000000..b387747f6e8 --- /dev/null +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_apply_and_add_to_operations/v1/mod.rs @@ -0,0 +1,41 @@ +use crate::drive::Drive; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::state_transition_action::batch::storage_sponsor_of_document_type; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::object_size_info::DocumentAndContractInfo; +use dpp::block::block_info::BlockInfo; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Generation 0, except that the update hands the document's storage over as an update in + /// a batch of drive operations does (protocol version 15): the stored bytes go to the + /// owner the written flags name, and an owner change involving the contract's owner, on a + /// document type offering gas sponsorship, is a full handover. + pub(in crate::drive::document::update) fn update_document_for_contract_apply_and_add_to_operations_v1( + &self, + document_and_contract_info: DocumentAndContractInfo, + block_info: &BlockInfo, + estimated_costs_only_with_layer_info: Option, + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let full_handover_owners = storage_sponsor_of_document_type( + document_and_contract_info.contract, + document_and_contract_info.document_type, + ) + .into_iter() + .collect(); + self.update_document_for_contract_apply_and_add_to_operations_v0_body( + document_and_contract_info, + block_info, + estimated_costs_only_with_layer_info, + Some(full_handover_owners), + transaction, + drive_operations, + platform_version, + ) + } +} diff --git a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs index b390c354565..d6665b581c5 100644 --- a/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs +++ b/packages/rs-drive/src/drive/document/update/internal/update_document_for_contract_operations/v1/mod.rs @@ -407,6 +407,11 @@ impl Drive { // An update naming a new owner moves the references of the indexes it leaves unchanged // too. An untrusted refresh writes the stored flags back, so those are refreshed as // trusted: what they point at is unchanged. + // + // The document's stored owner decides, not each reference's. Earlier generations could + // leave a reference with whoever paid for it while the document went to a new owner; + // such a reference stays with its payer until an update names another owner. No gas + // sponsor holds one: flags name a sponsor only from the generation that follows owners. let references_take_new_owner = references_follow_the_owner && old_document_info .get_storage_flags_ref() diff --git a/packages/rs-drive/src/state_transition_action/batch/mod.rs b/packages/rs-drive/src/state_transition_action/batch/mod.rs index 590550e26d2..58bb3fc7a25 100644 --- a/packages/rs-drive/src/state_transition_action/batch/mod.rs +++ b/packages/rs-drive/src/state_transition_action/batch/mod.rs @@ -5,6 +5,7 @@ use derive_more::From; use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::accessors::{DocumentTypeV0Getters, DocumentTypeV1Getters}; use dpp::data_contract::document_type::DocumentTypeRef; +use dpp::data_contract::DataContract; use dpp::tokens::gas_fees_paid_by::GasFeesPaidBy; use crate::drive::contract_groups::types::ContractGroupMembershipsForContract; use dpp::fee::fee_result::FeeResult; @@ -192,6 +193,7 @@ pub fn record_gas_sponsor_as_storage_owner( operations: &mut [DriveOperation], gas_sponsor_id: Identifier, ) { + let gas_sponsor = gas_sponsor_id.to_buffer(); for operation in operations { let DriveOperation::DocumentOperation(document_operation) = operation else { continue; @@ -218,7 +220,7 @@ pub fn record_gas_sponsor_as_storage_owner( .. } => owned_document_info .document_info - .set_storage_flags_owner(gas_sponsor_id.to_buffer()), + .set_storage_flags_owner(gas_sponsor), DocumentOperationType::DeleteDocument { .. } | DocumentOperationType::ForceDeleteDocument { .. } | DocumentOperationType::ForceDeleteReadDocument { .. } @@ -249,6 +251,16 @@ pub(crate) fn document_type_offers_gas_sponsorship(document_type: DocumentTypeRe .any(|token_cost| token_cost.gas_fees_paid_by != GasFeesPaidBy::DocumentOwner) } +/// The identity that may hold the storage of a document of the type which someone else owns: +/// the contract's owner, when the type offers gas sponsorship. A change of owner involving it +/// is a full handover (see `GroveDbOpBatch::owner_handover`). +pub(crate) fn storage_sponsor_of_document_type( + contract: &DataContract, + document_type: DocumentTypeRef, +) -> Option<[u8; 32]> { + document_type_offers_gas_sponsorship(document_type).then(|| contract.owner_id().to_buffer()) +} + /// Who pays the gas of a whole batch, as `GasFeesPaidBy::resolve` names it for each of its /// transitions #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/document.rs b/packages/rs-drive/src/util/batch/drive_op_batch/document.rs index 16fe3bb864e..93133d72c79 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/document.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/document.rs @@ -2,7 +2,7 @@ use crate::util::estimated_layers::EstimatedLayers; use crate::drive::Drive; use crate::error::Error; use crate::fees::op::LowLevelDriveOperation; -use crate::state_transition_action::batch::document_type_offers_gas_sponsorship; +use crate::state_transition_action::batch::storage_sponsor_of_document_type; use crate::state_transition_action::batch::batched_transition::document_transition::document_create_transition_action::ConsumedDocument; use crate::util::batch::drive_op_batch::DriveLowLevelOperationConverter; use crate::util::object_size_info::DocumentInfo::{DocumentRefAndSerialization, DocumentRefInfo}; @@ -27,7 +27,7 @@ use dpp::voting::vote_info_storage::contested_document_vote_poll_stored_info::Co use dpp::ProtocolError; use grovedb::TransactionArg; use std::borrow::Cow; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; /// A wrapper for a document operation #[derive(Clone, Debug)] @@ -271,27 +271,74 @@ impl DriveLowLevelOperationConverter for DocumentOperationType<'_> { } impl DocumentOperationType<'_> { - /// The owner of this update's contract, when its document type offers gas sponsorship: - /// the identity that may hold the storage of a document someone else owns. Batches always - /// carry the resolved contract, so an update that names only its id has no sponsor here. - pub(crate) fn storage_sponsor_of_updated_document(&self) -> Result, Error> { - let Self::UpdateDocument { - contract_info, - document_type_info, - .. - } = self - else { - return Ok(None); - }; - let contract = match contract_info { - DataContractInfo::DataContractFetchInfo(fetch_info) => &fetch_info.contract, - DataContractInfo::BorrowedDataContract(contract) => *contract, - DataContractInfo::OwnedDataContract(contract) => contract, - DataContractInfo::DataContractId(_) => return Ok(None), + /// Adds this operation's part to the owner handover of the batch it is in (see + /// `GroveDbOpBatch::owner_handover`): nothing unless it updates a document, and then the + /// owner of the update's contract when the document type offers gas sponsorship, the + /// identity that may hold the storage of a document someone else owns. + /// + /// An update naming only its contract's id reads the contract here, at no charge: lowering + /// the operation fetches it again and bills that. A contract that is not stored has no + /// sponsor, and the lowering answers for it. + pub(crate) fn add_to_owner_handover( + &self, + owner_handover: &mut Option>, + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let storage_sponsor = match self { + Self::UpdateDocument { + contract_info, + document_type_info, + .. + } => { + let fetched_contract; + let contract = match contract_info { + DataContractInfo::DataContractFetchInfo(fetch_info) => { + Some(&fetch_info.contract) + } + DataContractInfo::BorrowedDataContract(contract) => Some(*contract), + DataContractInfo::OwnedDataContract(contract) => Some(contract), + DataContractInfo::DataContractId(contract_id) => { + // Not cached here, so the lowering's fetch finds the cache as it + // would have + fetched_contract = drive.get_contract_with_fetch_info( + contract_id.to_buffer(), + false, + transaction, + platform_version, + )?; + fetched_contract + .as_ref() + .map(|fetch_info| &fetch_info.contract) + } + }; + match contract { + Some(contract) => storage_sponsor_of_document_type( + contract, + document_type_info.clone().resolve(contract)?, + ), + None => None, + } + } + Self::MultipleDocumentOperationsForSameContractDocumentType { + document_operations, + } if document_operations + .operations + .iter() + .any(|operation| matches!(operation, DocumentOperation::UpdateOperation(_))) => + { + storage_sponsor_of_document_type( + document_operations.contract, + document_operations.document_type, + ) + } + _ => return Ok(()), }; - let document_type = document_type_info.clone().resolve(contract)?; - Ok(document_type_offers_gas_sponsorship(document_type) - .then(|| contract.owner_id().to_buffer())) + owner_handover + .get_or_insert_with(BTreeSet::new) + .extend(storage_sponsor); + Ok(()) } /// Run all direct TTL cleanup before any operation in this batch is built. diff --git a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs index 6a3378ea352..5b66fc7bd10 100644 --- a/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs +++ b/packages/rs-drive/src/util/batch/drive_op_batch/drive_methods/apply_drive_operations/v2/mod.rs @@ -132,7 +132,8 @@ impl Drive { let forfeits_storage_refunds = operations .iter() .any(DriveOperation::forfeits_storage_refunds); - let owner_handover = DriveOperation::owner_handover(&operations)?; + let owner_handover = + DriveOperation::owner_handover(&operations, self, transaction, platform_version)?; // The document operations of a moderator's deletion go in a batch of their own only // when something else in the batch may free bytes someone is owed. let separates_forfeited_operations = forfeits_storage_refunds 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 43bcb1ac3ee..abf7b5cb89c 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 @@ -287,19 +287,22 @@ impl DriveOperation<'_> { /// documents it updates, for whom an owner change is a full handover. pub(crate) fn owner_handover( operations: &[DriveOperation], + drive: &Drive, + transaction: TransactionArg, + platform_version: &PlatformVersion, ) -> Result>, Error> { - let mut owners = None; + let mut owner_handover = None; for operation in operations { - if let DriveOperation::DocumentOperation( - document_operation @ DocumentOperationType::UpdateDocument { .. }, - ) = operation - { - owners - .get_or_insert_with(BTreeSet::new) - .extend(document_operation.storage_sponsor_of_updated_document()?); + if let DriveOperation::DocumentOperation(document_operation) = operation { + document_operation.add_to_owner_handover( + &mut owner_handover, + drive, + transaction, + platform_version, + )?; } } - Ok(owners) + Ok(owner_handover) } /// Whether the batch this operation is in refunds nobody for the storage its document diff --git a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs index f0ecf84ed42..77185f4715e 100644 --- a/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs +++ b/packages/rs-drive/src/util/grove_operations/grove_apply_batch_with_add_costs/v1/mod.rs @@ -17,13 +17,13 @@ impl Drive { /// as generation 0 does. pub(super) fn grove_apply_batch_with_add_costs_v1( &self, - ops: GroveDbOpBatch, + mut ops: GroveDbOpBatch, validate: bool, transaction: TransactionArg, drive_operations: &mut Vec, drive_version: &DriveVersion, ) -> Result<(), Error> { - let owner_handover = ops.owner_handover.clone(); + let owner_handover = ops.owner_handover.take(); self.grove_apply_batch_with_flags_update( ops, validate, @@ -96,18 +96,26 @@ mod tests { StorageFlags::new_single_epoch(epoch, Some(owner)).to_element_flags() } - fn update(old_owner: [u8; 32], new_owner: [u8; 32]) -> (ElementFlagsUpdate, StorageFlags) { + fn ownerless_flags(epoch: u16) -> ElementFlags { + StorageFlags::new_single_epoch(epoch, None).to_element_flags() + } + + /// A same size update of an element stored with `old_flags`, written with `new_flags`. + fn update_flags( + old_flags: Option, + mut new_flags: ElementFlags, + full_handover_owners: &BTreeSet<[u8; 32]>, + ) -> (ElementFlagsUpdate, StorageFlags) { let same_size = StorageCost { added_bytes: 0, replaced_bytes: 10, removed_bytes: NoStorageRemoval, }; - let mut new_flags = flags(2, new_owner); let answer = update_element_flags_for_owner_change( &same_size, - Some(flags(1, old_owner)), + old_flags, &mut new_flags, - &BTreeSet::from([SPONSOR]), + full_handover_owners, ) .expect("expected the flags to update"); let written = StorageFlags::from_element_flags_ref(&new_flags) @@ -116,6 +124,14 @@ mod tests { (answer, written) } + fn update(old_owner: [u8; 32], new_owner: [u8; 32]) -> (ElementFlagsUpdate, StorageFlags) { + update_flags( + Some(flags(1, old_owner)), + flags(2, new_owner), + &BTreeSet::from([SPONSOR]), + ) + } + #[test] fn should_keep_the_stored_bytes_and_change_their_owner_when_no_sponsor_is_involved() { let (answer, written) = update(OLD_OWNER, NEW_OWNER); @@ -154,4 +170,56 @@ mod tests { assert_eq!(written.owner_id(), Some(&SPONSOR)); assert_eq!(written.base_epoch(), &1); } + + #[test] + fn should_use_a_sponsors_flags_as_written_when_no_flags_are_stored() { + let (answer, written) = update_flags(None, flags(2, SPONSOR), &BTreeSet::from([SPONSOR])); + + assert_eq!(answer, ElementFlagsUpdate::Unchanged); + assert_eq!(written.owner_id(), Some(&SPONSOR)); + assert_eq!(written.base_epoch(), &2); + } + + #[test] + fn should_give_a_sponsor_the_stored_bytes_nobody_owned() { + let (answer, written) = update_flags( + Some(ownerless_flags(1)), + flags(2, SPONSOR), + &BTreeSet::from([SPONSOR]), + ); + + assert_eq!( + answer, + ElementFlagsUpdate::Changed, + "nobody is refunded, so nothing is replaced" + ); + assert_eq!(written.owner_id(), Some(&SPONSOR)); + assert_eq!(written.base_epoch(), &1); + } + + #[test] + fn should_leave_a_sponsors_element_alone_when_the_new_flags_name_nobody() { + let (answer, written) = update_flags( + Some(flags(1, SPONSOR)), + ownerless_flags(2), + &BTreeSet::from([SPONSOR]), + ); + + assert_ne!(answer, ElementFlagsUpdate::AccountAsReplacement); + assert_eq!(written.owner_id(), Some(&SPONSOR)); + assert_eq!(written.base_epoch(), &1); + } + + #[test] + fn should_keep_the_stored_bytes_of_every_owner_change_when_no_owner_gets_a_full_handover() { + let (answer, written) = update_flags( + Some(flags(1, SPONSOR)), + flags(2, NEW_OWNER), + &BTreeSet::new(), + ); + + assert_eq!(answer, ElementFlagsUpdate::Changed); + assert_eq!(written.owner_id(), Some(&NEW_OWNER)); + assert_eq!(written.base_epoch(), &1); + } } diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs b/packages/rs-platform-version/src/version/drive_versions/drive_document_method_versions/v5.rs index e96bbf31043..c6954ed119c 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 @@ -21,6 +21,8 @@ use crate::version::drive_versions::drive_document_method_versions::{ /// each height of the climb with the layer GroveDB deletes in there, not the one above it. /// * `update.update_document_for_contract_operations` is 2: when an update names a new owner, /// the references of the indexes it leaves unchanged are rewritten with that owner. +/// * `update.update_document_for_contract_apply_and_add_to_operations` is 1: an update applied +/// on its own hands storage over as one in a batch of drive operations does. /// * `insert_contested.add_contested_document_for_contract_operations` is 2: a poll's end date /// entries name whoever the document's storage flags name. pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = @@ -39,6 +41,7 @@ pub const DRIVE_DOCUMENT_METHOD_VERSIONS_V5: DriveDocumentMethodVersions = }, update: DriveDocumentUpdateMethodVersions { update_document_for_contract_operations: 2, // changed: unchanged index references follow a new owner + update_document_for_contract_apply_and_add_to_operations: 1, // changed: applied with the batch's owner handover ..DRIVE_DOCUMENT_METHOD_VERSIONS_V4.update }, insert_contested: DriveDocumentInsertContestedMethodVersions {