feat(vault): Implemented book_session and deposit locking logic#14
Conversation
- Created types.rs to centralize data structures - Defined BookingRecord struct and BookingStatus enum - Aligned field names with issue specification (rate_per_second, max_duration) Refs: LightForgeHub#6
- Migrated storage logic to use refined types from types module - Updated DataKey and persistence methods - Enhanced storage organization for better maintainability Refs: LightForgeHub#6
- Renamed create_booking to book_session for consistency - Implemented deposit locking logic with specifying rate and duration - Added booking_created event for frontend tracking Refs: LightForgeHub#6
- Exposed book_session instead of create_booking - Registered types module as a public module - Updated interface to reflect refined parameter naming Refs: LightForgeHub#6
- Re-aligned existing tests with new naming and structure - Added test_book_session_balance_transfer to verify contract locking logic - Ensured 100% pass rate for current and new tests Refs: LightForgeHub#6
📝 WalkthroughWalkthroughThis PR renames Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant Contract as Payment Vault
participant Token as Token Contract
User->>Contract: book_session(user, expert, rate_per_second, max_duration)
Note over Contract: Calculate deposit = rate_per_second × max_duration
Contract->>Token: transfer(user, contract, deposit)
Token->>Token: Update balances
Token-->>Contract: Transfer success
Contract->>Contract: Create BookingRecord and store
Contract->>Contract: Emit booking_created event
Contract-->>User: Return booking_id
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
contracts/payment-vault-contract/src/contract.rs (2)
95-102: Add duration bounds validation and use checked multiplication for payout calculations.The
actual_durationparameter is accepted from an untrusted oracle caller without validating againstbooking.max_duration, allowing over-duration inputs to proceed. Additionally, the multiplicationbooking.rate_per_second * (actual_duration as i128)can silently overflow. While the subsequent checks catch negative refunds, this leaves room for calculation errors and inconsistent state. Usechecked_mul()and validateactual_duration <= booking.max_durationupfront.Suggested fix
- let expert_pay = booking.rate_per_second * (actual_duration as i128); + if actual_duration > booking.max_duration { + return Err(VaultError::InvalidAmount); + } + + let expert_pay = booking.rate_per_second + .checked_mul(actual_duration as i128) + .ok_or(VaultError::InvalidAmount)?;
26-46: Guard against i128 overflow in deposit calculations.The unchecked multiplications at line 42 (
book_session) and line 96 (finalize_session) can overflow without being caught. The current checkif total_deposit <= 0only detects negative wraps, missing positive overflows where the result wraps to a small positive value. This would cause incorrect token transfers.Use
checked_multo safely calculate:
rate_per_second * (max_duration as i128)at line 42booking.rate_per_second * (actual_duration as i128)at line 96Return
VaultError::InvalidAmounton overflow.Suggested fix for line 42
- let total_deposit = rate_per_second * (max_duration as i128); + let total_deposit = rate_per_second + .checked_mul(max_duration as i128) + .ok_or(VaultError::InvalidAmount)?;
Summary
This PR implements the "Book Session" logic for the SkillSphere Payment Vault contract as specified in Issue #6. It refactors the existing booking functionality to align with the project's decentralized identity/payment architecture, introducing a structured
BookingRecordand a "Pay per Second" deposit locking mechanism.Related Issue
Closes #6
Changes Made
src/types.rscontainingBookingRecordandBookingStatusto centralize contract data models.storage.rsto persistBookingRecordand aligned storage keys.book_sessionfunction withrequire_authverification.rate_per_second * max_duration).BookingCreatedevent emission for indexer visibility.book_sessionin the public contract implementation and updated cross-module imports.Testing Done
cargo test)cargo build --release).Quality Checklist
Summary by CodeRabbit
New Features
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.