Skip to content

Commit 99f215c

Browse files
committed
chore: use custom errors
Signed-off-by: Tomás Migone <[email protected]>
1 parent 5557c5e commit 99f215c

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

packages/horizon/contracts/interfaces/internal/IHorizonStakingMain.sol

+16
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ interface IHorizonStakingMain {
471471
*/
472472
error HorizonStakingTooManyThawRequests();
473473

474+
/**
475+
* @notice Thrown when attempting to withdraw tokens that have not thawed (legacy undelegate).
476+
*/
477+
error HorizonStakingNothingToWithdraw();
478+
474479
// -- Errors: misc --
475480
/**
476481
* @notice Thrown during the transition period when attempting to withdraw tokens that are still thawing.
@@ -498,6 +503,11 @@ interface IHorizonStakingMain {
498503
*/
499504
error HorizonStakingInvalidDelegationFeeCut(uint256 feeCut);
500505

506+
/**
507+
* @notice Thrown when a legacy slash fails.
508+
*/
509+
error HorizonStakingLegacySlashFailed();
510+
501511
// -- Functions --
502512

503513
/**
@@ -972,4 +982,10 @@ interface IHorizonStakingMain {
972982
* @return Whether the operator is authorized or not
973983
*/
974984
function isAuthorized(address serviceProvider, address verifier, address operator) external view returns (bool);
985+
986+
/**
987+
* @notice Get the address of the staking extension.
988+
* @return The address of the staking extension
989+
*/
990+
function getStakingExtension() external view returns (address);
975991
}

packages/horizon/contracts/staking/HorizonStaking.sol

+11-2
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
361361
) {
362362
tokensToWithdraw = delegation.__DEPRECATED_tokensLocked;
363363
}
364-
require(tokensToWithdraw > 0, "!tokens");
364+
require(tokensToWithdraw > 0, HorizonStakingNothingToWithdraw());
365365

366366
// Reset lock
367367
delegation.__DEPRECATED_tokensLocked = 0;
@@ -402,7 +402,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
402402
verifierDestination
403403
)
404404
);
405-
require(success, "Delegatecall: legacySlash failed");
405+
require(success, HorizonStakingLegacySlashFailed());
406406
return;
407407
}
408408

@@ -562,6 +562,15 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
562562
return _isAuthorized(serviceProvider, verifier, operator);
563563
}
564564

565+
/*
566+
* GETTERS
567+
*/
568+
569+
/// @inheritdoc IHorizonStakingMain
570+
function getStakingExtension() external view override returns (address) {
571+
return STAKING_EXTENSION_ADDRESS;
572+
}
573+
565574
/*
566575
* PRIVATE FUNCTIONS
567576
*/

packages/horizon/contracts/staking/HorizonStakingBase.sol

-8
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ abstract contract HorizonStakingBase is
5050
SUBGRAPH_DATA_SERVICE_ADDRESS = subgraphDataServiceAddress;
5151
}
5252

53-
/**
54-
* @notice Receive ETH into the Staking contract: this will always revert
55-
* @dev This function is only here to prevent ETH from being sent to the contract
56-
*/
57-
receive() external payable {
58-
revert("RECEIVE_ETH_NOT_ALLOWED");
59-
}
60-
6153
/// @inheritdoc IHorizonStakingBase
6254
/// @dev Removes deprecated fields from the return value.
6355
function getServiceProvider(address serviceProvider) external view override returns (ServiceProvider memory) {

packages/horizon/test/staking/delegation/legacyWithdraw.t.sol

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ contract HorizonStakingLegacyWithdrawDelegationTest is HorizonStakingTest {
9595
_setStorage_DelegationPool(users.indexer, 0, 0, 0);
9696
_setLegacyDelegation(users.indexer, users.delegator, 0, 0, 0);
9797

98-
vm.expectRevert("!tokens");
98+
bytes memory expectedError = abi.encodeWithSignature("HorizonStakingNothingToWithdraw()");
99+
vm.expectRevert(expectedError);
99100
staking.withdrawDelegated(users.indexer, address(0));
100101
}
101102
}

packages/horizon/test/staking/slash/legacySlash.t.sol

+27
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,31 @@ contract HorizonStakingLegacySlashTest is HorizonStakingTest {
204204
resetPrank(users.legacySlasher);
205205
_legacySlash(users.indexer, 1000 ether, 500 ether, makeAddr("fisherman"));
206206
}
207+
208+
function test_LegacySlash_WhenDelegateCallFails() public useIndexer useLegacySlasher(users.legacySlasher) {
209+
// Setup indexer with:
210+
// - tokensStaked = 1000 GRT
211+
// - tokensAllocated = 800 GRT
212+
// - tokensLocked = 300 GRT
213+
214+
_setIndexer(
215+
users.indexer,
216+
1000 ether, // tokensStaked
217+
800 ether, // tokensAllocated
218+
300 ether, // tokensLocked
219+
0 // tokensLockedUntil
220+
);
221+
222+
// Send tokens manually to staking
223+
token.transfer(address(staking), 1100 ether);
224+
225+
// Change staking extension code to an invalid opcode so the delegatecall reverts
226+
address stakingExtension = staking.getStakingExtension();
227+
vm.etch(stakingExtension, hex"fe");
228+
229+
resetPrank(users.legacySlasher);
230+
bytes memory expectedError = abi.encodeWithSignature("HorizonStakingLegacySlashFailed()");
231+
vm.expectRevert(expectedError);
232+
staking.slash(users.indexer, 1000 ether, 500 ether, makeAddr("fisherman"));
233+
}
207234
}

0 commit comments

Comments
 (0)