Skip to content

Commit

Permalink
test: add more assumes, fix pow problem
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAustrian committed May 15, 2024
1 parent a42f561 commit 09dc5cd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 20 additions & 3 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {BMath} from 'contracts/BMath.sol';
import {IERC20} from 'contracts/BToken.sol';
import {Test} from 'forge-std/Test.sol';
import {LibString} from 'solmate/utils/LibString.sol';
import {Pow} from 'test/unit/Pow.sol';
import {Utils} from 'test/unit/Utils.sol';

// TODO: remove once `private` keyword is removed in all test cases
Expand All @@ -24,6 +25,10 @@ abstract contract BasePoolTest is Test, BConst, Utils, BMath {
MockBPool public bPool;
address[TOKENS_AMOUNT] public tokens;

// Deploy this external contract to perform a try-catch when calling bpow.
// If the call fails, it means that the function overflowed, then we reject the fuzzed inputs
Pow public pow = new Pow();

function setUp() public {
bPool = new MockBPool();

Expand Down Expand Up @@ -137,13 +142,13 @@ abstract contract BasePoolTest is Test, BConst, Utils, BMath {
}

function _assumeCalcSingleInGivenPoolOut(
uint256,
uint256 _tokenInBalance,
uint256 _tokenInDenorm,
uint256 _poolSupply,
uint256 _totalWeight,
uint256 _poolAmountOut,
uint256
) internal pure {
) internal view {
uint256 _normalizedWeight = bdiv(_tokenInDenorm, _totalWeight);
uint256 _newPoolSupply = badd(_poolSupply, _poolAmountOut);
vm.assume(_newPoolSupply > _poolSupply);
Expand All @@ -153,6 +158,18 @@ abstract contract BasePoolTest is Test, BConst, Utils, BMath {
uint256 _poolRatio = bdiv(_newPoolSupply, _poolSupply);
vm.assume(_poolRatio < MAX_BPOW_BASE);
vm.assume(BONE > _normalizedWeight);

uint256 _boo = bdiv(BONE, _normalizedWeight);
uint256 _tokenRatio;
try pow.pow(_poolRatio, _boo) returns (uint256 _result) {
// pow didn't overflow
_tokenRatio = _result;
} catch {
// pow did an overflow. Reject this inputs
vm.assume(false);
}

vm.assume(_tokenRatio < type(uint256).max / _tokenInBalance);
}
}

Expand Down Expand Up @@ -1021,7 +1038,7 @@ contract BPool_Unit_JoinswapPoolAmountOut is BasePoolTest {
_setTotalWeight(_fuzz.totalWeight);
}

function _assumeHappyPath(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) internal pure {
function _assumeHappyPath(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) internal view {
// safe bound assumptions
_fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE);
Expand Down
10 changes: 10 additions & 0 deletions test/unit/Pow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import {BNum} from 'contracts/BNum.sol';

contract Pow is BNum {
function pow(uint256 _base, uint256 _exp) public pure returns (uint256 _result) {
_result = bpow(_base, _exp);
}
}

0 comments on commit 09dc5cd

Please sign in to comment.