Skip to content

Commit

Permalink
Merge branch 'release/core-contracts/12' into martinvol/AddOraclesToF…
Browse files Browse the repository at this point in the history
…eeHandlersSellers
  • Loading branch information
martinvol authored Oct 15, 2024
2 parents e3432c0 + 0ae4b8c commit 291e57a
Show file tree
Hide file tree
Showing 31 changed files with 1,485 additions and 232 deletions.
361 changes: 315 additions & 46 deletions packages/protocol/contracts-0.8/common/EpochManager.sol

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import "./interfaces/IEpochManagerEnablerInitializer.sol";

contract EpochManagerEnabler is
Initializable,
UsingPrecompiles,
UsingRegistry,
UsingPrecompiles,
IEpochManagerEnabler,
IEpochManagerEnablerInitializer
{
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts-0.8/common/GasPriceMinimum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ contract GasPriceMinimum is
* @return Patch version of the contract.
*/
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
return (1, 2, 1, 0);
return (1, 2, 0, 2);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts-0.8/common/IsL2Check.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract IsL2Check {
_;
}

function isL2() public view returns (bool) {
function isL2() internal view returns (bool) {
uint32 size;
address _addr = proxyAdminAddress;
assembly {
Expand Down
73 changes: 73 additions & 0 deletions packages/protocol/contracts-0.8/common/PrecompilesOverride.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.7 <0.8.20;

import "../../contracts/common/interfaces/ICeloVersionedContract.sol";
import "../../contracts-0.8/common/IsL2Check.sol";

import "./UsingPrecompiles.sol";
import "./UsingRegistry.sol";

/**
* @title PrecompilesOverride Contract
* @notice This contract allows for a smoother transition from L1 to L2
* by abstracting away the usingPrecompile contract, and taking care of the L1 to L2 swtiching logic.
**/
contract PrecompilesOverride is UsingPrecompiles, UsingRegistry {
/**
* @notice Returns the epoch number at a block.
* @param blockNumber Block number where epoch number is calculated.
* @return Epoch number.
*/
function getEpochNumberOfBlock(uint256 blockNumber) public view override returns (uint256) {
if (isL2()) {
return getEpochManager().getEpochNumberOfBlock(blockNumber);
} else {
return epochNumberOfBlock(blockNumber, getEpochSize());
}
}

/**
* @notice Returns the epoch number at a block.
* @return Current epoch number.
*/
function getEpochNumber() public view override returns (uint256) {
return getEpochNumberOfBlock(block.number);
}

/**
* @notice Gets a validator signer address from the current validator set.
* @param index Index of requested validator in the validator set.
* @return Address of validator signer at the requested index.
*/
function validatorSignerAddressFromCurrentSet(
uint256 index
) public view override returns (address) {
if (isL2()) {
return getEpochManager().getElectedSignerByIndex(index);
} else {
super.validatorSignerAddressFromCurrentSet(index);
}
}

/**
* @notice Gets a validator address from the current validator set.
* @param index Index of requested validator in the validator set.
* @return Address of validator at the requested index.
*/

function validatorAddressFromCurrentSet(uint256 index) public view onlyL2 returns (address) {
return getEpochManager().getElectedAccountByIndex(index);
}

/**
* @notice Gets the size of the current elected validator set.
* @return Size of the current elected validator set.
*/
function numberValidatorsInCurrentSet() public view override returns (uint256) {
if (isL2()) {
return getEpochManager().numberOfElectedInCurrentSet();
} else {
return super.numberValidatorsInCurrentSet();
}
}
}
56 changes: 36 additions & 20 deletions packages/protocol/contracts-0.8/common/UsingPrecompiles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract UsingPrecompiles is IsL2Check {
* @param _decimals precision
* @return Numerator of the computed quantity (not reduced).
* @return Denominator of the computed quantity (not reduced).
* @dev This function will be deprecated in L2.
*/
function fractionMulExp(
uint256 aNumerator,
Expand All @@ -39,7 +40,7 @@ contract UsingPrecompiles is IsL2Check {
uint256 bDenominator,
uint256 exponent,
uint256 _decimals
) public view returns (uint256, uint256) {
) public view onlyL1 returns (uint256, uint256) {
require(aDenominator != 0 && bDenominator != 0, "a denominator is zero");
uint256 returnNumerator;
uint256 returnDenominator;
Expand All @@ -57,9 +58,9 @@ contract UsingPrecompiles is IsL2Check {
/**
* @notice Returns the current epoch size in blocks.
* @return The current epoch size in blocks.
* @dev This function will be deprecated in L2.
*/
function getEpochSize() public view returns (uint256) {
allowOnlyL1();
function getEpochSize() public view onlyL1 returns (uint256) {
bytes memory out;
bool success;
(success, out) = EPOCH_SIZE.staticcall(abi.encodePacked(true));
Expand All @@ -71,27 +72,30 @@ contract UsingPrecompiles is IsL2Check {
* @notice Returns the epoch number at a block.
* @param blockNumber Block number where epoch number is calculated.
* @return Epoch number.
* @dev This function will be deprecated in L2.
*/
function getEpochNumberOfBlock(uint256 blockNumber) public view returns (uint256) {
function getEpochNumberOfBlock(uint256 blockNumber) public view virtual onlyL1 returns (uint256) {
return epochNumberOfBlock(blockNumber, getEpochSize());
}

/**
* @notice Returns the epoch number at a block.
* @return Current epoch number.
* @dev This function will be deprecated in L2.
*/
function getEpochNumber() public view returns (uint256) {
function getEpochNumber() public view virtual onlyL1 returns (uint256) {
return getEpochNumberOfBlock(block.number);
}

/**
* @notice Gets a validator address from the current validator set.
* @notice Gets a validator signer address from the current validator set.
* @param index Index of requested validator in the validator set.
* @return Address of validator at the requested index.
* @return Address of validator signer at the requested index.
* @dev This function will be deprecated in L2.
*/
function validatorSignerAddressFromCurrentSet(
uint256 index
) public view virtual returns (address) {
) public view virtual onlyL1 returns (address) {
bytes memory out;
bool success;
(success, out) = GET_VALIDATOR.staticcall(abi.encodePacked(index, uint256(block.number)));
Expand All @@ -100,15 +104,16 @@ contract UsingPrecompiles is IsL2Check {
}

/**
* @notice Gets a validator address from the validator set at the given block number.
* @notice Gets a validator signer address from the validator set at the given block number.
* @param index Index of requested validator in the validator set.
* @param blockNumber Block number to retrieve the validator set from.
* @return Address of validator at the requested index.
* @return Address of validator signer at the requested index.
* @dev This function will be deprecated in L2.
*/
function validatorSignerAddressFromSet(
uint256 index,
uint256 blockNumber
) public view returns (address) {
) public view virtual onlyL1 returns (address) {
bytes memory out;
bool success;
(success, out) = GET_VALIDATOR.staticcall(abi.encodePacked(index, blockNumber));
Expand All @@ -119,8 +124,9 @@ contract UsingPrecompiles is IsL2Check {
/**
* @notice Gets the size of the current elected validator set.
* @return Size of the current elected validator set.
* @dev This function will be deprecated in L2.
*/
function numberValidatorsInCurrentSet() public view virtual returns (uint256) {
function numberValidatorsInCurrentSet() public view virtual onlyL1 returns (uint256) {
bytes memory out;
bool success;
(success, out) = NUMBER_VALIDATORS.staticcall(abi.encodePacked(uint256(block.number)));
Expand All @@ -132,8 +138,9 @@ contract UsingPrecompiles is IsL2Check {
* @notice Gets the size of the validator set that must sign the given block number.
* @param blockNumber Block number to retrieve the validator set from.
* @return Size of the validator set.
* @dev This function will be deprecated in L2.
*/
function numberValidatorsInSet(uint256 blockNumber) public view virtual returns (uint256) {
function numberValidatorsInSet(uint256 blockNumber) public view virtual onlyL1 returns (uint256) {
bytes memory out;
bool success;
(success, out) = NUMBER_VALIDATORS.staticcall(abi.encodePacked(blockNumber));
Expand All @@ -149,12 +156,13 @@ contract UsingPrecompiles is IsL2Check {
* @param blsPop The BLS public key proof-of-possession, which consists of a signature on the
* account address. 96 bytes.
* @return True upon success.
* @dev This function will be deprecated in L2.
*/
function checkProofOfPossession(
address sender,
bytes memory blsKey,
bytes memory blsPop
) public view returns (bool) {
) public view onlyL1 returns (bool) {
bool success;
(success, ) = PROOF_OF_POSSESSION.staticcall(abi.encodePacked(sender, blsKey, blsPop));
return success;
Expand All @@ -164,8 +172,9 @@ contract UsingPrecompiles is IsL2Check {
* @notice Parses block number out of header.
* @param header RLP encoded header
* @return Block number.
* @dev This function will be deprecated in L2.
*/
function getBlockNumberFromHeader(bytes memory header) public view returns (uint256) {
function getBlockNumberFromHeader(bytes memory header) public view onlyL1 returns (uint256) {
bytes memory out;
bool success;
(success, out) = BLOCK_NUMBER_FROM_HEADER.staticcall(abi.encodePacked(header));
Expand All @@ -177,8 +186,9 @@ contract UsingPrecompiles is IsL2Check {
* @notice Computes hash of header.
* @param header RLP encoded header
* @return Header hash.
* @dev This function will be deprecated in L2.
*/
function hashHeader(bytes memory header) public view returns (bytes32) {
function hashHeader(bytes memory header) public view onlyL1 returns (bytes32) {
bytes memory out;
bool success;
(success, out) = HASH_HEADER.staticcall(abi.encodePacked(header));
Expand All @@ -190,8 +200,9 @@ contract UsingPrecompiles is IsL2Check {
* @notice Gets the parent seal bitmap from the header at the given block number.
* @param blockNumber Block number to retrieve. Must be within 4 epochs of the current number.
* @return Bitmap parent seal with set bits at indices corresponding to signing validators.
* @dev This function will be deprecated in L2.
*/
function getParentSealBitmap(uint256 blockNumber) public view returns (bytes32) {
function getParentSealBitmap(uint256 blockNumber) public view onlyL1 returns (bytes32) {
bytes memory out;
bool success;
(success, out) = GET_PARENT_SEAL_BITMAP.staticcall(abi.encodePacked(blockNumber));
Expand All @@ -205,8 +216,11 @@ contract UsingPrecompiles is IsL2Check {
* header. If the parent hash is not in the blockchain, verification fails.
* @param header RLP encoded header
* @return Bitmap parent seal with set bits at indices correspoinding to signing validators.
* @dev This function will be deprecated in L2.
*/
function getVerifiedSealBitmapFromHeader(bytes memory header) public view returns (bytes32) {
function getVerifiedSealBitmapFromHeader(
bytes memory header
) public view onlyL1 returns (bytes32) {
bytes memory out;
bool success;
(success, out) = GET_VERIFIED_SEAL_BITMAP.staticcall(abi.encodePacked(header));
Expand All @@ -217,16 +231,18 @@ contract UsingPrecompiles is IsL2Check {
/**
* @notice Returns the minimum number of required signers for a given block number.
* @dev Computed in celo-blockchain as int(math.Ceil(float64(2*valSet.Size()) / 3))
* @dev This function will be deprecated in L2.
*/
function minQuorumSize(uint256 blockNumber) public view returns (uint256) {
function minQuorumSize(uint256 blockNumber) public view onlyL1 returns (uint256) {
return numberValidatorsInSet(blockNumber).mul(2).add(2).div(3);
}

/**
* @notice Computes byzantine quorum from current validator set size
* @return Byzantine quorum of validators.
* @dev This function will be deprecated in L2.
*/
function minQuorumSizeInCurrentSet() public view returns (uint256) {
function minQuorumSizeInCurrentSet() public view onlyL1 returns (uint256) {
return minQuorumSize(block.number);
}

Expand Down
Loading

0 comments on commit 291e57a

Please sign in to comment.