Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: property implementations + setup for foundry fork testing #810

Open
wants to merge 5 commits into
base: recon-update-sept-2024
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/contracts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MAINNET_RPC_URL=""
16 changes: 14 additions & 2 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
Use this guide to install foundry:
https://book.getfoundry.sh/getting-started/installation

## Running foundry tests
## Running Foundry tests
- Simply `cd` into target test package:
- `cd packages/contracts`
- Run `forge test`

## Running Foundry fork tests for broken property reproducers
To run Foundry fork tests for reproducers:
- Add the reproducer test for the broken property to the `ForkToFoundry` contract
- Change the block number in the call to `vm.createSelectFork` to the block number in the coverage report that gets dynamically replaced in the [`Setup`](https://github.com/ebtc-protocol/ebtc/blob/925073f04bdfe5a6b594898d6491950087eee23b/packages/contracts/contracts/TestContracts/invariants/Setup.sol#L348) contract.
- The block number shown in the coverage report for the run will be the block at which Echidna forked mainnet from for your run.
- Rename the `.env.example` file to `.env` and add your rpc url.
- `cd` into the target test package:
- `cd packages/contracts`
- Run `forge test --mt <broken_property_reproducer_test>`


## Remappings:
Foundry test configuration is using existing hardhat dependencies, such as @openzeppelin etc.
They are declated in `remappings.txt`.
Expand All @@ -26,4 +37,5 @@ Then from root of the project run:
yarn install
```

And everything should work
And everything should work

Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ abstract contract Properties is BeforeAfter, PropertiesDescriptions, Asserts, Pr
PriceFeedTestnet priceFeedMock,
CRLens crLens
) internal returns (bool) {
uint256 curentPrice = priceFeedMock.lastGoodPrice();
return crLens.quoteRealTCR() == cdpManager.getSyncedTCR(curentPrice);
uint256 currentPrice = priceFeedMock.fetchPrice();
return crLens.quoteRealTCR() == cdpManager.getSyncedTCR(currentPrice);
}

function invariant_GENERAL_13(
Expand All @@ -478,7 +478,7 @@ abstract contract Properties is BeforeAfter, PropertiesDescriptions, Asserts, Pr
) internal returns (bool) {
bytes32 currentCdp = sortedCdps.getFirst();

uint256 _price = priceFeedMock.lastGoodPrice();
uint256 _price = priceFeedMock.fetchPrice();

// Compare synched with quote for all Cdps
while (currentCdp != bytes32(0)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,13 @@ abstract contract Setup is BaseStorageVariables, PropertiesConstants {
event Log(string);

// This is a fix to allow facilitate dynamic replacement that searches for the `vm.roll` statements.
IHevm constant vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

function _setUpFork() internal {
IHevm vm = IHevm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);

// Add timestamp and block for Recon dynamic replacement
vm.roll(20770509);
vm.warp(1726578263);
vm.roll(20996709);
vm.warp(1729305851);

// NOTE: Addresses from: https://gist.github.com/GalloDaSballo/75d77f8d0837821156fe061d0d8687e1
defaultGovernance = address(0xaDDeE229Bd103bb5B10C3CdB595A01c425dd3264);
Expand Down
50 changes: 45 additions & 5 deletions packages/contracts/foundry_test/ForkToFoundry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ contract ForkToFoundry is
BeforeAfterWithLogging
{
function setUp() public {
vm.createSelectFork("YOUR_RPC_URL", 20777211);
string memory MAINNET_RPC_URL = vm.envString("MAINNET_RPC_URL");
// TODO: when testing locally change this block with block from coverage report set inside _setUpFork
vm.createSelectFork(MAINNET_RPC_URL, 20996709);

_setUpFork();
_setUpActors();
_setUpActorsFork();
actor = actors[address(USER1)];

// If the accounting hasn't been synced since the last rebase
Expand All @@ -42,7 +45,10 @@ contract ForkToFoundry is

// Previous cumulative CDPs per each rebase
// Will need to be adjusted
vars.cumulativeCdpsAtTimeOfRebase = 200;
// @audit removed because inconsistent with EchidnaForkTester setup
// vars.cumulativeCdpsAtTimeOfRebase = 200;

_setUpCdpFork();
}

// forge test --match-test test_asserts_GENERAL_13_1 -vv
Expand All @@ -54,12 +60,46 @@ contract ForkToFoundry is

}

// forge test --match-test test_asserts_GENERAL_12_0 -vv
// forge test --match-test test_asserts_GENERAL_12_0 -vv
function test_asserts_GENERAL_12_0() public {

vm.roll(block.number + 4963);
vm.warp(block.timestamp + 50417);
asserts_GENERAL_12();
}

// forge test --match-test test_asserts_GENERAL_12_1 -vv
function test_asserts_GENERAL_12_1() public {
// NOTE: from reproducer test immediately breaks but when asserts_test_fail is commented it doesn't
// vm.roll(block.number + 60364);
// vm.warp(block.timestamp + 11077);
// asserts_active_pool_invariant_5();

// // NOTE: removing this assertion and warp causes a failure
// // vm.roll(block.number + 1984);
// // vm.warp(block.timestamp + 322370);
// // asserts_test_fail();

// vm.roll(block.number + 33560);
// vm.warp(block.timestamp + 95);
// asserts_GENERAL_12();
// ========================

// NOTE: from shrunken logs breaks immediately
vm.roll(block.number + 1);
vm.warp(block.timestamp + 2973);
asserts_GENERAL_12();
}

// forge test --match-test test_asserts_GENERAL_13_2 -vv
function test_asserts_GENERAL_13_2() public {
// NOTE: from shrunken logs
// vm.roll(block.number + 1);
// vm.warp(block.timestamp + 2963);

// NOTE: from reproducer
vm.roll(block.number + 60471);
vm.warp(block.timestamp + 6401);

asserts_GENERAL_13();
}
}