Skip to content

Commit

Permalink
++ setter for oracle address
Browse files Browse the repository at this point in the history
  • Loading branch information
soloseng committed Sep 24, 2024
1 parent 4e4decf commit bb38303
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/protocol/contracts-0.8/common/EpochManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ contract EpochManager is

uint256 public firstKnownEpoch;
uint256 private currentEpochNumber;
address public oracleAddress;
address[] public elected;

mapping(address => ProcessedGroup) public processedGroups;
Expand All @@ -77,6 +78,7 @@ contract EpochManager is
*/
event EpochProcessingEnded(uint256 indexed epochNumber);
event EpochDurationSet(uint256 indexed newEpochDuration);
event OracleAddressSet(address indexed newOracleAddress);

/**
* @notice Emitted when an epoch payment is sent.
Expand Down Expand Up @@ -121,6 +123,7 @@ contract EpochManager is
_transferOwnership(msg.sender);
setRegistry(registryAddress);
setEpochDuration(newEpochDuration);
setOracleAddress(registry.getAddressForOrDie(SORTED_ORACLES_REGISTRY_ID));
}

// DESIGNDESICION(XXX): we assume that the first epoch on the L2 starts as soon as the system is initialized
Expand Down Expand Up @@ -368,11 +371,25 @@ contract EpochManager is
* @dev Can only be set by owner.
*/
function setEpochDuration(uint256 newEpochDuration) public onlyOwner {
require(newEpochDuration > 0, "New epoch duration must be greater than zero.");
require(!isOnEpochProcess(), "Cannot change epoch duration during processing.");
epochDuration = newEpochDuration;
emit EpochDurationSet(newEpochDuration);
}

/**
* @notice Sets the address of the Oracle used by this contract.
* @param newOracleAddress The address of the new oracle.
* @dev Can only be set by owner.
*/
function setOracleAddress(address newOracleAddress) public onlyOwner {
require(newOracleAddress != address(0), "Cannot set address zero as the Oracle.");
require(newOracleAddress != oracleAddress, "Oracle address cannot be the same.");
require(!isOnEpochProcess(), "Cannot change oracle address during epoch processing.");
oracleAddress = newOracleAddress;
emit OracleAddressSet(newOracleAddress);
}

function isTimeForNextEpoch() public view returns (bool) {
return block.timestamp >= epochs[currentEpochNumber].startTimestamp + epochDuration;
}
Expand Down Expand Up @@ -409,9 +426,8 @@ contract EpochManager is

// Mint all cUSD required for payment and the corresponding CELO
validators.mintStableToEpochManager(totalRewards);
// this should have a setter for the oracle.

(uint256 numerator, uint256 denominator) = IOracle(address(getSortedOracles())).getExchangeRate(
(uint256 numerator, uint256 denominator) = IOracle(oracleAddress).getExchangeRate(
address(getStableToken())
);

Expand Down
49 changes: 49 additions & 0 deletions packages/protocol/test-sol/unit/common/EpochManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ contract EpochManagerTest is Test, TestConstants, Utils08 {

event EpochProcessingStarted(uint256 indexed epochNumber);
event EpochDurationSet(uint256 indexed newEpochDuration);
event OracleAddressSet(address indexed newOracleAddress);

function setUp() public virtual {
epochManager = new EpochManager_WithMocks();
Expand Down Expand Up @@ -132,6 +133,7 @@ contract EpochManagerTest_initialize is EpochManagerTest {
function test_initialize() public virtual {
assertEq(address(epochManager.registry()), REGISTRY_ADDRESS);
assertEq(epochManager.epochDuration(), 10);
assertEq(epochManager.oracleAddress(), address(sortedOracles));
}

function test_Reverts_WhenAlreadyInitialized() public virtual {
Expand Down Expand Up @@ -246,6 +248,7 @@ contract EpochManagerTest_startNextEpochProcess is EpochManagerTest {
assertEq(reserveBalanceAfter, reserveBalanceBefore + 4);
}
}

contract EpochManagerTest_setEpochDuration is EpochManagerTest {
uint256 newEpochDuration = 5 * DAY;

Expand All @@ -269,6 +272,52 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest {
vm.expectRevert("Cannot change epoch duration during processing.");
epochManager.setEpochDuration(newEpochDuration);
}

function test_Reverts_WhenNewEpochDurationIsZero() public {
initializeEpochManagerSystem();

vm.expectRevert("New epoch duration must be greater than zero.");
epochManager.setEpochDuration(0);
}
}

contract EpochManagerTest_setOracleAddress is EpochManagerTest {
address newOracleAddress = actor("newOarcle");

function test_setsNewOracleAddress() public {
initializeEpochManagerSystem();
epochManager.setOracleAddress(newOracleAddress);
assertEq(epochManager.oracleAddress(), newOracleAddress);
}

function test_Emits_OracleAddressSetEvent() public {
initializeEpochManagerSystem();

vm.expectEmit(true, true, true, true);
emit OracleAddressSet(newOracleAddress);
epochManager.setOracleAddress(newOracleAddress);
}

function test_Reverts_WhenIsOnEpochProcess() public {
initializeEpochManagerSystem();
epochManager.startNextEpochProcess();
vm.expectRevert("Cannot change oracle address during epoch processing.");
epochManager.setOracleAddress(newOracleAddress);
}

function test_Reverts_WhenNewOracleAddressIsZero() public {
initializeEpochManagerSystem();

vm.expectRevert("Cannot set address zero as the Oracle.");
epochManager.setOracleAddress(address(0));
}

function test_Reverts_WhenNewOracleAddressIsunchanged() public {
initializeEpochManagerSystem();

vm.expectRevert("Oracle address cannot be the same.");
epochManager.setOracleAddress(address(sortedOracles));
}
}

contract EpochManagerTest_sendValidatorPayment is EpochManagerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract EpochManagerEnablerTest is Test, TestConstants, Utils08 {

address accountsAddress;
address nonOwner;
address oracle;

uint256 epochDuration = DAY;
uint256 numberValidators = 100;
Expand All @@ -50,6 +51,7 @@ contract EpochManagerEnablerTest is Test, TestConstants, Utils08 {
accountsAddress = actor("accountsAddress");

nonOwner = actor("nonOwner");
oracle = actor("oracle");

deployCodeTo("MockRegistry.sol", abi.encode(false), REGISTRY_ADDRESS);
deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress);
Expand All @@ -61,6 +63,7 @@ contract EpochManagerEnablerTest is Test, TestConstants, Utils08 {
registry.setAddressFor(EpochManagerEnablerContract, address(epochManagerEnabler));
registry.setAddressFor(AccountsContract, address(accounts));
registry.setAddressFor(CeloTokenContract, address(celoToken));
registry.setAddressFor(SortedOraclesContract, oracle);
registry.setAddressFor(CeloUnreleasedTreasuryContract, address(celoUnreleasedTreasury));

celoToken.setTotalSupply(CELO_SUPPLY_CAP);
Expand Down

0 comments on commit bb38303

Please sign in to comment.