Skip to content

Commit

Permalink
added getEpochNumberOfBlock() backwards compatibility support
Browse files Browse the repository at this point in the history
  • Loading branch information
soloseng committed Oct 4, 2024
1 parent fd36439 commit 50bec5c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 56 deletions.
144 changes: 92 additions & 52 deletions packages/protocol/contracts-0.8/common/EpochManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ contract EpochManager is
return epochs[epoch].lastBlock;
}

/**
* @notice Returns the epoch number of a specified blockNumber.
* @param _blockNumber Block number of the epoch info is retreived.
*/
function getEpochNumberOfBlock(uint256 _blockNumber) external view returns (uint256) {
(uint256 _epochNumber, , , , , ) = _getEpochByBlockNumber(_blockNumber);
return _epochNumber;
}

/**
* @notice Returns the epoch info of a specified blockNumber.
* @param _blockNumber Block number of the epoch info is retreived.
* @return firstEpoch The first block of the given block number.
* @return lastBlock The first block of the given block number.
* @return startTimestamp The starting timestamp of the given block number.
* @return rewardsBlock The reward block of the given block number.
* @return elected The set of elected validator for the given block number.
*/
function getEpochByBlockNumber(
uint256 _blockNumber
) external view returns (uint256, uint256, uint256, uint256, address[] memory) {
(
,
uint256 _firstBlock,
uint256 _lastBlock,
uint256 _startTimestamp,
uint256 _rewardsBlock,
address[] memory _elected
) = _getEpochByBlockNumber(_blockNumber);
return (_firstBlock, _lastBlock, _startTimestamp, _rewardsBlock, _elected);
}

/**
* @notice Returns the storage, major, minor, and patch version of the contract.
* @return Storage version of the contract.
Expand Down Expand Up @@ -486,31 +518,62 @@ contract EpochManager is
);
}

/**
* @notice Allocates rewards to elected validator accounts.
*/
function allocateValidatorsRewards() internal {
uint256 totalRewards = 0;
IScoreReader scoreReader = getScoreReader();
IValidators validators = getValidators();

EpochProcessState storage _epochProcessing = epochProcessing;

for (uint i = 0; i < elected.length; i++) {
uint256 validatorScore = scoreReader.getValidatorScore(elected[i]);
uint256 validatorReward = validators.computeEpochReward(
elected[i],
validatorScore,
_epochProcessing.perValidatorReward
);
validatorPendingPayments[elected[i]] += validatorReward;
totalRewards += validatorReward;
}
if (totalRewards == 0) {
return;
}

// Mint all cUSD required for payment and the corresponding CELO
validators.mintStableToEpochManager(totalRewards);

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

uint256 CELOequivalent = (numerator * totalRewards) / denominator;
getCeloUnreleasedTreasury().release(
registry.getAddressForOrDie(RESERVE_REGISTRY_ID),
CELOequivalent
);
}

/**
* @notice Returns the epoch info of a specified blockNumber.
* @param blockNumber Block number of the epoch info is retreived.
* @param _blockNumber Block number of the epoch info is retreived.
* @return firstEpoch The first block of the given block number.
* @return lastBlock The first block of the given block number.
* @return startTimestamp The starting timestamp of the given block number.
* @return rewardsBlock The reward block of the given block number.
* @return elected The set of elected validator for the given block number.
*/
function getEpochByBlockNumber(
function _getEpochByBlockNumber(
uint256 _blockNumber
)
public
internal
view
onlySystemAlreadyInitialized
returns (
uint256 epochNumber,
uint256 firstBlock,
uint256 lastBlock,
uint256 startTimestamp,
uint256 rewardsBlock,
address[] memory elected
)
returns (uint256, uint256, uint256, uint256, uint256, address[] memory)
{
require(_blockNumber <= block.number(), "Invalid blockNumber. Value too high.");
require(_blockNumber <= block.number, "Invalid blockNumber. Value too high.");

(uint256 _firstBlockOfFirstEpoch, , , , ) = getEpochByNumber(firstKnownEpoch);

Expand All @@ -519,7 +582,21 @@ contract EpochManager is
uint256 _firstBlockOfCurrentEpoch = epochs[currentEpochNumber].firstBlock;

if (_blockNumber > _firstBlockOfCurrentEpoch) {
return getCurrentEpoch();
(
uint256 _firstBlock,
uint256 _lastBlock,
uint256 _startTimestamp,
uint256 _rewardsBlock,
address[] memory _elected
) = getEpochByNumber(currentEpochNumber);
return (
currentEpochNumber,
_firstBlock,
_lastBlock,
_startTimestamp,
_rewardsBlock,
_elected
);
}

uint256 left = firstKnownEpoch;
Expand All @@ -529,7 +606,7 @@ contract EpochManager is
uint256 mid = (left + right) / 2;
Epoch memory _epoch = epochs[mid];

if (_blockNumber >= _epoch.firstBlock && blockNumber <= _epoch.lastBlock) {
if (_blockNumber >= _epoch.firstBlock && _blockNumber <= _epoch.lastBlock) {
return (
mid,
_epoch.firstBlock,
Expand All @@ -538,7 +615,7 @@ contract EpochManager is
_epoch.rewardsBlock,
_epoch.elected
);
} else if (blockNumber < _epoch.firstBlock) {
} else if (_blockNumber < _epoch.firstBlock) {
right = mid - 1;
} else {
left = mid + 1;
Expand All @@ -547,41 +624,4 @@ contract EpochManager is

revert("No matching epoch found for the given block number.");
}
/**
* @notice Allocates rewards to elected validator accounts.
*/
function allocateValidatorsRewards() internal {
uint256 totalRewards = 0;
IScoreReader scoreReader = getScoreReader();
IValidators validators = getValidators();

EpochProcessState storage _epochProcessing = epochProcessing;

for (uint i = 0; i < elected.length; i++) {
uint256 validatorScore = scoreReader.getValidatorScore(elected[i]);
uint256 validatorReward = validators.computeEpochReward(
elected[i],
validatorScore,
_epochProcessing.perValidatorReward
);
validatorPendingPayments[elected[i]] += validatorReward;
totalRewards += validatorReward;
}
if (totalRewards == 0) {
return;
}

// Mint all cUSD required for payment and the corresponding CELO
validators.mintStableToEpochManager(totalRewards);

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

uint256 CELOequivalent = (numerator * totalRewards) / denominator;
getCeloUnreleasedTreasury().release(
registry.getAddressForOrDie(RESERVE_REGISTRY_ID),
CELOequivalent
);
}
}
7 changes: 5 additions & 2 deletions packages/protocol/contracts-0.8/common/UsingPrecompiles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ contract UsingPrecompiles is IsL2Check, UsingRegistry {
* @return Epoch number.
*/
function getEpochNumberOfBlock(uint256 blockNumber) public view returns (uint256) {
allowOnlyL1();
return epochNumberOfBlock(blockNumber, getEpochSize());
if (isL2()) {
return getEpochManager().getEpochNumberOfBlock(blockNumber);
} else {
return epochNumberOfBlock(blockNumber, getEpochSize());
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/protocol/contracts-0.8/common/test/MockEpochManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ contract MockEpochManager is IEpochManager {
return isProcessingEpoch;
}

function getEpochByBlockNumber(
uint256 _blockNumber
) external view returns (uint256, uint256, uint256, uint256, address[] memory) {
address[] memory _elected = new address[](0);

return (0, 0, 0, 0, _elected);
}

function getEpochNumberOfBlock(uint256 _blockNumber) external view returns (uint256) {
return 0;
}

function sendValidatorPayment(address validator) public {
emit SendValidatorPaymentCalled(validator);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/protocol/contracts/common/UsingPrecompiles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ contract UsingPrecompiles is IsL2Check, UsingRegistry {
* @return Epoch number.
*/
function getEpochNumberOfBlock(uint256 blockNumber) public view returns (uint256) {
allowOnlyL1();
return epochNumberOfBlock(blockNumber, getEpochSize());
if (isL2()) {
return getEpochManager().getEpochNumberOfBlock(blockNumber);
} else {
return epochNumberOfBlock(blockNumber, getEpochSize());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface IEpochManager {
function getEpochByNumber(
uint256 epochNumber
) external view returns (uint256, uint256, uint256, uint256, address[] memory);
function getEpochByBlockNumber(
uint256 blockNumber
) external view returns (uint256, uint256, uint256, uint256, address[] memory);
function getEpochNumberOfBlock(uint256) external view returns (uint256);
function getCurrentEpochNumber() external view returns (uint256);
function getElected() external view returns (address[] memory);
function epochDuration() external view returns (uint256);
Expand Down

0 comments on commit 50bec5c

Please sign in to comment.