Skip to content

Commit

Permalink
test: t-26
Browse files Browse the repository at this point in the history
  • Loading branch information
ypatil12 committed Feb 21, 2025
1 parent b49723a commit ce78b08
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
51 changes: 49 additions & 2 deletions src/test/integration/IntegrationBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
uint256 expectedDelta = postAVSSlashShares.mulWad(WAD - _getBeaconChainSlashingFactor(staker));

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

Expand All @@ -1361,7 +1361,7 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
}

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

Expand Down Expand Up @@ -1581,6 +1581,24 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
}
}

/// @dev This is currently used
/// TODO: Figure out how to bound this better
function assert_Snap_Added_Staker_WithdrawableSharesAtLeast(
User staker,
IStrategy[] memory strategies,
uint[] memory addedShares,
string memory err
) internal {
uint[] memory curShares = _getStakerWithdrawableShares(staker, strategies);
// Use timewarp to get previous staker shares
uint[] memory prevShares = _getPrevStakerWithdrawableShares(staker, strategies);

// For each strategy, check (prev - removed == cur)
for (uint i = 0; i < strategies.length; i++) {
assertApproxEqAbs(prevShares[i] + addedShares[i], curShares[i], 1e3, err);
}
}

/// @dev Check that the staker's withdrawable shares have decreased by `removedShares`
function assert_Snap_Removed_Staker_WithdrawableShares(
User staker,
Expand Down Expand Up @@ -1656,6 +1674,25 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
}
}

/// @dev Check that the staker's withdrawable shares have decreased by at least `removedShares`
/// @dev Used to handle overslashing of beacon chain with AVS slashings
function assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(
User staker,
IStrategy[] memory strategies,
uint[] memory removedShares,
uint errBound,
string memory err
) internal {
uint[] memory curShares = _getStakerWithdrawableShares(staker, strategies);
// Use timewarp to get previous staker shares
uint[] memory prevShares = _getPrevStakerWithdrawableShares(staker, strategies);

// For each strategy, check diff between (prev-removed) and curr is at most 1 gwei
for (uint i = 0; i < strategies.length; i++) {
assertApproxEqAbs(prevShares[i] - removedShares[i], curShares[i], errBound, err);
}
}

function assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(
User staker,
IStrategy strat,
Expand All @@ -1665,6 +1702,16 @@ abstract contract IntegrationBase is IntegrationDeployer, TypeImporter {
assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(staker, strat.toArray(), removedShares.toArrayU256(), err);
}

function assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(
User staker,
IStrategy strat,
uint removedShares,
uint errBound,
string memory err
) internal {
assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(staker, strat.toArray(), removedShares.toArrayU256(), errBound, err);
}

function assert_Snap_Delta_StakerShares(
User staker,
IStrategy[] memory strategies,
Expand Down
16 changes: 14 additions & 2 deletions src/test/integration/IntegrationChecks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,19 @@ contract IntegrationCheckUtils is IntegrationBase {

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


function check_CompleteCheckpoint_WithAVSAndBCSlashing_HandleRoundDown_State(
User staker,
uint40[] memory slashedValidators,
uint64 slashedAmountGwei
) internal {
check_CompleteCheckpoint_State(staker);

assert_Snap_Unchanged_Staker_DepositShares(staker, "staker shares should not have decreased");
// TODO: bound this better
assert_Snap_Removed_Staker_WithdrawableShares_AtLeast(staker, BEACONCHAIN_ETH_STRAT, slashedAmountGwei * GWEI_TO_WEI, 1e11, "should have decreased withdrawable shares by at least slashed amount");
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");
}
}
32 changes: 32 additions & 0 deletions src/test/integration/tests/BC_AVS_Slashing.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,36 @@ contract Integration_BeaconChain_AVS_Ordering is IntegrationCheckUtils {
}
}

function testFuzz_avsSlash_verifyValidator_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 withdrawable shares should be slashed");
}

// 7. Verify Validator
cheats.deal(address(staker), 32 ether);
(uint40[] memory newValidators, uint64 addedBeaconBalanceGwei) = staker.startValidators();
beaconChain.advanceEpoch_NoRewards();
staker.verifyWithdrawalCredentials(newValidators);
check_VerifyWC_State(staker, newValidators, addedBeaconBalanceGwei);
assert_Snap_Added_Staker_WithdrawableSharesAtLeast(staker, BEACONCHAIN_ETH_STRAT.toArray(), uint256(addedBeaconBalanceGwei * GWEI_TO_WEI).toArrayU256(), "staker withdrawable shares should increase by the added beacon balance");

// 8. Slash first validators on BC
uint64 slashedAmountGwei = beaconChain.slashValidators(validators);
beaconChain.advanceEpoch_NoRewards();

// 9. Checkpoint
staker.startCheckpoint();
check_StartCheckpoint_WithPodBalance_State(staker, beaconBalanceGwei - slashedAmountGwei);
staker.completeCheckpoint();
check_CompleteCheckpoint_WithAVSAndBCSlashing_HandleRoundDown_State(staker, validators, slashedAmountGwei);
}

}

0 comments on commit ce78b08

Please sign in to comment.