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

Adds backwards compatibility in UsingPrecompile contract. #11236

Open
wants to merge 44 commits into
base: release/core-contracts/12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f3c3193
added `getEpochInfoOfEpoch(uint256)` to get epoch info of specific ep…
soloseng Oct 2, 2024
6d059e9
updated natspec
soloseng Oct 2, 2024
7a4f8a4
PR feedback
soloseng Oct 3, 2024
2e30ff6
Use constant
soloseng Oct 3, 2024
fca97c1
breakup test; added more
soloseng Oct 3, 2024
ae767fe
++ registry to precompile contract
soloseng Oct 4, 2024
9e5043e
Merge branch 'soloseng/epoch-of-block' into soloseng/backward-compati…
soloseng Oct 4, 2024
dd39947
added elected to epoch info
soloseng Oct 4, 2024
ece3579
numberValidatorsInSet backwards compatibility
soloseng Oct 4, 2024
accf227
validator address & validatorSigner address from set
soloseng Oct 4, 2024
6b7cf95
support `getEpochNumber()` backwards compatibility in UsingRegistry
soloseng Oct 4, 2024
feeb9e3
using `getEpochByBlockNumber` instead of `getEpochByNumber`
soloseng Oct 4, 2024
f22201d
limit span of loop only to L2 blocks
soloseng Oct 4, 2024
69a84e6
use binary search instead of blind loop
soloseng Oct 4, 2024
fd36439
++ natspec
soloseng Oct 4, 2024
50bec5c
added getEpochNumberOfBlock() backwards compatibility support
soloseng Oct 4, 2024
6732448
++ electedSigners
soloseng Oct 8, 2024
d786a56
make usingPrecompile onlyL1 contract
soloseng Oct 8, 2024
eaeeb8b
++ precompile override contract
soloseng Oct 8, 2024
1dcd131
fix epoch manager bug and mock
soloseng Oct 8, 2024
b6ac1f2
using precompileOverride in validators contract
soloseng Oct 8, 2024
6205b5f
wording
soloseng Oct 8, 2024
d84205d
using indexed function when querying specific validator accounts or s…
soloseng Oct 8, 2024
162d850
Merge branch 'release/core-contracts/12' into soloseng/UsingRegistry-…
soloseng Oct 8, 2024
35316a0
fixed failing test
soloseng Oct 9, 2024
a0c976a
using independent mapping for elected accounts and signers
soloseng Oct 9, 2024
86f74d9
removed old elected list
soloseng Oct 10, 2024
681a858
support 0.5.13 contracts using precompiles
soloseng Oct 10, 2024
6648ad4
-- registry import
soloseng Oct 10, 2024
f0a4fc3
++ registry in PrecompilesOverride
soloseng Oct 10, 2024
ae1577f
++ more tests
soloseng Oct 10, 2024
17566dc
increase gaslimit
soloseng Oct 10, 2024
23b0aa3
moved election history update to epoch processing start.
soloseng Oct 10, 2024
f645c26
unused var
soloseng Oct 10, 2024
96e45a4
clean up
soloseng Oct 10, 2024
04d46ee
fixed e2e test
soloseng Oct 11, 2024
a4f380f
simplify code
soloseng Oct 11, 2024
de140aa
Merge branch 'release/core-contracts/12' into soloseng/UsingRegistry-…
soloseng Oct 11, 2024
1e52b15
Merge branch 'release/core-contracts/12' into soloseng/UsingRegistry-…
martinvol Oct 14, 2024
f3b7a51
Removed support for election historical data after that it's unlikely…
soloseng Oct 14, 2024
c15fc93
PR feedback
soloseng Oct 14, 2024
78ea07f
-- spacing
soloseng Oct 14, 2024
954b346
reorder state var
soloseng Oct 14, 2024
baf0e79
stat var reorder
soloseng Oct 14, 2024
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
282 changes: 255 additions & 27 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
116 changes: 116 additions & 0 deletions packages/protocol/contracts-0.8/common/PrecompilesOverride.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// 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 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 signer at the requested index.
*/
function validatorSignerAddressFromSet(
uint256 index,
uint256 blockNumber
) public view override returns (address) {
if (isL2()) {
return getEpochManager().getElectedSignerAddressFromSet(index, blockNumber);
} else {
return super.validatorSignerAddressFromSet(index, blockNumber);
}
}

/**
* @notice Gets a validator 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.
*/
function validatorAddressFromSet(
uint256 index,
uint256 blockNumber
) public view onlyL2 returns (address) {
return getEpochManager().getElectedAccountAddressFromSet(index, blockNumber);
}

/**
* @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();
}
}

/**
* @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.
*/
function numberValidatorsInSet(uint256 blockNumber) public view override returns (uint256) {
if (isL2()) {
return getEpochManager().numberOfElectedInSet(blockNumber);
} else {
return super.numberValidatorsInSet(blockNumber);
}
}
}
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
Loading