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 2 commits
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198780
198736
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-ExecuteBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
231116
231028
Original file line number Diff line number Diff line change
@@ -1 +1 @@
244626
244516
Original file line number Diff line number Diff line change
@@ -1 +1 @@
302041
301909
Original file line number Diff line number Diff line change
@@ -1 +1 @@
224642
224554
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-ExecuteSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165165
165121
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150727
150683
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174481
174437
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-RevertInvalidNonce.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43785
43741
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-V3-ExclusiveFiller.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169089
169045
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-V3-InputOverride.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169170
169126
2 changes: 1 addition & 1 deletion .forge-snapshots/Base-V3DutchOrder-V3-OutputOverride.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169113
169069
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecay.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13179
13419
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayBounded.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1199
1193
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayFullyDecayed.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6677
6803
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayFullyDecayedNegative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6365
6491
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayNegative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1271
1265
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayNoDecayYet.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4703
4697
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayNoDecayYetNegative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4703
4697
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-DutchDecayRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1271
1265
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-ExtendedMultiPointDutchDecay.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88354
89554
2 changes: 1 addition & 1 deletion .forge-snapshots/V3-MultiPointDutchDecay.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26242
26725
3 changes: 2 additions & 1 deletion src/lib/NonlinearDutchDecayLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ library NonlinearDutchDecayLib {
if (decayStartBlock >= block.number || curve.relativeAmounts.length == 0) {
return startAmount.bound(minAmount, maxAmount);
}

// If the blockDelta is larger than type(uint16).max, a downcast overflow will occur
// We prevent this by capping the blockDelta to type(uint16).max to express a full decay
uint16 blockDelta = uint16(Math.min(block.number - decayStartBlock, type(uint16).max));
(uint16 startPoint, uint16 endPoint, int256 relStartAmount, int256 relEndAmount) =
locateCurvePosition(curve, blockDelta);
Expand Down
37 changes: 23 additions & 14 deletions test/lib/NonLinearDutchDecayLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {DutchDecayLib} from "../../src/lib/DutchDecayLib.sol";
import {NonlinearDutchDecayLib} from "../../src/lib/NonlinearDutchDecayLib.sol";
import {V3DutchOutput, V3DutchInput, NonlinearDutchDecay} from "../../src/lib/V3DutchOrderLib.sol";
import {Uint16Array, toUint256} from "../../src/types/Uint16Array.sol";
import {Math} from "openzeppelin-contracts/utils/math/Math.sol";
import {ArrayBuilder} from "../util/ArrayBuilder.sol";
import {CurveBuilder} from "../util/CurveBuilder.sol";

Expand Down Expand Up @@ -476,21 +477,27 @@ contract NonlinearDutchDecayLibTest is Test, GasSnapshot {
}

function testFuzzDutchDecayBeyondUint16Max(
uint16 lastValidBlock, // For curve
uint256 decayAmountFuzz, // For curve
// decay(curve, startAmount, decayStartBlock, minAmount, maxAmount);
uint256 startAmount,
uint256 blockDelta,
uint16 lastValidBlock,
uint256 decayAmountFuzz
uint256 decayStartBlock,
uint256 minAmount,
uint256 maxAmount,
uint256 currentBlock
) public {
uint256 decayStartBlock = 100;
vm.assume(decayStartBlock < type(uint256).max - type(uint16).max);
vm.assume(lastValidBlock > 0);
vm.assume(startAmount > 0);
vm.assume(startAmount > 0 && startAmount < uint256(type(int256).max));
vm.assume(maxAmount >= startAmount);
minAmount = bound(minAmount, 0, startAmount);
// bound only takes uint256, so we need to limit decayAmountFuzz to int256.max
// because we cast it to int256 in the decay function
decayAmountFuzz = bound(decayAmountFuzz, minAmount, startAmount);

// 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);
// This will happen when the block delta is larger than type(uint16).max
vm.assume(currentBlock > decayStartBlock + type(uint16).max);

uint16[] memory blocks = new uint16[](1);
blocks[0] = lastValidBlock;
Expand All @@ -500,10 +507,12 @@ contract NonlinearDutchDecayLibTest is Test, GasSnapshot {

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");
uint256 decayed = NonlinearDutchDecayLib.decay(curve, startAmount, decayStartBlock, minAmount, maxAmount);
assertEq(
decayed,
Math.max(startAmount - decayAmountFuzz, minAmount),
alanhwu marked this conversation as resolved.
Show resolved Hide resolved
"Should be fully decayed for block delta beyond uint16.max"
);
}
}
Loading