From d8fcd6cf6165e15993a5046d90ed5ec892e67a45 Mon Sep 17 00:00:00 2001 From: Mike-CZ Date: Thu, 10 Oct 2024 12:22:09 +0200 Subject: [PATCH] Add custom errors for `ConstantsManager` --- contracts/sfc/ConstantsManager.sol | 122 +++++++++++++++++++++------- contracts/sfc/GasPriceConstants.sol | 2 +- 2 files changed, 94 insertions(+), 30 deletions(-) diff --git a/contracts/sfc/ConstantsManager.sol b/contracts/sfc/ConstantsManager.sol index 7a7b8d3..b5283ea 100644 --- a/contracts/sfc/ConstantsManager.sol +++ b/contracts/sfc/ConstantsManager.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; -import "../ownership/Ownable.sol"; -import "../common/Decimal.sol"; +import {Ownable} from "../ownership/Ownable.sol"; +import {Decimal} from "../common/Decimal.sol"; contract ConstantsManager is Ownable { // Minimum amount of stake for a validator, i.e., 500000 FTM @@ -32,94 +32,158 @@ contract ConstantsManager is Ownable { uint256 public targetGasPowerPerSecond; uint256 public gasPriceBalancingCounterweight; + /** + * @dev Given value is too small + */ + error ValueTooSmall(); + + /** + * @dev Given value is too large + */ + error ValueTooLarge(); + function initialize() external initializer { Ownable.initialize(msg.sender); } function updateMinSelfStake(uint256 v) external virtual onlyOwner { - require(v >= 100000 * 1e18, "too small value"); - require(v <= 10000000 * 1e18, "too large value"); + if (v < 100000 * 1e18) { + revert ValueTooSmall(); + } + if (v > 10000000 * 1e18) { + revert ValueTooLarge(); + } minSelfStake = v; } function updateMaxDelegatedRatio(uint256 v) external virtual onlyOwner { - require(v >= Decimal.unit(), "too small value"); - require(v <= 31 * Decimal.unit(), "too large value"); + if (v < Decimal.unit()) { + revert ValueTooSmall(); + } + if (v > 31 * Decimal.unit()) { + revert ValueTooLarge(); + } maxDelegatedRatio = v; } function updateValidatorCommission(uint256 v) external virtual onlyOwner { - require(v <= Decimal.unit() / 2, "too large value"); + if (v > Decimal.unit() / 2) { + revert ValueTooLarge(); + } validatorCommission = v; } function updateBurntFeeShare(uint256 v) external virtual onlyOwner { - require(v <= Decimal.unit() / 2, "too large value"); + if (v > Decimal.unit() / 2) { + revert ValueTooLarge(); + } burntFeeShare = v; } function updateTreasuryFeeShare(uint256 v) external virtual onlyOwner { - require(v <= Decimal.unit() / 2, "too large value"); + if (v > Decimal.unit() / 2) { + revert ValueTooLarge(); + } treasuryFeeShare = v; } function updateUnlockedRewardRatio(uint256 v) external virtual onlyOwner { - require(v >= (5 * Decimal.unit()) / 100, "too small value"); - require(v <= Decimal.unit() / 2, "too large value"); + if (v < (5 * Decimal.unit()) / 100) { + revert ValueTooSmall(); + } + if (v > Decimal.unit() / 2) { + revert ValueTooLarge(); + } unlockedRewardRatio = v; } function updateMinLockupDuration(uint256 v) external virtual onlyOwner { - require(v >= 86400, "too small value"); - require(v <= 86400 * 30, "too large value"); + if (v < 86400) { + revert ValueTooSmall(); + } + if (v > 86400 * 30) { + revert ValueTooLarge(); + } minLockupDuration = v; } function updateMaxLockupDuration(uint256 v) external virtual onlyOwner { - require(v >= 86400 * 30, "too small value"); - require(v <= 86400 * 1460, "too large value"); + if (v < 86400 * 30) { + revert ValueTooSmall(); + } + if (v > 86400 * 1460) { + revert ValueTooLarge(); + } maxLockupDuration = v; } function updateWithdrawalPeriodEpochs(uint256 v) external virtual onlyOwner { - require(v >= 2, "too small value"); - require(v <= 100, "too large value"); + if (v < 2) { + revert ValueTooSmall(); + } + if (v > 100) { + revert ValueTooLarge(); + } withdrawalPeriodEpochs = v; } function updateWithdrawalPeriodTime(uint256 v) external virtual onlyOwner { - require(v >= 86400, "too small value"); - require(v <= 30 * 86400, "too large value"); + if (v < 86400) { + revert ValueTooSmall(); + } + if (v > 30 * 86400) { + revert ValueTooLarge(); + } withdrawalPeriodTime = v; } function updateBaseRewardPerSecond(uint256 v) external virtual onlyOwner { - require(v >= 0.5 * 1e18, "too small value"); - require(v <= 32 * 1e18, "too large value"); + if (v < 0.5 * 1e18) { + revert ValueTooSmall(); + } + if (v > 32 * 1e18) { + revert ValueTooLarge(); + } baseRewardPerSecond = v; } function updateOfflinePenaltyThresholdTime(uint256 v) external virtual onlyOwner { - require(v >= 86400, "too small value"); - require(v <= 10 * 86400, "too large value"); + if (v < 86400) { + revert ValueTooSmall(); + } + if (v > 10 * 86400) { + revert ValueTooLarge(); + } offlinePenaltyThresholdTime = v; } function updateOfflinePenaltyThresholdBlocksNum(uint256 v) external virtual onlyOwner { - require(v >= 100, "too small value"); - require(v <= 1000000, "too large value"); + if (v < 100) { + revert ValueTooSmall(); + } + if (v > 1000000) { + revert ValueTooLarge(); + } offlinePenaltyThresholdBlocksNum = v; } function updateTargetGasPowerPerSecond(uint256 v) external virtual onlyOwner { - require(v >= 1000000, "too small value"); - require(v <= 500000000, "too large value"); + if (v < 1000000) { + revert ValueTooSmall(); + } + if (v > 500000000) { + revert ValueTooLarge(); + } targetGasPowerPerSecond = v; } function updateGasPriceBalancingCounterweight(uint256 v) external virtual onlyOwner { - require(v >= 100, "too small value"); - require(v <= 10 * 86400, "too large value"); + if (v < 100) { + revert ValueTooSmall(); + } + if (v > 10 * 86400) { + revert ValueTooLarge(); + } gasPriceBalancingCounterweight = v; } } diff --git a/contracts/sfc/GasPriceConstants.sol b/contracts/sfc/GasPriceConstants.sol index 96d5931..2bc7be3 100644 --- a/contracts/sfc/GasPriceConstants.sol +++ b/contracts/sfc/GasPriceConstants.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; -import "../common/Decimal.sol"; +import {Decimal} from "../common/Decimal.sol"; library GP { function trimGasPriceChangeRatio(uint256 x) internal pure returns (uint256) {