From 43ae0093529a8c1fbfdd17745b2f8d0d5a7761e7 Mon Sep 17 00:00:00 2001 From: Morgan Kuphal Date: Fri, 17 May 2024 20:39:16 -0500 Subject: [PATCH] tests wip --- .../functions/dev/v1_X/FunctionsBilling.sol | 2 +- .../dev/v1_X/libraries/ChainSpecificUtil.sol | 8 +- .../tests/v1_X/ChainSpecificUtil.t.sol | 103 +++++++++++++----- .../tests/v1_X/FunctionsBilling.t.sol | 3 +- .../tests/v1_X/FunctionsCoordinator.t.sol | 2 +- .../src/v0.8/functions/tests/v1_X/Setup.t.sol | 3 +- .../v0.17.1/src/L2/L1Block.sol | 2 +- 7 files changed, 85 insertions(+), 38 deletions(-) diff --git a/contracts/src/v0.8/functions/dev/v1_X/FunctionsBilling.sol b/contracts/src/v0.8/functions/dev/v1_X/FunctionsBilling.sol index 9be1c9707e5..d653a9bcf9d 100644 --- a/contracts/src/v0.8/functions/dev/v1_X/FunctionsBilling.sol +++ b/contracts/src/v0.8/functions/dev/v1_X/FunctionsBilling.sol @@ -202,7 +202,7 @@ abstract contract FunctionsBilling is Routable, IFunctionsBilling { uint256 totalFeeWei = (gasPriceWei * executionGas) + l1FeeWei; // Basis Points are 1/100th of 1%, divide by 10_000 to bring back to original units - uint256 totalFeeWeiWithOverestimate = totalFee + ((totalFee * s_config.fulfillmentGasPriceOverEstimationBP) / 10_000); + uint256 totalFeeWeiWithOverestimate = totalFeeWei + ((totalFeeWei * s_config.fulfillmentGasPriceOverEstimationBP) / 10_000); uint96 estimatedGasReimbursementJuels = _getJuelsFromWei(totalFeeWeiWithOverestimate); diff --git a/contracts/src/v0.8/functions/dev/v1_X/libraries/ChainSpecificUtil.sol b/contracts/src/v0.8/functions/dev/v1_X/libraries/ChainSpecificUtil.sol index b6d9ae32418..36f54e3b2eb 100644 --- a/contracts/src/v0.8/functions/dev/v1_X/libraries/ChainSpecificUtil.sol +++ b/contracts/src/v0.8/functions/dev/v1_X/libraries/ChainSpecificUtil.sol @@ -10,7 +10,7 @@ library ChainSpecificUtil { // ------------ Start Arbitrum Constants ------------ /// @dev ARB_L1_FEE_DATA_PADDING_SIZE is the L1 data padding for Optimism - uint256 private const ARB_L1_FEE_DATA_PADDING_SIZE = 140; + uint256 private constant ARB_L1_FEE_DATA_PADDING_SIZE = 140; /// @dev ARBGAS_ADDR is the address of the ArbGasInfo precompile on Arbitrum. /// @dev reference: https://github.com/OffchainLabs/nitro/blob/v2.0.14/contracts/src/precompiles/ArbGasInfo.sol#L10 address private constant ARBGAS_ADDR = address(0x000000000000000000000000000000000000006C); @@ -24,7 +24,7 @@ library ChainSpecificUtil { // ------------ Start Optimism Constants ------------ /// @dev OP_L1_FEE_DATA_PADDING_SIZE is the L1 data padding for Optimism - uint256 private const OP_L1_FEE_DATA_PADDING_SIZE = 35; + uint256 private constant OP_L1_FEE_DATA_PADDING_SIZE = 35; /// @dev L1BLOCK_ADDR is the address of the L1Block precompile on Optimism. address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015); L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR); @@ -51,11 +51,11 @@ library ChainSpecificUtil { } else if (_isOptimismChainId(chainid)) { // https://docs.optimism.io/stack/transactions/fees#ecotone // note we conservatively assume all non-zero bytes: tx_compressed_size = tx_data_size_bytes - uint256 l1BaseFeeWei = L1BLOCK.baseFee(); + uint256 l1BaseFeeWei = L1BLOCK.basefee(); uint256 l1BaseFeeScalar = L1BLOCK.baseFeeScalar(); uint256 l1BlobBaseFeeWei = L1BLOCK.blobBaseFee(); uint256 l1BlobBaseFeeScalar = L1BLOCK.blobBaseFeeScalar(); - uint256 weightedGasPrice = 16 * l1BaseFeeScalar * l1BaseFee + l1BlobBaseFeeScalar * l1BlobBaseFeeWei; + uint256 weightedGasPrice = 16 * l1BaseFeeScalar * l1BaseFeeWei + l1BlobBaseFeeScalar * l1BlobBaseFeeWei; return weightedGasPrice * dataSizeBytes; } return 0; diff --git a/contracts/src/v0.8/functions/tests/v1_X/ChainSpecificUtil.t.sol b/contracts/src/v0.8/functions/tests/v1_X/ChainSpecificUtil.t.sol index 5384a66d912..f137e41d9b0 100644 --- a/contracts/src/v0.8/functions/tests/v1_X/ChainSpecificUtil.t.sol +++ b/contracts/src/v0.8/functions/tests/v1_X/ChainSpecificUtil.t.sol @@ -11,22 +11,29 @@ import {FunctionsResponse} from "../../dev/v1_X/libraries/FunctionsResponse.sol" import {FunctionsFulfillmentSetup} from "./Setup.t.sol"; import {ArbGasInfo} from "../../../vendor/@arbitrum/nitro-contracts/src/precompiles/ArbGasInfo.sol"; -import {OVM_GasPriceOracle} from "../../../vendor/@eth-optimism/contracts/v0.8.9/contracts/L2/predeploys/OVM_GasPriceOracle.sol"; +import {L1Block} from "../../../vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol"; -/// @notice #_getCurrentTxL1GasFees Arbitrum +/// @notice #_getL1FeeUpperLimit Arbitrum /// @dev Arbitrum gas formula = L2 Gas Price * (Gas used on L2 + Extra Buffer for L1 cost) /// @dev where Extra Buffer for L1 cost = (L1 Estimated Cost / L2 Gas Price) -contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillmentSetup { +contract ChainSpecificUtil__getL1FeeUpperLimit_Arbitrum is FunctionsFulfillmentSetup { address private constant ARBGAS_ADDR = address(0x000000000000000000000000000000000000006C); uint256 private constant L1_FEE_WEI = 15_818_209_764_247; uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE)); function setUp() public virtual override { - vm.mockCall(ARBGAS_ADDR, abi.encodeWithSelector(ArbGasInfo.getCurrentTxL1GasFees.selector), abi.encode(L1_FEE_WEI)); + uint256 unused = 0; + uint256 gasPerL1CalldataByte = 4; + vm.mockCall( + ARBGAS_ADDR, + abi.encodeWithSelector( + ArbGasInfo.getPricesInWei.selector), + abi.encode(unused, gasPerL1CalldataByte, unused, unused, unused, unused) + ); } - function test__getCurrentTxL1GasFees_SuccessWhenArbitrumMainnet() public { + function test__getL1FeeUpperLimit_SuccessWhenArbitrumMainnet() public { // Set the chainID vm.chainId(42161); @@ -43,7 +50,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenArbitrumGoerli() public { + function test__getL1FeeUpperLimit_SuccessWhenArbitrumGoerli() public { // Set the chainID vm.chainId(421613); @@ -60,7 +67,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenArbitrumSepolia() public { + function test__getL1FeeUpperLimit_SuccessWhenArbitrumSepolia() public { // Set the chainID vm.chainId(421614); @@ -78,24 +85,43 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Arbitrum is FunctionsFulfillme } } -/// @notice #_getCurrentTxL1GasFees Optimism +/// @notice #_getL1FeeUpperLimit Optimism /// @dev Optimism gas formula = ((l2_base_fee + l2_priority_fee) * l2_gas_used) + L1 data fee /// @dev where L1 data fee = l1_gas_price * ((count_zero_bytes(tx_data) * 4 + count_non_zero_bytes(tx_data) * 16) + fixed_overhead + noncalldata_gas) * dynamic_overhead -contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillmentSetup { - address private constant OVM_GASPRICEORACLE_ADDR = address(0x420000000000000000000000000000000000000F); - uint256 private constant L1_FEE_WEI = 15_818_209_764_247; +contract ChainSpecificUtil__getL1FeeUpperLimit_Optimism is FunctionsFulfillmentSetup { + address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015); + L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR); + uint256 private constant L1_BASE_FEE_WEI = 15_818_209; + uint256 private constant L1_BASE_FEE_SCALAR = 2; + uint256 private constant L1_BLOB_BASE_FEE_WEI = 15_818_209; + uint256 private constant L1_BLOB_BASE_FEE_SCALAR = 2; - uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE)); + uint96 l1FeeJuels = uint96((1e18 * L1_BASE_FEE_WEI) / uint256(LINK_ETH_RATE)); function setUp() public virtual override { vm.mockCall( - OVM_GASPRICEORACLE_ADDR, - abi.encodeWithSelector(OVM_GasPriceOracle.getL1Fee.selector), - abi.encode(L1_FEE_WEI) + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.basefee.selector), + abi.encode(L1_BASE_FEE_WEI) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.baseFeeScalar.selector), + abi.encode(L1_BASE_FEE_SCALAR) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.blobBaseFee.selector), + abi.encode(L1_BLOB_BASE_FEE_WEI) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.blobBaseFeeScalar.selector), + abi.encode(L1_BLOB_BASE_FEE_SCALAR) ); } - function test__getCurrentTxL1GasFees_SuccessWhenOptimismMainnet() public { + function test__getL1FeeUpperLimit_SuccessWhenOptimismMainnet() public { // Set the chainID vm.chainId(10); @@ -112,7 +138,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenOptimismGoerli() public { + function test__getL1FeeUpperLimit_SuccessWhenOptimismGoerli() public { // Set the chainID vm.chainId(420); @@ -129,7 +155,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenOptimismSepolia() public { + function test__getL1FeeUpperLimit_SuccessWhenOptimismSepolia() public { // Set the chainID vm.chainId(11155420); @@ -147,24 +173,43 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Optimism is FunctionsFulfillme } } -/// @notice #_getCurrentTxL1GasFees Base +/// @notice #_getL1FeeUpperLimit Base /// @dev Base gas formula uses Optimism formula = ((l2_base_fee + l2_priority_fee) * l2_gas_used) + L1 data fee /// @dev where L1 data fee = l1_gas_price * ((count_zero_bytes(tx_data) * 4 + count_non_zero_bytes(tx_data) * 16) + fixed_overhead + noncalldata_gas) * dynamic_overhead -contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSetup { - address private constant OVM_GASPRICEORACLE_ADDR = address(0x420000000000000000000000000000000000000F); - uint256 private constant L1_FEE_WEI = 15_818_209_764_247; +contract ChainSpecificUtil__getL1FeeUpperLimit_Base is FunctionsFulfillmentSetup { + address private constant L1BLOCK_ADDR = address(0x4200000000000000000000000000000000000015); + L1Block private constant L1BLOCK = L1Block(L1BLOCK_ADDR); + uint256 private constant L1_BASE_FEE_WEI = 15_818_209; + uint256 private constant L1_BASE_FEE_SCALAR = 2; + uint256 private constant L1_BLOB_BASE_FEE_WEI = 15_818_209; + uint256 private constant L1_BLOB_BASE_FEE_SCALAR = 2; - uint96 l1FeeJuels = uint96((1e18 * L1_FEE_WEI) / uint256(LINK_ETH_RATE)); + uint96 l1FeeJuels = uint96((1e18 * L1_BASE_FEE_WEI) / uint256(LINK_ETH_RATE)); function setUp() public virtual override { vm.mockCall( - OVM_GASPRICEORACLE_ADDR, - abi.encodeWithSelector(OVM_GasPriceOracle.getL1Fee.selector), - abi.encode(L1_FEE_WEI) + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.basefee.selector), + abi.encode(L1_BASE_FEE_WEI) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.baseFeeScalar.selector), + abi.encode(L1_BASE_FEE_SCALAR) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.blobBaseFee.selector), + abi.encode(L1_BLOB_BASE_FEE_WEI) + ); + vm.mockCall( + L1BLOCK_ADDR, + abi.encodeWithSelector(L1BLOCK.blobBaseFeeScalar.selector), + abi.encode(L1_BLOB_BASE_FEE_SCALAR) ); } - function test__getCurrentTxL1GasFees_SuccessWhenBaseMainnet() public { + function test__getL1FeeUpperLimit_SuccessWhenBaseMainnet() public { // Set the chainID vm.chainId(8453); @@ -181,7 +226,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSe assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenBaseGoerli() public { + function test__getL1FeeUpperLimit_SuccessWhenBaseGoerli() public { // Set the chainID vm.chainId(84531); @@ -198,7 +243,7 @@ contract ChainSpecificUtil__getCurrentTxL1GasFees_Base is FunctionsFulfillmentSe assertEq(s_responses[1].totalCostJuels, expectedTotalCostJuels); } - function test__getCurrentTxL1GasFees_SuccessWhenBaseSepolia() public { + function test__getL1FeeUpperLimit_SuccessWhenBaseSepolia() public { // Set the chainID vm.chainId(84532); diff --git a/contracts/src/v0.8/functions/tests/v1_X/FunctionsBilling.t.sol b/contracts/src/v0.8/functions/tests/v1_X/FunctionsBilling.t.sol index 774fe4e875c..b66c1e42f90 100644 --- a/contracts/src/v0.8/functions/tests/v1_X/FunctionsBilling.t.sol +++ b/contracts/src/v0.8/functions/tests/v1_X/FunctionsBilling.t.sol @@ -59,7 +59,8 @@ contract FunctionsBilling_UpdateConfig is FunctionsRouterSetup { fallbackNativePerUnitLink: getCoordinatorConfig().fallbackNativePerUnitLink * 2, fallbackUsdPerUnitLink: getCoordinatorConfig().fallbackUsdPerUnitLink * 2, fallbackUsdPerUnitLinkDecimals: getCoordinatorConfig().fallbackUsdPerUnitLinkDecimals * 2, - minimumEstimateGasPriceWei: getCoordinatorConfig().minimumEstimateGasPriceWei * 2 + minimumEstimateGasPriceWei: getCoordinatorConfig().minimumEstimateGasPriceWei * 2, + transmitTxSizeBytes: getCoordinatorConfig().transmitTxSizeBytes * 2 }); } diff --git a/contracts/src/v0.8/functions/tests/v1_X/FunctionsCoordinator.t.sol b/contracts/src/v0.8/functions/tests/v1_X/FunctionsCoordinator.t.sol index 3944c5873b1..03d9f645146 100644 --- a/contracts/src/v0.8/functions/tests/v1_X/FunctionsCoordinator.t.sol +++ b/contracts/src/v0.8/functions/tests/v1_X/FunctionsCoordinator.t.sol @@ -14,7 +14,7 @@ import {FunctionsRouterSetup, FunctionsDONSetup, FunctionsSubscriptionSetup} fro /// @notice #constructor contract FunctionsCoordinator_Constructor is FunctionsRouterSetup { function test_Constructor_Success() public { - assertEq(s_functionsCoordinator.typeAndVersion(), "Functions Coordinator v1.3.0"); + assertEq(s_functionsCoordinator.typeAndVersion(), "Functions Coordinator v1.3.1"); assertEq(s_functionsCoordinator.owner(), OWNER_ADDRESS); } } diff --git a/contracts/src/v0.8/functions/tests/v1_X/Setup.t.sol b/contracts/src/v0.8/functions/tests/v1_X/Setup.t.sol index 32dc53e42ab..eaae8b1ab2e 100644 --- a/contracts/src/v0.8/functions/tests/v1_X/Setup.t.sol +++ b/contracts/src/v0.8/functions/tests/v1_X/Setup.t.sol @@ -93,7 +93,8 @@ contract FunctionsRouterSetup is BaseTest { fallbackNativePerUnitLink: 5000000000000000, fallbackUsdPerUnitLink: 1400000000, fallbackUsdPerUnitLinkDecimals: 8, - minimumEstimateGasPriceWei: 1000000000 // 1 gwei + minimumEstimateGasPriceWei: 1000000000, // 1 gwei + transmitTxSizeBytes: 5000 }); } diff --git a/contracts/src/v0.8/vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol b/contracts/src/v0.8/vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol index e047e980a30..133d2f7ad16 100644 --- a/contracts/src/v0.8/vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol +++ b/contracts/src/v0.8/vendor/@eth-optimism/contracts-bedrock/v0.17.1/src/L2/L1Block.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.15; +pragma solidity ^0.8.15; import { ISemver } from "../universal/ISemver.sol";