From d1c5cc7556641eb83976f35b863eefe42bae2830 Mon Sep 17 00:00:00 2001 From: Miguel de Elias Date: Fri, 6 Dec 2024 16:44:11 -0300 Subject: [PATCH] fix: add a new mapping instead of splitting thaw requests (TRST-H02) --- .../contracts/staking/HorizonStaking.sol | 6 ++-- .../contracts/staking/HorizonStakingBase.sol | 26 ++++------------ .../staking/HorizonStakingStorage.sol | 30 ++++--------------- .../HorizonStakingShared.t.sol | 6 ++-- 4 files changed, 17 insertions(+), 51 deletions(-) diff --git a/packages/horizon/contracts/staking/HorizonStaking.sol b/packages/horizon/contracts/staking/HorizonStaking.sol index db29cf12a..257202d3e 100644 --- a/packages/horizon/contracts/staking/HorizonStaking.sol +++ b/packages/horizon/contracts/staking/HorizonStaking.sol @@ -1181,7 +1181,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain { * @param _thawRequestId The ID of the thaw request to delete. */ function _deleteProvisionThawRequest(bytes32 _thawRequestId) private { - delete _provisionThawRequests[_thawRequestId]; + delete _thawRequests[ThawRequestType.Provision][_thawRequestId]; } /** @@ -1189,7 +1189,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain { * @param _thawRequestId The ID of the thaw request to delete. */ function _deleteDelegationThawRequest(bytes32 _thawRequestId) private { - delete _delegationThawRequests[_thawRequestId]; + delete _thawRequests[ThawRequestType.Delegation][_thawRequestId]; } /** @@ -1197,7 +1197,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain { * @param _thawRequestId The ID of the thaw request to delete. */ function _deleteDelegationWithBeneficiaryThawRequest(bytes32 _thawRequestId) private { - delete _delegationWithBeneficiaryThawRequests[_thawRequestId]; + delete _thawRequests[ThawRequestType.DelegationWithBeneficiary][_thawRequestId]; } /** diff --git a/packages/horizon/contracts/staking/HorizonStakingBase.sol b/packages/horizon/contracts/staking/HorizonStakingBase.sol index 3563dba2e..6745fb2e1 100644 --- a/packages/horizon/contracts/staking/HorizonStakingBase.sol +++ b/packages/horizon/contracts/staking/HorizonStakingBase.sol @@ -323,7 +323,7 @@ abstract contract HorizonStakingBase is * @return The ID of the next thaw request in the list. */ function _getNextProvisionThawRequest(bytes32 _thawRequestId) internal view returns (bytes32) { - return _provisionThawRequests[_thawRequestId].next; + return _thawRequests[ThawRequestType.Provision][_thawRequestId].next; } /** @@ -332,7 +332,7 @@ abstract contract HorizonStakingBase is * @return The ID of the next thaw request in the list. */ function _getNextDelegationThawRequest(bytes32 _thawRequestId) internal view returns (bytes32) { - return _delegationThawRequests[_thawRequestId].next; + return _thawRequests[ThawRequestType.Delegation][_thawRequestId].next; } /** @@ -341,7 +341,7 @@ abstract contract HorizonStakingBase is * @return The ID of the next thaw request in the list. */ function _getNextDelegationWithBeneficiaryThawRequest(bytes32 _thawRequestId) internal view returns (bytes32) { - return _delegationWithBeneficiaryThawRequests[_thawRequestId].next; + return _thawRequests[ThawRequestType.DelegationWithBeneficiary][_thawRequestId].next; } /** @@ -360,15 +360,7 @@ abstract contract HorizonStakingBase is address _verifier, address _owner ) internal view returns (LinkedList.List storage) { - if (_requestType == ThawRequestType.Provision) { - return _provisionThawRequestLists[_serviceProvider][_verifier][_owner]; - } else if (_requestType == ThawRequestType.Delegation) { - return _delegationThawRequestLists[_serviceProvider][_verifier][_owner]; - } else if (_requestType == ThawRequestType.DelegationWithBeneficiary) { - return _delegationWithBeneficiaryThawRequestLists[_serviceProvider][_verifier][_owner]; - } else { - revert HorizonStakingInvalidThawRequestType(); - } + return _thawRequestLists[_requestType][_serviceProvider][_verifier][_owner]; } /** @@ -383,15 +375,7 @@ abstract contract HorizonStakingBase is ThawRequestType _requestType, bytes32 _thawRequestId ) internal view returns (IHorizonStakingTypes.ThawRequest storage) { - if (_requestType == ThawRequestType.Provision) { - return _provisionThawRequests[_thawRequestId]; - } else if (_requestType == ThawRequestType.Delegation) { - return _delegationThawRequests[_thawRequestId]; - } else if (_requestType == ThawRequestType.DelegationWithBeneficiary) { - return _delegationWithBeneficiaryThawRequests[_thawRequestId]; - } else { - revert("Unknown ThawRequestType"); - } + return _thawRequests[_requestType][_thawRequestId]; } /** diff --git a/packages/horizon/contracts/staking/HorizonStakingStorage.sol b/packages/horizon/contracts/staking/HorizonStakingStorage.sol index ca6a1450f..a470ac363 100644 --- a/packages/horizon/contracts/staking/HorizonStakingStorage.sol +++ b/packages/horizon/contracts/staking/HorizonStakingStorage.sol @@ -148,32 +148,14 @@ abstract contract HorizonStakingV1Storage { internal _delegationFeeCut; /// @dev Thaw requests - /// Details for each thawing operation in the staking contract for both service providers. - mapping(bytes32 thawRequestId => IHorizonStakingTypes.ThawRequest thawRequest) internal _provisionThawRequests; + /// Details for each thawing operation in the staking contract (for both service providers and delegators). + mapping(IHorizonStakingTypes.ThawRequestType thawRequestType => mapping(bytes32 thawRequestId => IHorizonStakingTypes.ThawRequest thawRequest)) + internal _thawRequests; /// @dev Thaw request lists - /// Metadata defining linked lists of thaw requests for each service provider (owner). - mapping(address serviceProvider => mapping(address verifier => mapping(address owner => LinkedList.List list))) - internal _provisionThawRequestLists; - - /// @dev Thaw requests - /// Details for each thawing operation in the staking contract for delegators. - mapping(bytes32 thawRequestId => IHorizonStakingTypes.ThawRequest thawRequest) internal _delegationThawRequests; - - /// @dev Thaw request lists - /// Metadata defining linked lists of thaw requests for each delegator (owner). - mapping(address serviceProvider => mapping(address verifier => mapping(address owner => LinkedList.List list))) - internal _delegationThawRequestLists; - - /// @dev Thaw requests - /// Details for each thawing operation in the staking contract for both delegators undelegating to a beneficiary. - mapping(bytes32 thawRequestId => IHorizonStakingTypes.ThawRequest thawRequest) - internal _delegationWithBeneficiaryThawRequests; - - /// @dev Thaw request lists - /// Metadata defining linked lists of thaw requests for each delegator (owner) undelegating to a beneficiary. - mapping(address serviceProvider => mapping(address verifier => mapping(address owner => LinkedList.List list))) - internal _delegationWithBeneficiaryThawRequestLists; + /// Metadata defining linked lists of thaw requests for each service provider or delegator (owner) + mapping(IHorizonStakingTypes.ThawRequestType thawRequestType => mapping(address serviceProvider => mapping(address verifier => mapping(address owner => LinkedList.List list)))) + internal _thawRequestLists; /// @dev Operator allow list /// Used for all verifiers except the subgraph data service. diff --git a/packages/horizon/test/shared/horizon-staking/HorizonStakingShared.t.sol b/packages/horizon/test/shared/horizon-staking/HorizonStakingShared.t.sol index 4e024f899..b47ac7d7e 100644 --- a/packages/horizon/test/shared/horizon-staking/HorizonStakingShared.t.sol +++ b/packages/horizon/test/shared/horizon-staking/HorizonStakingShared.t.sol @@ -1852,7 +1852,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest { address operator, bool legacy ) internal view returns (bool) { - uint256 slotNumber = legacy ? 21 : 35; + uint256 slotNumber = legacy ? 21 : 31; uint256 slot; if (legacy) { @@ -1934,7 +1934,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest { address verifier, bool legacy ) internal view returns (DelegationPoolInternalTest memory) { - uint256 slotNumber = legacy ? 20 : 37; + uint256 slotNumber = legacy ? 20 : 33; uint256 baseSlot; if (legacy) { baseSlot = uint256(keccak256(abi.encode(serviceProvider, slotNumber))); @@ -1966,7 +1966,7 @@ abstract contract HorizonStakingSharedTest is GraphBaseTest { address delegator, bool legacy ) internal view returns (DelegationInternal memory) { - uint256 slotNumber = legacy ? 20 : 37; + uint256 slotNumber = legacy ? 20 : 33; uint256 baseSlot; // DelegationPool