Skip to content

Commit

Permalink
fix: increasing minStake in vanillarRegistry will make some stakes un…
Browse files Browse the repository at this point in the history
…slashable, finding 185
  • Loading branch information
shaspitz committed Oct 30, 2024
1 parent 03867b1 commit 22fa110
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
5 changes: 0 additions & 5 deletions contracts-abi/abi/VanillaRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -1153,11 +1153,6 @@
"name": "NoFundsToWithdraw",
"inputs": []
},
{
"type": "error",
"name": "NotEnoughBalanceToSlash",
"inputs": []
},
{
"type": "error",
"name": "NotInitializing",
Expand Down
2 changes: 1 addition & 1 deletion contracts-abi/clients/VanillaRegistry/VanillaRegistry.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion contracts/contracts/interfaces/IVanillaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ interface IVanillaRegistry {
error WithdrawalAddressMismatch(address actualWithdrawalAddress, address expectedWithdrawalAddress);
error WithdrawalFailed();
error NoFundsToWithdraw();
error NotEnoughBalanceToSlash();
error SlashingTransferFailed();
error MinStakeMustBePositive();
error SlashAmountMustBePositive();
Expand Down
11 changes: 7 additions & 4 deletions contracts/contracts/validator-registry/VanillaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,20 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
for (uint256 i = 0; i < len; ++i) {
bytes calldata pubKey = blsPubKeys[i];
IVanillaRegistry.StakedValidator storage validator = stakedValidators[pubKey];
require(validator.balance >= minStake, IVanillaRegistry.NotEnoughBalanceToSlash());
if (!_isUnstaking(pubKey)) {
_unstakeSingle(pubKey);
}
validator.balance -= minStake;
slashingFundsTracker.accumulatedAmount += minStake;
uint256 toSlash = minStake;
if (validator.balance < minStake) {
toSlash = validator.balance;
}
validator.balance -= toSlash;
slashingFundsTracker.accumulatedAmount += toSlash;
bool isLastEntry = i == len - 1;
if (payoutIfDue && FeePayout.isPayoutDue(slashingFundsTracker) && isLastEntry) {
FeePayout.transferToRecipient(slashingFundsTracker);
}
emit Slashed(msg.sender, slashingFundsTracker.recipient, validator.withdrawalAddress, pubKey, minStake);
emit Slashed(msg.sender, slashingFundsTracker.recipient, validator.withdrawalAddress, pubKey, toSlash);
}
}

Expand Down
13 changes: 7 additions & 6 deletions contracts/test/validator-registry/VanillaRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ contract VanillaRegistryTest is Test {
assertEq(validatorRegistry.getStakedValidator(user1BLSKey).unstakeOccurrence.blockHeight, 0, "User1s unstake block number should be reset after withdrawal");
}

function testSlashWithoutEnoughStake() public {
function testSlashMinStakeIncreased() public {
vm.expectRevert(abi.encodeWithSelector(IVanillaRegistry.ValidatorRecordMustExist.selector, user1BLSKey));
bytes[] memory validators = new bytes[](1);
validators[0] = user1BLSKey;
Expand All @@ -430,13 +430,14 @@ contract VanillaRegistryTest is Test {
validatorRegistry.stake{value: stakeAmount}(validators);
vm.stopPrank();

vm.prank(SLASH_ORACLE);
vm.expectEmit(true, true, true, true);
emit Slashed(SLASH_ORACLE, SLASH_RECEIVER, user1, user1BLSKey, MIN_STAKE);
validatorRegistry.slash(validators, true);
vm.prank(owner);
validatorRegistry.setMinStake(MIN_STAKE * 2);

assertFalse(validatorRegistry.isValidatorOptedIn(user1BLSKey));

vm.expectRevert(IVanillaRegistry.NotEnoughBalanceToSlash.selector);
vm.prank(SLASH_ORACLE);
vm.expectEmit(true, true, true, true);
emit Slashed(SLASH_ORACLE, SLASH_RECEIVER, user1, user1BLSKey, MIN_STAKE+1);
validatorRegistry.slash(validators, true);
}

Expand Down

0 comments on commit 22fa110

Please sign in to comment.