feat: Escrow List Storage Exhaustion#196
Merged
Agbeleshe merged 2 commits intoApr 25, 2026
Merged
Conversation
Contributor
|
LGTM!! |
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.
Security & Scalability Enhancements for CraftNexus Escrow Contract
🎯 Overview
This PR implements four critical security and scalability improvements to the CraftNexus escrow smart contract, addressing storage limitations, fee recovery policies, release window constraints, and re-entrancy vulnerabilities.
📋 Changes Summary
1. ✅ Scalable Escrow List Storage
Problem: BuyerEscrows and SellerEscrows stored as vectors would exceed the 64KB per-entry limit with extensive transaction history, causing permanent failures for active users.
Solution: Replaced vector storage with indexed storage pattern using individual keys per escrow.
Implementation:
DataKeyvariants:BuyerEscrowIndexed,BuyerEscrowCount,SellerEscrowIndexed,SellerEscrowCountcreate_escrow_with_metadata()to use indexed storagemigrate_user_escrows()admin function for data migrationget_escrows_by_buyer()andget_escrows_by_seller()with fallback logicTests: 8 comprehensive tests covering indexed storage, migration, and backward compatibility
2. ✅ Configurable Expired Dispute Fee Policy
Problem: Platform lost fees when disputes expired without arbitrator resolution, even when the seller performed work.
Solution: Added configurable
ExpiredDisputeFeePolicyenum with 4 policy options.Implementation:
ExpiredDisputeFeePolicyenum:RefundFullNoPlatformFee(default) - Full refund to buyer, no platform feeRefundMinusPlatformFee- Refund minus platform fee (fee to platform)DeductFeeFromSeller- Full refund to buyer, fee deducted from seller's locked fundsSplitFee- 50/50 split of platform fee between buyer and sellerexpired_dispute_fee_policyfield toPlatformConfigresolve_expired_dispute()to apply configured policyupdate_expired_dispute_policy(),get_expired_dispute_policy()Tests: 12 comprehensive tests covering all policy options and edge cases
3. ✅ Minimum Release Window Constraint
Problem: Users could set 1-second release windows enabling "flash" auto-releases, bypassing escrow safety mechanisms.
Solution: Added configurable minimum release window with 1-day default.
Implementation:
DEFAULT_MIN_RELEASE_WINDOWconstant (86,400 seconds = 1 day)min_release_windowfield toPlatformConfigcreate_escrow_with_metadata()to enforce minimumset_min_release_window(),get_min_release_window()0 < window >= min_release_window <= MAX_TOTAL_RELEASE_WINDOWTests: 20 comprehensive tests covering validation, edge cases, and admin operations
4. ✅ Re-entrancy Protection via CEI Pattern
Problem: Re-entry guard ineffective against cross-contract callbacks. Several functions updated state AFTER external token transfers, creating re-entrancy vulnerabilities.
Solution: Implemented Checks-Effects-Interactions (CEI) pattern across all fund-moving functions.
Implementation:
Functions Fixed:
resolve_dispute()(line ~2200)resolve_expired_dispute()(line ~2297)accept_partial_refund()(line ~3126)cancel_recurring_escrow()(line ~3382)is_active = falseflag and active obligations decrement BEFORE token transferFunctions Verified Secure:
release_funds()- Already following CEI patternauto_release()- Already following CEI patternrefund()- Already following CEI patternTests: 9 comprehensive tests verifying CEI pattern implementation and state consistency
🧪 Testing
Test Coverage Summary
Test Files Created
src/scalability_test.rs- Indexed storage and migration testssrc/expired_dispute_fee_test.rs- Fee policy testssrc/min_release_window_test.rs- Release window validation testssrc/reentrancy_test.rs- CEI pattern and re-entrancy testsRunning Tests
🔧 Build Verification
Result: ✅ Build successful with no errors
📁 Files Modified
Core Contract
src/lib.rs- All feature implementations and fixesTest Files (New)
src/scalability_test.rssrc/expired_dispute_fee_test.rssrc/min_release_window_test.rssrc/reentrancy_test.rs🔒 Security Improvements
Before This PR
After This PR
🚀 Migration Guide
For Existing Deployments
1. Scalable Storage Migration
Note: Backward compatibility maintained - old vector storage still works until migrated.
2. Expired Dispute Fee Policy
3. Minimum Release Window
Note: Existing escrows are not affected. New escrows must meet the minimum.
📊 API Changes
New Admin Functions
Scalable Storage
Expired Dispute Fee Policy
Minimum Release Window
New Data Types
ExpiredDisputeFeePolicy Enum
New DataKey Variants
None - Fully Backward Compatible
All changes maintain backward compatibility:
🔍 Code Review Checklist
📚 Documentation
Implementation Summaries
SCALABILITY_IMPLEMENTATION_COMPLETE.mdEXPIRED_DISPUTE_FEE_IMPLEMENTATION.mdMIN_RELEASE_WINDOW_IMPLEMENTATION.mdREENTRANCY_FIX_COMPLETE.mdSECURITY_FIXES_SUMMARY.mdTechnical Details
craft-nexus-contract/SCALABILITY_UPGRADE.mdcraft-nexus-contract/EXPIRED_DISPUTE_FEE_POLICY.mdcraft-nexus-contract/MIN_RELEASE_WINDOW.mdcraft-nexus-contract/REENTRANCY_PROTECTION.mdQuick References
craft-nexus-contract/CEI_PATTERN_QUICK_REF.mdcraft-nexus-contract/EXPIRED_DISPUTE_QUICK_REFERENCE.mdcraft-nexus-contract/MIN_RELEASE_WINDOW_QUICK_REF.md🎯 Next Steps
Recommended Before Mainnet Deployment
Post-Deployment
🤝 Reviewers
Please pay special attention to:
📝 Additional Notes
Performance Considerations
Gas Optimization
Upgrade Path
✅ Definition of Done
🏆 Impact
This PR significantly enhances the security and scalability of the CraftNexus escrow contract:
Status: ✅ Ready for Review - All implementations complete, tested, and documented.
close #174
close #189
close #187
close #191