Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audit(12): Long-term orders may be filled with an incorrect amount #304

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/NonlinearDutchDecayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {OutputToken, InputToken} from "../base/ReactorStructs.sol";
import {V3DutchOutput, V3DutchInput, NonlinearDutchDecay} from "../lib/V3DutchOrderLib.sol";
import {FixedPointMathLib} from "solmate/src/utils/FixedPointMathLib.sol";
import {MathExt} from "./MathExt.sol";
import {Math} from "openzeppelin-contracts/utils/math/Math.sol";
import {Uint16ArrayLibrary, Uint16Array, fromUnderlying} from "../types/Uint16Array.sol";
import {DutchDecayLib} from "./DutchDecayLib.sol";

Expand Down Expand Up @@ -39,7 +40,7 @@ library NonlinearDutchDecayLib {
return startAmount.bound(minAmount, maxAmount);
}

uint16 blockDelta = uint16(block.number - decayStartBlock);
alanhwu marked this conversation as resolved.
Show resolved Hide resolved
uint16 blockDelta = uint16(Math.min(block.number - decayStartBlock, type(uint16).max));
(uint16 startPoint, uint16 endPoint, int256 relStartAmount, int256 relEndAmount) =
locateCurvePosition(curve, blockDelta);
// get decay of only the relative amounts
Expand Down
32 changes: 32 additions & 0 deletions test/lib/NonLinearDutchDecayLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,36 @@ contract NonlinearDutchDecayLibTest is Test, GasSnapshot {
vm.expectRevert(NonlinearDutchDecayLib.InvalidDecayCurve.selector);
mockNonlinearDutchDecayLibContract.decay(curve, startAmount, decayStartBlock, 1 ether, 1 ether);
}

function testFuzzDutchDecayBeyondUint16Max(
uint256 startAmount,
uint256 blockDelta,
uint16 lastValidBlock,
uint256 decayAmountFuzz
alanhwu marked this conversation as resolved.
Show resolved Hide resolved
) public {
uint256 decayStartBlock = 100;
vm.assume(lastValidBlock > 0);
vm.assume(startAmount > 0);

// Testing that we get a fully decayed curve instead of overflowed mistake
vm.assume(blockDelta > uint256(type(uint16).max));
vm.assume(blockDelta < type(uint256).max - decayStartBlock);

// Bound decayAmountFuzz between 0 and startAmount
decayAmountFuzz = bound(decayAmountFuzz, 0, startAmount);
alanhwu marked this conversation as resolved.
Show resolved Hide resolved

uint16[] memory blocks = new uint16[](1);
blocks[0] = lastValidBlock;

int256[] memory decayAmounts = new int256[](1);
decayAmounts[0] = int256(decayAmountFuzz);

NonlinearDutchDecay memory curve = CurveBuilder.multiPointCurve(blocks, decayAmounts);

uint256 currentBlock = decayStartBlock + blockDelta;
vm.roll(currentBlock);
uint256 decayed = NonlinearDutchDecayLib.decay(curve, startAmount, decayStartBlock, 0, startAmount);

assertEq(decayed, startAmount - decayAmountFuzz, "Should be fully decayed for block delta beyond uint16.max");
}
}
Loading