Feat/staked keys regression test#563
Open
Unclebaffa wants to merge 4 commits into
Open
Conversation
- Add StakedBalance data key to track staked keys per (creator, holder) - Add staked_balance storage helper function - Implement stake_keys() to lock keys from being sold - Implement unstake_keys() to unlock previously staked keys - Implement get_staked_balance() to query staked amount - Implement get_liquid_balance() to query sellable amount - Modify sell_key() to check liquid balance (total - staked) instead of just total balance - Staked keys cannot be sold until unstaked - Prevents holders from selling keys they have locked/committed
- Test that selling 5 keys fails when only 4 liquid keys available (6 staked out of 10 total) - Test that selling exactly 4 liquid keys succeeds - Test that staked balance remains unchanged after failed and successful sell attempts - Verify InsufficientBalance error when attempting to sell more than liquid balance - Verify total, staked, and liquid balances are correctly maintained
The sell_key function sells one key at a time, so tests need to: 1. Successfully sell 4 liquid keys (one at a time) 2. Fail on the 5th attempt when liquid balance reaches 0 3. Verify staked balance remains unchanged throughout Previous tests incorrectly expected the first sell to fail.
- TEST_SUMMARY.md: documents all 14 tests and invariants verified - TEST_FIX_SUMMARY.md: explains the test fix for sell_key behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
I have successfully implemented key staking functionality for the creator-keys contract to prevent selling of staked keys. Here's what was done:
✅ Implementation Complete (Commit 1)
File Modified:
creator-keys/src/lib.rsAdded Storage Key:
StakedBalance(Address, Address)to track staked keys per (creator, holder) pairNew Functions Implemented:
stake_keys()- Locks keys from being soldunstake_keys()- Unlocks previously staked keysget_staked_balance()- View staked amountget_liquid_balance()- View sellable amount (total - staked)Modified
sell_key()Function:InsufficientBalanceif trying to sell more than liquid balance✅ Tests Complete (Commit 2)
File Created:
creator-keys/tests/sell_requires_liquid_balance.rsThree comprehensive test cases covering all acceptance criteria:
test_sell_reverts_when_attempting_to_use_staked_keysInsufficientBalancetest_sell_succeeds_within_liquid_balance_limittest_staked_balance_unchanged_after_sell_attempts✅ Code Formatting Summary
What Was Accomplished
1. Code Formatting ✅
cargo fmtto fix all formatting issues in both implementation and test files2. Comprehensive Invariant Tests Added ✅
Created 14 comprehensive tests organized into three categories:
Core Protection Tests (3 tests):
Invariant Tests (6 tests):
invariant_liquid_equals_total_minus_staked- Mathematical correctnessinvariant_staked_never_exceeds_total- Boundary enforcementinvariant_total_equals_liquid_plus_staked- Balance compositioninvariant_staking_preserves_total_balance- Total immutabilityinvariant_sell_only_reduces_liquid_not_staked- Sell isolationinvariant_staked_isolated_per_creator_holder_pair- Storage isolationEdge Case Tests (5 tests):
3. Seven Key Invariants Verified ✅
Files Modified/Created
creator-keys/src/lib.rs- Implementation (already committed)creator-keys/tests/sell_requires_liquid_balance.rs- 14 comprehensive testsSTAKING_IMPLEMENTATION.md- Implementation documentationTEST_SUMMARY.md- Test documentationAll Acceptance Criteria Met ✅
Testing Philosophy
The test suite follows a defense-in-depth approach:
This ensures the implementation is not only correct today but remains correct through future modifications.
Closes #546