Repository navigation
nft live until ledger upper bound - #584
Conversation
WalkthroughThe NFT module's Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #584 +/- ##
=======================================
Coverage 96.23% 96.23%
=======================================
Files 57 57
Lines 5497 5498 +1
=======================================
+ Hits 5290 5291 +1
Misses 207 207 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
packages/tokens/src/non_fungible/storage.rs (2)
448-458:⚠️ Potential issue | 🟡 MinorDoc comment for
approve_for_owneris stale after this change.Two issues:
- Lines 448–451: The parameter note still says
live_until_ledgeris "implicitly bounded … specifying a higher value will cause the code to panic". This is no longer accurate — the function now explicitly validates the upper bound and returnsInvalidLiveUntilLedger.- Lines 455–458: The
# Errorsentry forInvalidLiveUntilLedgeronly says "less than the current ledger number"; it should also cover the upper-bound case.📝 Proposed doc update
- /// `live_until_ledger` argument is implicitly bounded by the maximum - /// allowed TTL extension for a temporary storage entry and specifying a - /// higher value will cause the code to panic. + /// Must be between the current ledger sequence and `max_live_until_ledger` + /// (inclusive), or `0` to revoke. /// /// # Errors /// /// * [`NonFungibleTokenError::InvalidApprover`] - If the owner address is /// not the actual owner of the token. - /// * [`NonFungibleTokenError::InvalidLiveUntilLedger`] - If the ledger - /// number is less than the current ledger number. + /// * [`NonFungibleTokenError::InvalidLiveUntilLedger`] - If the ledger + /// number is less than the current ledger sequence or greater than + /// `max_live_until_ledger`.The same "implicitly bounded" language in the
approvefunction doc at lines 303–305 should be updated analogously.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/tokens/src/non_fungible/storage.rs` around lines 448 - 458, Update the doc comments for approve_for_owner and approve to reflect the new explicit validation: replace the "implicitly bounded ... will cause the code to panic" wording with text stating that live_until_ledger is explicitly validated and values above the allowed TTL will return NonFungibleTokenError::InvalidLiveUntilLedger; also expand the # Errors entry for NonFungibleTokenError::InvalidLiveUntilLedger to mention both the case where live_until_ledger is less than the current ledger and the case where it exceeds the allowed maximum TTL.
372-378:⚠️ Potential issue | 🟠 Major
approve_for_allis missing the same upper-bound check added toapprove_for_owner.The same bug pattern from issue
#583still exists here: a caller can passlive_until_ledger = u32::MAX(or any value beyondmax_live_until_ledger) and the function will reach theextend_ttlcall at line 385 with a massivelive_forvalue, resulting in an SDK-level panic instead of a cleanInvalidLiveUntilLedgererror. Both approval functions should be consistent.🐛 Proposed fix for `approve_for_all`
- if live_until_ledger < current_ledger { + if live_until_ledger > e.ledger().max_live_until_ledger() + || live_until_ledger < current_ledger + { panic_with_error!(e, NonFungibleTokenError::InvalidLiveUntilLedger); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/tokens/src/non_fungible/storage.rs` around lines 372 - 378, approve_for_all is missing the upper-bound validation present in approve_for_owner, allowing callers to pass live_until_ledger > max_live_until_ledger (or u32::MAX) which leads to overflow in extend_ttl; add the same check used in approve_for_owner to approve_for_all: compute current_ledger via e.ledger().sequence() and compare live_until_ledger against max_live_until_ledger (and current_ledger as already done), and if it exceeds the maximum return the same NonFungibleTokenError::InvalidLiveUntilLedger error before calling extend_ttl to prevent SDK-level panics.
🧹 Nitpick comments (1)
packages/tokens/src/non_fungible/storage.rs (1)
480-490:e.ledger().sequence()is called twice; cache it likeapprove_for_alldoes.
e.ledger().sequence()is called at line 481 (inside the guard condition) and again at line 490 (live_forcalculation). The sibling functionapprove_for_allcaches this ascurrent_ledgerat line 372 and reuses it at line 384. Matching that pattern keeps the two functions consistent.♻️ Proposed refactor
+ let current_ledger = e.ledger().sequence(); + - if live_until_ledger > e.ledger().max_live_until_ledger() - || live_until_ledger < e.ledger().sequence() + if live_until_ledger > e.ledger().max_live_until_ledger() + || live_until_ledger < current_ledger { panic_with_error!(e, NonFungibleTokenError::InvalidLiveUntilLedger); } let approval_data = ApprovalData { approved: approved.clone(), live_until_ledger }; e.storage().temporary().set(&key, &approval_data); - let live_for = live_until_ledger - e.ledger().sequence(); + let live_for = live_until_ledger - current_ledger;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/tokens/src/non_fungible/storage.rs` around lines 480 - 490, The code in the approval path repeatedly calls e.ledger().sequence()—cache this value into a local variable (e.g., current_ledger) like approve_for_all does, and use that cached variable in both the guard that compares live_until_ledger and in the live_for calculation; update the guard that currently compares live_until_ledger < e.ledger().sequence() to use current_ledger and compute live_for as live_until_ledger - current_ledger, keeping ApprovalData and storage calls unchanged (refer to approve_for_all for the naming/pattern).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/tokens/src/non_fungible/storage.rs`:
- Around line 448-458: Update the doc comments for approve_for_owner and approve
to reflect the new explicit validation: replace the "implicitly bounded ... will
cause the code to panic" wording with text stating that live_until_ledger is
explicitly validated and values above the allowed TTL will return
NonFungibleTokenError::InvalidLiveUntilLedger; also expand the # Errors entry
for NonFungibleTokenError::InvalidLiveUntilLedger to mention both the case where
live_until_ledger is less than the current ledger and the case where it exceeds
the allowed maximum TTL.
- Around line 372-378: approve_for_all is missing the upper-bound validation
present in approve_for_owner, allowing callers to pass live_until_ledger >
max_live_until_ledger (or u32::MAX) which leads to overflow in extend_ttl; add
the same check used in approve_for_owner to approve_for_all: compute
current_ledger via e.ledger().sequence() and compare live_until_ledger against
max_live_until_ledger (and current_ledger as already done), and if it exceeds
the maximum return the same NonFungibleTokenError::InvalidLiveUntilLedger error
before calling extend_ttl to prevent SDK-level panics.
---
Nitpick comments:
In `@packages/tokens/src/non_fungible/storage.rs`:
- Around line 480-490: The code in the approval path repeatedly calls
e.ledger().sequence()—cache this value into a local variable (e.g.,
current_ledger) like approve_for_all does, and use that cached variable in both
the guard that compares live_until_ledger and in the live_for calculation;
update the guard that currently compares live_until_ledger <
e.ledger().sequence() to use current_ledger and compute live_for as
live_until_ledger - current_ledger, keeping ApprovalData and storage calls
unchanged (refer to approve_for_all for the naming/pattern).
Fixes #583
PR Checklist
Summary by CodeRabbit