Skip to content

Commit

Permalink
test: 24-25
Browse files Browse the repository at this point in the history
  • Loading branch information
ypatil12 committed Feb 21, 2025
1 parent 71b49f1 commit b49723a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
36 changes: 34 additions & 2 deletions src/test/integration/IntegrationBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1309,11 +1309,42 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
}
}

function assert_Snap_StakerWithdrawableShares_AfterAVSAndBCSlash(
User staker,
uint256 originalWithdrawableShares,
AllocateParams memory allocateParams,
SlashingParams memory slashingParams,
string memory err
) internal {
uint[] memory curShares = _getWithdrawableShares(staker, allocateParams.strategies);
uint[] memory prevShares = _getPrevWithdrawableShares(staker, allocateParams.strategies);

// Only the BC slash is considered for
for (uint i = 0; i < allocateParams.strategies.length; i++) {
require(allocateParams.strategies.length == 1, "only beacon strategy supported");
IStrategy strat = allocateParams.strategies[i];

uint256 avsSlashedShares = 0;

if (slashingParams.strategies.contains(strat)) {
uint wadToSlash = slashingParams.wadsToSlash[slashingParams.strategies.indexOf(strat)];
avsSlashedShares = originalWithdrawableShares.mulWadRoundUp(allocateParams.newMagnitudes[i].mulWadRoundUp(wadToSlash));
}

uint256 postAVSSlashShares = originalWithdrawableShares - avsSlashedShares;

uint256 expectedDelta = postAVSSlashShares.mulWad(WAD - _getBeaconChainSlashingFactor(staker));

// TODO: bound this better
assertApproxEqAbs(prevShares[i] - expectedDelta, curShares[i], 1e4, err);
}
}

// Same as above, but when a BC slash occurs before an AVS slash
function assert_Snap_StakerWithdrawableShares_AfterBCAndAVSSlash(
User staker,
AllocateParams memory allocateParams,
SlashingParams memory slashingParams,
uint64 slashedBalanceGwei,
string memory err
) internal {
uint[] memory curShares = _getWithdrawableShares(staker, allocateParams.strategies);
Expand All @@ -1329,7 +1360,8 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
slashedShares = prevShares[i].mulWadRoundUp(allocateParams.newMagnitudes[i].mulWadRoundUp(wadToSlash));
}

assertApproxEqAbs(prevShares[i] - slashedShares, curShares[i], 1, err);
// TODO: bound this better
assertApproxEqAbs(prevShares[i] - slashedShares, curShares[i], 1e4, err);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/integration/IntegrationChecks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ contract IntegrationCheckUtils is IntegrationBase {
BC/AVS SLASHING CHECKS
*******************************************************************************/

function check_CompleteCheckPoint_AVSSLash_BCSLash_HandleRoundDown_State(
function check_CompleteCheckPoint_AfterAVSAndBCSlash(
User staker,
uint40[] memory slashedValidators,
uint256 originalWithdrawableShares,
Expand All @@ -1020,6 +1020,9 @@ contract IntegrationCheckUtils is IntegrationBase {
// 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");

//
// Calculate the withdrawable shares
assert_Snap_StakerWithdrawableShares_AfterAVSAndBCSlash(staker, originalWithdrawableShares, allocateParams, slashingParams, "should have decreased withdrawable shares correctly");
}


}
51 changes: 28 additions & 23 deletions src/test/integration/tests/BC_AVS_Slashing.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,42 @@ contract Integration_BeaconChain_AVS_Ordering is IntegrationCheckUtils {
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");
assert_Snap_StakerWithdrawableShares_AfterSlash(staker, allocateParams, slashingParams, "staker withdrawable 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);
uint64 slashedAmountGwei = beaconChain.slashValidators(validators);
beaconChain.advanceEpoch_NoRewards();

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

// 8. Checkpoint
staker.startCheckpoint();
check_StartCheckpoint_WithPodBalance_State(staker, beaconBalanceGwei - slashedBalanceGwei);
check_StartCheckpoint_WithPodBalance_State(staker, beaconBalanceGwei - slashedAmountGwei);
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");
check_CompleteCheckPoint_AfterAVSAndBCSlash(staker, validators, initDepositShares[0], slashedAmountGwei, allocateParams, slashingParams);
}

function testFuzz_bcSlash_checkpoint_avsSlash(uint24 _random) public rand(_random) {
// 6. Slash staker on BC
uint64 slashedAmountGwei = beaconChain.slashValidators(validators);
beaconChain.advanceEpoch_NoRewards();

// 7. Checkpoint
staker.startCheckpoint();
check_StartCheckpoint_WithPodBalance_State(staker, beaconBalanceGwei - slashedAmountGwei);
staker.completeCheckpoint();
check_CompleteCheckpoint_WithSlashing_State(staker, validators, slashedAmountGwei);

// 8. 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_AfterBCAndAVSSlash(staker, allocateParams, slashingParams, "staker withdrawable shares should be slashed");
}
}

}

0 comments on commit b49723a

Please sign in to comment.