Skip to content

Commit

Permalink
fix: prevent provisions from being overwritten if they exist (OZ H-05)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Aug 23, 2024
1 parent 4787e12 commit 0a9fe15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ interface IHorizonStakingMain {
*/
error HorizonStakingInvalidThawingPeriod(uint64 thawingPeriod, uint64 maxThawingPeriod);

/**
* @notice Thrown when attempting to create a provision for a data service that already has a provision.
*/
error HorizonStakingProvisionAlreadyExists();

// -- Errors: stake --

/**
Expand Down
1 change: 1 addition & 0 deletions packages/horizon/contracts/staking/HorizonStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ contract HorizonStaking is HorizonStakingBase, IHorizonStakingMain {
_thawingPeriod <= _maxThawingPeriod,
HorizonStakingInvalidThawingPeriod(_thawingPeriod, _maxThawingPeriod)
);
require(_provisions[_serviceProvider][_verifier].createdAt == 0, HorizonStakingProvisionAlreadyExists());
uint256 tokensIdle = _getIdleStake(_serviceProvider);
require(_tokens <= tokensIdle, HorizonStakingInsufficientIdleStake(_tokens, tokensIdle));

Expand Down
20 changes: 17 additions & 3 deletions packages/horizon/test/staking/provision/provision.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "forge-std/Test.sol";
import { HorizonStakingTest } from "../HorizonStaking.t.sol";

contract HorizonStakingProvisionTest is HorizonStakingTest {

/*
* TESTS
*/
Expand Down Expand Up @@ -68,6 +67,21 @@ contract HorizonStakingProvisionTest is HorizonStakingTest {
staking.provision(users.indexer, subgraphDataServiceAddress, provisionTokens, 0, 0);
}

function testProvision_RevertWhen_AlreadyExists(
uint256 amount,
uint32 maxVerifierCut,
uint64 thawingPeriod
) public useIndexer useProvision(amount / 2, maxVerifierCut, thawingPeriod) {
resetPrank(users.indexer);

token.approve(address(staking), amount / 2);
staking.stake(amount / 2);

bytes memory expectedError = abi.encodeWithSignature("HorizonStakingProvisionAlreadyExists()");
vm.expectRevert(expectedError);
staking.provision(users.indexer, subgraphDataServiceAddress, amount / 2, maxVerifierCut, thawingPeriod);
}

function testProvision_OperatorAddTokensToProvision(
uint256 amount,
uint32 maxVerifierCut,
Expand All @@ -94,12 +108,12 @@ contract HorizonStakingProvisionTest is HorizonStakingTest {
) public useIndexer useProvision(amount, maxVerifierCut, thawingPeriod) {
vm.startPrank(users.operator);
bytes memory expectedError = abi.encodeWithSignature(
"HorizonStakingNotAuthorized(address,address,address)",
"HorizonStakingNotAuthorized(address,address,address)",
users.operator,
users.indexer,
subgraphDataServiceAddress
);
vm.expectRevert(expectedError);
staking.provision(users.indexer, subgraphDataServiceAddress, amount, maxVerifierCut, thawingPeriod);
}
}
}

0 comments on commit 0a9fe15

Please sign in to comment.