Skip to content

Commit

Permalink
feat(Profile,RoninValidatorSet): add draft implementation for zk-evm …
Browse files Browse the repository at this point in the history
…integration
  • Loading branch information
TuDo1403 committed Oct 25, 2024
1 parent f0cdb07 commit 9d49a13
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 33 deletions.
6 changes: 4 additions & 2 deletions src/libraries/LibArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ library LibArray {
*/
function addAndSum(
uint256[] memory arr1,
uint256[] memory arr2
uint256[] memory arr2,
uint256[] memory arr3
) internal pure returns (uint256[] memory res, uint256 total) {
uint256 length = arr1.length;
if (length != arr2.length) revert ErrLengthMismatch();
if (length != arr3.length) revert ErrLengthMismatch();

res = new uint256[](length);
for (uint256 i; i < length; ++i) {
res[i] = arr1[i] + arr2[i];
res[i] = arr1[i] + arr2[i] + arr3[i];
total += res[i];
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/mocks/validator/MockValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ contract MockValidatorSet is
address profileContract
) external { }

function initializeV5(
address zkFeePlazaContract
) external { }

function submitBlockReward() external payable override { }

function onL2BlockRewardSubmitted(
address cid
) external payable override { }

function getPeriodEndBlock(
uint256 period
) external view override returns (uint256) { }
Expand Down
92 changes: 85 additions & 7 deletions src/ronin/profile/Profile.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
_setCooldownConfig(cooldown);
}

function initializeV4(
address rollupManager
) external reinitializer(4) {
_setContract(ContractType.ZK_ROLLUP_MANAGER, rollupManager);
}

/**
* @dev Add addresses of renounced candidates into registry. Only called during {initializeV2}.
*/
Expand All @@ -71,6 +77,44 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
emit ProfileMigrated(id, candidateAdmin, treasury);
}

/**
* @inheritdoc IProfile
*/
function getId2RollupId(
address id
) public view returns (uint32) {
return _id2Profile[id].rollupId;
}

/**
* @inheritdoc IProfile
*/
function getRollupId2Id(
uint32 rollupId
) external view returns (address) {
(bool found, address id) = _tryGetRollupId2Id(rollupId);
if (!found) revert ErrLookUpIdFromRollupIdFailed(rollupId);
return id;
}

/**
* @inheritdoc IProfile
*/
function getId2Aggregator(
address id
) external view returns (address) {
return _id2Profile[id].aggregator;
}

/**
* @inheritdoc IProfile
*/
function getId2Sequencer(
address id
) external view returns (address) {
return _id2Profile[id].sequencer;
}

/**
* @inheritdoc IProfile
*/
Expand Down Expand Up @@ -253,6 +297,15 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
return _getVRFKeyHash2Id(vrfKeyHash);
}

/**
* @inheritdoc IProfile
*/
function tryGetRollupId2Id(
uint32 rollupId
) external view returns (bool found, address id) {
return _tryGetRollupId2Id(rollupId);
}

/**
* @inheritdoc IProfile
*/
Expand Down Expand Up @@ -313,6 +366,16 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
found = id != address(0);
}

/**
* @dev Try Look up the `id` by `rollupId`, return a boolean indicating whether the query success.
*/
function _tryGetRollupId2Id(
uint32 rollupId
) internal view returns (bool found, address id) {
id = _rollupId2Id[rollupId];
found = id != address(0);
}

/**
* @inheritdoc IProfile
*/
Expand Down Expand Up @@ -449,6 +512,28 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
_setVRFKeyHash(_profile, vrfKeyHash);
}

/**
* @inheritdoc IProfile
*/
function changeSequencerAddr(address id, address sequencer) external onlyAdmin {
CandidateProfile storage _profile = _getId2ProfileHelper(id);

_requireNonZeroAndNonDuplicated(RoleAccess.SEQUENCER, sequencer);
_requireRollupCreated(_profile);
_setSequencer(_profile, sequencer);
}

/**
* @inheritdoc IProfile
*/
function changeAggregatorAddr(address id, address aggregator) external onlyAdmin {
CandidateProfile storage _profile = _getId2ProfileHelper(id);

_requireNonZeroAndNonDuplicated(RoleAccess.AGGREGATOR, aggregator);
_requireRollupCreated(_profile);
_setAggregator(_profile, aggregator);
}

function _requireCandidateAdmin(
CandidateProfile storage sProfile
) internal view {
Expand All @@ -458,13 +543,6 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
) revert ErrUnauthorized(msg.sig, RoleAccess.CANDIDATE_ADMIN);
}

function _requireNotOnRenunciation(
address id
) internal view {
IRoninValidatorSet validatorContract = IRoninValidatorSet(getContract(ContractType.VALIDATOR));
if (validatorContract.getCandidateInfoById(id).revokingTimestamp > 0) revert ErrValidatorOnRenunciation(id);
}

/**
* @inheritdoc IProfile
*/
Expand Down
18 changes: 18 additions & 0 deletions src/ronin/profile/ProfileHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

pragma solidity ^0.8.9;

import { IRoninValidatorSet } from "../../interfaces/validator/IRoninValidatorSet.sol";
import { PCUVerifyBLSPublicKey } from "../../precompile-usages/PCUVerifyBLSPublicKey.sol";
import "../../udvts/Types.sol";
import { ContractType } from "../../utils/ContractType.sol";
import "../../utils/RoleAccess.sol";
import { ProfileStorage } from "./ProfileStorage.sol";

Expand All @@ -25,6 +27,13 @@ abstract contract ProfileHandler is PCUVerifyBLSPublicKey, ProfileStorage {
_requireNonDuplicatedPubkey(profile.pubkey);
}

function _requireNotOnRenunciation(
address id
) internal view {
IRoninValidatorSet validatorContract = IRoninValidatorSet(getContract(ContractType.VALIDATOR));
if (validatorContract.getCandidateInfoById(id).revokingTimestamp > 0) revert ErrValidatorOnRenunciation(id);
}

function _requireNonZeroAndNonDuplicated(RoleAccess addressType, address addr) internal view {
if (addr == address(0)) revert ErrZeroAddress(addressType);
_requireNonDuplicated(addressType, addr);
Expand Down Expand Up @@ -86,6 +95,15 @@ abstract contract ProfileHandler is PCUVerifyBLSPublicKey, ProfileStorage {
}
}

/**
* @dev Checks if the candidate has a rollup contract created and reverts if not.
*/
function _requireRollupCreated(
CandidateProfile storage _profile
) internal view {
if (_profile.rollupId == 0) revert ErrZeroRollupId(_profile.id);
}

function _requireCooldownPassedAndStartCooldown(
CandidateProfile storage _profile
) internal {
Expand Down
25 changes: 24 additions & 1 deletion src/ronin/profile/ProfileStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ abstract contract ProfileStorage is IProfile, HasContracts {
/// @dev Mapping from vrf key hash => id address.
mapping(bytes32 vrfKeyHash => address cid) internal _vrfKeyHash2Id;

/// @dev Mapping from rollup id => id address.
mapping(uint32 rollupId => address cid) internal _rollupId2Id;

/// @dev Upgradeable gap.
bytes32[46] __gap;
bytes32[45] __gap;

/**
* @dev Add a profile from memory to storage.
Expand Down Expand Up @@ -120,6 +123,26 @@ abstract contract ProfileStorage is IProfile, HasContracts {
emit VRFKeyHashChanged(_profile.id, vrfKeyHash);
}

/**
* @dev Change the aggregator address of the profile.
*/
function _setAggregator(CandidateProfile storage _profile, address aggregator) internal {
_profile.aggregator = aggregator;
_registry[uint256(uint160(address(aggregator)))] = true;

emit AggregatorChanged(_profile.id, aggregator);
}

/**
* @dev Change the sequencer address of the profile.
*/
function _setSequencer(CandidateProfile storage _profile, address sequencer) internal {
_profile.sequencer = sequencer;
_registry[uint256(uint160(address(sequencer)))] = true;

emit SequencerChanged(_profile.id, sequencer);
}

function _startCooldown(
CandidateProfile storage _profile
) internal {
Expand Down
28 changes: 27 additions & 1 deletion src/ronin/profile/ProfileXComponents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import "./ProfileHandler.sol";
pragma solidity ^0.8.9;

abstract contract ProfileXComponents is IProfile, ProfileHandler {
/**
* @inheritdoc IProfile
*/
function execCreateRollup(address id, uint32 rollupId) external onlyContract(ContractType.ZK_ROLLUP_MANAGER) {
CandidateProfile storage _profile = _getId2ProfileHelper(id);

_requireNotOnRenunciation(id);

if (_registry[rollupId]) revert ErrRollupIdAlreadyRegistered(rollupId);
if (rollupId == 0) revert ErrZeroRollupId(id);
if (_profile.id == address(0)) revert ErrNonExistentProfile();
if (_profile.rollupId != 0) revert ErrExistentRollup(id, _profile.rollupId);

_profile.rollupId = rollupId;
_registry[rollupId] = true;

// By default, the aggregator and sequencer are the same as the profile id.
_setAggregator(_profile, id);
_setSequencer(_profile, id);

emit RollupCreated(id, rollupId);
}

/**
* @inheritdoc IProfile
*/
Expand Down Expand Up @@ -36,7 +59,10 @@ abstract contract ProfileXComponents is IProfile, ProfileHandler {
oldConsensus: TConsensus.wrap(address(0)),
registeredAt: block.timestamp,
vrfKeyHash: 0x0,
vrfKeyHashLastChange: 0
vrfKeyHashLastChange: 0,
rollupId: 0,
aggregator: address(0),
sequencer: address(0)
});

_requireNonDuplicatedInRegistry(profile);
Expand Down
10 changes: 10 additions & 0 deletions src/ronin/validator/CandidateManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ abstract contract CandidateManager is
return _candidateIndex[cid] != 0;
}

/**
* @dev Checks whether the candidate has created a rollup before.
*/
function _isRollupOwner(
address cid
) internal view returns (bool) {
return IProfile(getContract(ContractType.PROFILE)).getId2RollupId(cid) != 0;
}

/**
* @inheritdoc ICandidateManager
*/
Expand Down Expand Up @@ -300,6 +309,7 @@ abstract contract CandidateManager is
function _setRevokingTimestamp(ValidatorCandidate storage _candidate, uint256 timestamp) internal {
address cid = __css2cid(_candidate.__shadowedConsensus);
if (!_isValidatorCandidateById(cid)) revert ErrNonExistentCandidate();
if (_isRollupOwner(cid)) revert ErrRollupOwnerCannotRenounce();
_candidate.revokingTimestamp = timestamp;
emit CandidateRevokingTimestampUpdated(cid, timestamp);
}
Expand Down
Loading

0 comments on commit 9d49a13

Please sign in to comment.