Skip to content

Commit

Permalink
Another Invariant
Browse files Browse the repository at this point in the history
  • Loading branch information
alexroan committed Nov 2, 2022
1 parent 62cb161 commit f2bbcb8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 11 deletions.
15 changes: 8 additions & 7 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
ModulusFuzz:test_mod_resultIsAlwaysLessThanModDivisor(uint256,uint256) (runs: 256, μ: 38497, ~: 40674)
GAS_ModulusGas_ModAdmin:test_setModDivisor() (gas: 13835)
ModulusFuzz:test_mod_resultIsAlwaysLessThanModDivisor(uint256,uint256) (runs: 256, μ: 40853, ~: 43186)
GAS_ModulusGas_ModAdmin:test_setModDivisor() (gas: 14812)
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)
GAS_ModulusGas_Stranger:test_mod() (gas: 14179)
ModulusInvariants:invariant_gettersNeverRevert() (runs: 256, calls: 65536, reverts: 0)
ModulusInvariants:invariant_resultShouldAlwaysBeLessThanModDivisor() (runs: 256, calls: 65536, reverts: 0)
ModulusUnit:test_constructor() (gas: 10608)
ModulusUnit:test_mod() (gas: 30655)
ModulusUnit:test_setModAdmin() (gas: 15159)
ModulusUnit:test_mod() (gas: 36800)
ModulusUnit:test_setModAdmin() (gas: 20464)
ModulusUnit:test_setModAdmin_onlyOwner_reverts() (gas: 11986)
ModulusUnit:test_setModDivisor() (gas: 17968)
ModulusUnit:test_setModDivisor() (gas: 24647)
ModulusUnit:test_setModDivisor_onlyModAdmin_reverts() (gas: 11895)
ModulusUnit:test_setModDivisor_zeroValue_reverts() (gas: 11166)
4 changes: 2 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ max_test_rejects = 120000
seed = '0xB00B1E5'

[invariant]
runs = 512
depth = 512
runs = 256
depth = 256
fail_on_revert = true

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
8 changes: 7 additions & 1 deletion src/Modulus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.16;
contract Modulus {
event ModAdminSet(address indexed previous, address indexed modAdmin);
event ModDivisorSet(uint256 previous, uint256 modDivisor);
event ResultReset();
event Result(uint256 n, uint256 modDivisor, uint256 result);

error OnlyOwner(address expected, address actual);
Expand Down Expand Up @@ -36,10 +37,15 @@ contract Modulus {
uint256 previous = s_modDivisor;
s_modDivisor = modDivisor;
emit ModDivisorSet(previous, modDivisor);
s_result = 0;
emit ResultReset();
}

function mod(uint256 n) external {
s_result = n % s_modDivisor;
uint256 modDivisor = s_modDivisor;
uint256 result = n % modDivisor;
s_result = result;
emit Result(n, modDivisor, result);
}

function getOwner() external view returns (address owner) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ contract ModulusInvariants is Test, InvariantsBase, Constants {
s_modulus.getModAdmin();
s_modulus.getOwner();
}

function invariant_resultShouldAlwaysBeLessThanModDivisor() public {
assertLe(s_modulus.getResult(), s_modulus.getModDivisor());
}
}
13 changes: 12 additions & 1 deletion test/Unit/Modulus.t.sol → test/Unit/ModulusUnit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ contract ModulusUnit is Test, Constants {
s_modulus.setModAdmin(STRANGER);
}

event ModAdminSet(address indexed previous, address indexed modAdmin);
function test_setModAdmin() public {
vm.expectEmit(true, true, true, false);
emit ModAdminSet(s_modulus.getModAdmin(), STRANGER);
s_modulus.setModAdmin(STRANGER);
assertEq(s_modulus.getModAdmin(), STRANGER);
}
Expand All @@ -56,20 +59,28 @@ contract ModulusUnit is Test, Constants {
s_modulus.setModDivisor(0);
}

event ModDivisorSet(uint256 previous, uint256 modDivisor);
event ResultReset();
function test_setModDivisor() public {
changePrank(MOD_ADMIN);
vm.expectEmit(true, false, false, false);
emit ModDivisorSet(s_modulus.getModDivisor(), MOD_DIVISOR_2);
vm.expectEmit(true, false, false, false);
emit ResultReset();
s_modulus.setModDivisor(MOD_DIVISOR_2);
assertEq(s_modulus.getModDivisor(), MOD_DIVISOR_2);
}

// ==========================================================================
// MOD
// ==========================================================================

event Result(uint256 n, uint256 modDivisor, uint256 result);
function test_mod() public {
uint256 numerator = 50;
uint256 expected = numerator % MOD_DIVISOR_1;

vm.expectEmit(true, false, false, false);
emit Result(numerator, MOD_DIVISOR_1, expected);
s_modulus.mod(numerator);
assertEq(s_modulus.getResult(), expected);
}
Expand Down

0 comments on commit f2bbcb8

Please sign in to comment.