-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SFC and NodeDriver(Auth) upgradable (#96)
* Initial configuration * Fix tests * Disable solhint warning * Add proxy test * Replace Ownable and Initializable with OZ implementation * Make NodeDriver upgradable * Make NodeDriverAuth upgradable * Add constructor for constants manager
- Loading branch information
Showing
15 changed files
with
818 additions
and
237 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.27; | ||
|
||
import {Ownable} from "../ownership/Ownable.sol"; | ||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import {Decimal} from "../common/Decimal.sol"; | ||
|
||
/** | ||
* @custom:security-contact [email protected] | ||
*/ | ||
contract ConstantsManager is Ownable { | ||
contract ConstantsManager is OwnableUpgradeable { | ||
// Minimum amount of stake for a validator, i.e., 500000 FTM | ||
uint256 public minSelfStake; | ||
// Maximum ratio of delegations a validator can have, say, 15 times of self-stake | ||
|
@@ -47,8 +47,8 @@ contract ConstantsManager is Ownable { | |
*/ | ||
error ValueTooLarge(); | ||
|
||
function initialize() external initializer { | ||
Ownable.initialize(msg.sender); | ||
constructor(address owner) initializer { | ||
__Ownable_init(owner); | ||
} | ||
|
||
function updateMinSelfStake(uint256 v) external virtual onlyOwner { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.27; | ||
|
||
import {Initializable} from "../common/Initializable.sol"; | ||
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import {NodeDriverAuth} from "./NodeDriverAuth.sol"; | ||
import {IEVMWriter} from "../interfaces/IEVMWriter.sol"; | ||
import {INodeDriver} from "../interfaces/INodeDriver.sol"; | ||
|
@@ -12,21 +13,13 @@ import {INodeDriver} from "../interfaces/INodeDriver.sol"; | |
* @dev Methods with onlyNode modifier are called by Sonic internal txs during epoch sealing. | ||
* @custom:security-contact [email protected] | ||
*/ | ||
contract NodeDriver is Initializable, INodeDriver { | ||
contract NodeDriver is OwnableUpgradeable, UUPSUpgradeable, INodeDriver { | ||
NodeDriverAuth internal backend; | ||
IEVMWriter internal evmWriter; | ||
|
||
error NotNode(); | ||
error NotBackend(); | ||
|
||
event UpdatedBackend(address indexed backend); | ||
|
||
/// NodeDriverAuth can replace itself | ||
function setBackend(address _backend) external onlyBackend { | ||
emit UpdatedBackend(_backend); | ||
backend = NodeDriverAuth(_backend); | ||
} | ||
|
||
/// Callable only by NodeDriverAuth (which mediates calls from SFC and from admins) | ||
modifier onlyBackend() { | ||
if (msg.sender != address(backend)) { | ||
|
@@ -44,12 +37,17 @@ contract NodeDriver is Initializable, INodeDriver { | |
|
||
/// Initialization is called only once, after the contract deployment. | ||
/// Because the contract code is written directly into genesis, constructor cannot be used. | ||
function initialize(address _backend, address _evmWriterAddress) external initializer { | ||
function initialize(address _backend, address _evmWriterAddress, address _owner) external initializer { | ||
__Ownable_init(_owner); | ||
__UUPSUpgradeable_init(); | ||
backend = NodeDriverAuth(_backend); | ||
emit UpdatedBackend(_backend); | ||
evmWriter = IEVMWriter(_evmWriterAddress); | ||
} | ||
|
||
/// Override the upgrade authorization check to allow upgrades only from the owner. | ||
// solhint-disable-next-line no-empty-blocks | ||
function _authorizeUpgrade(address) internal override onlyOwner {} | ||
|
||
function setBalance(address acc, uint256 value) external onlyBackend { | ||
evmWriter.setBalance(acc, value); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.27; | ||
|
||
import {Initializable} from "../common/Initializable.sol"; | ||
import {Ownable} from "../ownership/Ownable.sol"; | ||
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import {ISFC} from "../interfaces/ISFC.sol"; | ||
import {NodeDriver} from "./NodeDriver.sol"; | ||
import {INodeDriverExecutable} from "../interfaces/INodeDriverExecutable.sol"; | ||
|
||
/** | ||
* @custom:security-contact [email protected] | ||
*/ | ||
contract NodeDriverAuth is Initializable, Ownable { | ||
contract NodeDriverAuth is OwnableUpgradeable, UUPSUpgradeable { | ||
ISFC internal sfc; | ||
NodeDriver internal driver; | ||
|
||
|
@@ -23,11 +23,16 @@ contract NodeDriverAuth is Initializable, Ownable { | |
|
||
// Initialize NodeDriverAuth, NodeDriver and SFC in one call to allow fewer genesis transactions | ||
function initialize(address payable _sfc, address _driver, address _owner) external initializer { | ||
Ownable.initialize(_owner); | ||
__Ownable_init(_owner); | ||
__UUPSUpgradeable_init(); | ||
driver = NodeDriver(_driver); | ||
sfc = ISFC(_sfc); | ||
} | ||
|
||
/// Override the upgrade authorization check to allow upgrades only from the owner. | ||
// solhint-disable-next-line no-empty-blocks | ||
function _authorizeUpgrade(address) internal override onlyOwner {} | ||
|
||
/// Callable only by SFC contract. | ||
modifier onlySFC() { | ||
if (msg.sender != address(sfc)) { | ||
|
@@ -44,11 +49,6 @@ contract NodeDriverAuth is Initializable, Ownable { | |
_; | ||
} | ||
|
||
/// Change NodeDriverAuth used by NodeDriver. Callable by network admin. | ||
function migrateTo(address newDriverAuth) external onlyOwner { | ||
driver.setBackend(newDriverAuth); | ||
} | ||
|
||
function _execute(address executable, address newOwner, bytes32 selfCodeHash, bytes32 driverCodeHash) internal { | ||
_transferOwnership(executable); | ||
INodeDriverExecutable(executable).execute(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.27; | ||
|
||
import {Ownable} from "../ownership/Ownable.sol"; | ||
import {Initializable} from "../common/Initializable.sol"; | ||
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import {Decimal} from "../common/Decimal.sol"; | ||
import {NodeDriverAuth} from "./NodeDriverAuth.sol"; | ||
import {ConstantsManager} from "./ConstantsManager.sol"; | ||
|
@@ -13,7 +13,7 @@ import {Version} from "../version/Version.sol"; | |
* @notice The SFC maintains a list of validators and delegators and distributes rewards to them. | ||
* @custom:security-contact [email protected] | ||
*/ | ||
contract SFC is Initializable, Ownable, Version { | ||
contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version { | ||
uint256 internal constant OK_STATUS = 0; | ||
uint256 internal constant WITHDRAWN_BIT = 1; | ||
uint256 internal constant OFFLINE_BIT = 1 << 3; | ||
|
@@ -224,14 +224,19 @@ contract SFC is Initializable, Ownable, Version { | |
address _c, | ||
address owner | ||
) external initializer { | ||
Ownable.initialize(owner); | ||
__Ownable_init(owner); | ||
__UUPSUpgradeable_init(); | ||
currentSealedEpoch = sealedEpoch; | ||
node = NodeDriverAuth(nodeDriver); | ||
c = ConstantsManager(_c); | ||
totalSupply = _totalSupply; | ||
getEpochSnapshot[sealedEpoch].endTime = _now(); | ||
} | ||
|
||
/// Override the upgrade authorization check to allow upgrades only from the owner. | ||
// solhint-disable-next-line no-empty-blocks | ||
function _authorizeUpgrade(address) internal override onlyOwner {} | ||
|
||
/// Receive fallback to revert transfers. | ||
receive() external payable { | ||
revert TransfersNotAllowed(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.