Skip to content

feat: add storage TTL bump-on-touch for all persistent keys (#19) - #67

Open
malaysiaonelove wants to merge 1 commit into
Kqirox:mainfrom
malaysiaonelove:feat/issue-19-storage-ttl-bump-on-touch
Open

feat: add storage TTL bump-on-touch for all persistent keys (#19)#67
malaysiaonelove wants to merge 1 commit into
Kqirox:mainfrom
malaysiaonelove:feat/issue-19-storage-ttl-bump-on-touch

Conversation

@malaysiaonelove

Copy link
Copy Markdown

Summary

Implements Issue #19 — storage TTL handling and bump-on-touch for every persistent read/write site across all six Orivex contracts.

Changes

New: contracts/common crate

  • LEDGER_BUMP_PERSISTENT = 535_000 (≈ 30 days at 5 s/ledger)
  • LEDGER_THRESHOLD_PERSISTENT = 517_000 (threshold guard to avoid redundant bumps on hot keys)
  • bump_persistent(&env, &key) generic helper wrapping extend_ttl

TTL bumps added to all 6 contracts

Contract Persistent key(s) Bump sites
course-registry Course(u32), Progress(Address, u32) create_course, update_metadata, enroll, set_course_status, is_course_finished, get_course, get_progress, transfer_ownership, complete_module
badge-nft UserBadges(Address) mint_badge, revoke_badge, get_badges (transitively covers get_badge_count, has_badge)
reward-pool Spender(Address) add_approved_spender, distribute_reward
stake-vault UserStake(Address) stake, unstake, get_multiplier
governance Proposal(u32), UserVote(Address, u32) get_proposal, cast_vote, cancel_proposal, execute_proposal
quest-engine Quest(u32), Submission(Address, u32) create_build_quest, create_explore_quest, get_quest, submit_proof, get_submission, review_submission, refund_quest, batch_review_submissions, verify_explore_quest

Audit test

contracts/common/src/ttl_audit_test.rs — 6 tests (one per contract) that:

  1. Write a persistent key via the contract's public API
  2. Advance the ledger 100,000 sequences via env.ledger().with_mut(|li| li.sequence_number += 100_000)
  3. Read the key back and assert the value is unchanged

Run with:

cd contracts && cargo test ttl_audit

README

Full Storage TTL Semantics section added documenting constants, policy, code examples, covered-keys table, audit test instructions, and fee considerations.

Fee impact

The threshold guard (LEDGER_THRESHOLD_PERSISTENT = 517_000) means the host only writes a new TTL when the current one is genuinely close to expiry — roughly once every ≈ 25 hours on a continuously-touched key. Redundant calls are free.

Closes #19

- Add contracts/common crate with LEDGER_BUMP_PERSISTENT (535_000) and
  LEDGER_THRESHOLD_PERSISTENT (517_000) constants and bump_persistent()
  helper that wraps extend_ttl with a threshold guard to minimize fees

- course-registry: bump DataKey::Course(u32) and DataKey::Progress(Address, u32)
  on every persistent read and write; has() guard on optional progress keys

- badge-nft: bump DataKey::UserBadges(Address) on every read and write;
  get_badge_count and has_badge get the bump transitively via get_badges

- reward-pool: bump DataKey::Spender(Address) after write in
  add_approved_spender and after authorized read in distribute_reward

- stake-vault: bump DataKey::UserStake(Address) after read and write in
  stake; after read in unstake (key is then removed); has() guard in
  get_multiplier for non-stakers

- governance: bump DataKey::Proposal(u32) and DataKey::UserVote(Address, u32)
  on every persistent read and write across get_proposal, cast_vote,
  cancel_proposal, and execute_proposal

- quest-engine: bump DataKey::Quest(u32) and DataKey::Submission(Address, u32)
  on every persistent read and write; is_some() guard in get_quest and
  get_submission for missing-key paths

- Add contracts/common/src/ttl_audit_test.rs with six audit tests (one per
  contract) that write a persistent key, advance the ledger 100,000 sequences,
  then assert the value is still readable (run: cargo test ttl_audit)

- Update README.md with Storage TTL Semantics section documenting constants,
  bump-on-touch policy, covered keys table, audit test instructions, and
  fee considerations

Closes Kqirox#19
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.

[19] Add storage TTL handling analysis and bump-on-touch for all persistent reads

1 participant