Skip to content

Commit

Permalink
test: fuzz rounding permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJabberwock committed May 3, 2024
1 parent 35aee17 commit af3778f
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 285 deletions.
2 changes: 2 additions & 0 deletions pkg/solidity-utils/contracts/math/StableMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "./FixedPoint.sol";
// Some variables have non mixed case names (e.g. P_D) that relate to the mathematical derivations.
// solhint-disable private-vars-leading-underscore, var-name-mixedcase

// Any new logic changes to this library need to be propagated to the `StableMathMock` for tests to pass.

library StableMath {
using FixedPoint for uint256;

Expand Down
57 changes: 9 additions & 48 deletions pkg/solidity-utils/contracts/test/RoundingMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,38 @@ pragma solidity ^0.8.24;
import { FixedPoint } from "../math/FixedPoint.sol";

abstract contract RoundingMock {
// `Disabled` basically preserves the existing direction, while the other two either change or preserve the direction
// according to the operation. I.e. `RoundDown` changes the direction for operations that round up, and preserve the
// direction for operations that round down, and vice versa.
enum MockRounding {
Disabled,
RoundDown,
RoundUp
}

MockRounding public mockRounding;

function setMockRounding(MockRounding _mockRounding) external {
mockRounding = _mockRounding;
}

function mulDown(uint256 a, uint256 b) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundUp) {
function mockMul(uint256 a, uint256 b, bool roundUp) internal pure returns (uint256) {
if (roundUp) {
return FixedPoint.mulUp(a, b);
} else {
return FixedPoint.mulDown(a, b);
}
}

function mulUp(uint256 a, uint256 b) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundDown) {
return FixedPoint.mulDown(a, b);
} else {
return FixedPoint.mulUp(a, b);
}
}

function divDown(uint256 a, uint256 b) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundUp) {
function mockDiv(uint256 a, uint256 b, bool roundUp) internal pure returns (uint256) {
if (roundUp) {
return FixedPoint.divUp(a, b);
} else {
return FixedPoint.divDown(a, b);
}
}

function divUp(uint256 a, uint256 b) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundDown) {
return FixedPoint.divDown(a, b);
function mockDivRaw(uint256 a, uint256 b, bool roundUp) internal pure returns (uint256) {
if (roundUp) {
return FixedPoint.divUpRaw(a, b);
} else {
return FixedPoint.divUp(a, b);
}
}

function divUpRaw(uint256 a, uint256 b) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundDown) {
return divDownRaw(a, b);
} else {
return FixedPoint.divUpRaw(a, b);
}
}

function powDown(uint256 x, uint256 y) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundUp) {
function mockPow(uint256 x, uint256 y, bool roundUp) internal pure returns (uint256) {
if (roundUp) {
return FixedPoint.powUp(x, y);
} else {
return FixedPoint.powDown(x, y);
}
}

function powUp(uint256 x, uint256 y) internal view returns (uint256) {
if (mockRounding == MockRounding.RoundDown) {
return FixedPoint.powDown(x, y);
} else {
return FixedPoint.powUp(x, y);
}
}

function divDownRaw(uint256 a, uint256 b) private pure returns (uint256) {
return a / b;
}
Expand Down
Loading

0 comments on commit af3778f

Please sign in to comment.