-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new ban validator functionality (#12)
* new ban validator functionality create a new contract and implement a ban function for the validators that will be banned; create IInspector interface and compile create new internal function to handle unstake in order to re-use it; write the tests for the new inspector contract; adapt the other tests with the new min stake and min delegate;
- Loading branch information
Showing
18 changed files
with
2,257 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.17; | ||
|
||
/** | ||
* @notice Data type for the banned validators' withdrawals | ||
* @param liquidTokens The amount of liquid tokens to be taken on withdrawal from the penalized validator | ||
* @param withdrawableAmount The amount that is available for withdrawal after validator's penalty | ||
*/ | ||
struct WithdrawalInfo { | ||
uint256 liquidTokens; | ||
uint256 withdrawableAmount; | ||
} | ||
|
||
interface IInspector { | ||
event ValidatorBanned(address indexed validator); | ||
|
||
/** | ||
* @notice Manual ban of a validator | ||
* @dev Function can be executed only by the governor/owner | ||
* @param validator Address of the validator | ||
*/ | ||
function banValidator(address validator) external; | ||
|
||
/** | ||
* @notice Set the penalty amount for the banned validators | ||
* @param newPenalty Amount of the penalty | ||
*/ | ||
function setValidatorPenalty(uint256 newPenalty) external; | ||
|
||
/** | ||
* @notice Set the reward of the person who reports a validator | ||
* @param newReward Amount of the reward | ||
*/ | ||
function setReporterReward(uint256 newReward) external; | ||
|
||
/** | ||
* @notice Withdraw funds left for a banned validator | ||
* @dev Function can be executed only by the banned validator | ||
*/ | ||
function withdrawBannedFunds() external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.17; | ||
|
||
import "./IInspector.sol"; | ||
import "./../Staking/Staking.sol"; | ||
import "./../../../common/Errors.sol"; | ||
|
||
abstract contract Inspector is IInspector, Staking { | ||
/// @notice The penalty that will be taken and burned from the bad valiator's staked amount | ||
uint256 public validatorPenalty; | ||
|
||
/// @notice The reward for the person who reports a validator that have to be banned | ||
uint256 public reporterReward; | ||
|
||
/** | ||
* @notice The withdrawal info that is required for a banned validator to withdraw the funds left | ||
* @dev The withdrawal amount is calculated as the difference between | ||
* the validator's total stake and any penalties applied due to a ban | ||
*/ | ||
mapping(address => WithdrawalInfo) public withdrawalBalances; | ||
|
||
// _______________ Modifiers _______________ | ||
|
||
// Only address that is banned | ||
modifier onlyBanned() { | ||
if (validators[msg.sender].status != ValidatorStatus.Banned) revert Unauthorized("BANNED_VALIDATOR"); | ||
_; | ||
} | ||
|
||
// _______________ Initializer _______________ | ||
|
||
function __Inspector_init() internal onlyInitializing { | ||
__Inspector_init_unchained(); | ||
} | ||
|
||
function __Inspector_init_unchained() internal onlyInitializing { | ||
validatorPenalty = 700 ether; | ||
reporterReward = 300 ether; | ||
} | ||
|
||
// _______________ External functions _______________ | ||
|
||
/** | ||
* @inheritdoc IInspector | ||
*/ | ||
function banValidator(address validator) external onlyOwner { | ||
if (validators[validator].status != ValidatorStatus.Registered) revert Unauthorized("UNREGISTERED_VALIDATOR"); | ||
|
||
uint256 stakedAmount = balanceOf(validator); | ||
if (stakedAmount != 0) { | ||
_burn(validator, stakedAmount); | ||
StateSyncer._syncStake(validator, 0); | ||
uint256 amountLeft = rewardPool.onUnstake(validator, stakedAmount, 0); | ||
|
||
uint256 penalty = validatorPenalty; | ||
if (amountLeft < penalty) penalty = amountLeft; | ||
|
||
withdrawalBalances[validator].liquidTokens = stakedAmount; | ||
withdrawalBalances[validator].withdrawableAmount = amountLeft - penalty; | ||
} | ||
|
||
validators[validator].status = ValidatorStatus.Banned; | ||
|
||
emit ValidatorBanned(validator); | ||
} | ||
|
||
// _______________ Public functions _______________ | ||
|
||
/** | ||
* @inheritdoc IInspector | ||
*/ | ||
function setValidatorPenalty(uint256 newPenalty) public onlyOwner { | ||
validatorPenalty = newPenalty; | ||
} | ||
|
||
/** | ||
* @inheritdoc IInspector | ||
*/ | ||
function setReporterReward(uint256 newReward) public onlyOwner { | ||
reporterReward = newReward; | ||
} | ||
|
||
/** | ||
* @inheritdoc IInspector | ||
*/ | ||
function withdrawBannedFunds() public onlyBanned { | ||
WithdrawalInfo memory withdrawalBalance = withdrawalBalances[msg.sender]; | ||
|
||
delete withdrawalBalances[msg.sender]; | ||
|
||
LiquidStaking._collectTokens(msg.sender, withdrawalBalance.liquidTokens); | ||
|
||
_withdraw(msg.sender, withdrawalBalance.withdrawableAmount); | ||
} | ||
|
||
// slither-disable-next-line unused-state,naming-convention | ||
uint256[50] private __gap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.