Skip to content

Commit

Permalink
take into account Philip's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Sep 26, 2023
1 parent f8633b5 commit f96f1bc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions tests/integration/double_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (s *CCVTestSuite) TestHandleConsumerDoubleVoting() {
// we create two votes that only differ by their Block IDs and
// signed them using the same validator private key and chain ID
// of the consumer chain
"valid double voting evidence 1 - should pass",
"valid double voting evidence - should pass",
&tmtypes.DuplicateVoteEvidence{
VoteA: consuVote,
VoteB: consuBadVote,
Expand All @@ -141,7 +141,7 @@ func (s *CCVTestSuite) TestHandleConsumerDoubleVoting() {
},
{
// create a double voting evidence using the provider validator key
"valid double voting evidence 2 - should pass",
"valid double voting evidence - should not pass because validator tombstoned in the previous test case",
&tmtypes.DuplicateVoteEvidence{
VoteA: provVote,
VoteB: provBadVote,
Expand All @@ -151,7 +151,7 @@ func (s *CCVTestSuite) TestHandleConsumerDoubleVoting() {
},
s.consumerChain.ChainID,
provVal.PubKey,
true,
false,
},
}

Expand Down
4 changes: 3 additions & 1 deletion x/ccv/provider/keeper/punish_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ func (k Keeper) JailAndTombstoneValidator(ctx sdk.Context, providerAddr types.Pr
}

// ComputePowerToSlash computes the power to be slashed based on the tokens in non-matured `undelegations` and
// `redelegations`, as well as the current `power` of the validator
// `redelegations`, as well as the current `power` of the validator.
// Note that this method does not perform any slashing.
func (k Keeper) ComputePowerToSlash(ctx sdk.Context, validator stakingtypes.Validator, undelegations []stakingtypes.UnbondingDelegation,
redelegations []stakingtypes.Redelegation, power int64, powerReduction sdk.Int,
) int64 {
// compute the total numbers of tokens currently being undelegated
undelegationsInTokens := sdk.NewInt(0)

// Note that we use a **cached** context to avoid any actual slashing of undelegations or redelegations.
cachedCtx, _ := ctx.CacheContext()
for _, u := range undelegations {
amountSlashed := k.stakingKeeper.SlashUnbondingDelegation(cachedCtx, u, 0, sdk.NewDec(1))
Expand Down
8 changes: 8 additions & 0 deletions x/ccv/provider/keeper/punish_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,17 @@ func TestComputePowerToSlash(t *testing.T) {
}).AnyTimes(),
)

tokensBeforeCall := validator.GetTokens()
delegatorSharesBeforeCall := validator.GetDelegatorShares()

actualPower := providerKeeper.ComputePowerToSlash(ctx, validator,
tc.undelegations, tc.redelegations, tc.power, tc.powerReduction)

// safeguard check that validator remains unmodified after a call to `ComputePowerToSlash`
// `ComputePowerToSlash` only computes the power and does not modify the state of the system in any way
require.Equal(t, tokensBeforeCall, validator.GetTokens())
require.Equal(t, delegatorSharesBeforeCall, validator.GetDelegatorShares())

if tc.expectedPower != actualPower {
require.Fail(t, fmt.Sprintf("\"%s\" failed", tc.name),
"expected is %d but actual is %d", tc.expectedPower, actualPower)
Expand Down

0 comments on commit f96f1bc

Please sign in to comment.