Skip to content

Commit

Permalink
add fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thurendous committed Sep 11, 2024
1 parent 49364a2 commit 76314e5
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ update:; forge update

build:; forge build

test :; forge test -vv
test :; forge test

snapshot :; forge snapshot

Expand Down
6 changes: 2 additions & 4 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ src = "src"
out = "out"
libs = ["lib"]

# fuzz test times
fuzz_runs = 2048
# fuzz test max time
fuzz_max_time = 60
[fuzz]
runs = 4096

ffi = true
ast = true
Expand Down
339 changes: 338 additions & 1 deletion test/fuzz/FuzzVotingPowerExchange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.24;

import {Test, console} from "forge-std/Test.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
import {AmbassadorNft} from "src/AmbassadorNft.sol";
import {DeployContracts, DeploymentResult} from "script/DeployContracts.s.sol";
Expand All @@ -11,6 +12,7 @@ import {GovToken} from "src/GovToken.sol";
import {VotingPowerExchange} from "src/VotingPowerExchange.sol";
import {VotingPowerExchangeTestHelper} from "../integration/utils/VotingPowerExchangeTestHelper.t.sol";
import {MessageHashUtils} from "lib/openzeppelin-contracts/contracts/utils/cryptography/MessageHashUtils.sol";
import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";

contract VotingPwoerExchangeTest is Test {
// instances
Expand Down Expand Up @@ -75,5 +77,340 @@ contract VotingPwoerExchangeTest is Test {
vm.label(user2, "user2");
}

function testFuzz_exchange(uint256 amount) public {}
///////////////////////////
///// Other functions /////
///////////////////////////
/// The `setVotingPowerCap` function
function testSetVotingPowerCap(uint256 cap) public {
vm.assume(cap > votingPowerExchange.getVotingPowerCap());
vm.prank(manager);
vm.expectEmit(true, true, true, true);
emit VotingPowerExchange.VotingPowerCapSet(cap);
votingPowerExchange.setVotingPowerCap(cap);
assertEq(votingPowerExchange.getVotingPowerCap(), cap);
}

function testSetVotingPowerCap_revertWhenLowerThanExisting(uint256 cap) public {
vm.assume(cap <= votingPowerExchange.getVotingPowerCap());
vm.prank(manager);
vm.expectRevert(VotingPowerExchange.VotingPowerExchange__LevelIsLowerThanExisting.selector);
votingPowerExchange.setVotingPowerCap(cap);
}

function testSetVotingPowerCap_revertWhenNonManager(address nonManager) public {
vm.assume(nonManager != manager);
vm.prank(nonManager);
vm.expectRevert(
abi.encodeWithSelector(
IAccessControl.AccessControlUnauthorizedAccount.selector, nonManager, votingPowerExchange.MANAGER_ROLE()
)
);
vm.prank(nonManager);
votingPowerExchange.setVotingPowerCap(100e18);
}

///////////////////////////////////////////////////////////////
/// Test the `calculateVotingPowerFromBurnedAmount` function //
///////////////////////////////////////////////////////////////
function testFuzzCalculateVotingPowerFromBurnedAmount(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 0, 92675e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 0);
assertTrue(votingPower <= 110e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_76750_92675(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 76750e18, 92675e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 100e18);
assertTrue(votingPower <= 110e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_62325_76750(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 62325e18, 76750e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 90e18);
assertTrue(votingPower <= 100e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_49400_62325(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 49400e18, 62325e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 80e18);
assertTrue(votingPower <= 90e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_37975_49400(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 37975e18, 49400e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 70e18);
assertTrue(votingPower <= 80e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_28050_37975(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 28050e18, 37975e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 60e18);
assertTrue(votingPower <= 70e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_19625_28050(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 19625e18, 28050e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 50e18);
assertTrue(votingPower <= 60e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_12700_19625(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 12700e18, 19625e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 40e18);
assertTrue(votingPower <= 50e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_7275_12700(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 7275e18, 12700e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 30e18);
assertTrue(votingPower <= 40e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_3350_7275(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 3350e18, 7275e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 20e18);
assertTrue(votingPower <= 30e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_925_3350(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 925e18, 3350e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 10e18);
assertTrue(votingPower <= 20e18);
}

function testFuzzCalculateVotingPowerFromBurnedAmount_0_925(uint256 burnedAmount) public view {
// limit the burnedAmount to a reasonable range
burnedAmount = bound(burnedAmount, 0, 925e18);

uint256 votingPower = votingPowerExchange.calculateVotingPowerFromBurnedAmount(burnedAmount);
uint256 expectedVotingPower = calculateExpectedVotingPower(burnedAmount);

assertEq(votingPower, expectedVotingPower);
assertTrue(votingPower >= 0);
assertTrue(votingPower <= 10e18);
}

/////////////////////////////////////////////////////////////////
/// Test the `calculateBurningAmountFromVotingPower` function ///
/////////////////////////////////////////////////////////////////
function testFuzzCalculateBurningAmountFromVotingPower_0_110(uint256 votingPowerAmount) public view{
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 0, 110e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 0);
assertTrue(burningAmount <= 92675e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_100_110(uint256 votingPowerAmount) public view{
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 100e18, 110e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 76750e18);
assertTrue(burningAmount <= 92675e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_90_100(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 90e18, 100e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 62325e18);
assertTrue(burningAmount <= 76750e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_80_90(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 80e18, 90e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 49400e18);
assertTrue(burningAmount <= 62325e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_70_80(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 70e18, 80e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 37975e18);
assertTrue(burningAmount <= 49400e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_60_70(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 60e18, 70e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 28050e18);
assertTrue(burningAmount <= 37975e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_50_60(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 50e18, 60e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 19625e18);
assertTrue(burningAmount <= 28050e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_40_50(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 40e18, 50e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 12700e18);
assertTrue(burningAmount <= 19625e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_30_40(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 30e18, 40e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 7275e18);
assertTrue(burningAmount <= 12700e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_20_30(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 20e18, 30e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 3350e18);
assertTrue(burningAmount <= 7275e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_10_20(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 10e18, 20e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 925e18);
assertTrue(burningAmount <= 3350e18);
}

function testFuzzCalculateBurningAmountFromVotingPower_0_10(uint256 votingPowerAmount) public view {
// limit the votingPowerAmount to a reasonable range
votingPowerAmount = bound(votingPowerAmount, 0, 10e18);

uint256 burningAmount = votingPowerExchange.calculateBurningAmountFromVotingPower(votingPowerAmount);
uint256 expectedBurningAmount = calculateExpectedBurningAmount(votingPowerAmount);

assertEq(burningAmount, expectedBurningAmount);
assertTrue(burningAmount >= 0);
assertTrue(burningAmount <= 925e18);
}

function calculateExpectedBurningAmount(uint256 votingPowerAmount) internal pure returns (uint256) {
uint256 term = 15 * (votingPowerAmount * votingPowerAmount) / 1e18 + 35 * votingPowerAmount;
return term / 2;
}

function calculateExpectedVotingPower(uint256 burnedAmount) internal pure returns (uint256) {
uint256 innerValue = (30625 * 1e16 + 30 * burnedAmount);
uint256 sqrtPart = 2 * Math.sqrt(innerValue) * 1e9;
return (uint256(sqrtPart) - 5 * 1e18) / 30 - 1e18;
}
}
Loading

0 comments on commit 76314e5

Please sign in to comment.