|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2024 Aztec Labs. |
| 3 | +pragma solidity >=0.8.27; |
| 4 | + |
| 5 | +import { |
| 6 | + IStaking, ValidatorInfo, Exit, Status, OperatorInfo |
| 7 | +} from "@aztec/core/interfaces/IStaking.sol"; |
| 8 | +import {Errors} from "@aztec/core/libraries/Errors.sol"; |
| 9 | +import {Timestamp} from "@aztec/core/libraries/TimeMath.sol"; |
| 10 | +import {IERC20} from "@oz/token/ERC20/IERC20.sol"; |
| 11 | +import {SafeERC20} from "@oz/token/ERC20/utils/SafeERC20.sol"; |
| 12 | +import {EnumerableSet} from "@oz/utils/structs/EnumerableSet.sol"; |
| 13 | + |
| 14 | +contract Staking is IStaking { |
| 15 | + using SafeERC20 for IERC20; |
| 16 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 17 | + |
| 18 | + // Constant pulled out of the ass |
| 19 | + Timestamp public constant EXIT_DELAY = Timestamp.wrap(60 * 60 * 24); |
| 20 | + |
| 21 | + address public immutable SLASHER; |
| 22 | + IERC20 public immutable STAKING_ASSET; |
| 23 | + uint256 public immutable MINIMUM_STAKE; |
| 24 | + |
| 25 | + // address <=> index |
| 26 | + EnumerableSet.AddressSet internal attesters; |
| 27 | + |
| 28 | + mapping(address attester => ValidatorInfo) internal info; |
| 29 | + mapping(address attester => Exit) internal exits; |
| 30 | + |
| 31 | + constructor(address _slasher, IERC20 _stakingAsset, uint256 _minimumStake) { |
| 32 | + SLASHER = _slasher; |
| 33 | + STAKING_ASSET = _stakingAsset; |
| 34 | + MINIMUM_STAKE = _minimumStake; |
| 35 | + } |
| 36 | + |
| 37 | + function finaliseWithdraw(address _attester) external override(IStaking) { |
| 38 | + ValidatorInfo storage validator = info[_attester]; |
| 39 | + require(validator.status == Status.EXITING, Errors.Staking__NotExiting(_attester)); |
| 40 | + |
| 41 | + Exit storage exit = exits[_attester]; |
| 42 | + require( |
| 43 | + exit.exitableAt <= Timestamp.wrap(block.timestamp), |
| 44 | + Errors.Staking__WithdrawalNotUnlockedYet(Timestamp.wrap(block.timestamp), exit.exitableAt) |
| 45 | + ); |
| 46 | + |
| 47 | + uint256 amount = validator.stake; |
| 48 | + address recipient = exit.recipient; |
| 49 | + |
| 50 | + delete exits[_attester]; |
| 51 | + delete info[_attester]; |
| 52 | + |
| 53 | + STAKING_ASSET.transfer(recipient, amount); |
| 54 | + |
| 55 | + emit IStaking.WithdrawFinalised(_attester, recipient, amount); |
| 56 | + } |
| 57 | + |
| 58 | + function slash(address _attester, uint256 _amount) external override(IStaking) { |
| 59 | + require(msg.sender == SLASHER, Errors.Staking__NotSlasher(SLASHER, msg.sender)); |
| 60 | + |
| 61 | + ValidatorInfo storage validator = info[_attester]; |
| 62 | + require(validator.status != Status.NONE, Errors.Staking__NoOneToSlash(_attester)); |
| 63 | + |
| 64 | + // There is a special, case, if exiting and past the limit, it is untouchable! |
| 65 | + require( |
| 66 | + !( |
| 67 | + validator.status == Status.EXITING |
| 68 | + && exits[_attester].exitableAt <= Timestamp.wrap(block.timestamp) |
| 69 | + ), |
| 70 | + Errors.Staking__CannotSlashExitedStake(_attester) |
| 71 | + ); |
| 72 | + validator.stake -= _amount; |
| 73 | + |
| 74 | + // If the attester was validating AND is slashed below the MINIMUM_STAKE we update him to LIVING |
| 75 | + // When LIVING, he can only start exiting, we don't "really" exit him, because that cost |
| 76 | + // gas and cost edge cases around recipient, so lets just avoid that. |
| 77 | + if (validator.status == Status.VALIDATING && validator.stake < MINIMUM_STAKE) { |
| 78 | + require(attesters.remove(_attester), Errors.Staking__FailedToRemove(_attester)); |
| 79 | + validator.status = Status.LIVING; |
| 80 | + } |
| 81 | + |
| 82 | + emit Slashed(_attester, _amount); |
| 83 | + } |
| 84 | + |
| 85 | + function getInfo(address _attester) |
| 86 | + external |
| 87 | + view |
| 88 | + override(IStaking) |
| 89 | + returns (ValidatorInfo memory) |
| 90 | + { |
| 91 | + return info[_attester]; |
| 92 | + } |
| 93 | + |
| 94 | + function getProposerForAttester(address _attester) |
| 95 | + external |
| 96 | + view |
| 97 | + override(IStaking) |
| 98 | + returns (address) |
| 99 | + { |
| 100 | + return info[_attester].proposer; |
| 101 | + } |
| 102 | + |
| 103 | + function getExit(address _attester) external view override(IStaking) returns (Exit memory) { |
| 104 | + return exits[_attester]; |
| 105 | + } |
| 106 | + |
| 107 | + function getAttesterAtIndex(uint256 _index) external view override(IStaking) returns (address) { |
| 108 | + return attesters.at(_index); |
| 109 | + } |
| 110 | + |
| 111 | + function getProposerAtIndex(uint256 _index) external view override(IStaking) returns (address) { |
| 112 | + return info[attesters.at(_index)].proposer; |
| 113 | + } |
| 114 | + |
| 115 | + function getOperatorAtIndex(uint256 _index) |
| 116 | + external |
| 117 | + view |
| 118 | + override(IStaking) |
| 119 | + returns (OperatorInfo memory) |
| 120 | + { |
| 121 | + address attester = attesters.at(_index); |
| 122 | + return OperatorInfo({proposer: info[attester].proposer, attester: attester}); |
| 123 | + } |
| 124 | + |
| 125 | + function deposit(address _attester, address _proposer, address _withdrawer, uint256 _amount) |
| 126 | + public |
| 127 | + virtual |
| 128 | + override(IStaking) |
| 129 | + { |
| 130 | + require(_amount >= MINIMUM_STAKE, Errors.Staking__InsufficientStake(_amount, MINIMUM_STAKE)); |
| 131 | + STAKING_ASSET.transferFrom(msg.sender, address(this), _amount); |
| 132 | + require(info[_attester].status == Status.NONE, Errors.Staking__AlreadyRegistered(_attester)); |
| 133 | + require(attesters.add(_attester), Errors.Staking__AlreadyActive(_attester)); |
| 134 | + |
| 135 | + // If BLS, need to check possession of private key to avoid attacks. |
| 136 | + |
| 137 | + info[_attester] = ValidatorInfo({ |
| 138 | + stake: _amount, |
| 139 | + withdrawer: _withdrawer, |
| 140 | + proposer: _proposer, |
| 141 | + status: Status.VALIDATING |
| 142 | + }); |
| 143 | + |
| 144 | + emit IStaking.Deposit(_attester, _proposer, _withdrawer, _amount); |
| 145 | + } |
| 146 | + |
| 147 | + function initiateWithdraw(address _attester, address _recipient) |
| 148 | + public |
| 149 | + virtual |
| 150 | + override(IStaking) |
| 151 | + returns (bool) |
| 152 | + { |
| 153 | + ValidatorInfo storage validator = info[_attester]; |
| 154 | + |
| 155 | + require( |
| 156 | + msg.sender == validator.withdrawer, |
| 157 | + Errors.Staking__NotWithdrawer(validator.withdrawer, msg.sender) |
| 158 | + ); |
| 159 | + require( |
| 160 | + validator.status == Status.VALIDATING || validator.status == Status.LIVING, |
| 161 | + Errors.Staking__NothingToExit(_attester) |
| 162 | + ); |
| 163 | + if (validator.status == Status.VALIDATING) { |
| 164 | + require(attesters.remove(_attester), Errors.Staking__FailedToRemove(_attester)); |
| 165 | + } |
| 166 | + |
| 167 | + // Note that the "amount" is not stored here, but reusing the `validators` |
| 168 | + // We always exit fully. |
| 169 | + exits[_attester] = |
| 170 | + Exit({exitableAt: Timestamp.wrap(block.timestamp) + EXIT_DELAY, recipient: _recipient}); |
| 171 | + validator.status = Status.EXITING; |
| 172 | + |
| 173 | + emit IStaking.WithdrawInitiated(_attester, _recipient, validator.stake); |
| 174 | + |
| 175 | + return true; |
| 176 | + } |
| 177 | + |
| 178 | + function getActiveAttesterCount() public view override(IStaking) returns (uint256) { |
| 179 | + return attesters.length(); |
| 180 | + } |
| 181 | +} |
0 commit comments