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

fix: gov, add initializers #298

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions contracts/child/ForkParams.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
pragma solidity 0.8.19;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

/**
@title ForkParams
@author Polygon Technology (@QEDK)
@notice Configurable softfork features that are read by the client on each epoch
@dev The contract allows for configurable softfork parameters without genesis updation
*/
contract ForkParams is Ownable {
contract ForkParams is Ownable, Initializable {
mapping(bytes32 => uint256) public featureToBlockNumber; // keccak256("FEATURE_NAME") -> blockNumber

event NewFeature(bytes32 indexed feature, uint256 indexed block);
event UpdatedFeature(bytes32 indexed feature, uint256 indexed block);

/**
* @notice constructor function to set the owner
* @notice initialize function to set the owner
* @param newOwner address to transfer the ownership to
*/
constructor(address newOwner) {
function initialize(address newOwner) public initializer {
_transferOwnership(newOwner);
}

Expand Down
24 changes: 22 additions & 2 deletions contracts/child/NetworkParams.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
pragma solidity 0.8.19;

import "@openzeppelin/contracts/access/Ownable2Step.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

/**
@title NetworkParams
@author Polygon Technology (@QEDK)
@notice Configurable network parameters that are read by the client on each epoch
@dev The contract allows for configurable network parameters without the need for a hardfork
*/
contract NetworkParams is Ownable2Step {
contract NetworkParams is Ownable2Step, Initializable {
struct InitParams {
address newOwner;
uint256 newBlockGasLimit;
uint256 newCheckpointBlockInterval; // in blocks
uint256 newEpochSize; // in blocks
uint256 newEpochReward; // in wei
uint256 newMinValidatorSetSize;
uint256 newMaxValidatorSetSize;
Expand All @@ -28,6 +30,7 @@ contract NetworkParams is Ownable2Step {

uint256 public blockGasLimit;
uint256 public checkpointBlockInterval; // in blocks
uint256 public epochSize; // in blocks
uint256 public epochReward; // in wei
uint256 public minValidatorSetSize;
uint256 public maxValidatorSetSize;
Expand All @@ -41,6 +44,7 @@ contract NetworkParams is Ownable2Step {

event NewBlockGasLimit(uint256 indexed gasLimit);
event NewCheckpointBlockInterval(uint256 indexed checkpointInterval);
event NewEpochSize(uint256 indexed size);
event NewEpochReward(uint256 indexed reward);
event NewMinValidatorSetSize(uint256 indexed minValidatorSet);
event NewMaxValdidatorSetSize(uint256 indexed maxValidatorSet);
Expand All @@ -57,11 +61,12 @@ contract NetworkParams is Ownable2Step {
* @dev disallows setting of zero values for sanity check purposes
* @param initParams initial set of values for the network
*/
constructor(InitParams memory initParams) {
function initialize(InitParams memory initParams) public initializer {
require(
initParams.newOwner != address(0) &&
initParams.newBlockGasLimit != 0 &&
initParams.newCheckpointBlockInterval != 0 &&
initParams.newEpochSize != 0 &&
initParams.newEpochReward != 0 &&
initParams.newMinValidatorSetSize != 0 &&
initParams.newMaxValidatorSetSize != 0 &&
Expand All @@ -76,6 +81,7 @@ contract NetworkParams is Ownable2Step {
);
blockGasLimit = initParams.newBlockGasLimit;
checkpointBlockInterval = initParams.newCheckpointBlockInterval;
epochSize = initParams.newEpochSize;
epochReward = initParams.newEpochReward;
minValidatorSetSize = initParams.newMinValidatorSetSize;
maxValidatorSetSize = initParams.newMaxValidatorSetSize;
Expand Down Expand Up @@ -113,6 +119,18 @@ contract NetworkParams is Ownable2Step {
emit NewCheckpointBlockInterval(newCheckpointBlockInterval);
}

/**
* @notice function to set new epoch size
* @dev disallows setting of a zero value for sanity check purposes
* @param newEpochSize new epoch reward
*/
function setNewEpochSize(uint256 newEpochSize) external onlyOwner {
require(newEpochSize != 0, "NetworkParams: INVALID_EPOCH_SIZE");
epochSize = newEpochSize;

emit NewEpochSize(newEpochSize);
}

/**
* @notice function to set new epoch reward
* @dev disallows setting of a zero value for sanity check purposes
Expand All @@ -130,6 +148,7 @@ contract NetworkParams is Ownable2Step {
* @dev disallows setting of a zero value for sanity check purposes
* @param newMinValidatorSetSize new minimum validator set size
*/
// slither-disable-next-line similar-names
function setNewMinValidatorSetSize(uint256 newMinValidatorSetSize) external onlyOwner {
require(newMinValidatorSetSize != 0, "NetworkParams: INVALID_MIN_VALIDATOR_SET_SIZE");
minValidatorSetSize = newMinValidatorSetSize;
Expand All @@ -142,6 +161,7 @@ contract NetworkParams is Ownable2Step {
* @dev disallows setting of a zero value for sanity check purposes
* @param newMaxValidatorSetSize new maximum validator set size
*/
// slither-disable-next-line similar-names
function setNewMaxValidatorSetSize(uint256 newMaxValidatorSetSize) external onlyOwner {
require(newMaxValidatorSetSize != 0, "NetworkParams: INVALID_MAX_VALIDATOR_SET_SIZE");
maxValidatorSetSize = newMaxValidatorSetSize;
Expand Down
2 changes: 1 addition & 1 deletion contracts/child/validator/RewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract RewardPool is IRewardPool, System, Initializable {
require(paidRewardPerEpoch[epochId] == 0, "REWARD_ALREADY_DISTRIBUTED");
uint256 totalBlocks = validatorSet.totalBlocks(epochId);
require(totalBlocks != 0, "EPOCH_NOT_COMMITTED");
uint256 epochSize = validatorSet.EPOCH_SIZE();
uint256 epochSize = networkParams.epochSize();
// slither-disable-next-line divide-before-multiply
uint256 reward = (networkParams.epochReward() * totalBlocks) / epochSize;
// TODO disincentivize long epoch times
Expand Down
22 changes: 11 additions & 11 deletions contracts/child/validator/ValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20VotesUpg
import "../../lib/WithdrawalQueue.sol";
import "../../interfaces/child/validator/IValidatorSet.sol";
import "../../interfaces/IStateSender.sol";
import "../../child/NetworkParams.sol";
import "../System.sol";

contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
Expand All @@ -13,13 +14,11 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
bytes32 private constant STAKE_SIG = keccak256("STAKE");
bytes32 private constant UNSTAKE_SIG = keccak256("UNSTAKE");
bytes32 private constant SLASH_SIG = keccak256("SLASH");
uint256 public constant WITHDRAWAL_WAIT_PERIOD = 1;

IStateSender private stateSender;
address private stateReceiver;
address private rootChainManager;
// slither-disable-next-line naming-convention
uint256 public EPOCH_SIZE;
NetworkParams private networkParams;

uint256 public currentEpochId;

Expand All @@ -32,21 +31,18 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
address newStateSender,
address newStateReceiver,
address newRootChainManager,
uint256 newEpochSize,
address newNetworkParams,
ValidatorInit[] memory initialValidators
) public initializer {
require(
newStateSender != address(0) &&
newStateReceiver != address(0) &&
newRootChainManager != address(0) &&
newEpochSize != 0,
newStateSender != address(0) && newStateReceiver != address(0) && newRootChainManager != address(0),
"INVALID_INPUT"
);
__ERC20_init("ValidatorSet", "VSET");
stateSender = IStateSender(newStateSender);
stateReceiver = newStateReceiver;
rootChainManager = newRootChainManager;
EPOCH_SIZE = newEpochSize;
networkParams = NetworkParams(newNetworkParams);
for (uint256 i = 0; i < initialValidators.length; ) {
_stake(initialValidators[i].addr, initialValidators[i].stake);
unchecked {
Expand All @@ -64,7 +60,10 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
uint256 newEpochId = currentEpochId++;
require(id == newEpochId, "UNEXPECTED_EPOCH_ID");
require(epoch.endBlock > epoch.startBlock, "NO_BLOCKS_COMMITTED");
require((epoch.endBlock - epoch.startBlock + 1) % EPOCH_SIZE == 0, "EPOCH_MUST_BE_DIVISIBLE_BY_EPOCH_SIZE");
require(
(epoch.endBlock - epoch.startBlock + 1) % networkParams.epochSize() == 0,
"EPOCH_MUST_BE_DIVISIBLE_BY_EPOCH_SIZE"
);
require(epochs[newEpochId - 1].endBlock + 1 == epoch.startBlock, "INVALID_START_BLOCK");
epochs[newEpochId] = epoch;
_commitBlockNumbers[newEpochId] = block.number;
Expand Down Expand Up @@ -102,6 +101,7 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
/**
* @inheritdoc IValidatorSet
*/
// slither-disable-next-line unused-return
function withdrawable(address account) external view returns (uint256 amount) {
(amount, ) = withdrawals[account].withdrawable(currentEpochId);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
}

function _registerWithdrawal(address account, uint256 amount) internal {
withdrawals[account].append(amount, currentEpochId + WITHDRAWAL_WAIT_PERIOD);
withdrawals[account].append(amount, currentEpochId + networkParams.withdrawalWaitPeriod());
emit WithdrawalRegistered(account, amount);
}

Expand Down
5 changes: 0 additions & 5 deletions contracts/interfaces/child/validator/IValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ interface IValidatorSet is IStateReceiver {
/// @dev calls the bridge to release the funds on root
function withdraw() external;

/// @notice amount of blocks in an epoch
/// @dev when an epoch is committed a multiple of this number of blocks must be committed
// slither-disable-next-line naming-convention
function EPOCH_SIZE() external view returns (uint256);

/// @notice total amount of blocks in a given epoch
function totalBlocks(uint256 epochId) external view returns (uint256 length);

Expand Down
2 changes: 1 addition & 1 deletion docs/child/ChildERC1155PredicateAccessList.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function renounceOwnership() external nonpayable



*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby disabling any functionality that is only available to the owner.*


### rootERC1155Predicate
Expand Down
2 changes: 1 addition & 1 deletion docs/child/ChildERC20PredicateAccessList.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function renounceOwnership() external nonpayable



*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby disabling any functionality that is only available to the owner.*


### rootERC20Predicate
Expand Down
2 changes: 1 addition & 1 deletion docs/child/ChildERC721PredicateAccessList.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function renounceOwnership() external nonpayable



*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby disabling any functionality that is only available to the owner.*


### rootERC721Predicate
Expand Down
34 changes: 33 additions & 1 deletion docs/child/ForkParams.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ function featureToBlockNumber(bytes32) external view returns (uint256)
|---|---|---|
| _0 | uint256 | undefined |

### initialize

```solidity
function initialize(address newOwner) external nonpayable
```

initialize function to set the owner



#### Parameters

| Name | Type | Description |
|---|---|---|
| newOwner | address | address to transfer the ownership to |

### isFeatureActivated

```solidity
Expand Down Expand Up @@ -96,7 +112,7 @@ function renounceOwnership() external nonpayable



*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby disabling any functionality that is only available to the owner.*


### transferOwnership
Expand Down Expand Up @@ -136,6 +152,22 @@ function to update the block number for a feature

## Events

### Initialized

```solidity
event Initialized(uint8 version)
```



*Triggered when the contract has been initialized or reinitialized.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| version | uint8 | undefined |

### NewFeature

```solidity
Expand Down
2 changes: 1 addition & 1 deletion docs/child/NativeERC20Mintable.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ function renounceOwnership() external nonpayable



*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.*
*Leaves the contract without owner. It will not be possible to call `onlyOwner` functions. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby disabling any functionality that is only available to the owner.*


### rootToken
Expand Down
Loading