From 259c89895cfd1373709a09d948cef8be2ea06b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Tue, 4 Jun 2024 14:02:41 +0200 Subject: [PATCH] fix: improving vague assumption (#55) * fix: improving vague assumption * fix: bounding totalWeight to its max * fix: rm min weight in bind method * fix: totalweight min bound --- test/unit/BPool.t.sol | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/unit/BPool.t.sol b/test/unit/BPool.t.sol index 91657644..d4d61b7d 100644 --- a/test/unit/BPool.t.sol +++ b/test/unit/BPool.t.sol @@ -156,7 +156,7 @@ abstract contract BasePoolTest is Test, BConst, Utils, BMath { uint256 _totalSupply ) internal pure { uint256 _normalizedWeight = bdiv(_tokenInDenorm, _totalWeight); - vm.assume(_normalizedWeight < BONE); // TODO: why this? if the weights are between allowed it should be fine + vm.assume(_normalizedWeight < bdiv(MAX_WEIGHT, MAX_TOTAL_WEIGHT)); uint256 _zaz = bmul(bsub(BONE, _normalizedWeight), _swapFee); uint256 _tokenAmountInAfterFee = bmul(_tokenAmountIn, bsub(BONE, _zaz)); @@ -373,7 +373,7 @@ contract BPool_Unit_GetTotalDenormalizedWeight is BasePoolTest { contract BPool_Unit_GetNormalizedWeight is BasePoolTest { function test_Returns_NormalizedWeight(address _token, uint256 _weight, uint256 _totalWeight) public { _weight = bound(_weight, MIN_WEIGHT, MAX_WEIGHT); - _totalWeight = bound(_totalWeight, MIN_WEIGHT, MAX_WEIGHT * MAX_BOUND_TOKENS); + _totalWeight = bound(_totalWeight, MIN_WEIGHT, MAX_TOTAL_WEIGHT); vm.assume(_weight < _totalWeight); _setRecord(_token, BPool.Record({bound: true, index: 0, denorm: _weight, balance: 0})); _setTotalWeight(_totalWeight); @@ -672,7 +672,7 @@ contract BPool_Unit_Bind is BasePoolTest { } _fuzz.balance = bound(_fuzz.balance, MIN_BALANCE, type(uint256).max); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT, MAX_TOTAL_WEIGHT - MIN_WEIGHT); + _fuzz.totalWeight = bound(_fuzz.totalWeight, 0, MAX_TOTAL_WEIGHT - MIN_WEIGHT); _fuzz.denorm = bound(_fuzz.denorm, MIN_WEIGHT, MAX_TOTAL_WEIGHT - _fuzz.totalWeight); } @@ -2371,7 +2371,7 @@ contract BPool_Unit_JoinswapExternAmountIn is BasePoolTest { // safe bound assumptions _fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_WEIGHT * MAX_BOUND_TOKENS); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max); _fuzz.tokenInBalance = bound(_fuzz.tokenInBalance, MIN_BALANCE, type(uint256).max); @@ -2551,7 +2551,7 @@ contract BPool_Unit_JoinswapPoolAmountOut is BasePoolTest { // safe bound assumptions _fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_WEIGHT * MAX_BOUND_TOKENS); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); _fuzz.poolAmountOut = bound(_fuzz.poolAmountOut, INIT_POOL_SUPPLY, type(uint256).max - INIT_POOL_SUPPLY); _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max - _fuzz.poolAmountOut); @@ -2636,7 +2636,7 @@ contract BPool_Unit_JoinswapPoolAmountOut is BasePoolTest { // Replicating _assumeHappyPath, but removing irrelevant assumptions and conditioning the revert _fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_WEIGHT * MAX_BOUND_TOKENS); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); _fuzz.tokenInBalance = bound(_fuzz.tokenInBalance, MIN_BALANCE, type(uint256).max / MAX_IN_RATIO); _fuzz.poolAmountOut = bound(_fuzz.poolAmountOut, INIT_POOL_SUPPLY, type(uint256).max - INIT_POOL_SUPPLY); _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max - _fuzz.poolAmountOut); @@ -2799,7 +2799,7 @@ contract BPool_Unit_ExitswapPoolAmountIn is BasePoolTest { // safe bound assumptions _fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_WEIGHT * MAX_BOUND_TOKENS); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max); // max @@ -2881,7 +2881,7 @@ contract BPool_Unit_ExitswapPoolAmountIn is BasePoolTest { // Replicating _assumeHappyPath, but removing irrelevant assumptions and conditioning the revert _fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_WEIGHT * MAX_BOUND_TOKENS); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); _fuzz.tokenOutBalance = bound(_fuzz.tokenOutBalance, MIN_BALANCE, type(uint256).max / MAX_OUT_RATIO); _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max); vm.assume(_fuzz.totalSupply < type(uint256).max - _fuzz.poolAmountIn); @@ -3063,7 +3063,7 @@ contract BPool_Unit_ExitswapExternAmountOut is BasePoolTest { // safe bound assumptions _fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT); _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * MAX_BOUND_TOKENS, MAX_TOTAL_WEIGHT); + _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); // min _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max);