Skip to content

Fungible: explicit total_supply in capped - #662

Merged
brozorec merged 1 commit into
mainfrom
fungible-cap
Mar 25, 2026
Merged

brozorec merged 1 commit into
mainfrom
fungible-cap

Conversation

@brozorec

@brozorec brozorec commented Mar 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #626

PR Checklist

  • Tests
  • Documentation

Summary by CodeRabbit

  • Breaking Changes

    • Cap validation function signature updated to require total supply as an explicit parameter, replacing the previous internal storage lookup approach. This affects implementations using capped token functionality.
  • Documentation

    • Clarified cap validation behavior and error handling in function documentation.

@brozorec
brozorec requested a review from ozgunozerk March 25, 2026 10:12
@brozorec brozorec self-assigned this Mar 25, 2026
@coderabbitai

coderabbitai Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The check_cap function signature is updated to accept the current total supply as an explicit parameter instead of fetching it from storage. All call sites are updated to provide the current total supply value, and documentation is clarified to reflect the new behavior.

Changes

Cohort / File(s) Summary
Function Signature Update
packages/tokens/src/fungible/extensions/capped/storage.rs
Updated check_cap to accept total_supply as explicit parameter; removed storage lookup for TotalSupply. Core cap enforcement logic (checked_add and overflow/exceeded cap validation) remains unchanged.
Documentation Clarification
packages/tokens/src/fungible/extensions/capped/mod.rs
Updated module-level documentation to clarify that check_cap panics when total_supply + amount would exceed the cap, reflecting the new parameter-based behavior.
Call Site Updates
examples/fungible-capped/src/contract.rs, packages/tokens/src/fungible/extensions/capped/test.rs
Updated all check_cap invocations to pass Base::total_supply(e) as the third argument, adapting to the new function signature.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • ozgunozerk

Poem

🐰 A cap that held a hidden flaw,
When mints slipped past without a gnaw,
But now with supply passed explicit and true,
The token stays bounded—secure through and through! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is incomplete and does not match the template requirements. While it references issue #626 and marks checklist items, it lacks a detailed description of the changes introduced. Add a comprehensive explanation of the changes in the 'Describe the changes introduced in this pull request' section. Explain why the total_supply parameter is now explicit, how it addresses the bypass vulnerability in issue #626, and provide context for understanding the PR's purpose.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: making total supply explicit in the capped extension for fungible tokens.
Linked Issues check ✅ Passed The code changes directly address the bypass vulnerability by making total_supply an explicit parameter in check_cap, preventing reliance on stale state and ensuring cap checks reflect the actual current supply.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #626: updating check_cap signature, adjusting documentation, and updating call sites in the capped extension module.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fungible-cap

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/tokens/src/fungible/extensions/capped/storage.rs (1)

64-73: Clarify the trust boundary for total_supply in docs.

Please explicitly state that total_supply must come from the same authoritative source used by mint state transitions (and be read immediately before mint), otherwise cap enforcement can be bypassed by stale input.

✍️ Suggested doc adjustment
-/// * `total_supply` - The current total supply of tokens.
+/// * `total_supply` - The current total supply of tokens from the authoritative
+///   supply source used by mint logic (e.g., `Base::total_supply`).
+///   Passing stale or derived values can invalidate cap enforcement.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tokens/src/fungible/extensions/capped/storage.rs` around lines 64 -
73, The documentation for check_cap does not specify the trust boundary for the
total_supply parameter; update the doc comment for pub fn check_cap(e: &Env,
amount: i128, total_supply: i128) to state that total_supply must be read from
the same authoritative mint state (the live storage used for mint transitions)
immediately before calling check_cap (i.e., do not pass stale or external
totals), and note that failing to do so can allow cap enforcement to be
bypassed; also reference query_cap and any mint-state accessor (e.g., the
contract's total_supply getter) so callers know the correct source to read from.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/tokens/src/fungible/extensions/capped/test.rs`:
- Around line 22-23: Add a targeted regression test in
packages/tokens/src/fungible/extensions/capped/test.rs that replicates the
stale/local-supply desynchronization bypass from issue `#626`: simulate a scenario
where total supply observed by the cap check (use check_cap and
Base::total_supply) is stale relative to a concurrent/local mint path and then
invoke the mint flow (Base::mint) to assert the cap cannot be exceeded;
specifically create steps that (1) set supply near the cap, (2) read/record a
stale supply value, (3) perform the alternate mint path that previously bypassed
the cap, and (4) assert via check_cap and a final Base::total_supply that total
supply ≤ cap. Reference and reuse existing helpers check_cap, Base::mint, and
Base::total_supply so the test proves the chosen mint flow blocks the bypass and
add equivalent cases for the other reported line ranges.

---

Nitpick comments:
In `@packages/tokens/src/fungible/extensions/capped/storage.rs`:
- Around line 64-73: The documentation for check_cap does not specify the trust
boundary for the total_supply parameter; update the doc comment for pub fn
check_cap(e: &Env, amount: i128, total_supply: i128) to state that total_supply
must be read from the same authoritative mint state (the live storage used for
mint transitions) immediately before calling check_cap (i.e., do not pass stale
or external totals), and note that failing to do so can allow cap enforcement to
be bypassed; also reference query_cap and any mint-state accessor (e.g., the
contract's total_supply getter) so callers know the correct source to read from.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4485a92b-7183-4f3e-a98c-97091fe35e7b

📥 Commits

Reviewing files that changed from the base of the PR and between ff17d24 and 0e69a88.

📒 Files selected for processing (4)
  • examples/fungible-capped/src/contract.rs
  • packages/tokens/src/fungible/extensions/capped/mod.rs
  • packages/tokens/src/fungible/extensions/capped/storage.rs
  • packages/tokens/src/fungible/extensions/capped/test.rs

@codecov

codecov Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.16%. Comparing base (ff17d24) to head (0e69a88).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #662      +/-   ##
==========================================
- Coverage   96.16%   96.16%   -0.01%     
==========================================
  Files          59       59              
  Lines        6134     6132       -2     
==========================================
- Hits         5899     5897       -2     
  Misses        235      235              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@brozorec
brozorec merged commit 2919d2c into main Mar 25, 2026
6 checks passed
@brozorec
brozorec deleted the fungible-cap branch March 25, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[M-02] - Capped Extension Cap Check Can Be Bypassed When Minting Does Not Update Local Total Supply

2 participants