Skip to content

Commit

Permalink
Fix/fuzz tests improvements (#178)
Browse files Browse the repository at this point in the history
* chore: fix linter setup

* chore: update smocked files

* chore: add missing spdx identifier

* chore: add an npm script to run echidna tests

* test: ensure SpotPriceAfterBelowSpotPriceBefore is never thrown

* chore: fix natspec issues

* test: ensure fuzz_joinExitPool body is runnable

* chore: cleaning up fuzz (#179)

* feat: creating BCoWPoolForTest to avoid modifying core contracts

* fix: test:echidna script

* fix: safeTransfer issue with echidna

* chore: update test contract licenses

* test: document property 25

* chore: remove unimplemented function

---------

Co-authored-by: Weißer Hase <[email protected]>
  • Loading branch information
0xteddybear and wei3erHase authored Jul 29, 2024
1 parent 637aa83 commit 42d2a04
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/newBFactory.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4130633
4130621
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ broadcast/*/*/*
out

# echidna corpuses
**/corpuses/*
**/corpuses/*
**/crytic-export/*
1 change: 0 additions & 1 deletion .solhintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
test/smock/*
test/manual-smock/*
test/invariants/*
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sort_imports = true
solc_version = '0.8.25'
libs = ["node_modules", "lib"]
optimizer_runs = 500
evm_version = 'shanghai'
evm_version = 'cancun'
fs_permissions = [{ access = "read-write", path = ".forge-snapshots/"}]
# 2018: function can be view, so far only caused by mocks
# 2394: solc insists on reporting on every transient storage use
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"prepare": "husky install",
"smock": "smock-foundry --contracts src/contracts",
"test": "yarn test:integration && yarn test:unit",
"test:echidna": "find test/invariants/fuzz -regex '.*\\.t\\.sol$' |cut -d '/' -f 4 | cut -d . -f 1 |xargs -I{} echidna test/invariants/fuzz/{}.t.sol --contract Fuzz{} --config test/invariants/fuzz/{}.yaml",
"test:integration": "forge test --ffi --match-path 'test/integration/**' -vvv --isolate",
"test:local": "FOUNDRY_FUZZ_RUNS=100 forge test -vvv",
"test:scaffold": "bulloak check --fix test/unit/*.tree && forge fmt",
Expand Down
20 changes: 7 additions & 13 deletions src/contracts/BPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ contract BPool is BToken, BMath, IBPool {
/// @dev Sum of all token weights
uint256 internal _totalWeight;

/// TEST TEST TEST TEST TEST TEST TEST TEST
bytes32 internal _reenteringMutex;
/// TEST TEST TEST TEST TEST TEST TEST TEST

/// @dev Logs the call data
modifier _logs_() {
emit LOG_CALL(msg.sig, msg.sender, msg.data);
Expand Down Expand Up @@ -152,8 +148,8 @@ contract BPool is BToken, BMath, IBPool {

_pullUnderlying(token, msg.sender, balance);
}
/// @inheritdoc IBPool

/// @inheritdoc IBPool
function unbind(address token) external _logs_ _lock_ _controller_ _notFinalized_ {
if (!_records[token].bound) {
revert BPool_TokenNotBound();
Expand Down Expand Up @@ -601,10 +597,9 @@ contract BPool is BToken, BMath, IBPool {
* be interpreted as locked
*/
function _setLock(bytes32 value) internal virtual {
// assembly ("memory-safe") {
// tstore(_MUTEX_TRANSIENT_STORAGE_SLOT, value)
// }
_reenteringMutex = value;
assembly ("memory-safe") {
tstore(_MUTEX_TRANSIENT_STORAGE_SLOT, value)
}
}

/**
Expand Down Expand Up @@ -675,9 +670,8 @@ contract BPool is BToken, BMath, IBPool {
* allowing calls
*/
function _getLock() internal view virtual returns (bytes32 value) {
// assembly ("memory-safe") {
// value := tload(_MUTEX_TRANSIENT_STORAGE_SLOT)
// }
value = _reenteringMutex;
assembly ("memory-safe") {
value := tload(_MUTEX_TRANSIENT_STORAGE_SLOT)
}
}
}
13 changes: 13 additions & 0 deletions test/invariants/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"rules": {
"custom-errors": "off",
"no-empty-blocks":"off",
"reason-string": "off",
"reentrancy": "off",
"style-guide-casing": [ "warn", {
"ignoreVariables": true,
"ignorePublicFunctions": true,
"ignoreExternalFunctions": true
}]
}
}
3 changes: 2 additions & 1 deletion test/invariants/PROPERTIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
| only the settler can commit a hash | High level | 22 | [x] | [x] |
| when a hash has been commited, only this order can be settled | High level | 23 | [ ] | [ ] |
| BToken should not break the ToB ERC20 properties** | High level | 24 | | [x] |
| Spot price after swap is always greater than before swap | High level | 25 | | [x] |

> (*) Bundled with 24
Expand Down Expand Up @@ -88,4 +89,4 @@ power of a power should mult the exp (x^a)^b == x^(a*b)
## Untested (precision issues in test settingsq)
calcOutGivenIn should be inv with calcInGivenOut
calcPoolOutGivenSingleIn should be inv with calcSingleInGivenPoolOut
calcSingleOutGivenPoolIn should be inv with calcPoolInGivenSingleOut
calcSingleOutGivenPoolIn should be inv with calcPoolInGivenSingleOut
2 changes: 1 addition & 1 deletion test/invariants/fuzz/BNum.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {EchidnaTest} from '../helpers/AdvancedTestsUtils.sol';
Expand Down
3 changes: 2 additions & 1 deletion test/invariants/fuzz/BToken.t.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {EchidnaTest} from '../helpers/AdvancedTestsUtils.sol';
import {CryticERC20ExternalBasicProperties} from
'@crytic/properties/contracts/ERC20/external/properties/ERC20ExternalBasicProperties.sol';
import {ITokenMock} from '@crytic/properties/contracts/ERC20/external/util/ITokenMock.sol';
import {PropertiesConstants} from '@crytic/properties/contracts/util/PropertiesConstants.sol';
import 'contracts/BToken.sol';
import {BToken} from 'contracts/BToken.sol';

contract FuzzBToken is CryticERC20ExternalBasicProperties, EchidnaTest {
constructor() {
Expand Down
Loading

0 comments on commit 42d2a04

Please sign in to comment.