Skip to content

Drive: moderator ChangeDocumentFields flags new storage with the document owner, so tombstones and unhides refund moderator-paid bytes to the author #5575

Open

Description

@PastaPastaPasta

Summary

When a moderator changes a document's fields (ContractUserModerationAction::ChangeDocumentFields), Drive writes every new element (the document item, re-keyed index references and any new index subtrees) with storage flags naming the document owner, although the moderator pays for those bytes. When those elements are later removed, by the owner's own replace (for example a tombstone that drops a skipIfAbsent property) or by a later moderator change (unhide, relabel, mod 1→2), the storage refund goes to the document owner.

The result: an author can be refunded storage that a moderator paid for. On a contract that lanes listings by a moderator field, an author who posts, gets hidden and then tombstones the post can end with more credits than before posting.

Where (v5.0.0-rc.8)

packages/rs-drive/src/state_transition_action/action_convert_to_operations/contract/contract_user_moderation_transition.rs:324-330

// ... 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.
let owner_id = document.owner_id();
let storage_flags = StorageFlags::new_single_epoch(epoch.index, Some(owner_id.to_buffer()));

These flags go into DocumentOperationType::UpdateDocument (:331-345). From there they land on every element the update inserts: the reference built at update_document_for_contract_operations/v1/mod.rs:290-294, new trees at :645-651, :829-858, :885-897, the [0] tree at :1045-1054, and the reference at :1058-1080.

How the refund is routed:

  1. Removed bytes are split by flag owner (grove_apply_batch_with_add_costs/v0/mod.rs:77; grovedb grovedb-epoch-based-storage-flags/src/lib.rs:773-860).
  2. The split goes to FeeRefunds::from_storage_removal (fees/op.rs:423-449).
  3. A non-payer's refund is credited to that identity's balance (fee_result/refunds.rs:137-154 → apply_balance_change_from_fee_to_identity/v1/mod.rs:198-222).

ForfeitStorageRefunds (drive_op_batch/mod.rs:265-271) is used for moderator deletions only.

The comment shows the owner flag was deliberate for the document item: the owner's refund of the document's own storage stays the owner's. What it doesn't account for is that the same flags also go on the index elements and subtrees the moderator pays to create.

Repro (testnet, Drive 5.0.0-rc.8, PV14)

Contracts:

  • Phase 1: a social contract with elected moderation, whose listing indexes are keyed [live, mod, …] with skipIfAbsent: ["live"]: 5Kucw9g9TsiSRXbns2mPumXKVkYW7jwVrTdjFuFhMEwU.
  • Phase 2: a trimmed copy with an appointed interim moderator: BdjWpPEcAwHpZ3mJ8po8duYVge4WdJXV8m5EBPdo4z3v.

Identities: author 3o7nv2C8fkynwjmZRNaWqZw5GyMdTkFpcYi95Bdfkr5n, moderator 9HX1B7LeHJCcBDnj3B6SWcEZz1SLhz2uRsi89WFXwXvo.

Steps (every figure is a settled balance delta, with the signer's identity-contract nonce checked +1 and the other identity's +0):

  1. The author creates a post with tags and mentions. The post action fee of 20,000,000 goes to the moderators pot.
  2. The moderator sets mod: 1 with changeDocumentFields. Every [live, mod, …] and [mentionedUserIds, mod, …] entry moves to the mod = 1 lane.
  3. The author tombstones the post: a replace that drops live, so it leaves the skip-live indexes, and drops the mentions.

The moderator's balance moved by 0.00 at all 13 tombstones of hidden posts. The author was refunded the moderator-paid hidden-lane entries.

Shape Author paid (incl. 20M action fee) Refund at hide Refund at tombstone Author net Same post, never hidden
1 tag 159.03M 61.97M 65.23M −31.84M −103.64M
4 fresh tags + 8 fresh mentions 569.51M 335.78M 406.27M +172.55M −164.06M
4 reused 61-char tags + 8 mentions 586.32M 346.79M 417.02M +177.49M —

Example post ids on 5Kucw9g9…: 8eQVgzNVR3se83nRLXJqGQ9VGMJ391EAvHvha9F9wbXp (worst shape), 8mkrPAULrENkYcH9jbCYNqqXxHUXg6hjG2cnvDUowJEU (1 tag).

Moderator-triggered paths credit the author the same way:

Moderator action Author gets Moderator pays
Unhide, then the author's normal tombstone +178.53M (worst shape: +523.39M) 245.69M
Change modLabel only (re-keys the [mod, $moderatedAt] log entry) +15.69M 23.85M
mod 1→2 +130.89M 204.50M
mod 2→1 +184.42M 143.65M
Re-hide after an unhide +50.64M —

Repeated hide/unhide cycles move credits from the moderator to the author without limit.

Expected

Storage a moderator pays for should be flagged with the moderator, so the moderator gets the refund when it's removed. At minimum this applies to every new index element and subtree a moderator change inserts. The document item may reasonably keep the owner's flags (one owner per element; the moderator's share of it is small: $moderatedBy + $moderatedAt + the changed fields, 42 B here).

Suggested fix

In contract_user_moderation_transition.rs:328-330, keep the owner flags for the document item but pass moderator-owned flags (StorageFlags::new_single_epoch(epoch.index, Some(moderator_id))) for the index elements the update inserts. This needs UpdateDocument (or the moderation path) to carry separate flags for index references and subtrees.

#5574 (re-keys charged as delete + insert) would, if fixed by carrying the old element's flags onto moved entries, remove most of these bytes from the moderator's bill in the first place. Each fix alone stops the author's profit.

Contract-side workaround and its limit

A contract can stop the author-only path by freezing the lane-keyed fields of a hidden document through a tombstone: a conditional immutable entry with when: {"anyOf":[{"absent":"deleted"},{"present":"$old.mod"}]}. This is tested on BdjWpPEc…: every author-only attempt was refused, and the worst shape nets the author −67.96M.

It can't stop the moderator-triggered paths, because a condition can't read whether a document was ever moderated: $old.$moderatedAt is refused with 10231. Readable moderation system fields in replace conditions would let contracts close more of this, but the fix belongs in Drive.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions