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): pass epochSize #310

Merged
merged 2 commits into from
Jul 27, 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
4 changes: 2 additions & 2 deletions contracts/child/NetworkParams.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract NetworkParams is Ownable2Step, Initializable {
event NewEpochSize(uint256 indexed size);
event NewEpochReward(uint256 indexed reward);
event NewMinValidatorSetSize(uint256 indexed minValidatorSet);
event NewMaxValdidatorSetSize(uint256 indexed maxValidatorSet);
event NewMaxValidatorSetSize(uint256 indexed maxValidatorSet);
event NewWithdrawalWaitPeriod(uint256 indexed withdrawalPeriod);
event NewBlockTime(uint256 indexed blockTime);
event NewBlockTimeDrift(uint256 indexed blockTimeDrift);
Expand Down Expand Up @@ -144,7 +144,7 @@ contract NetworkParams is Ownable2Step, Initializable {
require(newMaxValidatorSetSize != 0, "NetworkParams: INVALID_MAX_VALIDATOR_SET_SIZE");
maxValidatorSetSize = newMaxValidatorSetSize;

emit NewMaxValdidatorSetSize(newMaxValidatorSetSize);
emit NewMaxValidatorSetSize(newMaxValidatorSetSize);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions contracts/child/validator/RewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ contract RewardPool is IRewardPool, System, Initializable {
/**
* @inheritdoc IRewardPool
*/
function distributeRewardFor(uint256 epochId, Uptime[] calldata uptime) external onlySystemCall {
function distributeRewardFor(uint256 epochId, Uptime[] calldata uptime, uint256 epochSize) external onlySystemCall {
require(paidRewardPerEpoch[epochId] == 0, "REWARD_ALREADY_DISTRIBUTED");
uint256 totalBlocks = validatorSet.totalBlocks(epochId);
require(totalBlocks != 0, "EPOCH_NOT_COMMITTED");
uint256 epochSize = networkParams.epochSize();
// slither-disable-next-line divide-before-multiply
uint256 reward = (networkParams.epochReward() * totalBlocks) / epochSize;
// TODO disincentivize long epoch times
Expand Down
7 changes: 2 additions & 5 deletions contracts/child/validator/ValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ contract ValidatorSet is IValidatorSet, ERC20VotesUpgradeable, System {
/**
* @inheritdoc IValidatorSet
*/
function commitEpoch(uint256 id, Epoch calldata epoch) external onlySystemCall {
function commitEpoch(uint256 id, Epoch calldata epoch, uint256 epochSize) external onlySystemCall {
uint256 newEpochId = currentEpochId++;
require(id == newEpochId, "UNEXPECTED_EPOCH_ID");
require(epoch.endBlock > epoch.startBlock, "NO_BLOCKS_COMMITTED");
require(
(epoch.endBlock - epoch.startBlock + 1) % networkParams.epochSize() == 0,
"EPOCH_MUST_BE_DIVISIBLE_BY_EPOCH_SIZE"
);
require((epoch.endBlock - epoch.startBlock + 1) % 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
2 changes: 1 addition & 1 deletion contracts/interfaces/child/validator/IRewardPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface IRewardPool {
/// @notice distributes reward for the given epoch
/// @dev transfers funds from sender to this contract
/// @param uptime uptime data for every validator
function distributeRewardFor(uint256 epochId, Uptime[] calldata uptime) external;
function distributeRewardFor(uint256 epochId, Uptime[] calldata uptime, uint256 epochSize) external;

/// @notice withdraws pending rewards for the sender (validator)
function withdrawReward() external;
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/child/validator/IValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface IValidatorSet is IStateReceiver {

/// @notice commits a new epoch
/// @dev system call
function commitEpoch(uint256 id, Epoch calldata epoch) external;
function commitEpoch(uint256 id, Epoch calldata epoch, uint256 epochSize) external;

/// @notice allows a validator to announce their intention to withdraw a given amount of tokens
/// @dev initializes a waiting period before the tokens can be withdrawn
Expand Down
19 changes: 10 additions & 9 deletions test/forge/child/validator/RewardPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ abstract contract Uninitialized is Test {
RewardPool pool;
address rewardWallet = makeAddr("rewardWallet");
address alice = makeAddr("alice");
uint256 epochSize = 64;
wschwab marked this conversation as resolved.
Show resolved Hide resolved

NetworkParams networkParams;

function setUp() public virtual {
networkParams = new NetworkParams();
networkParams.initialize(NetworkParams.InitParams(address(1), 1, 64, 1 ether, 1, 1, 1, 1, 1, 1, 1, 1));
networkParams.initialize(NetworkParams.InitParams(address(1), 1, 1, 1 ether, 1, 1, 1, 1, 1, 1, 1, 1));

token = new MockERC20();
validatorSet = new ValidatorSet();
Expand All @@ -32,7 +33,7 @@ abstract contract Uninitialized is Test {
validatorSet.initialize(address(1), address(1), address(1), address(networkParams), init);
Epoch memory epoch = Epoch({startBlock: 1, endBlock: 64, epochRoot: bytes32(0)});
vm.prank(SYSTEM);
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
vm.roll(block.number + 1);
pool = new RewardPool();
token.mint(rewardWallet, 1000 ether);
Expand All @@ -55,7 +56,7 @@ abstract contract Distributed is Initialized {
uptime[0] = Uptime({validator: address(this), signedBlocks: 64});
uptime[1] = Uptime({validator: alice, signedBlocks: 64});
vm.prank(SYSTEM);
pool.distributeRewardFor(1, uptime);
pool.distributeRewardFor(1, uptime, epochSize);
}
}

Expand All @@ -73,29 +74,29 @@ contract RewardPool_Distribute is Initialized {

function test_RevertOnlySystem() public {
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, "SYSTEMCALL"));
pool.distributeRewardFor(1, new Uptime[](0));
pool.distributeRewardFor(1, new Uptime[](0), epochSize);
}

function test_RevertGenesisEpoch() public {
Uptime[] memory uptime = new Uptime[](0);
vm.expectRevert("EPOCH_NOT_COMMITTED");
vm.prank(SYSTEM);
pool.distributeRewardFor(0, uptime);
pool.distributeRewardFor(0, uptime, epochSize);
}

function test_RevertFutureEpoch() public {
Uptime[] memory uptime = new Uptime[](0);
vm.expectRevert("EPOCH_NOT_COMMITTED");
vm.prank(SYSTEM);
pool.distributeRewardFor(2, uptime);
pool.distributeRewardFor(2, uptime, epochSize);
}

function test_RevertSignedBlocksExceedsTotalBlocks() public {
Uptime[] memory uptime = new Uptime[](1);
uptime[0] = Uptime({validator: address(this), signedBlocks: 65});
vm.prank(SYSTEM);
vm.expectRevert("SIGNED_BLOCKS_EXCEEDS_TOTAL");
pool.distributeRewardFor(1, uptime);
pool.distributeRewardFor(1, uptime, epochSize);
}

function test_DistributeRewards(uint256 epochReward) public {
Expand All @@ -111,7 +112,7 @@ contract RewardPool_Distribute is Initialized {
vm.prank(SYSTEM);
vm.expectEmit(true, true, true, true);
emit RewardDistributed(1, totalReward);
pool.distributeRewardFor(1, uptime);
pool.distributeRewardFor(1, uptime, epochSize);
assertEq(pool.pendingRewards(address(this)), reward1);
assertEq(pool.pendingRewards(alice), reward2);
assertEq(pool.paidRewardPerEpoch(1), totalReward);
Expand All @@ -123,7 +124,7 @@ contract RewardPool_DuplicateDistribution is Distributed {
Uptime[] memory uptime = new Uptime[](0);
vm.startPrank(SYSTEM);
vm.expectRevert("REWARD_ALREADY_DISTRIBUTED");
pool.distributeRewardFor(1, uptime);
pool.distributeRewardFor(1, uptime, epochSize);
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/forge/child/validator/ValidatorSet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract contract Uninitialized is Test {

function setUp() public virtual {
networkParams = new NetworkParams();
networkParams.initialize(NetworkParams.InitParams(address(1), 1, epochSize, 1 ether, 1, 1, 1, 1, 1, 1, 1, 1));
networkParams.initialize(NetworkParams.InitParams(address(1), 1, 1, 1 ether, 1, 1, 1, 1, 1, 1, 1, 1));

stateSender = new L2StateSender();
validatorSet = new ValidatorSet();
Expand All @@ -47,7 +47,7 @@ abstract contract Committed is Initialized {
_beforeCommit();
Epoch memory epoch = Epoch({startBlock: 1, endBlock: 64, epochRoot: bytes32(0)});
vm.prank(SYSTEM);
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
vm.roll(block.number + 1);
_afterCommit();
}
Expand Down Expand Up @@ -77,23 +77,23 @@ contract ValidatorSet_CommitEpoch is Initialized {
function test_RevertOnlySystemCall() public {
Epoch memory epoch = Epoch({startBlock: 1, endBlock: 64, epochRoot: bytes32(0)});
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector, "SYSTEMCALL"));
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
}

function test_RevertInvalidEpochId(uint256 id) public {
vm.assume(id != 1);
Epoch memory epoch = Epoch({startBlock: 1, endBlock: 64, epochRoot: bytes32(0)});
vm.expectRevert("UNEXPECTED_EPOCH_ID");
vm.prank(SYSTEM);
validatorSet.commitEpoch(id, epoch);
validatorSet.commitEpoch(id, epoch, epochSize);
}

function test_RevertNoBlocksCommitted(uint256 startBlock, uint256 endBlock) public {
vm.assume(endBlock <= startBlock);
Epoch memory epoch = Epoch({startBlock: startBlock, endBlock: endBlock, epochRoot: bytes32(0)});
vm.expectRevert("NO_BLOCKS_COMMITTED");
vm.prank(SYSTEM);
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
}

function test_RevertEpochSize(uint256 startBlock, uint256 endBlock) public {
Expand All @@ -102,22 +102,22 @@ contract ValidatorSet_CommitEpoch is Initialized {
Epoch memory epoch = Epoch({startBlock: startBlock, endBlock: endBlock, epochRoot: bytes32(0)});
vm.expectRevert("EPOCH_MUST_BE_DIVISIBLE_BY_EPOCH_SIZE");
vm.prank(SYSTEM);
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
}

function test_RevertInvalidStartBlock() public {
Epoch memory epoch = Epoch({startBlock: 0, endBlock: 63, epochRoot: bytes32(0)});
vm.expectRevert("INVALID_START_BLOCK");
vm.prank(SYSTEM);
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
}

function test_CommitEpoch() public {
Epoch memory epoch = Epoch({startBlock: 1, endBlock: 64, epochRoot: bytes32(0)});
vm.prank(SYSTEM);
vm.expectEmit(true, true, true, true);
emit NewEpoch(1, 1, 64, bytes32(0));
validatorSet.commitEpoch(1, epoch);
validatorSet.commitEpoch(1, epoch, epochSize);
assertEq(validatorSet.currentEpochId(), 2);
assertEq(validatorSet.epochEndBlocks(1), 64);
assertEq(validatorSet.totalBlocks(1), 64);
Expand Down
Loading