Skip to content

Commit

Permalink
chore: push
Browse files Browse the repository at this point in the history
  • Loading branch information
ypatil12 committed Feb 20, 2025
1 parent ee0e51a commit 23f3466
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/test/integration/IntegrationBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,38 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
assertEq(depositScalingFactors[i], WAD, err);
}
}

function assert_withdrawableSharesDecreasedByAtLeast(
User staker,
IStrategy[] memory strategies,
uint256[] memory originalShares,
uint256[] memory expectedDecreases,
string memory err
) internal view {
uint256[] memory currentShares = _getWithdrawableShares(staker, strategies);
for (uint i = 0; i < currentShares.length; i++) {
assertLt(currentShares[i], originalShares[i] - expectedDecreases[i], err);
}
}

function assert_withdrawableSharesDecreasedByAtLeast(
User staker,
IStrategy strategy,
uint256 originalShares,
uint256 expectedDecreases,
string memory err
) internal view {
return assert_withdrawableSharesDecreasedByAtLeast(staker, strategy.toArray(), originalShares.toArrayU256(), expectedDecreases.toArrayU256(), err);
}

function assert_withdrawbleShares_bc_AVS_Slash_State(
User staker,
IStrategy strategy,
uint256 expectedDecreases,
string memory err
) internal view {
}


/*******************************************************************************
SNAPSHOT ASSERTIONS
Expand Down
24 changes: 23 additions & 1 deletion src/test/integration/IntegrationChecks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "src/test/integration/users/User_M2.t.sol";

/// @notice Contract that provides utility functions to reuse common test blocks & checks
contract IntegrationCheckUtils is IntegrationBase {
using ArrayLib for IStrategy[];
using ArrayLib for *;
using SlashingLib for *;
using StdStyle for *;

Expand Down Expand Up @@ -998,4 +998,26 @@ contract IntegrationCheckUtils is IntegrationBase {
}
}
}

/*******************************************************************************
BC/AVS SLASHING CHECKS
*******************************************************************************/

function check_CompleteCheckPoint_AVSSLash_BCSLash_HandleRoundDown_State(
User staker,
uint40[] memory slashedValidators,
uint256 originalWithdrawableShares,
uint64 slashedBalanceGwei
) internal {
// Checkpoint State
check_CompleteCheckpoint_State(staker);
assert_Snap_Unchanged_Staker_DepositShares(staker, "staker shares should not have decreased");
assert_Snap_Removed_ActiveValidatorCount(staker, slashedValidators.length, "should have decreased active validator count");
assert_Snap_Removed_ActiveValidators(staker, slashedValidators, "exited validators should each be WITHDRAWN");

// Between the AVS slash and the BC slash, the shares should have decreased by at least the BC slash amount
assert_withdrawableSharesDecreasedByAtLeast(staker, BEACONCHAIN_ETH_STRAT, originalWithdrawableShares, uint256(slashedBalanceGwei * GWEI_TO_WEI), "should have decreased withdrawable shares by at least the BC slash amount");

assert_withdrawbleShare
}
}
99 changes: 99 additions & 0 deletions src/test/integration/tests/BC_AVS_Slashing.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;

import "src/test/integration/IntegrationChecks.t.sol";

contract Integration_BeaconChain_AVS_Ordering is IntegrationCheckUtils {
using ArrayLib for *;

AVS avs;
OperatorSet operatorSet;

User operator;
AllocateParams allocateParams;

User staker;
uint64 beaconBalanceGwei;
uint40[] validators;
IStrategy[] strategies;
uint[] initTokenBalances;
uint[] initDepositShares;

function _init() internal override {
_configAssetTypes(HOLDS_ETH);

// Create staker, operator, and avs
(staker, strategies, initTokenBalances) = _newRandomStaker();
(operator,,) = _newRandomOperator();
(avs,) = _newRandomAVS();

// 1. Deposit into strategies
(validators, beaconBalanceGwei) = staker.startValidators();
beaconChain.advanceEpoch_NoRewards();
staker.verifyWithdrawalCredentials(validators);
initDepositShares = _calculateExpectedShares(strategies, initTokenBalances);
check_Deposit_State(staker, strategies, initDepositShares);

// 2. Delegate staker to operator
staker.delegateTo(operator);
check_Delegation_State(staker, operator, strategies, initDepositShares);

// 3. Create an operator set containing the strategies held by the staker
operatorSet = avs.createOperatorSet(strategies);

// 4. Register operator to AVS
operator.registerForOperatorSet(operatorSet);
check_Registration_State_NoAllocation(operator, operatorSet, allStrats);

// 5. Allocate to the operator set
allocateParams = _genAllocation_AllAvailable(operator, operatorSet);
operator.modifyAllocations(allocateParams);
check_IncrAlloc_State_Slashable(operator, allocateParams);

_rollBlocksForCompleteAllocation(operator, operatorSet, strategies);
}

function testFuzz_avsSlash_bcSlash_checkpoint(uint24 _random) public rand(_random){
// 6. Slash Operator by AVS
SlashingParams memory slashingParams;
{
(IStrategy[] memory strategiesToSlash, uint256[] memory wadsToSlash) =
_randStrategiesAndWadsToSlash(operatorSet);
console.log("Strategies to slash: %s", strategiesToSlash.length);
slashingParams = avs.slashOperator(operator, operatorSet.id, strategiesToSlash, wadsToSlash);
assert_Snap_Allocations_Slashed(slashingParams, operatorSet, true, "operator allocations should be slashed");
assert_Snap_Unchanged_Staker_DepositShares(staker, "staker deposit shares should be unchanged after slashing");
assert_Snap_StakerWithdrawableShares_AfterSlash(staker, allocateParams, slashingParams, "staker deposit shares should be slashed");
}
uint[] memory sharesGotten = _getWithdrawableShares(staker, strategies);
console.log("Shares after avs slash: ", sharesGotten[0]);

// 7. Slash Staker on BC
uint64 slashedBalanceGwei = beaconChain.slashValidators(validators);
beaconChain.advanceEpoch_NoRewards();

console.log("slashedBalanceGwei: ", slashedBalanceGwei);

// 8. Checkpoint
staker.startCheckpoint();
check_StartCheckpoint_WithPodBalance_State(staker, beaconBalanceGwei - slashedBalanceGwei);
staker.completeCheckpoint();
uint64[] memory maxMagnitudes = _getMaxMagnitudes(operator, strategies);
console.log("maxMagnitudes: ", maxMagnitudes[0]);
uint64 bcsf = _getBeaconChainSlashingFactor(staker);
console.log("bcsf: ", bcsf);
sharesGotten = _getWithdrawableShares(staker, strategies);
console.log("Shares after bc slash: ", sharesGotten[0]);
check_CompleteCheckPoint_AVSSLash_BCSLash_HandleRoundDown_State(staker, validators, initDepositShares[0], slashedBalanceGwei);


// sharesGotten = _getWithdrawableShares(staker, strategies);
// console.log("Shares after bc slash: ", sharesGotten[0]);
// console.log("Test test test");

// Assert state
// assert_Snap_Unchanged_Staker_DepositShares(staker, "staker deposit shares should be unchanged after slashing");
// assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(staker, BEACONCHAIN_ETH_STRAT, slashedBalanceGwei * GWEI_TO_WEI, "should have decreased withdrawable shares by slashed amount");
}

}

0 comments on commit 23f3466

Please sign in to comment.