|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Aztec Labs. |
| 3 | +pragma solidity >=0.8.27; |
| 4 | + |
| 5 | +import {Errors} from "@aztec/core/libraries/Errors.sol"; |
| 6 | +import {Signature, SignatureLib} from "@aztec/shared/libraries/SignatureLib.sol"; |
| 7 | +import {Timestamp} from "@aztec/shared/libraries/TimeMath.sol"; |
| 8 | +import {CoordinationSignatureLib} from "./CoordinationSignatureLib.sol"; |
| 9 | +import {StakingLib, AttesterExitLimitState, AttesterExitAuthorization} from "./StakingLib.sol"; |
| 10 | + |
| 11 | +/// @notice External attester-exit functions separated to keep rollup libraries within the contract size limit. |
| 12 | +/// @dev Library calls execute against the calling rollup's staking storage. |
| 13 | +library AttesterExitExtLib { |
| 14 | + using SignatureLib for Signature; |
| 15 | + |
| 16 | + bytes32 internal constant ATTESTER_EXIT_TYPEHASH = keccak256("AttesterExit(address attester,uint256 deadline)"); |
| 17 | + |
| 18 | + /// @notice Initiates an exit for a position controlled by the calling attester. |
| 19 | + function initiateWithdrawByAttester(address _attester) external { |
| 20 | + StakingLib.initiateWithdrawByAttester(_attester); |
| 21 | + } |
| 22 | + |
| 23 | + /// @notice Initiates an attester exit authorized by an EIP-712 signature. |
| 24 | + /// @param _authorization The signed exit authorization. |
| 25 | + function initiateWithdrawByAttesterWithSignature(AttesterExitAuthorization calldata _authorization) external { |
| 26 | + _authorize(_authorization); |
| 27 | + StakingLib.initiateWithdrawByAttesterWithSignature(_authorization.attester); |
| 28 | + } |
| 29 | + |
| 30 | + /// @notice Initiates multiple attester exits authorized by EIP-712 signatures. |
| 31 | + /// @param _authorizations The signed exit authorizations to execute atomically. |
| 32 | + function initiateWithdrawByAttesterBatch(AttesterExitAuthorization[] calldata _authorizations) external { |
| 33 | + uint256 exitCount = _authorizations.length; |
| 34 | + require(exitCount > 0, Errors.Staking__EmptyAttesterExitBatch()); |
| 35 | + |
| 36 | + StakingLib.checkAttesterExitInstance(); |
| 37 | + StakingLib.consumeAttesterExitAllowance(exitCount); |
| 38 | + |
| 39 | + for (uint256 i = 0; i < exitCount;) { |
| 40 | + AttesterExitAuthorization calldata authorization = _authorizations[i]; |
| 41 | + _authorize(authorization); |
| 42 | + StakingLib.initiateWithdrawByAttesterBatchItem(authorization.attester); |
| 43 | + unchecked { |
| 44 | + ++i; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// @notice Initiates as many authorized attester exits as the current limit permits. |
| 50 | + /// @param _authorizations The ordered signed exit authorizations. |
| 51 | + /// @return exitCount The number of authorizations processed from the start of the array. |
| 52 | + function initiateWithdrawByAttesterBatchUpToLimit(AttesterExitAuthorization[] calldata _authorizations) |
| 53 | + external |
| 54 | + returns (uint256 exitCount) |
| 55 | + { |
| 56 | + uint256 requestedExitCount = _authorizations.length; |
| 57 | + require(requestedExitCount > 0, Errors.Staking__EmptyAttesterExitBatch()); |
| 58 | + |
| 59 | + StakingLib.checkAttesterExitInstance(); |
| 60 | + exitCount = StakingLib.consumeAttesterExitAllowanceUpTo(requestedExitCount); |
| 61 | + |
| 62 | + for (uint256 i = 0; i < exitCount;) { |
| 63 | + AttesterExitAuthorization calldata authorization = _authorizations[i]; |
| 64 | + _authorize(authorization); |
| 65 | + StakingLib.initiateWithdrawByAttesterBatchItem(authorization.attester); |
| 66 | + unchecked { |
| 67 | + ++i; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// @notice Returns the current window used to count attester exits. |
| 73 | + function getAttesterExitWindow() external view returns (Timestamp) { |
| 74 | + return StakingLib.getAttesterExitWindow(); |
| 75 | + } |
| 76 | + |
| 77 | + /// @notice Returns attester-exit capacity for the calling rollup. |
| 78 | + /// @dev canExit does not establish eligibility of an individual position. |
| 79 | + function getAttesterExitLimitState() external view returns (AttesterExitLimitState memory) { |
| 80 | + return StakingLib.getAttesterExitLimitState(); |
| 81 | + } |
| 82 | + |
| 83 | + function _authorize(AttesterExitAuthorization calldata _authorization) private view { |
| 84 | + require( |
| 85 | + block.timestamp <= _authorization.deadline, |
| 86 | + Errors.Staking__AttesterExitAuthorizationExpired(_authorization.deadline, block.timestamp) |
| 87 | + ); |
| 88 | + |
| 89 | + bytes32 structHash = keccak256(abi.encode(ATTESTER_EXIT_TYPEHASH, _authorization.attester, _authorization.deadline)); |
| 90 | + bytes32 digest = CoordinationSignatureLib.toTypedDataHash(structHash); |
| 91 | + _authorization.signature.verify(_authorization.attester, digest); |
| 92 | + } |
| 93 | +} |
0 commit comments