# Determining how many votes to buy may run OOG.

Ethos ReputationMarket's linear decrement loop to find affordable vote count can exceed Base block gas limit, making large vote purchases unexecutable.

- Payout: Audit contest
- Severity: medium
- Program: Ethos Network
- Found by: bughuntoor
- Disclosed: 2024-12-23
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-12-ethos-update-judging/issues/43
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-12-ethos-update%3AM-1
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:dos

**Target:** ReputationMarket.sol buyVotes loop
**Root cause:** To find the max affordable votes, the contract decrements currentVotesToBuy one at a time and recomputes _calculateBuy each iteration (~33k gas per iteration) instead of binary searching.
**Steps:**
1. Set market base price 0.0001 ETH, one-sided market so Trust vote costs 0.00001 ETH.
2. User calls buyVotes with msg.value=1 ETH, maxVotesToBuy=100,000, minVotesToBuy=90,000 (10% slippage).
3. Price movement means only ~91,000 votes affordable, so loop runs ~9,000 iterations.
4. Each iteration costs ~33k gas; ~7,000 iterations exceed Base 240,000,000 gas limit, reverting OOG.
5. PoC: call LMSR.getCost in a loop measuring gasleft() to confirm ~33k gas per check.
**How found:** Code reading of the buyVotes affordability loop; noticed per-iteration cost and computed iterations needed to exceed Base block gas limit, then wrote a Foundry PoC measuring gas per _calculateBuy call.
**Insight:** When a contract searches for a max affordable amount by decrementing one unit at a time, compute iterations = (max-min) and multiply by per-iteration gas; large slippage windows on low-priced assets can exceed block gas limits even on cheap L2s.

## Proof of concept (copied exactly from the original report)

**Foundry test measuring gas per buy calculation**

```
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../src/LMSR.sol";

contract MyTest is Test {
    uint256 basePrice = 1e18;
    struct Market { 
        uint256[2] votes;
    }

    function test_gasCalc() public { 
        uint256 initYesVotes = 50;
        uint256 initNoVotes = 30;

        Market memory testMarket;
        testMarket.votes[0] = initYesVotes;
        testMarket.votes[1] = initNoVotes;

        uint256 gasLeft = gasleft();    

        uint256[] memory voteDelta = new uint256[](2);

        voteDelta[0] = testMarket.votes[0];
        voteDelta[1] = testMarket.votes[1] + 1;

        int256 costRatio = LMSR.getCost(
            initYesVotes, 
            initNoVotes, 
            voteDelta[0],
            voteDelta[1],
            1000);


        uint256 positiveCostRatio = costRatio > 0 ? uint256(costRatio) : uint256(costRatio * -1 );

        uint256 cost = positiveCostRatio * basePrice / 1e18;

        uint256 gasLeft2 = gasleft();
        uint256 gasConsumed = gasLeft - gasLeft2;
        console.log(gasConsumed);
    }
}
```

