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

Deprecate random.sol #10997

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
2 changes: 0 additions & 2 deletions .github/workflows/protocol_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ jobs:

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
pahor167 marked this conversation as resolved.
Show resolved Hide resolved
version: "nightly-f625d0fa7c51e65b4bf1e8f7931cd1c6e2e285e9"

- name: Install forge dependencies
run: forge install
Expand Down
28 changes: 21 additions & 7 deletions packages/protocol/contracts-0.8/common/IsL2Check.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity >=0.8.0 <0.8.20;
pragma solidity >=0.5.13 <0.8.20;

/**
* @title Based on predeploy returns whether this is L1 or L2.
Expand All @@ -7,17 +7,31 @@ contract IsL2Check {
address constant proxyAdminAddress = 0x4200000000000000000000000000000000000018;

modifier onlyL1() {
if (IsL2()) {
revert("This method is not supported in L2 anymore.");
if (isL2()) {
revert("This method is no longer supported in L2.");
}
_;
}
modifier onlyL2() {
if (isL1()) {
revert("This method is not supported in L1.");
}
_;
}

function IsL2() public view returns (bool) {
return address(proxyAdminAddress).code.length > 0;
function isL2() public view returns (bool) {
return isContract(proxyAdminAddress);
}

function IsL1() public view returns (bool) {
return !IsL2();
function isL1() public view returns (bool) {
return !isL2();
}

function isContract(address _addr) private view returns (bool) {
uint32 size;
assembly {
size := extcodesize(_addr)
}
return (size > 0);
}
}
33 changes: 24 additions & 9 deletions packages/protocol/contracts/identity/Random.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "../common/CalledByVm.sol";
import "../common/Initializable.sol";
import "../common/UsingPrecompiles.sol";
import "../common/interfaces/ICeloVersionedContract.sol";
import "../../contracts-0.8/common/IsL2Check.sol";

/**
* @title Provides randomness for verifier selection
Expand All @@ -18,7 +19,8 @@ contract Random is
Ownable,
Initializable,
UsingPrecompiles,
CalledByVm
CalledByVm,
IsL2Check
{
using SafeMath for uint256;

Expand Down Expand Up @@ -64,24 +66,26 @@ contract Random is
bytes32 randomness,
bytes32 newCommitment,
address proposer
) external onlyVm {
) external onlyL1 onlyVm {
_revealAndCommit(randomness, newCommitment, proposer);
}

/**
* @notice Querying the current randomness value.
* @return Returns the current randomness value.
* @dev Only available on L1.
*/
function random() external view returns (bytes32) {
function random() external view onlyL1 returns (bytes32) {
return _getBlockRandomness(block.number, block.number);
}

/**
* @notice Get randomness values of previous blocks.
* @param blockNumber The number of block whose randomness value we want to know.
* @return The associated randomness value.
* @dev Only available on L1.
*/
function getBlockRandomness(uint256 blockNumber) external view returns (bytes32) {
function getBlockRandomness(uint256 blockNumber) external view onlyL1 returns (bytes32) {
return _getBlockRandomness(blockNumber, block.number);
}

Expand All @@ -93,14 +97,15 @@ contract Random is
* @return Patch version of the contract.
*/
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
return (1, 1, 1, 1);
return (1, 1, 2, 0);
}

/**
* @notice Sets the number of old random blocks whose randomness values can be queried.
* @param value Number of old random blocks whose randomness values can be queried.
* @dev Only available on L1.
*/
function setRandomnessBlockRetentionWindow(uint256 value) public onlyOwner {
function setRandomnessBlockRetentionWindow(uint256 value) public onlyL1 onlyOwner {
require(value > 0, "randomnessBlockRetetionWindow cannot be zero");
randomnessBlockRetentionWindow = value;
emit RandomnessBlockRetentionWindowSet(value);
Expand All @@ -120,8 +125,13 @@ contract Random is
* @param randomness Bytes that will be added to the entropy pool.
* @param newCommitment The hash of randomness that will be revealed in the future.
* @param proposer Address of the block proposer.
* @dev Only available on L1.
*/
function _revealAndCommit(bytes32 randomness, bytes32 newCommitment, address proposer) internal {
function _revealAndCommit(
bytes32 randomness,
bytes32 newCommitment,
address proposer
) internal onlyL1 {
require(newCommitment != computeCommitment(0), "cannot commit zero randomness");

// ensure revealed randomness matches previous commitment
Expand Down Expand Up @@ -149,8 +159,9 @@ contract Random is
* @param randomness The new randomness added to history.
* @dev The calls to this function should be made so that on the next call, blockNumber will
* be the previous one, incremented by one.
* @dev Only available on L1.
*/
function addRandomness(uint256 blockNumber, bytes32 randomness) internal {
function addRandomness(uint256 blockNumber, bytes32 randomness) internal onlyL1 {
history[blockNumber] = randomness;
if (blockNumber % getEpochSize() == 0) {
if (lastEpochBlock < historyFirst) {
Expand Down Expand Up @@ -187,8 +198,12 @@ contract Random is
* @param blockNumber The number of block whose randomness value we want to know.
* @param cur Number of the current block.
* @return The associated randomness value.
* @dev Only available on L1.
*/
function _getBlockRandomness(uint256 blockNumber, uint256 cur) internal view returns (bytes32) {
function _getBlockRandomness(
uint256 blockNumber,
uint256 cur
) internal view onlyL1 returns (bytes32) {
require(blockNumber <= cur, "Cannot query randomness of future blocks");
require(
blockNumber == lastEpochBlock ||
Expand Down
17 changes: 8 additions & 9 deletions packages/protocol/test-sol/common/IsL2Check.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,31 @@ contract IsL2CheckBase is Test {

contract IsL2Check_IsL2Test is IsL2CheckBase {
function test_IsL2() public {
assertFalse(isL2Check.IsL2());
assertFalse(isL2Check.isL2());
}

function test_IsL1() public {
assertTrue(isL2Check.IsL1());
assertTrue(isL2Check.isL1());
}

function test_IsL2_WhenProxyAdminSet() public {
helper_WhenProxyAdminAddressIsSet();
assertTrue(isL2Check.IsL2());
assertTrue(isL2Check.isL2());
}

function test_IsL1_WhenProxyAdminSet() public {
helper_WhenProxyAdminAddressIsSet();
assertFalse(isL2Check.IsL1());
assertFalse(isL2Check.isL1());
}
}

contract IsL2Check_OnlyL1 is IsL2CheckBase {
function test_WhenIsL1() public view {
isL2Check.onlyL1Function();
}

function test_WhenIsL2_WhenProxyAdminSet() public {
helper_WhenProxyAdminAddressIsSet();
vm.expectRevert("This method is not supported in L2 anymore.");
vm.expectRevert("This method is no longer supported in L2.");
isL2Check.onlyL1Function();
}
function test_WhenIsL1() public view {
isL2Check.onlyL1Function();
}
}
27 changes: 24 additions & 3 deletions packages/protocol/test-sol/identity/Random.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Utils } from "@test-sol/utils.sol";
import "@celo-contracts/identity/Random.sol";
import "@celo-contracts/identity/test/RandomTest.sol";

contract SetRandomnessRetentionWindow is Test {
contract RandomnessTest_SetRandomnessRetentionWindow is Test, IsL2Check {
event RandomnessBlockRetentionWindowSet(uint256 value);

RandomTest random;
Expand All @@ -33,9 +33,15 @@ contract SetRandomnessRetentionWindow is Test {
vm.prank(address(0x45));
random.setRandomnessBlockRetentionWindow(1000);
}

function test_Reverts_WhenCalledOnL2() public {
deployCodeTo("Registry.sol", abi.encode(false), proxyAdminAddress);
vm.expectRevert("This method is no longer supported in L2.");
random.setRandomnessBlockRetentionWindow(1000);
}
}

contract AddTestRandomness is Test, Utils {
contract RandomnessTest_AddTestRandomness is Test, Utils, IsL2Check {
uint256 constant RETENTION_WINDOW = 5;
uint256 constant EPOCH_SIZE = 10;

Expand Down Expand Up @@ -208,9 +214,17 @@ contract AddTestRandomness is Test, Utils {
vm.expectRevert("Cannot query randomness older than the stored history");
random.getTestRandomness(lastBlockOfEpoch - EPOCH_SIZE, block.number);
}

function test_Reverts_WhenCalledOnL2() public {
deployCodeTo("Registry.sol", abi.encode(false), proxyAdminAddress);
vm.expectRevert("This method is no longer supported in L2.");
random.addTestRandomness(1, 0x0000000000000000000000000000000000000000000000000000000000000001);
vm.expectRevert("This method is no longer supported in L2.");
random.getTestRandomness(1, 4);
}
}

contract RevealAndCommit is Test, Utils {
contract RandomnessTest_RevealAndCommit is Test, Utils, IsL2Check {
address constant ACCOUNT = address(0x01);
bytes32 constant RANDONMESS = bytes32(uint256(0x00));

Expand Down Expand Up @@ -246,4 +260,11 @@ contract RevealAndCommit is Test, Utils {

assertEq(random.getBlockRandomness(block.number), expected);
}

function test_Reverts_WhenCalledOnL2() public {
deployCodeTo("Registry.sol", abi.encode(false), proxyAdminAddress);
vm.expectRevert("This method is no longer supported in L2.");
blockTravel(2);
random.testRevealAndCommit(RANDONMESS, commitmentFor(0x01), ACCOUNT);
}
}
2 changes: 1 addition & 1 deletion packages/protocol/test-sol/utils/ECDSAHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract ECDSAHelper is Test {
bytes32 _s
) public returns (bytes memory) {
address SECP256K1Address = actor("SECP256K1Address");
deployCodeTo("out/SECP256K1.sol/SECP256K1.0.5.17.json", SECP256K1Address);
deployCodeTo("SECP256K1.sol:SECP256K1", SECP256K1Address);
pahor167 marked this conversation as resolved.
Show resolved Hide resolved
sECP256K1 = ISECP256K1(SECP256K1Address);

string memory header = "\x19Ethereum Signed Message:\n32";
Expand Down
53 changes: 13 additions & 40 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3786,12 +3786,10 @@
dependencies:
antlr4ts "^0.5.0-alpha.4"

"@solidity-parser/parser@^0.16.0":
version "0.16.0"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.16.0.tgz#1fb418c816ca1fc3a1e94b08bcfe623ec4e1add4"
integrity sha512-ESipEcHyRHg4Np4SqBCfcXwyxxna1DgFVz69bgpLV8vzl/NP1DtcKsJ4dJZXWQhY/Z4J2LeKBiOkOVZn9ct33Q==
dependencies:
antlr4ts "^0.5.0-alpha.4"
"@solidity-parser/parser@^0.17.0":
version "0.17.0"
resolved "https://registry.yarnpkg.com/@solidity-parser/parser/-/parser-0.17.0.tgz#52a2fcc97ff609f72011014e4c5b485ec52243ef"
integrity sha512-Nko8R0/kUo391jsEHHxrGM07QFdnPGvlmox4rmH0kNiNAashItAilhy4Mv4pK5gQmW5f4sXAF58fwJbmlkGcVw==

"@solidity-parser/parser@^0.18.0":
version "0.18.0"
Expand Down Expand Up @@ -5531,7 +5529,7 @@ abi-to-sol@^0.6.6:
source-map-support "^0.5.19"
optionalDependencies:
prettier "^2.7.1"
prettier-plugin-solidity "^1.3.1"
prettier-plugin-solidity "^1.0.0-dev.23"

abi-to-sol@^0.7.0:
version "0.7.1"
Expand All @@ -5547,7 +5545,7 @@ abi-to-sol@^0.7.0:
source-map-support "^0.5.19"
optionalDependencies:
prettier "^2.7.1"
prettier-plugin-solidity "^1.3.1"
prettier-plugin-solidity "^1.0.0-dev.23"

abitype@0.8.7:
version "0.8.7"
Expand Down Expand Up @@ -8745,11 +8743,6 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

dir-to-object@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/dir-to-object/-/dir-to-object-2.0.0.tgz#29723e9bd1c3e58e4f307bd04ff634c0370c8f8a"
integrity sha512-sXs0JKIhymON7T1UZuO2Ud6VTNAx/VTBXIl4+3mjb2RgfOpt+hectX0x04YqPOPdkeOAKoJuKqwqnXXURNPNEA==

dirty-chai@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/dirty-chai/-/dirty-chai-2.0.1.tgz#6b2162ef17f7943589da840abc96e75bda01aff3"
Expand Down Expand Up @@ -9538,13 +9531,6 @@ espree@^9.6.0, espree@^9.6.1:
acorn-jsx "^5.3.2"
eslint-visitor-keys "^3.4.1"

esprima-extract-comments@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/esprima-extract-comments/-/esprima-extract-comments-1.1.0.tgz#0dacab567a5900240de6d344cf18c33617becbc9"
integrity sha512-sBQUnvJwpeE9QnPrxh7dpI/dp67erYG4WXEAreAMoelPRpMR7NWb4YtwRPn9b+H1uLQKl/qS8WYmyaljTpjIsw==
dependencies:
esprima "^4.0.0"

esprima@2.7.x, esprima@^2.7.1:
version "2.7.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"
Expand Down Expand Up @@ -10325,14 +10311,6 @@ extglob@^0.3.0, extglob@^0.3.1:
dependencies:
is-extglob "^1.0.0"

extract-comments@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/extract-comments/-/extract-comments-1.1.0.tgz#b90bca033a056bd69b8ba1c6b6b120fc2ee95c18"
integrity sha512-dzbZV2AdSSVW/4E7Ti5hZdHWbA+Z80RJsJhr5uiL10oyjl/gy7/o+HI1HwK4/WSZhlq4SNKU3oUzXlM13Qx02Q==
dependencies:
esprima-extract-comments "^1.1.0"
parse-code-context "^1.0.0"

extsprintf@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05"
Expand Down Expand Up @@ -16584,11 +16562,6 @@ parse-cache-control@^1.0.1:
resolved "https://registry.yarnpkg.com/parse-cache-control/-/parse-cache-control-1.0.1.tgz#8eeab3e54fa56920fe16ba38f77fa21aacc2d74e"
integrity sha512-60zvsJReQPX5/QP0Kzfd/VrpjScIQ7SHBW6bFCYfEP+fp0Eppr1SHhIO5nd1PjZtvclzSzES9D/p5nFJurwfWg==

parse-code-context@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/parse-code-context/-/parse-code-context-1.0.0.tgz#718c295c593d0d19a37f898473268cc75e98de1e"
integrity sha512-OZQaqKaQnR21iqhlnPfVisFjBWjhnMl5J9MgbP8xC+EwoVqbXrq78lp+9Zb3ahmLzrIX5Us/qbvBnaS3hkH6OA==

parse-conflict-json@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/parse-conflict-json/-/parse-conflict-json-2.0.2.tgz#3d05bc8ffe07d39600dc6436c6aefe382033d323"
Expand Down Expand Up @@ -17248,7 +17221,7 @@ prettier-plugin-solidity@^1.3.1:
semver "^7.5.4"
solidity-comments-extractor "^0.0.8"

prettier@^1.14.2, prettier@^1.15.3:
prettier@^1.14.2:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
Expand Down Expand Up @@ -18508,7 +18481,7 @@ semver@7.5.2:
dependencies:
lru-cache "^6.0.0"

semver@7.x, semver@^7.0.0, semver@^7.1.1, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.3.8:
semver@7.x, semver@^7.0.0, semver@^7.1.1, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7:
version "7.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.0.tgz#ed8c5dc8efb6c629c88b23d41dc9bf40c1d96cd0"
integrity sha512-+XC0AD/R7Q2mPSRuy2Id0+CGTZ98+8f+KvwirxOKIEyid+XSx6HbC63p+O4IndTHuX5Z+JxQ0TghCkO5Cg/2HA==
Expand Down Expand Up @@ -18966,12 +18939,12 @@ solidity-bytes-utils@0.0.7:
dependencies:
truffle-hdwallet-provider "0.0.3"

solidity-comments-extractor@^0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.7.tgz#99d8f1361438f84019795d928b931f4e5c39ca19"
integrity sha512-wciNMLg/Irp8OKGrh3S2tfvZiZ0NEyILfcRCXCD4mp7SgK/i9gzLfhY2hY7VMCQJ3kH9UB9BzNdibIVMchzyYw==
solidity-comments-extractor@^0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/solidity-comments-extractor/-/solidity-comments-extractor-0.0.8.tgz#f6e148ab0c49f30c1abcbecb8b8df01ed8e879f8"
integrity sha512-htM7Vn6LhHreR+EglVMd2s+sZhcXAirB1Zlyrv5zBuTxieCvjfnRpd7iZk75m/u6NOlEyQ94C6TWbBn2cY7w8g==

solidity-parser-antlr@^0.4.11, solidity-parser-antlr@^0.4.2:
solidity-parser-antlr@^0.4.2:
version "0.4.11"
resolved "https://registry.yarnpkg.com/solidity-parser-antlr/-/solidity-parser-antlr-0.4.11.tgz#af43e1f13b3b88309a875455f5d6e565b05ee5f1"
integrity sha512-4jtxasNGmyC0midtjH/lTFPZYvTTUMy6agYcF+HoMnzW8+cqo3piFrINb4ZCzpPW+7tTVFCGa5ubP34zOzeuMg==
Expand Down
Loading