Skip to content

Commit

Permalink
add contraint such that opt out only works if the chain is running
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Feb 7, 2024
1 parent 78e12c8 commit 656adc2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions x/ccv/provider/keeper/partial_set_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (k Keeper) HandleOptIn(ctx sdk.Context, chainID string, providerAddr types.
if !k.IsConsumerProposedOrRegistered(ctx, chainID) {
return errorsmod.Wrapf(
types.ErrUnknownConsumerChainId,
"opting in to an unknown consumer chain id: %s", chainID)
"opting in to an unknown consumer chain, with id: %s", chainID)
}

if k.IsToBeOptedOut(ctx, chainID, providerAddr) {
Expand Down Expand Up @@ -49,10 +49,12 @@ func (k Keeper) HandleOptIn(ctx sdk.Context, chainID string, providerAddr types.
}

func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) error {
if !k.IsConsumerProposedOrRegistered(ctx, chainID) {
if _, found := k.GetConsumerClientId(ctx, chainID); !found {
// A validator can only opt out from a running chain. We check this by checking the consumer client id, because
// `SetConsumerClientId` is set when the chain starts in `CreateConsumerClientInCachedCtx` of `BeginBlockInit`.
return errorsmod.Wrapf(
types.ErrUnknownConsumerChainId,
"opting out of an unknown consumer chain id: %s", chainID)
"opting out of an unknown or not running consumer chain, with id: %s", chainID)
}

if k.IsToBeOptedIn(ctx, chainID, providerAddr) {
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/partial_set_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func TestHandleOptOut(t *testing.T) {

providerAddr := types.NewProviderConsAddress([]byte("providerAddr"))

// trying to opt out from a non-proposed and non-registered chain returns an error
// trying to opt out from a not running chain returns an error
require.Error(t, providerKeeper.HandleOptOut(ctx, "unknownChainID", providerAddr))

// if validator (`providerAddr`) is to be opted in, then we cancel that the validator is about
// to be opted out and do not consider the validator to opt out
providerKeeper.SetToBeOptedIn(ctx, "chainID", providerAddr)
providerKeeper.SetProposedConsumerChain(ctx, "chainID", 1)
providerKeeper.SetConsumerClientId(ctx, "chainID", "clientID")
require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr))
err := providerKeeper.HandleOptOut(ctx, "chainID", providerAddr)
require.NoError(t, err)
Expand Down

0 comments on commit 656adc2

Please sign in to comment.