Repository navigation
Round the redeemCustom prorata bound with basketsNeeded - #1312
Conversation
redeemCustom() bounded each withdrawal by `balance * amount / totalSupply`,
which rounds separately from the reduction in basketsNeeded. On a historical
basket that over-weights a token, the bound applies and a redemption could
lower that balance's ratio to basketsNeeded by a fraction of a token-wei.
Bound by `balance * baskets / basketsNeeded` instead, where `baskets` is what
basketsNeeded is reduced by, and take the share of the balance truncated to
18 decimals, as Asset.bal() does. A custom redemption can then no longer
lower the ratio of any balance to basketsNeeded, including for tokens with
more than 18 decimals.
- P0 takes the share of asset.bal() and shifts to {qTok}. P1 computes the
same value in {qTok} to save bytecode.
- ReadFacet.redeemCustom() and ReadFacet.redeem() (`available`) use the same
bound, so the facade quotes the exact withdrawal.
- Compile RTokenP1 with 141 optimizer runs, the most that keeps it under the
contract size limit (24551 bytes).
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…t for overrides - ReadFacet.redeem(): restore the original available calculation. RToken.redeem() has no prorata bound, so there is nothing to mirror, and the facade serves older RTokens too. - hardhat.config: per-file compiler overrides replace the default settings, so request storageLayout for them explicitly (RToken and BackingManager). - Tests: cover 6 decimals and assert the exact prorata share. - Changelog: note dust-only redemptions revert with empty redemption. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Replaces the unchecked exponent in the redeemCustom prorata bound with a checked loop, and restates the comments around it. Same result. A checked exponent does not fit at any optimizer setting; the loop fits at 126 runs (24,544 bytes). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Use asset.bal().muluDivu().shiftl_toUint() for the prorata bound, replacing the raw-unit form. To make room, call the governance, notFrozen, and notTradingPausedOrFrozen checks through private functions, as StRSRP1 does. RTokenP1 is 24,572 bytes at 145 optimizer runs. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…prorata-rounding Stack on #1320. - CHANGELOG: keep both sides. - hardhat.config: drop the BackingManagerP1 compiler override. With the stack it is 24,333 bytes at the default 200 optimizer runs. RTokenP1 is unaffected and stays at 145 runs (24,572 bytes). - Tests: call the overloaded refreshBasket() by full signature. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…prorata-rounding Brings in the base branch's latest commits so the stack lines up. They net to no code change, so this branch's tree is unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Stacked on #1320.
Summary
RToken.redeemCustom()bounds each withdrawal by the redeemer's prorata share of the BackingManager's balance. That share wasbalance * amount / totalSupply, which rounds separately from the reduction inbasketsNeeded(basketsNeeded * amount / totalSupply). On a historical basket that gives a token more weight than the current one, the bound is what limits the withdrawal, and the two roundings could leave that balance a fraction of a token-wei lower relative tobasketsNeededthan before the redemption.redeem()does not have this property, because its withdrawal is derived from the already-rounded basket amount.This PR bounds the withdrawal by
balance * baskets / basketsNeededinstead, wherebasketsis the amountbasketsNeededis reduced by. The share is taken of the balance truncated to 18 decimals, asAsset.bal()does, so it is a share of the same balanceBasketHandler.basketsHeldBy()uses. After this change a custom redemption cannot lower the ratio of any balance tobasketsNeeded, including for collateral with more than 18 decimals.Changes
contracts/p0/RToken.sol: take the share ofasset.bal()and shift to{qTok}, the same wayquote()produces redemption amounts. The bound is now applied before the recipient balances are saved, as in P1.contracts/p1/RToken.sol: the same expression as P0. To make room for it, thegovernance,notFrozen, andnotTradingPausedOrFrozenchecks are called through private functions, asStRSRP1already does. Who can call what is unchanged.contracts/facade/facets/ReadFacet.sol:redeemCustom()uses the same bound, so the facade quotes the exact withdrawal.redeem()is unchanged.hardhat.config.ts: compileRTokenP1with 145 optimizer runs, and drop theBackingManagerP1override. See below. A per-file compiler override replaces the default settings, so theRTokenoverride requestsstorageLayoutexplicitly; without it the upgrade-safety checks have no layout for that contract.CHANGELOG.md: 4.3.0 entries.Contract size
RTokenP1was 24,555 bytes, 21 under the limit. The new bound brings it to 25,647 bytes at 200 optimizer runs and 25,215 at 1 run, so on its own it does not fit at any setting.Each use of a modifier copies its body into the function.
RTokenP1usedgovernancefour times, andnotFrozenandnotTradingPausedOrFrozentwice each. Calling them through private functions saves 893 bytes at 200 runs, which leavesRTokenP1at 24,754. 145 is the most runs that fits: 24,572 bytes at 145, 24,675 at 146.BackingManagerP1no longer needs its 100-run override. With the changes below this PR in the stack it is 24,333 bytes at the default 200 runs, so the override and its changelog line are removed.One ordering difference follows from this: in
redeemTo(),redeemCustom()andmonetizeDonations()the frozen or paused check now runs after the reentrancy lock is taken instead of before. Either way the call reverts.Behavior change
When the bound applies, a withdrawal can be smaller than before by dust. In sampling the difference was at most a few hundred wei for an 18-decimal token. The bound never pays more than before. A redemption whose only withdrawals were that dust now reverts with
empty redemption. Redemptions where the bound does not apply are unchanged.A facade built from this branch quotes the new bound. Against an RToken that has not been upgraded it can under-quote a custom redemption by that dust.
Testing
New tests in
test/RToken.test.ts, for 6-, 18- and 21-decimal collateral, on P0 and P1: repeated custom redemptions on a historical basket must pay out exactly the sharebalance * baskets / basketsNeeded(of the balance at 18 decimals), must not lower the ratio of the balance tobasketsNeeded, must leave the RToken fully collateralized, and must pay out exactly whatReadFacet.redeemCustom()quotes.Checked that the new tests fail:
RTokenand the previousReadFacetbalance * baskets / basketsNeededthat does not truncate to 18 decimalsRan on P0 and P1:
RToken,Facade,Recollateralization,Main,Upgradeability,scenario/BadCollateralPlugin, and the#fastset.🤖 Generated with Claude Code
Summary by CodeRabbit