From 23eac56ea00f7ca98228ac74bb73d2fcc220cabc Mon Sep 17 00:00:00 2001 From: Zach Kolodny Date: Thu, 16 May 2024 20:32:56 -0400 Subject: [PATCH] added custom errors to diamond proxy --- .../contracts/common/L1ContractErrors.sol | 2 ++ .../chain-deps/DiamondProxy.sol | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/l1-contracts/contracts/common/L1ContractErrors.sol b/l1-contracts/contracts/common/L1ContractErrors.sol index c897c040b..023f08d85 100644 --- a/l1-contracts/contracts/common/L1ContractErrors.sol +++ b/l1-contracts/contracts/common/L1ContractErrors.sol @@ -43,6 +43,8 @@ error HashMismatch(bytes32 expected, bytes32 actual); error HyperchainLimitReached(); error TimeNotReached(); error TooMuchGas(); +error MalformedCalldata(); +error FacetIsFrozen(bytes4 func); enum SharedBridgeKey { PostUpgradeFirstBatch, diff --git a/l1-contracts/contracts/state-transition/chain-deps/DiamondProxy.sol b/l1-contracts/contracts/state-transition/chain-deps/DiamondProxy.sol index 5cf26ac82..1671b602b 100644 --- a/l1-contracts/contracts/state-transition/chain-deps/DiamondProxy.sol +++ b/l1-contracts/contracts/state-transition/chain-deps/DiamondProxy.sol @@ -3,6 +3,7 @@ pragma solidity 0.8.24; import {Diamond} from "../libraries/Diamond.sol"; +import {ValueMismatch, MalformedCalldata, InvalidSelector, FacetIsFrozen} from "../../common/L1ContractErrors.sol"; /// @title Diamond Proxy Contract (EIP-2535) /// @author Matter Labs @@ -11,7 +12,9 @@ contract DiamondProxy { constructor(uint256 _chainId, Diamond.DiamondCutData memory _diamondCut) { // Check that the contract is deployed on the expected chain. // Thus, the contract deployed by the same Create2 factory on the different chain will have different addresses! - require(_chainId == block.chainid, "pr"); + if (_chainId != block.chainid) { + revert ValueMismatch(block.chainid, _chainId); + } Diamond.diamondCut(_diamondCut); } @@ -22,13 +25,21 @@ contract DiamondProxy { // Check whether the data contains a "full" selector or it is empty. // Required because Diamond proxy finds a facet by function signature, // which is not defined for data length in range [1, 3]. - require(msg.data.length >= 4 || msg.data.length == 0, "Ut"); + if (msg.data.length < 4 && msg.data.length != 0) { + revert MalformedCalldata(); + } // Get facet from function selector Diamond.SelectorToFacet memory facet = diamondStorage.selectorToFacet[msg.sig]; address facetAddress = facet.facetAddress; - require(facetAddress != address(0), "F"); // Proxy has no facet for this selector - require(!diamondStorage.isFrozen || !facet.isFreezable, "q1"); // Facet is frozen + // Proxy has no facet for this selector + if (facetAddress == address(0)) { + revert InvalidSelector(msg.sig); + } + // Facet is frozen + if (diamondStorage.isFrozen && facet.isFreezable) { + revert FacetIsFrozen(msg.sig); + } assembly { // The pointer to the free memory slot