From f2bbcb8ce7497bae77606e9ed66bbbf9b1a162f7 Mon Sep 17 00:00:00 2001 From: Alex Roan Date: Wed, 2 Nov 2022 00:49:53 +0000 Subject: [PATCH] Another Invariant --- .gas-snapshot | 15 ++++++++------- foundry.toml | 4 ++-- src/Modulus.sol | 8 +++++++- test/Fuzz/{Modulus.t.sol => ModulusFuzz.t.sol} | 0 test/Gas/{Modulus.t.sol => ModulusGas.t.sol} | 0 .../{Modulus.t.sol => ModulusInvariants.t.sol} | 4 ++++ test/Unit/{Modulus.t.sol => ModulusUnit.t.sol} | 13 ++++++++++++- 7 files changed, 33 insertions(+), 11 deletions(-) rename test/Fuzz/{Modulus.t.sol => ModulusFuzz.t.sol} (100%) rename test/Gas/{Modulus.t.sol => ModulusGas.t.sol} (100%) rename test/Invariant/{Modulus.t.sol => ModulusInvariants.t.sol} (91%) rename test/Unit/{Modulus.t.sol => ModulusUnit.t.sol} (80%) diff --git a/.gas-snapshot b/.gas-snapshot index 134ce5f..8f1c78c 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -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) diff --git a/foundry.toml b/foundry.toml index 99c6eec..9da12b5 100644 --- a/foundry.toml +++ b/foundry.toml @@ -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 \ No newline at end of file diff --git a/src/Modulus.sol b/src/Modulus.sol index f098579..5b2855f 100644 --- a/src/Modulus.sol +++ b/src/Modulus.sol @@ -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); @@ -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) { diff --git a/test/Fuzz/Modulus.t.sol b/test/Fuzz/ModulusFuzz.t.sol similarity index 100% rename from test/Fuzz/Modulus.t.sol rename to test/Fuzz/ModulusFuzz.t.sol diff --git a/test/Gas/Modulus.t.sol b/test/Gas/ModulusGas.t.sol similarity index 100% rename from test/Gas/Modulus.t.sol rename to test/Gas/ModulusGas.t.sol diff --git a/test/Invariant/Modulus.t.sol b/test/Invariant/ModulusInvariants.t.sol similarity index 91% rename from test/Invariant/Modulus.t.sol rename to test/Invariant/ModulusInvariants.t.sol index d132f27..680435e 100644 --- a/test/Invariant/Modulus.t.sol +++ b/test/Invariant/ModulusInvariants.t.sol @@ -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()); + } } \ No newline at end of file diff --git a/test/Unit/Modulus.t.sol b/test/Unit/ModulusUnit.t.sol similarity index 80% rename from test/Unit/Modulus.t.sol rename to test/Unit/ModulusUnit.t.sol index f391aa8..78feac3 100644 --- a/test/Unit/Modulus.t.sol +++ b/test/Unit/ModulusUnit.t.sol @@ -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); } @@ -56,8 +59,14 @@ 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); } @@ -65,11 +74,13 @@ contract ModulusUnit is Test, Constants { // ========================================================================== // 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); }