Skip to content
Business logic Audit finding Intermediate

update_market() nextEpoch calculation incorrect

The update_market() function calculates nextEpoch incorrectly when crossing an epoch boundary, using i + BLOCK_EPOCH instead of epoch + BLOCK_EPOCH. This causes accCantoPerShare to be over- or under-calculated, potentially increasing allocated rewards.

bin2chen Follow with xuwinnie

Originally disclosed on Code4rena

Submitted by bin2chen, also found by xuwinnie

A very important logic of update_market() is to update accCantoPerShare. When updating, if it crosses the epoch boundary, it needs to use the corresponding epoch’s cantoPerBlock[epoch]. For example: cantoPerBlock[100000] = 100 cantoPerBlock[200000] = 0 (No reward) lastRewardBlock = 199999 block.number = 200002

At this time, accCantoPerShare needs to be increased: = cantoPerBlock[100000] * 1 Delta + cantoPerBlock[200000] *2 Delta = 100 * (200000 - 199999) + 0 * (200002 - 200000) = 100

The code is as follows:

    function update_market(address _market) public {
        require(lendingMarketWhitelist[_market], "Market not whitelisted");
        MarketInfo storage market = marketInfo[_market];
        if (block.number > market.lastRewardBlock) {
            uint256 marketSupply = lendingMarketTotalBalance[_market];
            if (marketSupply > 0) {
                uint256 i = market.lastRewardBlock;
                while (i < block.number) {
                    uint256 epoch = (i / BLOCK_EPOCH) * BLOCK_EPOCH; // Rewards and voting weights are aligned on a weekly basis
@>                  uint256 nextEpoch = i + BLOCK_EPOCH;
                    uint256 blockDelta = Math.min(nextEpoch, block.number) - i;
                    uint256 cantoReward = (blockDelta *
                        cantoPerBlock[epoch] *
                        gaugeController.gauge_relative_weight_write(_market, epoch)) / 1e18;
                    market.accCantoPerShare += uint128((cantoReward * 1e18) / marketSupply);
                    market.secRewardsPerShare += uint128((blockDelta * 1e18) / marketSupply); // TODO: Scaling
                    i += blockDelta;
                }
            }
            market.lastRewardBlock = uint64(block.number);
        }
    }

The above code, the calculation of nextEpoch is wrong

uint256 nextEpoch = i + BLOCK_EPOCH;

The correct one should be

uint256 nextEpoch = epoch + BLOCK_EPOCH;

As a result, the increase of accCantoPerShare becomes: = cantoPerBlock[100000] * 3 Delta = 100 * (200002 - 199999) = 300

Impact

The calculation of update_market() is incorrect when crossing the epoch boundary, causing the calculation of accCantoPerShare to be incorrect, which may increase the allocated rewards.

Proof of Concept

The following code demonstrates the above example, when added to LendingLedgerTest.t.sol:

    function test_ErrorNextEpoch() external {
        vm.roll(fromEpoch);
        whiteListMarket();
        //only set first , second cantoPerBlock = 0
        vm.prank(goverance);
        ledger.setRewards(fromEpoch, fromEpoch, amountPerBlock);
        vm.prank(lendingMarket);
        ledger.sync_ledger(lender, 1e18);

        //set lastRewardBlock = nextEpoch - 10
        vm.roll(fromEpoch + BLOCK_EPOCH - 10);
        ledger.update_market(lendingMarket);

        //cross-border
        vm.roll(fromEpoch + BLOCK_EPOCH + 10);
        ledger.update_market(lendingMarket);

        //show more
        (uint128 accCantoPerShare,,) = ledger.marketInfo(lendingMarket);
        console.log("accCantoPerShare:",accCantoPerShare);
        console.log("more:",accCantoPerShare - amountPerEpoch);

    }
$ forge test -vvv --match-test test_ErrorNextEpoch

Running 1 test for src/test/LendingLedger.t.sol:LendingLedgerTest
[PASS] test_ErrorNextEpoch() (gas: 185201)
Logs:
  accCantoPerShare: 1000100000000000000
  more: 100000000000000

Recommended Mitigation

    function update_market(address _market) public {
        require(lendingMarketWhitelist[_market], "Market not whitelisted");
        MarketInfo storage market = marketInfo[_market];
        if (block.number > market.lastRewardBlock) {
            uint256 marketSupply = lendingMarketTotalBalance[_market];
            if (marketSupply > 0) {
                uint256 i = market.lastRewardBlock;
                while (i < block.number) {
                    uint256 epoch = (i / BLOCK_EPOCH) * BLOCK_EPOCH; // Rewards and voting weights are aligned on a weekly basis
-                   uint256 nextEpoch = i + BLOCK_EPOCH;
+                   uint256 nextEpoch = epoch + BLOCK_EPOCH;

Alex the Entreprenerd (Judge) commented:

The Warden has shown a mistake in accounting, due to relying on a spot time, it is possible to influence the math in a way that would result in incorrect accounting.

Because of this I agree with High Severity.

OpenCoreCH (Canto) confirmed


The original report as published on Code4rena, by bin2chen. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely, the nextEpoch variable is calculated as i + BLOCK_EPOCH instead of epoch + BLOCK_EPOCH, so when i is not at the start of an epoch, the next epoch boundary is misaligned.

Impact

The incorrect calculation may increase the allocated rewards, leading to incorrect accounting and potentially excessive reward distribution.

What the attacker needed

Market must be whitelisted; marketSupply > 0; block.number > lastRewardBlock; crossing an epoch boundary

The fix

The fix is to change the nextEpoch calculation to use epoch + BLOCK_EPOCH instead of i + BLOCK_EPOCH.

How it was found

  1. Set rewards for the first epoch only, with subsequent epochs having zero rewards.
  2. Advance the block number to near the end of the first epoch and call update_market().
  3. Advance the block number past the epoch boundary and call update_market() again.
  4. Observe that accCantoPerShare is higher than expected, showing extra rewards were allocated.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This report is about a reward calculation bug in a smart contract. The contract gives out rewards per block, but the reward rate changes at certain block intervals called epochs. When a user's action spans across an epoch boundary, the code should split the reward calculation into two parts: one for the old epoch and one for the new epoch. The bug causes it to use the wrong epoch's rate for the entire period, leading to incorrect reward amounts.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Canto

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.