Skip to content

Commit

Permalink
For some reason the second migration fails trying to use the registry
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvol committed Feb 5, 2025
1 parent 1ce1809 commit 2a07f65
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 45 deletions.
38 changes: 30 additions & 8 deletions packages/protocol/contracts-0.8/common/EpochManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "../../contracts/common/Initializable.sol";
import "../../contracts/common/interfaces/IEpochManager.sol";
import "../../contracts/common/interfaces/ICeloVersionedContract.sol";
import "./interfaces/IEpochManagerInitializer.sol";
import "forge-std/console.sol";

/**
* @title Contract used for managing CELO L2 epoch and elections.
Expand Down Expand Up @@ -140,7 +141,8 @@ contract EpochManager is
* @notice Throws if called when EpochManager system has not yet been initalized.
*/
modifier onlySystemAlreadyInitialized() {
require(systemAlreadyInitialized(), "Epoch system not initialized");
console.log("onlySystemAlreadyInitialized", _systemAlreadyInitialized());
require(_systemAlreadyInitialized(), "Epoch system not initialized");
_;
}

Expand Down Expand Up @@ -170,15 +172,14 @@ contract EpochManager is
function initializeSystem(
uint256 firstEpochNumber,
uint256 firstEpochBlock,
address[] memory firstElected
) external // onlyEpochManagerEnabler // TODO fixme
{
address[] memory firstElected // onlyEpochManagerEnabler // TODO fixme
) external {
require(
getCeloToken().balanceOf(registry.getAddressForOrDie(CELO_UNRELEASED_TREASURY_REGISTRY_ID)) >
0,
"CeloUnreleasedTreasury not yet funded."
);
require(!systemAlreadyInitialized(), "Epoch system already initialized");
require(!_systemAlreadyInitialized(), "Epoch system already initialized");
require(firstEpochNumber > 0, "First epoch number must be greater than 0");
require(firstEpochBlock > 0, "First epoch block must be greater than 0");
require(
Expand Down Expand Up @@ -558,10 +559,19 @@ contract EpochManager is
function getEpochNumberOfBlock(
uint256 _blockNumber
) external view onlySystemAlreadyInitialized returns (uint256) {
console.log("EpochManager.getEpochNumberOfBlock1");
return 1;
(uint256 _epochNumber, , , , ) = _getEpochByBlockNumber(_blockNumber);
console.log("EpochManager.getEpochNumberOfBlock2");
return _epochNumber;
}

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

/**
* @notice Returns the epoch info of a specified blockNumber.
* @param _blockNumber Block number of the epoch info is retreived.
Expand Down Expand Up @@ -650,7 +660,11 @@ contract EpochManager is
/**
* @return Whether or not the EpochManager contract has been activated to start processing epochs.
*/
function systemAlreadyInitialized() public view returns (bool) {
function systemAlreadyInitialized() external view returns (bool) {
return _systemAlreadyInitialized();
}

function _systemAlreadyInitialized() internal view returns (bool) {
return initialized && isSystemInitialized;
}

Expand Down Expand Up @@ -777,13 +791,19 @@ contract EpochManager is
onlySystemAlreadyInitialized
returns (uint256, uint256, uint256, uint256, uint256)
{
console.log("EpochManager1");
console.log("block.numbe", block.number);
console.log("_blockNumber", _blockNumber);
require(_blockNumber <= block.number, "Invalid blockNumber. Value too high.");

console.log("EpochManager2");
(uint256 _firstBlockOfFirstEpoch, , , ) = getEpochByNumber(firstKnownEpoch);
console.log("_firstBlockOfFirstEpoch", _firstBlockOfFirstEpoch);

console.log("EpochManager4");
require(_blockNumber >= _firstBlockOfFirstEpoch, "Invalid blockNumber. Value too low.");

console.log("EpochManager5");
uint256 _firstBlockOfCurrentEpoch = epochs[currentEpochNumber].firstBlock;
console.log("EpochManager6");

if (_blockNumber >= _firstBlockOfCurrentEpoch) {
(
Expand All @@ -792,9 +812,11 @@ contract EpochManager is
uint256 _startTimestamp,
uint256 _rewardsBlock
) = getEpochByNumber(currentEpochNumber);
console.log("EpochManager7");
return (currentEpochNumber, _firstBlock, _lastBlock, _startTimestamp, _rewardsBlock);
}

console.log("EpochManager8");
uint256 left = firstKnownEpoch;
uint256 right = currentEpochNumber - 1;

Expand Down
1 change: 1 addition & 0 deletions packages/protocol/contracts-0.8/common/IsL2Check.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ contract IsL2Check {
* @return Whether or not the current network is a Celo L2.
*/
function isL2() internal view returns (bool) {
return true;
uint32 size;
address _addr = proxyAdminAddress;
assembly {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "../../contracts-0.8/common/IsL2Check.sol";

import "./UsingPrecompiles.sol";
import "./UsingRegistry.sol";
import "forge-std/console.sol";

/**
* @title PrecompilesOverride Contract
Expand All @@ -19,10 +20,16 @@ contract PrecompilesOverride is UsingPrecompiles, UsingRegistry {
* @return Epoch number.
*/
function getEpochNumberOfBlock(uint256 blockNumber) public view override returns (uint256) {
console.log("PrecompilesOverride.getEpochNumberOfBlock0");
if (isL2()) {
getEpochManager().isBlocked();
console.log("PrecompilesOverride.getEpochNumberOfBlock1");
return getEpochManager().getEpochNumberOfBlock(blockNumber);
console.log("getEpochNumberOfBlock1_");
} else {
console.log("PrecompilesOverride.getEpochNumberOfBlock2");
return epochNumberOfBlock(blockNumber, getEpochSize());
console.log("getEpochNumberOfBlock2_");
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/protocol/contracts-0.8/governance/Validators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ contract Validators is
function registerValidatorNoBls(
bytes calldata ecdsaPublicKey
) external nonReentrant onlyL2 returns (bool) {
console.log(
"registerValidatorNoBls Can call epoch manager from here?",
getEpochManager().isBlocked()
);
address account = getAccounts().validatorSignerToAccount(msg.sender);
_isRegistrationAllowed(account);
require(!isValidator(account) && !isValidatorGroup(account), "Already registered");
Expand Down Expand Up @@ -1456,37 +1460,49 @@ contract Validators is
*/
function updateMembershipHistory(address account, address group) private returns (bool) {
MembershipHistory storage history = validators[account].membershipHistory;
console.log("updateMembershipHistory0");
uint256 epochNumber = getEpochNumber();
console.log("updateMembershipHistory1");

uint256 head = history.numEntries == 0 ? 0 : history.tail.add(history.numEntries.sub(1));

if (history.numEntries > 0 && group == address(0)) {
history.lastRemovedFromGroupTimestamp = block.timestamp;
}
console.log("updateMembershipHistory2");

if (history.numEntries > 0 && history.entries[head].epochNumber == epochNumber) {
// There have been no elections since the validator last changed membership, overwrite the
// previous entry.
console.log("updateMembershipHistory3");
history.entries[head] = MembershipHistoryEntry(epochNumber, group);
return true;
}

console.log("updateMembershipHistory4");
// There have been elections since the validator last changed membership, create a new entry.
uint256 index = history.numEntries == 0 ? 0 : head.add(1);
history.entries[index] = MembershipHistoryEntry(epochNumber, group);
console.log("updateMembershipHistory5");
if (history.numEntries < membershipHistoryLength) {
// Not enough entries, don't remove any.
console.log("updateMembershipHistory6");
history.numEntries = history.numEntries.add(1);
console.log("updateMembershipHistory7");
} else if (history.numEntries == membershipHistoryLength) {
console.log("updateMembershipHistory8");
// Exactly enough entries, delete the oldest one to account for the one we added.
delete history.entries[history.tail];
history.tail = history.tail.add(1);
console.log("updateMembershipHistory9");
} else {
console.log("updateMembershipHistory10");
// Too many entries, delete the oldest two to account for the one we added.
delete history.entries[history.tail];
delete history.entries[history.tail.add(1)];
history.numEntries = history.numEntries.sub(1);
history.tail = history.tail.add(2);
console.log("updateMembershipHistory11");
}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/contracts/common/interfaces/IProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity >=0.5.13 <0.9.0;

interface IProxy {
// function() external payable;
function _setImplementation(address implementation) external;
function _setAndInitializeImplementation(
address implementation,
bytes calldata callbackData
Expand Down
102 changes: 79 additions & 23 deletions packages/protocol/migrations_sol/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
proxy._setAndInitializeImplementation(implementation, initializeCalldata);
}

function setImplementationOnProxyNotInitialize(IProxy proxy, string memory contractName) public {
bytes memory implementationBytecode = vm.getCode(
string.concat("out/", contractName, ".sol/", contractName, ".json")
);
bool testingDeployment = false;
bytes memory initialCode = abi.encodePacked(
implementationBytecode,
abi.encode(testingDeployment)
);
address implementation = create2deploy(bytes32(proxyNonce), initialCode);
// nonce to avoid having the same address to deploy to, likely won't needed but just in case
proxyNonce++;
console.log(" Implementation deployed to:", address(implementation));
console.log(" Calling initialize(..)");
proxy._setImplementation(implementation);
}

function deployProxiedContract(
string memory contractName,
address toProxy,
Expand Down Expand Up @@ -267,10 +284,13 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
);
string memory json = vm.readFile("./migrations_sol/migrationsConfig.json");

setupUsingRegistry();

vm.startBroadcast(DEPLOYER_ACCOUNT);

setupUsingRegistry();
console.log("Account owner:", IProxy(address(getAccounts()))._getOwner());
// upgradeValidators(); // TODO remove
// upgradeEpochManager(); // TODO remove
// console.log("Account owner:", IProxy(address(getAccounts()))._getOwner());

// Proxy for Registry is already set, just deploy implementation
migrateEpochManagerEnabler();
Expand All @@ -284,7 +304,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
migrateGovernance(json);
vm.stopBroadcast();

electValidators(json);
// electValidators(json);

// vm.startBroadcast(DEPLOYER_ACCOUNT);

Expand All @@ -293,6 +313,23 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
// vm.stopBroadcast();
}

// function run3() public {
// string memory json = vm.readFile("./migrations_sol/migrationsConfig.json");
// setupUsingRegistry();
// electValidators(json);

// }

function upgradeValidators() public {
IProxy proxy = IProxy(address(getValidators()));
setImplementationOnProxyNotInitialize(proxy, "Validators");
}

function upgradeEpochManager() public {
IProxy proxy = IProxy(address(getEpochManager()));
setImplementationOnProxyNotInitialize(proxy, "EpochManager");
}

/**
* The function calls defined here are required by the parent UsingRegistry.sol contract.
*/
Expand Down Expand Up @@ -716,7 +753,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
IProxy proxy = IProxy(proxyAddress);
console.log(" Proxy deployed to:", address(proxy));

address implementation = address(0x8464135c8F25Da09e49BC8782676a84730C318bC);
address implementation = address(0x8464135c8F25Da09e49BC8782676a84730C318bC); // FIXMEEE

proxy._setAndInitializeImplementation(
implementation,
Expand Down Expand Up @@ -1069,7 +1106,14 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
}
}

IEpochManager(getEpochManager()).initializeSystem(1, block.number, signers); // TODO fix signers
getEpochManager().initializeSystem(1, block.number - 1, signers); // TODO fix signers
console.log(
"systemAlreadyInitialized",
IEpochManager(getEpochManager()).systemAlreadyInitialized()
);
console.log("Epoch is #", getEpochManager().getEpochNumberOfBlock(1473));

console.log("Address of EpochManager is", address(getEpochManager()));
}

function migrateEpochManager(string memory json) public {
Expand Down Expand Up @@ -1255,8 +1299,9 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
);

(bytes memory ecdsaPubKey, , , ) = _generateEcdsaPubKeyWithSigner(accountAddress, validatorKey);
console.log("Can call epoch manager from here?", getEpochManager().isBlocked());
getValidators().registerValidatorNoBls(ecdsaPubKey);
getValidators().affiliate(groupToAffiliate);
// getValidators().affiliate(groupToAffiliate);
console.log("Done registering validators");

vm.stopBroadcast();
Expand Down Expand Up @@ -1353,6 +1398,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
json.parseRaw(".validators.validatorLockedGoldRequirements.value"),
(uint256)
);

// attestationKeys not migrated

if (valKeys.length == 0) {
Expand All @@ -1365,7 +1411,7 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
);
}

uint256 groupCount = 3; // TODO add to config
uint256 groupCount = 3; // TODO add to config // TODO I modified this

address[] memory groups = new address[](groupCount);

Expand All @@ -1386,8 +1432,9 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {

// TODO change name of variable amount of groups for amount in group
for (uint256 groupIndex = 0; groupIndex < groupCount; groupIndex++) {
console.log("groupIndex = 0; group");
address groupAddress = groups[groupIndex];
console.log("Registering members for group: ", groupAddress);
console.log("Registering members for group: ", groupIndex, groupAddress);
for (uint256 validatorIndex = 0; validatorIndex < maxGroupSize; validatorIndex++) {
uint256 validatorKeyIndex = getValidatorKeyIndex(
groupCount,
Expand All @@ -1396,27 +1443,36 @@ contract Migration is Script, UsingRegistry, MigrationsConstants {
maxGroupSize
);
console.log("Registering validator #: ", validatorIndex);

address validator = registerValidator(
validatorIndex,
valKeys[validatorKeyIndex],
validatorLockedGoldRequirements,
groupAddress
);
// TODO start broadcast
console.log("Adding to group...");

vm.startBroadcast(groups[groupIndex]);
address greater = groupIndex == 0 ? address(0) : groups[groupIndex - 1];

if (validatorIndex == 0) {
getValidators().addFirstMember(validator, address(0), greater);
console.log("Making group vote for itself");
} else {
getValidators().addMember(validator);
}
getElection().vote(groupAddress, validatorLockedGoldRequirements, address(0), greater);

vm.stopBroadcast();
// // TODO start broadcast
// console.log("Adding to group...");

// vm.startBroadcast(groups[groupIndex]);
// address greater = groupIndex == 0 ? address(0) : groups[groupIndex - 1];
// console.log("getParsedSignatureOfAddress1");

// if (validatorIndex == 0) {
// console.log("getParsedSignatureOfAddress2");
// getValidators().addFirstMember(validator, address(0), greater);
// console.log("Making group vote for itself");
// } else {
// console.log("getParsedSignatureOfAddress3");
// getValidators().addMember(validator);
// console.log("getParsedSignatureOfAddress4");
// }
// console.log("getParsedSignatureOfAddress5");
// console.log("Owner is:", getElection().owner());
// console.log("getParsedSignatureOfAddress5.2");
// getElection().vote(groupAddress, validatorLockedGoldRequirements, address(0), greater);
// console.log("getParsedSignatureOfAddress6");

// vm.stopBroadcast();
}
}
}
Expand Down
Loading

0 comments on commit 2a07f65

Please sign in to comment.