Skip to content

refactor: consolidate DataKey::Paused into CommonDataKey - #649

Open
Rafiat30 wants to merge 1 commit into
Betta-Pay:mainfrom
Rafiat30:refactor/consolidate-pause-datakey
Open

refactor: consolidate DataKey::Paused into CommonDataKey#649
Rafiat30 wants to merge 1 commit into
Betta-Pay:mainfrom
Rafiat30:refactor/consolidate-pause-datakey

Conversation

@Rafiat30

Copy link
Copy Markdown

Closes #573

Summary

Both governance_contract::DataKey and settlement_contract::DataKey declared their own Paused variant, even though both contracts already read and wrote the pause flag exclusively through bettapay_common::storage::is_paused / set_paused, which operate on bettapay_common::CommonDataKey::Paused. The local Paused variants in each contract's own DataKey enum were dead code — never constructed or matched anywhere except their own declaration — left over from before the shared helper existed. Their continued presence is exactly what the issue describes: duplicated key names, a shared-keyspace illusion, and doc ambiguity about which enum actually owns the key.

This PR removes the duplicate declarations so CommonDataKey::Paused is the single source, and documents what "storage separation" actually means for a key type shared this way (each contract's instance storage is a distinct ledger entry — sharing the enum shares only the wire shape of the key, never any data).

Changes

Modified files

  • governance_contract/src/lib.rs — removed the unused Paused variant from DataKey; rewrote the comment above the enum to correctly list only RecoveryAddress/PendingRecovery/Paused as living in CommonDataKey (the previous comment incorrectly grouped Admin in with them, even though Admin has always stayed on each contract's own DataKey) and to explain storage separation.
  • settlement_contract/src/types.rs — same removal and comment fix for its DataKey enum.
  • bettapay_common/src/storage.rs — expanded the module doc comment: explains that CommonDataKey sharing a Rust type across contracts does not share any on-chain data (Soroban instance storage is scoped per contract address), and reworked the old SCVal-encoding-compatibility note into a historical note now that the duplicate variants it justified have been removed.

Test files

  • bettapay_common/test_snapshots/storage/tests/is_paused_defaults_to_false_and_round_trips_via_common_data_key.1.json (new)
  • bettapay_common/test_snapshots/storage/tests/two_contract_instances_have_independent_paused_flags.1.json (new)

No production contract behavior changed: is_paused/set_paused already read/wrote CommonDataKey::Paused before this PR; removing the dead local variants doesn't touch any storage path either contract actually exercises.

Implementation details

  • Verified the local Paused variants were genuinely unreferenced (grep -rn "DataKey::Paused" across both contracts only ever matched the enum declaration itself, never a .get/.set/.has/.remove call site) before removing them.
  • Kept the change scoped to Paused specifically, per the issue title. RecoveryAddress and PendingRecovery have the identical dead-variant pattern in both contracts' local DataKey enums (the existing comment already claimed they "live in CommonDataKey instead of here" even before this PR) — left untouched to keep this PR focused on a single concern per CONTRIBUTING.md; a natural follow-up if the maintainers want it.
  • #[contracttype]-derived enums encode on-chain by variant name only (not by the parent enum's Rust name), which is why this consolidation needed no storage migration — documented as a historical note in storage.rs since it's still useful context for why this was safe, even though the duplicate variants that made the point concrete are now gone.

Tests added

  • is_paused_defaults_to_false_and_round_trips_via_common_data_key — registers a dummy contract, then asserts is_paused reads back false with no entry written, and that set_paused(true)/set_paused(false) round-trip correctly, all against CommonDataKey::Paused directly.
  • two_contract_instances_have_independent_paused_flags — registers two separate contract instances, pauses one via CommonDataKey::Paused, and asserts the other instance's is_paused is unaffected. This is the executable proof that sharing the key type across contracts does not share any pause state between them — the "document storage separation" half of the issue.

Both new tests live in bettapay_common::storage's existing #[cfg(test)] mod tests block, alongside the existing primary_admin_* tests, using the same Env::default() + testutils::Address pattern already used there (plus a minimal #[contract] struct DummyContract;, needed because Env::as_contract requires a registered contract address to switch storage context into).

How to test

# Build WASM once (governance_contract's tests embed it via include_bytes!)
cargo build --target wasm32-unknown-unknown --release

# Full workspace suite
cargo test --workspace

# Just the new/relevant tests
cargo test -p bettapay_common storage::tests

# Confirm no code references the removed variant anywhere
grep -rn "DataKey::Paused" governance_contract/src settlement_contract/src
# (only bettapay_common::CommonDataKey::Paused usages should remain)

# Clippy stays clean with the removed dead variants
cargo clippy --workspace --all-targets --all-features -- -D warnings

…aKey

governance_contract::DataKey and settlement_contract::DataKey each still
declared their own dead `Paused` variant even though both contracts had
already switched their actual reads/writes over to
bettapay_common::storage::is_paused/set_paused, which operate on
CommonDataKey::Paused. The local variants were never constructed or
matched anywhere outside their own enum declaration - a leftover from
before the shared helper existed - but their presence duplicated the key
name across three enums and made it look like each contract still owned
its own pause flag.

- Remove the unused `Paused` variant from governance_contract::DataKey
  and settlement_contract::DataKey. CommonDataKey::Paused is now the only
  declaration of this key in the workspace.
- Expand the module-level doc comment on bettapay_common::storage to
  explain what "storage separation" means for a key type shared this way:
  each contract's instance storage is a separate ledger entry, so sharing
  CommonDataKey does not share any data - it only shares the wire shape of
  the key. Reworded the SCVal-compatibility note (why this consolidation
  needed no storage migration) into a historical note now that the
  duplicate variants are gone.
- Add is_paused_defaults_to_false_and_round_trips_via_common_data_key and
  two_contract_instances_have_independent_paused_flags to
  bettapay_common::storage's test module, exercising is_paused/set_paused
  directly against CommonDataKey::Paused and confirming two contract
  instances sharing the key type still have fully independent pause
  flags.

RecoveryAddress and PendingRecovery have the identical dead-variant
pattern in both contracts' local DataKey enums; left untouched here to
keep this change scoped to the Paused key called out in the issue.

Closes Betta-Pay#573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Governance and settlement both define DataKey::Paused with element sharing implications

1 participant