From e8ab66577c544c87484d9997e9f4ac72b20fb5fe Mon Sep 17 00:00:00 2001 From: seolaoh Date: Wed, 17 Jul 2024 17:39:13 +0900 Subject: [PATCH] feat(contracts): apply PR reviews --- .../contracts/contracts/L1/AssetManager.sol | 27 ++++++-- .../contracts/L1/ValidatorManager.sol | 26 ++------ .../contracts/L1/interfaces/IAssetManager.sol | 63 ++++++++++--------- .../L1/interfaces/IValidatorManager.sol | 43 +++++-------- 4 files changed, 78 insertions(+), 81 deletions(-) diff --git a/packages/contracts/contracts/L1/AssetManager.sol b/packages/contracts/contracts/L1/AssetManager.sol index b55bcc7fb..3b1593db0 100644 --- a/packages/contracts/contracts/L1/AssetManager.sol +++ b/packages/contracts/contracts/L1/AssetManager.sol @@ -215,6 +215,13 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager { return kghAssets + kroAssets; } + /** + * @inheritdoc IAssetManager + */ + function getWithdrawAccount(address validator) external view returns (address) { + return _vaults[validator].withdrawAccount; + } + /** * @inheritdoc IAssetManager */ @@ -304,14 +311,23 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager { } /** - * @notice Deposit KRO to register as a validator. - * This function is only called by the ValidatorManager contract. + * @notice Deposit KRO to register as a validator. This function is only called by the + * ValidatorManager contract. * - * @param validator Address of the validator. - * @param assets The amount of KRO to deposit. + * @param validator Address of the validator. + * @param assets The amount of KRO to deposit. + * @param withdrawAccount An account where assets can be withdrawn to. Only this account can + * withdraw the assets. */ - function depositToRegister(address validator, uint128 assets) external onlyValidatorManager { + function depositToRegister( + address validator, + uint128 assets, + address withdrawAccount + ) external onlyValidatorManager { if (assets == 0) revert NotAllowedZeroInput(); + if (withdrawAccount == address(0)) revert ZeroAddress(); + + _vaults[validator].withdrawAccount = withdrawAccount; _deposit(validator, assets, false); emit Deposited(validator, assets); } @@ -323,6 +339,7 @@ contract AssetManager is ISemver, IERC721Receiver, IAssetManager { if (assets == 0) revert NotAllowedZeroInput(); if (VALIDATOR_MANAGER.getStatus(msg.sender) == IValidatorManager.ValidatorStatus.NONE) revert ImproperValidatorStatus(); + _deposit(msg.sender, assets, true); emit Deposited(msg.sender, assets); } diff --git a/packages/contracts/contracts/L1/ValidatorManager.sol b/packages/contracts/contracts/L1/ValidatorManager.sol index ef10a0d8b..fb24778d7 100644 --- a/packages/contracts/contracts/L1/ValidatorManager.sol +++ b/packages/contracts/contracts/L1/ValidatorManager.sol @@ -182,14 +182,11 @@ contract ValidatorManager is ISemver, IValidatorManager { if (commissionRate > COMMISSION_RATE_DENOM) revert MaxCommissionRateExceeded(); - if (withdrawAccount == address(0)) revert ZeroAddress(); - Validator storage validatorInfo = _validatorInfo[msg.sender]; validatorInfo.isInitiated = true; validatorInfo.commissionRate = commissionRate; - validatorInfo.withdrawAccount = withdrawAccount; - ASSET_MANAGER.depositToRegister(msg.sender, assets); + ASSET_MANAGER.depositToRegister(msg.sender, assets, withdrawAccount); bool ready = assets >= MIN_ACTIVATE_AMOUNT; if (ready) { @@ -243,7 +240,7 @@ contract ValidatorManager is ISemver, IValidatorManager { if (newCommissionRate == oldCommissionRate) revert SameCommissionRate(); validatorInfo.pendingCommissionRate = newCommissionRate; - validatorInfo.commissionChangeInitTime = uint128(block.timestamp); + validatorInfo.commissionChangeInitiatedAt = uint128(block.timestamp); emit ValidatorCommissionChangeInitiated(msg.sender, oldCommissionRate, newCommissionRate); } @@ -264,7 +261,7 @@ contract ValidatorManager is ISemver, IValidatorManager { validatorInfo.commissionRate = newCommissionRate; validatorInfo.pendingCommissionRate = 0; - validatorInfo.commissionChangeInitTime = 0; + validatorInfo.commissionChangeInitiatedAt = 0; emit ValidatorCommissionChangeFinalized(msg.sender, oldCommissionRate, newCommissionRate); } @@ -348,18 +345,6 @@ contract ValidatorManager is ISemver, IValidatorManager { return _validatorInfo[validator].pendingCommissionRate; } - // TODO: move this to AssetManager - /** - * @notice Returns the address of withdraw account of given validator. - * - * @param validator Address of the validator. - * - * @return The address of withdraw account of given validator. - */ - function getWithdrawAccount(address validator) external view returns (address) { - return _validatorInfo[validator].withdrawAccount; - } - /** * @inheritdoc IValidatorManager */ @@ -470,8 +455,9 @@ contract ValidatorManager is ISemver, IValidatorManager { /** * @inheritdoc IValidatorManager */ - function canFinalizeCommissionChangeAt(address validator) public view returns (uint256) { - return _validatorInfo[validator].commissionChangeInitTime + COMMISSION_CHANGE_DELAY_SECONDS; + function canFinalizeCommissionChangeAt(address validator) public view returns (uint128) { + return + _validatorInfo[validator].commissionChangeInitiatedAt + COMMISSION_CHANGE_DELAY_SECONDS; } /** diff --git a/packages/contracts/contracts/L1/interfaces/IAssetManager.sol b/packages/contracts/contracts/L1/interfaces/IAssetManager.sol index 391fd078d..2c941a658 100644 --- a/packages/contracts/contracts/L1/interfaces/IAssetManager.sol +++ b/packages/contracts/contracts/L1/interfaces/IAssetManager.sol @@ -9,20 +9,20 @@ interface IAssetManager { /** * @notice Represents the asset information of the vault of a validator. * - * @custom:field validatorKro Total amount of KRO that deposited by the validator and - * accumulated as validator reward (including validatorKroReserved). - * @custom:field validatorKroReserved Total amount of validator KRO that reserved during output - * submission or challenge creation. - * @custom:field totalKro Total amount of KRO that delegated by the delegators and - * accumulated as KRO delegation reward (including totalKroInKgh). - * @custom:field totalKroShares Total shares for KRO delegation in the vault. - * @custom:field totalKgh Total number of KGH in the vault. - * @custom:field totalKroInKgh Total amount of KRO which KGHs in the vault have. - * @custom:field rewardPerKghStored Accumulated boosted reward per 1 KGH. + * @custom:field validatorKro Total amount of KRO that deposited by the validator and + * accumulated as validator reward (including validatorKroBonded). + * @custom:field validatorKroBonded Total amount of validator KRO that bonded during output + * submission or challenge creation. + * @custom:field totalKro Total amount of KRO that delegated by the delegators and + * accumulated as KRO delegation reward (including totalKroInKgh). + * @custom:field totalKroShares Total shares for KRO delegation in the vault. + * @custom:field totalKgh Total number of KGH in the vault. + * @custom:field totalKroInKgh Total amount of KRO which KGHs in the vault have. + * @custom:field rewardPerKghStored Accumulated boosted reward per 1 KGH. */ struct Asset { uint128 validatorKro; - uint128 validatorKroReserved; + uint128 validatorKroBonded; uint128 totalKro; uint128 totalKroShares; uint128 totalKgh; @@ -45,18 +45,17 @@ interface IAssetManager { /** * @notice Constructs the delegator of KGH in the vault of a validator. * - * @custom:field lastDelegatedAt Last timestamp when the delegator delegated. The delegator - * can undelegate after UNDELEGATION_DELAY_SECONDS elapsed. - * @custom:field rewardPerKghPaid Accumulated paid boosted reward per 1 KGH. - * @custom:field kghNum Total number of KGH delegated. - * @custom:field delegationHistory A mapping of tokenId to the delegation timestamp. - * @custom:field kroShares A mapping of tokenId to the amount of shares for KRO in KGH. + * @custom:field rewardPerKghPaid Accumulated paid boosted reward per 1 KGH. + * @custom:field kghNum Total number of KGH delegated. + * @custom:field delegatedAt A mapping of tokenId to the delegation timestamp. The + * delegator can undelegate after UNDELEGATION_DELAY_SECONDS + * elapsed from each delegation timestamp. + * @custom:field kroShares A mapping of tokenId to the amount of shares for KRO in KGH. */ struct KghDelegator { - uint128 lastDelegatedAt; uint128 rewardPerKghPaid; uint256 kghNum; - mapping(uint256 => uint128) delegationHistory; + mapping(uint256 => uint128) delegatedAt; mapping(uint256 => uint128) kroShares; } @@ -228,6 +227,11 @@ interface IAssetManager { */ error NotAllowedZeroInput(); + /** + * @notice Reverts when the address is zero address. + */ + error ZeroAddress(); + /** * @notice Reverts when the asset is insufficient. */ @@ -260,14 +264,14 @@ interface IAssetManager { * * @return When the validator can withdraw KRO. */ - function canWithdrawAt(address validator) external view returns (uint256); + function canWithdrawAt(address validator) external view returns (uint128); /** - * @notice Returns the total amount of KRO a validator has deposited and rewarded. + * @notice Returns the total amount of KRO a validator has deposited and been rewarded. * * @param validator Address of the validator. * - * @return The total amount of KRO a validator has deposited and rewarded. + * @return The total amount of KRO a validator has deposited and been rewarded. */ function totalValidatorKro(address validator) external view returns (uint128); @@ -325,7 +329,7 @@ interface IAssetManager { function canUndelegateKroAt( address validator, address delegator - ) external view returns (uint256); + ) external view returns (uint128); /** * @notice Returns the number of KGH delegated by the given delegator. @@ -353,18 +357,21 @@ interface IAssetManager { ) external view returns (uint128); /** - * @notice Returns when the KGH delegators can undelegate KGH. The delegators can undelegate - * after UNDELEGATION_DELAY_SECONDS elapsed from lastDelegatedAt. + * @notice Returns when the KGH delegators can undelegate KGH. The delegators can undelegate KGH + * for the given token id after UNDELEGATION_DELAY_SECONDS elapsed from delegation + * timestamp. * * @param validator Address of the validator. * @param delegator Address of the KGH delegator. + * @param tokenId The token id of KGH to undelegate. * - * @return When the KGH delegators can undelegate KGH. + * @return When the KGH delegators can undelegate KGH for the given token id. */ function canUndelegateKghAt( address validator, - address delegator - ) external view returns (uint256); + address delegator, + uint256 tokenId + ) external view returns (uint128); /** * @notice Allows an on-chain or off-chain user to simulate the effects of their KRO delegation diff --git a/packages/contracts/contracts/L1/interfaces/IValidatorManager.sol b/packages/contracts/contracts/L1/interfaces/IValidatorManager.sol index f35a65fee..05b95f6c3 100644 --- a/packages/contracts/contracts/L1/interfaces/IValidatorManager.sol +++ b/packages/contracts/contracts/L1/interfaces/IValidatorManager.sol @@ -72,19 +72,19 @@ interface IValidatorManager { /** * @notice Constructs the information of a validator. * - * @custom:field isInitiated Whether the validator is initiated. - * @custom:field noSubmissionCount Number of counts that the validator did not submit the - * output in priority round. - * @custom:field commissionRate Commission rate of validator. - * @custom:field pendingCommissionRate Pending commission rate of validator. - * @custom:field commissionChangeInitTime Timestamp of commission change initialization. + * @custom:field isInitiated Whether the validator is initiated. + * @custom:field noSubmissionCount Number of counts that the validator did not submit + * the output in priority round. + * @custom:field commissionRate Commission rate of validator. + * @custom:field pendingCommissionRate Pending commission rate of validator. + * @custom:field commissionChangeInitiatedAt Timestamp of commission change initialization. */ struct Validator { bool isInitiated; uint8 noSubmissionCount; uint8 commissionRate; uint8 pendingCommissionRate; - uint128 commissionChangeInitTime; + uint128 commissionChangeInitiatedAt; } /** @@ -145,21 +145,13 @@ interface IValidatorManager { ); /** - * @notice Emitted when a validator is jailed when it does not submit outputs in priority round. + * @notice Emitted when a validator is jailed. * * @param validator Address of the validator. * @param expiresAt The expiration timestamp of the jail. */ event ValidatorJailed(address indexed validator, uint128 expiresAt); - /** - * @notice Emitted when a validator is jailed by slashing. - * - * @param validator Address of the validator. - * @param expiresAt The expiration timestamp of the jail. - */ - event ValidatorJailedBySlashing(address indexed validator, uint128 expiresAt); - /** * @notice Emitted when a validator is unjailed. * @@ -168,20 +160,20 @@ interface IValidatorManager { event ValidatorUnjailed(address indexed validator); /** - * @notice Emitted when validator KRO is reserved during output submission or challenge creation. + * @notice Emitted when validator KRO is bonded during output submission or challenge creation. * * @param validator Address of the validator. - * @param amount The amount of KRO reserved. + * @param amount The amount of KRO bonded. */ - event ValidatorKroReserved(address indexed validator, uint128 amount); + event ValidatorKroBonded(address indexed validator, uint128 amount); /** - * @notice Emitted when validator KRO is unreserved during output finalization or slashing. + * @notice Emitted when validator KRO is unbonded during output finalization or slashing. * * @param validator Address of the validator. - * @param amount The amount of KRO unreserved. + * @param amount The amount of KRO unbonded. */ - event ValidatorKroUnreserved(address indexed validator, uint128 amount); + event ValidatorKroUnbonded(address indexed validator, uint128 amount); /** * @notice Emitted when the output reward is distributed. @@ -257,11 +249,6 @@ interface IValidatorManager { */ error NotElapsedCommissionChangeDelay(); - /** - * @notice Reverts when the address is zero address. - */ - error ZeroAddress(); - /** * @notice Reverts when try to unjail before jail period elapsed. */ @@ -380,7 +367,7 @@ interface IValidatorManager { * * @return When commission change of given validator can be finalized. */ - function canFinalizeCommissionChangeAt(address validator) external view returns (uint256); + function canFinalizeCommissionChangeAt(address validator) external view returns (uint128); /** * @notice Checks the eligibility to submit L2 checkpoint output during output submission.