Skip to content

Audit Fix#838

Merged
Kukoomomo merged 9 commits intomainfrom
feature/audit_fix
Dec 17, 2025
Merged

Audit Fix#838
Kukoomomo merged 9 commits intomainfrom
feature/audit_fix

Conversation

@Kukoomomo
Copy link
Copy Markdown
Contributor

@Kukoomomo Kukoomomo commented Dec 17, 2025

Summary by CodeRabbit

  • New Features

    • Enhanced access control logic for token registry management with improved owner and allow-list permission checks.
    • Added price ratio reset mechanism on token data changes to ensure recalculation accuracy.
  • Bug Fixes

    • Improved token registration validation with granular error handling for ID and address collisions.
    • Updated price calculation formula to use simplified ceiling-division logic.
  • Tests

    • Significantly expanded test coverage for access control, price calculations, and token operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@Kukoomomo Kukoomomo requested a review from a team as a code owner December 17, 2025 09:37
@Kukoomomo Kukoomomo requested review from panos-xyz and removed request for a team December 17, 2025 09:37
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 17, 2025

Warning

Rate limit exceeded

@Kukoomomo has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 22 minutes and 37 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between a86fe62 and 5aaa22d.

📒 Files selected for processing (3)
  • contracts/contracts/l2/system/IL2TokenRegistry.sol (2 hunks)
  • contracts/contracts/l2/system/L2TokenRegistry.sol (8 hunks)
  • contracts/contracts/test/L2TokenRegistry.t.sol (20 hunks)

Walkthrough

This PR refactors error semantics in the token registry interface (distinguishing tokenID vs address collisions), updates access control logic to explicitly handle owner and allow-list permissions, simplifies price calculation with ceiling division, and adds comprehensive test coverage for data consistency and batch operations.

Changes

Cohort / File(s) Summary
Error Definition Updates
contracts/contracts/l2/system/IL2TokenRegistry.sol
Renamed TokenAlreadyRegistered() to TokenIDAlreadyRegistered(); added TokenAddressAlreadyRegistered() and ZeroTokenAmount(); removed InvalidPercent() and AlreadyInitialized()
Access Control & Validation Refactor
contracts/contracts/l2/system/L2TokenRegistry.sol
Refactored access control to use explicit owner vs allow-list checks; updated token registration error semantics to distinguish tokenID collisions from address collisions; tightened token info update validation with non-zero scale requirement; simplified price calculation with ceiling-division formula; added priceRatio reset to 0 after token data changes
Test Coverage Expansion
contracts/contracts/test/L2TokenRegistry.t.sol
Updated error expectations in existing tests; added 40+ new test cases covering access control logic (test_onlyAllowed_*), allow-list operations, token info updates with priceRatio resets, data consistency, batch operations, and edge cases

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Key areas requiring attention:

  • Access control refactor in L2TokenRegistry.sol — verify owner always-allowed semantics and allow-list disable/enable branching logic are correct
  • PriceRatio reset placement — ensure resets occur in all applicable state-change methods (updateTokenScale, updateTokenInfo)
  • Price calculation formula change — validate ceiling-division derivation matches intended behavior
  • Error path updates — confirm all error sites use new semantics (TokenIDAlreadyRegistered vs TokenAddressAlreadyRegistered)

Possibly related PRs

  • morph-l2/morph#834: Also modifies IL2TokenRegistry and L2TokenRegistry to update price calculation error handling and implements ZeroTokenAmount error with related test updates

Suggested reviewers

  • panos-xyz
  • FletcherMan
  • curryxbo

Poem

🐰 A rabbit hops through token rules,
With price math tightened, error jewels,
Access now explicit, clean and bright,
Ratios reset when changes ignite!
Data flows constant, tests align—
Registry logic now divine ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Audit Fix' is vague and generic, failing to convey specific information about the substantial changes made to error semantics, access control logic, and token registration validation across multiple contract files. Use a more descriptive title that captures the main changes, such as 'Refactor L2TokenRegistry error semantics and access control logic' or 'Update token registration error handling and access control validation'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

❤️ Share

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
contracts/contracts/l2/system/L2TokenRegistry.sol (1)

306-308: priceRatio reset ensures data consistency but has operational implications.

The unconditional reset of priceRatio to 0 after any updateTokenInfo call ensures consistency, but note that even minor changes (like toggling isActive) will require a subsequent updatePriceRatio call. Consider documenting that batchUpdateTokenStatus should be used for status-only changes to avoid the reset.

contracts/contracts/test/L2TokenRegistry.t.sol (1)

237-237: Hardcoded storage slot may need updating if contract layout changes.

The storage slot 151 for tokenRegistry mapping is hardcoded in multiple storage verification tests (lines 237, 271, 297, 331). While this is acceptable for testing internal implementation, consider adding a comment noting that this value must be updated if the contract's storage layout changes, or using Foundry's stdstore utilities for more robust slot detection.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18b49b2 and a86fe62.

📒 Files selected for processing (3)
  • contracts/contracts/l2/system/IL2TokenRegistry.sol (1 hunks)
  • contracts/contracts/l2/system/L2TokenRegistry.sol (8 hunks)
  • contracts/contracts/test/L2TokenRegistry.t.sol (7 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: test
  • GitHub Check: Analyze (rust)
  • GitHub Check: Analyze (go)
🔇 Additional comments (11)
contracts/contracts/l2/system/IL2TokenRegistry.sol (1)

61-72: Improved error semantics for token registration.

The new errors TokenIDAlreadyRegistered() and TokenAddressAlreadyRegistered() provide better granularity compared to a generic TokenAlreadyRegistered() error, making it easier for callers to understand the exact cause of registration failures. The ZeroTokenAmount() error clearly communicates the edge case in calculateTokenAmount.

contracts/contracts/l2/system/L2TokenRegistry.sol (5)

47-58: Access control logic is correct and well-documented.

The refactored onlyAllowed modifier correctly implements the intended behavior:

  • Owner always has access regardless of allowList status
  • When allowListEnabled is true, allowList users can access
  • When allowListEnabled is false, only owner can access

The logic !isOwner && !isAllowedByList properly handles all cases since isAllowedByList is false when allowListEnabled is false.


224-225: Error semantics correctly distinguish ID vs address collisions.

The checks properly use the new granular errors:

  • TokenIDAlreadyRegistered when the tokenID already has a registered address
  • TokenAddressAlreadyRegistered when the address is already mapped to another tokenID

275-280: Validation improvements in updateTokenInfo.

Adding the non-zero scale check ensures consistency with token registration. The address collision check correctly allows keeping the same address while preventing collisions with other tokenIDs.


442-446: Ceiling division with ZeroTokenAmount error is correct.

The ceiling division formula (numerator + ratio - 1) / ratio is standard and correct. The ZeroTokenAmount error appropriately signals that the calculation would result in zero tokens (which only happens when ethAmount is 0, since scale is validated to be non-zero).


494-496: Consistent priceRatio reset after scale update.

Resetting priceRatio to 0 when scale changes prevents inconsistent calculations, as the price formula depends on scale. This is consistent with the updateTokenInfo behavior.

contracts/contracts/test/L2TokenRegistry.t.sol (5)

106-122: Error expectations correctly updated for new error semantics.

The tests properly distinguish between TokenIDAlreadyRegistered (same ID, different address) and TokenAddressAlreadyRegistered (different ID, same address) scenarios.


406-415: Good test coverage for scale validation in updateTokenInfo.

This test correctly verifies that updateTokenInfo rejects zero scale values, maintaining consistency with the registration validation.


639-649: Correct test for ZeroTokenAmount error.

This test verifies that passing ethAmount = 0 results in the ZeroTokenAmount() revert, which is the expected behavior given the ceiling division formula.


655-861: Excellent comprehensive test coverage for access control.

The onlyAllowed modifier tests cover all edge cases including:

  • Owner access regardless of allowList status
  • AllowList user access when enabled
  • Reverts for non-allowList users
  • Reverts when allowList is disabled (even for listed users)
  • User removal from allowList

The truth table documentation at lines 805-815 is particularly helpful for understanding the expected behavior.


1184-1507: Thorough test coverage for priceRatio reset behavior.

The test suite comprehensively covers:

  • Reset behavior on various update operations
  • Data consistency verification after scale changes
  • Batch operation isolation (one token's reset doesn't affect others)
  • Recovery workflow documentation
  • Preservation of other token data during reset

The test_data_consistency_after_scale_update test at lines 1264-1297 is particularly valuable for demonstrating that calculations remain consistent when both scale and priceRatio are updated appropriately.

@Kukoomomo Kukoomomo merged commit e64256e into main Dec 17, 2025
9 checks passed
@Kukoomomo Kukoomomo deleted the feature/audit_fix branch December 17, 2025 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants