Skip to content

Commit

Permalink
admin facet
Browse files Browse the repository at this point in the history
  • Loading branch information
koloz193 committed May 17, 2024
1 parent 353cc02 commit 35809ef
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
4 changes: 1 addition & 3 deletions l1-contracts/contracts/bridge/L1SharedBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import {
NoFundsTransferred,
ZeroBalance,
ValueMismatch,
RevertWithMsg,
ERR_RECEIVE_ETH_BRIDGE,
NonEmptyMsgValue,
L2BridgeNotDeployed,
TokenNotSupported,
Expand Down Expand Up @@ -230,7 +228,7 @@ contract L1SharedBridge is IL1SharedBridge, ReentrancyGuard, Ownable2StepUpgrade

function receiveEth(uint256 _chainId) external payable {
if (BRIDGE_HUB.getHyperchain(_chainId) != msg.sender) {
revert RevertWithMsg(ERR_RECEIVE_ETH_BRIDGE);
revert Unauthorized(msg.sender);
}
}

Expand Down
9 changes: 6 additions & 3 deletions l1-contracts/contracts/common/L1ContractErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ error ZeroAddress();
error SharedBridgeValueAlreadySet(SharedBridgeKey);
error NoFundsTransferred();
error ZeroBalance();
error RevertWithMsg(string message);
error NonEmptyMsgValue();
error L2BridgeNotDeployed(uint256 chainId);
error TokenNotSupported(address token);
Expand Down Expand Up @@ -45,6 +44,12 @@ error TimeNotReached();
error TooMuchGas();
error MalformedCalldata();
error FacetIsFrozen(bytes4 func);
error PubdataBatchIsLessThanTxn();
error InvalidPubdataPricingMode();
error InvalidValue();
error ChainAlreadyLive();
error InvalidProtocolVersion();
error DiamondFreezeIncorrectState();

enum SharedBridgeKey {
PostUpgradeFirstBatch,
Expand All @@ -59,5 +64,3 @@ enum BytecodeError {
Length,
WordsMustBeOdd
}

string constant ERR_RECEIVE_ETH_BRIDGE = "receiveEth not state transition";
67 changes: 52 additions & 15 deletions l1-contracts/contracts/state-transition/chain-deps/facets/Admin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import {MAX_GAS_PER_TRANSACTION} from "../../../common/Config.sol";
import {FeeParams, PubdataPricingMode} from "../ZkSyncHyperchainStorage.sol";
import {ZkSyncHyperchainBase} from "./ZkSyncHyperchainBase.sol";
import {IStateTransitionManager} from "../../IStateTransitionManager.sol";
import {
Unauthorized,
TooMuchGas,
PubdataBatchIsLessThanTxn,
InvalidPubdataPricingMode,
InvalidValue,
ChainAlreadyLive,
HashMismatch,
ValueMismatch,
InvalidProtocolVersion,
DiamondFreezeIncorrectState
} from "../../../common/L1ContractErrors.sol";

// While formally the following import is not used, it is needed to inherit documentation from it
import {IZkSyncHyperchainBase} from "../../chain-interfaces/IZkSyncHyperchainBase.sol";
Expand All @@ -31,7 +43,10 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {
/// @inheritdoc IAdmin
function acceptAdmin() external {
address pendingAdmin = s.pendingAdmin;
require(msg.sender == pendingAdmin, "n4"); // Only proposed by current admin address can claim the admin rights
// Only proposed by current admin address can claim the admin rights
if (msg.sender != pendingAdmin) {
revert Unauthorized(msg.sender);
}

address previousAdmin = s.admin;
s.admin = pendingAdmin;
Expand All @@ -56,7 +71,9 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {

/// @inheritdoc IAdmin
function setPriorityTxMaxGasLimit(uint256 _newPriorityTxMaxGasLimit) external onlyStateTransitionManager {
require(_newPriorityTxMaxGasLimit <= MAX_GAS_PER_TRANSACTION, "n5");
if (_newPriorityTxMaxGasLimit > MAX_GAS_PER_TRANSACTION) {
revert TooMuchGas();
}

uint256 oldPriorityTxMaxGasLimit = s.priorityTxMaxGasLimit;
s.priorityTxMaxGasLimit = _newPriorityTxMaxGasLimit;
Expand All @@ -67,11 +84,16 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {
function changeFeeParams(FeeParams calldata _newFeeParams) external onlyAdminOrStateTransitionManager {
// Double checking that the new fee params are valid, i.e.
// the maximal pubdata per batch is not less than the maximal pubdata per priority transaction.
require(_newFeeParams.maxPubdataPerBatch >= _newFeeParams.priorityTxMaxPubdata, "n6");
if (_newFeeParams.maxPubdataPerBatch < _newFeeParams.priorityTxMaxPubdata) {
revert PubdataBatchIsLessThanTxn();
}

FeeParams memory oldFeeParams = s.feeParams;

require(_newFeeParams.pubdataPricingMode == oldFeeParams.pubdataPricingMode, "n7"); // we cannot change pubdata pricing mode
// we cannot change pubdata pricing mode
if (_newFeeParams.pubdataPricingMode != oldFeeParams.pubdataPricingMode) {
revert InvalidPubdataPricingMode();
}

s.feeParams = _newFeeParams;

Expand All @@ -80,7 +102,9 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {

/// @inheritdoc IAdmin
function setTokenMultiplier(uint128 _nominator, uint128 _denominator) external onlyAdminOrStateTransitionManager {
require(_denominator != 0, "AF: denominator 0");
if (_denominator == 0) {
revert InvalidValue();
}
uint128 oldNominator = s.baseTokenGasPriceMultiplierNominator;
uint128 oldDenominator = s.baseTokenGasPriceMultiplierDenominator;

Expand All @@ -92,7 +116,10 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {

/// @inheritdoc IAdmin
function setPubdataPricingMode(PubdataPricingMode _pricingMode) external onlyAdmin {
require(s.totalBatchesCommitted == 0, "AdminFacet: set validium only after genesis"); // Validium mode can be set only before the first batch is processed
// Validium mode can be set only before the first batch is processed
if (s.totalBatchesCommitted != 0) {
revert ChainAlreadyLive();
}
s.feeParams.pubdataPricingMode = _pricingMode;
emit ValidiumModeStatusUpdate(_pricingMode);
}
Expand All @@ -113,15 +140,19 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {
Diamond.DiamondCutData calldata _diamondCut
) external onlyAdminOrStateTransitionManager {
bytes32 cutHashInput = keccak256(abi.encode(_diamondCut));
require(
cutHashInput == IStateTransitionManager(s.stateTransitionManager).upgradeCutHash(_oldProtocolVersion),
"AdminFacet: cutHash mismatch"
);

require(s.protocolVersion == _oldProtocolVersion, "AdminFacet: protocolVersion mismatch in STC when upgrading");
bytes32 upgradeCutHash = IStateTransitionManager(s.stateTransitionManager).upgradeCutHash(_oldProtocolVersion);
if (cutHashInput != upgradeCutHash) {
revert HashMismatch(upgradeCutHash, cutHashInput);
}

if (s.protocolVersion != _oldProtocolVersion) {
revert ValueMismatch(s.protocolVersion, _oldProtocolVersion);
}
Diamond.diamondCut(_diamondCut);
emit ExecuteUpgrade(_diamondCut);
require(s.protocolVersion > _oldProtocolVersion, "AdminFacet: protocolVersion mismatch in STC after upgrading");
if (s.protocolVersion <= _oldProtocolVersion) {
revert InvalidProtocolVersion();
}
}

/// @inheritdoc IAdmin
Expand All @@ -138,7 +169,10 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {
function freezeDiamond() external onlyStateTransitionManager {
Diamond.DiamondStorage storage diamondStorage = Diamond.getDiamondStorage();

require(!diamondStorage.isFrozen, "a9"); // diamond proxy is frozen already
// diamond proxy is frozen already
if (diamondStorage.isFrozen) {
revert DiamondFreezeIncorrectState();
}
diamondStorage.isFrozen = true;

emit Freeze();
Expand All @@ -148,7 +182,10 @@ contract AdminFacet is ZkSyncHyperchainBase, IAdmin {
function unfreezeDiamond() external onlyStateTransitionManager {
Diamond.DiamondStorage storage diamondStorage = Diamond.getDiamondStorage();

require(diamondStorage.isFrozen, "a7"); // diamond proxy is not frozen
// diamond proxy is not frozen
if (!diamondStorage.isFrozen) {
revert DiamondFreezeIncorrectState();
}
diamondStorage.isFrozen = false;

emit Unfreeze();
Expand Down

0 comments on commit 35809ef

Please sign in to comment.