Skip to content

Commit

Permalink
Merge branch 'feat/l2-epoch-system' into soloseng/return-account-inst…
Browse files Browse the repository at this point in the history
…ead-of-signer
  • Loading branch information
soloseng committed Sep 17, 2024
2 parents a14568f + f4b4564 commit 7a10127
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 141 deletions.
24 changes: 7 additions & 17 deletions packages/protocol/contracts-0.8/common/EpochManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ contract EpochManager is
mapping(uint256 => Epoch) private epochs;
mapping(address => uint256) public validatorPendingPayments;

address public carbonOffsettingPartner;
address public epochManagerEnabler;

/**
* @notice Event emited when epochProcessing has begun.
* @param epochNumber The epoch number that is being processed.
Expand All @@ -77,7 +74,10 @@ contract EpochManager is
event EpochProcessingEnded(uint256 indexed epochNumber);

modifier onlyEpochManagerEnabler() {
require(msg.sender == epochManagerEnabler, "msg.sender is not Initializer");
require(
msg.sender == registry.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID),
"msg.sender is not Enabler"
);
_;
}

Expand All @@ -92,19 +92,10 @@ contract EpochManager is
* @param registryAddress The address of the registry core smart contract.
* @param newEpochDuration The duration of an epoch in seconds.
*/
function initialize(
address registryAddress,
uint256 newEpochDuration,
address _carbonOffsettingPartner,
address _epochManagerEnabler
) external initializer {
require(_carbonOffsettingPartner != address(0), "carbonOffsettingPartner address is required");
require(_epochManagerEnabler != address(0), "EpochManagerEnabler address is required");
function initialize(address registryAddress, uint256 newEpochDuration) external initializer {
_transferOwnership(msg.sender);
setRegistry(registryAddress);
setEpochDuration(newEpochDuration);
carbonOffsettingPartner = _carbonOffsettingPartner;
epochManagerEnabler = _epochManagerEnabler;
}

// DESIGNDESICION(XXX): we assume that the first epoch on the L2 starts as soon as the system is initialized
Expand Down Expand Up @@ -140,7 +131,6 @@ contract EpochManager is
_currentEpoch.startTimestamp = block.timestamp;

elected = firstElected;
epochManagerEnabler = address(0);
}

// TODO maybe "freezeEpochRewards" "prepareForNextEpoch"
Expand Down Expand Up @@ -228,7 +218,7 @@ contract EpochManager is
epochProcessing.totalRewardsCommunity
);
getCeloUnreleasedTreasure().release(
carbonOffsettingPartner,
getEpochRewards().carbonOffsettingPartner(),
epochProcessing.totalRewardsCarbonFund
);
// run elections
Expand Down Expand Up @@ -313,7 +303,7 @@ contract EpochManager is
}

function systemAlreadyInitialized() public view returns (bool) {
return initialized && epochManagerEnabler == address(0);
return initialized && elected.length > 0;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/contracts-0.8/common/UsingRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ contract UsingRegistry is Ownable {
bytes32 constant LOCKED_CELO_REGISTRY_ID = keccak256(abi.encodePacked("LockedCelo"));
bytes32 constant CELO_UNRELEASED_TREASURE_REGISTRY_ID =
keccak256(abi.encodePacked("CeloUnreleasedTreasure"));
bytes32 constant EPOCH_MANAGER_ENABLER_REGISTRY_ID =
keccak256(abi.encodePacked("EpochManagerEnabler"));
bytes32 constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager"));
bytes32 constant SCORE_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("ScoreManager"));
// solhint-enable state-visibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@
pragma solidity >=0.5.13 <0.9.0;

interface IEpochManagerInitializer {
function initialize(
address registryAddress,
uint256 newEpochDuration,
address _carbonOffsettingPartner,
address _epochManagerEnabler
) external;
function initialize(address registryAddress, uint256 newEpochDuration) external;
}
5 changes: 0 additions & 5 deletions packages/protocol/contracts-0.8/governance/Validators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ contract Validators is
_;
}

modifier onlyEpochManager() {
require(msg.sender == address(getEpochManager()), "Only epoch manager can call");
_;
}

/**
* @notice Sets initialized == true on implementation contracts
* @param test Set to true to skip implementation initialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "../../../contracts/governance/interfaces/IEpochRewards.sol";
*/
contract EpochRewardsMock08 is IEpochRewards {
uint256 private numValidatorsInCurrentSet;
address public carbonOffsettingPartner;

function setNumberValidatorsInCurrentSet(uint256 value) external {
numValidatorsInCurrentSet = value;
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/common/UsingRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ contract UsingRegistry is Ownable {
bytes32 constant CELO_UNRELEASED_TREASURE_REGISTRY_ID =
keccak256(abi.encodePacked("CeloUnreleasedTreasure"));
bytes32 constant EPOCH_REWARDS_REGISTRY_ID = keccak256(abi.encodePacked("EpochRewards"));
bytes32 constant EPOCH_MANAGER_INITIALIZER_REGISTRY_ID =
keccak256(abi.encodePacked("EpochManagerInitializer"));
bytes32 constant EPOCH_MANAGER_ENABLER_REGISTRY_ID =
keccak256(abi.encodePacked("EpochManagerEnabler"));
bytes32 constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager"));
// solhint-enable state-visibility

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/common/UsingRegistryV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ contract UsingRegistryV2 {
bytes32 internal constant CELO_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("CeloToken"));
bytes32 internal constant LOCKED_CELO_REGISTRY_ID = keccak256(abi.encodePacked("LockedCelo"));
bytes32 internal constant EPOCH_REWARDS_REGISTRY_ID = keccak256(abi.encodePacked("EpochRewards"));
bytes32 internal constant EPOCH_MANAGER_INITIALIZER_REGISTRY_ID =
keccak256(abi.encodePacked("EpochManagerInitializer"));
bytes32 internal constant EPOCH_MANAGER_ENABLER_REGISTRY_ID =
keccak256(abi.encodePacked("EpochManagerEnabler"));
bytes32 internal constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager"));

modifier onlyRegisteredContract(bytes32 identifierHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface IEpochManager {
function getCurrentEpoch() external view returns (uint256, uint256, uint256, uint256);
function getCurrentEpochNumber() external view returns (uint256);
function getElected() external view returns (address[] memory);
function epochManagerEnabler() external view returns (address);
function epochDuration() external view returns (uint256);
function firstKnownEpoch() external view returns (uint256);
function getEpochProcessingState()
Expand Down
17 changes: 7 additions & 10 deletions packages/protocol/contracts/governance/EpochRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ contract EpochRewards is
* @param value The percentage of the total reward to be sent to the community funds.
* @return True upon success.
*/
function setCommunityRewardFraction(uint256 value) public onlyOwner onlyL1 returns (bool) {
function setCommunityRewardFraction(uint256 value) public onlyOwner returns (bool) {
require(
value != communityRewardFraction.unwrap() && value < FixidityLib.fixed1().unwrap(),
"Value must be different from existing community reward fraction and less than 1"
Expand All @@ -302,10 +302,7 @@ contract EpochRewards is
* @param value The percentage of the total reward to be sent to the carbon offsetting partner.
* @return True upon success.
*/
function setCarbonOffsettingFund(
address partner,
uint256 value
) public onlyOwner onlyL1 returns (bool) {
function setCarbonOffsettingFund(address partner, uint256 value) public onlyOwner returns (bool) {
require(
partner != carbonOffsettingPartner || value != carbonOffsettingFraction.unwrap(),
"Partner and value must be different from existing carbon offsetting fund"
Expand All @@ -322,7 +319,7 @@ contract EpochRewards is
* @param value The percentage of floating Gold voting to target.
* @return True upon success.
*/
function setTargetVotingGoldFraction(uint256 value) public onlyOwner onlyL1 returns (bool) {
function setTargetVotingGoldFraction(uint256 value) public onlyOwner returns (bool) {
require(value != targetVotingGoldFraction.unwrap(), "Target voting gold fraction unchanged");
require(
value < FixidityLib.fixed1().unwrap(),
Expand All @@ -338,7 +335,7 @@ contract EpochRewards is
* @param value The value in Celo Dollars.
* @return True upon success.
*/
function setTargetValidatorEpochPayment(uint256 value) public onlyOwner onlyL1 returns (bool) {
function setTargetValidatorEpochPayment(uint256 value) public onlyOwner returns (bool) {
require(value != targetValidatorEpochPayment, "Target validator epoch payment unchanged");
targetValidatorEpochPayment = value;
emit TargetValidatorEpochPaymentSet(value);
Expand All @@ -358,7 +355,7 @@ contract EpochRewards is
uint256 max,
uint256 underspendAdjustmentFactor,
uint256 overspendAdjustmentFactor
) public onlyOwner onlyL1 returns (bool) {
) public onlyOwner returns (bool) {
require(
max != rewardsMultiplierParams.max.unwrap() ||
overspendAdjustmentFactor != rewardsMultiplierParams.adjustmentFactors.overspend.unwrap() ||
Expand All @@ -385,7 +382,7 @@ contract EpochRewards is
function setTargetVotingYieldParameters(
uint256 max,
uint256 adjustmentFactor
) public onlyOwner onlyL1 returns (bool) {
) public onlyOwner returns (bool) {
require(
max != targetVotingYieldParams.max.unwrap() ||
adjustmentFactor != targetVotingYieldParams.adjustmentFactor.unwrap(),
Expand All @@ -407,7 +404,7 @@ contract EpochRewards is
* @param targetVotingYield The relative target block reward for voters.
* @return True upon success.
*/
function setTargetVotingYield(uint256 targetVotingYield) public onlyOwner onlyL1 returns (bool) {
function setTargetVotingYield(uint256 targetVotingYield) public onlyOwner returns (bool) {
FixidityLib.Fraction memory target = FixidityLib.wrap(targetVotingYield);
require(
target.lte(targetVotingYieldParams.max),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface IEpochRewards {
function getCarbonOffsettingFraction() external view returns (uint256);
function getTargetVotingGoldFraction() external view returns (uint256);
function getRewardsMultiplier() external view returns (uint256);
function carbonOffsettingPartner() external view returns (address);
}
1 change: 0 additions & 1 deletion packages/protocol/migrationsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ const DefaultConfig = {
},
epochManager: {
newEpochDuration: 100,
carbonOffsettingPartner: '0x0000000000000000000000000000000000000000',
},
exchange: {
spread: 5 / 1000,
Expand Down
8 changes: 1 addition & 7 deletions packages/protocol/migrations_sol/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,6 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
}

function migrateEpochManager(string memory json) public {
address carbonOffsettingPartner = abi.decode(
json.parseRaw(".epochManager.carbonOffsettingPartner"),
(address)
);
address newEpochDuration = abi.decode(
json.parseRaw(".epochManager.newEpochDuration"),
(address)
Expand All @@ -974,9 +970,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
abi.encodeWithSelector(
IEpochManagerInitializer.initialize.selector,
REGISTRY_ADDRESS,
newEpochDuration,
carbonOffsettingPartner,
registry.getAddressForString("EpochManagerEnabler")
newEpochDuration
)
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/migrations_sol/MigrationL2.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ contract MigrationL2 is Script, MigrationsConstants, UsingRegistry {
console.log("Initializing EpochManager system");
address[] memory firstElected = getValidators().getRegisteredValidators();
IEpochManager epochManager = getEpochManager();
address epochManagerEnablerAddress = epochManager.epochManagerEnabler();
address epochManagerEnablerAddress = registry.getAddressForOrDie(
EPOCH_MANAGER_ENABLER_REGISTRY_ID
);

IEpochManagerEnabler epochManagerEnabler = IEpochManagerEnabler(epochManagerEnablerAddress);
epochManagerEnabler.initEpochManager();
Expand Down
3 changes: 1 addition & 2 deletions packages/protocol/migrations_sol/migrationsConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@
"frozen": false
},
"epochManager": {
"newEpochDuration": 86400,
"carbonOffsettingPartner": "0xD533Ca259b330c7A88f74E000a3FaEa2d63B7972"
"newEpochDuration": 86400
},
"random": {
"randomnessBlockRetentionWindow": "720",
Expand Down
14 changes: 2 additions & 12 deletions packages/protocol/migrations_ts/26_103_epoch_manager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { SOLIDITY_08_PACKAGE } from '@celo/protocol/contractPackages'
import { CeloContractName } from '@celo/protocol/lib/registry-utils'
import { deploymentForCoreContract, getProxiedContract } from '@celo/protocol/lib/web3-utils'
import { deploymentForCoreContract } from '@celo/protocol/lib/web3-utils'
import { config } from '@celo/protocol/migrationsConfig'
import { EpochManagerInstance } from 'types/08'

const initializeArgs = async (): Promise<any[]> => {
const epochManagerInitializer = await getProxiedContract(
CeloContractName.EpochManagerEnabler,
SOLIDITY_08_PACKAGE
)

return [
config.registry.predeployedProxyAddress,
config.epochManager.newEpochDuration,
config.epochManager.carbonOffsettingPartner,
epochManagerInitializer.address,
]
return [config.registry.predeployedProxyAddress, config.epochManager.newEpochDuration]
}

module.exports = deploymentForCoreContract<EpochManagerInstance>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"CeloUnreleasedTreasure": ["0x000000000000000000000000000000000000ce10"],
"EpochManager": ["0x000000000000000000000000000000000000ce10", 86400, "0xD533Ca259b330c7A88f74E000a3FaEa2d63B7972", "0x0000000000000000000000000000000000000000"],
"EpochManager": ["0x000000000000000000000000000000000000ce10", 86400],
"EpochManagerEnabler": ["0x000000000000000000000000000000000000ce10"],
"ScoreManager": [],
"FeeCurrencyDirectory": []
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/test-sol/constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract TestConstants {
string constant GovernanceContract = "Governance";
string constant EpochRewardsContract = "EpochRewards";
string constant EpochManagerContract = "EpochManager";
string constant EpochManagerEnablerContract = "EpochManagerEnabler";
string constant ScoreManagerContract = "ScoreManager";
string constant ReserveContract = "Reserve";
string constant CeloUnreleasedTreasureContract = "CeloUnreleasedTreasure";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract E2E_EpochManager is Test, Devchain, Utils08, ECDSAHelper08 {
uint256 totalVotes = election.getTotalVotes();

epochManagerOwner = Ownable(address(epochManager)).owner();
epochManagerEnabler = epochManager.epochManagerEnabler();
epochManagerEnabler = registry.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID);
firstElected = getValidators().getRegisteredValidators();

epochDuration = epochManager.epochDuration();
Expand Down Expand Up @@ -98,7 +98,7 @@ contract E2E_EpochManager_InitializeSystem is E2E_EpochManager {
}

function test_shouldRevert_WhenCalledByNonEnabler() public {
vm.expectRevert("msg.sender is not Initializer");
vm.expectRevert("msg.sender is not Enabler");
epochManager.initializeSystem(1, 1, firstElected);
}

Expand Down
38 changes: 1 addition & 37 deletions packages/protocol/test-sol/devchain/migration/Migration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,6 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants {
epochManagerEnabler = IEpochManagerEnabler(
registry.getAddressForStringOrDie("EpochManagerEnabler")
);
scoreManager = IScoreManager(registry.getAddressForStringOrDie("ScoreManager"));
election = IElection(registry.getAddressForStringOrDie("Election"));
celoUnreleasedTreasure = ICeloUnreleasedTreasure(
registry.getAddressForStringOrDie("CeloUnreleasedTreasure")
);

address scoreManagerOwner = scoreManager.owner();
vm.startPrank(scoreManagerOwner);

scoreManager.setGroupScore(groupList[0], groupScore[0]);
scoreManager.setGroupScore(groupList[1], groupScore[1]);
scoreManager.setGroupScore(groupList[2], groupScore[2]);

scoreManager.setValidatorScore(validatorsList[0], validatorScore[0]);
scoreManager.setValidatorScore(validatorsList[1], validatorScore[1]);
scoreManager.setValidatorScore(validatorsList[2], validatorScore[2]);
scoreManager.setValidatorScore(validatorsList[3], validatorScore[3]);
scoreManager.setValidatorScore(validatorsList[4], validatorScore[4]);
scoreManager.setValidatorScore(validatorsList[5], validatorScore[5]);

vm.stopPrank();

activateValidators();
vm.deal(address(celoUnreleasedTreasure), 100_000_000 ether);
}

function activateValidators() public {
Expand All @@ -233,18 +209,6 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants {
}
}

function test_IsSetupCorrect() public {
assertEq(
registry.getAddressForStringOrDie("EpochManagerEnabler"),
epochManager.epochManagerEnabler()
);
assertEq(
registry.getAddressForStringOrDie("EpochManagerEnabler"),
address(epochManagerEnabler)
);
assertEq(address(epochManagerEnabler), epochManager.epochManagerEnabler());
}

function test_Reverts_whenSystemNotInitialized() public {
vm.expectRevert("Epoch system not initialized");
epochManager.startNextEpochProcess();
Expand All @@ -267,7 +231,7 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants {
function test_Reverts_whenAlreadyInitialized() public {
_MockL2Migration(validatorsList);

vm.prank(address(0));
vm.prank(address(epochManagerEnabler));
vm.expectRevert("Epoch system already initialized");
epochManager.initializeSystem(100, block.number, firstElected);
}
Expand Down
Loading

0 comments on commit 7a10127

Please sign in to comment.