Skip to content

Commit

Permalink
fix(Profile); rename check to require
Browse files Browse the repository at this point in the history
  • Loading branch information
nxqbao committed Nov 8, 2023
1 parent 43eda58 commit 61e439d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
11 changes: 5 additions & 6 deletions contracts/ronin/profile/Profile.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
function requestChangeAdminAddress(address id, address newAdminAddr) external {
CandidateProfile storage _profile = _getId2ProfileHelper(id);
_requireCandidateAdmin(_profile);
_checkNonZeroAndNonDuplicated(RoleAccess.ADMIN, newAdminAddr);
_requireNonZeroAndNonDuplicated(RoleAccess.ADMIN, newAdminAddr);
_setAdmin(_profile, newAdminAddr);

IStaking stakingContract = IStaking(getContract(ContractType.STAKING));
Expand Down Expand Up @@ -129,16 +129,15 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
* + [x] Handling slash fast finality
* + [x] Handling slash double sign
* - Update in Proposal contract for:
* + [ ] Refund of emergency exit mapping
* + [ ] ...
* + [-] Preserve the consensus address and recipient target of locked amount of emergency exit
* - Update Trusted Org contracts:
* + [x] Remove and delete weight of the old consensus
* + [x] Replace and add weight for the new consensus
*/
function requestChangeConsensusAddr(address id, TConsensus newConsensusAddr) external {
CandidateProfile storage _profile = _getId2ProfileHelper(id);
_requireCandidateAdmin(_profile);
_checkNonZeroAndNonDuplicated(RoleAccess.CONSENSUS, TConsensus.unwrap(newConsensusAddr));
_requireNonZeroAndNonDuplicated(RoleAccess.CONSENSUS, TConsensus.unwrap(newConsensusAddr));

TConsensus oldConsensusAddr = _profile.consensus;
_setConsensus(_profile, newConsensusAddr);
Expand Down Expand Up @@ -171,7 +170,7 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
function requestChangeTreasuryAddr(address id, address payable newTreasury) external {
CandidateProfile storage _profile = _getId2ProfileHelper(id);
_requireCandidateAdmin(_profile);
_checkNonZeroAndNonDuplicated(RoleAccess.TREASURY, newTreasury);
_requireNonZeroAndNonDuplicated(RoleAccess.TREASURY, newTreasury);
_setTreasury(_profile, newTreasury);

IRoninValidatorSet validatorContract = IRoninValidatorSet(getContract(ContractType.VALIDATOR));
Expand All @@ -186,7 +185,7 @@ contract Profile is IProfile, ProfileXComponents, Initializable {
function changePubkey(address id, bytes memory pubkey) external {
CandidateProfile storage _profile = _getId2ProfileHelper(id);
_requireCandidateAdmin(_profile);
_checkNonDuplicatedPubkey(pubkey);
_requireNonDuplicatedPubkey(pubkey);
_setPubkey(_profile, pubkey);

emit PubkeyChanged(id, pubkey);
Expand Down
34 changes: 21 additions & 13 deletions contracts/ronin/profile/ProfileHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,36 @@ abstract contract ProfileHandler is ProfileStorage {
/**
* @dev Checks each element in the new profile and reverts if there is duplication with any existing profile.
*/
function _checkDuplicatedInRegistry(CandidateProfile memory profile) internal view {
_checkNonZeroAndNonDuplicated(RoleAccess.CONSENSUS, TConsensus.unwrap(profile.consensus));
_checkNonZeroAndNonDuplicated(RoleAccess.CANDIDATE_ADMIN, profile.admin);
_checkNonZeroAndNonDuplicated(RoleAccess.TREASURY, profile.treasury);
_checkNonDuplicated(RoleAccess.TREASURY, profile.__reservedGovernor);
_checkNonDuplicatedPubkey(profile.pubkey);
function _requireNonDuplicatedInRegistry(CandidateProfile memory profile) internal view {
_requireNonZeroAndNonDuplicated(RoleAccess.CONSENSUS, TConsensus.unwrap(profile.consensus));
_requireNonZeroAndNonDuplicated(RoleAccess.CANDIDATE_ADMIN, profile.admin);
_requireNonZeroAndNonDuplicated(RoleAccess.TREASURY, profile.treasury);
_requireNonDuplicated(RoleAccess.TREASURY, profile.__reservedGovernor);
_requireNonDuplicatedPubkey(profile.pubkey);
}

function _checkNonZeroAndNonDuplicated(RoleAccess addressType, address addr) internal view {
function _requireNonZeroAndNonDuplicated(RoleAccess addressType, address addr) internal view {
if (addr == address(0)) revert ErrZeroAddress(addressType);
_checkNonDuplicated(addressType, addr);
_requireNonDuplicated(addressType, addr);
}

function _checkNonDuplicated(RoleAccess addressType, address addr) internal view {
if (_registry[uint256(uint160(addr))]) {
function _requireNonDuplicated(RoleAccess addressType, address addr) internal view {
if (_checkNonDuplicatedAddr(addr)) {
revert ErrDuplicatedInfo(addressType, uint256(uint160(addr)));
}
}

function _checkNonDuplicatedPubkey(bytes memory pubkey) internal view {
if (_registry[_hashPubkey(pubkey)]) {
function _checkNonDuplicatedAddr(address addr) internal view returns (bool) {
return _registry[uint256(uint160(addr))];
}

function _requireNonDuplicatedPubkey(bytes memory pubkey) internal view {
if (_checkNonDuplicatedPubkey(pubkey)) {
revert ErrDuplicatedPubkey(pubkey);
}
}
}

function _checkNonDuplicatedPubkey(bytes memory pubkey) internal view returns (bool) {
return _registry[_hashPubkey(pubkey)];
}
}
2 changes: 1 addition & 1 deletion contracts/ronin/profile/ProfileXComponents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract contract ProfileXComponents is IProfile, ProfileHandler {
__reservedGovernor: address(0),
pubkey: pubkey
});
_checkDuplicatedInRegistry(profile);
_requireNonDuplicatedInRegistry(profile);
_addNewProfile(_profile, profile);
}
}

0 comments on commit 61e439d

Please sign in to comment.