Skip to content

Commit

Permalink
audit/6: short circuit scaling if mpsPerPriorityFeeWei is 0 (#263)
Browse files Browse the repository at this point in the history
* short circuit scaling if mpsPerPriorityFeeWei is 0

* add test for short circuit case
  • Loading branch information
zhongeric authored Aug 2, 2024
1 parent d4b7da8 commit 9fefbea
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
184760
184378
Original file line number Diff line number Diff line change
@@ -1 +1 @@
202864
202100
Original file line number Diff line number Diff line change
@@ -1 +1 @@
212677
211635
Original file line number Diff line number Diff line change
@@ -1 +1 @@
266386
265066
Original file line number Diff line number Diff line change
@@ -1 +1 @@
196390
195626
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151151
150769
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136713
136331
Original file line number Diff line number Diff line change
@@ -1 +1 @@
160461
160079
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153336
153077
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153145
153081
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153139
153075
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130589
130207
6 changes: 4 additions & 2 deletions src/lib/PriorityFeeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ library PriorityFeeLib {
unchecked {
return InputToken({
token: input.token,
amount: input.amount.mulDivDown((MPS - scalingFactor), MPS),
amount: scalingFactor == 0 ? input.amount : input.amount.mulDivDown((MPS - scalingFactor), MPS),
maxAmount: input.amount
});
}
Expand All @@ -41,7 +41,9 @@ library PriorityFeeLib {
function scale(PriorityOutput memory output, uint256 priorityFee) internal pure returns (OutputToken memory) {
return OutputToken({
token: output.token,
amount: output.amount.mulDivUp((MPS + (priorityFee * output.mpsPerPriorityFeeWei)), MPS),
amount: output.mpsPerPriorityFeeWei == 0
? output.amount
: output.amount.mulDivUp((MPS + (priorityFee * output.mpsPerPriorityFeeWei)), MPS),
recipient: output.recipient
});
}
Expand Down
17 changes: 16 additions & 1 deletion test/lib/PriorityFeeLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,27 @@ contract PriorityFeeLibTest is Test {
uint256 largeAmount = type(uint256).max / MPS + 1;

PriorityOutput memory output =
PriorityOutput({token: address(0), amount: largeAmount, mpsPerPriorityFeeWei: 0, recipient: address(0)});
PriorityOutput({token: address(0), amount: largeAmount, mpsPerPriorityFeeWei: 1, recipient: address(0)});

vm.expectRevert();
PriorityFeeLib.scale(output, tx.gasprice);
}

/// @notice an otherwise reverting large amount should be short circuited if the mpsPerPriorityFeeWei is 0
function testScaleLargeOutputShortCircuit() public {
uint256 priorityFee = 0;
vm.txGasPrice(priorityFee);
assertEq(tx.gasprice, priorityFee);

uint256 largeAmount = type(uint256).max / MPS + 1;

PriorityOutput memory output =
PriorityOutput({token: address(0), amount: largeAmount, mpsPerPriorityFeeWei: 0, recipient: address(0)});

OutputToken memory scaledOutput = PriorityFeeLib.scale(output, tx.gasprice);
assertEq(scaledOutput.amount, output.amount);
}

function testScaleOutputPriorityFee_fuzz(uint256 priorityFee, uint256 mpsPerPriorityFeeWei) public {
// the amount of MPS to scale the output by
uint256 scalingFactor = MPS;
Expand Down

0 comments on commit 9fefbea

Please sign in to comment.