forked from alexroan/forge-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
163 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
ModulusFuzz:test_mod_resultIsAlwaysLessThanModDivisor(uint256,uint256) (runs: 256, μ: 38396, ~: 40651) | ||
GAS_ModulusGas_ModAdmin:test_setModDivisor() (gas: 13812) | ||
ModulusFuzz:test_mod_resultIsAlwaysLessThanModDivisor(uint256,uint256) (runs: 256, μ: 38497, ~: 40674) | ||
GAS_ModulusGas_ModAdmin:test_setModDivisor() (gas: 13835) | ||
GAS_ModulusGas_Owner:test_setModAdmin() (gas: 14155) | ||
GAS_ModulusGas_Stranger:test_getModDivisor() (gas: 7514) | ||
GAS_ModulusGas_Stranger:test_getOwner() (gas: 7577) | ||
GAS_ModulusGas_Stranger:test_getResult() (gas: 7546) | ||
GAS_ModulusGas_Stranger:test_mod() (gas: 12544) | ||
ModulusInvariants:invariant_gettersNeverRevert() (runs: 512, calls: 262144, reverts: 0) | ||
ModulusUnit:test_constructor() (gas: 10608) | ||
ModulusUnit:test_mod() (gas: 30677) | ||
ModulusUnit:test_mod_revertsIfModDivisorIsZero() (gas: 13544) | ||
ModulusUnit:test_mod() (gas: 30655) | ||
ModulusUnit:test_setModAdmin() (gas: 15159) | ||
ModulusUnit:test_setModAdmin_onlyOwner_reverts() (gas: 11908) | ||
ModulusUnit:test_setModDivisor() (gas: 17945) | ||
ModulusUnit:test_setModAdmin_onlyOwner_reverts() (gas: 11986) | ||
ModulusUnit:test_setModDivisor() (gas: 17968) | ||
ModulusUnit:test_setModDivisor_onlyModAdmin_reverts() (gas: 11895) | ||
ModulusUnit:test_setModDivisor_zeroValue_reverts() (gas: 11166) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.16; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {Modulus} from "../../../src/Modulus.sol"; | ||
|
||
contract ActorModAdmin is Test { | ||
Modulus internal s_modulus; | ||
|
||
function storeModulus(Modulus modulus) external { | ||
s_modulus = modulus; | ||
} | ||
|
||
function setModDivisor(uint256 modDivisor) external { | ||
if (modDivisor == 0) modDivisor = 1; | ||
s_modulus.setModDivisor(modDivisor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.16; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {Modulus} from "../../../src/Modulus.sol"; | ||
|
||
contract ActorStranger is Test { | ||
Modulus internal s_modulus; | ||
|
||
function storeModulus(Modulus modulus) external { | ||
s_modulus = modulus; | ||
} | ||
|
||
function mod(uint256 numerator) external { | ||
s_modulus.mod(numerator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.16; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
|
||
contract InvariantsBase is Test { | ||
|
||
struct FuzzSelector { | ||
address addr; | ||
bytes4[] selectors; | ||
} | ||
|
||
address[] private s_excludeContracts; | ||
address[] private s_targetContracts; | ||
FuzzSelector[] private s_targetSelectors; | ||
address[] private s_targetSenders; | ||
|
||
constructor() { | ||
// https://github.com/foundry-rs/foundry/issues/2963 | ||
s_targetSenders.push(address(1)); | ||
} | ||
|
||
function excludeContracts() public view returns (address[] memory) { | ||
return s_excludeContracts; | ||
} | ||
|
||
function targetContracts() public view returns (address[] memory) { | ||
return s_targetContracts; | ||
} | ||
|
||
function targetSelectors() public view returns (FuzzSelector[] memory) { | ||
return s_targetSelectors; | ||
} | ||
|
||
function targetSenders() public view returns (address[] memory) { | ||
return s_targetSenders; | ||
} | ||
|
||
// To avoid calling auxiliary functions that are inherited but not under test | ||
// such as forge-std/Test.sol functions. | ||
function addSelectors( | ||
address newSelectorAddress, | ||
bytes4[] memory newSelectors | ||
) public { | ||
s_targetSelectors.push(FuzzSelector(newSelectorAddress, newSelectors)); | ||
} | ||
|
||
function addSender(address newSenderAddress) public { | ||
s_targetSenders.push(newSenderAddress); | ||
} | ||
|
||
// Utility function to exclude contracts that shouldn't be called | ||
function excludeContract(address excludedContractAddress) public { | ||
s_excludeContracts.push(excludedContractAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.16; | ||
|
||
import {Modulus} from "../../src/Modulus.sol"; | ||
import {Test} from "forge-std/Test.sol"; | ||
import {Constants} from "../Constants.t.sol"; | ||
import {InvariantsBase} from "./InvariantsBase.t.sol"; | ||
import {ActorModAdmin} from "./Actors/ActorModAdmin.t.sol"; | ||
import {ActorStranger} from "./Actors/ActorStranger.t.sol"; | ||
|
||
contract ModulusInvariants is Test, InvariantsBase, Constants { | ||
Modulus internal s_modulus; | ||
|
||
function setUp() public virtual { | ||
// Deploy actors | ||
ActorModAdmin modAdmin = new ActorModAdmin(); | ||
ActorStranger stranger = new ActorStranger(); | ||
|
||
// Deploy Modulus contract | ||
changePrank(address(OWNER)); | ||
s_modulus = new Modulus(address(modAdmin)); | ||
excludeContract(address(s_modulus)); | ||
|
||
// Store the Modulus address on each of the actors | ||
modAdmin.storeModulus(s_modulus); | ||
stranger.storeModulus(s_modulus); | ||
|
||
// Add mod admin selectors to callable functions | ||
bytes4[] memory modAdminSelectors = new bytes4[](1); | ||
modAdminSelectors[0] = ActorModAdmin.setModDivisor.selector; | ||
addSelectors(address(modAdmin), modAdminSelectors); | ||
|
||
// Add stranger selectors to callable functions | ||
bytes4[] memory strangerSelectors = new bytes4[](1); | ||
strangerSelectors[0] = ActorStranger.mod.selector; | ||
addSelectors(address(stranger), strangerSelectors); | ||
} | ||
|
||
function invariant_gettersNeverRevert() public { | ||
s_modulus.getResult(); | ||
s_modulus.getModDivisor(); | ||
s_modulus.getModAdmin(); | ||
s_modulus.getOwner(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters