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

Make SFC and NodeDriver(Auth) upgradable #96

Merged
merged 9 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
57 changes: 0 additions & 57 deletions contracts/common/Initializable.sol

This file was deleted.

96 changes: 0 additions & 96 deletions contracts/ownership/Ownable.sol

This file was deleted.

6 changes: 3 additions & 3 deletions contracts/sfc/ConstantsManager.sol
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 security@fantom.foundation
*/
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
Expand Down Expand Up @@ -48,7 +48,7 @@ contract ConstantsManager is Ownable {
error ValueTooLarge();

function initialize() external initializer {
Ownable.initialize(msg.sender);
__Ownable_init(msg.sender);
Mike-CZ marked this conversation as resolved.
Show resolved Hide resolved
}

function updateMinSelfStake(uint256 v) external virtual onlyOwner {
Expand Down
5 changes: 3 additions & 2 deletions contracts/sfc/NetworkInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity 0.8.27;

import {ISFC} from "../interfaces/ISFC.sol";
import {NodeDriver, NodeDriverAuth} from "./NodeDriver.sol";
import {NodeDriver} from "./NodeDriver.sol";
import {NodeDriverAuth} from "./NodeDriverAuth.sol";
import {ConstantsManager} from "./ConstantsManager.sol";
import {Decimal} from "../common/Decimal.sol";

Expand All @@ -20,7 +21,7 @@ contract NetworkInitializer {
address _evmWriter,
address _owner
) external {
NodeDriver(_driver).initialize(_auth, _evmWriter);
NodeDriver(_driver).initialize(_auth, _evmWriter, _owner);
NodeDriverAuth(_auth).initialize(_sfc, _driver, _owner);

ConstantsManager consts = new ConstantsManager();
Expand Down
14 changes: 11 additions & 3 deletions contracts/sfc/NodeDriver.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// 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 {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.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";
Expand All @@ -12,7 +14,7 @@ import {INodeDriver} from "../interfaces/INodeDriver.sol";
* @dev Methods with onlyNode modifier are called by Sonic internal txs during epoch sealing.
* @custom:security-contact security@fantom.foundation
*/
contract NodeDriver is Initializable, INodeDriver {
contract NodeDriver is Initializable, OwnableUpgradeable, UUPSUpgradeable, INodeDriver {
NodeDriverAuth internal backend;
IEVMWriter internal evmWriter;

Expand Down Expand Up @@ -44,12 +46,18 @@ 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);
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/sfc/NodeDriverAuth.sol
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 {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.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 security@fantom.foundation
*/
contract NodeDriverAuth is Initializable, Ownable {
contract NodeDriverAuth is Initializable, OwnableUpgradeable {
Mike-CZ marked this conversation as resolved.
Show resolved Hide resolved
ISFC internal sfc;
NodeDriver internal driver;

Expand All @@ -23,7 +23,7 @@ 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);
driver = NodeDriver(_driver);
sfc = ISFC(_sfc);
}
Expand Down
14 changes: 10 additions & 4 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// 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 {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {Decimal} from "../common/Decimal.sol";
import {NodeDriverAuth} from "./NodeDriverAuth.sol";
import {ConstantsManager} from "./ConstantsManager.sol";
Expand All @@ -13,7 +14,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 security@fantom.foundation
*/
contract SFC is Initializable, Ownable, Version {
contract SFC is Initializable, OwnableUpgradeable, UUPSUpgradeable, Version {
uint256 internal constant OK_STATUS = 0;
uint256 internal constant WITHDRAWN_BIT = 1;
uint256 internal constant OFFLINE_BIT = 1 << 3;
Expand Down Expand Up @@ -224,14 +225,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();
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/UnitTestSFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract UnitTestNetworkInitializer {
address _evmWriter,
address _owner
) external {
NodeDriver(_driver).initialize(_auth, _evmWriter);
NodeDriver(_driver).initialize(_auth, _evmWriter, _owner);
NodeDriverAuth(_auth).initialize(_sfc, _driver, _owner);

UnitTestConstantsManager consts = new UnitTestConstantsManager();
Expand Down
3 changes: 2 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { HardhatUserConfig } from 'hardhat/config';
import * as dotenv from 'dotenv';
import '@nomicfoundation/hardhat-chai-matchers';
import '@nomicfoundation/hardhat-ethers';
import '@openzeppelin/hardhat-upgrades';
import '@typechain/hardhat';
import 'hardhat-contract-sizer';
import 'hardhat-gas-reporter';
import 'solidity-coverage';
import '@typechain/hardhat';

dotenv.config();

Expand Down
Loading