From 0de069d427741b2923d29ae981b0ecedbfe0a817 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Wed, 29 May 2024 00:40:48 -0400 Subject: [PATCH] tests for fee controller gas limit --- .forge-snapshots/set protocol fee.snap | 2 +- ...wap skips hook call if hook is caller.snap | 2 +- .forge-snapshots/swap with dynamic fee.snap | 2 +- .../swap with lp fee and protocol fee.snap | 2 +- .../swap with return dynamic fee.snap | 2 +- .../update dynamic fee in before swap.snap | 2 +- src/test/ProtocolFeeControllerTest.sol | 8 +++++ src/test/ProtocolFeesImplementation.sol | 25 ++++++++++++++++ test/ProtocolFeesImplementation.t.sol | 29 ++++++++++++++++++- test/utils/Deployers.sol | 5 +++- 10 files changed, 71 insertions(+), 8 deletions(-) diff --git a/.forge-snapshots/set protocol fee.snap b/.forge-snapshots/set protocol fee.snap index d745d907d..191ca91c4 100644 --- a/.forge-snapshots/set protocol fee.snap +++ b/.forge-snapshots/set protocol fee.snap @@ -1 +1 @@ -31702 \ No newline at end of file +31746 \ No newline at end of file diff --git a/.forge-snapshots/swap skips hook call if hook is caller.snap b/.forge-snapshots/swap skips hook call if hook is caller.snap index 48ffdca94..e2b9d027b 100644 --- a/.forge-snapshots/swap skips hook call if hook is caller.snap +++ b/.forge-snapshots/swap skips hook call if hook is caller.snap @@ -1 +1 @@ -211054 \ No newline at end of file +211066 \ No newline at end of file diff --git a/.forge-snapshots/swap with dynamic fee.snap b/.forge-snapshots/swap with dynamic fee.snap index 5e7a69da6..c9cc8867d 100644 --- a/.forge-snapshots/swap with dynamic fee.snap +++ b/.forge-snapshots/swap with dynamic fee.snap @@ -1 +1 @@ -142260 \ No newline at end of file +142272 \ No newline at end of file diff --git a/.forge-snapshots/swap with lp fee and protocol fee.snap b/.forge-snapshots/swap with lp fee and protocol fee.snap index f8a54567b..4fb713d1f 100644 --- a/.forge-snapshots/swap with lp fee and protocol fee.snap +++ b/.forge-snapshots/swap with lp fee and protocol fee.snap @@ -1 +1 @@ -172061 \ No newline at end of file +172073 \ No newline at end of file diff --git a/.forge-snapshots/swap with return dynamic fee.snap b/.forge-snapshots/swap with return dynamic fee.snap index 8dae67f26..4ae7d6793 100644 --- a/.forge-snapshots/swap with return dynamic fee.snap +++ b/.forge-snapshots/swap with return dynamic fee.snap @@ -1 +1 @@ -148491 \ No newline at end of file +148503 \ No newline at end of file diff --git a/.forge-snapshots/update dynamic fee in before swap.snap b/.forge-snapshots/update dynamic fee in before swap.snap index b2df693e3..1f6c8b1bb 100644 --- a/.forge-snapshots/update dynamic fee in before swap.snap +++ b/.forge-snapshots/update dynamic fee in before swap.snap @@ -1 +1 @@ -150749 \ No newline at end of file +150761 \ No newline at end of file diff --git a/src/test/ProtocolFeeControllerTest.sol b/src/test/ProtocolFeeControllerTest.sol index 13813083b..681ca330a 100644 --- a/src/test/ProtocolFeeControllerTest.sol +++ b/src/test/ProtocolFeeControllerTest.sol @@ -59,3 +59,11 @@ contract InvalidReturnSizeProtocolFeeControllerTest is IProtocolFeeController { } } } + +contract GasLimitProtocolFeeControllerTest is IProtocolFeeController { + function protocolFeeForPool(PoolKey memory /* key */ ) external pure returns (uint24) { + // consume gas + while (true) {} + return 1000; + } +} diff --git a/src/test/ProtocolFeesImplementation.sol b/src/test/ProtocolFeesImplementation.sol index c31be6140..8be58eb70 100644 --- a/src/test/ProtocolFeesImplementation.sol +++ b/src/test/ProtocolFeesImplementation.sol @@ -33,4 +33,29 @@ contract ProtocolFeesImplementation is ProtocolFees { function updateProtocolFees(Currency currency, uint256 amount) public { ProtocolFees._updateProtocolFees(currency, amount); } + + function consumeGasLimitAndFetchFee(PoolKey memory key) public { + // consume gas before calling fetchProtocolFee / getting the protocolFeeForPool from the controller + while (true) { + // once gas left is less than the limit, stop consuming gas + if (gasleft() < 9079256829993496519) { + break; + } + } + // fetch the protocol fee after consuming gas + // will revert since the gas left is less than the limit + fetchProtocolFee(key); + } + + function consumeGasAndFetchFee(PoolKey memory key) public { + while (true) { + // consume just under the gas limit + if (gasleft() < 9079256829993490000) { + break; + } + } + // try to fetch the protocol fee + // will revert while fetching since the gas limit has been reached + fetchProtocolFee(key); + } } diff --git a/test/ProtocolFeesImplementation.t.sol b/test/ProtocolFeesImplementation.t.sol index 462999b9d..e8ac41c14 100644 --- a/test/ProtocolFeesImplementation.t.sol +++ b/test/ProtocolFeesImplementation.t.sol @@ -19,7 +19,8 @@ import { OutOfBoundsProtocolFeeControllerTest, RevertingProtocolFeeControllerTest, OverflowProtocolFeeControllerTest, - InvalidReturnSizeProtocolFeeControllerTest + InvalidReturnSizeProtocolFeeControllerTest, + GasLimitProtocolFeeControllerTest } from "../src/test/ProtocolFeeControllerTest.sol"; contract ProtocolFeesTest is Test, GasSnapshot, Deployers { @@ -218,4 +219,30 @@ contract ProtocolFeesTest is Test, GasSnapshot, Deployers { assertFalse(success); assertEq(protocolFee, 0); } + + function test_fetchProtocolFee_gasLimit() public { + gasLimitFeeController = new GasLimitProtocolFeeControllerTest(); + protocolFees.setProtocolFeeController(gasLimitFeeController); + vm.prank(address(gasLimitFeeController)); + (bool success, uint24 protocolFee) = protocolFees.fetchProtocolFee(key); + assertFalse(success); + assertEq(protocolFee, 0); + } + + function test_fetchProtocolFee_revertsWithProtocolFeeCannotBeFetched() public { + protocolFees = new ProtocolFeesImplementation(9079256829993496519); + protocolFees.setProtocolFeeController(feeController); + vm.prank(address(feeController)); + vm.expectRevert(IProtocolFees.ProtocolFeeCannotBeFetched.selector); + // consumes too much gas before fetching the fee + protocolFees.consumeGasLimitAndFetchFee(key); + } + + function test_fetchProtocolFee_revertsWithGasLimitExceeded() public { + protocolFees = new ProtocolFeesImplementation(9079256829993496519); + protocolFees.setProtocolFeeController(feeController); + vm.prank(address(feeController)); + vm.expectRevert(); + protocolFees.consumeGasAndFetchFee(key); + } } diff --git a/test/utils/Deployers.sol b/test/utils/Deployers.sol index e3e3dea00..59087f246 100644 --- a/test/utils/Deployers.sol +++ b/test/utils/Deployers.sol @@ -28,7 +28,8 @@ import { OutOfBoundsProtocolFeeControllerTest, RevertingProtocolFeeControllerTest, OverflowProtocolFeeControllerTest, - InvalidReturnSizeProtocolFeeControllerTest + InvalidReturnSizeProtocolFeeControllerTest, + GasLimitProtocolFeeControllerTest } from "../../src/test/ProtocolFeeControllerTest.sol"; contract Deployers { @@ -72,6 +73,7 @@ contract Deployers { OutOfBoundsProtocolFeeControllerTest outOfBoundsFeeController; OverflowProtocolFeeControllerTest overflowFeeController; InvalidReturnSizeProtocolFeeControllerTest invalidReturnSizeFeeController; + GasLimitProtocolFeeControllerTest gasLimitFeeController; PoolKey key; PoolKey nativeKey; @@ -111,6 +113,7 @@ contract Deployers { outOfBoundsFeeController = new OutOfBoundsProtocolFeeControllerTest(); overflowFeeController = new OverflowProtocolFeeControllerTest(); invalidReturnSizeFeeController = new InvalidReturnSizeProtocolFeeControllerTest(); + gasLimitFeeController = new GasLimitProtocolFeeControllerTest(); manager.setProtocolFeeController(feeController); }