From 2289ec401f16fe926806148d232d681885d27352 Mon Sep 17 00:00:00 2001 From: "A.L." Date: Fri, 29 Nov 2024 07:46:55 +0800 Subject: [PATCH] test: add more cases to cover lines --- .gas-snapshot | 147 ++++++++++++++------------- src/ReservoirPriceOracle.sol | 2 +- src/libraries/OracleErrors.sol | 2 +- test/unit/ReservoirPriceOracle.t.sol | 31 ++++++ 4 files changed, 108 insertions(+), 74 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index c202d10..7ce7848 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,75 +1,78 @@ -QueryProcessorTest:testFindNearestSample_CanFindExactValue(uint32,uint256,uint256,uint256) (runs: 256, μ: 91248691, ~: 96711532) -QueryProcessorTest:testFindNearestSample_CanFindIntermediateValue(uint32,uint256,uint256,uint256) (runs: 256, μ: 90607414, ~: 96828584) -QueryProcessorTest:testFindNearestSample_NotInitialized() (gas: 1056946800) -QueryProcessorTest:testFindNearestSample_OneSample(uint256) (runs: 256, μ: 96842, ~: 96904) -QueryProcessorTest:testGetInstantValue() (gas: 146521) -QueryProcessorTest:testGetInstantValue_NotInitialized(uint256) (runs: 256, μ: 23265, ~: 23265) -QueryProcessorTest:testGetInstantValue_NotInitialized_BeyondBufferSize(uint8,uint16) (runs: 256, μ: 85399653, ~: 85399508) -QueryProcessorTest:testGetPastAccumulator_BufferEmpty(uint8) (runs: 256, μ: 33569, ~: 33672) -QueryProcessorTest:testGetPastAccumulator_ExactMatch(uint32,uint256,uint256,uint16) (runs: 256, μ: 96273323, ~: 105945368) -QueryProcessorTest:testGetPastAccumulator_ExactMatch_LatestAccumulator(uint32,uint256,uint256) (runs: 256, μ: 93181270, ~: 101444188) -QueryProcessorTest:testGetPastAccumulator_ExactMatch_OldestAccumulator(uint32,uint256,uint256) (runs: 256, μ: 93242374, ~: 101509778) -QueryProcessorTest:testGetPastAccumulator_ExtrapolatesBeyondLatest(uint32,uint256,uint256,uint256) (runs: 256, μ: 90553325, ~: 96770318) -QueryProcessorTest:testGetPastAccumulator_InterpolatesBetweenPastAccumulators(uint32,uint256,uint256,uint256) (runs: 256, μ: 90623575, ~: 96841969) -QueryProcessorTest:testGetPastAccumulator_InvalidAgo(uint32,uint256,uint256,uint256) (runs: 256, μ: 90538924, ~: 96754551) -QueryProcessorTest:testGetPastAccumulator_QueryTooOld(uint32,uint256,uint256,uint256) (runs: 256, μ: 90559688, ~: 96773416) -QueryProcessorTest:testGetTimeWeightedAverage_BadSecs() (gas: 13015) -ReservoirPriceOracleTest:testClearRoute() (gas: 69413) -ReservoirPriceOracleTest:testClearRoute_AllWordsCleared() (gas: 194885) -ReservoirPriceOracleTest:testDesignatePair() (gas: 34856) -ReservoirPriceOracleTest:testDesignatePair_IncorrectPair() (gas: 23725) -ReservoirPriceOracleTest:testDesignatePair_NotOwner() (gas: 19374) -ReservoirPriceOracleTest:testDesignatePair_TokenOrderReversed() (gas: 36863) -ReservoirPriceOracleTest:testGetQuote(uint256,uint256) (runs: 256, μ: 42388, ~: 42694) -ReservoirPriceOracleTest:testGetQuote_AmountInTooLarge() (gas: 14273) -ReservoirPriceOracleTest:testGetQuote_BaseIsVault(uint256) (runs: 256, μ: 518131, ~: 517657) -ReservoirPriceOracleTest:testGetQuote_ComplicatedDecimals() (gas: 13490628) -ReservoirPriceOracleTest:testGetQuote_ERC4626AssetFails() (gas: 23701) -ReservoirPriceOracleTest:testGetQuote_Inverse(uint256,uint256) (runs: 256, μ: 46610, ~: 46178) -ReservoirPriceOracleTest:testGetQuote_MultipleHops() (gas: 134813) -ReservoirPriceOracleTest:testGetQuote_MultipleHops_Inverse() (gas: 135253) -ReservoirPriceOracleTest:testGetQuote_MultipleHops_PriceZero() (gas: 152500) -ReservoirPriceOracleTest:testGetQuote_NoFallbackOracle() (gas: 22718) -ReservoirPriceOracleTest:testGetQuote_PriceZero() (gas: 17835) -ReservoirPriceOracleTest:testGetQuote_RandomizeAllParam_1HopRoute(uint256,uint256,address,address,uint8,uint8) (runs: 256, μ: 6885479, ~: 6885358) -ReservoirPriceOracleTest:testGetQuote_RandomizeAllParam_2HopRoute(uint256,uint256,uint256,address,address,address,uint8,uint8,uint8) (runs: 256, μ: 13633870, ~: 13634024) -ReservoirPriceOracleTest:testGetQuote_SameBaseQuote(uint256,address) (runs: 256, μ: 10654, ~: 10654) -ReservoirPriceOracleTest:testGetQuote_UseFallback() (gas: 46078) -ReservoirPriceOracleTest:testGetQuote_ZeroIn() (gas: 46843) -ReservoirPriceOracleTest:testGetQuotes(uint256,uint256) (runs: 256, μ: 33769, ~: 34075) -ReservoirPriceOracleTest:testName() (gas: 10312) -ReservoirPriceOracleTest:testPriceCache_Inverted() (gas: 27380) -ReservoirPriceOracleTest:testSetFallbackOracle_NotOwner() (gas: 11966) -ReservoirPriceOracleTest:testSetRoute() (gas: 73783) -ReservoirPriceOracleTest:testSetRoute_InvalidRewardThreshold() (gas: 45074) -ReservoirPriceOracleTest:testSetRoute_InvalidRoute() (gas: 26799) -ReservoirPriceOracleTest:testSetRoute_InvalidRouteLength() (gas: 25265) -ReservoirPriceOracleTest:testSetRoute_MultipleHops() (gas: 237829) -ReservoirPriceOracleTest:testSetRoute_NotSorted() (gas: 15662) -ReservoirPriceOracleTest:testSetRoute_OverwriteExisting() (gas: 199977) -ReservoirPriceOracleTest:testSetRoute_SameToken() (gas: 15664) -ReservoirPriceOracleTest:testUndesignatePair() (gas: 37368) -ReservoirPriceOracleTest:testUndesignatePair_NotOwner() (gas: 16664) -ReservoirPriceOracleTest:testUpdatePrice_AboveThresholdBelowMaxReward(uint256) (runs: 256, μ: 212450, ~: 212495) -ReservoirPriceOracleTest:testUpdatePrice_BelowThreshold(uint256) (runs: 256, μ: 193585, ~: 192954) -ReservoirPriceOracleTest:testUpdatePrice_BeyondMaxReward(uint256) (runs: 256, μ: 208298, ~: 208353) -ReservoirPriceOracleTest:testUpdatePrice_FirstUpdate() (gas: 200053) -ReservoirPriceOracleTest:testUpdatePrice_IntermediateRoutes() (gas: 21079894) -ReservoirPriceOracleTest:testUpdatePrice_NoPath() (gas: 17953) -ReservoirPriceOracleTest:testUpdatePrice_PriceOutOfRange() (gas: 6998653) -ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_ContractNoReceive() (gas: 192780) -ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_InsufficientReward(uint256) (runs: 256, μ: 264915, ~: 264240) -ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_ZeroRecipient() (gas: 186672) -ReservoirPriceOracleTest:testUpdateRewardGasAmount() (gas: 20940) -ReservoirPriceOracleTest:testUpdateRewardGasAmount_NotOwner() (gas: 11961) -ReservoirPriceOracleTest:testUpdateTwapPeriod(uint256) (runs: 256, μ: 24115, ~: 24291) -ReservoirPriceOracleTest:testUpdateTwapPeriod_InvalidTwapPeriod(uint256) (runs: 256, μ: 19857, ~: 20332) -ReservoirPriceOracleTest:testValidatePair_NoDesignatedPair() (gas: 139101) -ReservoirPriceOracleTest:testWritePriceCache(uint256) (runs: 256, μ: 36820, ~: 36346) +QueryProcessorTest:testFindNearestSample_CanFindExactValue(uint32,uint256,uint256,uint256) (runs: 256, μ: 72021263, ~: 80647386) +QueryProcessorTest:testFindNearestSample_CanFindIntermediateValue(uint32,uint256,uint256,uint256) (runs: 256, μ: 70481909, ~: 79354051) +QueryProcessorTest:testFindNearestSample_NotInitialized() (gas: 1056944146) +QueryProcessorTest:testFindNearestSample_OneSample(uint256) (runs: 256, μ: 81215, ~: 81248) +QueryProcessorTest:testGetInstantValue() (gas: 126314) +QueryProcessorTest:testGetInstantValue_NotInitialized(uint256) (runs: 256, μ: 19400, ~: 19400) +QueryProcessorTest:testGetInstantValue_NotInitialized_BeyondBufferSize(uint8,uint16) (runs: 256, μ: 70425209, ~: 70425126) +QueryProcessorTest:testGetPastAccumulator_BufferEmpty(uint8) (runs: 256, μ: 26922, ~: 26984) +QueryProcessorTest:testGetPastAccumulator_ExactMatch(uint32,uint256,uint256,uint16) (runs: 256, μ: 74540972, ~: 82895341) +QueryProcessorTest:testGetPastAccumulator_ExactMatch_LatestAccumulator(uint32,uint256,uint256) (runs: 256, μ: 70097476, ~: 77901320) +QueryProcessorTest:testGetPastAccumulator_ExactMatch_OldestAccumulator(uint32,uint256,uint256) (runs: 256, μ: 70127426, ~: 77933087) +QueryProcessorTest:testGetPastAccumulator_ExtrapolatesBeyondLatest(uint32,uint256,uint256,uint256) (runs: 256, μ: 70455556, ~: 79325066) +QueryProcessorTest:testGetPastAccumulator_InterpolatesBetweenPastAccumulators(uint32,uint256,uint256,uint256) (runs: 256, μ: 70489752, ~: 79360343) +QueryProcessorTest:testGetPastAccumulator_InvalidAgo(uint32,uint256,uint256,uint256) (runs: 256, μ: 70447172, ~: 79317439) +QueryProcessorTest:testGetPastAccumulator_QueryTooOld(uint32,uint256,uint256,uint256) (runs: 256, μ: 70458583, ~: 79327014) +QueryProcessorTest:testGetTimeWeightedAverage(uint32,uint256,uint256,uint256,uint256) (runs: 256, μ: 110178296, ~: 122265976) +QueryProcessorTest:testGetTimeWeightedAverage_BadSecs() (gas: 10981) +ReservoirPriceOracleTest:testClearRoute() (gas: 52484) +ReservoirPriceOracleTest:testClearRoute_AllWordsCleared() (gas: 160641) +ReservoirPriceOracleTest:testDesignatePair() (gas: 29056) +ReservoirPriceOracleTest:testDesignatePair_IncorrectPair() (gas: 21126) +ReservoirPriceOracleTest:testDesignatePair_NotOwner() (gas: 17519) +ReservoirPriceOracleTest:testDesignatePair_TokenOrderReversed() (gas: 30628) +ReservoirPriceOracleTest:testGetQuote(uint256,uint256) (runs: 256, μ: 34025, ~: 34149) +ReservoirPriceOracleTest:testGetQuote_AmountInTooLarge() (gas: 12940) +ReservoirPriceOracleTest:testGetQuote_BaseIsVault(uint256) (runs: 256, μ: 401367, ~: 401124) +ReservoirPriceOracleTest:testGetQuote_ComplicatedDecimals() (gas: 10388171) +ReservoirPriceOracleTest:testGetQuote_ERC4626AssetFails() (gas: 21476) +ReservoirPriceOracleTest:testGetQuote_Inverse(uint256,uint256) (runs: 256, μ: 36148, ~: 36211) +ReservoirPriceOracleTest:testGetQuote_MultipleHops() (gas: 112270) +ReservoirPriceOracleTest:testGetQuote_MultipleHops_Inverse() (gas: 112544) +ReservoirPriceOracleTest:testGetQuote_MultipleHops_PriceZero() (gas: 122874) +ReservoirPriceOracleTest:testGetQuote_NoFallbackOracle() (gas: 20799) +ReservoirPriceOracleTest:testGetQuote_PriceZero() (gas: 15930) +ReservoirPriceOracleTest:testGetQuote_RandomizeAllParam_1HopRoute(uint256,uint256,address,address,uint8,uint8) (runs: 256, μ: 5331593, ~: 5331557) +ReservoirPriceOracleTest:testGetQuote_RandomizeAllParam_2HopRoute(uint256,uint256,uint256,address,address,address,uint8,uint8,uint8) (runs: 256, μ: 10530244, ~: 10530277) +ReservoirPriceOracleTest:testGetQuote_SameBaseQuote(uint256,address) (runs: 256, μ: 8994, ~: 8994) +ReservoirPriceOracleTest:testGetQuote_UseFallback() (gas: 38334) +ReservoirPriceOracleTest:testGetQuote_ZeroIn() (gas: 36979) +ReservoirPriceOracleTest:testGetQuotes(uint256,uint256) (runs: 256, μ: 26203, ~: 26327) +ReservoirPriceOracleTest:testName() (gas: 9355) +ReservoirPriceOracleTest:testPriceCache_Inverted() (gas: 22016) +ReservoirPriceOracleTest:testSetFallbackOracle_NotOwner() (gas: 10928) +ReservoirPriceOracleTest:testSetRoute() (gas: 61702) +ReservoirPriceOracleTest:testSetRoute_InvalidDecimals() (gas: 761507) +ReservoirPriceOracleTest:testSetRoute_InvalidRewardThreshold() (gas: 37944) +ReservoirPriceOracleTest:testSetRoute_InvalidRewardThresholdLength() (gas: 18072) +ReservoirPriceOracleTest:testSetRoute_InvalidRoute() (gas: 20107) +ReservoirPriceOracleTest:testSetRoute_InvalidRouteLength() (gas: 19231) +ReservoirPriceOracleTest:testSetRoute_MultipleHops() (gas: 202204) +ReservoirPriceOracleTest:testSetRoute_NotSorted() (gas: 13029) +ReservoirPriceOracleTest:testSetRoute_OverwriteExisting() (gas: 170924) +ReservoirPriceOracleTest:testSetRoute_SameToken() (gas: 12997) +ReservoirPriceOracleTest:testUndesignatePair() (gas: 30215) +ReservoirPriceOracleTest:testUndesignatePair_NotOwner() (gas: 15322) +ReservoirPriceOracleTest:testUpdatePrice_AboveThresholdBelowMaxReward(uint256) (runs: 256, μ: 165909, ~: 165929) +ReservoirPriceOracleTest:testUpdatePrice_BelowThreshold(uint256) (runs: 256, μ: 150821, ~: 150497) +ReservoirPriceOracleTest:testUpdatePrice_BeyondMaxReward(uint256) (runs: 256, μ: 163385, ~: 163410) +ReservoirPriceOracleTest:testUpdatePrice_FirstUpdate() (gas: 154534) +ReservoirPriceOracleTest:testUpdatePrice_IntermediateRoutes() (gas: 16300656) +ReservoirPriceOracleTest:testUpdatePrice_NoPath() (gas: 15998) +ReservoirPriceOracleTest:testUpdatePrice_PriceOutOfRange() (gas: 5411994) +ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_ContractNoReceive() (gas: 153671) +ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_InsufficientReward(uint256) (runs: 256, μ: 213287, ~: 213505) +ReservoirPriceOracleTest:testUpdatePrice_RewardEligible_ZeroRecipient() (gas: 147341) +ReservoirPriceOracleTest:testUpdateRewardGasAmount() (gas: 19038) +ReservoirPriceOracleTest:testUpdateRewardGasAmount_NotOwner() (gas: 10953) +ReservoirPriceOracleTest:testUpdateTwapPeriod(uint256) (runs: 256, μ: 21616, ~: 21709) +ReservoirPriceOracleTest:testUpdateTwapPeriod_InvalidTwapPeriod(uint256) (runs: 256, μ: 17706, ~: 17994) +ReservoirPriceOracleTest:testValidatePair_NoDesignatedPair() (gas: 120366) +ReservoirPriceOracleTest:testWritePriceCache(uint256) (runs: 256, μ: 30384, ~: 30141) RoutesLibTest:testGetDecimalDifference() (gas: 4852) RoutesLibTest:testIsCompositeRoute() (gas: 5268) RoutesLibTest:testPackSimplePrice(int8,uint256) (runs: 256, μ: 10185, ~: 9724) -SamplesTest:testAccumulator() (gas: 4537) -SamplesTest:testAccumulator_BadVariableRequest() (gas: 3862) -SamplesTest:testInstant() (gas: 4492) -SamplesTest:testInstant_BadVariableRequest() (gas: 3883) \ No newline at end of file +SamplesTest:testAccumulator() (gas: 3930) +SamplesTest:testAccumulator_BadVariableRequest() (gas: 3355) +SamplesTest:testInstant() (gas: 3880) +SamplesTest:testInstant_BadVariableRequest() (gas: 3376) \ No newline at end of file diff --git a/src/ReservoirPriceOracle.sol b/src/ReservoirPriceOracle.sol index d405366..2289b1a 100644 --- a/src/ReservoirPriceOracle.sol +++ b/src/ReservoirPriceOracle.sol @@ -492,7 +492,7 @@ contract ReservoirPriceOracle is IPriceOracle, Owned(msg.sender), ReentrancyGuar _validateTokens(aToken0, aToken1); require(lRouteLength > 1 && lRouteLength <= Constants.MAX_ROUTE_LENGTH, OracleErrors.InvalidRouteLength()); require(aRoute[0] == aToken0 && aRoute[lRouteLength - 1] == aToken1, OracleErrors.InvalidRoute()); - require(aRewardThresholds.length == lRouteLength - 1, OracleErrors.InvalidArrayLengthRewardThresholds()); + require(aRewardThresholds.length == lRouteLength - 1, OracleErrors.InvalidRewardThresholdsLength()); bytes32 lSlot = Utils.calculateSlot(aToken0, aToken1); diff --git a/src/libraries/OracleErrors.sol b/src/libraries/OracleErrors.sol index 2a14a8d..95b3ca3 100644 --- a/src/libraries/OracleErrors.sol +++ b/src/libraries/OracleErrors.sol @@ -6,7 +6,7 @@ library OracleErrors { // config errors error IncorrectTokensDesignatePair(); error InvalidRewardThreshold(); - error InvalidArrayLengthRewardThresholds(); + error InvalidRewardThresholdsLength(); error InvalidRoute(); error InvalidRouteLength(); error InvalidTokensProvided(); diff --git a/test/unit/ReservoirPriceOracle.t.sol b/test/unit/ReservoirPriceOracle.t.sol index 07c9aa7..5ade774 100644 --- a/test/unit/ReservoirPriceOracle.t.sol +++ b/test/unit/ReservoirPriceOracle.t.sol @@ -1083,6 +1083,37 @@ contract ReservoirPriceOracleTest is BaseTest { _oracle.setRoute(lRoute[0], lRoute[1], lRoute, lInvalidRewardThreshold); } + function testSetRoute_InvalidRewardThresholdLength() external { + address[] memory lRoute = new address[](2); + uint16[] memory lRewardThreshold = new uint16[](2); + lRewardThreshold[0] = Constants.BP_SCALE; + lRoute[0] = address(_tokenC); + lRoute[1] = address(_tokenD); + + // act & assert + vm.expectRevert(OracleErrors.InvalidRewardThresholdsLength.selector); + _oracle.setRoute(address(_tokenC), address(_tokenD), lRoute, lRewardThreshold); + } + + function testSetRoute_InvalidDecimals() external { + // arrange + MintableERC20 lToken = new MintableERC20("AA", "AA", 21); + address[] memory lRoute = new address[](2); + lRoute[0] = address(lToken) < address(_tokenA) ? address(lToken) : address(_tokenA); + lRoute[1] = address(lToken) < address(_tokenA) ? address(_tokenA) : address(lToken); + uint16[] memory lRewardThreshold = new uint16[](1); + lRewardThreshold[0] = Constants.BP_SCALE; + + // act & assert + vm.expectRevert(OracleErrors.UnsupportedTokenDecimals.selector); + _oracle.setRoute( + address(lToken) < address(_tokenA) ? address(lToken) : address(_tokenA), + address(lToken) < address(_tokenA) ? address(_tokenA) : address(lToken), + lRoute, + lRewardThreshold + ); + } + function testUpdateRewardGasAmount_NotOwner() external { // act & assert vm.prank(address(123));