Skip to content

Commit

Permalink
feat: consolidate slashAmount and minStake for vanillaRegistry (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz authored Sep 20, 2024
1 parent 0a85a2b commit 5fe2843
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 302 deletions.
42 changes: 5 additions & 37 deletions contracts-abi/abi/VanillaRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@
"type": "uint256",
"internalType": "uint256"
},
{
"name": "_slashAmount",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "_slashOracle",
"type": "address",
Expand Down Expand Up @@ -322,19 +317,6 @@
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "setSlashAmount",
"inputs": [
{
"name": "newSlashAmount",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "setSlashOracle",
Expand Down Expand Up @@ -645,25 +627,6 @@
],
"anonymous": false
},
{
"type": "event",
"name": "SlashAmountSet",
"inputs": [
{
"name": "msgSender",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "newSlashAmount",
"type": "uint256",
"indexed": false,
"internalType": "uint256"
}
],
"anonymous": false
},
{
"type": "event",
"name": "SlashOracleSet",
Expand Down Expand Up @@ -1012,6 +975,11 @@
"name": "NotInitializing",
"inputs": []
},
{
"type": "error",
"name": "NothingToWithdraw",
"inputs": []
},
{
"type": "error",
"name": "OwnableInvalidOwner",
Expand Down
192 changes: 13 additions & 179 deletions contracts-abi/clients/VanillaRegistry/VanillaRegistry.go

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions contracts/contracts/interfaces/IVanillaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ interface IVanillaRegistry {
/// @dev Event emitted when the min stake parameter is set.
event MinStakeSet(address indexed msgSender, uint256 newMinStake);

/// @dev Event emitted when the slash amount parameter is set.
event SlashAmountSet(address indexed msgSender, uint256 newSlashAmount);

/// @dev Event emitted when the slash oracle parameter is set.
event SlashOracleSet(address indexed msgSender, address newSlashOracle);

Expand All @@ -53,6 +50,7 @@ interface IVanillaRegistry {
error SenderIsNotSlashOracle(address sender, address slashOracle);
error WithdrawalAddressMustBeSet();
error MustUnstakeToWithdraw();
error NothingToWithdraw();
error AtLeastOneRecipientRequired();
error StakeTooLowForNumberOfKeys(uint256 msgValue, uint256 numberOfKeys);
error WithdrawingTooSoon();
Expand All @@ -69,7 +67,6 @@ interface IVanillaRegistry {
/// @dev Initializes the contract with the provided parameters.
function initialize(
uint256 _minStake,
uint256 _slashAmount,
address _slashOracle,
address _slashReceiver,
uint256 _unstakePeriodBlocks,
Expand Down Expand Up @@ -124,9 +121,6 @@ interface IVanillaRegistry {
/// @dev Enables the owner to set the minimum stake parameter.
function setMinStake(uint256 newMinStake) external;

/// @dev Enables the owner to set the slash amount parameter.
function setSlashAmount(uint256 newSlashAmount) external;

/// @dev Enables the owner to set the slash oracle parameter.
function setSlashOracle(address newSlashOracle) external;

Expand Down
32 changes: 8 additions & 24 deletions contracts/contracts/validator-registry/VanillaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,12 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
/// @dev Initializes the contract with the provided parameters.
function initialize(
uint256 _minStake,
uint256 _slashAmount,
address _slashOracle,
address _slashReceiver,
uint256 _unstakePeriodBlocks,
address _owner
) external initializer {
_setMinStake(_minStake);
_setSlashAmount(_slashAmount);
_setSlashOracle(_slashOracle);
_setSlashReceiver(_slashReceiver);
_setUnstakePeriodBlocks(_unstakePeriodBlocks);
Expand Down Expand Up @@ -180,11 +178,6 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
_setMinStake(newMinStake);
}

/// @dev Enables the owner to set the slash amount parameter.
function setSlashAmount(uint256 newSlashAmount) external onlyOwner {
_setSlashAmount(newSlashAmount);
}

/// @dev Enables the owner to set the slash oracle parameter.
function setSlashOracle(address newSlashOracle) external onlyOwner {
_setSlashOracle(newSlashOracle);
Expand Down Expand Up @@ -321,6 +314,7 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
require(block.number > stakedValidators[pubKey].unstakeOccurrence.blockHeight + unstakePeriodBlocks,
IVanillaRegistry.WithdrawingTooSoon());
uint256 balance = stakedValidators[pubKey].balance;
require(balance != 0, IVanillaRegistry.NothingToWithdraw());
address withdrawalAddress = stakedValidators[pubKey].withdrawalAddress;
delete stakedValidators[pubKey];
(bool success, ) = withdrawalAddress.call{value: balance}("");
Expand All @@ -330,23 +324,21 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
}

/*
* @dev Internal function to slash ETH on behalf of one or multiple validators via their BLS pubkey.
* @dev Internal function to slash minStake worth of ETH on behalf of one or multiple validators via their BLS pubkey.
* @param blsPubKeys The BLS public keys to slash.
*/
function _slash(bytes[] calldata blsPubKeys) internal {
uint256 len = blsPubKeys.length;
for (uint256 i = 0; i < len; ++i) {
bytes calldata pubKey = blsPubKeys[i];
require(stakedValidators[pubKey].balance > slashAmount,
IVanillaRegistry.NotEnoughBalanceToSlash());
stakedValidators[pubKey].balance -= slashAmount;
if (!_isUnstaking(pubKey)) {
_unstakeSingle(pubKey);
require(stakedValidators[pubKey].balance >= minStake, IVanillaRegistry.NotEnoughBalanceToSlash());
stakedValidators[pubKey].balance -= minStake;
if (!_isUnstaking(pubKey)) {
_unstakeSingle(pubKey);
}
(bool success, ) = slashReceiver.call{value: slashAmount}("");
(bool success, ) = slashReceiver.call{value: minStake}("");
require(success, IVanillaRegistry.SlashingTransferFailed());
emit Slashed(msg.sender, slashReceiver,
stakedValidators[pubKey].withdrawalAddress, pubKey, slashAmount);
emit Slashed(msg.sender, slashReceiver, stakedValidators[pubKey].withdrawalAddress, pubKey, minStake);
}
}

Expand All @@ -357,14 +349,6 @@ contract VanillaRegistry is IVanillaRegistry, VanillaRegistryStorage,
emit MinStakeSet(msg.sender, newMinStake);
}

/// @dev Internal function to set the slash amount parameter.
function _setSlashAmount(uint256 newSlashAmount) internal {
require(newSlashAmount != 0, IVanillaRegistry.SlashAmountMustBePositive());
require(newSlashAmount < minStake, IVanillaRegistry.SlashAmountMustBeLessThanMinStake());
slashAmount = newSlashAmount;
emit SlashAmountSet(msg.sender, newSlashAmount);
}

/// @dev Internal function to set the slash oracle parameter.
function _setSlashOracle(address newSlashOracle) internal {
require(newSlashOracle != address(0), IVanillaRegistry.SlashOracleMustBeSet());
Expand Down
13 changes: 4 additions & 9 deletions contracts/scripts/validator-registry/DeployVanillaRegistry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {console} from "forge-std/console.sol";
contract BaseDeploy is Script {
function deployVanillaRegistry(
uint256 minStake,
uint256 slashAmount,
address slashOracle,
address slashReceiver,
uint256 unstakePeriodBlocks,
Expand All @@ -24,7 +23,7 @@ contract BaseDeploy is Script {
"VanillaRegistry.sol",
abi.encodeCall(
VanillaRegistry.initialize,
(minStake, slashAmount, slashOracle, slashReceiver, unstakePeriodBlocks, owner)
(minStake, slashOracle, slashReceiver, unstakePeriodBlocks, owner)
)
);
console.log("VanillaRegistry UUPS proxy deployed to:", address(proxy));
Expand All @@ -36,7 +35,6 @@ contract BaseDeploy is Script {

contract DeployHolesky is BaseDeploy {
uint256 constant public MIN_STAKE = 0.0001 ether; // 10k vals = 1 ETH cost
uint256 constant public SLASH_AMOUNT = 0.00003 ether;
address constant public SLASH_ORACLE = 0x4535bd6fF24860b5fd2889857651a85fb3d3C6b1;
address constant public SLASH_RECEIVER = 0x4535bd6fF24860b5fd2889857651a85fb3d3C6b1;
uint256 constant public UNSTAKE_PERIOD_BLOCKS = 32 * 3; // 2 epoch finalization time + settlement buffer
Expand All @@ -47,24 +45,21 @@ contract DeployHolesky is BaseDeploy {
function run() external {
require(block.chainid == 17000, "must deploy on Holesky");
vm.startBroadcast();
deployVanillaRegistry(MIN_STAKE, SLASH_AMOUNT,
SLASH_ORACLE, SLASH_RECEIVER, UNSTAKE_PERIOD_BLOCKS, OWNER);
deployVanillaRegistry(MIN_STAKE, SLASH_ORACLE, SLASH_RECEIVER, UNSTAKE_PERIOD_BLOCKS, OWNER);
vm.stopBroadcast();
}
}

contract DeployAnvil is BaseDeploy {
uint256 constant public MIN_STAKE = 3 ether;
uint256 constant public SLASH_AMOUNT = 1 ether;
uint256 constant public MIN_STAKE = 1 ether;
address constant public SLASH_ORACLE = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
address constant public SLASH_RECEIVER = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC;
uint256 constant public UNSTAKE_PERIOD_BLOCKS = 100;

function run() external {
require(block.chainid == 31337, "must deploy on anvil");
vm.startBroadcast();
deployVanillaRegistry(MIN_STAKE, SLASH_AMOUNT,
SLASH_ORACLE, SLASH_RECEIVER, UNSTAKE_PERIOD_BLOCKS, msg.sender);
deployVanillaRegistry(MIN_STAKE, SLASH_ORACLE, SLASH_RECEIVER, UNSTAKE_PERIOD_BLOCKS, msg.sender);
vm.stopBroadcast();
}
}
Loading

0 comments on commit 5fe2843

Please sign in to comment.