Skip to content
Business logic Audit finding Intermediate

update_market() market weight incorrect

The reward calculation in update_market() used block numbers as the time parameter for gaugeController.gauge_relative_weight_write(), but that function expects a timestamp. This mismatch caused the weight to be looked up for an epoch in 1970, resulting in near-zero rewards and preventing users from earning their fair share.

bin2chen Follow with xuwinnie

Originally disclosed on Code4rena

Submitted by bin2chen, also found by xuwinnie

In update_market() We need to get the weight percentage of the corresponding market epoch through gaugeController.

Then allocate cantoPerBlock[epoch] according to the percentage The main logic 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);
        }
    }
  1. Calculate epoch
  2. Then get the corresponding weight of the market through gaugeController.gauge_relative_weight_write(market,epoch)

The problem is that epoch is block number

market.lastRewardBlock = uint64(block.number)

uint256 epoch = (i / BLOCKEPOCH) * BLOCKEPOCH

But the second parameter of gaugeController.gauge_relative_weight_write(market,epoch) is time

gauge_relative_weight_write()->_gauge_relative_weight()

contract GaugeController {
@>  uint256 public constant WEEK = 7 days;
...
    function _gauge_relative_weight(address _gauge, uint256 _time) private view returns (uint256) {
@>      uint256 t = (_time / WEEK) * WEEK;
        uint256 total_weight = points_sum[t].bias;
        if (total_weight > 0) {
            uint256 gauge_weight = points_weight[_gauge][t].bias;
            return (MULTIPLIER * gauge_weight) / total_weight;
        } else {
            return 0;
        }
    }

For example, the current canto BLOCK: 7999034 After calculation in gaugeController, it is: 7999034 / WEEK * WEEK = 1970-04-02 The wrong time cycle, the weight obtained is basically 0, so the reward cannot be obtained.

Impact

Incorrectly using block number as a time parameter, unable to get weight, unable to accumulate rewards.

Recommended Mitigation

It is recommended that LendingLedger refer to GaugeController, and also use time to record epoch.

OpenCoreCH (Canto) confirmed and commented:

True, this was time in the past, will be changed.

Alex the Entreprenerd (Judge) commented:

The finding shows how, due to using the incorrect units (blocks instead of seconds), it is possible to cause the cantoReward math to be incorrect.

Based on the block the result would either cause a total loss of rewards (example from the warden) or an incorrect amount.

While the impact is limited to rewards, the incorrect formula seems to warrant a High Severity.


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

The code used block numbers instead of timestamps when calling gaugeController.gauge_relative_weight_write(), which expects a time value in seconds.

Impact

Rewards could be almost entirely lost or calculated incorrectly, but the impact is limited to reward distribution.

What the attacker needed

None

The fix

The fix is to use time (seconds) instead of block numbers for the epoch, as confirmed by the project.

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 smart contract bug where a function that calculates rewards used the wrong kind of number. The contract passed a block number (like a counter of how many blocks have been mined) to a function that expected a timestamp (seconds since 1970). Because the numbers are very different, the reward calculation looked up a time in 1970 and found almost no weight, so users got almost no rewards.

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.