Skip to content

Commit

Permalink
refactor(contracts/core): improved FeeOracleV2 update efficiency (#2537)
Browse files Browse the repository at this point in the history
Refactored FeeOracleV2 such that data cost fee updates can be made once 
and update all execution layer pricing for all chains that post to any given 
data layer. Also, conversion rates are set independently, which updates pricing
for all routes that rely on them.

issue: #1951
  • Loading branch information
Zodomo authored Nov 29, 2024
1 parent a16e2fc commit 1e69efc
Show file tree
Hide file tree
Showing 11 changed files with 2,243 additions and 482 deletions.
25 changes: 23 additions & 2 deletions contracts/bindings/admin.go

Large diffs are not rendered by default.

1,239 changes: 1,061 additions & 178 deletions contracts/bindings/feeoraclev2.go

Large diffs are not rendered by default.

31 changes: 18 additions & 13 deletions contracts/core/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Admin_Test:test_pause_unpause() (gas: 30680773)
Admin_Test:test_pause_unpause_bridge() (gas: 25710460)
Admin_Test:test_pause_unpause_xcall() (gas: 30634703)
Admin_Test:test_pause_unpause_xsubmit() (gas: 30634454)
Admin_Test:test_upgrade() (gas: 34715948)
Admin_Test:test_pause_unpause() (gas: 32315662)
Admin_Test:test_pause_unpause_bridge() (gas: 27345415)
Admin_Test:test_pause_unpause_xcall() (gas: 32269636)
Admin_Test:test_pause_unpause_xsubmit() (gas: 32269343)
Admin_Test:test_upgrade() (gas: 36350837)
AllocPredeploys_Test:test_num_allocs() (gas: 1181319043)
AllocPredeploys_Test:test_predeploys() (gas: 1181300853)
AllocPredeploys_Test:test_preinstalls() (gas: 1182017269)
Expand All @@ -14,14 +14,19 @@ FeeOracleV1_Test:test_setGasPrice() (gas: 41034)
FeeOracleV1_Test:test_setManager() (gas: 45904)
FeeOracleV1_Test:test_setProtocolFee() (gas: 31610)
FeeOracleV1_Test:test_setToNativeRate() (gas: 41132)
FeeOracleV2_Test:test_bulkSetFeeParams() (gas: 119117)
FeeOracleV2_Test:test_feeFor() (gas: 103301)
FeeOracleV2_Test:test_setBaseGasLimit() (gas: 32009)
FeeOracleV2_Test:test_setDataGasPrice() (gas: 44216)
FeeOracleV2_Test:test_setExecGasPrice() (gas: 44247)
FeeOracleV2_Test:test_setManager() (gas: 45775)
FeeOracleV2_Test:test_setProtocolFee() (gas: 32226)
FeeOracleV2_Test:test_setToNativeRate() (gas: 43640)
FeeOracleV2_Test:test_bulkSetDataCostParams() (gas: 105995)
FeeOracleV2_Test:test_bulkSetExecFeeParams() (gas: 121898)
FeeOracleV2_Test:test_bulkSetToNativeRate() (gas: 73174)
FeeOracleV2_Test:test_feeFor() (gas: 234120)
FeeOracleV2_Test:test_setBaseBytes() (gas: 38952)
FeeOracleV2_Test:test_setBaseGasLimit() (gas: 38980)
FeeOracleV2_Test:test_setDataCostId() (gas: 57832)
FeeOracleV2_Test:test_setDataGasPrice() (gas: 43793)
FeeOracleV2_Test:test_setExecGasPrice() (gas: 43858)
FeeOracleV2_Test:test_setGasPerByte() (gas: 44065)
FeeOracleV2_Test:test_setManager() (gas: 45817)
FeeOracleV2_Test:test_setProtocolFee() (gas: 32367)
FeeOracleV2_Test:test_setToNativeRate() (gas: 42821)
InitializableHelper_Test:test_disableInitalizers() (gas: 181686)
InitializableHelper_Test:test_getInitialized() (gas: 178023)
OmniBridgeL1_Test:test_bridge() (gas: 228802)
Expand Down
17 changes: 17 additions & 0 deletions contracts/core/script/admin/Admin.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { InitializableHelper } from "script/utils/InitializableHelper.sol";
import { EIP1967Helper } from "script/utils/EIP1967Helper.sol";
import { OmniPortal } from "src/xchain/OmniPortal.sol";
import { FeeOracleV1 } from "src/xchain/FeeOracleV1.sol";
import { FeeOracleV2 } from "src/xchain/FeeOracleV2.sol";
import { PortalRegistry } from "src/xchain/PortalRegistry.sol";
import { OmniGasPump } from "src/token/OmniGasPump.sol";
import { OmniGasStation } from "src/token/OmniGasStation.sol";
Expand Down Expand Up @@ -197,6 +198,22 @@ contract Admin is Script {
// TODO: add post upgrade tests
}

/**
* @notice Upgrade a FeeOracleV2 contract.
* @param admin The address of the admin account, owner of the proxy admin
* @param deployer The address of the account that will deploy the new implementation.
* @param proxy The address of the proxy to upgrade.
*/
function upgradeFeeOracleV2(address admin, address deployer, address proxy, bytes calldata data) public {
vm.startBroadcast(deployer);
address impl = address(new FeeOracleV2());
vm.stopBroadcast();

_upgradeProxy(admin, proxy, impl, data);

// TODO: add post upgrade tests
}

/**
* @notice Upgrade an OmniGasPump contract.
* @param admin The address of the admin account, owner of the proxy admin
Expand Down
173 changes: 137 additions & 36 deletions contracts/core/src/interfaces/IFeeOracleV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,182 @@ import { IConversionRateOracle } from "./IConversionRateOracle.sol";
* @notice Extends IFeeOracle with FeeOracleV2 methods
*/
interface IFeeOracleV2 is IFeeOracle, IConversionRateOracle {
/// @notice Emitted when fee parameters for a chain are set.
event FeeParamsSet(uint64 chainId, uint64 execGasPrice, uint64 dataGasPrice, uint64 toNativeRate);
/// @notice Thrown when the caller is not the manager.
error NotManager();

/// @notice Emitted when the base gas limit is set.
event BaseGasLimitSet(uint24 baseGasLimit);
/// @notice Thrown when there are no fee parameters for a chain or data cost ID.
error NoFeeParams();

/// @notice Emitted when the base protocol fee is set.
event ProtocolFeeSet(uint72 protocolFee);
/// @notice Thrown when the address is zero.
error ZeroAddress();

/// @notice Thrown when the chain ID is zero.
error ZeroChainId();

/// @notice Thrown when the gas price is zero.
error ZeroGasPrice();

/// @notice Thrown when the gas token is zero.
error ZeroGasToken();

/// @notice Thrown when the data cost ID is zero.
error ZeroDataCostId();

/// @notice Thrown when the gas per byte is zero.
error ZeroGasPerByte();

/// @notice Thrown when the native rate is zero.
error ZeroNativeRate();

/// @notice Emitted when fee parameters for a chain are set.
event FeeParamsSet(uint16 gasToken, uint32 baseGasLimit, uint64 chainId, uint64 gasPrice, uint64 dataCostId);

/// @notice Emitted when data cost parameters for a data cost ID are set.
event DataCostParamsSet(uint16 gasToken, uint32 baseBytes, uint64 id, uint64 gasPrice, uint64 gasPerByte);

/// @notice Emitted when the gas price for a destination chain is set.
event ExecGasPriceSet(uint64 chainId, uint64 gasPrice);

/// @notice Emitted when the data gas price for a destination chain is set.
event DataGasPriceSet(uint64 chainId, uint64 gasPrice);
/// @notice Emitted when the data gas price for a data cost ID is set.
event DataGasPriceSet(uint64 dataCostId, uint64 gasPrice);

/// @notice Emitted when the base gas limit for a destination chain is set.
event BaseGasLimitSet(uint64 chainId, uint32 baseGasLimit);

/// @notice Emitted when the to-native conversion rate for a destination chain is set.
event ToNativeRateSet(uint64 chainId, uint64 toNativeRate);
/// @notice Emitted when the base bytes buffer for a data cost ID is set.
event BaseBytesSet(uint64 dataCostId, uint32 baseBytes);

/// @notice Emitted when the data cost ID for a destination chain is set.
event DataCostIdSet(uint64 chainId, uint64 dataCostId);

/// @notice Emitted when the gas per byte for a data cost ID is set.
event GasPerByteSet(uint64 dataCostId, uint64 gasPerByte);

/// @notice Emitted when the to-native conversion rate for a gas token is set.
event ToNativeRateSet(uint16 gasToken, uint256 nativeRate);

/// @notice Emitted when the base protocol fee is set.
event ProtocolFeeSet(uint96 protocolFee);

/// @notice Emitted when the manager is changed.
event ManagerSet(address manager);

/**
* @notice Fee parameters for a specific chain.
* @custom:field gasToken The gas token ID.
* @custom:field baseGasLimit The base gas limit for that chain.
* @custom:field chainId The chain ID.
* @custom:field execGasPrice The execution gas price on that chain (denominated in chains native token).
* @custom:field dataGasPrice The data gas price on that chain (denominated in chains native token).
* ex. for Optimism, dataGasPrice is Ethereum L1's blob gas price.
* @custom:field toNativeRate The conversion rate from the chains native token to this chain's
* native token. Rate is numerator over CONVERSION_RATE_DENOM.
* @custom:field gasPrice The execution gas price on that chain (denominated in chains native token).
* @custom:field dataCostId The data cost ID for that chain.
*/
struct FeeParams {
uint16 gasToken;
uint32 baseGasLimit;
uint64 chainId;
uint64 execGasPrice;
uint64 dataGasPrice;
uint64 toNativeRate;
uint64 gasPrice;
uint64 dataCostId;
}

/**
* @notice Data cost parameters for a data cost ID.
* @custom:field gasToken The gas token ID.
* @custom:field baseBytes The base bytes buffer in bytes for that data cost ID.
* @custom:field id The data cost ID.
* @custom:field gasPrice The data gas price for that data cost ID (denominated in chains native token).
* @custom:field gasPerByte The gas per byte for that data cost ID.
*/
struct DataCostParams {
uint16 gasToken;
uint32 baseBytes;
uint64 id;
uint64 gasPrice;
uint64 gasPerByte;
}

/**
* @notice Parameters for a gas token's to-native conversion rate.
* @custom:field gasToken The gas token ID.
* @custom:field nativeRate The to-native conversion rate for that gas token.
*/
struct ToNativeRateParams {
uint16 gasToken;
uint256 nativeRate;
}

/// @notice Returns the protocol fee.
function protocolFee() external view returns (uint96);

/// @notice Returns the manager's address.
function manager() external view returns (address);

/// @notice Returns the conversion rate from `gasToken` to this chain's native token (normalized by CONVERSION_RATE_DENOM)
function tokenToNativeRate(uint16 gasToken) external view returns (uint256);

/// @notice Returns the fee parameters for a destination chain.
function feeParams(uint64 chainId) external view returns (FeeParams memory);

/// @notice Returns the data cost parameters for a data cost ID.
function dataCostParams(uint64 dataCostId) external view returns (DataCostParams memory);

/// @notice Returns the execution gas price for a destination chain.
function execGasPrice(uint64 chainId) external view returns (uint64);

/// @notice Returns the data gas price for a destination chain.
function dataGasPrice(uint64 chainId) external view returns (uint64);
/// @notice Returns the data gas price for a data cost ID.
function dataGasPrice(uint64 dataCostId) external view returns (uint64);

/// @notice Returns the to-native conversion rate for a destination chain.
function toNativeRate(uint64 chainId) external view returns (uint256);
/// @notice Returns the base gas limit for a destination chain.
function baseGasLimit(uint64 chainId) external view returns (uint32);

/// @notice Returns the manager's address.
function manager() external view returns (address);
/// @notice Returns the base bytes buffer for a data cost ID.
function baseBytes(uint64 dataCostId) external view returns (uint32);

/// @notice Returns the protocol fee.
function protocolFee() external view returns (uint72);
/// @notice Returns the gas token for a destination chain.
function execGasToken(uint64 chainId) external view returns (uint16);

/// @notice Returns the gas token for a data cost ID.
function dataGasToken(uint64 dataCostId) external view returns (uint16);

/// @notice Returns the data cost ID for a destination chain.
function execDataCostId(uint64 chainId) external view returns (uint64);

/// @notice Returns the base gas limit.
function baseGasLimit() external view returns (uint24);
/// @notice Returns the gas per byte for a data cost ID.
function dataGasPerByte(uint64 dataCostId) external view returns (uint64);

/// @notice Returns the to-native conversion rate for a destination chain.
function toNativeRate(uint64 chainId) external view returns (uint256);

/// @notice Set the fee parameters for a list of destination chains.
function bulkSetFeeParams(FeeParams[] calldata params) external;

/// @notice Set the data cost parameters for a list of data cost IDs.
function bulkSetDataCostParams(DataCostParams[] calldata params) external;

/// @notice Set the to-native conversion rate for a list of gas tokens.
function bulkSetToNativeRate(ToNativeRateParams[] calldata params) external;

/// @notice Set the execution gas price for a destination chain.
function setExecGasPrice(uint64 chainId, uint64 execGasPrice) external;
function setExecGasPrice(uint64 chainId, uint64 gasPrice) external;

/// @notice Set the data gas price for a data cost ID.
function setDataGasPrice(uint64 dataCostId, uint64 gasPrice) external;

/// @notice Set the base gas limit for a destination chain.
function setBaseGasLimit(uint64 chainId, uint32 newBaseGasLimit) external;

/// @notice Set the base bytes buffer for a data cost ID.
function setBaseBytes(uint64 dataCostId, uint32 newBaseBytes) external;

/// @notice Set the data gas price for a destination chain.
function setDataGasPrice(uint64 chainId, uint64 dataGasPrice) external;
/// @notice Set the data cost ID for a destination chain.
function setDataCostId(uint64 chainId, uint64 dataCostId) external;

/// @notice Set the to native conversion rate for a destination chain.
function setToNativeRate(uint64 chainId, uint64 toNativeRate) external;
/// @notice Set the gas per byte for a data cost ID.
function setGasPerByte(uint64 dataCostId, uint64 gasPerByte) external;

/// @notice Set the base gas limit for each xmsg.
function setBaseGasLimit(uint24 gasLimit) external;
/// @notice Set the to native conversion rate for a gas token.
function setToNativeRate(uint16 gasToken, uint256 nativeRate) external;

/// @notice Set the base protocol fee for each xmsg.
function setProtocolFee(uint72 fee) external;
function setProtocolFee(uint96 fee) external;

/// @notice Set the manager admin account.
function setManager(address manager) external;
Expand Down
Loading

0 comments on commit 1e69efc

Please sign in to comment.