From 4f4a917c6130e997a7f5efe78285bf235acc6120 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:38:55 -0500 Subject: [PATCH 01/23] WIP inheritance issues ? --- .../common/PrecompilesOverrideV2.sol | 72 +++++++ .../contracts-0.8/common/UsingRegistry2.sol | 156 ++++++++++++++ .../contracts-0.8/common/UsingRegistryV2.sol | 200 ++++++++++++++++++ .../devchain/e2e/common/EpochManager.t.sol | 6 +- .../e2e/common/FeeCurrencyDirectory.t.sol | 4 +- .../protocol/test-sol/devchain/e2e/utils.sol | 7 +- .../migration/IntegrationValidators.t.sol | 4 +- .../devchain/migration/Migration.t.sol | 6 +- .../test-sol/unit/common/EpochManager.t.sol | 6 +- .../unit/common/EpochManagerEnabler.t.sol | 6 +- .../unit/common/FeeCurrencyDirectory.t.sol | 15 +- .../test-sol/unit/common/ProxyFactory08.t.sol | 4 +- .../protocol/test-sol/utils/WhenL2-08.sol | 11 + packages/protocol/test-sol/utils08.sol | 51 ++++- 14 files changed, 520 insertions(+), 28 deletions(-) create mode 100644 packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol create mode 100644 packages/protocol/contracts-0.8/common/UsingRegistry2.sol create mode 100644 packages/protocol/contracts-0.8/common/UsingRegistryV2.sol create mode 100644 packages/protocol/test-sol/utils/WhenL2-08.sol diff --git a/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol b/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol new file mode 100644 index 00000000000..d3bf0a75337 --- /dev/null +++ b/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol @@ -0,0 +1,72 @@ +// 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 "./UsingRegistryV2.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 PrecompilesOverrideV2 is UsingPrecompiles, UsingRegistryV2 { + /** + * @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 { + return 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(); + } + } +} diff --git a/packages/protocol/contracts-0.8/common/UsingRegistry2.sol b/packages/protocol/contracts-0.8/common/UsingRegistry2.sol new file mode 100644 index 00000000000..a9bfcc8d407 --- /dev/null +++ b/packages/protocol/contracts-0.8/common/UsingRegistry2.sol @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: LGPL-3.0-only +pragma solidity >=0.8.0 <0.8.20; + +// Note: This is not an exact copy of UsingRegistry or UsingRegistryV2 in the contract's folder +// because Mento's interfaces still don't support Solidity 0.8 + +import "@openzeppelin/contracts8/access/Ownable.sol"; +import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; + +import "../../contracts/common/interfaces/IRegistry.sol"; +import "../../contracts/common/interfaces/IAccounts.sol"; +import "../../contracts/common/interfaces/IEpochManager.sol"; +import "../../contracts/common/interfaces/IFreezer.sol"; +import "../../contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; +import "../../contracts/common/interfaces/IFeeCurrencyWhitelist.sol"; +import "../../contracts/common/interfaces/IFeeHandlerSeller.sol"; +import "../../contracts/common/interfaces/IEpochManager.sol"; +import "../../contracts/governance/interfaces/IGovernance.sol"; +import "../../contracts/governance/interfaces/ILockedGold.sol"; +import "../../contracts/governance/interfaces/ILockedCelo.sol"; +import "../../contracts/governance/interfaces/IValidators.sol"; +import "../../contracts/governance/interfaces/IElection.sol"; +import "../../contracts/governance/interfaces/IEpochRewards.sol"; +import "../../contracts/stability/interfaces/ISortedOracles.sol"; + +import "./interfaces/IScoreReader.sol"; + +contract UsingRegistryy is Ownable { + // solhint-disable state-visibility + bytes32 constant ACCOUNTS_REGISTRY_ID = keccak256(abi.encodePacked("Accounts")); + bytes32 constant ATTESTATIONS_REGISTRY_ID = keccak256(abi.encodePacked("Attestations")); + bytes32 constant DOWNTIME_SLASHER_REGISTRY_ID = keccak256(abi.encodePacked("DowntimeSlasher")); + bytes32 constant DOUBLE_SIGNING_SLASHER_REGISTRY_ID = + keccak256(abi.encodePacked("DoubleSigningSlasher")); + bytes32 constant ELECTION_REGISTRY_ID = keccak256(abi.encodePacked("Election")); + bytes32 constant EPOCH_REWARDS_REGISTRY_ID = keccak256(abi.encodePacked("EpochRewards")); + bytes32 constant EXCHANGE_REGISTRY_ID = keccak256(abi.encodePacked("Exchange")); + bytes32 constant FEE_CURRENCY_WHITELIST_REGISTRY_ID = + keccak256(abi.encodePacked("FeeCurrencyWhitelist")); + bytes32 constant FREEZER_REGISTRY_ID = keccak256(abi.encodePacked("Freezer")); + bytes32 constant GOLD_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("GoldToken")); + bytes32 constant GOVERNANCE_REGISTRY_ID = keccak256(abi.encodePacked("Governance")); + bytes32 constant GOVERNANCE_SLASHER_REGISTRY_ID = + keccak256(abi.encodePacked("GovernanceSlasher")); + bytes32 constant LOCKED_GOLD_REGISTRY_ID = keccak256(abi.encodePacked("LockedGold")); + bytes32 constant RESERVE_REGISTRY_ID = keccak256(abi.encodePacked("Reserve")); + bytes32 constant RANDOM_REGISTRY_ID = keccak256(abi.encodePacked("Random")); + bytes32 constant SORTED_ORACLES_REGISTRY_ID = keccak256(abi.encodePacked("SortedOracles")); + bytes32 constant STABLE_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("StableToken")); + bytes32 constant VALIDATORS_REGISTRY_ID = keccak256(abi.encodePacked("Validators")); + bytes32 constant MENTOFEEHANDLERSELLER_REGISTRY_ID = + keccak256(abi.encodePacked("MentoFeeHandlerSeller")); + bytes32 constant CELO_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("CeloToken")); + bytes32 constant LOCKED_CELO_REGISTRY_ID = keccak256(abi.encodePacked("LockedCelo")); + bytes32 constant CELO_UNRELEASED_TREASURY_REGISTRY_ID = + keccak256(abi.encodePacked("CeloUnreleasedTreasury")); + bytes32 constant EPOCH_MANAGER_ENABLER_REGISTRY_ID = + keccak256(abi.encodePacked("EpochManagerEnabler")); + bytes32 constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager")); + bytes32 constant SCORE_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("ScoreManager")); + // solhint-enable state-visibility + + IRegistry public registry; + + event RegistrySet(address indexed registryAddress); + + modifier onlyRegisteredContract(bytes32 identifierHash) { + require(registry.getAddressForOrDie(identifierHash) == msg.sender, "only registered contract"); + _; + } + + modifier onlyRegisteredContracts(bytes32[] memory identifierHashes) { + require(registry.isOneOf(identifierHashes, msg.sender), "only registered contracts"); + _; + } + + /** + * @notice Updates the address pointing to a Registry contract. + * @param registryAddress The address of a registry contract for routing to other contracts. + */ + function setRegistry(address registryAddress) public onlyOwner { + require(registryAddress != address(0), "Cannot register the null address"); + registry = IRegistry(registryAddress); + emit RegistrySet(registryAddress); + } + + function getGoldToken() internal view returns (IERC20) { + return IERC20(registry.getAddressForOrDie(GOLD_TOKEN_REGISTRY_ID)); + } + + function getCeloToken() internal view returns (IERC20) { + return IERC20(registry.getAddressForOrDie(CELO_TOKEN_REGISTRY_ID)); + } + + function getFreezer() internal view returns (IFreezer) { + return IFreezer(registry.getAddressForOrDie(FREEZER_REGISTRY_ID)); + } + + function getSortedOracles() internal view returns (ISortedOracles) { + return ISortedOracles(registry.getAddressForOrDie(SORTED_ORACLES_REGISTRY_ID)); + } + + function getFeeCurrencyWhitelist() internal view returns (IFeeCurrencyWhitelist) { + return IFeeCurrencyWhitelist(registry.getAddressForOrDie(FEE_CURRENCY_WHITELIST_REGISTRY_ID)); + } + + function getLockedGold() internal view returns (ILockedGold) { + return ILockedGold(registry.getAddressForOrDie(LOCKED_GOLD_REGISTRY_ID)); + } + + function getLockedCelo() internal view returns (ILockedCelo) { + return ILockedCelo(registry.getAddressForOrDie(LOCKED_CELO_REGISTRY_ID)); + } + + // Current version of Mento doesn't support 0.8 + function getStableToken() internal view returns (address) { + return registry.getAddressForOrDie(STABLE_TOKEN_REGISTRY_ID); + } + + function getMentoFeeHandlerSeller() internal view returns (IFeeHandlerSeller) { + return IFeeHandlerSeller(registry.getAddressForOrDie(MENTOFEEHANDLERSELLER_REGISTRY_ID)); + } + + function getAccounts() internal view returns (IAccounts) { + return IAccounts(registry.getAddressForOrDie(ACCOUNTS_REGISTRY_ID)); + } + + function getValidators() internal view returns (IValidators) { + return IValidators(registry.getAddressForOrDie(VALIDATORS_REGISTRY_ID)); + } + + function getElection() internal view returns (IElection) { + return IElection(registry.getAddressForOrDie(ELECTION_REGISTRY_ID)); + } + + function getEpochRewards() internal view returns (IEpochRewards) { + return IEpochRewards(registry.getAddressForOrDie(EPOCH_REWARDS_REGISTRY_ID)); + } + + function getGovernance() internal view returns (IGovernance) { + return IGovernance(registry.getAddressForOrDie(GOVERNANCE_REGISTRY_ID)); + } + + function getCeloUnreleasedTreasury() internal view returns (ICeloUnreleasedTreasury) { + return + ICeloUnreleasedTreasury(registry.getAddressForOrDie(CELO_UNRELEASED_TREASURY_REGISTRY_ID)); + } + + function getEpochManager() internal view returns (IEpochManager) { + return IEpochManager(registry.getAddressForOrDie(EPOCH_MANAGER_REGISTRY_ID)); + } + + function getScoreReader() internal view returns (IScoreReader) { + return IScoreReader(registry.getAddressForOrDie(SCORE_MANAGER_REGISTRY_ID)); + } +} diff --git a/packages/protocol/contracts-0.8/common/UsingRegistryV2.sol b/packages/protocol/contracts-0.8/common/UsingRegistryV2.sol new file mode 100644 index 00000000000..deea520916d --- /dev/null +++ b/packages/protocol/contracts-0.8/common/UsingRegistryV2.sol @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: LGPL-3.0-only +pragma solidity >=0.8.0 <0.8.20; + +import "@openzeppelin/contracts8/access/Ownable.sol"; +import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; + +import "../../contracts/common/interfaces/IAccounts.sol"; +import "../../contracts/common/interfaces/IEpochManager.sol"; +import "../../contracts/common/interfaces/IFeeCurrencyWhitelist.sol"; +import "../../contracts/common/interfaces/IFreezer.sol"; +import "../../contracts/common/interfaces/IRegistry.sol"; +import "../../contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; + +import "../../contracts/governance/interfaces/IElection.sol"; +import "../../contracts/governance/interfaces/IEpochRewards.sol"; +import "../../contracts/governance/interfaces/IGovernance.sol"; +import "../../contracts/governance/interfaces/ILockedGold.sol"; +import "../../contracts/governance/interfaces/ILockedCelo.sol"; +import "../../contracts/governance/interfaces/IValidators.sol"; + +import "../../contracts/identity/interfaces/IRandom.sol"; +import "../../contracts/identity/interfaces/IAttestations.sol"; +import "../../contracts/identity/interfaces/IFederatedAttestations.sol"; + +// import "../../lib/mento-core/contracts/interfaces/IExchange.sol"; +// import "../../lib/mento-core/contracts/interfaces/IReserve.sol"; +// import "../../lib/mento-core/contracts/interfaces/IStableToken.sol"; +import "../../contracts/stability/interfaces/ISortedOracles.sol"; + +contract UsingRegistryV2 { + address internal constant registryAddress = 0x000000000000000000000000000000000000ce10; + IRegistry public constant registryContract = IRegistry(registryAddress); + + bytes32 internal constant ACCOUNTS_REGISTRY_ID = keccak256(abi.encodePacked("Accounts")); + bytes32 internal constant ATTESTATIONS_REGISTRY_ID = keccak256(abi.encodePacked("Attestations")); + bytes32 internal constant DOWNTIME_SLASHER_REGISTRY_ID = + keccak256(abi.encodePacked("DowntimeSlasher")); + bytes32 internal constant DOUBLE_SIGNING_SLASHER_REGISTRY_ID = + keccak256(abi.encodePacked("DoubleSigningSlasher")); + bytes32 internal constant ELECTION_REGISTRY_ID = keccak256(abi.encodePacked("Election")); + bytes32 internal constant EXCHANGE_REGISTRY_ID = keccak256(abi.encodePacked("Exchange")); + bytes32 internal constant EXCHANGE_EURO_REGISTRY_ID = keccak256(abi.encodePacked("ExchangeEUR")); + bytes32 internal constant EXCHANGE_REAL_REGISTRY_ID = keccak256(abi.encodePacked("ExchangeBRL")); + + bytes32 internal constant FEE_CURRENCY_WHITELIST_REGISTRY_ID = + keccak256(abi.encodePacked("FeeCurrencyWhitelist")); + bytes32 internal constant FEDERATED_ATTESTATIONS_REGISTRY_ID = + keccak256(abi.encodePacked("FederatedAttestations")); + bytes32 internal constant FREEZER_REGISTRY_ID = keccak256(abi.encodePacked("Freezer")); + bytes32 internal constant GOLD_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("GoldToken")); + bytes32 internal constant GOVERNANCE_REGISTRY_ID = keccak256(abi.encodePacked("Governance")); + bytes32 internal constant GOVERNANCE_SLASHER_REGISTRY_ID = + keccak256(abi.encodePacked("GovernanceSlasher")); + bytes32 internal constant LOCKED_GOLD_REGISTRY_ID = keccak256(abi.encodePacked("LockedGold")); + bytes32 internal constant RESERVE_REGISTRY_ID = keccak256(abi.encodePacked("Reserve")); + bytes32 internal constant RANDOM_REGISTRY_ID = keccak256(abi.encodePacked("Random")); + bytes32 internal constant SORTED_ORACLES_REGISTRY_ID = + keccak256(abi.encodePacked("SortedOracles")); + bytes32 internal constant STABLE_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("StableToken")); + bytes32 internal constant STABLE_EURO_TOKEN_REGISTRY_ID = + keccak256(abi.encodePacked("StableTokenEUR")); + bytes32 internal constant STABLE_REAL_TOKEN_REGISTRY_ID = + keccak256(abi.encodePacked("StableTokenBRL")); + bytes32 internal constant VALIDATORS_REGISTRY_ID = keccak256(abi.encodePacked("Validators")); + bytes32 internal constant CELO_UNRELEASED_TREASURY_REGISTRY_ID = + keccak256(abi.encodePacked("CeloUnreleasedTreasury")); + + bytes32 internal constant CELO_TOKEN_REGISTRY_ID = keccak256(abi.encodePacked("CeloToken")); + bytes32 internal constant LOCKED_CELO_REGISTRY_ID = keccak256(abi.encodePacked("LockedCelo")); + bytes32 internal constant EPOCH_REWARDS_REGISTRY_ID = keccak256(abi.encodePacked("EpochRewards")); + bytes32 internal constant EPOCH_MANAGER_ENABLER_REGISTRY_ID = + keccak256(abi.encodePacked("EpochManagerEnabler")); + bytes32 internal constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager")); + + modifier onlyRegisteredContract(bytes32 identifierHash) { + require( + registryContract.getAddressForOrDie(identifierHash) == msg.sender, + "only registered contract" + ); + _; + } + + modifier onlyRegisteredContracts(bytes32[] memory identifierHashes) { + require(registryContract.isOneOf(identifierHashes, msg.sender), "only registered contracts"); + _; + } + + function getAccounts() internal view returns (IAccounts) { + return IAccounts(registryContract.getAddressForOrDie(ACCOUNTS_REGISTRY_ID)); + } + + function getAttestations() internal view returns (IAttestations) { + return IAttestations(registryContract.getAddressForOrDie(ATTESTATIONS_REGISTRY_ID)); + } + + function getElection() internal view returns (IElection) { + return IElection(registryContract.getAddressForOrDie(ELECTION_REGISTRY_ID)); + } + + // function getExchange() internal view returns (IExchange) { + // return IExchange(registryContract.getAddressForOrDie(EXCHANGE_REGISTRY_ID)); + // } + + // function getExchangeDollar() internal view returns (IExchange) { + // return getExchange(); + // } + + // function getExchangeEuro() internal view returns (IExchange) { + // return IExchange(registryContract.getAddressForOrDie(EXCHANGE_EURO_REGISTRY_ID)); + // } + + // function getExchangeREAL() internal view returns (IExchange) { + // return IExchange(registryContract.getAddressForOrDie(EXCHANGE_REAL_REGISTRY_ID)); + // } + + function getFeeCurrencyWhitelistRegistry() internal view returns (IFeeCurrencyWhitelist) { + return + IFeeCurrencyWhitelist( + registryContract.getAddressForOrDie(FEE_CURRENCY_WHITELIST_REGISTRY_ID) + ); + } + + function getFederatedAttestations() internal view returns (IFederatedAttestations) { + return + IFederatedAttestations( + registryContract.getAddressForOrDie(FEDERATED_ATTESTATIONS_REGISTRY_ID) + ); + } + + function getFreezer() internal view returns (IFreezer) { + return IFreezer(registryContract.getAddressForOrDie(FREEZER_REGISTRY_ID)); + } + + function getGoldToken() internal view returns (IERC20) { + return IERC20(registryContract.getAddressForOrDie(GOLD_TOKEN_REGISTRY_ID)); + } + + function getCeloToken() internal view returns (IERC20) { + return IERC20(registryContract.getAddressForOrDie(CELO_TOKEN_REGISTRY_ID)); + } + + function getGovernance() internal view returns (IGovernance) { + return IGovernance(registryContract.getAddressForOrDie(GOVERNANCE_REGISTRY_ID)); + } + + function getLockedGold() internal view returns (ILockedGold) { + return ILockedGold(registryContract.getAddressForOrDie(LOCKED_GOLD_REGISTRY_ID)); + } + + function getLockedCelo() internal view returns (ILockedCelo) { + return ILockedCelo(registryContract.getAddressForOrDie(LOCKED_CELO_REGISTRY_ID)); + } + + function getRandom() internal view returns (IRandom) { + return IRandom(registryContract.getAddressForOrDie(RANDOM_REGISTRY_ID)); + } + + // function getReserve() internal view returns (IReserve) { + // return IReserve(registryContract.getAddressForOrDie(RESERVE_REGISTRY_ID)); + // } + + function getSortedOracles() internal view returns (ISortedOracles) { + return ISortedOracles(registryContract.getAddressForOrDie(SORTED_ORACLES_REGISTRY_ID)); + } + + // function getStableToken() internal view returns (IStableToken) { + // return IStableToken(registryContract.getAddressForOrDie(STABLE_TOKEN_REGISTRY_ID)); + // } + + // function getStableDollarToken() internal view returns (IStableToken) { + // return getStableToken(); + // } + + // function getStableEuroToken() internal view returns (IStableToken) { + // return IStableToken(registryContract.getAddressForOrDie(STABLE_EURO_TOKEN_REGISTRY_ID)); + // } + + // function getStableRealToken() internal view returns (IStableToken) { + // return IStableToken(registryContract.getAddressForOrDie(STABLE_REAL_TOKEN_REGISTRY_ID)); + // } + + function getValidators() internal view returns (IValidators) { + return IValidators(registryContract.getAddressForOrDie(VALIDATORS_REGISTRY_ID)); + } + + function getCeloUnreleasedTreasury() internal view returns (ICeloUnreleasedTreasury) { + return + ICeloUnreleasedTreasury( + registryContract.getAddressForOrDie(CELO_UNRELEASED_TREASURY_REGISTRY_ID) + ); + } + + function getEpochRewards() internal view returns (IEpochRewards) { + return IEpochRewards(registryContract.getAddressForOrDie(EPOCH_REWARDS_REGISTRY_ID)); + } + + function getEpochManager() internal view returns (IEpochManager) { + return IEpochManager(registryContract.getAddressForOrDie(EPOCH_MANAGER_REGISTRY_ID)); + } +} diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index 6283865af02..02fbb11ebf4 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -1,9 +1,9 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; +// import { Utils08 } from "@test-sol/utils08.sol"; import { IEpochManager } from "@celo-contracts/common/interfaces/IEpochManager.sol"; @@ -12,7 +12,7 @@ import "@test-sol/utils/ECDSAHelper08.sol"; import "@openzeppelin/contracts8/utils/structs/EnumerableSet.sol"; import { console } from "forge-std/console.sol"; -contract E2E_EpochManager is Test, Devchain, Utils08, ECDSAHelper08 { +contract E2E_EpochManager is Devchain, ECDSAHelper08 { using EnumerableSet for EnumerableSet.AddressSet; struct VoterWithPK { diff --git a/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol b/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol index f613eb2574a..bebf0ed4f44 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol @@ -1,12 +1,12 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; -contract E2EDemo is Test, Devchain { +contract E2EDemo is Devchain { function test_ShouldAllowOwnerSetCurrencyConfig() public { address token = address(1); uint256 intrinsicGas = 21000; diff --git a/packages/protocol/test-sol/devchain/e2e/utils.sol b/packages/protocol/test-sol/devchain/e2e/utils.sol index 0e84cfafa26..0c6798dbcb4 100644 --- a/packages/protocol/test-sol/devchain/e2e/utils.sol +++ b/packages/protocol/test-sol/devchain/e2e/utils.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "@celo-contracts-8/common/UsingRegistry.sol"; +// import "@celo-contracts-8/common/UsingRegistry2.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; import { IEpochManager } from "@celo-contracts/common/interfaces/IEpochManager.sol"; import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; @@ -15,9 +15,10 @@ import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts/stability/interfaces/ISortedOracles.sol"; import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; -import { TestConstants } from "@test-sol/constants.sol"; +// import { TestConstants } from "@test-sol/constants.sol"; +import "@test-sol/utils08.sol"; -contract Devchain is UsingRegistry, TestConstants { +contract Devchain is Utils08 { // Used in exceptional circumstances when a contract is not in UsingRegistry.sol IRegistry devchainRegistry = IRegistry(REGISTRY_ADDRESS); diff --git a/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol b/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol index 2ba650dffe7..24f89201956 100644 --- a/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol +++ b/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol @@ -1,10 +1,10 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; -contract IntegrationsValidators is Test, Devchain { +contract IntegrationsValidators is Devchain { function test_deaffiliateWorskWithEpochManager() public { vm.prank(election.electValidatorAccounts()[0]); validators.deaffiliate(); diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index bfe56896280..9caa8da42d5 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -1,10 +1,10 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; -import { TestConstants } from "@test-sol/constants.sol"; +// import { Utils08 } from "@test-sol/utils08.sol"; +// import { TestConstants } from "@test-sol/constants.sol"; import { MigrationsConstants } from "@migrations-sol/constants.sol"; import { FeeCurrencyDirectory } from "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index bebd3999b10..2a9083bb553 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/stability/test/MockStableToken.sol"; import "@celo-contracts-8/common/test/MockCeloToken.sol"; @@ -9,7 +9,7 @@ import "@celo-contracts/common/interfaces/ICeloToken.sol"; import "@celo-contracts-8/common/ScoreManager.sol"; import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; -import { TestConstants } from "@test-sol/constants.sol"; +// import { TestConstants } from "@test-sol/constants.sol"; import { Utils08 } from "@test-sol/utils08.sol"; import "@celo-contracts/stability/test/MockSortedOracles.sol"; @@ -26,7 +26,7 @@ import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/Valid import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; import { console } from "forge-std/console.sol"; -contract EpochManagerTest is Test, TestConstants, Utils08 { +contract EpochManagerTest is Utils08 { EpochManager_WithMocks epochManager; MockSortedOracles sortedOracles; diff --git a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol index 3e91fc2e121..320598d9618 100644 --- a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import "@celo-contracts-8/common/EpochManager.sol"; import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; @@ -10,7 +10,7 @@ import { CeloUnreleasedTreasury } from "@celo-contracts-8/common/CeloUnreleasedT import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; -import { TestConstants } from "@test-sol/constants.sol"; +// import { TestConstants } from "@test-sol/constants.sol"; import { Utils08 } from "@test-sol/utils08.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; @@ -20,7 +20,7 @@ import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/Valid import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; import "@celo-contracts-8/common/test/MockCeloToken.sol"; -contract EpochManagerEnablerTest is Test, TestConstants, Utils08 { +contract EpochManagerEnablerTest is Utils08 { EpochManager epochManager; EpochManagerEnablerMock epochManagerEnabler; MockCeloUnreleasedTreasury celoUnreleasedTreasury; diff --git a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol index fd4161cfba5..0cd6b598bce 100644 --- a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol +++ b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol @@ -1,17 +1,20 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import "celo-foundry-8/Test.sol"; +import { Utils08 } from "@test-sol/utils08.sol"; +import "@test-sol/utils/WhenL2-08.sol"; + import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts-8/common/mocks/MockOracle.sol"; -contract FeeCurrencyDirectoryTestBase is Test { +contract FeeCurrencyDirectoryTest is Utils08 { FeeCurrencyDirectory directory; MockOracle oracle; address nonOwner; address owner; function setUp() public virtual { + super.setUp(); owner = address(this); nonOwner = actor("nonOwner"); oracle = new MockOracle(); @@ -21,7 +24,9 @@ contract FeeCurrencyDirectoryTestBase is Test { } } -contract TestSetCurrencyConfig is FeeCurrencyDirectoryTestBase { +contract FeeCurrencyDirectoryTest_L2 is FeeCurrencyDirectoryTest, WhenL2 {} + +contract TestSetCurrencyConfig is FeeCurrencyDirectoryTest { function test_ShouldAllowOwnerSetCurrencyConfig() public { address token = address(1); uint256 intrinsicGas = 21000; @@ -63,7 +68,7 @@ contract TestSetCurrencyConfig is FeeCurrencyDirectoryTestBase { } } -contract TestRemoveCurrencies is FeeCurrencyDirectoryTestBase { +contract TestRemoveCurrencies is FeeCurrencyDirectoryTest { function setUp() public override { super.setUp(); address token = address(4); @@ -99,7 +104,7 @@ contract TestRemoveCurrencies is FeeCurrencyDirectoryTestBase { } } -contract TestGetExchangeRate is FeeCurrencyDirectoryTestBase { +contract TestGetExchangeRate is FeeCurrencyDirectoryTest { address token; function setUp() public override { diff --git a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol index c290e570b67..bab9e029569 100644 --- a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol +++ b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol @@ -1,12 +1,12 @@ pragma solidity ^0.8.15; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; import "@celo-contracts-8/common/ProxyFactory08.sol"; import "@celo-contracts/common/interfaces/IProxy.sol"; import { Utils08 } from "@test-sol/utils08.sol"; -contract ProxyFactoryTest is Test, Utils08 { +contract ProxyFactoryTest is Utils08 { ProxyFactory08 proxyFactory08; bytes proxyInitCode; address constant owner = address(0xAA963FC97281d9632d96700aB62A4D1340F9a28a); diff --git a/packages/protocol/test-sol/utils/WhenL2-08.sol b/packages/protocol/test-sol/utils/WhenL2-08.sol new file mode 100644 index 00000000000..ec8af5f5e5f --- /dev/null +++ b/packages/protocol/test-sol/utils/WhenL2-08.sol @@ -0,0 +1,11 @@ +pragma solidity >=0.5.13 <0.9.0; +pragma experimental ABIEncoderV2; + +import "@test-sol/utils08.sol"; + +contract WhenL2 is Utils08 { + function setUp() public { + super.setUp(); + whenL2WithEpochManagerInitialization(); + } +} diff --git a/packages/protocol/test-sol/utils08.sol b/packages/protocol/test-sol/utils08.sol index c9495b72a23..7f633642b87 100644 --- a/packages/protocol/test-sol/utils08.sol +++ b/packages/protocol/test-sol/utils08.sol @@ -3,9 +3,33 @@ pragma solidity >=0.5.13 <0.9.0; import "celo-foundry-8/Test.sol"; import { TestConstants } from "@test-sol/constants.sol"; -contract Utils08 is TestConstants { +// import "@celo-contracts-8/common/EpochManager.sol"; +import "@test-sol/unit/common/mocks/MockEpochManager.sol"; +import "@celo-contracts/common/interfaces/IRegistry.sol"; +import "@celo-contracts-8/common/IsL2Check.sol"; +import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; + +contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { + IRegistry registry; + MockEpochManager public epochManager; uint256 public constant secondsInOneBlock = 5; + function setUp() public { + setupRegistry(); + setupEpochManager(); + } + + function setupRegistry() public { + deployCodeTo("Registry.sol", abi.encode(false), REGISTRY_ADDRESS); + registry = IRegistry(REGISTRY_ADDRESS); + } + + function setupEpochManager() public { + epochManager = new MockEpochManager(); + + registry.setAddressFor(EpochManagerContract, address(epochManager)); + } + function timeTravel(Vm vm, uint256 timeDelta) public { vm.warp(block.timestamp + timeDelta); } @@ -29,7 +53,16 @@ contract Utils08 is TestConstants { timeTravel(vm, n * DAY); } - function whenL2(Vm vm) public { + function travelNEpoch(Vm vm, uint256 n) public { + if (isL2()) { + travelNL2Epoch(vm, n); + } else { + // blockTravel((n * ph.epochSize()) + 1); + travelEpochL1(vm); + } + } + + function whenL2() public { vm.etch(0x4200000000000000000000000000000000000018, abi.encodePacked(bytes1(0x01))); } @@ -44,4 +77,18 @@ contract Utils08 is TestConstants { function compareStrings(string memory a, string memory b) public pure returns (bool) { return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); } + + function whenL2WithEpochManagerInitialization() internal { + uint256 l1EpochNumber = getEpochNumber(); + address epochManagerEnabler = actor("EpochManagerEnabler"); + registry.setAddressFor(EpochManagerContract, address(epochManager)); + + address[] memory _elected = new address[](2); + _elected[0] = actor("validator"); + _elected[1] = actor("otherValidator"); + + whenL2(); + vm.prank(epochManagerEnabler); + epochManager.initializeSystem(l1EpochNumber, block.number, _elected); + } } From 4a2c7f759eabfa7ff6f4a66bb6525ace873bdeee Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:58:24 -0500 Subject: [PATCH 02/23] fixing compilation --- packages/protocol/test-sol/constants.sol | 11 +++++++---- .../test-sol/unit/common/FeeCurrencyDirectory.t.sol | 8 ++++++-- .../test-sol/unit/common/ProxyFactory08.t.sol | 3 ++- packages/protocol/test-sol/utils/WhenL2-08.sol | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/protocol/test-sol/constants.sol b/packages/protocol/test-sol/constants.sol index e6f6a0e15d3..0b54aa9cb8a 100644 --- a/packages/protocol/test-sol/constants.sol +++ b/packages/protocol/test-sol/constants.sol @@ -6,10 +6,13 @@ contract TestConstants { uint256 public constant FIXED1 = 1e24; uint256 public constant MINUTE = 60; uint256 public constant HOUR = 60 * MINUTE; - uint256 public constant DAY = 24 * HOUR; - uint256 public constant MONTH = 30 * DAY; - uint256 constant WEEK = 7 * DAY; - uint256 public constant YEAR = 365 * DAY; + // uint256 public constant DAY = 24 * HOUR; + uint256 public constant MONTH = 30 * 86400; + // uint256 public constant MONTH = 30 * DAY; + uint256 constant WEEK = 7 * 86400; + // uint256 constant WEEK = 7 * DAY; + uint256 public constant YEAR = 365 * 86400; + // uint256 public constant YEAR = 365 * DAY; uint256 public constant L2_BLOCK_IN_EPOCH = 43200; // Contract names diff --git a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol index cbfd1e3a73b..c053f1d6b48 100644 --- a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol +++ b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol @@ -15,7 +15,7 @@ contract FeeCurrencyDirectoryTest is Utils08 { event CurrencyConfigSet(address indexed token, address indexed oracle, uint256 intrinsicGas); event CurrencyRemoved(address indexed token); - function setUp() public virtual { + function setUp() public virtual override { super.setUp(); owner = address(this); nonOwner = actor("nonOwner"); @@ -26,7 +26,11 @@ contract FeeCurrencyDirectoryTest is Utils08 { } } -contract FeeCurrencyDirectoryTest_L2 is FeeCurrencyDirectoryTest, WhenL2 {} +contract FeeCurrencyDirectoryTest_L2 is FeeCurrencyDirectoryTest, WhenL2 { + function setUp() public override(FeeCurrencyDirectoryTest, WhenL2) { + super.setUp(); + } +} contract TestSetCurrencyConfig is FeeCurrencyDirectoryTest { function test_ShouldAllowOwnerSetCurrencyConfig() public { diff --git a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol index bab9e029569..a365aeff8ed 100644 --- a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol +++ b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol @@ -11,7 +11,8 @@ contract ProxyFactoryTest is Utils08 { bytes proxyInitCode; address constant owner = address(0xAA963FC97281d9632d96700aB62A4D1340F9a28a); - function setUp() public { + function setUp() public override { + super.setUp(); proxyFactory08 = new ProxyFactory08(); proxyInitCode = vm.getCode("Proxy.sol"); } diff --git a/packages/protocol/test-sol/utils/WhenL2-08.sol b/packages/protocol/test-sol/utils/WhenL2-08.sol index ec8af5f5e5f..9057091c52a 100644 --- a/packages/protocol/test-sol/utils/WhenL2-08.sol +++ b/packages/protocol/test-sol/utils/WhenL2-08.sol @@ -4,7 +4,7 @@ pragma experimental ABIEncoderV2; import "@test-sol/utils08.sol"; contract WhenL2 is Utils08 { - function setUp() public { + function setUp() public virtual override { super.setUp(); whenL2WithEpochManagerInitialization(); } From d8a532f554ad820a3013add3d17c881a54cefc48 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 20 Dec 2024 21:52:34 -0500 Subject: [PATCH 03/23] removed mento from UsingRegistryV2 in 08 --- .../common/PrecompilesOverrideV2.sol | 4 +- ...istryV2.sol => UsingRegistryV2NoMento.sol} | 12 +++++- packages/protocol/test-sol/utils08.sol | 40 +++++++++++-------- 3 files changed, 37 insertions(+), 19 deletions(-) rename packages/protocol/contracts-0.8/common/{UsingRegistryV2.sol => UsingRegistryV2NoMento.sol} (94%) diff --git a/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol b/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol index d3bf0a75337..4279e197fb4 100644 --- a/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol +++ b/packages/protocol/contracts-0.8/common/PrecompilesOverrideV2.sol @@ -5,14 +5,14 @@ import "../../contracts/common/interfaces/ICeloVersionedContract.sol"; import "../../contracts-0.8/common/IsL2Check.sol"; import "./UsingPrecompiles.sol"; -import "./UsingRegistryV2.sol"; +import "./UsingRegistryV2NoMento.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 PrecompilesOverrideV2 is UsingPrecompiles, UsingRegistryV2 { +contract PrecompilesOverrideV2 is UsingPrecompiles, UsingRegistryV2NoMento { /** * @notice Returns the epoch number at a block. * @param blockNumber Block number where epoch number is calculated. diff --git a/packages/protocol/contracts-0.8/common/UsingRegistryV2.sol b/packages/protocol/contracts-0.8/common/UsingRegistryV2NoMento.sol similarity index 94% rename from packages/protocol/contracts-0.8/common/UsingRegistryV2.sol rename to packages/protocol/contracts-0.8/common/UsingRegistryV2NoMento.sol index deea520916d..64144c917b3 100644 --- a/packages/protocol/contracts-0.8/common/UsingRegistryV2.sol +++ b/packages/protocol/contracts-0.8/common/UsingRegistryV2NoMento.sol @@ -1,9 +1,14 @@ // SPDX-License-Identifier: LGPL-3.0-only pragma solidity >=0.8.0 <0.8.20; +// Note: This is not an exact copy of UsingRegistry or UsingRegistryV2 in the contract's folder +// because Mento's interfaces still don't support Solidity 0.8 + import "@openzeppelin/contracts8/access/Ownable.sol"; import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; +import "./interfaces/IScoreReader.sol"; + import "../../contracts/common/interfaces/IAccounts.sol"; import "../../contracts/common/interfaces/IEpochManager.sol"; import "../../contracts/common/interfaces/IFeeCurrencyWhitelist.sol"; @@ -27,7 +32,7 @@ import "../../contracts/identity/interfaces/IFederatedAttestations.sol"; // import "../../lib/mento-core/contracts/interfaces/IStableToken.sol"; import "../../contracts/stability/interfaces/ISortedOracles.sol"; -contract UsingRegistryV2 { +contract UsingRegistryV2NoMento { address internal constant registryAddress = 0x000000000000000000000000000000000000ce10; IRegistry public constant registryContract = IRegistry(registryAddress); @@ -71,6 +76,7 @@ contract UsingRegistryV2 { bytes32 internal constant EPOCH_MANAGER_ENABLER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManagerEnabler")); bytes32 internal constant EPOCH_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("EpochManager")); + bytes32 internal constant SCORE_MANAGER_REGISTRY_ID = keccak256(abi.encodePacked("ScoreManager")); modifier onlyRegisteredContract(bytes32 identifierHash) { require( @@ -197,4 +203,8 @@ contract UsingRegistryV2 { function getEpochManager() internal view returns (IEpochManager) { return IEpochManager(registryContract.getAddressForOrDie(EPOCH_MANAGER_REGISTRY_ID)); } + + function getScoreReader() internal view returns (IScoreReader) { + return IScoreReader(registryContract.getAddressForOrDie(SCORE_MANAGER_REGISTRY_ID)); + } } diff --git a/packages/protocol/test-sol/utils08.sol b/packages/protocol/test-sol/utils08.sol index 7f633642b87..ba055feb832 100644 --- a/packages/protocol/test-sol/utils08.sol +++ b/packages/protocol/test-sol/utils08.sol @@ -1,6 +1,7 @@ pragma solidity >=0.5.13 <0.9.0; -import "celo-foundry-8/Test.sol"; +// import "celo-foundry-8/Test.sol"; +import { Test as ForgeTest } from "@lib/celo-foundry-8/lib/forge-std/src/Test.sol"; import { TestConstants } from "@test-sol/constants.sol"; // import "@celo-contracts-8/common/EpochManager.sol"; @@ -9,12 +10,13 @@ import "@celo-contracts/common/interfaces/IRegistry.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; -contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { +contract Utils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { + // contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { IRegistry registry; MockEpochManager public epochManager; uint256 public constant secondsInOneBlock = 5; - function setUp() public { + function setUp() public virtual { setupRegistry(); setupEpochManager(); } @@ -30,35 +32,34 @@ contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { registry.setAddressFor(EpochManagerContract, address(epochManager)); } - function timeTravel(Vm vm, uint256 timeDelta) public { + function timeTravel(uint256 timeDelta) public { vm.warp(block.timestamp + timeDelta); } - function blockTravel(Vm vm, uint256 blockDelta) public { + function blockTravel(uint256 blockDelta) public { vm.roll(block.number + blockDelta); } - function travelEpochL1(Vm vm) public { + function travelEpochL1() public { uint256 blocksInEpoch = 17280; uint256 timeDelta = blocksInEpoch * 5; - blockTravel(vm, blocksInEpoch); - timeTravel(vm, timeDelta); + blockTravel(blocksInEpoch); + timeTravel(timeDelta); } // XXX: this function only increases the block number and timestamp, but does not actually change epoch. // XXX: you must start and finish epoch processing to change epochs. - function travelNL2Epoch(Vm vm, uint256 n) public { + function travelNL2Epoch(uint256 n) public { uint256 blocksInEpoch = L2_BLOCK_IN_EPOCH; - blockTravel(vm, n * blocksInEpoch); - timeTravel(vm, n * DAY); + blockTravel(n * blocksInEpoch); + timeTravel(n * DAY); } - function travelNEpoch(Vm vm, uint256 n) public { + function travelNEpoch(uint256 n) public { if (isL2()) { - travelNL2Epoch(vm, n); + travelNL2Epoch(n); } else { - // blockTravel((n * ph.epochSize()) + 1); - travelEpochL1(vm); + travelEpochL1(); } } @@ -66,7 +67,14 @@ contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { vm.etch(0x4200000000000000000000000000000000000018, abi.encodePacked(bytes1(0x01))); } - function actorWithPK(Vm vm, string memory name) public returns (address, uint256) { + function actor(string memory name) public returns (address) { + uint256 pk = uint256(keccak256(bytes(name))); + address addr = vm.addr(pk); + vm.label(addr, name); + return addr; + } + + function actorWithPK(string memory name) public returns (address, uint256) { uint256 pk = uint256(keccak256(bytes(name))); address addr = vm.addr(pk); vm.label(addr, name); From afe5356840a6e33d638154b95dc1080863aa5e06 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:57:57 -0500 Subject: [PATCH 04/23] using local precompile handler --- .../test-sol/utils/PrecompileHandler.sol | 178 ++++++++++++++++++ packages/protocol/test-sol/utils08.sol | 19 +- 2 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 packages/protocol/test-sol/utils/PrecompileHandler.sol diff --git a/packages/protocol/test-sol/utils/PrecompileHandler.sol b/packages/protocol/test-sol/utils/PrecompileHandler.sol new file mode 100644 index 00000000000..286eb1f4c5c --- /dev/null +++ b/packages/protocol/test-sol/utils/PrecompileHandler.sol @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.5.13 <0.8.20; + +// Note: This is contract is a copy of `PrecompileHandler` in celo-foundry, but uses `UsingPrecompile` instead of `Precompiles`. +// This contract is to be removed/deprecated once the transition to L2 is live on mainnet. + +import "@lib/celo-foundry-8/lib/forge-std/src/Vm.sol"; +import "@lib/celo-foundry-8/lib/forge-std/src/console2.sol"; +import "@celo-contracts-8/common/UsingPrecompiles.sol"; + +contract PrecompileHandler is UsingPrecompiles { + address private constant VM_ADDRESS = + address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))); + Vm public constant _vm = Vm(VM_ADDRESS); + + bytes4 constant TRANSFER_SIG = bytes4(keccak256("transfer(address,address,uint256)")); + bytes4 constant EPOCH_SIZE_SIG = bytes4(keccak256("epochSize()")); + bytes4 constant CATCHALL_SIG = bytes4(keccak256("catchAll()")); + + uint256 public epochSize = 17280; + bool public debug = false; + + struct Mock { + bool success; + bytes returnData; + bool exists; + } + + mapping(address => mapping(bytes32 => Mock)) private mockedCalls; + bytes private _empty; + Mock private successMock = Mock(true, _empty, true); + Mock private revertMock = Mock(true, _empty, true); + + constructor() public { + _vm.etch(TRANSFER, proxyTo(TRANSFER_SIG)); + _vm.label(TRANSFER, "TRANSFER"); + + _vm.etch(EPOCH_SIZE, proxyTo(EPOCH_SIZE_SIG)); + _vm.label(EPOCH_SIZE, "EPOCH_SIZE"); + + bytes memory catchAllProxy = proxyTo(CATCHALL_SIG); + _vm.etch(FRACTION_MUL, catchAllProxy); + _vm.label(FRACTION_MUL, "FRACTION_MUL"); + + _vm.etch(PROOF_OF_POSSESSION, catchAllProxy); + _vm.label(PROOF_OF_POSSESSION, "PROOF_OF_POSSESSION"); + + _vm.etch(GET_VALIDATOR, catchAllProxy); + _vm.label(GET_VALIDATOR, "GET_VALIDATOR"); + + _vm.etch(NUMBER_VALIDATORS, catchAllProxy); + _vm.label(NUMBER_VALIDATORS, "NUMBER_VALIDATORS"); + + _vm.etch(BLOCK_NUMBER_FROM_HEADER, catchAllProxy); + _vm.label(BLOCK_NUMBER_FROM_HEADER, "BLOCK_NUMBER_FROM_HEADER"); + + _vm.etch(HASH_HEADER, catchAllProxy); + _vm.label(HASH_HEADER, "HASH_HEADER"); + + _vm.etch(GET_PARENT_SEAL_BITMAP, catchAllProxy); + _vm.label(GET_PARENT_SEAL_BITMAP, "GET_PARENT_SEAL_BITMAP"); + + _vm.etch(GET_VERIFIED_SEAL_BITMAP, catchAllProxy); + _vm.label(GET_VERIFIED_SEAL_BITMAP, "GET_VERIFIED_SEAL_BITMAP"); + } + + function transfer(address from, address to, uint256 amount) public returns (bool) { + _vm.deal(from, from.balance - amount); + _vm.deal(to, to.balance + amount); + return true; + } + + function setEpochSize(uint256 epochSize_) public { + epochSize = epochSize_; + } + + function setDebug(bool debug_) public { + debug = debug_; + } + + function mockSuccess(address prec, bytes32 callHash) public { + setMock(prec, callHash, successMock); + + if (debug) { + console2.log("Mock success"); + console2.log(prec); + console2.logBytes32(callHash); + } + } + + function mockRevert(address prec, bytes32 callHash) public { + setMock(prec, callHash, revertMock); + + if (debug) { + console2.log("Mock revert"); + console2.log(prec); + console2.logBytes32(callHash); + } + } + + function mockReturn(address prec, bytes32 callHash, bytes memory returnData) public { + setMock(prec, callHash, Mock(true, returnData, true)); + + if (debug) { + console2.log("Mock success with data"); + console2.log(prec); + console2.logBytes32(callHash); + console2.logBytes(returnData); + } + } + + function clearMock(address prec, bytes32 callHash) public { + delete mockedCalls[prec][callHash]; + } + + function setMock(address prec, bytes32 callHash, Mock memory mock) internal { + require(prec >= GET_VERIFIED_SEAL_BITMAP && prec <= TRANSFER, "precompile not supported"); + + mockedCalls[prec][callHash] = mock; + } + + function catchAll() public view { + bytes memory cd; + assembly { + cd := mload(0x40) + let cds := sub(calldatasize(), 0x4) + mstore(cd, cds) + calldatacopy(add(cd, 0x20), 0x4, cds) + mstore(0x40, add(cd, add(cds, 0x20))) + } + + bytes32 cdh = keccak256(cd); + Mock memory mock = mockedCalls[msg.sender][cdh]; + + if (mock.exists == false) { + console2.log(msg.sender); + console2.logBytes(cd); + console2.logBytes32(cdh); + revert("mock not defined for call"); + } + + if (mock.success == false) { + revert(); + } + + if (mock.returnData.length > 0) { + bytes memory returnData = mock.returnData; + assembly { + let rds := mload(returnData) + return(add(returnData, 0x20), rds) + } + } + } + + function proxyTo(bytes4 sig) internal view returns (bytes memory) { + address prec = address(this); + bytes memory ptr; + + assembly { + ptr := mload(0x40) + mstore(ptr, 0x60) + let mc := add(ptr, 0x20) + let addrPrefix := shl(0xf8, 0x73) + let addr := shl(0x58, prec) + let sigPrefix := shl(0x50, 0x63) + let shiftedSig := shl(0x30, shr(0xe0, sig)) + let suffix := 0x600060043601 + mstore(mc, or(addrPrefix, or(addr, or(sigPrefix, or(shiftedSig, suffix))))) + mc := add(mc, 0x20) + mstore(mc, 0x8260e01b82523660006004840137600080828434885af13d6000816000823e82) + mc := add(mc, 0x20) + mstore(mc, 0x60008114604a578282f35b8282fd000000000000000000000000000000000000) + mstore(0x40, add(ptr, 0x80)) + } + + return ptr; + } +} diff --git a/packages/protocol/test-sol/utils08.sol b/packages/protocol/test-sol/utils08.sol index ba055feb832..17ea9a050f4 100644 --- a/packages/protocol/test-sol/utils08.sol +++ b/packages/protocol/test-sol/utils08.sol @@ -1,23 +1,28 @@ pragma solidity >=0.5.13 <0.9.0; -// import "celo-foundry-8/Test.sol"; import { Test as ForgeTest } from "@lib/celo-foundry-8/lib/forge-std/src/Test.sol"; import { TestConstants } from "@test-sol/constants.sol"; +import { PrecompileHandler } from "@test-sol/utils/PrecompileHandler.sol"; -// import "@celo-contracts-8/common/EpochManager.sol"; -import "@test-sol/unit/common/mocks/MockEpochManager.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; + +import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; contract Utils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { - // contract Utils08 is Test, TestConstants, IsL2Check, PrecompilesOverrideV2 { IRegistry registry; - MockEpochManager public epochManager; + PrecompileHandler ph; + EpochManager_WithMocks public epochManager; + + address public epochManagerEnabler; uint256 public constant secondsInOneBlock = 5; function setUp() public virtual { + ph = new PrecompileHandler(); setupRegistry(); + epochManagerEnabler = actor("EpochManagerEnabler"); + registry.setAddressFor(EpochManagerEnablerContract, epochManagerEnabler); setupEpochManager(); } @@ -27,7 +32,7 @@ contract Utils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { } function setupEpochManager() public { - epochManager = new MockEpochManager(); + epochManager = new EpochManager_WithMocks(); registry.setAddressFor(EpochManagerContract, address(epochManager)); } @@ -88,8 +93,6 @@ contract Utils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { function whenL2WithEpochManagerInitialization() internal { uint256 l1EpochNumber = getEpochNumber(); - address epochManagerEnabler = actor("EpochManagerEnabler"); - registry.setAddressFor(EpochManagerContract, address(epochManager)); address[] memory _elected = new address[](2); _elected[0] = actor("validator"); From 23bd8327910e8e0ca3bcd1e94731549cc02a7e10 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:29:47 -0500 Subject: [PATCH 05/23] fixed unit test inheritance conflicts --- .../test-sol/unit/common/EpochManager.t.sol | 359 +++++++++--------- .../unit/common/EpochManagerEnabler.t.sol | 91 ++--- 2 files changed, 216 insertions(+), 234 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 1596e9ae02e..9937272ba68 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -1,33 +1,25 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -// import "celo-foundry-8/Test.sol"; -import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; -import "@celo-contracts-8/stability/test/MockStableToken.sol"; -import "@celo-contracts-8/common/test/MockCeloToken.sol"; import "@celo-contracts/common/interfaces/ICeloToken.sol"; -import "@celo-contracts-8/common/ScoreManager.sol"; import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; - -// import { TestConstants } from "@test-sol/constants.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; - +import { MockElection } from "@celo-contracts/governance/test/MockElection.sol"; import "@celo-contracts/stability/test/MockSortedOracles.sol"; -import "@celo-contracts/common/interfaces/IRegistry.sol"; - +import "@celo-contracts-8/common/ScoreManager.sol"; +import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; +import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; +import "@celo-contracts-8/common/test/MockCeloToken.sol"; +import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; import { IMockValidators } from "@celo-contracts-8/governance/test/IMockValidators.sol"; - import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewardsMock.sol"; -import { MockElection } from "@celo-contracts/governance/test/MockElection.sol"; +import "@celo-contracts-8/stability/test/MockStableToken.sol"; -import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; +import { Utils08 } from "@test-sol/utils08.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; -import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; -import { console } from "forge-std/console.sol"; contract EpochManagerTest is Utils08 { - EpochManager_WithMocks epochManager; + EpochManager_WithMocks epochManagerContract; MockSortedOracles sortedOracles; MockStableToken08 stableToken; @@ -36,7 +28,6 @@ contract EpochManagerTest is Utils08 { MockAccounts accounts; IMockValidators validators; - address epochManagerEnabler; address carbonOffsettingPartner; address communityRewardFund; address reserveAddress; @@ -48,7 +39,6 @@ contract EpochManagerTest is Utils08 { uint256 epochDuration = DAY; address[] firstElected; - IRegistry registry; MockCeloToken08 celoToken; MockCeloUnreleasedTreasury celoUnreleasedTreasury; ScoreManager scoreManager; @@ -60,9 +50,7 @@ contract EpochManagerTest is Utils08 { uint256 validator2Reward = 43e18; address validator1; - uint256 validator1PK; address validator2; - uint256 validator2PK; address group = actor("group"); @@ -80,8 +68,9 @@ contract EpochManagerTest is Utils08 { event GroupMarkedForProcessing(address indexed group, uint256 indexed epochNumber); event GroupProcessed(address indexed group, uint256 indexed epochNumber); - function setUp() public virtual { - epochManager = new EpochManager_WithMocks(); + function setUp() public virtual override { + super.setUp(); + epochManagerContract = new EpochManager_WithMocks(); sortedOracles = new MockSortedOracles(); epochRewards = new EpochRewardsMock08(); validators = IMockValidators(actor("validators05")); @@ -91,8 +80,8 @@ contract EpochManagerTest is Utils08 { election = new MockElection(); accounts = new MockAccounts(); - (validator1, validator1PK) = actorWithPK(vm, "validator1"); - (validator2, validator2PK) = actorWithPK(vm, "validator2"); + validator1 = actor("validator"); + validator2 = actor("otherValidator"); firstElected.push(validator1); firstElected.push(validator2); @@ -102,7 +91,6 @@ contract EpochManagerTest is Utils08 { reserveAddress = actor("reserve"); - epochManagerEnabler = actor("epochManagerEnabler"); carbonOffsettingPartner = actor("carbonOffsettingPartner"); communityRewardFund = actor("communityRewardFund"); @@ -111,11 +99,9 @@ contract EpochManagerTest is Utils08 { deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); deployCodeTo("MockValidators.sol", abi.encode(false), address(validators)); - registry = IRegistry(REGISTRY_ADDRESS); scoreManager = ScoreManager(scoreManagerAddress); - registry.setAddressFor(EpochManagerContract, address(epochManager)); - registry.setAddressFor(EpochManagerEnablerContract, epochManagerEnabler); + registry.setAddressFor(EpochManagerContract, address(epochManagerContract)); registry.setAddressFor(SortedOraclesContract, address(sortedOracles)); registry.setAddressFor(GovernanceContract, communityRewardFund); registry.setAddressFor(EpochRewardsContract, address(epochRewards)); @@ -136,12 +122,12 @@ contract EpochManagerTest is Utils08 { sortedOracles.setMedianRate(address(stableToken), stableAmountForRate); - scoreManager.setValidatorScore(actor("validator1"), 1); + scoreManager.setValidatorScore(validator1, 1); - epochManager.initialize(REGISTRY_ADDRESS, 10); + epochManagerContract.initialize(REGISTRY_ADDRESS, 10); epochRewards.setCarbonOffsettingPartner(carbonOffsettingPartner); - blockTravel(vm, firstEpochBlock); + blockTravel(firstEpochBlock); validators.setEpochRewards(validator1, validator1Reward); validators.setEpochRewards(validator2, validator2Reward); @@ -161,11 +147,26 @@ contract EpochManagerTest is Utils08 { election.setElectedValidators(members); - deployCodeTo("MockRegistry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS); + // deployCodeTo("MockRegistry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - travelNL2Epoch(vm, 1); + travelNL2Epoch(1); + } + + function _travelAndProcess_N_L2Epoch(uint256 n) public { + for (uint256 i = 0; i < n; i++) { + travelNL2Epoch(1); + epochManagerContract.startNextEpochProcess(); + + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); + + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); + } } function getGroupsWithLessersAndGreaters() @@ -184,89 +185,74 @@ contract EpochManagerTest is Utils08 { return (groups, lessers, greaters); } - - function _travelAndProcess_N_L2Epoch(uint256 n) public { - for (uint256 i = 0; i < n; i++) { - travelNL2Epoch(vm, 1); - epochManager.startNextEpochProcess(); - - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); - - epochManager.finishNextEpochProcess(groups, lessers, greaters); - } - } } 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)); + assertEq(address(epochManagerContract.registry()), REGISTRY_ADDRESS); + assertEq(epochManagerContract.epochDuration(), 10); + assertEq(epochManagerContract.oracleAddress(), address(sortedOracles)); } function test_Reverts_WhenAlreadyInitialized() public virtual { vm.expectRevert("contract already initialized"); - epochManager.initialize(REGISTRY_ADDRESS, 10); + epochManagerContract.initialize(REGISTRY_ADDRESS, 10); } } contract EpochManagerTest_initializeSystem is EpochManagerTest { function test_processCanBeStarted() public virtual { vm.prank(epochManagerEnabler); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); ( uint256 _firstEpochBlock, uint256 _lastEpochBlock, uint256 _startTimestamp, uint256 _currentRewardsBlock - ) = epochManager.getCurrentEpoch(); - assertGt(epochManager.getElectedAccounts().length, 0); - assertEq(epochManager.firstKnownEpoch(), firstEpochNumber); + ) = epochManagerContract.getCurrentEpoch(); + assertGt(epochManagerContract.getElectedAccounts().length, 0); + assertEq(epochManagerContract.firstKnownEpoch(), firstEpochNumber); assertEq(_firstEpochBlock, firstEpochBlock); assertEq(_lastEpochBlock, 0); assertEq(_startTimestamp, block.timestamp); assertEq(_currentRewardsBlock, 0); - assertEq(epochManager.getElectedAccounts(), firstElected); + assertEq(epochManagerContract.getElectedAccounts(), firstElected); } function test_Reverts_processCannotBeStartedAgain() public virtual { vm.prank(epochManagerEnabler); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); vm.prank(epochManagerEnabler); vm.expectRevert("Epoch system already initialized"); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); } function test_Reverts_WhenSystemInitializedByOtherContract() public virtual { vm.expectRevert("msg.sender is not Enabler"); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); } } contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { function test_Reverts_whenSystemNotInitialized() public { vm.expectRevert("Epoch system not initialized"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { vm.prank(epochManagerEnabler); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); vm.expectRevert("Epoch is not ready to start"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Reverts_WhenEpochProcessingAlreadyStarted() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); vm.expectRevert("Epoch process is already started"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Emits_EpochProcessingStartedEvent() public { @@ -274,28 +260,28 @@ contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { vm.expectEmit(true, true, true, true); emit EpochProcessingStarted(firstEpochNumber); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_SetsTheEpochRewardBlock() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); - (, , , uint256 _currentRewardsBlock) = epochManager.getCurrentEpoch(); + epochManagerContract.startNextEpochProcess(); + (, , , uint256 _currentRewardsBlock) = epochManagerContract.getCurrentEpoch(); assertEq(_currentRewardsBlock, block.number); } function test_SetsTheEpochRewardAmounts() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); ( uint256 status, uint256 perValidatorReward, uint256 totalRewardsVoter, uint256 totalRewardsCommunity, uint256 totalRewardsCarbonFund - ) = epochManager.getEpochProcessingState(); + ) = epochManagerContract.getEpochProcessingState(); assertEq(status, 1); assertEq(perValidatorReward, 5); assertEq(totalRewardsVoter, 6); @@ -305,14 +291,14 @@ contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { function test_ShouldMintTotalValidatorStableRewardsToEpochManager() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); assertEq(validators.mintedStable(), validator1Reward + validator2Reward); } function test_ShouldReleaseCorrectAmountToReserve() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); uint256 reserveBalanceAfter = celoToken.balanceOf(reserveAddress); assertEq( reserveBalanceAfter, @@ -326,8 +312,8 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { function test_setsNewEpochDuration() public { initializeEpochManagerSystem(); - epochManager.setEpochDuration(newEpochDuration); - assertEq(epochManager.epochDuration(), newEpochDuration); + epochManagerContract.setEpochDuration(newEpochDuration); + assertEq(epochManagerContract.epochDuration(), newEpochDuration); } function test_Emits_EpochDurationSetEvent() public { @@ -335,21 +321,21 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { vm.expectEmit(true, true, true, true); emit EpochDurationSet(newEpochDuration); - epochManager.setEpochDuration(newEpochDuration); + epochManagerContract.setEpochDuration(newEpochDuration); } function test_Reverts_WhenIsOnEpochProcess() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); vm.expectRevert("Cannot change epoch duration during processing."); - epochManager.setEpochDuration(newEpochDuration); + epochManagerContract.setEpochDuration(newEpochDuration); } function test_Reverts_WhenNewEpochDurationIsZero() public { initializeEpochManagerSystem(); vm.expectRevert("New epoch duration must be greater than zero."); - epochManager.setEpochDuration(0); + epochManagerContract.setEpochDuration(0); } } @@ -358,8 +344,8 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { function test_setsNewOracleAddress() public { initializeEpochManagerSystem(); - epochManager.setOracleAddress(newOracleAddress); - assertEq(epochManager.oracleAddress(), newOracleAddress); + epochManagerContract.setOracleAddress(newOracleAddress); + assertEq(epochManagerContract.oracleAddress(), newOracleAddress); } function test_Emits_OracleAddressSetEvent() public { @@ -367,28 +353,28 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { vm.expectEmit(true, true, true, true); emit OracleAddressSet(newOracleAddress); - epochManager.setOracleAddress(newOracleAddress); + epochManagerContract.setOracleAddress(newOracleAddress); } function test_Reverts_WhenIsOnEpochProcess() public { initializeEpochManagerSystem(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); vm.expectRevert("Cannot change oracle address during epoch processing."); - epochManager.setOracleAddress(newOracleAddress); + epochManagerContract.setOracleAddress(newOracleAddress); } function test_Reverts_WhenNewOracleAddressIsZero() public { initializeEpochManagerSystem(); vm.expectRevert("Cannot set address zero as the Oracle."); - epochManager.setOracleAddress(address(0)); + epochManagerContract.setOracleAddress(address(0)); } function test_Reverts_WhenNewOracleAddressIsunchanged() public { initializeEpochManagerSystem(); vm.expectRevert("Oracle address cannot be the same."); - epochManager.setOracleAddress(address(sortedOracles)); + epochManagerContract.setOracleAddress(address(sortedOracles)); } } @@ -421,18 +407,18 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { validators.setMembers(group, members); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - stableToken.mint(address(epochManager), paymentAmount * 2); - epochManagerBalanceBefore = stableToken.balanceOf(address(epochManager)); - epochManager._setPaymentAllocation(validator1, paymentAmount); + stableToken.mint(address(epochManagerContract), paymentAmount * 2); + epochManagerBalanceBefore = stableToken.balanceOf(address(epochManagerContract)); + epochManagerContract._setPaymentAllocation(validator1, paymentAmount); } function test_sendsCUsdFromEpochManagerToValidator() public { - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, paymentAmount); assertEq(epochManagerBalanceAfter, epochManagerBalanceBefore - paymentAmount); @@ -441,11 +427,11 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function test_sendsCUsdFromEpochManagerToValidatorAndGroup() public { validators.setCommission(group, twentyFivePercent); - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); uint256 groupBalanceAfter = stableToken.balanceOf(group); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, threeQuartersOfPayment); assertEq(groupBalanceAfter, quarterOfPayment); @@ -455,11 +441,11 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function test_sendsCUsdFromEpochManagerToValidatorAndBeneficiary() public { accounts.setPaymentDelegationFor(validator1, beneficiary, twentyFivePercent); - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); uint256 beneficiaryBalanceAfter = stableToken.balanceOf(beneficiary); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, threeQuartersOfPayment); assertEq(beneficiaryBalanceAfter, quarterOfPayment); @@ -470,12 +456,12 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { validators.setCommission(group, fiftyPercent); accounts.setPaymentDelegationFor(validator1, beneficiary, fiftyPercent); - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); uint256 groupBalanceAfter = stableToken.balanceOf(group); uint256 beneficiaryBalanceAfter = stableToken.balanceOf(beneficiary); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, quarterOfPayment); assertEq(groupBalanceAfter, halfOfPayment); @@ -487,7 +473,7 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { validators.setCommission(group, fiftyPercent); accounts.setPaymentDelegationFor(validator1, beneficiary, fiftyPercent); - vm.expectEmit(true, true, true, true, address(epochManager)); + vm.expectEmit(true, true, true, true, address(epochManagerContract)); emit ValidatorEpochPaymentDistributed( validator1, quarterOfPayment, @@ -496,19 +482,19 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { beneficiary, quarterOfPayment ); - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); } function test_doesNothingIfNotAllocated() public { validators.setCommission(group, fiftyPercent); accounts.setPaymentDelegationFor(validator2, beneficiary, fiftyPercent); - epochManager.sendValidatorPayment(validator2); + epochManagerContract.sendValidatorPayment(validator2); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); uint256 groupBalanceAfter = stableToken.balanceOf(group); uint256 beneficiaryBalanceAfter = stableToken.balanceOf(beneficiary); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, 0); assertEq(groupBalanceAfter, 0); @@ -517,11 +503,11 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { } function test_doesntAllowDoubleSending() public { - epochManager.sendValidatorPayment(validator1); - epochManager.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); + epochManagerContract.sendValidatorPayment(validator1); uint256 validatorBalanceAfter = stableToken.balanceOf(validator1); - uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManager)); + uint256 epochManagerBalanceAfter = stableToken.balanceOf(address(epochManagerContract)); assertEq(validatorBalanceAfter, paymentAmount); assertEq(epochManagerBalanceAfter, epochManagerBalanceBefore - paymentAmount); @@ -565,7 +551,7 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { vm.prank(epochManagerEnabler); initializeEpochManagerSystem(); - elected = epochManager.getElectedAccounts(); + elected = epochManagerContract.getElectedAccounts(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } @@ -574,22 +560,22 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { address[] memory groups = new address[](0); vm.expectRevert("Epoch process is not started"); - epochManager.finishNextEpochProcess(groups, groups, groups); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); } function test_Reverts_WhenGroupsDoNotMatch() public { address[] memory groups = new address[](0); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); vm.expectRevert("number of groups does not match"); - epochManager.finishNextEpochProcess(groups, groups, groups); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); } function test_Reverts_WhenGroupsNotFromElected() public { address[] memory groups = new address[](1); groups[0] = group2; - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); vm.expectRevert("group not from current elected set"); - epochManager.finishNextEpochProcess(groups, groups, groups); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); } function test_TransfersToCommunityAndCarbonOffsetting() public { @@ -599,8 +585,8 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { address[] memory greaters ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); @@ -613,8 +599,8 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { address[] memory greaters ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertEq(election.distributedEpochRewards(group), groupEpochRewards); } @@ -626,16 +612,16 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { address[] memory greaters ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); address[] memory newElected = new address[](2); newElected[0] = validator3; newElected[1] = validator4; election.setElectedValidators(newElected); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - address[] memory afterElected = epochManager.getElectedAccounts(); + address[] memory afterElected = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < newElected.length; i++) { assertEq(newElected[i], afterElected[i]); @@ -649,14 +635,14 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { address[] memory greaters ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); for (uint i = 0; i < groups.length; i++) { vm.expectEmit(true, true, true, true); emit GroupProcessed(groups[i], firstEpochNumber); } - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); } } @@ -697,64 +683,67 @@ contract EpochManagerTest_setToProcessGroups is EpochManagerTest { vm.prank(epochManagerEnabler); initializeEpochManagerSystem(); - elected = epochManager.getElectedAccounts(); + elected = epochManagerContract.getElectedAccounts(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } function test_Reverts_WhenNotStarted() public { vm.expectRevert("Epoch process is not started"); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); } function test_setsToProcessGroups() public { (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); - assertEq(EpochManager(address(epochManager)).toProcessGroups(), groups.length); + assertEq(EpochManager(address(epochManagerContract)).toProcessGroups(), groups.length); } function test_blocksChilds() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); - assertTrue(epochManager.isBlocked()); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + assertTrue(epochManagerContract.isBlocked()); } function test_Reverts_startEpochAgain() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); vm.expectRevert("Epoch process is already started"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Reverts_WhenSetToProcessGroups() public { vm.expectRevert("Epoch process is not started"); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); } function test_setsGroupRewards() public { (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { - assertEq(EpochManager(address(epochManager)).processedGroups(group), groupEpochRewards); + assertEq( + EpochManager(address(epochManagerContract)).processedGroups(group), + groupEpochRewards + ); } } function test_Emits_GroupMarkedForProcessingEvent() public { (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); for (uint i = 0; i < groups.length; i++) { vm.expectEmit(true, true, true, true); emit GroupMarkedForProcessing(groups[i], firstEpochNumber); } - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); } } @@ -797,45 +786,45 @@ contract EpochManagerTest_processGroup is EpochManagerTest { vm.prank(epochManagerEnabler); initializeEpochManagerSystem(); - elected = epochManager.getElectedAccounts(); + elected = epochManagerContract.getElectedAccounts(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } function test_Reverts_WhenNotStarted() public { vm.expectRevert("Indivudual epoch process is not started"); - epochManager.processGroup(group, address(0), address(0)); + epochManagerContract.processGroup(group, address(0), address(0)); } function test_Reverts_WhenGroupNotInToProcessGroups() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); vm.expectRevert("group not from current elected set"); - epochManager.processGroup(group2, address(0), address(0)); + epochManagerContract.processGroup(group2, address(0), address(0)); } function test_ProcessesGroup() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); - epochManager.processGroup(group, address(0), address(0)); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); - (uint256 status, , , , ) = epochManager.getEpochProcessingState(); + (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); assertEq(status, 0); } function test_TransfersToCommunityAndCarbonOffsetting() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); - epochManager.processGroup(group, address(0), address(0)); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); } function test_TransfersToValidatorGroup() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); - epochManager.processGroup(group, address(0), address(0)); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); assertEq(election.distributedEpochRewards(group), groupEpochRewards); } @@ -847,7 +836,7 @@ contract EpochManagerTest_processGroup is EpochManagerTest { address[] memory greaters ) = getGroupsWithLessersAndGreaters(); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); address[] memory newElected = new address[](2); newElected[0] = validator3; @@ -860,19 +849,19 @@ contract EpochManagerTest_processGroup is EpochManagerTest { accounts.setValidatorSigner(validator3, signer3); accounts.setValidatorSigner(validator4, signer4); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { - epochManager.processGroup(groups[i], lessers[i], greaters[i]); + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); } - address[] memory afterElected = epochManager.getElectedAccounts(); + address[] memory afterElected = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < newElected.length; i++) { assertEq(newElected[i], afterElected[i]); } - address[] memory afterSigners = epochManager.getElectedSigners(); + address[] memory afterSigners = epochManagerContract.getElectedSigners(); assertEq(afterSigners.length, signers.length); for (uint256 i = 0; i < signers.length; i++) { assertEq(signers[i], afterSigners[i]); @@ -880,11 +869,11 @@ contract EpochManagerTest_processGroup is EpochManagerTest { } function test_Emits_GroupProcessed() public { - epochManager.startNextEpochProcess(); - epochManager.setToProcessGroups(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); vm.expectEmit(true, true, true, true); emit GroupProcessed(group, firstEpochNumber); - epochManager.processGroup(group, address(0), address(0)); + epochManagerContract.processGroup(group, address(0), address(0)); } } @@ -893,10 +882,14 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { uint256 numberOfEpochsToTravel = 9; initializeEpochManagerSystem(); - uint256 _startingEpochNumber = epochManager.getCurrentEpochNumber(); + uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); + + ( + uint256 startingEpochFirstBlock, + , + uint256 startingEpochStartTimestamp, - (uint256 startingEpochFirstBlock, , uint256 startingEpochStartTimestamp, ) = epochManager - .getCurrentEpoch(); + ) = epochManagerContract.getCurrentEpoch(); _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); @@ -905,7 +898,7 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { uint256 _lastBlock, uint256 _startTimestamp, uint256 _rewardBlock - ) = epochManager.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); + ) = epochManagerContract.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); assertEq( startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * (numberOfEpochsToTravel + 1)) + 1, @@ -918,14 +911,14 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { initializeEpochManagerSystem(); - uint256 _startingEpochNumber = epochManager.getCurrentEpochNumber(); + uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); uint256 numberOfEpochsToTravel = 7; ( uint256 _startingEpochFirstBlock, uint256 _startingLastBlock, uint256 _startingStartTimestamp, uint256 _startingRewardBlock - ) = epochManager.getCurrentEpoch(); + ) = epochManagerContract.getCurrentEpoch(); _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); @@ -934,7 +927,7 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { uint256 _initialLastBlock, uint256 _initialStartTimestamp, uint256 _initialRewardBlock - ) = epochManager.getEpochByNumber(_startingEpochNumber); + ) = epochManagerContract.getEpochByNumber(_startingEpochNumber); assertEq(_initialFirstBlock, _startingEpochFirstBlock); assertEq(_initialLastBlock, _startingLastBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock); @@ -952,7 +945,7 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { uint256 _lastBlock, uint256 _startTimestamp, uint256 _rewardBlock - ) = epochManager.getEpochByNumber(500); + ) = epochManagerContract.getEpochByNumber(500); assertEq(_firstBlock, 0); assertEq(_lastBlock, 0); @@ -964,12 +957,12 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { initializeEpochManagerSystem(); - assertEq(epochManager.getEpochNumberOfBlock(firstEpochBlock), firstEpochNumber); + assertEq(epochManagerContract.getEpochNumberOfBlock(firstEpochBlock), firstEpochNumber); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getEpochNumberOfBlock(firstEpochBlock); + epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); } } @@ -979,7 +972,7 @@ contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { _travelAndProcess_N_L2Epoch(2); - (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManager.getEpochByBlockNumber( + (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManagerContract.getEpochByBlockNumber( firstEpochBlock + (3 * L2_BLOCK_IN_EPOCH) ); assertEq(_firstBlock, firstEpochBlock + 1 + (2 * L2_BLOCK_IN_EPOCH)); @@ -988,43 +981,43 @@ contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getEpochNumberOfBlock(firstEpochBlock); + epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); } } contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { function test_ShouldRetreiveTheNumberOfElected() public { initializeEpochManagerSystem(); - assertEq(epochManager.numberOfElectedInCurrentSet(), 2); + assertEq(epochManagerContract.numberOfElectedInCurrentSet(), 2); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.numberOfElectedInCurrentSet(); + epochManagerContract.numberOfElectedInCurrentSet(); } } contract EpochManagerTest_getElectedAccounts is EpochManagerTest { function test_ShouldRetreiveThelistOfElectedAccounts() public { initializeEpochManagerSystem(); - assertEq(epochManager.getElectedAccounts(), firstElected); + assertEq(epochManagerContract.getElectedAccounts(), firstElected); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getElectedAccounts(); + epochManagerContract.getElectedAccounts(); } } contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { function test_ShouldRetreiveThecorrectValidator() public { initializeEpochManagerSystem(); - assertEq(epochManager.getElectedAccountByIndex(0), validator1); + assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getElectedAccountByIndex(0); + epochManagerContract.getElectedAccountByIndex(0); } } contract EpochManagerTest_getElectedSigners is EpochManagerTest { @@ -1033,12 +1026,12 @@ contract EpochManagerTest_getElectedSigners is EpochManagerTest { address[] memory electedSigners = new address[](firstElected.length); electedSigners[0] = accounts.getValidatorSigner(firstElected[0]); electedSigners[1] = accounts.getValidatorSigner(firstElected[1]); - assertEq(epochManager.getElectedSigners(), electedSigners); + assertEq(epochManagerContract.getElectedSigners(), electedSigners); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getElectedSigners(); + epochManagerContract.getElectedSigners(); } } contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { @@ -1047,11 +1040,11 @@ contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { address[] memory electedSigners = new address[](firstElected.length); electedSigners[1] = accounts.getValidatorSigner(firstElected[1]); - assertEq(epochManager.getElectedSignerByIndex(1), electedSigners[1]); + assertEq(epochManagerContract.getElectedSignerByIndex(1), electedSigners[1]); } function test_Reverts_WhenL1() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getElectedSignerByIndex(1); + epochManagerContract.getElectedSignerByIndex(1); } } diff --git a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol index 320598d9618..8eb9ffcadfe 100644 --- a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol @@ -1,32 +1,23 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -// import "celo-foundry-8/Test.sol"; -import "@celo-contracts-8/common/EpochManager.sol"; - -import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; +import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; +import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import { CeloUnreleasedTreasury } from "@celo-contracts-8/common/CeloUnreleasedTreasury.sol"; -import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; -import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; +import "@celo-contracts-8/common/test/MockCeloToken.sol"; +import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; +import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewardsMock.sol"; -// import { TestConstants } from "@test-sol/constants.sol"; import { Utils08 } from "@test-sol/utils08.sol"; - -import "@celo-contracts/common/interfaces/IRegistry.sol"; - -import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewardsMock.sol"; +import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; -import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; -import "@celo-contracts-8/common/test/MockCeloToken.sol"; contract EpochManagerEnablerTest is Utils08 { - EpochManager epochManager; - EpochManagerEnablerMock epochManagerEnabler; + EpochManagerEnablerMock epochManagerEnablerContract; MockCeloUnreleasedTreasury celoUnreleasedTreasury; MockCeloToken08 celoToken; - IRegistry registry; IAccounts accounts; address accountsAddress; @@ -40,10 +31,12 @@ contract EpochManagerEnablerTest is Utils08 { event LastKnownFirstBlockOfEpochSet(uint256 lastKnownFirstBlockOfEpoch); event LastKnownElectedAccountsSet(); - function setUp() public virtual { + function setUp() public virtual override { + super.setUp(); + // used by test that uses L1 epochSize() ph.setEpochSize(17280); - epochManager = new EpochManager(true); - epochManagerEnabler = new EpochManagerEnablerMock(); + + epochManagerEnablerContract = new EpochManagerEnablerMock(); celoToken = new MockCeloToken08(); celoUnreleasedTreasury = new MockCeloUnreleasedTreasury(); @@ -53,14 +46,10 @@ contract EpochManagerEnablerTest is Utils08 { nonOwner = actor("nonOwner"); oracle = actor("oracle"); - deployCodeTo("MockRegistry.sol", abi.encode(false), REGISTRY_ADDRESS); deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); - - registry = IRegistry(REGISTRY_ADDRESS); accounts = IAccounts(accountsAddress); - registry.setAddressFor(EpochManagerContract, address(epochManager)); - registry.setAddressFor(EpochManagerEnablerContract, address(epochManagerEnabler)); + registry.setAddressFor(EpochManagerEnablerContract, address(epochManagerEnablerContract)); registry.setAddressFor(AccountsContract, address(accounts)); registry.setAddressFor(CeloTokenContract, address(celoToken)); registry.setAddressFor(SortedOraclesContract, oracle); @@ -69,12 +58,12 @@ contract EpochManagerEnablerTest is Utils08 { celoToken.setTotalSupply(CELO_SUPPLY_CAP); celoToken.setBalanceOf(address(celoUnreleasedTreasury), L2_INITIAL_STASH_BALANCE); - epochManagerEnabler.initialize(REGISTRY_ADDRESS); + epochManagerEnablerContract.initialize(REGISTRY_ADDRESS); epochManager.initialize(REGISTRY_ADDRESS, epochDuration); _setupValidators(); - travelEpochL1(vm); - travelEpochL1(vm); + travelEpochL1(); + travelEpochL1(); } function _setupValidators() internal { @@ -82,105 +71,105 @@ contract EpochManagerEnablerTest is Utils08 { vm.prank(vm.addr(i + 1)); accounts.createAccount(); - epochManagerEnabler.addValidator(vm.addr(i + 1)); + epochManagerEnablerContract.addValidator(vm.addr(i + 1)); } } } contract EpochManagerEnablerTest_initialize is EpochManagerEnablerTest { function test_initialize() public { - assertEq(address(epochManagerEnabler.registry()), REGISTRY_ADDRESS); + assertEq(address(epochManagerEnablerContract.registry()), REGISTRY_ADDRESS); } function test_Reverts_WhenAlreadyInitialized() public virtual { vm.expectRevert("contract already initialized"); - epochManagerEnabler.initialize(REGISTRY_ADDRESS); + epochManagerEnablerContract.initialize(REGISTRY_ADDRESS); } } contract EpochManagerEnablerTest_initEpochManager is EpochManagerEnablerTest { function test_CanBeCalledByAnyone() public { - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); - whenL2(vm); + whenL2(); vm.prank(nonOwner); - epochManagerEnabler.initEpochManager(); + epochManagerEnablerContract.initEpochManager(); assertGt(epochManager.getElectedAccounts().length, 0); assertTrue(epochManager.systemAlreadyInitialized()); } function test_Reverts_ifEpochAndValidatorsAreNotCaptured() public { - whenL2(vm); + whenL2(); vm.expectRevert("lastKnownEpochNumber not set."); - epochManagerEnabler.initEpochManager(); + epochManagerEnablerContract.initEpochManager(); } function test_Reverts_whenL1() public { vm.expectRevert("This method is not supported in L1."); - epochManagerEnabler.initEpochManager(); + epochManagerEnablerContract.initEpochManager(); } } contract EpochManagerEnablerTest_captureEpochAndValidators is EpochManagerEnablerTest { function test_Reverts_whenL2() public { - whenL2(vm); + whenL2(); vm.expectRevert("This method is no longer supported in L2."); - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); } function test_shouldSetLastKnownElectedAccounts() public { - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); - assertEq(epochManagerEnabler.getlastKnownElectedAccounts().length, numberValidators); + assertEq(epochManagerEnablerContract.getlastKnownElectedAccounts().length, numberValidators); } function test_shouldSetLastKnownEpochNumber() public { - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); - assertEq(epochManagerEnabler.lastKnownEpochNumber(), 3); + assertEq(epochManagerEnablerContract.lastKnownEpochNumber(), 3); } function test_shouldSetLastKnownFirstBlockOfEpoch() public { - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); - assertEq(epochManagerEnabler.lastKnownFirstBlockOfEpoch(), 17280 * 2); + assertEq(epochManagerEnablerContract.lastKnownFirstBlockOfEpoch(), 17280 * 2); } function test_Emits_LastKnownEpochNumberSet() public { vm.expectEmit(true, true, true, true); emit LastKnownEpochNumberSet(3); - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); } function test_Emits_LastKnownElectedAccountsSet() public { vm.expectEmit(true, true, true, true); emit LastKnownElectedAccountsSet(); - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); } function test_Emits_LastKnownFirstBlockOfEpochSet() public { vm.expectEmit(true, true, true, true); emit LastKnownFirstBlockOfEpochSet(34560); - epochManagerEnabler.captureEpochAndValidators(); + epochManagerEnablerContract.captureEpochAndValidators(); } } contract EpochManagerEnablerTest_getFirstBlockOfEpoch is EpochManagerEnablerTest { function test_blockIsEpockBlock() public { vm.roll(27803520); - epochManagerEnabler.setFirstBlockOfEpoch(); - assertEq(epochManagerEnabler.lastKnownFirstBlockOfEpoch(), 27803520); + epochManagerEnablerContract.setFirstBlockOfEpoch(); + assertEq(epochManagerEnablerContract.lastKnownFirstBlockOfEpoch(), 27803520); } function test_blockIsNotEpochBlock() public { vm.roll(27817229); - epochManagerEnabler.setFirstBlockOfEpoch(); - assertEq(epochManagerEnabler.lastKnownFirstBlockOfEpoch(), 27803520); + epochManagerEnablerContract.setFirstBlockOfEpoch(); + assertEq(epochManagerEnablerContract.lastKnownFirstBlockOfEpoch(), 27803520); } } From e85910d4e21afb0f6c375435173db81829af8a14 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:40:12 -0500 Subject: [PATCH 06/23] removed shadowed var --- packages/protocol/migrations_sol/Migration.s.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/migrations_sol/Migration.s.sol b/packages/protocol/migrations_sol/Migration.s.sol index aaa9303e3fa..a0f4165ad12 100644 --- a/packages/protocol/migrations_sol/Migration.s.sol +++ b/packages/protocol/migrations_sol/Migration.s.sol @@ -1069,7 +1069,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants { } console.log(string.concat("Setting constitution thresholds for: ", contractName)); - IRegistry registry = IRegistry(REGISTRY_ADDRESS); + registry = IRegistry(REGISTRY_ADDRESS); address contractAddress = registry.getAddressForString(contractName); From 8b6b83624b76257bfd0c6e6204e93d65e84e0d2b Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:26:48 -0500 Subject: [PATCH 07/23] passing devchain e2e --- .../devchain/e2e/common/EpochManager.t.sol | 260 +++++++++--------- .../e2e/common/FeeCurrencyDirectory.t.sol | 1 - .../protocol/test-sol/devchain/e2e/utils.sol | 12 +- .../protocol/test-sol/utils/ECDSAHelper08.sol | 4 +- 4 files changed, 134 insertions(+), 143 deletions(-) diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index 02fbb11ebf4..6e0fca26bd6 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -1,18 +1,16 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; -// import { Utils08 } from "@test-sol/utils08.sol"; import { IEpochManager } from "@celo-contracts/common/interfaces/IEpochManager.sol"; import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@test-sol/utils/ECDSAHelper08.sol"; import "@openzeppelin/contracts8/utils/structs/EnumerableSet.sol"; -import { console } from "forge-std/console.sol"; +import { console } from "forge-std-8/console.sol"; -contract E2E_EpochManager is Devchain, ECDSAHelper08 { +contract E2E_EpochManager is ECDSAHelper08, Devchain { using EnumerableSet for EnumerableSet.AddressSet; struct VoterWithPK { @@ -26,7 +24,6 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { } address epochManagerOwner; - address epochManagerEnabler; address[] firstElected; uint256 epochDuration; @@ -42,12 +39,12 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { EnumerableSet.AddressSet internal electedGroupsHelper; - function setUp() public virtual { - epochManagerOwner = Ownable(address(epochManager)).owner(); - epochManagerEnabler = registry.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID); + function setUp() public virtual override { + epochManagerOwner = Ownable(address(epochManagerContract)).owner(); + epochManagerEnabler = registryContract.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID); firstElected = getValidators().getRegisteredValidators(); - epochDuration = epochManager.epochDuration(); + epochDuration = epochManagerContract.epochDuration(); vm.deal(address(celoUnreleasedTreasury), L2_INITIAL_STASH_BALANCE); // 80% of the total supply to the treasury - whis will be yet distributed } @@ -70,10 +67,10 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { } address[] memory registeredValidators = getValidators().getRegisteredValidators(); - travelEpochL1(vm); - travelEpochL1(vm); - travelEpochL1(vm); - travelEpochL1(vm); + travelEpochL1(); + travelEpochL1(); + travelEpochL1(); + travelEpochL1(); for (uint256 i = 0; i < registeredValidators.length; i++) { (, , address validatorGroup, , ) = getValidators().getValidator(registeredValidators[i]); if (getElection().getPendingVotesForGroup(validatorGroup) == 0) { @@ -104,7 +101,7 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { GroupWithVotes[] memory groupWithVotes ) { - (, , uint256 maxTotalRewards, , ) = epochManager.getEpochProcessingState(); + (, , uint256 maxTotalRewards, , ) = epochManagerContract.getEpochProcessingState(); (, groupWithVotes) = getGroupsWithVotes(); lessers = new address[](_groups.length); @@ -235,7 +232,7 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { } function getValidatorGroupsFromElected() internal view returns (address[] memory) { - address[] memory elected = epochManager.getElectedAccounts(); + address[] memory elected = epochManagerContract.getElectedAccounts(); address[] memory validatorGroups = new address[](elected.length); for (uint256 i = 0; i < elected.length; i++) { (, , address group, , ) = validators.getValidator(elected[i]); @@ -300,9 +297,8 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { } function getCurrentlyElectedGroups() internal returns (address[] memory) { - address[] memory currentlyElected = epochManager.getElectedAccounts(); + address[] memory currentlyElected = epochManagerContract.getElectedAccounts(); - // clearElectedGroupsHelper(); for (uint256 i = 0; i < currentlyElected.length; i++) { (, , address group, , ) = validators.getValidator(currentlyElected[i]); electedGroupsHelper.add(group); @@ -314,48 +310,48 @@ contract E2E_EpochManager is Devchain, ECDSAHelper08 { contract E2E_EpochManager_InitializeSystem is E2E_EpochManager { function setUp() public override { super.setUp(); - whenL2(vm); + whenL2(); } function test_shouldRevert_WhenCalledByNonEnabler() public { vm.expectRevert("msg.sender is not Enabler"); - epochManager.initializeSystem(1, 1, firstElected); + epochManagerContract.initializeSystem(1, 1, firstElected); } function test_ShouldInitializeSystem() public { vm.prank(epochManagerEnabler); - epochManager.initializeSystem(42, 43, firstElected); + epochManagerContract.initializeSystem(42, 43, firstElected); - assertEq(epochManager.firstKnownEpoch(), 42); - assertEq(epochManager.getCurrentEpochNumber(), 42); + assertEq(epochManagerContract.firstKnownEpoch(), 42); + assertEq(epochManagerContract.getCurrentEpochNumber(), 42); - assertTrue(epochManager.systemAlreadyInitialized()); + assertTrue(epochManagerContract.systemAlreadyInitialized()); } } contract E2E_EpochManager_GetCurrentEpoch is E2E_EpochManager { function setUp() public override { super.setUp(); - whenL2(vm); + whenL2(); } function test_Revert_WhenSystemNotInitialized() public { vm.expectRevert("Epoch system not initialized"); - epochManager.getCurrentEpoch(); + epochManagerContract.getCurrentEpoch(); } function test_ReturnExpectedValues() public { vm.prank(epochManagerEnabler); - epochManager.initializeSystem(42, 43, firstElected); + epochManagerContract.initializeSystem(42, 43, firstElected); - assertEq(epochManager.firstKnownEpoch(), 42); - assertEq(epochManager.getCurrentEpochNumber(), 42); + assertEq(epochManagerContract.firstKnownEpoch(), 42); + assertEq(epochManagerContract.getCurrentEpochNumber(), 42); ( uint256 firstBlock, uint256 lastBlock, uint256 startTimestamp, uint256 rewardsBlock - ) = epochManager.getCurrentEpoch(); + ) = epochManagerContract.getCurrentEpoch(); assertEq(firstBlock, 43); assertEq(lastBlock, 0); assertEq(startTimestamp, block.timestamp); @@ -367,7 +363,7 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { function setUp() public override { super.setUp(); activateValidators(); - whenL2(vm); + whenL2(); validatorsArray = getValidators().getRegisteredValidators(); groups = getValidators().getRegisteredValidatorGroups(); @@ -389,12 +385,12 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { vm.stopPrank(); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(1, 1, firstElected); + epochManagerContract.initializeSystem(1, 1, firstElected); } function test_shouldHaveInitialValues() public { - assertEq(epochManager.firstKnownEpoch(), 1); - assertEq(epochManager.getCurrentEpochNumber(), 1); + assertEq(epochManagerContract.firstKnownEpoch(), 1); + assertEq(epochManagerContract.getCurrentEpochNumber(), 1); // get getEpochProcessingState ( @@ -403,7 +399,7 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { uint256 totalRewardsVote, uint256 totalRewardsCommunity, uint256 totalRewardsCarbonFund - ) = epochManager.getEpochProcessingState(); + ) = epochManagerContract.getEpochProcessingState(); assertEq(status, 0); // Not started assertEq(perValidatorReward, 0); assertEq(totalRewardsVote, 0); @@ -412,9 +408,9 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { } function test_shouldStartNextEpochProcessing() public { - timeTravel(vm, epochDuration + 1); + timeTravel(epochDuration + 1); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); ( uint256 status, @@ -422,7 +418,7 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { uint256 totalRewardsVote, uint256 totalRewardsCommunity, uint256 totalRewardsCarbonFund - ) = epochManager.getEpochProcessingState(); + ) = epochManagerContract.getEpochProcessingState(); assertEq(status, 1); // Started assertGt(perValidatorReward, 0, "perValidatorReward"); assertGt(totalRewardsVote, 0, "totalRewardsVote"); @@ -439,10 +435,10 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { function setUp() public override { super.setUp(); activateValidators(); - whenL2(vm); + whenL2(); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(1, 1, firstElected); + epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); groups = getValidators().getRegisteredValidatorGroups(); @@ -463,8 +459,8 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { vm.stopPrank(); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); } function test_shouldFinishNextEpochProcessing() public { @@ -473,38 +469,38 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - uint256 currentEpoch = epochManager.getCurrentEpochNumber(); - address[] memory currentlyElected = epochManager.getElectedAccounts(); + uint256 currentEpoch = epochManagerContract.getCurrentEpochNumber(); + address[] memory currentlyElected = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { originalyElected.add(currentlyElected[i]); } // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - assertEq(currentEpoch + 1, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 1, epochManagerContract.getCurrentEpochNumber()); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(currentlyElected[i]), true); } - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); - assertEq(currentEpoch + 2, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 2, epochManagerContract.getCurrentEpochNumber()); - address[] memory newlyElected2 = epochManager.getElectedAccounts(); + address[] memory newlyElected2 = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(newlyElected2[i]), true); @@ -516,39 +512,42 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { 1 ); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); groups.push(newValidatorGroup); validatorsArray.push(newValidator); - assertEq(epochManager.getElectedAccounts().length, validators.getRegisteredValidators().length); + assertEq( + epochManagerContract.getElectedAccounts().length, + validators.getRegisteredValidators().length + ); assertEq(groups.length, validators.getRegisteredValidatorGroups().length); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); - assertEq(epochManager.getElectedAccounts().length, validatorsArray.length); + assertEq(epochManagerContract.getElectedAccounts().length, validatorsArray.length); // lower the number of electable validators vm.prank(election.owner()); election.setElectableValidators(1, validatorsArray.length - 1); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); ( @@ -557,14 +556,14 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { uint256 totalRewardsVoter, uint256 totalRewardsCommunity, uint256 totalRewardsCarbonFund - ) = epochManager.getEpochProcessingState(); + ) = epochManagerContract.getEpochProcessingState(); assertEq(perValidatorReward, 0, "perValidatorReward"); assertEq(totalRewardsVoter, 0, "totalRewardsVoter"); assertEq(totalRewardsCommunity, 0, "totalRewardsCommunity"); assertEq(totalRewardsCarbonFund, 0, "totalRewardsCarbonFund"); - assertEq(epochManager.getElectedAccounts().length, validatorsArray.length - 1); + assertEq(epochManagerContract.getElectedAccounts().length, validatorsArray.length - 1); } function clearElectedGroupsHelper() internal { @@ -582,10 +581,10 @@ contract E2E_GasTest_Setup is E2E_EpochManager { function setUpHelper(uint256 validatorGroupCount, uint256 validatorPerGroupCount) internal { activateValidators(); - whenL2(vm); + whenL2(); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(1, 1, firstElected); + epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); groups = getValidators().getRegisteredValidatorGroups(); @@ -604,46 +603,46 @@ contract E2E_GasTest_Setup is E2E_EpochManager { vm.stopPrank(); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); address[] memory lessers; address[] memory greaters; GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - uint256 currentEpoch = epochManager.getCurrentEpochNumber(); - address[] memory currentlyElected = epochManager.getElectedAccounts(); + uint256 currentEpoch = epochManagerContract.getCurrentEpochNumber(); + address[] memory currentlyElected = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { originalyElected.add(currentlyElected[i]); } // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - assertEq(currentEpoch + 1, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 1, epochManagerContract.getCurrentEpochNumber()); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(currentlyElected[i]), true); } - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); - assertEq(currentEpoch + 2, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 2, epochManagerContract.getCurrentEpochNumber()); - address[] memory newlyElected2 = epochManager.getElectedAccounts(); + address[] memory newlyElected2 = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(newlyElected2[i]), true); @@ -653,24 +652,24 @@ contract E2E_GasTest_Setup is E2E_EpochManager { registerNewValidatorGroupWithValidator(i, validatorPerGroupCount); } - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); activateValidators(); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); groups = getCurrentlyElectedGroups(); - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); } } @@ -692,12 +691,12 @@ contract E2E_GasTest1_FinishNextEpochProcess is E2E_GasTest_Setup { GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); uint256 gasLeftBefore1 = gasleft(); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); uint256 gasLeftAfter1 = gasleft(); console.log("validator groups: 120"); console.log("validators per group: 2"); console.log("finishNextEpochProcess gas used 2: ", gasLeftBefore1 - gasLeftAfter1); - console.log("elected count2: ", epochManager.getElectedAccounts().length); + console.log("elected count2: ", epochManagerContract.getElectedAccounts().length); } } @@ -719,12 +718,12 @@ contract E2E_GasTest2_FinishNextEpochProcess is E2E_GasTest_Setup { GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); uint256 gasLeftBefore1 = gasleft(); - epochManager.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); uint256 gasLeftAfter1 = gasleft(); console.log("validator groups: 60"); console.log("validators per group: 2"); console.log("finishNextEpochProcess gas used 2: ", gasLeftBefore1 - gasLeftAfter1); - console.log("elected count2: ", epochManager.getElectedAccounts().length); + console.log("elected count2: ", epochManagerContract.getElectedAccounts().length); } } @@ -735,10 +734,10 @@ contract E2E_FinishNextEpochProcess_Split is E2E_GasTest_Setup { super.setUp(); activateValidators(); - whenL2(vm); + whenL2(); vm.prank(epochManagerEnabler); - epochManager.initializeSystem(1, 1, firstElected); + epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); groups = getValidators().getRegisteredValidatorGroups(); @@ -757,53 +756,52 @@ contract E2E_FinishNextEpochProcess_Split is E2E_GasTest_Setup { vm.stopPrank(); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); address[] memory lessers; address[] memory greaters; GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - uint256 currentEpoch = epochManager.getCurrentEpochNumber(); - address[] memory currentlyElected = epochManager.getElectedAccounts(); + uint256 currentEpoch = epochManagerContract.getCurrentEpochNumber(); + address[] memory currentlyElected = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { originalyElected.add(currentlyElected[i]); } // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { - epochManager.processGroup(groups[i], lessers[i], greaters[i]); + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); } - assertEq(currentEpoch + 1, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 1, epochManagerContract.getCurrentEpochNumber()); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(currentlyElected[i]), true); } - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); // wait some time before finishing - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { - epochManager.processGroup(groups[i], lessers[i], greaters[i]); + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); } - // epochManager.finishNextEpochProcess(groups, lessers, greaters); assertGroupWithVotes(groupWithVotes); - assertEq(currentEpoch + 2, epochManager.getCurrentEpochNumber()); + assertEq(currentEpoch + 2, epochManagerContract.getCurrentEpochNumber()); - address[] memory newlyElected2 = epochManager.getElectedAccounts(); + address[] memory newlyElected2 = epochManagerContract.getElectedAccounts(); for (uint256 i = 0; i < currentlyElected.length; i++) { assertEq(originalyElected.contains(newlyElected2[i]), true); @@ -815,28 +813,28 @@ contract E2E_FinishNextEpochProcess_Split is E2E_GasTest_Setup { registerNewValidatorGroupWithValidator(i, validatorPerGroupCount); } - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { - epochManager.processGroup(groups[i], lessers[i], greaters[i]); + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); } activateValidators(); - timeTravel(vm, epochDuration + 1); - epochManager.startNextEpochProcess(); + timeTravel(epochDuration + 1); + epochManagerContract.startNextEpochProcess(); groups = getCurrentlyElectedGroups(); - timeTravel(vm, epochDuration / 2); - blockTravel(vm, 100); + timeTravel(epochDuration / 2); + blockTravel(100); } /** @@ -850,11 +848,11 @@ contract E2E_FinishNextEpochProcess_Split is E2E_GasTest_Setup { address[] memory greaters; GroupWithVotes[] memory groupWithVotes; (lessers, greaters, groupWithVotes) = getLessersAndGreaters(groups); - epochManager.setToProcessGroups(); + epochManagerContract.setToProcessGroups(); for (uint256 i = 0; i < groups.length; i++) { uint256 gasLeftBefore1 = gasleft(); - epochManager.processGroup(groups[i], lessers[i], greaters[i]); + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); uint256 gasLeftAfter1 = gasleft(); console.log("processGroup gas used: ", gasLeftBefore1 - gasLeftAfter1); } diff --git a/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol b/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol index bebf0ed4f44..e59aaf14297 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/FeeCurrencyDirectory.t.sol @@ -1,7 +1,6 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; diff --git a/packages/protocol/test-sol/devchain/e2e/utils.sol b/packages/protocol/test-sol/devchain/e2e/utils.sol index 0c6798dbcb4..452323ce03b 100644 --- a/packages/protocol/test-sol/devchain/e2e/utils.sol +++ b/packages/protocol/test-sol/devchain/e2e/utils.sol @@ -19,13 +19,10 @@ import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import "@test-sol/utils08.sol"; contract Devchain is Utils08 { - // Used in exceptional circumstances when a contract is not in UsingRegistry.sol - IRegistry devchainRegistry = IRegistry(REGISTRY_ADDRESS); - // All core contracts that are expected to be in the Registry on the devchain ISortedOracles sortedOracles; FeeCurrencyDirectory feeCurrencyDirectory; - IEpochManager epochManager; + IEpochManager epochManagerContract; ICeloUnreleasedTreasury celoUnreleasedTreasury; IValidators validators; IAccounts accounts; @@ -34,16 +31,13 @@ contract Devchain is Utils08 { ILockedCelo lockedCelo; constructor() { - // The following line is required by UsingRegistry.sol - setRegistry(REGISTRY_ADDRESS); - // Fetch all core contracts that are expeceted to be in the Registry on the devchain sortedOracles = getSortedOracles(); feeCurrencyDirectory = FeeCurrencyDirectory( - devchainRegistry.getAddressForStringOrDie("FeeCurrencyDirectory") + registryContract.getAddressForStringOrDie("FeeCurrencyDirectory") ); // FeeCurrencyDirectory is not in UsingRegistry.sol - epochManager = getEpochManager(); + epochManagerContract = getEpochManager(); celoUnreleasedTreasury = getCeloUnreleasedTreasury(); validators = getValidators(); accounts = getAccounts(); diff --git a/packages/protocol/test-sol/utils/ECDSAHelper08.sol b/packages/protocol/test-sol/utils/ECDSAHelper08.sol index 3c761bb9e76..6cbc87de8ee 100644 --- a/packages/protocol/test-sol/utils/ECDSAHelper08.sol +++ b/packages/protocol/test-sol/utils/ECDSAHelper08.sol @@ -1,8 +1,8 @@ pragma solidity >=0.5.13 <0.8.20; -import "celo-foundry-8/Test.sol"; +import { Utils08 } from "@test-sol/utils08.sol"; import "@test-sol/utils/SECP256K1.sol"; -contract ECDSAHelper08 is Test { +contract ECDSAHelper08 is Utils08 { ISECP256K1 sECP256K1; function addressToPublicKey( From 5b3dd2d4b265421cf066ef9e4b619f12d47aeba1 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:27:07 -0500 Subject: [PATCH 08/23] passing migration test --- .../migration/IntegrationValidators.t.sol | 1 - .../devchain/migration/Migration.t.sol | 69 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol b/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol index 24f89201956..a20042556e7 100644 --- a/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol +++ b/packages/protocol/test-sol/devchain/migration/IntegrationValidators.t.sol @@ -1,7 +1,6 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -// import "celo-foundry-8/Test.sol"; import { Devchain } from "@test-sol/devchain/e2e/utils.sol"; contract IntegrationsValidators is Devchain { diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index 9caa8da42d5..8cc23cd02d4 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -1,13 +1,11 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -// import "celo-foundry-8/Test.sol"; +import { Utils08 } from "@test-sol/utils08.sol"; -// import { Utils08 } from "@test-sol/utils08.sol"; -// import { TestConstants } from "@test-sol/constants.sol"; import { MigrationsConstants } from "@migrations-sol/constants.sol"; -import { FeeCurrencyDirectory } from "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; +import { FeeCurrencyDirectory } from "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; import "@celo-contracts/common/interfaces/IProxy.sol"; import "@celo-contracts/common/interfaces/ICeloToken.sol"; @@ -16,7 +14,6 @@ import "@celo-contracts/common/interfaces/IEpochManager.sol"; import "@celo-contracts/common/interfaces/IEpochManagerEnabler.sol"; import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import "@celo-contracts/governance/interfaces/IElection.sol"; - import "@celo-contracts/governance/interfaces/IValidators.sol"; import "@celo-contracts-8/common/interfaces/IPrecompiles.sol"; @@ -24,12 +21,15 @@ import "@celo-contracts-8/common/interfaces/IScoreManager.sol"; import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; -contract IntegrationTest is Test, TestConstants, Utils08 { - IRegistry registry = IRegistry(REGISTRY_ADDRESS); +import { console2 } from "forge-std-8/console2.sol"; +// contract IntegrationTest is Test, TestConstants, Utils08 { +contract IntegrationTest is Utils08 { uint256 constant RESERVE_BALANCE = 69411663406170917420347916; // current as of 08/20/24 - // function setUp() public virtual {} + function setUp() public virtual override { + registry = IRegistry(REGISTRY_ADDRESS); + } /** * @notice Removes CBOR encoded metadata from the tail of the deployedBytecode. @@ -69,6 +69,7 @@ contract RegistryIntegrationTest is IntegrationTest, MigrationsConstants { function test_shouldHaveAddressInRegistry() public view { for (uint256 i = 0; i < contractsInRegistry.length; i++) { string memory contractName = contractsInRegistry[i]; + console2.log("### current contract", contractName); address contractAddress = registry.getAddressFor(keccak256(abi.encodePacked(contractName))); console2.log(contractName, "address in Registry is: ", contractAddress); assert(contractAddress != address(0)); @@ -146,8 +147,8 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { ICeloToken celoToken; IAccounts accountsContract; IValidators validatorsContract; - IEpochManager epochManager; - IEpochManagerEnabler epochManagerEnabler; + IEpochManager epochManagerContract; + IEpochManagerEnabler epochManagerEnablerContrtact; IScoreManager scoreManager; IElection election; ICeloUnreleasedTreasury celoUnreleasedTreasury; @@ -165,7 +166,8 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { uint256[] groupScore = [5e23, 7e23, 1e24]; uint256[] validatorScore = [1e23, 1e23, 1e23, 1e23, 1e23, 1e23]; - function setUp() public { + function setUp() public override { + super.setUp(); randomAddress = actor("randomAddress"); validatorsContract = IValidators(registry.getAddressForStringOrDie("Validators")); @@ -188,18 +190,18 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { vm.prank(address(0)); celoToken.mint(randomAddress, L1_MINTED_CELO_SUPPLY - RESERVE_BALANCE); // mint outstanding l1 supply before L2. - epochManager = IEpochManager(registry.getAddressForStringOrDie("EpochManager")); - epochManagerEnabler = IEpochManagerEnabler( + epochManagerContract = IEpochManager(registry.getAddressForStringOrDie("EpochManager")); + epochManagerEnablerContrtact = IEpochManagerEnabler( registry.getAddressForStringOrDie("EpochManagerEnabler") ); } function activateValidators() public { address[] memory registeredValidators = validatorsContract.getRegisteredValidators(); - travelEpochL1(vm); - travelEpochL1(vm); - travelEpochL1(vm); - travelEpochL1(vm); + travelEpochL1(); + travelEpochL1(); + travelEpochL1(); + travelEpochL1(); for (uint256 i = 0; i < registeredValidators.length; i++) { (, , address validatorGroup, , ) = validatorsContract.getValidator(registeredValidators[i]); if (election.getPendingVotesForGroup(validatorGroup) == 0) { @@ -213,36 +215,37 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { function test_Reverts_whenSystemNotInitialized() public { vm.expectRevert("Epoch system not initialized"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { // fund treasury vm.prank(address(0)); celoToken.mint(unreleasedTreasury, L2_INITIAL_STASH_BALANCE); + vm.deal(unreleasedTreasury, L2_INITIAL_STASH_BALANCE); uint256 l1EpochNumber = IPrecompiles(address(validatorsContract)).getEpochNumber(); - vm.prank(address(epochManagerEnabler)); - epochManager.initializeSystem(l1EpochNumber, block.number, validatorsList); + vm.prank(address(epochManagerEnablerContrtact)); + epochManagerContract.initializeSystem(l1EpochNumber, block.number, validatorsList); vm.expectRevert("Epoch is not ready to start"); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); } function test_Reverts_whenAlreadyInitialized() public { _MockL2Migration(validatorsList); - vm.prank(address(epochManagerEnabler)); + vm.prank(address(epochManagerEnablerContrtact)); vm.expectRevert("Epoch system already initialized"); - epochManager.initializeSystem(100, block.number, firstElected); + epochManagerContract.initializeSystem(100, block.number, firstElected); } function test_Reverts_whenTransferingCeloToUnreleasedTreasury() public { _MockL2Migration(validatorsList); - blockTravel(vm, 43200); - timeTravel(vm, DAY); + blockTravel(43200); + timeTravel(DAY); vm.prank(randomAddress); @@ -253,13 +256,13 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { function test_SetsCurrentRewardBlock() public { _MockL2Migration(validatorsList); - blockTravel(vm, L2_BLOCK_IN_EPOCH); - timeTravel(vm, DAY); + blockTravel(L2_BLOCK_IN_EPOCH); + timeTravel(DAY); - epochManager.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); - (, , , uint256 _currentRewardsBlock) = epochManager.getCurrentEpoch(); - (uint256 status, , , , ) = epochManager.getEpochProcessingState(); + (, , , uint256 _currentRewardsBlock) = epochManagerContract.getCurrentEpoch(); + (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); assertEq(_currentRewardsBlock, block.number); assertEq(status, 1); } @@ -277,12 +280,12 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { vm.prank(address(0)); celoToken.mint(unreleasedTreasury, L2_INITIAL_STASH_BALANCE); - whenL2(vm); + whenL2(); _setValidatorL2Score(); - vm.prank(address(epochManagerEnabler)); + vm.prank(address(epochManagerEnablerContrtact)); - epochManager.initializeSystem(l1EpochNumber, block.number, firstElected); + epochManagerContract.initializeSystem(l1EpochNumber, block.number, firstElected); } function _setValidatorL2Score() internal { From ee99877d21db13f9a80dfc3414e6d01ea7b87d8e Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Wed, 8 Jan 2025 13:20:12 -0500 Subject: [PATCH 09/23] override Utils08 setup and cleanup --- .../devchain/e2e/common/EpochManager.t.sol | 2 +- .../protocol/test-sol/devchain/e2e/utils.sol | 5 +++-- .../devchain/migration/Migration.t.sol | 22 +++++++++---------- .../test-sol/unit/common/EpochManager.t.sol | 1 - 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index 6e0fca26bd6..02ece21de92 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -39,7 +39,7 @@ contract E2E_EpochManager is ECDSAHelper08, Devchain { EnumerableSet.AddressSet internal electedGroupsHelper; - function setUp() public virtual override { + function setUp() public virtual override(Devchain, Utils08) { epochManagerOwner = Ownable(address(epochManagerContract)).owner(); epochManagerEnabler = registryContract.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID); firstElected = getValidators().getRegisteredValidators(); diff --git a/packages/protocol/test-sol/devchain/e2e/utils.sol b/packages/protocol/test-sol/devchain/e2e/utils.sol index 452323ce03b..a7e84f401bb 100644 --- a/packages/protocol/test-sol/devchain/e2e/utils.sol +++ b/packages/protocol/test-sol/devchain/e2e/utils.sol @@ -1,7 +1,6 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -// import "@celo-contracts-8/common/UsingRegistry2.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; import { IEpochManager } from "@celo-contracts/common/interfaces/IEpochManager.sol"; import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; @@ -15,7 +14,6 @@ import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts/stability/interfaces/ISortedOracles.sol"; import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; -// import { TestConstants } from "@test-sol/constants.sol"; import "@test-sol/utils08.sol"; contract Devchain is Utils08 { @@ -49,4 +47,7 @@ contract Devchain is Utils08 { // TODO: Consider asserting that all contracts we expect are available in the Devchain class // (see list in migrations_sol/constants.sol) } + function setUp() public virtual override { + // Added to avoid adding a setup function in each e2e test, when its not required. + } } diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index 8cc23cd02d4..99b6cd6c985 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -5,7 +5,6 @@ import { Utils08 } from "@test-sol/utils08.sol"; import { MigrationsConstants } from "@migrations-sol/constants.sol"; -import { FeeCurrencyDirectory } from "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; import "@celo-contracts/common/interfaces/IProxy.sol"; import "@celo-contracts/common/interfaces/ICeloToken.sol"; @@ -16,6 +15,7 @@ import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import "@celo-contracts/governance/interfaces/IElection.sol"; import "@celo-contracts/governance/interfaces/IValidators.sol"; +import { FeeCurrencyDirectory } from "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts-8/common/interfaces/IPrecompiles.sol"; import "@celo-contracts-8/common/interfaces/IScoreManager.sol"; @@ -23,7 +23,6 @@ import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; import { console2 } from "forge-std-8/console2.sol"; -// contract IntegrationTest is Test, TestConstants, Utils08 { contract IntegrationTest is Utils08 { uint256 constant RESERVE_BALANCE = 69411663406170917420347916; // current as of 08/20/24 @@ -66,16 +65,6 @@ contract IntegrationTest is Utils08 { contract RegistryIntegrationTest is IntegrationTest, MigrationsConstants { IProxy proxy; - function test_shouldHaveAddressInRegistry() public view { - for (uint256 i = 0; i < contractsInRegistry.length; i++) { - string memory contractName = contractsInRegistry[i]; - console2.log("### current contract", contractName); - address contractAddress = registry.getAddressFor(keccak256(abi.encodePacked(contractName))); - console2.log(contractName, "address in Registry is: ", contractAddress); - assert(contractAddress != address(0)); - } - } - function test_shouldHaveCorrectBytecode() public { // Converting contract names to hashes for comparison bytes32 hashAccount = keccak256(abi.encodePacked("Accounts")); @@ -141,6 +130,15 @@ contract RegistryIntegrationTest is IntegrationTest, MigrationsConstants { } } } + + function test_shouldHaveAddressInRegistry() public view { + for (uint256 i = 0; i < contractsInRegistry.length; i++) { + string memory contractName = contractsInRegistry[i]; + address contractAddress = registry.getAddressFor(keccak256(abi.encodePacked(contractName))); + console2.log(contractName, "address in Registry is: ", contractAddress); + assert(contractAddress != address(0)); + } + } } contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 9937272ba68..a1c22431194 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -147,7 +147,6 @@ contract EpochManagerTest is Utils08 { election.setElectedValidators(members); - // deployCodeTo("MockRegistry.sol", abi.encode(false), PROXY_ADMIN_ADDRESS); vm.prank(epochManagerEnabler); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); From 97d7ef34b98720218d0211ac6c711bcfdaa3f6a7 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:16:36 -0500 Subject: [PATCH 10/23] change Utils08 filename --- .../protocol/test-sol/{utils08.sol => TestWithutils08.sol} | 2 +- .../protocol/test-sol/devchain/e2e/common/EpochManager.t.sol | 2 +- packages/protocol/test-sol/devchain/e2e/utils.sol | 4 ++-- packages/protocol/test-sol/devchain/migration/Migration.t.sol | 4 ++-- packages/protocol/test-sol/unit/common/EpochManager.t.sol | 4 ++-- .../protocol/test-sol/unit/common/EpochManagerEnabler.t.sol | 4 ++-- .../protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol | 4 ++-- packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol | 4 ++-- packages/protocol/test-sol/utils/ECDSAHelper08.sol | 4 ++-- packages/protocol/test-sol/utils/WhenL2-08.sol | 4 ++-- 10 files changed, 18 insertions(+), 18 deletions(-) rename packages/protocol/test-sol/{utils08.sol => TestWithutils08.sol} (97%) diff --git a/packages/protocol/test-sol/utils08.sol b/packages/protocol/test-sol/TestWithutils08.sol similarity index 97% rename from packages/protocol/test-sol/utils08.sol rename to packages/protocol/test-sol/TestWithutils08.sol index 17ea9a050f4..7d36134263d 100644 --- a/packages/protocol/test-sol/utils08.sol +++ b/packages/protocol/test-sol/TestWithutils08.sol @@ -10,7 +10,7 @@ import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; -contract Utils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { +contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { IRegistry registry; PrecompileHandler ph; EpochManager_WithMocks public epochManager; diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index 02ece21de92..91eb44b6ccb 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -39,7 +39,7 @@ contract E2E_EpochManager is ECDSAHelper08, Devchain { EnumerableSet.AddressSet internal electedGroupsHelper; - function setUp() public virtual override(Devchain, Utils08) { + function setUp() public virtual override(Devchain, TestWithUtils08) { epochManagerOwner = Ownable(address(epochManagerContract)).owner(); epochManagerEnabler = registryContract.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID); firstElected = getValidators().getRegisteredValidators(); diff --git a/packages/protocol/test-sol/devchain/e2e/utils.sol b/packages/protocol/test-sol/devchain/e2e/utils.sol index a7e84f401bb..51db1620ee1 100644 --- a/packages/protocol/test-sol/devchain/e2e/utils.sol +++ b/packages/protocol/test-sol/devchain/e2e/utils.sol @@ -14,9 +14,9 @@ import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts/stability/interfaces/ISortedOracles.sol"; import "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; -import "@test-sol/utils08.sol"; +import "@test-sol/TestWithUtils08.sol"; -contract Devchain is Utils08 { +contract Devchain is TestWithUtils08 { // All core contracts that are expected to be in the Registry on the devchain ISortedOracles sortedOracles; FeeCurrencyDirectory feeCurrencyDirectory; diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index 99b6cd6c985..15ccb7f6008 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { MigrationsConstants } from "@migrations-sol/constants.sol"; @@ -23,7 +23,7 @@ import "@openzeppelin/contracts8/token/ERC20/IERC20.sol"; import { console2 } from "forge-std-8/console2.sol"; -contract IntegrationTest is Utils08 { +contract IntegrationTest is TestWithUtils08 { uint256 constant RESERVE_BALANCE = 69411663406170917420347916; // current as of 08/20/24 function setUp() public virtual override { diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index a1c22431194..1908b589845 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -15,10 +15,10 @@ import { IMockValidators } from "@celo-contracts-8/governance/test/IMockValidato import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewardsMock.sol"; import "@celo-contracts-8/stability/test/MockStableToken.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; -contract EpochManagerTest is Utils08 { +contract EpochManagerTest is TestWithUtils08 { EpochManager_WithMocks epochManagerContract; MockSortedOracles sortedOracles; diff --git a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol index 8eb9ffcadfe..5dd45835b42 100644 --- a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol @@ -9,11 +9,11 @@ import "@celo-contracts-8/common/test/MockCeloToken.sol"; import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewardsMock.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; -contract EpochManagerEnablerTest is Utils08 { +contract EpochManagerEnablerTest is TestWithUtils08 { EpochManagerEnablerMock epochManagerEnablerContract; MockCeloUnreleasedTreasury celoUnreleasedTreasury; MockCeloToken08 celoToken; diff --git a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol index c053f1d6b48..4c015e76746 100644 --- a/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol +++ b/packages/protocol/test-sol/unit/common/FeeCurrencyDirectory.t.sol @@ -1,13 +1,13 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.7 <0.8.20; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import "@test-sol/utils/WhenL2-08.sol"; import "@celo-contracts-8/common/FeeCurrencyDirectory.sol"; import "@celo-contracts-8/common/mocks/MockOracle.sol"; -contract FeeCurrencyDirectoryTest is Utils08 { +contract FeeCurrencyDirectoryTest is TestWithUtils08 { FeeCurrencyDirectory directory; MockOracle oracle; address nonOwner; diff --git a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol index a365aeff8ed..cbb83d24ab1 100644 --- a/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol +++ b/packages/protocol/test-sol/unit/common/ProxyFactory08.t.sol @@ -4,9 +4,9 @@ pragma solidity ^0.8.15; import "@celo-contracts-8/common/ProxyFactory08.sol"; import "@celo-contracts/common/interfaces/IProxy.sol"; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; -contract ProxyFactoryTest is Utils08 { +contract ProxyFactoryTest is TestWithUtils08 { ProxyFactory08 proxyFactory08; bytes proxyInitCode; address constant owner = address(0xAA963FC97281d9632d96700aB62A4D1340F9a28a); diff --git a/packages/protocol/test-sol/utils/ECDSAHelper08.sol b/packages/protocol/test-sol/utils/ECDSAHelper08.sol index 6cbc87de8ee..e377a744c81 100644 --- a/packages/protocol/test-sol/utils/ECDSAHelper08.sol +++ b/packages/protocol/test-sol/utils/ECDSAHelper08.sol @@ -1,8 +1,8 @@ pragma solidity >=0.5.13 <0.8.20; -import { Utils08 } from "@test-sol/utils08.sol"; +import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import "@test-sol/utils/SECP256K1.sol"; -contract ECDSAHelper08 is Utils08 { +contract ECDSAHelper08 is TestWithUtils08 { ISECP256K1 sECP256K1; function addressToPublicKey( diff --git a/packages/protocol/test-sol/utils/WhenL2-08.sol b/packages/protocol/test-sol/utils/WhenL2-08.sol index 9057091c52a..802e66a6ee3 100644 --- a/packages/protocol/test-sol/utils/WhenL2-08.sol +++ b/packages/protocol/test-sol/utils/WhenL2-08.sol @@ -1,9 +1,9 @@ pragma solidity >=0.5.13 <0.9.0; pragma experimental ABIEncoderV2; -import "@test-sol/utils08.sol"; +import "@test-sol/TestWithUtils08.sol"; -contract WhenL2 is Utils08 { +contract WhenL2 is TestWithUtils08 { function setUp() public virtual override { super.setUp(); whenL2WithEpochManagerInitialization(); From 48f0b7fca18e4477fd0942c582775ce30f48143f Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 10 Jan 2025 13:18:34 -0500 Subject: [PATCH 11/23] passing devchain --- packages/protocol/test-sol/constants.sol | 1 + .../devchain/e2e/common/EpochManager.t.sol | 19 +++++++++++-------- .../devchain/migration/Migration.t.sol | 11 +++++------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/protocol/test-sol/constants.sol b/packages/protocol/test-sol/constants.sol index 0b54aa9cb8a..8ca9372f1c2 100644 --- a/packages/protocol/test-sol/constants.sol +++ b/packages/protocol/test-sol/constants.sol @@ -13,6 +13,7 @@ contract TestConstants { // uint256 constant WEEK = 7 * DAY; uint256 public constant YEAR = 365 * 86400; // uint256 public constant YEAR = 365 * DAY; + uint256 public constant L1_BLOCK_IN_EPOCH = 17280; uint256 public constant L2_BLOCK_IN_EPOCH = 43200; // Contract names diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index 91eb44b6ccb..d50fbe0577c 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -24,6 +24,7 @@ contract E2E_EpochManager is ECDSAHelper08, Devchain { } address epochManagerOwner; + address epochManagerEnablerAddress; address[] firstElected; uint256 epochDuration; @@ -39,9 +40,11 @@ contract E2E_EpochManager is ECDSAHelper08, Devchain { EnumerableSet.AddressSet internal electedGroupsHelper; - function setUp() public virtual override(Devchain, TestWithUtils08) { + function setUp() public virtual override(TestWithUtils08, Devchain) { epochManagerOwner = Ownable(address(epochManagerContract)).owner(); - epochManagerEnabler = registryContract.getAddressForOrDie(EPOCH_MANAGER_ENABLER_REGISTRY_ID); + epochManagerEnablerAddress = registryContract.getAddressForOrDie( + EPOCH_MANAGER_ENABLER_REGISTRY_ID + ); firstElected = getValidators().getRegisteredValidators(); epochDuration = epochManagerContract.epochDuration(); @@ -319,7 +322,7 @@ contract E2E_EpochManager_InitializeSystem is E2E_EpochManager { } function test_ShouldInitializeSystem() public { - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(42, 43, firstElected); assertEq(epochManagerContract.firstKnownEpoch(), 42); @@ -340,7 +343,7 @@ contract E2E_EpochManager_GetCurrentEpoch is E2E_EpochManager { } function test_ReturnExpectedValues() public { - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(42, 43, firstElected); assertEq(epochManagerContract.firstKnownEpoch(), 42); @@ -384,7 +387,7 @@ contract E2E_EpochManager_StartNextEpochProcess is E2E_EpochManager { vm.stopPrank(); - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(1, 1, firstElected); } @@ -437,7 +440,7 @@ contract E2E_EpochManager_FinishNextEpochProcess is E2E_EpochManager { activateValidators(); whenL2(); - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); @@ -583,7 +586,7 @@ contract E2E_GasTest_Setup is E2E_EpochManager { activateValidators(); whenL2(); - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); @@ -736,7 +739,7 @@ contract E2E_FinishNextEpochProcess_Split is E2E_GasTest_Setup { activateValidators(); whenL2(); - vm.prank(epochManagerEnabler); + vm.prank(epochManagerEnablerAddress); epochManagerContract.initializeSystem(1, 1, firstElected); validatorsArray = getValidators().getRegisteredValidators(); diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index 15ccb7f6008..5c1bee88e02 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -143,10 +143,9 @@ contract RegistryIntegrationTest is IntegrationTest, MigrationsConstants { contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { ICeloToken celoToken; - IAccounts accountsContract; IValidators validatorsContract; IEpochManager epochManagerContract; - IEpochManagerEnabler epochManagerEnablerContrtact; + IEpochManagerEnabler epochManagerEnablerContract; IScoreManager scoreManager; IElection election; ICeloUnreleasedTreasury celoUnreleasedTreasury; @@ -189,7 +188,7 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { celoToken.mint(randomAddress, L1_MINTED_CELO_SUPPLY - RESERVE_BALANCE); // mint outstanding l1 supply before L2. epochManagerContract = IEpochManager(registry.getAddressForStringOrDie("EpochManager")); - epochManagerEnablerContrtact = IEpochManagerEnabler( + epochManagerEnablerContract = IEpochManagerEnabler( registry.getAddressForStringOrDie("EpochManagerEnabler") ); } @@ -224,7 +223,7 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { uint256 l1EpochNumber = IPrecompiles(address(validatorsContract)).getEpochNumber(); - vm.prank(address(epochManagerEnablerContrtact)); + vm.prank(address(epochManagerEnablerContract)); epochManagerContract.initializeSystem(l1EpochNumber, block.number, validatorsList); vm.expectRevert("Epoch is not ready to start"); @@ -234,7 +233,7 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { function test_Reverts_whenAlreadyInitialized() public { _MockL2Migration(validatorsList); - vm.prank(address(epochManagerEnablerContrtact)); + vm.prank(address(epochManagerEnablerContract)); vm.expectRevert("Epoch system already initialized"); epochManagerContract.initializeSystem(100, block.number, firstElected); } @@ -281,7 +280,7 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { whenL2(); _setValidatorL2Score(); - vm.prank(address(epochManagerEnablerContrtact)); + vm.prank(address(epochManagerEnablerContract)); epochManagerContract.initializeSystem(l1EpochNumber, block.number, firstElected); } From bf5de2de3bc594444d62ee44328e3b40bb5a9e6e Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 10 Jan 2025 13:32:51 -0500 Subject: [PATCH 12/23] reorg TestWithUtils08 --- .../protocol/test-sol/TestWithutils08.sol | 89 ++++++++++++++++--- .../devchain/e2e/common/EpochManager.t.sol | 6 +- .../devchain/migration/Migration.t.sol | 6 +- .../test-sol/unit/common/EpochManager.t.sol | 19 ++-- .../interfaces/IEpochManagerEnablerMock.sol | 11 +++ .../protocol/test-sol/utils/WhenL2-08.sol | 12 +++ 6 files changed, 112 insertions(+), 31 deletions(-) create mode 100644 packages/protocol/test-sol/unit/common/interfaces/IEpochManagerEnablerMock.sol diff --git a/packages/protocol/test-sol/TestWithutils08.sol b/packages/protocol/test-sol/TestWithutils08.sol index 7d36134263d..04e07353cc9 100644 --- a/packages/protocol/test-sol/TestWithutils08.sol +++ b/packages/protocol/test-sol/TestWithutils08.sol @@ -3,8 +3,11 @@ pragma solidity >=0.5.13 <0.9.0; import { Test as ForgeTest } from "@lib/celo-foundry-8/lib/forge-std/src/Test.sol"; import { TestConstants } from "@test-sol/constants.sol"; import { PrecompileHandler } from "@test-sol/utils/PrecompileHandler.sol"; +import { IEpochManagerEnablerMock } from "@test-sol/unit/common/interfaces/IEpochManagerEnablerMock.sol"; +import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; import "@celo-contracts/common/interfaces/IRegistry.sol"; +import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; @@ -14,15 +17,24 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver IRegistry registry; PrecompileHandler ph; EpochManager_WithMocks public epochManager; + EpochManagerEnablerMock epochManagerEnabler; + + IAccounts accountsContract; + IEpochManagerEnablerMock epochManagerEnablerMockInterface; + + address accountsAddress; + address mockOracleAddress; - address public epochManagerEnabler; uint256 public constant secondsInOneBlock = 5; + uint256 numberValidators = 100; + uint256 l2EpochDuration = DAY; function setUp() public virtual { ph = new PrecompileHandler(); + ph.setEpochSize(L1_BLOCK_IN_EPOCH); setupRegistry(); - epochManagerEnabler = actor("EpochManagerEnabler"); - registry.setAddressFor(EpochManagerEnablerContract, epochManagerEnabler); + setupAccounts(); + setupEpochManagerEnabler(); setupEpochManager(); } @@ -31,12 +43,27 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver registry = IRegistry(REGISTRY_ADDRESS); } + function setupAccounts() public { + accountsAddress = actor("accountsAddress"); + deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); + accountsContract = IAccounts(accountsAddress); + registry.setAddressFor(AccountsContract, accountsAddress); + } + function setupEpochManager() public { epochManager = new EpochManager_WithMocks(); - + mockOracleAddress = actor("oracle"); + registry.setAddressFor(SortedOraclesContract, mockOracleAddress); + epochManager.initialize(REGISTRY_ADDRESS, l2EpochDuration); registry.setAddressFor(EpochManagerContract, address(epochManager)); } + function setupEpochManagerEnabler() public { + epochManagerEnabler = new EpochManagerEnablerMock(); + epochManagerEnabler.initialize(REGISTRY_ADDRESS); + registry.setAddressFor(EpochManagerEnablerContract, address(epochManagerEnabler)); + } + function timeTravel(uint256 timeDelta) public { vm.warp(block.timestamp + timeDelta); } @@ -45,11 +72,11 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver vm.roll(block.number + blockDelta); } - function travelEpochL1() public { - uint256 blocksInEpoch = 17280; + function travelNEpochL1(uint256 n) public { + uint256 blocksInEpoch = L1_BLOCK_IN_EPOCH; uint256 timeDelta = blocksInEpoch * 5; - blockTravel(blocksInEpoch); - timeTravel(timeDelta); + blockTravel(n * blocksInEpoch); + timeTravel(n * timeDelta); } // XXX: this function only increases the block number and timestamp, but does not actually change epoch. @@ -64,7 +91,7 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver if (isL2()) { travelNL2Epoch(n); } else { - travelEpochL1(); + travelNEpochL1(n); } } @@ -91,15 +118,51 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); } - function whenL2WithEpochManagerInitialization() internal { - uint256 l1EpochNumber = getEpochNumber(); + function _registerAndElectValidatorsForL2() internal { + address enablerAddr = registry.getAddressFor(EPOCH_MANAGER_ENABLER_REGISTRY_ID); + epochManagerEnablerMockInterface = IEpochManagerEnablerMock(enablerAddr); address[] memory _elected = new address[](2); _elected[0] = actor("validator"); _elected[1] = actor("otherValidator"); + vm.prank(_elected[0]); + accountsContract.createAccount(); + epochManagerEnablerMockInterface.addValidator(_elected[0]); + + vm.prank(_elected[1]); + accountsContract.createAccount(); + epochManagerEnablerMockInterface.addValidator(_elected[1]); + + for (uint256 i = 2; i < numberValidators; i++) { + vm.prank(vm.addr(i + 1)); + accountsContract.createAccount(); + + epochManagerEnablerMockInterface.addValidator(vm.addr(i + 1)); + } + travelNEpochL1(2); + } + + function whenL2WithEpochManagerInitialization() internal { + _registerAndElectValidatorsForL2(); + + epochManagerEnabler.captureEpochAndValidators(); + + whenL2(); + + epochManagerEnabler.initEpochManager(); + } + + function whenL2WithoutEpochManagerInitialization() internal { + _registerAndElectValidatorsForL2(); + + epochManagerEnablerMockInterface.captureEpochAndValidators(); + + whenL2(); + } + function whenL2WithoutEpochCapture() internal { + _registerAndElectValidatorsForL2(); + whenL2(); - vm.prank(epochManagerEnabler); - epochManager.initializeSystem(l1EpochNumber, block.number, _elected); } } diff --git a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol index d50fbe0577c..d1074a63657 100644 --- a/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/devchain/e2e/common/EpochManager.t.sol @@ -70,10 +70,8 @@ contract E2E_EpochManager is ECDSAHelper08, Devchain { } address[] memory registeredValidators = getValidators().getRegisteredValidators(); - travelEpochL1(); - travelEpochL1(); - travelEpochL1(); - travelEpochL1(); + travelNEpochL1(4); + for (uint256 i = 0; i < registeredValidators.length; i++) { (, , address validatorGroup, , ) = getValidators().getValidator(registeredValidators[i]); if (getElection().getPendingVotesForGroup(validatorGroup) == 0) { diff --git a/packages/protocol/test-sol/devchain/migration/Migration.t.sol b/packages/protocol/test-sol/devchain/migration/Migration.t.sol index 5c1bee88e02..70294397cc3 100644 --- a/packages/protocol/test-sol/devchain/migration/Migration.t.sol +++ b/packages/protocol/test-sol/devchain/migration/Migration.t.sol @@ -195,10 +195,8 @@ contract EpochManagerIntegrationTest is IntegrationTest, MigrationsConstants { function activateValidators() public { address[] memory registeredValidators = validatorsContract.getRegisteredValidators(); - travelEpochL1(); - travelEpochL1(); - travelEpochL1(); - travelEpochL1(); + travelNEpochL1(4); + for (uint256 i = 0; i < registeredValidators.length; i++) { (, , address validatorGroup, , ) = validatorsContract.getValidator(registeredValidators[i]); if (election.getPendingVotesForGroup(validatorGroup) == 0) { diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 1908b589845..3ec7a30b76f 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -32,7 +32,6 @@ contract EpochManagerTest is TestWithUtils08 { address communityRewardFund; address reserveAddress; address scoreManagerAddress; - address accountsAddress; uint256 firstEpochNumber = 100; uint256 firstEpochBlock = 100; @@ -147,7 +146,7 @@ contract EpochManagerTest is TestWithUtils08 { election.setElectedValidators(members); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); travelNL2Epoch(1); @@ -201,7 +200,7 @@ contract EpochManagerTest_initialize is EpochManagerTest { contract EpochManagerTest_initializeSystem is EpochManagerTest { function test_processCanBeStarted() public virtual { - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); ( uint256 _firstEpochBlock, @@ -219,9 +218,9 @@ contract EpochManagerTest_initializeSystem is EpochManagerTest { } function test_Reverts_processCannotBeStartedAgain() public virtual { - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); vm.expectRevert("Epoch system already initialized"); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); } @@ -239,7 +238,7 @@ contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { } function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); vm.expectRevert("Epoch is not ready to start"); @@ -405,7 +404,7 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { members[1] = validator2; validators.setMembers(group, members); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); stableToken.mint(address(epochManagerContract), paymentAmount * 2); @@ -547,7 +546,7 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { members[1] = validator4; validators.setMembers(group2, members); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); initializeEpochManagerSystem(); elected = epochManagerContract.getElectedAccounts(); @@ -679,7 +678,7 @@ contract EpochManagerTest_setToProcessGroups is EpochManagerTest { members[1] = validator4; validators.setMembers(group2, members); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); initializeEpochManagerSystem(); elected = epochManagerContract.getElectedAccounts(); @@ -782,7 +781,7 @@ contract EpochManagerTest_processGroup is EpochManagerTest { members[1] = validator4; validators.setMembers(group2, members); - vm.prank(epochManagerEnabler); + vm.prank(address(epochManagerEnabler)); initializeEpochManagerSystem(); elected = epochManagerContract.getElectedAccounts(); diff --git a/packages/protocol/test-sol/unit/common/interfaces/IEpochManagerEnablerMock.sol b/packages/protocol/test-sol/unit/common/interfaces/IEpochManagerEnablerMock.sol new file mode 100644 index 00000000000..47dacc21987 --- /dev/null +++ b/packages/protocol/test-sol/unit/common/interfaces/IEpochManagerEnablerMock.sol @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: LGPL-3.0-only +pragma solidity >=0.5.13 <0.9.0; + +interface IEpochManagerEnablerMock { + function initEpochManager() external; + function captureEpochAndValidators() external; + function numberValidatorsInCurrentSet() external returns (uint256); + function numberValidatorsInSet(uint256) external returns (uint256); + function validatorSignerAddressFromCurrentSet(uint256 index) external returns (address); + function addValidator(address validator) external; +} diff --git a/packages/protocol/test-sol/utils/WhenL2-08.sol b/packages/protocol/test-sol/utils/WhenL2-08.sol index 802e66a6ee3..beb4a8915cc 100644 --- a/packages/protocol/test-sol/utils/WhenL2-08.sol +++ b/packages/protocol/test-sol/utils/WhenL2-08.sol @@ -9,3 +9,15 @@ contract WhenL2 is TestWithUtils08 { whenL2WithEpochManagerInitialization(); } } +contract WhenL2NoInitialization is TestWithUtils08 { + function setUp() public virtual override { + super.setUp(); + whenL2WithoutEpochManagerInitialization(); + } +} +contract WhenL2NoCapture is TestWithUtils08 { + function setUp() public virtual override { + super.setUp(); + whenL2WithoutEpochCapture(); + } +} From 4322801bfb3ea1f937953e548e0850ba0fc2997c Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:02:32 -0500 Subject: [PATCH 13/23] Passing L2 epochManagerEnabler test --- .../unit/common/EpochManagerEnabler.t.sol | 79 ++++++++----------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol index 5dd45835b42..a351692ada3 100644 --- a/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManagerEnabler.t.sol @@ -1,7 +1,6 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity >=0.8.0 <0.8.20; -import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; import { ICeloUnreleasedTreasury } from "@celo-contracts/common/interfaces/ICeloUnreleasedTreasury.sol"; import { CeloUnreleasedTreasury } from "@celo-contracts-8/common/CeloUnreleasedTreasury.sol"; @@ -11,21 +10,17 @@ import { EpochRewardsMock08 } from "@celo-contracts-8/governance/test/EpochRewar import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { EpochManagerEnablerMock } from "@test-sol/mocks/EpochManagerEnablerMock.sol"; +import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; +import "@test-sol/utils/WhenL2-08.sol"; contract EpochManagerEnablerTest is TestWithUtils08 { EpochManagerEnablerMock epochManagerEnablerContract; + EpochManager_WithMocks public epochManagerContract; MockCeloUnreleasedTreasury celoUnreleasedTreasury; MockCeloToken08 celoToken; - IAccounts accounts; - - address accountsAddress; address nonOwner; - address oracle; - - uint256 epochDuration = DAY; - uint256 numberValidators = 100; event LastKnownEpochNumberSet(uint256 lastKnownEpochNumber); event LastKnownFirstBlockOfEpochSet(uint256 lastKnownFirstBlockOfEpoch); @@ -33,46 +28,33 @@ contract EpochManagerEnablerTest is TestWithUtils08 { function setUp() public virtual override { super.setUp(); - // used by test that uses L1 epochSize() - ph.setEpochSize(17280); epochManagerEnablerContract = new EpochManagerEnablerMock(); celoToken = new MockCeloToken08(); celoUnreleasedTreasury = new MockCeloUnreleasedTreasury(); - accountsAddress = actor("accountsAddress"); - nonOwner = actor("nonOwner"); - oracle = actor("oracle"); - - deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); - accounts = IAccounts(accountsAddress); registry.setAddressFor(EpochManagerEnablerContract, address(epochManagerEnablerContract)); - registry.setAddressFor(AccountsContract, address(accounts)); registry.setAddressFor(CeloTokenContract, address(celoToken)); - registry.setAddressFor(SortedOraclesContract, oracle); registry.setAddressFor(CeloUnreleasedTreasuryContract, address(celoUnreleasedTreasury)); celoToken.setTotalSupply(CELO_SUPPLY_CAP); celoToken.setBalanceOf(address(celoUnreleasedTreasury), L2_INITIAL_STASH_BALANCE); epochManagerEnablerContract.initialize(REGISTRY_ADDRESS); - epochManager.initialize(REGISTRY_ADDRESS, epochDuration); - - _setupValidators(); - travelEpochL1(); - travelEpochL1(); } +} - function _setupValidators() internal { - for (uint256 i = 0; i < numberValidators; i++) { - vm.prank(vm.addr(i + 1)); - accounts.createAccount(); - - epochManagerEnablerContract.addValidator(vm.addr(i + 1)); - } +contract EpochManagerEnablerTest_L2 is EpochManagerEnablerTest, WhenL2NoInitialization { + function setUp() public override(EpochManagerEnablerTest, WhenL2NoInitialization) { + super.setUp(); + } +} +contract EpochManagerEnablerTest_L2_NoCapture is EpochManagerEnablerTest, WhenL2NoCapture { + function setUp() public override(EpochManagerEnablerTest, WhenL2NoCapture) { + super.setUp(); } } @@ -88,36 +70,37 @@ contract EpochManagerEnablerTest_initialize is EpochManagerEnablerTest { } contract EpochManagerEnablerTest_initEpochManager is EpochManagerEnablerTest { - function test_CanBeCalledByAnyone() public { - epochManagerEnablerContract.captureEpochAndValidators(); + function test_Reverts_whenL1() public { + vm.expectRevert("This method is not supported in L1."); - whenL2(); + epochManagerEnablerContract.initEpochManager(); + } +} + +contract EpochManagerEnablerTest_initEpochManager_L2 is EpochManagerEnablerTest_L2 { + function test_CanBeCalledByAnyone() public { vm.prank(nonOwner); epochManagerEnablerContract.initEpochManager(); assertGt(epochManager.getElectedAccounts().length, 0); assertTrue(epochManager.systemAlreadyInitialized()); } +} +contract EpochManagerEnablerTest_initEpochManager_L2_NoCapture is + EpochManagerEnablerTest_L2_NoCapture +{ function test_Reverts_ifEpochAndValidatorsAreNotCaptured() public { - whenL2(); vm.expectRevert("lastKnownEpochNumber not set."); epochManagerEnablerContract.initEpochManager(); } - - function test_Reverts_whenL1() public { - vm.expectRevert("This method is not supported in L1."); - - epochManagerEnablerContract.initEpochManager(); - } } contract EpochManagerEnablerTest_captureEpochAndValidators is EpochManagerEnablerTest { - function test_Reverts_whenL2() public { - whenL2(); - vm.expectRevert("This method is no longer supported in L2."); - epochManagerEnablerContract.captureEpochAndValidators(); + function setUp() public override { + super.setUp(); + _registerAndElectValidatorsForL2(); } function test_shouldSetLastKnownElectedAccounts() public { @@ -160,6 +143,14 @@ contract EpochManagerEnablerTest_captureEpochAndValidators is EpochManagerEnable } } +contract EpochManagerEnablerTest_captureEpochAndValidators_L2 is EpochManagerEnablerTest_L2 { + function test_Reverts_whenL2() public { + whenL2(); + vm.expectRevert("This method is no longer supported in L2."); + epochManagerEnablerContract.captureEpochAndValidators(); + } +} + contract EpochManagerEnablerTest_getFirstBlockOfEpoch is EpochManagerEnablerTest { function test_blockIsEpockBlock() public { vm.roll(27803520); From 99f09b2bcd05873dbf226df07d9c9cd6437bbbb0 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:25:54 -0500 Subject: [PATCH 14/23] renamed to force update --- .../test-sol/{TestWithutils08.sol => TestWithUtils080.sol} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/protocol/test-sol/{TestWithutils08.sol => TestWithUtils080.sol} (100%) diff --git a/packages/protocol/test-sol/TestWithutils08.sol b/packages/protocol/test-sol/TestWithUtils080.sol similarity index 100% rename from packages/protocol/test-sol/TestWithutils08.sol rename to packages/protocol/test-sol/TestWithUtils080.sol From da2d92a2f49bc6fa61de2187b02120acafeb9c6d Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:26:29 -0500 Subject: [PATCH 15/23] using capital letter --- .../test-sol/{TestWithUtils080.sol => TestWithUtils08.sol} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/protocol/test-sol/{TestWithUtils080.sol => TestWithUtils08.sol} (100%) diff --git a/packages/protocol/test-sol/TestWithUtils080.sol b/packages/protocol/test-sol/TestWithUtils08.sol similarity index 100% rename from packages/protocol/test-sol/TestWithUtils080.sol rename to packages/protocol/test-sol/TestWithUtils08.sol From 21ad79026a1b1f151adc61e5d1418bb276b2d392 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:26:43 -0500 Subject: [PATCH 16/23] wip partially migrated without mockAccounts contract --- .../protocol/test-sol/TestWithUtils08.sol | 18 +- .../test-sol/unit/common/EpochManager.t.sol | 1216 +++++++++-------- 2 files changed, 694 insertions(+), 540 deletions(-) diff --git a/packages/protocol/test-sol/TestWithUtils08.sol b/packages/protocol/test-sol/TestWithUtils08.sol index 04e07353cc9..fbe276d21be 100644 --- a/packages/protocol/test-sol/TestWithUtils08.sol +++ b/packages/protocol/test-sol/TestWithUtils08.sol @@ -10,14 +10,17 @@ import "@celo-contracts/common/interfaces/IRegistry.sol"; import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; +import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; +import { console } from "forge-std-8/console.sol"; contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { IRegistry registry; PrecompileHandler ph; EpochManager_WithMocks public epochManager; EpochManagerEnablerMock epochManagerEnabler; + MockAccounts accountss; IAccounts accountsContract; IEpochManagerEnablerMock epochManagerEnablerMockInterface; @@ -47,7 +50,13 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver accountsAddress = actor("accountsAddress"); deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); accountsContract = IAccounts(accountsAddress); + + // accountss = new MockAccounts(); + + // registry.setAddressFor(AccountsContract, address(accountss)); + // console.log("### Accounts address: ", address(accountss)); registry.setAddressFor(AccountsContract, accountsAddress); + console.log("### Accounts address: ", accountsAddress); } function setupEpochManager() public { @@ -128,17 +137,21 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver vm.prank(_elected[0]); accountsContract.createAccount(); + // accountss.setValidatorSigner(_elected[0], _elected[0]); epochManagerEnablerMockInterface.addValidator(_elected[0]); vm.prank(_elected[1]); accountsContract.createAccount(); + // accountss.setValidatorSigner(_elected[1], _elected[1]); epochManagerEnablerMockInterface.addValidator(_elected[1]); for (uint256 i = 2; i < numberValidators; i++) { - vm.prank(vm.addr(i + 1)); + address _currentValidator = vm.addr(i + 1); + vm.prank(_currentValidator); accountsContract.createAccount(); + // accountss.setValidatorSigner(_currentValidator, _currentValidator); - epochManagerEnablerMockInterface.addValidator(vm.addr(i + 1)); + epochManagerEnablerMockInterface.addValidator(_currentValidator); } travelNEpochL1(2); } @@ -151,6 +164,7 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver whenL2(); epochManagerEnabler.initEpochManager(); + console.log("### Done initEpochManager"); } function whenL2WithoutEpochManagerInitialization() internal { diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 3ec7a30b76f..de169a5d2fa 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -7,7 +7,7 @@ import { MockElection } from "@celo-contracts/governance/test/MockElection.sol"; import "@celo-contracts/stability/test/MockSortedOracles.sol"; import "@celo-contracts-8/common/ScoreManager.sol"; -import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; +// import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/test/MockCeloToken.sol"; import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; @@ -17,6 +17,7 @@ import "@celo-contracts-8/stability/test/MockStableToken.sol"; import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; +import "@test-sol/utils/WhenL2-08.sol"; contract EpochManagerTest is TestWithUtils08 { EpochManager_WithMocks epochManagerContract; @@ -25,7 +26,7 @@ contract EpochManagerTest is TestWithUtils08 { MockStableToken08 stableToken; EpochRewardsMock08 epochRewards; MockElection election; - MockAccounts accounts; + // MockAccounts accountss; IMockValidators validators; address carbonOffsettingPartner; @@ -33,7 +34,8 @@ contract EpochManagerTest is TestWithUtils08 { address reserveAddress; address scoreManagerAddress; - uint256 firstEpochNumber = 100; + uint256 firstEpochNumber = 3; + // uint256 firstEpochNumber = 100; uint256 firstEpochBlock = 100; uint256 epochDuration = DAY; address[] firstElected; @@ -77,7 +79,7 @@ contract EpochManagerTest is TestWithUtils08 { celoToken = new MockCeloToken08(); celoUnreleasedTreasury = new MockCeloUnreleasedTreasury(); election = new MockElection(); - accounts = new MockAccounts(); + // accountss = new MockAccounts(); validator1 = actor("validator"); validator2 = actor("otherValidator"); @@ -86,7 +88,7 @@ contract EpochManagerTest is TestWithUtils08 { firstElected.push(validator2); scoreManagerAddress = actor("scoreManagerAddress"); - accountsAddress = actor("accountsAddress"); + // accountsAddress = actor("accountsAddress"); reserveAddress = actor("reserve"); @@ -95,7 +97,7 @@ contract EpochManagerTest is TestWithUtils08 { deployCodeTo("MockRegistry.sol", abi.encode(false), REGISTRY_ADDRESS); deployCodeTo("ScoreManager.sol", abi.encode(false), scoreManagerAddress); - deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); + // deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); deployCodeTo("MockValidators.sol", abi.encode(false), address(validators)); scoreManager = ScoreManager(scoreManagerAddress); @@ -111,7 +113,7 @@ contract EpochManagerTest is TestWithUtils08 { registry.setAddressFor(CeloTokenContract, address(celoToken)); registry.setAddressFor(ReserveContract, reserveAddress); registry.setAddressFor(ElectionContract, address(election)); - registry.setAddressFor(AccountsContract, address(accounts)); + // registry.setAddressFor(AccountsContract, address(accountss)); // XXX this overrides the accountss address from utils celoToken.setTotalSupply(CELO_SUPPLY_CAP); vm.deal(address(celoUnreleasedTreasury), L2_INITIAL_STASH_BALANCE); @@ -133,11 +135,12 @@ contract EpochManagerTest is TestWithUtils08 { } function initializeEpochManagerSystem() public { + // TODO(soloseng): rename function validators.setValidatorGroup(group); validators.setValidator(validator1); - accounts.setValidatorSigner(validator1, validator1); + // accountss.setValidatorSigner(validator1, validator1); validators.setValidator(validator2); - accounts.setValidatorSigner(validator2, validator2); + // accountss.setValidatorSigner(validator2, validator2); address[] memory members = new address[](2); members[0] = validator1; @@ -146,8 +149,8 @@ contract EpochManagerTest is TestWithUtils08 { election.setElectedValidators(members); - vm.prank(address(epochManagerEnabler)); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + // vm.prank(address(epochManagerEnabler)); + // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); travelNL2Epoch(1); } @@ -185,6 +188,17 @@ contract EpochManagerTest is TestWithUtils08 { } } +contract EpochManagerTest_L2_NoInit is EpochManagerTest, WhenL2NoInitialization { + function setUp() public virtual override(EpochManagerTest, WhenL2NoInitialization) { + super.setUp(); + } +} +contract EpochManagerTest_L2 is EpochManagerTest, WhenL2 { + function setUp() public virtual override(EpochManagerTest, WhenL2) { + super.setUp(); + } +} + contract EpochManagerTest_initialize is EpochManagerTest { function test_initialize() public virtual { assertEq(address(epochManagerContract.registry()), REGISTRY_ADDRESS); @@ -230,16 +244,36 @@ contract EpochManagerTest_initializeSystem is EpochManagerTest { epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); } } +contract EpochManagerTest_initializeSystem_L2 is + EpochManagerTest_L2_NoInit, + EpochManagerTest_initializeSystem +{ + function setUp() public override(EpochManagerTest, EpochManagerTest_L2_NoInit) { + super.setUp(); + } +} // XXX(soloseng):is this needed, since it would be calling epochmanagerEnabler. contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { + function test_Reverts_onL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.startNextEpochProcess(); + } +} +contract EpochManagerTest_startNextEpochProcess_L2_NoInit is EpochManagerTest_L2_NoInit { function test_Reverts_whenSystemNotInitialized() public { vm.expectRevert("Epoch system not initialized"); epochManagerContract.startNextEpochProcess(); } +} +contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { + // function test_Reverts_whenSystemNotInitialized() public { + // vm.expectRevert("Epoch system not initialized"); + // epochManagerContract.startNextEpochProcess(); + // } function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { - vm.prank(address(epochManagerEnabler)); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + // vm.prank(address(epochManagerEnabler)); + // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); vm.expectRevert("Epoch is not ready to start"); epochManagerContract.startNextEpochProcess(); @@ -322,13 +356,6 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { epochManagerContract.setEpochDuration(newEpochDuration); } - function test_Reverts_WhenIsOnEpochProcess() public { - initializeEpochManagerSystem(); - epochManagerContract.startNextEpochProcess(); - vm.expectRevert("Cannot change epoch duration during processing."); - epochManagerContract.setEpochDuration(newEpochDuration); - } - function test_Reverts_WhenNewEpochDurationIsZero() public { initializeEpochManagerSystem(); @@ -337,6 +364,22 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { } } +contract EpochManagerTest_setEpochDuration_L2 is + EpochManagerTest_L2, + EpochManagerTest_setEpochDuration +{ + function setUp() public override(EpochManagerTest_L2, EpochManagerTest) { + super.setUp(); + } + + function test_Reverts_WhenIsOnEpochProcess() public { + initializeEpochManagerSystem(); + epochManagerContract.startNextEpochProcess(); + vm.expectRevert("Cannot change epoch duration during processing."); + epochManagerContract.setEpochDuration(newEpochDuration); + } +} + contract EpochManagerTest_setOracleAddress is EpochManagerTest { address newOracleAddress = actor("newOarcle"); @@ -354,12 +397,12 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { epochManagerContract.setOracleAddress(newOracleAddress); } - function test_Reverts_WhenIsOnEpochProcess() public { - initializeEpochManagerSystem(); - epochManagerContract.startNextEpochProcess(); - vm.expectRevert("Cannot change oracle address during epoch processing."); - epochManagerContract.setOracleAddress(newOracleAddress); - } + // function test_Reverts_WhenIsOnEpochProcess() public { + // initializeEpochManagerSystem(); + // epochManagerContract.startNextEpochProcess(); + // vm.expectRevert("Cannot change oracle address during epoch processing."); + // epochManagerContract.setOracleAddress(newOracleAddress); + // } function test_Reverts_WhenNewOracleAddressIsZero() public { initializeEpochManagerSystem(); @@ -376,9 +419,54 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { } } +contract EpochManagerTest_setOracleAddress_L2 is + EpochManagerTest_L2, + EpochManagerTest_setOracleAddress +{ + function setUp() public override(EpochManagerTest_L2, EpochManagerTest) { + super.setUp(); + } + // address newOracleAddress = actor("newOarcle"); + + // function test_setsNewOracleAddress() public { + // initializeEpochManagerSystem(); + // epochManagerContract.setOracleAddress(newOracleAddress); + // assertEq(epochManagerContract.oracleAddress(), newOracleAddress); + // } + + // function test_Emits_OracleAddressSetEvent() public { + // initializeEpochManagerSystem(); + + // vm.expectEmit(true, true, true, true); + // emit OracleAddressSet(newOracleAddress); + // epochManagerContract.setOracleAddress(newOracleAddress); + // } + + function test_Reverts_WhenIsOnEpochProcess() public { + initializeEpochManagerSystem(); + epochManagerContract.startNextEpochProcess(); + vm.expectRevert("Cannot change oracle address during epoch processing."); + epochManagerContract.setOracleAddress(newOracleAddress); + } + + // function test_Reverts_WhenNewOracleAddressIsZero() public { + // initializeEpochManagerSystem(); + + // vm.expectRevert("Cannot set address zero as the Oracle."); + // epochManagerContract.setOracleAddress(address(0)); + // } + + // function test_Reverts_WhenNewOracleAddressIsunchanged() public { + // initializeEpochManagerSystem(); + + // vm.expectRevert("Oracle address cannot be the same."); + // epochManagerContract.setOracleAddress(address(sortedOracles)); + // } +} + contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { - address signer1 = actor("signer1"); - address signer2 = actor("signer2"); + // address signer1 = actor("signer1"); + // address signer2 = actor("signer2"); address beneficiary = actor("beneficiary"); uint256 paymentAmount = 4 ether; @@ -392,20 +480,67 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function setUp() public override { super.setUp(); + _registerAndElectValidatorsForL2(); + validators.setValidatorGroup(group); + validators.setValidator(validator1); + // accountss.setValidatorSigner(validator1, signer1); + validators.setValidator(validator2); + // accountss.setValidatorSigner(validator2, signer2); + + address[] memory members = new address[](2); + members[0] = validator1; + members[1] = validator2; + validators.setMembers(group, members); + + // vm.prank(address(epochManagerEnabler)); + // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + + stableToken.mint(address(epochManagerContract), paymentAmount * 2); + epochManagerBalanceBefore = stableToken.balanceOf(address(epochManagerContract)); + epochManagerContract._setPaymentAllocation(validator1, paymentAmount); + } + + function test_Reverts_onL1() public { + validators.setCommission(group, fiftyPercent); + vm.prank(validator1); + accountsContract.setPaymentDelegation(beneficiary, fiftyPercent); + + vm.expectRevert("Epoch system not initialized"); + + epochManagerContract.sendValidatorPayment(validator1); + } +} + +contract EpochManagerTest_sendValidatorPayment_L2 is EpochManagerTest_L2 { + // address signer1 = actor("signer1"); + // address signer2 = actor("signer2"); + address beneficiary = actor("beneficiary"); + + uint256 paymentAmount = 4 ether; + uint256 quarterOfPayment = paymentAmount / 4; + uint256 halfOfPayment = paymentAmount / 2; + uint256 threeQuartersOfPayment = (paymentAmount / 4) * 3; + uint256 twentyFivePercent = 250000000000000000000000; + uint256 fiftyPercent = 500000000000000000000000; + + uint256 epochManagerBalanceBefore; + + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); validators.setValidatorGroup(group); validators.setValidator(validator1); - accounts.setValidatorSigner(validator1, signer1); + // accountss.setValidatorSigner(validator1, signer1); validators.setValidator(validator2); - accounts.setValidatorSigner(validator2, signer2); + // accountss.setValidatorSigner(validator2, signer2); address[] memory members = new address[](2); members[0] = validator1; members[1] = validator2; validators.setMembers(group, members); - vm.prank(address(epochManagerEnabler)); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + // vm.prank(address(epochManagerEnabler)); + // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); stableToken.mint(address(epochManagerContract), paymentAmount * 2); epochManagerBalanceBefore = stableToken.balanceOf(address(epochManagerContract)); @@ -437,7 +572,9 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { } function test_sendsCUsdFromEpochManagerToValidatorAndBeneficiary() public { - accounts.setPaymentDelegationFor(validator1, beneficiary, twentyFivePercent); + // accountss.setPaymentDelegationFor(validator1, beneficiary, twentyFivePercent); + vm.prank(validator1); + accountsContract.setPaymentDelegation(beneficiary, twentyFivePercent); epochManagerContract.sendValidatorPayment(validator1); @@ -452,7 +589,8 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function test_sendsCUsdFromEpochManagerToValidatorAndGroupAndBeneficiary() public { validators.setCommission(group, fiftyPercent); - accounts.setPaymentDelegationFor(validator1, beneficiary, fiftyPercent); + vm.prank(validator1); + accountsContract.setPaymentDelegation(beneficiary, fiftyPercent); epochManagerContract.sendValidatorPayment(validator1); @@ -469,7 +607,8 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function test_emitsAValidatorEpochPaymentDistributedEvent() public { validators.setCommission(group, fiftyPercent); - accounts.setPaymentDelegationFor(validator1, beneficiary, fiftyPercent); + vm.prank(validator1); + accountsContract.setPaymentDelegation(beneficiary, fiftyPercent); vm.expectEmit(true, true, true, true, address(epochManagerContract)); emit ValidatorEpochPaymentDistributed( @@ -485,7 +624,8 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { function test_doesNothingIfNotAllocated() public { validators.setCommission(group, fiftyPercent); - accounts.setPaymentDelegationFor(validator2, beneficiary, fiftyPercent); + vm.prank(validator2); + accountsContract.setPaymentDelegation(beneficiary, fiftyPercent); epochManagerContract.sendValidatorPayment(validator2); @@ -512,537 +652,537 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { } } -contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { - address signer1 = actor("signer1"); - address signer2 = actor("signer2"); +// contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { +// address signer1 = actor("signer1"); +// address signer2 = actor("signer2"); + +// address validator3 = actor("validator3"); +// address validator4 = actor("validator4"); - address validator3 = actor("validator3"); - address validator4 = actor("validator4"); +// address group2 = actor("group2"); - address group2 = actor("group2"); +// address[] elected; - address[] elected; +// uint256 groupEpochRewards = 44e18; - uint256 groupEpochRewards = 44e18; +// function setUp() public override { +// super.setUp(); - function setUp() public override { - super.setUp(); +// validators.setValidatorGroup(group); +// validators.setValidator(validator1); +// accountss.setValidatorSigner(validator1, signer1); +// validators.setValidator(validator2); +// accountss.setValidatorSigner(validator2, signer2); - validators.setValidatorGroup(group); - validators.setValidator(validator1); - accounts.setValidatorSigner(validator1, signer1); - validators.setValidator(validator2); - accounts.setValidatorSigner(validator2, signer2); +// validators.setValidatorGroup(group2); +// validators.setValidator(validator3); +// validators.setValidator(validator4); - validators.setValidatorGroup(group2); - validators.setValidator(validator3); - validators.setValidator(validator4); +// address[] memory members = new address[](3); +// members[0] = validator1; +// members[1] = validator2; +// validators.setMembers(group, members); +// members[0] = validator3; +// members[1] = validator4; +// validators.setMembers(group2, members); - address[] memory members = new address[](3); - members[0] = validator1; - members[1] = validator2; - validators.setMembers(group, members); - members[0] = validator3; - members[1] = validator4; - validators.setMembers(group2, members); +// vm.prank(address(epochManagerEnabler)); +// initializeEpochManagerSystem(); - vm.prank(address(epochManagerEnabler)); - initializeEpochManagerSystem(); +// elected = epochManagerContract.getElectedAccounts(); - elected = epochManagerContract.getElectedAccounts(); +// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); +// } - election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); - } +// function test_Reverts_WhenNotStarted() public { +// address[] memory groups = new address[](0); - function test_Reverts_WhenNotStarted() public { - address[] memory groups = new address[](0); +// vm.expectRevert("Epoch process is not started"); +// epochManagerContract.finishNextEpochProcess(groups, groups, groups); +// } - vm.expectRevert("Epoch process is not started"); - epochManagerContract.finishNextEpochProcess(groups, groups, groups); - } +// function test_Reverts_WhenGroupsDoNotMatch() public { +// address[] memory groups = new address[](0); +// epochManagerContract.startNextEpochProcess(); +// vm.expectRevert("number of groups does not match"); +// epochManagerContract.finishNextEpochProcess(groups, groups, groups); +// } - function test_Reverts_WhenGroupsDoNotMatch() public { - address[] memory groups = new address[](0); - epochManagerContract.startNextEpochProcess(); - vm.expectRevert("number of groups does not match"); - epochManagerContract.finishNextEpochProcess(groups, groups, groups); - } +// function test_Reverts_WhenGroupsNotFromElected() public { +// address[] memory groups = new address[](1); +// groups[0] = group2; +// epochManagerContract.startNextEpochProcess(); +// vm.expectRevert("group not from current elected set"); +// epochManagerContract.finishNextEpochProcess(groups, groups, groups); +// } - function test_Reverts_WhenGroupsNotFromElected() public { - address[] memory groups = new address[](1); - groups[0] = group2; - epochManagerContract.startNextEpochProcess(); - vm.expectRevert("group not from current elected set"); - epochManagerContract.finishNextEpochProcess(groups, groups, groups); - } +// function test_TransfersToCommunityAndCarbonOffsetting() public { +// ( +// address[] memory groups, +// address[] memory lessers, +// address[] memory greaters +// ) = getGroupsWithLessersAndGreaters(); - function test_TransfersToCommunityAndCarbonOffsetting() public { - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - epochManagerContract.startNextEpochProcess(); - epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); +// assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); +// assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); +// } - assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); - assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); - } +// function test_TransfersToValidatorGroup() public { +// ( +// address[] memory groups, +// address[] memory lessers, +// address[] memory greaters +// ) = getGroupsWithLessersAndGreaters(); - function test_TransfersToValidatorGroup() public { - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - epochManagerContract.startNextEpochProcess(); - epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); +// assertEq(election.distributedEpochRewards(group), groupEpochRewards); +// } - assertEq(election.distributedEpochRewards(group), groupEpochRewards); - } +// function test_SetsNewlyElectedCorrectly() public { +// ( +// address[] memory groups, +// address[] memory lessers, +// address[] memory greaters +// ) = getGroupsWithLessersAndGreaters(); - function test_SetsNewlyElectedCorrectly() public { - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); +// epochManagerContract.startNextEpochProcess(); - epochManagerContract.startNextEpochProcess(); +// address[] memory newElected = new address[](2); +// newElected[0] = validator3; +// newElected[1] = validator4; +// election.setElectedValidators(newElected); + +// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - address[] memory newElected = new address[](2); - newElected[0] = validator3; - newElected[1] = validator4; - election.setElectedValidators(newElected); +// address[] memory afterElected = epochManagerContract.getElectedAccounts(); + +// for (uint256 i = 0; i < newElected.length; i++) { +// assertEq(newElected[i], afterElected[i]); +// } +// } + +// function test_Emits_GroupProcessedEvent() public { +// ( +// address[] memory groups, +// address[] memory lessers, +// address[] memory greaters +// ) = getGroupsWithLessersAndGreaters(); + +// epochManagerContract.startNextEpochProcess(); + +// for (uint i = 0; i < groups.length; i++) { +// vm.expectEmit(true, true, true, true); +// emit GroupProcessed(groups[i], firstEpochNumber); +// } + +// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); +// } +// } + +// contract EpochManagerTest_setToProcessGroups is EpochManagerTest { +// address signer1 = actor("signer1"); +// address signer2 = actor("signer2"); + +// address validator3 = actor("validator3"); +// address validator4 = actor("validator4"); + +// address group2 = actor("group2"); + +// address[] elected; + +// uint256 groupEpochRewards = 44e18; + +// function setUp() public override { +// super.setUp(); + +// validators.setValidatorGroup(group); +// validators.setValidator(validator1); +// accountss.setValidatorSigner(validator1, signer1); +// validators.setValidator(validator2); +// accountss.setValidatorSigner(validator2, signer2); + +// validators.setValidatorGroup(group2); +// validators.setValidator(validator3); +// validators.setValidator(validator4); + +// address[] memory members = new address[](3); +// members[0] = validator1; +// members[1] = validator2; +// validators.setMembers(group, members); +// members[0] = validator3; +// members[1] = validator4; +// validators.setMembers(group2, members); + +// vm.prank(address(epochManagerEnabler)); +// initializeEpochManagerSystem(); - epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); +// elected = epochManagerContract.getElectedAccounts(); - address[] memory afterElected = epochManagerContract.getElectedAccounts(); +// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); +// } - for (uint256 i = 0; i < newElected.length; i++) { - assertEq(newElected[i], afterElected[i]); - } - } - - function test_Emits_GroupProcessedEvent() public { - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); - - epochManagerContract.startNextEpochProcess(); - - for (uint i = 0; i < groups.length; i++) { - vm.expectEmit(true, true, true, true); - emit GroupProcessed(groups[i], firstEpochNumber); - } - - epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); - } -} - -contract EpochManagerTest_setToProcessGroups is EpochManagerTest { - address signer1 = actor("signer1"); - address signer2 = actor("signer2"); - - address validator3 = actor("validator3"); - address validator4 = actor("validator4"); - - address group2 = actor("group2"); - - address[] elected; - - uint256 groupEpochRewards = 44e18; - - function setUp() public override { - super.setUp(); - - validators.setValidatorGroup(group); - validators.setValidator(validator1); - accounts.setValidatorSigner(validator1, signer1); - validators.setValidator(validator2); - accounts.setValidatorSigner(validator2, signer2); - - validators.setValidatorGroup(group2); - validators.setValidator(validator3); - validators.setValidator(validator4); - - address[] memory members = new address[](3); - members[0] = validator1; - members[1] = validator2; - validators.setMembers(group, members); - members[0] = validator3; - members[1] = validator4; - validators.setMembers(group2, members); - - vm.prank(address(epochManagerEnabler)); - initializeEpochManagerSystem(); +// function test_Reverts_WhenNotStarted() public { +// vm.expectRevert("Epoch process is not started"); +// epochManagerContract.setToProcessGroups(); +// } - elected = epochManagerContract.getElectedAccounts(); +// function test_setsToProcessGroups() public { +// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); - } - - function test_Reverts_WhenNotStarted() public { - vm.expectRevert("Epoch process is not started"); - epochManagerContract.setToProcessGroups(); - } - - function test_setsToProcessGroups() public { - (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); - assertEq(EpochManager(address(epochManagerContract)).toProcessGroups(), groups.length); - } +// assertEq(EpochManager(address(epochManagerContract)).toProcessGroups(), groups.length); +// } - function test_blocksChilds() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - assertTrue(epochManagerContract.isBlocked()); - } +// function test_blocksChilds() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// assertTrue(epochManagerContract.isBlocked()); +// } - function test_Reverts_startEpochAgain() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - vm.expectRevert("Epoch process is already started"); - epochManagerContract.startNextEpochProcess(); - } - - function test_Reverts_WhenSetToProcessGroups() public { - vm.expectRevert("Epoch process is not started"); - epochManagerContract.setToProcessGroups(); - } - - function test_setsGroupRewards() public { - (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - - for (uint256 i = 0; i < groups.length; i++) { - assertEq( - EpochManager(address(epochManagerContract)).processedGroups(group), - groupEpochRewards - ); - } - } - - function test_Emits_GroupMarkedForProcessingEvent() public { - (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); - - epochManagerContract.startNextEpochProcess(); - - for (uint i = 0; i < groups.length; i++) { - vm.expectEmit(true, true, true, true); - emit GroupMarkedForProcessing(groups[i], firstEpochNumber); - } - - epochManagerContract.setToProcessGroups(); - } -} - -contract EpochManagerTest_processGroup is EpochManagerTest { - address signer1 = actor("signer1"); - address signer2 = actor("signer2"); - address signer3 = actor("signer3"); - address signer4 = actor("signer4"); - - address validator3 = actor("validator3"); - address validator4 = actor("validator4"); - - address group2 = actor("group2"); - - address[] elected; - - uint256 groupEpochRewards = 44e18; - - function setUp() public override { - super.setUp(); - - validators.setValidatorGroup(group); - validators.setValidator(validator1); - accounts.setValidatorSigner(validator1, signer1); - validators.setValidator(validator2); - accounts.setValidatorSigner(validator2, signer2); - - validators.setValidatorGroup(group2); - validators.setValidator(validator3); - validators.setValidator(validator4); - - address[] memory members = new address[](3); - members[0] = validator1; - members[1] = validator2; - validators.setMembers(group, members); - members[0] = validator3; - members[1] = validator4; - validators.setMembers(group2, members); - - vm.prank(address(epochManagerEnabler)); - initializeEpochManagerSystem(); - - elected = epochManagerContract.getElectedAccounts(); - - election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); - } - - function test_Reverts_WhenNotStarted() public { - vm.expectRevert("Indivudual epoch process is not started"); - epochManagerContract.processGroup(group, address(0), address(0)); - } - - function test_Reverts_WhenGroupNotInToProcessGroups() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - vm.expectRevert("group not from current elected set"); - epochManagerContract.processGroup(group2, address(0), address(0)); - } - - function test_ProcessesGroup() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - epochManagerContract.processGroup(group, address(0), address(0)); - - (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); - assertEq(status, 0); - } - - function test_TransfersToCommunityAndCarbonOffsetting() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - epochManagerContract.processGroup(group, address(0), address(0)); - - assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); - assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); - } - - function test_TransfersToValidatorGroup() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - epochManagerContract.processGroup(group, address(0), address(0)); - - assertEq(election.distributedEpochRewards(group), groupEpochRewards); - } - - function test_SetsNewlyElectedCorrectly() public { - ( - address[] memory groups, - address[] memory lessers, - address[] memory greaters - ) = getGroupsWithLessersAndGreaters(); - - epochManagerContract.startNextEpochProcess(); - - address[] memory newElected = new address[](2); - newElected[0] = validator3; - newElected[1] = validator4; - election.setElectedValidators(newElected); - - address[] memory signers = new address[](2); - signers[0] = signer3; - signers[1] = signer4; - accounts.setValidatorSigner(validator3, signer3); - accounts.setValidatorSigner(validator4, signer4); - - epochManagerContract.setToProcessGroups(); - - for (uint256 i = 0; i < groups.length; i++) { - epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); - } - - address[] memory afterElected = epochManagerContract.getElectedAccounts(); - - for (uint256 i = 0; i < newElected.length; i++) { - assertEq(newElected[i], afterElected[i]); - } - - address[] memory afterSigners = epochManagerContract.getElectedSigners(); - assertEq(afterSigners.length, signers.length); - for (uint256 i = 0; i < signers.length; i++) { - assertEq(signers[i], afterSigners[i]); - } - } - - function test_Emits_GroupProcessed() public { - epochManagerContract.startNextEpochProcess(); - epochManagerContract.setToProcessGroups(); - vm.expectEmit(true, true, true, true); - emit GroupProcessed(group, firstEpochNumber); - epochManagerContract.processGroup(group, address(0), address(0)); - } -} - -contract EpochManagerTest_getEpochByNumber is EpochManagerTest { - function test_shouldReturnTheEpochInfoOfSpecifiedEpoch() public { - uint256 numberOfEpochsToTravel = 9; - - initializeEpochManagerSystem(); - uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); - - ( - uint256 startingEpochFirstBlock, - , - uint256 startingEpochStartTimestamp, - - ) = epochManagerContract.getCurrentEpoch(); - - _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); - - ( - uint256 _firstBlock, - uint256 _lastBlock, - uint256 _startTimestamp, - uint256 _rewardBlock - ) = epochManagerContract.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); - - assertEq( - startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * (numberOfEpochsToTravel + 1)) + 1, - _firstBlock - ); - assertEq(_lastBlock, 0); - assertEq(startingEpochStartTimestamp + (DAY * (numberOfEpochsToTravel + 1)), _startTimestamp); - assertEq(_rewardBlock, 0); - } - - function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { - initializeEpochManagerSystem(); - uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); - uint256 numberOfEpochsToTravel = 7; - ( - uint256 _startingEpochFirstBlock, - uint256 _startingLastBlock, - uint256 _startingStartTimestamp, - uint256 _startingRewardBlock - ) = epochManagerContract.getCurrentEpoch(); - - _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); - - ( - uint256 _initialFirstBlock, - uint256 _initialLastBlock, - uint256 _initialStartTimestamp, - uint256 _initialRewardBlock - ) = epochManagerContract.getEpochByNumber(_startingEpochNumber); - - assertEq(_initialFirstBlock, _startingEpochFirstBlock); - assertEq(_initialLastBlock, _startingLastBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock); - assertEq(_initialStartTimestamp, _startingStartTimestamp); - assertEq( - _initialRewardBlock, - _startingRewardBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock + 1 - ); - } - - function test_ReturnsZeroForFutureEpochs() public { - initializeEpochManagerSystem(); - ( - uint256 _firstBlock, - uint256 _lastBlock, - uint256 _startTimestamp, - uint256 _rewardBlock - ) = epochManagerContract.getEpochByNumber(500); - - assertEq(_firstBlock, 0); - assertEq(_lastBlock, 0); - assertEq(_startTimestamp, 0); - assertEq(_rewardBlock, 0); - } -} - -contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { - function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { - initializeEpochManagerSystem(); - assertEq(epochManagerContract.getEpochNumberOfBlock(firstEpochBlock), firstEpochNumber); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); - } -} - -contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { - function test_ShouldRetreiveTheCorrectEpochInfoOfGivenBlock() public { - initializeEpochManagerSystem(); - - _travelAndProcess_N_L2Epoch(2); - - (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManagerContract.getEpochByBlockNumber( - firstEpochBlock + (3 * L2_BLOCK_IN_EPOCH) - ); - assertEq(_firstBlock, firstEpochBlock + 1 + (2 * L2_BLOCK_IN_EPOCH)); - assertEq(_lastBlock, firstEpochBlock + 1 + (3 * L2_BLOCK_IN_EPOCH) - 1); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); - } -} - -contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { - function test_ShouldRetreiveTheNumberOfElected() public { - initializeEpochManagerSystem(); - assertEq(epochManagerContract.numberOfElectedInCurrentSet(), 2); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.numberOfElectedInCurrentSet(); - } -} - -contract EpochManagerTest_getElectedAccounts is EpochManagerTest { - function test_ShouldRetreiveThelistOfElectedAccounts() public { - initializeEpochManagerSystem(); - assertEq(epochManagerContract.getElectedAccounts(), firstElected); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getElectedAccounts(); - } -} - -contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { - function test_ShouldRetreiveThecorrectValidator() public { - initializeEpochManagerSystem(); - assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getElectedAccountByIndex(0); - } -} -contract EpochManagerTest_getElectedSigners is EpochManagerTest { - function test_ShouldRetreiveTheElectedSigners() public { - initializeEpochManagerSystem(); - address[] memory electedSigners = new address[](firstElected.length); - electedSigners[0] = accounts.getValidatorSigner(firstElected[0]); - electedSigners[1] = accounts.getValidatorSigner(firstElected[1]); - assertEq(epochManagerContract.getElectedSigners(), electedSigners); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getElectedSigners(); - } -} -contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { - function test_ShouldRetreiveThecorrectElectedSigner() public { - initializeEpochManagerSystem(); - address[] memory electedSigners = new address[](firstElected.length); - - electedSigners[1] = accounts.getValidatorSigner(firstElected[1]); - assertEq(epochManagerContract.getElectedSignerByIndex(1), electedSigners[1]); - } - - function test_Reverts_WhenL1() public { - vm.expectRevert("Epoch system not initialized"); - epochManagerContract.getElectedSignerByIndex(1); - } -} +// function test_Reverts_startEpochAgain() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// vm.expectRevert("Epoch process is already started"); +// epochManagerContract.startNextEpochProcess(); +// } + +// function test_Reverts_WhenSetToProcessGroups() public { +// vm.expectRevert("Epoch process is not started"); +// epochManagerContract.setToProcessGroups(); +// } + +// function test_setsGroupRewards() public { +// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); + +// for (uint256 i = 0; i < groups.length; i++) { +// assertEq( +// EpochManager(address(epochManagerContract)).processedGroups(group), +// groupEpochRewards +// ); +// } +// } + +// function test_Emits_GroupMarkedForProcessingEvent() public { +// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); + +// epochManagerContract.startNextEpochProcess(); + +// for (uint i = 0; i < groups.length; i++) { +// vm.expectEmit(true, true, true, true); +// emit GroupMarkedForProcessing(groups[i], firstEpochNumber); +// } + +// epochManagerContract.setToProcessGroups(); +// } +// } + +// contract EpochManagerTest_processGroup is EpochManagerTest { +// address signer1 = actor("signer1"); +// address signer2 = actor("signer2"); +// address signer3 = actor("signer3"); +// address signer4 = actor("signer4"); + +// address validator3 = actor("validator3"); +// address validator4 = actor("validator4"); + +// address group2 = actor("group2"); + +// address[] elected; + +// uint256 groupEpochRewards = 44e18; + +// function setUp() public override { +// super.setUp(); + +// validators.setValidatorGroup(group); +// validators.setValidator(validator1); +// accountss.setValidatorSigner(validator1, signer1); +// validators.setValidator(validator2); +// accountss.setValidatorSigner(validator2, signer2); + +// validators.setValidatorGroup(group2); +// validators.setValidator(validator3); +// validators.setValidator(validator4); + +// address[] memory members = new address[](3); +// members[0] = validator1; +// members[1] = validator2; +// validators.setMembers(group, members); +// members[0] = validator3; +// members[1] = validator4; +// validators.setMembers(group2, members); + +// vm.prank(address(epochManagerEnabler)); +// initializeEpochManagerSystem(); + +// elected = epochManagerContract.getElectedAccounts(); + +// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); +// } + +// function test_Reverts_WhenNotStarted() public { +// vm.expectRevert("Indivudual epoch process is not started"); +// epochManagerContract.processGroup(group, address(0), address(0)); +// } + +// function test_Reverts_WhenGroupNotInToProcessGroups() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// vm.expectRevert("group not from current elected set"); +// epochManagerContract.processGroup(group2, address(0), address(0)); +// } + +// function test_ProcessesGroup() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// epochManagerContract.processGroup(group, address(0), address(0)); + +// (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); +// assertEq(status, 0); +// } + +// function test_TransfersToCommunityAndCarbonOffsetting() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// epochManagerContract.processGroup(group, address(0), address(0)); + +// assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); +// assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); +// } + +// function test_TransfersToValidatorGroup() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// epochManagerContract.processGroup(group, address(0), address(0)); + +// assertEq(election.distributedEpochRewards(group), groupEpochRewards); +// } + +// function test_SetsNewlyElectedCorrectly() public { +// ( +// address[] memory groups, +// address[] memory lessers, +// address[] memory greaters +// ) = getGroupsWithLessersAndGreaters(); + +// epochManagerContract.startNextEpochProcess(); + +// address[] memory newElected = new address[](2); +// newElected[0] = validator3; +// newElected[1] = validator4; +// election.setElectedValidators(newElected); + +// address[] memory signers = new address[](2); +// signers[0] = signer3; +// signers[1] = signer4; +// accountss.setValidatorSigner(validator3, signer3); +// accountss.setValidatorSigner(validator4, signer4); + +// epochManagerContract.setToProcessGroups(); + +// for (uint256 i = 0; i < groups.length; i++) { +// epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); +// } + +// address[] memory afterElected = epochManagerContract.getElectedAccounts(); + +// for (uint256 i = 0; i < newElected.length; i++) { +// assertEq(newElected[i], afterElected[i]); +// } + +// address[] memory afterSigners = epochManagerContract.getElectedSigners(); +// assertEq(afterSigners.length, signers.length); +// for (uint256 i = 0; i < signers.length; i++) { +// assertEq(signers[i], afterSigners[i]); +// } +// } + +// function test_Emits_GroupProcessed() public { +// epochManagerContract.startNextEpochProcess(); +// epochManagerContract.setToProcessGroups(); +// vm.expectEmit(true, true, true, true); +// emit GroupProcessed(group, firstEpochNumber); +// epochManagerContract.processGroup(group, address(0), address(0)); +// } +// } + +// contract EpochManagerTest_getEpochByNumber is EpochManagerTest { +// function test_shouldReturnTheEpochInfoOfSpecifiedEpoch() public { +// uint256 numberOfEpochsToTravel = 9; + +// initializeEpochManagerSystem(); +// uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); + +// ( +// uint256 startingEpochFirstBlock, +// , +// uint256 startingEpochStartTimestamp, + +// ) = epochManagerContract.getCurrentEpoch(); + +// _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); + +// ( +// uint256 _firstBlock, +// uint256 _lastBlock, +// uint256 _startTimestamp, +// uint256 _rewardBlock +// ) = epochManagerContract.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); + +// assertEq( +// startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * (numberOfEpochsToTravel + 1)) + 1, +// _firstBlock +// ); +// assertEq(_lastBlock, 0); +// assertEq(startingEpochStartTimestamp + (DAY * (numberOfEpochsToTravel + 1)), _startTimestamp); +// assertEq(_rewardBlock, 0); +// } + +// function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { +// initializeEpochManagerSystem(); +// uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); +// uint256 numberOfEpochsToTravel = 7; +// ( +// uint256 _startingEpochFirstBlock, +// uint256 _startingLastBlock, +// uint256 _startingStartTimestamp, +// uint256 _startingRewardBlock +// ) = epochManagerContract.getCurrentEpoch(); + +// _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); + +// ( +// uint256 _initialFirstBlock, +// uint256 _initialLastBlock, +// uint256 _initialStartTimestamp, +// uint256 _initialRewardBlock +// ) = epochManagerContract.getEpochByNumber(_startingEpochNumber); + +// assertEq(_initialFirstBlock, _startingEpochFirstBlock); +// assertEq(_initialLastBlock, _startingLastBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock); +// assertEq(_initialStartTimestamp, _startingStartTimestamp); +// assertEq( +// _initialRewardBlock, +// _startingRewardBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock + 1 +// ); +// } + +// function test_ReturnsZeroForFutureEpochs() public { +// initializeEpochManagerSystem(); +// ( +// uint256 _firstBlock, +// uint256 _lastBlock, +// uint256 _startTimestamp, +// uint256 _rewardBlock +// ) = epochManagerContract.getEpochByNumber(500); + +// assertEq(_firstBlock, 0); +// assertEq(_lastBlock, 0); +// assertEq(_startTimestamp, 0); +// assertEq(_rewardBlock, 0); +// } +// } + +// contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { +// function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { +// initializeEpochManagerSystem(); +// assertEq(epochManagerContract.getEpochNumberOfBlock(firstEpochBlock), firstEpochNumber); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); +// } +// } + +// contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { +// function test_ShouldRetreiveTheCorrectEpochInfoOfGivenBlock() public { +// initializeEpochManagerSystem(); + +// _travelAndProcess_N_L2Epoch(2); + +// (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManagerContract.getEpochByBlockNumber( +// firstEpochBlock + (3 * L2_BLOCK_IN_EPOCH) +// ); +// assertEq(_firstBlock, firstEpochBlock + 1 + (2 * L2_BLOCK_IN_EPOCH)); +// assertEq(_lastBlock, firstEpochBlock + 1 + (3 * L2_BLOCK_IN_EPOCH) - 1); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); +// } +// } + +// contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { +// function test_ShouldRetreiveTheNumberOfElected() public { +// initializeEpochManagerSystem(); +// assertEq(epochManagerContract.numberOfElectedInCurrentSet(), 2); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.numberOfElectedInCurrentSet(); +// } +// } + +// contract EpochManagerTest_getElectedAccounts is EpochManagerTest { +// function test_ShouldRetreiveThelistOfElectedAccounts() public { +// initializeEpochManagerSystem(); +// assertEq(epochManagerContract.getElectedAccounts(), firstElected); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getElectedAccounts(); +// } +// } + +// contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { +// function test_ShouldRetreiveThecorrectValidator() public { +// initializeEpochManagerSystem(); +// assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getElectedAccountByIndex(0); +// } +// } +// contract EpochManagerTest_getElectedSigners is EpochManagerTest { +// function test_ShouldRetreiveTheElectedSigners() public { +// initializeEpochManagerSystem(); +// address[] memory electedSigners = new address[](firstElected.length); +// electedSigners[0] = accountss.getValidatorSigner(firstElected[0]); +// electedSigners[1] = accountss.getValidatorSigner(firstElected[1]); +// assertEq(epochManagerContract.getElectedSigners(), electedSigners); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getElectedSigners(); +// } +// } +// contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { +// function test_ShouldRetreiveThecorrectElectedSigner() public { +// initializeEpochManagerSystem(); +// address[] memory electedSigners = new address[](firstElected.length); + +// electedSigners[1] = accountss.getValidatorSigner(firstElected[1]); +// assertEq(epochManagerContract.getElectedSignerByIndex(1), electedSigners[1]); +// } + +// function test_Reverts_WhenL1() public { +// vm.expectRevert("Epoch system not initialized"); +// epochManagerContract.getElectedSignerByIndex(1); +// } +// } From f44045cb9af9555134c9c20e164c792200ed8453 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:33:13 -0500 Subject: [PATCH 17/23] ++ finish epoch processing L2 test --- .../test-sol/unit/common/EpochManager.t.sol | 214 +++++++++--------- 1 file changed, 113 insertions(+), 101 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index de169a5d2fa..bcca4cd2aca 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -142,7 +142,7 @@ contract EpochManagerTest is TestWithUtils08 { validators.setValidator(validator2); // accountss.setValidatorSigner(validator2, validator2); - address[] memory members = new address[](2); + address[] memory members = new address[](100); members[0] = validator1; members[1] = validator2; validators.setMembers(group, members); @@ -652,137 +652,149 @@ contract EpochManagerTest_sendValidatorPayment_L2 is EpochManagerTest_L2 { } } -// contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { -// address signer1 = actor("signer1"); -// address signer2 = actor("signer2"); +contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { + function test_Reverts_onL1() public { + address[] memory groups = new address[](0); -// address validator3 = actor("validator3"); -// address validator4 = actor("validator4"); + vm.expectRevert("Epoch process is not started"); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); + } +} -// address group2 = actor("group2"); +contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { + address validator3 = actor("validator3"); + address validator4 = actor("validator4"); -// address[] elected; + address group2 = actor("group2"); -// uint256 groupEpochRewards = 44e18; + address[] elected; -// function setUp() public override { -// super.setUp(); + uint256 groupEpochRewards = 44e18; -// validators.setValidatorGroup(group); -// validators.setValidator(validator1); -// accountss.setValidatorSigner(validator1, signer1); -// validators.setValidator(validator2); -// accountss.setValidatorSigner(validator2, signer2); + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); -// validators.setValidatorGroup(group2); -// validators.setValidator(validator3); -// validators.setValidator(validator4); + validators.setValidatorGroup(group); + validators.setValidator(validator1); + validators.setValidator(validator2); -// address[] memory members = new address[](3); -// members[0] = validator1; -// members[1] = validator2; -// validators.setMembers(group, members); -// members[0] = validator3; -// members[1] = validator4; -// validators.setMembers(group2, members); + validators.setValidatorGroup(group2); + validators.setValidator(validator3); + validators.setValidator(validator4); -// vm.prank(address(epochManagerEnabler)); -// initializeEpochManagerSystem(); + address[] memory members = new address[](100); + address[] memory group2Members = new address[](3); + members[0] = validator1; + members[1] = validator2; -// elected = epochManagerContract.getElectedAccounts(); + for (uint256 i = 2; i < numberValidators; i++) { + address _currentValidator = vm.addr(i + 1); + members[i] = _currentValidator; + } -// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); -// } + validators.setMembers(group, members); -// function test_Reverts_WhenNotStarted() public { -// address[] memory groups = new address[](0); + group2Members[0] = validator3; + group2Members[1] = validator4; + validators.setMembers(group2, group2Members); -// vm.expectRevert("Epoch process is not started"); -// epochManagerContract.finishNextEpochProcess(groups, groups, groups); -// } + initializeEpochManagerSystem(); -// function test_Reverts_WhenGroupsDoNotMatch() public { -// address[] memory groups = new address[](0); -// epochManagerContract.startNextEpochProcess(); -// vm.expectRevert("number of groups does not match"); -// epochManagerContract.finishNextEpochProcess(groups, groups, groups); -// } + elected = epochManagerContract.getElectedAccounts(); + console.log("### electead accounts length", elected.length); + election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); + travelNL2Epoch(1); + } -// function test_Reverts_WhenGroupsNotFromElected() public { -// address[] memory groups = new address[](1); -// groups[0] = group2; -// epochManagerContract.startNextEpochProcess(); -// vm.expectRevert("group not from current elected set"); -// epochManagerContract.finishNextEpochProcess(groups, groups, groups); -// } + function test_Reverts_WhenNotStarted() public { + address[] memory groups = new address[](0); -// function test_TransfersToCommunityAndCarbonOffsetting() public { -// ( -// address[] memory groups, -// address[] memory lessers, -// address[] memory greaters -// ) = getGroupsWithLessersAndGreaters(); + vm.expectRevert("Epoch process is not started"); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); + } -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); + function test_Reverts_WhenGroupsDoNotMatch() public { + address[] memory groups = new address[](0); + epochManagerContract.startNextEpochProcess(); + vm.expectRevert("number of groups does not match"); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); + } -// assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); -// assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); -// } + function test_Reverts_WhenGroupsNotFromElected() public { + address[] memory groups = new address[](1); + groups[0] = group2; + epochManagerContract.startNextEpochProcess(); + vm.expectRevert("group not from current elected set"); + epochManagerContract.finishNextEpochProcess(groups, groups, groups); + } -// function test_TransfersToValidatorGroup() public { -// ( -// address[] memory groups, -// address[] memory lessers, -// address[] memory greaters -// ) = getGroupsWithLessersAndGreaters(); + function test_TransfersToCommunityAndCarbonOffsetting() public { + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); -// assertEq(election.distributedEpochRewards(group), groupEpochRewards); -// } + assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); + assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); + } -// function test_SetsNewlyElectedCorrectly() public { -// ( -// address[] memory groups, -// address[] memory lessers, -// address[] memory greaters -// ) = getGroupsWithLessersAndGreaters(); + function test_TransfersToValidatorGroup() public { + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); -// epochManagerContract.startNextEpochProcess(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); -// address[] memory newElected = new address[](2); -// newElected[0] = validator3; -// newElected[1] = validator4; -// election.setElectedValidators(newElected); + assertEq(election.distributedEpochRewards(group), groupEpochRewards); + } -// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); + function test_SetsNewlyElectedCorrectly() public { + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); -// address[] memory afterElected = epochManagerContract.getElectedAccounts(); + epochManagerContract.startNextEpochProcess(); -// for (uint256 i = 0; i < newElected.length; i++) { -// assertEq(newElected[i], afterElected[i]); -// } -// } + address[] memory newElected = new address[](2); + newElected[0] = validator3; + newElected[1] = validator4; + election.setElectedValidators(newElected); -// function test_Emits_GroupProcessedEvent() public { -// ( -// address[] memory groups, -// address[] memory lessers, -// address[] memory greaters -// ) = getGroupsWithLessersAndGreaters(); + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); -// epochManagerContract.startNextEpochProcess(); + address[] memory afterElected = epochManagerContract.getElectedAccounts(); -// for (uint i = 0; i < groups.length; i++) { -// vm.expectEmit(true, true, true, true); -// emit GroupProcessed(groups[i], firstEpochNumber); -// } + for (uint256 i = 0; i < newElected.length; i++) { + assertEq(newElected[i], afterElected[i]); + } + } -// epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); -// } -// } + function test_Emits_GroupProcessedEvent() public { + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); + + epochManagerContract.startNextEpochProcess(); + + for (uint i = 0; i < groups.length; i++) { + vm.expectEmit(true, true, true, true); + emit GroupProcessed(groups[i], firstEpochNumber); + } + + epochManagerContract.finishNextEpochProcess(groups, lessers, greaters); + } +} // contract EpochManagerTest_setToProcessGroups is EpochManagerTest { // address signer1 = actor("signer1"); From 24b9f966bbb7acb752574391d2c7e687e0976436 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:17:49 -0500 Subject: [PATCH 18/23] clean up setup --- .../test-sol/unit/common/EpochManager.t.sol | 68 +++++++------------ 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index bcca4cd2aca..8e12995409f 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -55,6 +55,11 @@ contract EpochManagerTest is TestWithUtils08 { address group = actor("group"); + address validator3 = actor("validator3"); + address validator4 = actor("validator4"); + + address group2 = actor("group2"); + event ValidatorEpochPaymentDistributed( address indexed validator, uint256 validatorPayment, @@ -138,19 +143,30 @@ contract EpochManagerTest is TestWithUtils08 { // TODO(soloseng): rename function validators.setValidatorGroup(group); validators.setValidator(validator1); - // accountss.setValidatorSigner(validator1, validator1); + validators.setValidator(validator2); - // accountss.setValidatorSigner(validator2, validator2); + + validators.setValidatorGroup(group2); + validators.setValidator(validator3); + validators.setValidator(validator4); address[] memory members = new address[](100); + address[] memory group2Members = new address[](3); members[0] = validator1; members[1] = validator2; + + for (uint256 i = 2; i < numberValidators; i++) { + address _currentValidator = vm.addr(i + 1); + members[i] = _currentValidator; + } + validators.setMembers(group, members); - election.setElectedValidators(members); + group2Members[0] = validator3; + group2Members[1] = validator4; + validators.setMembers(group2, group2Members); - // vm.prank(address(epochManagerEnabler)); - // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + election.setElectedValidators(members); travelNL2Epoch(1); } @@ -662,48 +678,14 @@ contract EpochManagerTest_finishNextEpochProcess is EpochManagerTest { } contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { - address validator3 = actor("validator3"); - address validator4 = actor("validator4"); - - address group2 = actor("group2"); - - address[] elected; - uint256 groupEpochRewards = 44e18; function setUp() public override(EpochManagerTest_L2) { super.setUp(); - validators.setValidatorGroup(group); - validators.setValidator(validator1); - validators.setValidator(validator2); - - validators.setValidatorGroup(group2); - validators.setValidator(validator3); - validators.setValidator(validator4); - - address[] memory members = new address[](100); - address[] memory group2Members = new address[](3); - members[0] = validator1; - members[1] = validator2; - - for (uint256 i = 2; i < numberValidators; i++) { - address _currentValidator = vm.addr(i + 1); - members[i] = _currentValidator; - } - - validators.setMembers(group, members); - - group2Members[0] = validator3; - group2Members[1] = validator4; - validators.setMembers(group2, group2Members); - initializeEpochManagerSystem(); - elected = epochManagerContract.getElectedAccounts(); - console.log("### electead accounts length", elected.length); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); - travelNL2Epoch(1); } function test_Reverts_WhenNotStarted() public { @@ -797,8 +779,8 @@ contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { } // contract EpochManagerTest_setToProcessGroups is EpochManagerTest { -// address signer1 = actor("signer1"); -// address signer2 = actor("signer2"); +// // address signer1 = actor("signer1"); +// // address signer2 = actor("signer2"); // address validator3 = actor("validator3"); // address validator4 = actor("validator4"); @@ -814,9 +796,9 @@ contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { // validators.setValidatorGroup(group); // validators.setValidator(validator1); -// accountss.setValidatorSigner(validator1, signer1); +// // accountss.setValidatorSigner(validator1, signer1); // validators.setValidator(validator2); -// accountss.setValidatorSigner(validator2, signer2); +// // accountss.setValidatorSigner(validator2, signer2); // validators.setValidatorGroup(group2); // validators.setValidator(validator3); From 8f2fcf5e1441f7419c4c08d3c45c356ffcba64a6 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Thu, 16 Jan 2025 13:06:55 -0500 Subject: [PATCH 19/23] ++ more working L1 & L2 test --- .../test-sol/unit/common/EpochManager.t.sol | 568 +++++++++--------- 1 file changed, 280 insertions(+), 288 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 8e12995409f..7b1d9002025 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -35,8 +35,8 @@ contract EpochManagerTest is TestWithUtils08 { address scoreManagerAddress; uint256 firstEpochNumber = 3; - // uint256 firstEpochNumber = 100; - uint256 firstEpochBlock = 100; + + // uint256 firstEpochBlock = 100; uint256 epochDuration = DAY; address[] firstElected; @@ -133,8 +133,6 @@ contract EpochManagerTest is TestWithUtils08 { epochManagerContract.initialize(REGISTRY_ADDRESS, 10); epochRewards.setCarbonOffsettingPartner(carbonOffsettingPartner); - blockTravel(firstEpochBlock); - validators.setEpochRewards(validator1, validator1Reward); validators.setEpochRewards(validator2, validator2Reward); } @@ -229,9 +227,27 @@ contract EpochManagerTest_initialize is EpochManagerTest { } contract EpochManagerTest_initializeSystem is EpochManagerTest { + uint256 lastKnownEpochNumber; + uint256 lastKnownFirstBlockOfEpoch; + address[] lastKnownElectedAccounts; + + function setUp() public override { + super.setUp(); + _registerAndElectValidatorsForL2(); + epochManagerEnabler.captureEpochAndValidators(); + + lastKnownEpochNumber = epochManagerEnabler.lastKnownEpochNumber(); + lastKnownFirstBlockOfEpoch = epochManagerEnabler.lastKnownFirstBlockOfEpoch(); + lastKnownElectedAccounts = epochManagerEnabler.getlastKnownElectedAccounts(); + } function test_processCanBeStarted() public virtual { vm.prank(address(epochManagerEnabler)); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + + epochManagerContract.initializeSystem( + lastKnownEpochNumber, + lastKnownFirstBlockOfEpoch, + lastKnownElectedAccounts + ); ( uint256 _firstEpochBlock, uint256 _lastEpochBlock, @@ -239,35 +255,47 @@ contract EpochManagerTest_initializeSystem is EpochManagerTest { uint256 _currentRewardsBlock ) = epochManagerContract.getCurrentEpoch(); assertGt(epochManagerContract.getElectedAccounts().length, 0); - assertEq(epochManagerContract.firstKnownEpoch(), firstEpochNumber); - assertEq(_firstEpochBlock, firstEpochBlock); + assertEq(epochManagerContract.firstKnownEpoch(), lastKnownEpochNumber); + assertEq(_firstEpochBlock, lastKnownFirstBlockOfEpoch); assertEq(_lastEpochBlock, 0); assertEq(_startTimestamp, block.timestamp); assertEq(_currentRewardsBlock, 0); - assertEq(epochManagerContract.getElectedAccounts(), firstElected); + assertEq(epochManagerContract.getElectedAccounts(), lastKnownElectedAccounts); } function test_Reverts_processCannotBeStartedAgain() public virtual { vm.prank(address(epochManagerEnabler)); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem( + lastKnownEpochNumber, + lastKnownFirstBlockOfEpoch, + lastKnownElectedAccounts + ); vm.prank(address(epochManagerEnabler)); vm.expectRevert("Epoch system already initialized"); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem( + lastKnownEpochNumber, + lastKnownFirstBlockOfEpoch, + lastKnownElectedAccounts + ); } function test_Reverts_WhenSystemInitializedByOtherContract() public virtual { vm.expectRevert("msg.sender is not Enabler"); - epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); + epochManagerContract.initializeSystem( + lastKnownEpochNumber, + lastKnownFirstBlockOfEpoch, + lastKnownElectedAccounts + ); } } -contract EpochManagerTest_initializeSystem_L2 is - EpochManagerTest_L2_NoInit, - EpochManagerTest_initializeSystem -{ - function setUp() public override(EpochManagerTest, EpochManagerTest_L2_NoInit) { - super.setUp(); - } -} // XXX(soloseng):is this needed, since it would be calling epochmanagerEnabler. +// contract EpochManagerTest_initializeSystem_L2 is +// EpochManagerTest_L2_NoInit, +// EpochManagerTest_initializeSystem +// { +// function setUp() public override(EpochManagerTest, EpochManagerTest_L2_NoInit) { +// super.setUp(); +// } +// } // XXX(soloseng):is this needed, since it would be calling epochmanagerEnabler. contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { function test_Reverts_onL1() public { @@ -282,15 +310,7 @@ contract EpochManagerTest_startNextEpochProcess_L2_NoInit is EpochManagerTest_L2 } } contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { - // function test_Reverts_whenSystemNotInitialized() public { - // vm.expectRevert("Epoch system not initialized"); - // epochManagerContract.startNextEpochProcess(); - // } - function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { - // vm.prank(address(epochManagerEnabler)); - // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - vm.expectRevert("Epoch is not ready to start"); epochManagerContract.startNextEpochProcess(); } @@ -499,18 +519,13 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { _registerAndElectValidatorsForL2(); validators.setValidatorGroup(group); validators.setValidator(validator1); - // accountss.setValidatorSigner(validator1, signer1); validators.setValidator(validator2); - // accountss.setValidatorSigner(validator2, signer2); address[] memory members = new address[](2); members[0] = validator1; members[1] = validator2; validators.setMembers(group, members); - // vm.prank(address(epochManagerEnabler)); - // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - stableToken.mint(address(epochManagerContract), paymentAmount * 2); epochManagerBalanceBefore = stableToken.balanceOf(address(epochManagerContract)); epochManagerContract._setPaymentAllocation(validator1, paymentAmount); @@ -546,18 +561,13 @@ contract EpochManagerTest_sendValidatorPayment_L2 is EpochManagerTest_L2 { validators.setValidatorGroup(group); validators.setValidator(validator1); - // accountss.setValidatorSigner(validator1, signer1); validators.setValidator(validator2); - // accountss.setValidatorSigner(validator2, signer2); address[] memory members = new address[](2); members[0] = validator1; members[1] = validator2; validators.setMembers(group, members); - // vm.prank(address(epochManagerEnabler)); - // epochManagerContract.initializeSystem(firstEpochNumber, firstEpochBlock, firstElected); - stableToken.mint(address(epochManagerContract), paymentAmount * 2); epochManagerBalanceBefore = stableToken.balanceOf(address(epochManagerContract)); epochManagerContract._setPaymentAllocation(validator1, paymentAmount); @@ -778,313 +788,295 @@ contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { } } -// contract EpochManagerTest_setToProcessGroups is EpochManagerTest { -// // address signer1 = actor("signer1"); -// // address signer2 = actor("signer2"); +contract EpochManagerTest_setToProcessGroups is EpochManagerTest { + function test_Reverts_onL1() public { + vm.expectRevert("Epoch process is not started"); + epochManagerContract.setToProcessGroups(); + } +} -// address validator3 = actor("validator3"); -// address validator4 = actor("validator4"); +contract EpochManagerTest_setToProcessGroups_L2 is EpochManagerTest_L2 { + uint256 groupEpochRewards = 44e18; -// address group2 = actor("group2"); + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); -// address[] elected; + initializeEpochManagerSystem(); -// uint256 groupEpochRewards = 44e18; + election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); + } -// function setUp() public override { -// super.setUp(); + function test_Reverts_WhenNotStarted() public { + vm.expectRevert("Epoch process is not started"); + epochManagerContract.setToProcessGroups(); + } -// validators.setValidatorGroup(group); -// validators.setValidator(validator1); -// // accountss.setValidatorSigner(validator1, signer1); -// validators.setValidator(validator2); -// // accountss.setValidatorSigner(validator2, signer2); - -// validators.setValidatorGroup(group2); -// validators.setValidator(validator3); -// validators.setValidator(validator4); - -// address[] memory members = new address[](3); -// members[0] = validator1; -// members[1] = validator2; -// validators.setMembers(group, members); -// members[0] = validator3; -// members[1] = validator4; -// validators.setMembers(group2, members); - -// vm.prank(address(epochManagerEnabler)); -// initializeEpochManagerSystem(); + function test_setsToProcessGroups() public { + (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); -// elected = epochManagerContract.getElectedAccounts(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); -// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); -// } + assertEq(EpochManager(address(epochManagerContract)).toProcessGroups(), groups.length); + } -// function test_Reverts_WhenNotStarted() public { -// vm.expectRevert("Epoch process is not started"); -// epochManagerContract.setToProcessGroups(); -// } + function test_blocksChilds() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + assertTrue(epochManagerContract.isBlocked()); + } -// function test_setsToProcessGroups() public { -// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); + function test_Reverts_startEpochAgain() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + vm.expectRevert("Epoch process is already started"); + epochManagerContract.startNextEpochProcess(); + } -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); + function test_Reverts_WhenSetToProcessGroups() public { + vm.expectRevert("Epoch process is not started"); + epochManagerContract.setToProcessGroups(); + } -// assertEq(EpochManager(address(epochManagerContract)).toProcessGroups(), groups.length); -// } + function test_setsGroupRewards() public { + (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); -// function test_blocksChilds() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// assertTrue(epochManagerContract.isBlocked()); -// } + for (uint256 i = 0; i < groups.length; i++) { + assertEq( + EpochManager(address(epochManagerContract)).processedGroups(group), + groupEpochRewards + ); + } + } -// function test_Reverts_startEpochAgain() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// vm.expectRevert("Epoch process is already started"); -// epochManagerContract.startNextEpochProcess(); -// } + function test_Emits_GroupMarkedForProcessingEvent() public { + (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); -// function test_Reverts_WhenSetToProcessGroups() public { -// vm.expectRevert("Epoch process is not started"); -// epochManagerContract.setToProcessGroups(); -// } + epochManagerContract.startNextEpochProcess(); -// function test_setsGroupRewards() public { -// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); - -// for (uint256 i = 0; i < groups.length; i++) { -// assertEq( -// EpochManager(address(epochManagerContract)).processedGroups(group), -// groupEpochRewards -// ); -// } -// } + for (uint i = 0; i < groups.length; i++) { + vm.expectEmit(true, true, true, true); + emit GroupMarkedForProcessing(groups[i], firstEpochNumber); + } -// function test_Emits_GroupMarkedForProcessingEvent() public { -// (address[] memory groups, , ) = getGroupsWithLessersAndGreaters(); + epochManagerContract.setToProcessGroups(); + } +} -// epochManagerContract.startNextEpochProcess(); +contract EpochManagerTest_processGroup is EpochManagerTest { + // function setUp() public override { + // super.setUp(); + // } -// for (uint i = 0; i < groups.length; i++) { -// vm.expectEmit(true, true, true, true); -// emit GroupMarkedForProcessing(groups[i], firstEpochNumber); -// } + function test_Reverts_onL1() public { + vm.expectRevert("Indivudual epoch process is not started"); + epochManagerContract.processGroup(group, address(0), address(0)); + } +} -// epochManagerContract.setToProcessGroups(); -// } -// } +contract EpochManagerTest_processGroup_L2 is EpochManagerTest_L2 { + uint256 groupEpochRewards = 44e18; -// contract EpochManagerTest_processGroup is EpochManagerTest { -// address signer1 = actor("signer1"); -// address signer2 = actor("signer2"); -// address signer3 = actor("signer3"); -// address signer4 = actor("signer4"); + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); -// address validator3 = actor("validator3"); -// address validator4 = actor("validator4"); + initializeEpochManagerSystem(); -// address group2 = actor("group2"); + election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); + } -// address[] elected; + function test_Reverts_WhenNotStarted() public { + vm.expectRevert("Indivudual epoch process is not started"); + epochManagerContract.processGroup(group, address(0), address(0)); + } -// uint256 groupEpochRewards = 44e18; + function test_Reverts_WhenGroupNotInToProcessGroups() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + vm.expectRevert("group not from current elected set"); + epochManagerContract.processGroup(group2, address(0), address(0)); + } -// function setUp() public override { -// super.setUp(); + function test_ProcessesGroup() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); -// validators.setValidatorGroup(group); -// validators.setValidator(validator1); -// accountss.setValidatorSigner(validator1, signer1); -// validators.setValidator(validator2); -// accountss.setValidatorSigner(validator2, signer2); - -// validators.setValidatorGroup(group2); -// validators.setValidator(validator3); -// validators.setValidator(validator4); - -// address[] memory members = new address[](3); -// members[0] = validator1; -// members[1] = validator2; -// validators.setMembers(group, members); -// members[0] = validator3; -// members[1] = validator4; -// validators.setMembers(group2, members); - -// vm.prank(address(epochManagerEnabler)); -// initializeEpochManagerSystem(); + (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); + assertEq(status, 0); + } -// elected = epochManagerContract.getElectedAccounts(); + function test_TransfersToCommunityAndCarbonOffsetting() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); -// election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); -// } + assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); + assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); + } -// function test_Reverts_WhenNotStarted() public { -// vm.expectRevert("Indivudual epoch process is not started"); -// epochManagerContract.processGroup(group, address(0), address(0)); -// } + function test_TransfersToValidatorGroup() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + epochManagerContract.processGroup(group, address(0), address(0)); -// function test_Reverts_WhenGroupNotInToProcessGroups() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// vm.expectRevert("group not from current elected set"); -// epochManagerContract.processGroup(group2, address(0), address(0)); -// } + assertEq(election.distributedEpochRewards(group), groupEpochRewards); + } -// function test_ProcessesGroup() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// epochManagerContract.processGroup(group, address(0), address(0)); + function test_SetsNewlyElectedCorrectly() public { + ( + address[] memory groups, + address[] memory lessers, + address[] memory greaters + ) = getGroupsWithLessersAndGreaters(); -// (uint256 status, , , , ) = epochManagerContract.getEpochProcessingState(); -// assertEq(status, 0); -// } + epochManagerContract.startNextEpochProcess(); -// function test_TransfersToCommunityAndCarbonOffsetting() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// epochManagerContract.processGroup(group, address(0), address(0)); + address[] memory newElected = new address[](2); + newElected[0] = validator3; + newElected[1] = validator4; -// assertEq(celoToken.balanceOf(communityRewardFund), epochRewards.totalRewardsCommunity()); -// assertEq(celoToken.balanceOf(carbonOffsettingPartner), epochRewards.totalRewardsCarbonFund()); -// } + vm.prank(newElected[0]); + accountsContract.createAccount(); + vm.prank(newElected[1]); + accountsContract.createAccount(); -// function test_TransfersToValidatorGroup() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// epochManagerContract.processGroup(group, address(0), address(0)); + election.setElectedValidators(newElected); -// assertEq(election.distributedEpochRewards(group), groupEpochRewards); -// } + address[] memory signers = new address[](2); + signers[0] = validator3; + signers[1] = validator4; -// function test_SetsNewlyElectedCorrectly() public { -// ( -// address[] memory groups, -// address[] memory lessers, -// address[] memory greaters -// ) = getGroupsWithLessersAndGreaters(); + epochManagerContract.setToProcessGroups(); -// epochManagerContract.startNextEpochProcess(); + for (uint256 i = 0; i < groups.length; i++) { + epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); + } + + address[] memory afterElected = epochManagerContract.getElectedAccounts(); + + for (uint256 i = 0; i < newElected.length; i++) { + assertEq(newElected[i], afterElected[i]); + } + + address[] memory afterSigners = epochManagerContract.getElectedSigners(); + assertEq(afterSigners.length, signers.length); + for (uint256 i = 0; i < signers.length; i++) { + assertEq(signers[i], afterSigners[i]); + } + } + + function test_Emits_GroupProcessed() public { + epochManagerContract.startNextEpochProcess(); + epochManagerContract.setToProcessGroups(); + vm.expectEmit(true, true, true, true); + emit GroupProcessed(group, firstEpochNumber); + epochManagerContract.processGroup(group, address(0), address(0)); + } +} -// address[] memory newElected = new address[](2); -// newElected[0] = validator3; -// newElected[1] = validator4; -// election.setElectedValidators(newElected); +contract EpochManagerTest_getEpochByNumber is EpochManagerTest { + function test_Reverts_onL1() public { + uint256 numberOfEpochsToTravel = 9; + vm.expectRevert("Epoch system not initialized"); -// address[] memory signers = new address[](2); -// signers[0] = signer3; -// signers[1] = signer4; -// accountss.setValidatorSigner(validator3, signer3); -// accountss.setValidatorSigner(validator4, signer4); + ( + uint256 _firstBlock, + uint256 _lastBlock, + uint256 _startTimestamp, + uint256 _rewardBlock + ) = epochManagerContract.getEpochByNumber(9); + } +} +contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); -// epochManagerContract.setToProcessGroups(); + initializeEpochManagerSystem(); + } + function test_shouldReturnTheEpochInfoOfSpecifiedEpoch() public { + uint256 numberOfEpochsToTravel = 9; -// for (uint256 i = 0; i < groups.length; i++) { -// epochManagerContract.processGroup(groups[i], lessers[i], greaters[i]); -// } + uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); -// address[] memory afterElected = epochManagerContract.getElectedAccounts(); + ( + uint256 startingEpochFirstBlock, + , + uint256 startingEpochStartTimestamp, -// for (uint256 i = 0; i < newElected.length; i++) { -// assertEq(newElected[i], afterElected[i]); -// } + ) = epochManagerContract.getCurrentEpoch(); -// address[] memory afterSigners = epochManagerContract.getElectedSigners(); -// assertEq(afterSigners.length, signers.length); -// for (uint256 i = 0; i < signers.length; i++) { -// assertEq(signers[i], afterSigners[i]); -// } -// } + _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); -// function test_Emits_GroupProcessed() public { -// epochManagerContract.startNextEpochProcess(); -// epochManagerContract.setToProcessGroups(); -// vm.expectEmit(true, true, true, true); -// emit GroupProcessed(group, firstEpochNumber); -// epochManagerContract.processGroup(group, address(0), address(0)); -// } -// } + ( + uint256 _firstBlock, + uint256 _lastBlock, + uint256 _startTimestamp, + uint256 _rewardBlock + ) = epochManagerContract.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); -// contract EpochManagerTest_getEpochByNumber is EpochManagerTest { -// function test_shouldReturnTheEpochInfoOfSpecifiedEpoch() public { -// uint256 numberOfEpochsToTravel = 9; + assertEq( + startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * (numberOfEpochsToTravel + 1)) + 1, + _firstBlock + ); + assertEq(_lastBlock, 0); + assertEq(startingEpochStartTimestamp + (DAY * (numberOfEpochsToTravel + 1)), _startTimestamp); + assertEq(_rewardBlock, 0); + } -// initializeEpochManagerSystem(); -// uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); + function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { + uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); + console.log("current block", block.number); + uint256 numberOfEpochsToTravel = 7; + ( + uint256 _startingEpochFirstBlock, + uint256 _startingLastBlock, + uint256 _startingStartTimestamp, + uint256 _startingRewardBlock + ) = epochManagerContract.getCurrentEpoch(); -// ( -// uint256 startingEpochFirstBlock, -// , -// uint256 startingEpochStartTimestamp, + _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); -// ) = epochManagerContract.getCurrentEpoch(); + ( + uint256 _initialFirstBlock, + uint256 _initialLastBlock, + uint256 _initialStartTimestamp, + uint256 _initialRewardBlock + ) = epochManagerContract.getEpochByNumber(_startingEpochNumber); -// _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); + assertEq(_initialFirstBlock, _startingEpochFirstBlock, "Starting block does not match"); -// ( -// uint256 _firstBlock, -// uint256 _lastBlock, -// uint256 _startTimestamp, -// uint256 _rewardBlock -// ) = epochManagerContract.getEpochByNumber(_startingEpochNumber + numberOfEpochsToTravel); + assertEq( + _initialLastBlock, + _startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * 2), + "Last block does not match" + ); -// assertEq( -// startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * (numberOfEpochsToTravel + 1)) + 1, -// _firstBlock -// ); -// assertEq(_lastBlock, 0); -// assertEq(startingEpochStartTimestamp + (DAY * (numberOfEpochsToTravel + 1)), _startTimestamp); -// assertEq(_rewardBlock, 0); -// } + assertEq(_initialStartTimestamp, _startingStartTimestamp, "Timestamp does not match"); + assertEq( + _initialRewardBlock, + _startingEpochFirstBlock + (L2_BLOCK_IN_EPOCH * 2) + 1, + "Reward block does not match" + ); + } -// function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { -// initializeEpochManagerSystem(); -// uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); -// uint256 numberOfEpochsToTravel = 7; -// ( -// uint256 _startingEpochFirstBlock, -// uint256 _startingLastBlock, -// uint256 _startingStartTimestamp, -// uint256 _startingRewardBlock -// ) = epochManagerContract.getCurrentEpoch(); - -// _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); - -// ( -// uint256 _initialFirstBlock, -// uint256 _initialLastBlock, -// uint256 _initialStartTimestamp, -// uint256 _initialRewardBlock -// ) = epochManagerContract.getEpochByNumber(_startingEpochNumber); - -// assertEq(_initialFirstBlock, _startingEpochFirstBlock); -// assertEq(_initialLastBlock, _startingLastBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock); -// assertEq(_initialStartTimestamp, _startingStartTimestamp); -// assertEq( -// _initialRewardBlock, -// _startingRewardBlock + (L2_BLOCK_IN_EPOCH * 2) + firstEpochBlock + 1 -// ); -// } + function test_ReturnsZeroForFutureEpochs() public { + initializeEpochManagerSystem(); + ( + uint256 _firstBlock, + uint256 _lastBlock, + uint256 _startTimestamp, + uint256 _rewardBlock + ) = epochManagerContract.getEpochByNumber(500); -// function test_ReturnsZeroForFutureEpochs() public { -// initializeEpochManagerSystem(); -// ( -// uint256 _firstBlock, -// uint256 _lastBlock, -// uint256 _startTimestamp, -// uint256 _rewardBlock -// ) = epochManagerContract.getEpochByNumber(500); - -// assertEq(_firstBlock, 0); -// assertEq(_lastBlock, 0); -// assertEq(_startTimestamp, 0); -// assertEq(_rewardBlock, 0); -// } -// } + assertEq(_firstBlock, 0); + assertEq(_lastBlock, 0); + assertEq(_startTimestamp, 0); + assertEq(_rewardBlock, 0); + } +} // contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { // function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { From 0b31d9ca37cc09d3519e1c022e28998145997b07 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:31:29 -0500 Subject: [PATCH 20/23] ++ remaining L2 tests --- .../test-sol/unit/common/EpochManager.t.sol | 232 ++++++++++-------- 1 file changed, 135 insertions(+), 97 deletions(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 7b1d9002025..81bc7f14529 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -979,15 +979,9 @@ contract EpochManagerTest_processGroup_L2 is EpochManagerTest_L2 { contract EpochManagerTest_getEpochByNumber is EpochManagerTest { function test_Reverts_onL1() public { - uint256 numberOfEpochsToTravel = 9; vm.expectRevert("Epoch system not initialized"); - ( - uint256 _firstBlock, - uint256 _lastBlock, - uint256 _startTimestamp, - uint256 _rewardBlock - ) = epochManagerContract.getEpochByNumber(9); + epochManagerContract.getEpochByNumber(9); } } contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { @@ -1030,12 +1024,8 @@ contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); console.log("current block", block.number); uint256 numberOfEpochsToTravel = 7; - ( - uint256 _startingEpochFirstBlock, - uint256 _startingLastBlock, - uint256 _startingStartTimestamp, - uint256 _startingRewardBlock - ) = epochManagerContract.getCurrentEpoch(); + (uint256 _startingEpochFirstBlock, , uint256 _startingStartTimestamp, ) = epochManagerContract + .getCurrentEpoch(); _travelAndProcess_N_L2Epoch(numberOfEpochsToTravel); @@ -1078,97 +1068,145 @@ contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { } } -// contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { -// function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { -// initializeEpochManagerSystem(); -// assertEq(epochManagerContract.getEpochNumberOfBlock(firstEpochBlock), firstEpochNumber); -// } - -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); -// } -// } - -// contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { -// function test_ShouldRetreiveTheCorrectEpochInfoOfGivenBlock() public { -// initializeEpochManagerSystem(); - -// _travelAndProcess_N_L2Epoch(2); +contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getEpochNumberOfBlock(75); + } +} +contract EpochManagerTest_getEpochNumberOfBlock_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + } -// (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManagerContract.getEpochByBlockNumber( -// firstEpochBlock + (3 * L2_BLOCK_IN_EPOCH) -// ); -// assertEq(_firstBlock, firstEpochBlock + 1 + (2 * L2_BLOCK_IN_EPOCH)); -// assertEq(_lastBlock, firstEpochBlock + 1 + (3 * L2_BLOCK_IN_EPOCH) - 1); -// } + function test_ShouldRetreiveTheCorrectBlockNumberOfTheEpoch() public { + assertEq( + epochManagerContract.getEpochNumberOfBlock(epochManagerEnabler.lastKnownFirstBlockOfEpoch()), + firstEpochNumber + ); + } +} -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getEpochNumberOfBlock(firstEpochBlock); -// } -// } +contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getEpochNumberOfBlock(1000); + } +} +contract EpochManagerTest_getEpochByBlockNumber_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + _travelAndProcess_N_L2Epoch(2); + } + function test_ShouldRetreiveTheCorrectEpochInfoOfGivenBlock() public { + (uint256 _firstBlock, uint256 _lastBlock, , ) = epochManagerContract.getEpochByBlockNumber( + epochManagerEnabler.lastKnownFirstBlockOfEpoch() + (3 * L2_BLOCK_IN_EPOCH) + ); + assertEq( + _firstBlock, + epochManagerEnabler.lastKnownFirstBlockOfEpoch() + 1 + (2 * L2_BLOCK_IN_EPOCH) + ); + assertEq( + _lastBlock, + epochManagerEnabler.lastKnownFirstBlockOfEpoch() + 1 + (3 * L2_BLOCK_IN_EPOCH) - 1 + ); + } +} -// contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { -// function test_ShouldRetreiveTheNumberOfElected() public { -// initializeEpochManagerSystem(); -// assertEq(epochManagerContract.numberOfElectedInCurrentSet(), 2); -// } +contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.numberOfElectedInCurrentSet(); + } +} +contract EpochManagerTest_numberOfElectedInCurrentSet_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + } + function test_ShouldRetreiveTheNumberOfElected() public { + assertEq( + epochManagerContract.numberOfElectedInCurrentSet(), + epochManagerEnabler.getlastKnownElectedAccounts().length + ); + } +} -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.numberOfElectedInCurrentSet(); -// } -// } +contract EpochManagerTest_getElectedAccounts is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getElectedAccounts(); + } +} -// contract EpochManagerTest_getElectedAccounts is EpochManagerTest { -// function test_ShouldRetreiveThelistOfElectedAccounts() public { -// initializeEpochManagerSystem(); -// assertEq(epochManagerContract.getElectedAccounts(), firstElected); -// } +contract EpochManagerTest_getElectedAccounts_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + } + function test_ShouldRetreiveThelistOfElectedAccounts() public { + assertEq( + epochManagerContract.getElectedAccounts(), + epochManagerEnabler.getlastKnownElectedAccounts() + ); + } +} -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getElectedAccounts(); -// } -// } +contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getElectedAccountByIndex(0); + } +} +contract EpochManagerTest_getElectedAccountByIndex_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + } + function test_ShouldRetreiveThecorrectValidator() public { + assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); + } +} -// contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { -// function test_ShouldRetreiveThecorrectValidator() public { -// initializeEpochManagerSystem(); -// assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); -// } +contract EpochManagerTest_getElectedSigners is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getElectedSigners(); + } +} +contract EpochManagerTest_getElectedSigners_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + } -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getElectedAccountByIndex(0); -// } -// } -// contract EpochManagerTest_getElectedSigners is EpochManagerTest { -// function test_ShouldRetreiveTheElectedSigners() public { -// initializeEpochManagerSystem(); -// address[] memory electedSigners = new address[](firstElected.length); -// electedSigners[0] = accountss.getValidatorSigner(firstElected[0]); -// electedSigners[1] = accountss.getValidatorSigner(firstElected[1]); -// assertEq(epochManagerContract.getElectedSigners(), electedSigners); -// } + function test_ShouldRetreiveTheElectedSigners() public { + address[] memory knownElectedAccounts = epochManagerEnabler.getlastKnownElectedAccounts(); + address[] memory electedSigners = new address[](knownElectedAccounts.length); + for (uint256 i = 0; i < knownElectedAccounts.length; i++) { + electedSigners[i] = accountsContract.getValidatorSigner(knownElectedAccounts[i]); + } + assertEq(epochManagerContract.getElectedSigners(), electedSigners); + } +} -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getElectedSigners(); -// } -// } -// contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { -// function test_ShouldRetreiveThecorrectElectedSigner() public { -// initializeEpochManagerSystem(); -// address[] memory electedSigners = new address[](firstElected.length); - -// electedSigners[1] = accountss.getValidatorSigner(firstElected[1]); -// assertEq(epochManagerContract.getElectedSignerByIndex(1), electedSigners[1]); -// } +contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { + function test_Reverts_WhenL1() public { + vm.expectRevert("Epoch system not initialized"); + epochManagerContract.getElectedSignerByIndex(1); + } +} +contract EpochManagerTest_getElectedSignerByIndex_L2 is EpochManagerTest_L2 { + function setUp() public override(EpochManagerTest_L2) { + super.setUp(); + initializeEpochManagerSystem(); + } + function test_ShouldRetreiveThecorrectElectedSigner() public { + address[] memory knownElectedAccounts = epochManagerEnabler.getlastKnownElectedAccounts(); + address[] memory electedSigners = new address[](knownElectedAccounts.length); -// function test_Reverts_WhenL1() public { -// vm.expectRevert("Epoch system not initialized"); -// epochManagerContract.getElectedSignerByIndex(1); -// } -// } + electedSigners[1] = accountsContract.getValidatorSigner(knownElectedAccounts[1]); + assertEq(epochManagerContract.getElectedSignerByIndex(1), electedSigners[1]); + } +} From 286e5696e96d2e71f13921146c89254805bd8f7b Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:38:18 -0500 Subject: [PATCH 21/23] clean up comments --- .../protocol/test-sol/TestWithUtils08.sol | 11 --- .../test-sol/unit/common/EpochManager.t.sol | 84 ++++++++++--------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/packages/protocol/test-sol/TestWithUtils08.sol b/packages/protocol/test-sol/TestWithUtils08.sol index fbe276d21be..fce5cf1fe3f 100644 --- a/packages/protocol/test-sol/TestWithUtils08.sol +++ b/packages/protocol/test-sol/TestWithUtils08.sol @@ -10,7 +10,6 @@ import "@celo-contracts/common/interfaces/IRegistry.sol"; import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; -import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; import { console } from "forge-std-8/console.sol"; @@ -20,7 +19,6 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver PrecompileHandler ph; EpochManager_WithMocks public epochManager; EpochManagerEnablerMock epochManagerEnabler; - MockAccounts accountss; IAccounts accountsContract; IEpochManagerEnablerMock epochManagerEnablerMockInterface; @@ -51,12 +49,7 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); accountsContract = IAccounts(accountsAddress); - // accountss = new MockAccounts(); - - // registry.setAddressFor(AccountsContract, address(accountss)); - // console.log("### Accounts address: ", address(accountss)); registry.setAddressFor(AccountsContract, accountsAddress); - console.log("### Accounts address: ", accountsAddress); } function setupEpochManager() public { @@ -137,19 +130,16 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver vm.prank(_elected[0]); accountsContract.createAccount(); - // accountss.setValidatorSigner(_elected[0], _elected[0]); epochManagerEnablerMockInterface.addValidator(_elected[0]); vm.prank(_elected[1]); accountsContract.createAccount(); - // accountss.setValidatorSigner(_elected[1], _elected[1]); epochManagerEnablerMockInterface.addValidator(_elected[1]); for (uint256 i = 2; i < numberValidators; i++) { address _currentValidator = vm.addr(i + 1); vm.prank(_currentValidator); accountsContract.createAccount(); - // accountss.setValidatorSigner(_currentValidator, _currentValidator); epochManagerEnablerMockInterface.addValidator(_currentValidator); } @@ -164,7 +154,6 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver whenL2(); epochManagerEnabler.initEpochManager(); - console.log("### Done initEpochManager"); } function whenL2WithoutEpochManagerInitialization() internal { diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 81bc7f14529..a505d38a900 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -137,8 +137,7 @@ contract EpochManagerTest is TestWithUtils08 { validators.setEpochRewards(validator2, validator2Reward); } - function initializeEpochManagerSystem() public { - // TODO(soloseng): rename function + function setupAndElectValidators() public { validators.setValidatorGroup(group); validators.setValidator(validator1); @@ -226,6 +225,8 @@ contract EpochManagerTest_initialize is EpochManagerTest { } } +// XXX: No explicit L2 test for this function, as the L1 check happens on the EpochManagerEnabler contract +// This test spoofs the EpochManagerEnabler to execute the function. contract EpochManagerTest_initializeSystem is EpochManagerTest { uint256 lastKnownEpochNumber; uint256 lastKnownFirstBlockOfEpoch; @@ -288,14 +289,6 @@ contract EpochManagerTest_initializeSystem is EpochManagerTest { ); } } -// contract EpochManagerTest_initializeSystem_L2 is -// EpochManagerTest_L2_NoInit, -// EpochManagerTest_initializeSystem -// { -// function setUp() public override(EpochManagerTest, EpochManagerTest_L2_NoInit) { -// super.setUp(); -// } -// } // XXX(soloseng):is this needed, since it would be calling epochmanagerEnabler. contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { function test_Reverts_onL1() public { @@ -303,12 +296,14 @@ contract EpochManagerTest_startNextEpochProcess is EpochManagerTest { epochManagerContract.startNextEpochProcess(); } } + contract EpochManagerTest_startNextEpochProcess_L2_NoInit is EpochManagerTest_L2_NoInit { function test_Reverts_whenSystemNotInitialized() public { vm.expectRevert("Epoch system not initialized"); epochManagerContract.startNextEpochProcess(); } } + contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { function test_Reverts_WhenEndOfEpochHasNotBeenReached() public { vm.expectRevert("Epoch is not ready to start"); @@ -316,7 +311,7 @@ contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { } function test_Reverts_WhenEpochProcessingAlreadyStarted() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); vm.expectRevert("Epoch process is already started"); @@ -324,7 +319,7 @@ contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { } function test_Emits_EpochProcessingStartedEvent() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectEmit(true, true, true, true); emit EpochProcessingStarted(firstEpochNumber); @@ -332,7 +327,7 @@ contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { } function test_SetsTheEpochRewardBlock() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); (, , , uint256 _currentRewardsBlock) = epochManagerContract.getCurrentEpoch(); @@ -340,7 +335,7 @@ contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { } function test_SetsTheEpochRewardAmounts() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); ( @@ -358,14 +353,14 @@ contract EpochManagerTest_startNextEpochProcess_L2 is EpochManagerTest_L2 { } function test_ShouldMintTotalValidatorStableRewardsToEpochManager() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); assertEq(validators.mintedStable(), validator1Reward + validator2Reward); } function test_ShouldReleaseCorrectAmountToReserve() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); uint256 reserveBalanceAfter = celoToken.balanceOf(reserveAddress); assertEq( @@ -379,13 +374,13 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { uint256 newEpochDuration = 5 * DAY; function test_setsNewEpochDuration() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.setEpochDuration(newEpochDuration); assertEq(epochManagerContract.epochDuration(), newEpochDuration); } function test_Emits_EpochDurationSetEvent() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectEmit(true, true, true, true); emit EpochDurationSet(newEpochDuration); @@ -393,7 +388,7 @@ contract EpochManagerTest_setEpochDuration is EpochManagerTest { } function test_Reverts_WhenNewEpochDurationIsZero() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectRevert("New epoch duration must be greater than zero."); epochManagerContract.setEpochDuration(0); @@ -409,7 +404,7 @@ contract EpochManagerTest_setEpochDuration_L2 is } function test_Reverts_WhenIsOnEpochProcess() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); vm.expectRevert("Cannot change epoch duration during processing."); epochManagerContract.setEpochDuration(newEpochDuration); @@ -420,13 +415,13 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { address newOracleAddress = actor("newOarcle"); function test_setsNewOracleAddress() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.setOracleAddress(newOracleAddress); assertEq(epochManagerContract.oracleAddress(), newOracleAddress); } function test_Emits_OracleAddressSetEvent() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectEmit(true, true, true, true); emit OracleAddressSet(newOracleAddress); @@ -434,21 +429,21 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { } // function test_Reverts_WhenIsOnEpochProcess() public { - // initializeEpochManagerSystem(); + // setupAndElectValidators(); // epochManagerContract.startNextEpochProcess(); // vm.expectRevert("Cannot change oracle address during epoch processing."); // epochManagerContract.setOracleAddress(newOracleAddress); // } function test_Reverts_WhenNewOracleAddressIsZero() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectRevert("Cannot set address zero as the Oracle."); epochManagerContract.setOracleAddress(address(0)); } function test_Reverts_WhenNewOracleAddressIsunchanged() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); vm.expectRevert("Oracle address cannot be the same."); epochManagerContract.setOracleAddress(address(sortedOracles)); @@ -465,13 +460,13 @@ contract EpochManagerTest_setOracleAddress_L2 is // address newOracleAddress = actor("newOarcle"); // function test_setsNewOracleAddress() public { - // initializeEpochManagerSystem(); + // setupAndElectValidators(); // epochManagerContract.setOracleAddress(newOracleAddress); // assertEq(epochManagerContract.oracleAddress(), newOracleAddress); // } // function test_Emits_OracleAddressSetEvent() public { - // initializeEpochManagerSystem(); + // setupAndElectValidators(); // vm.expectEmit(true, true, true, true); // emit OracleAddressSet(newOracleAddress); @@ -479,21 +474,21 @@ contract EpochManagerTest_setOracleAddress_L2 is // } function test_Reverts_WhenIsOnEpochProcess() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); epochManagerContract.startNextEpochProcess(); vm.expectRevert("Cannot change oracle address during epoch processing."); epochManagerContract.setOracleAddress(newOracleAddress); } // function test_Reverts_WhenNewOracleAddressIsZero() public { - // initializeEpochManagerSystem(); + // setupAndElectValidators(); // vm.expectRevert("Cannot set address zero as the Oracle."); // epochManagerContract.setOracleAddress(address(0)); // } // function test_Reverts_WhenNewOracleAddressIsunchanged() public { - // initializeEpochManagerSystem(); + // setupAndElectValidators(); // vm.expectRevert("Oracle address cannot be the same."); // epochManagerContract.setOracleAddress(address(sortedOracles)); @@ -693,7 +688,7 @@ contract EpochManagerTest_finishNextEpochProcess_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } @@ -801,7 +796,7 @@ contract EpochManagerTest_setToProcessGroups_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } @@ -882,7 +877,7 @@ contract EpochManagerTest_processGroup_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); election.setGroupEpochRewardsBasedOnScore(group, groupEpochRewards); } @@ -984,11 +979,12 @@ contract EpochManagerTest_getEpochByNumber is EpochManagerTest { epochManagerContract.getEpochByNumber(9); } } + contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_shouldReturnTheEpochInfoOfSpecifiedEpoch() public { uint256 numberOfEpochsToTravel = 9; @@ -1053,7 +1049,7 @@ contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { } function test_ReturnsZeroForFutureEpochs() public { - initializeEpochManagerSystem(); + setupAndElectValidators(); ( uint256 _firstBlock, uint256 _lastBlock, @@ -1074,6 +1070,7 @@ contract EpochManagerTest_getEpochNumberOfBlock is EpochManagerTest { epochManagerContract.getEpochNumberOfBlock(75); } } + contract EpochManagerTest_getEpochNumberOfBlock_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); @@ -1093,10 +1090,11 @@ contract EpochManagerTest_getEpochByBlockNumber is EpochManagerTest { epochManagerContract.getEpochNumberOfBlock(1000); } } + contract EpochManagerTest_getEpochByBlockNumber_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); _travelAndProcess_N_L2Epoch(2); } function test_ShouldRetreiveTheCorrectEpochInfoOfGivenBlock() public { @@ -1120,10 +1118,11 @@ contract EpochManagerTest_numberOfElectedInCurrentSet is EpochManagerTest { epochManagerContract.numberOfElectedInCurrentSet(); } } + contract EpochManagerTest_numberOfElectedInCurrentSet_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_ShouldRetreiveTheNumberOfElected() public { assertEq( @@ -1143,7 +1142,7 @@ contract EpochManagerTest_getElectedAccounts is EpochManagerTest { contract EpochManagerTest_getElectedAccounts_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_ShouldRetreiveThelistOfElectedAccounts() public { assertEq( @@ -1159,10 +1158,11 @@ contract EpochManagerTest_getElectedAccountByIndex is EpochManagerTest { epochManagerContract.getElectedAccountByIndex(0); } } + contract EpochManagerTest_getElectedAccountByIndex_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_ShouldRetreiveThecorrectValidator() public { assertEq(epochManagerContract.getElectedAccountByIndex(0), validator1); @@ -1175,10 +1175,11 @@ contract EpochManagerTest_getElectedSigners is EpochManagerTest { epochManagerContract.getElectedSigners(); } } + contract EpochManagerTest_getElectedSigners_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_ShouldRetreiveTheElectedSigners() public { @@ -1197,10 +1198,11 @@ contract EpochManagerTest_getElectedSignerByIndex is EpochManagerTest { epochManagerContract.getElectedSignerByIndex(1); } } + contract EpochManagerTest_getElectedSignerByIndex_L2 is EpochManagerTest_L2 { function setUp() public override(EpochManagerTest_L2) { super.setUp(); - initializeEpochManagerSystem(); + setupAndElectValidators(); } function test_ShouldRetreiveThecorrectElectedSigner() public { address[] memory knownElectedAccounts = epochManagerEnabler.getlastKnownElectedAccounts(); From c2af92f03ac69af931f6d39ee0b9df163328f339 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:48:22 -0500 Subject: [PATCH 22/23] more clean up --- .../protocol/test-sol/TestWithUtils08.sol | 2 - .../test-sol/unit/common/EpochManager.t.sol | 57 +------------------ 2 files changed, 1 insertion(+), 58 deletions(-) diff --git a/packages/protocol/test-sol/TestWithUtils08.sol b/packages/protocol/test-sol/TestWithUtils08.sol index fce5cf1fe3f..d409a983906 100644 --- a/packages/protocol/test-sol/TestWithUtils08.sol +++ b/packages/protocol/test-sol/TestWithUtils08.sol @@ -12,7 +12,6 @@ import { IAccounts } from "@celo-contracts/common/interfaces/IAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/IsL2Check.sol"; import "@celo-contracts-8/common/PrecompilesOverrideV2.sol"; -import { console } from "forge-std-8/console.sol"; contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOverrideV2 { IRegistry registry; @@ -48,7 +47,6 @@ contract TestWithUtils08 is ForgeTest, TestConstants, IsL2Check, PrecompilesOver accountsAddress = actor("accountsAddress"); deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); accountsContract = IAccounts(accountsAddress); - registry.setAddressFor(AccountsContract, accountsAddress); } diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index a505d38a900..7bea65caedd 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -7,7 +7,6 @@ import { MockElection } from "@celo-contracts/governance/test/MockElection.sol"; import "@celo-contracts/stability/test/MockSortedOracles.sol"; import "@celo-contracts-8/common/ScoreManager.sol"; -// import { MockAccounts } from "@celo-contracts-8/common/mocks/MockAccounts.sol"; import "@celo-contracts-8/common/mocks/EpochManager_WithMocks.sol"; import "@celo-contracts-8/common/test/MockCeloToken.sol"; import { MockCeloUnreleasedTreasury } from "@celo-contracts-8/common/test/MockCeloUnreleasedTreasury.sol"; @@ -26,7 +25,6 @@ contract EpochManagerTest is TestWithUtils08 { MockStableToken08 stableToken; EpochRewardsMock08 epochRewards; MockElection election; - // MockAccounts accountss; IMockValidators validators; address carbonOffsettingPartner; @@ -35,8 +33,6 @@ contract EpochManagerTest is TestWithUtils08 { address scoreManagerAddress; uint256 firstEpochNumber = 3; - - // uint256 firstEpochBlock = 100; uint256 epochDuration = DAY; address[] firstElected; @@ -84,7 +80,6 @@ contract EpochManagerTest is TestWithUtils08 { celoToken = new MockCeloToken08(); celoUnreleasedTreasury = new MockCeloUnreleasedTreasury(); election = new MockElection(); - // accountss = new MockAccounts(); validator1 = actor("validator"); validator2 = actor("otherValidator"); @@ -93,7 +88,6 @@ contract EpochManagerTest is TestWithUtils08 { firstElected.push(validator2); scoreManagerAddress = actor("scoreManagerAddress"); - // accountsAddress = actor("accountsAddress"); reserveAddress = actor("reserve"); @@ -102,7 +96,6 @@ contract EpochManagerTest is TestWithUtils08 { deployCodeTo("MockRegistry.sol", abi.encode(false), REGISTRY_ADDRESS); deployCodeTo("ScoreManager.sol", abi.encode(false), scoreManagerAddress); - // deployCodeTo("Accounts.sol", abi.encode(false), accountsAddress); deployCodeTo("MockValidators.sol", abi.encode(false), address(validators)); scoreManager = ScoreManager(scoreManagerAddress); @@ -118,7 +111,6 @@ contract EpochManagerTest is TestWithUtils08 { registry.setAddressFor(CeloTokenContract, address(celoToken)); registry.setAddressFor(ReserveContract, reserveAddress); registry.setAddressFor(ElectionContract, address(election)); - // registry.setAddressFor(AccountsContract, address(accountss)); // XXX this overrides the accountss address from utils celoToken.setTotalSupply(CELO_SUPPLY_CAP); vm.deal(address(celoUnreleasedTreasury), L2_INITIAL_STASH_BALANCE); @@ -140,9 +132,7 @@ contract EpochManagerTest is TestWithUtils08 { function setupAndElectValidators() public { validators.setValidatorGroup(group); validators.setValidator(validator1); - validators.setValidator(validator2); - validators.setValidatorGroup(group2); validators.setValidator(validator3); validators.setValidator(validator4); @@ -206,6 +196,7 @@ contract EpochManagerTest_L2_NoInit is EpochManagerTest, WhenL2NoInitialization super.setUp(); } } + contract EpochManagerTest_L2 is EpochManagerTest, WhenL2 { function setUp() public virtual override(EpochManagerTest, WhenL2) { super.setUp(); @@ -428,13 +419,6 @@ contract EpochManagerTest_setOracleAddress is EpochManagerTest { epochManagerContract.setOracleAddress(newOracleAddress); } - // function test_Reverts_WhenIsOnEpochProcess() public { - // setupAndElectValidators(); - // epochManagerContract.startNextEpochProcess(); - // vm.expectRevert("Cannot change oracle address during epoch processing."); - // epochManagerContract.setOracleAddress(newOracleAddress); - // } - function test_Reverts_WhenNewOracleAddressIsZero() public { setupAndElectValidators(); @@ -457,21 +441,6 @@ contract EpochManagerTest_setOracleAddress_L2 is function setUp() public override(EpochManagerTest_L2, EpochManagerTest) { super.setUp(); } - // address newOracleAddress = actor("newOarcle"); - - // function test_setsNewOracleAddress() public { - // setupAndElectValidators(); - // epochManagerContract.setOracleAddress(newOracleAddress); - // assertEq(epochManagerContract.oracleAddress(), newOracleAddress); - // } - - // function test_Emits_OracleAddressSetEvent() public { - // setupAndElectValidators(); - - // vm.expectEmit(true, true, true, true); - // emit OracleAddressSet(newOracleAddress); - // epochManagerContract.setOracleAddress(newOracleAddress); - // } function test_Reverts_WhenIsOnEpochProcess() public { setupAndElectValidators(); @@ -479,25 +448,9 @@ contract EpochManagerTest_setOracleAddress_L2 is vm.expectRevert("Cannot change oracle address during epoch processing."); epochManagerContract.setOracleAddress(newOracleAddress); } - - // function test_Reverts_WhenNewOracleAddressIsZero() public { - // setupAndElectValidators(); - - // vm.expectRevert("Cannot set address zero as the Oracle."); - // epochManagerContract.setOracleAddress(address(0)); - // } - - // function test_Reverts_WhenNewOracleAddressIsunchanged() public { - // setupAndElectValidators(); - - // vm.expectRevert("Oracle address cannot be the same."); - // epochManagerContract.setOracleAddress(address(sortedOracles)); - // } } contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { - // address signer1 = actor("signer1"); - // address signer2 = actor("signer2"); address beneficiary = actor("beneficiary"); uint256 paymentAmount = 4 ether; @@ -538,8 +491,6 @@ contract EpochManagerTest_sendValidatorPayment is EpochManagerTest { } contract EpochManagerTest_sendValidatorPayment_L2 is EpochManagerTest_L2 { - // address signer1 = actor("signer1"); - // address signer2 = actor("signer2"); address beneficiary = actor("beneficiary"); uint256 paymentAmount = 4 ether; @@ -593,7 +544,6 @@ contract EpochManagerTest_sendValidatorPayment_L2 is EpochManagerTest_L2 { } function test_sendsCUsdFromEpochManagerToValidatorAndBeneficiary() public { - // accountss.setPaymentDelegationFor(validator1, beneficiary, twentyFivePercent); vm.prank(validator1); accountsContract.setPaymentDelegation(beneficiary, twentyFivePercent); @@ -861,10 +811,6 @@ contract EpochManagerTest_setToProcessGroups_L2 is EpochManagerTest_L2 { } contract EpochManagerTest_processGroup is EpochManagerTest { - // function setUp() public override { - // super.setUp(); - // } - function test_Reverts_onL1() public { vm.expectRevert("Indivudual epoch process is not started"); epochManagerContract.processGroup(group, address(0), address(0)); @@ -1018,7 +964,6 @@ contract EpochManagerTest_getEpochByNumber_L2 is EpochManagerTest_L2 { function test_ReturnsHistoricalEpochInfoAfter_N_Epochs() public { uint256 _startingEpochNumber = epochManagerContract.getCurrentEpochNumber(); - console.log("current block", block.number); uint256 numberOfEpochsToTravel = 7; (uint256 _startingEpochFirstBlock, , uint256 _startingStartTimestamp, ) = epochManagerContract .getCurrentEpoch(); From d5d11783fcf089a127971b131d60a6127828a484 Mon Sep 17 00:00:00 2001 From: soloseng <102702451+soloseng@users.noreply.github.com> Date: Tue, 21 Jan 2025 13:32:10 -0500 Subject: [PATCH 23/23] PR feedback --- packages/protocol/test-sol/unit/common/EpochManager.t.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/test-sol/unit/common/EpochManager.t.sol b/packages/protocol/test-sol/unit/common/EpochManager.t.sol index 7bea65caedd..a684542f601 100644 --- a/packages/protocol/test-sol/unit/common/EpochManager.t.sol +++ b/packages/protocol/test-sol/unit/common/EpochManager.t.sol @@ -16,7 +16,7 @@ import "@celo-contracts-8/stability/test/MockStableToken.sol"; import { TestWithUtils08 } from "@test-sol/TestWithUtils08.sol"; import { ValidatorsMock } from "@test-sol/unit/governance/validators/mocks/ValidatorsMock.sol"; -import "@test-sol/utils/WhenL2-08.sol"; +import { WhenL2, WhenL2NoInitialization } from "@test-sol/utils/WhenL2-08.sol"; contract EpochManagerTest is TestWithUtils08 { EpochManager_WithMocks epochManagerContract;