Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom errors for common and ownership packages #73

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion contracts/common/Initializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ contract Initializable {
*/
bool private initializing;

/**
* @dev The contract instance has already been initialized.
*/
error ContractInitialized();

/**
* @dev Modifier to use in the initializer function of a contract.
*/
modifier initializer() {
require(initializing || !initialized, "Contract instance has already been initialized");
if (!initializing && initialized) {
revert ContractInitialized();
}

bool isTopLevelCall = !initializing;
if (isTopLevelCall) {
Expand Down
11 changes: 9 additions & 2 deletions contracts/common/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "./Initializable.sol";
import {Initializable} from "./Initializable.sol";

/**
* @dev Contract module that helps prevent reentrant calls to a function.
Expand All @@ -19,6 +19,11 @@ contract ReentrancyGuard is Initializable {
// counter to allow mutex lock with only one SSTORE operation
uint256 private _guardCounter;

/**
* @dev Reentrant call.
*/
error ReentrantCall();

function initialize() internal initializer {
// The counter starts at one to prevent changing it from zero to a non-zero
// value, which is a more expensive operation.
Expand All @@ -36,7 +41,9 @@ contract ReentrancyGuard is Initializable {
_guardCounter += 1;
uint256 localCounter = _guardCounter;
_;
require(localCounter == _guardCounter, "ReentrancyGuard: reentrant call");
if (localCounter != _guardCounter) {
revert ReentrantCall();
}
}

uint256[50] private ______gap;
Expand Down
21 changes: 17 additions & 4 deletions contracts/ownership/Ownable.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "../common/Initializable.sol";

import {Initializable} from "../common/Initializable.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
Expand All @@ -15,6 +14,16 @@ import "../common/Initializable.sol";
contract Ownable is Initializable {
address private _owner;

/**
* @dev The caller is not the owner.
*/
error NotOwner();

/**
* @dev Given zero address.
*/
error ZeroAddress();
Copy link
Collaborator

@thaarok thaarok Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to be compatible with OpenZeppelin's Ownable:

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

(it is ok if an error/method name is long - only the hash is written into the contract code)
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol#L23-L32


event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/**
Expand All @@ -36,7 +45,9 @@ contract Ownable is Initializable {
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(isOwner(), "Ownable: caller is not the owner");
if (!isOwner()) {
revert NotOwner();
}
_;
}

Expand Down Expand Up @@ -71,7 +82,9 @@ contract Ownable is Initializable {
* @dev Transfers ownership of the contract to a new account (`newOwner`).
*/
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0), "Ownable: new owner is the zero address");
if (newOwner == address(0)) {
revert ZeroAddress();
}
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
Expand Down
9 changes: 0 additions & 9 deletions contracts/sfc/ConstantsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,10 @@ contract ConstantsManager is Ownable {
uint256 public targetGasPowerPerSecond;
uint256 public gasPriceBalancingCounterweight;

address private secondaryOwner_erased;

// event SecondaryOwnershipTransferred(address indexed previousOwner, address indexed newOwner);

function initialize() external initializer {
Ownable.initialize(msg.sender);
}

// function setSecondaryOwner(address v) onlyOwner external {
// emit SecondaryOwnershipTransferred(secondaryOwner, v);
// secondaryOwner = v;
// }

function updateMinSelfStake(uint256 v) external virtual onlyOwner {
require(v >= 100000 * 1e18, "too small value");
require(v <= 10000000 * 1e18, "too large value");
Expand Down
1 change: 0 additions & 1 deletion contracts/sfc/NodeDriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ contract NodeDriverAuth is Initializable, Ownable {
}

contract NodeDriver is Initializable {
uint256 private erased0;
NodeDriverAuth internal backend;
EVMWriter internal evmWriter;

Expand Down
8 changes: 0 additions & 8 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ contract SFC is SFCBase, Version {
getEpochSnapshot[sealedEpoch].endTime = _now();
}

function updateStakeTokenizerAddress(address addr) external onlyOwner {
stakeTokenizerAddress = addr;
}

function updateLibAddress(address v) external onlyOwner {
libAddress = v;
}
Expand All @@ -126,10 +122,6 @@ contract SFC is SFCBase, Version {
voteBookAddress = v;
}

function updateSFTMFinalizer(address v) external onlyOwner {
sftmFinalizer = v;
}

function migrateValidatorPubkeyUniquenessFlag(uint256 start, uint256 end) external {
for (uint256 vid = start; vid < end; vid++) {
bytes memory pubkey = getValidatorPubkey[vid];
Expand Down
8 changes: 0 additions & 8 deletions contracts/sfc/SFCI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ interface SFCI {

function slashingRefundRatio(uint256) external view returns (uint256);

function stakeTokenizerAddress() external view returns (address);

function stashedRewardsUntilEpoch(address, uint256) external view returns (uint256);

function totalActiveStake() external view returns (uint256);
Expand Down Expand Up @@ -171,8 +169,6 @@ interface SFCI {

function updateSlashingRefundRatio(uint256 validatorID, uint256 refundRatio) external;

function updateStakeTokenizerAddress(address addr) external;

function updateTreasuryAddress(address v) external;

function burnFTM(uint256 amount) external;
Expand Down Expand Up @@ -233,10 +229,6 @@ interface SFCI {

function voteBookAddress() external view returns (address);

function liquidateSFTM(address delegator, uint256 toValidatorID, uint256 amount) external;

function updateSFTMFinalizer(address v) external;

function updateValidatorPubkey(bytes calldata pubkey) external;

function migrateValidatorPubkeyUniquenessFlag(uint256 start, uint256 end) external;
Expand Down
43 changes: 0 additions & 43 deletions contracts/sfc/SFCLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.9;
import "../common/Decimal.sol";
import "./GasPriceConstants.sol";
import "./SFCBase.sol";
import "./StakeTokenizer.sol";
import "./NodeDriver.sol";

contract SFCLib is SFCBase {
Expand Down Expand Up @@ -229,7 +228,6 @@ contract SFCLib is SFCBase {

require(amount > 0, "zero amount");
require(amount <= getUnlockedStake(delegator, toValidatorID), "not enough unlocked stake");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");

require(getWithdrawalRequest[delegator][toValidatorID][wrID].amount == 0, "wrID already exists");

Expand All @@ -244,37 +242,6 @@ contract SFCLib is SFCBase {
emit Undelegated(delegator, toValidatorID, wrID, amount);
}

// liquidateSFTM is used for finalization of last fMint positions with outstanding sFTM balances
// it allows to undelegate without the unboding period, and also to unlock stake without a penalty.
// Such a simplification, which might be dangerous generally, is okay here because there's only a small amount
// of leftover sFTM
function liquidateSFTM(address delegator, uint256 toValidatorID, uint256 amount) external {
require(msg.sender == sftmFinalizer, "not sFTM finalizer");
_stashRewards(delegator, toValidatorID);

require(amount > 0, "zero amount");
StakeTokenizer(stakeTokenizerAddress).redeemSFTMFor(msg.sender, delegator, toValidatorID, amount);
require(amount <= getStake[delegator][toValidatorID], "not enough stake");
uint256 unlockedStake = getUnlockedStake(delegator, toValidatorID);
if (amount > unlockedStake) {
LockedDelegation storage ld = getLockupInfo[delegator][toValidatorID];
ld.lockedStake = ld.lockedStake - amount - unlockedStake;
emit UnlockedStake(delegator, toValidatorID, amount - unlockedStake, 0);
}

_rawUndelegate(delegator, toValidatorID, amount, false, true, false);

_syncValidator(toValidatorID, false);

emit Undelegated(delegator, toValidatorID, 0xffffffffff, amount);

// It's important that we transfer after erasing (protection against Re-Entrancy)
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send FTM");

emit Withdrawn(delegator, toValidatorID, 0xffffffffff, amount);
}

function isSlashed(uint256 validatorID) public view returns (bool) {
return getValidator[validatorID].status & CHEATER_MASK != 0;
}
Expand All @@ -298,7 +265,6 @@ contract SFCLib is SFCBase {
function _withdraw(address delegator, uint256 toValidatorID, uint256 wrID, address payable receiver) private {
WithdrawalRequest memory request = getWithdrawalRequest[delegator][toValidatorID][wrID];
require(request.epoch != 0, "request doesn't exist");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");

uint256 requestTime = request.time;
uint256 requestEpoch = request.epoch;
Expand Down Expand Up @@ -455,7 +421,6 @@ contract SFCLib is SFCBase {
}

function _claimRewards(address delegator, uint256 toValidatorID) internal returns (Rewards memory rewards) {
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");
_stashRewards(delegator, toValidatorID);
rewards = _rewardsStash[delegator][toValidatorID];
uint256 totalReward = rewards.unlockedReward + rewards.lockupBaseReward + rewards.lockupExtraReward;
Expand Down Expand Up @@ -522,13 +487,6 @@ contract SFCLib is SFCBase {
epochEndTime(epoch) <= getLockupInfo[delegator][toValidatorID].endTime;
}

function _checkAllowedToWithdraw(address delegator, uint256 toValidatorID) internal view returns (bool) {
if (stakeTokenizerAddress == address(0)) {
return true;
}
return StakeTokenizer(stakeTokenizerAddress).allowedToWithdrawStake(delegator, toValidatorID);
}

function getUnlockedStake(address delegator, uint256 toValidatorID) public view returns (uint256) {
if (!isLockedUp(delegator, toValidatorID)) {
return getStake[delegator][toValidatorID];
Expand Down Expand Up @@ -653,7 +611,6 @@ contract SFCLib is SFCBase {
require(amount > 0, "zero amount");
require(isLockedUp(delegator, toValidatorID), "not locked up");
require(amount <= ld.lockedStake, "not enough locked stake");
require(_checkAllowedToWithdraw(delegator, toValidatorID), "outstanding sFTM balance");
require(!_redirected(delegator), "redirected");

_stashRewards(delegator, toValidatorID);
Expand Down
10 changes: 0 additions & 10 deletions contracts/sfc/SFCState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,11 @@ contract SFCState is Initializable, Ownable {
uint256 totalSupply;
}

uint256 private erased0;
uint256 public totalSupply;
mapping(uint256 => EpochSnapshot) public getEpochSnapshot;

uint256 private erased1;
uint256 private erased2;

mapping(uint256 => uint256) public slashingRefundRatio; // validator ID -> (slashing refund ratio)

address public stakeTokenizerAddress;

uint256 private erased3;
uint256 private erased4;
uint256 public minGasPrice;

address public treasuryAddress;
Expand All @@ -102,8 +94,6 @@ contract SFCState is Initializable, Ownable {

address public voteBookAddress;

address internal sftmFinalizer;

struct Penalty {
uint256 amount;
uint256 end;
Expand Down
61 changes: 0 additions & 61 deletions contracts/sfc/StakeTokenizer.sol

This file was deleted.

4 changes: 0 additions & 4 deletions contracts/test/UnitTestSFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ interface SFCUnitTestI {

function slashingRefundRatio(uint256) external view returns (uint256);

function stakeTokenizerAddress() external view returns (address);

function stashedRewardsUntilEpoch(address, uint256) external view returns (uint256);

function targetGasPowerPerSecond() external view returns (uint256);
Expand Down Expand Up @@ -251,8 +249,6 @@ interface SFCUnitTestI {

function updateSlashingRefundRatio(uint256 validatorID, uint256 refundRatio) external;

function updateStakeTokenizerAddress(address addr) external;

function updateTreasuryAddress(address v) external;

function burnFTM(uint256 amount) external;
Expand Down
Loading
Loading