diff --git a/x/ccv/provider/keeper/keeper.go b/x/ccv/provider/keeper/keeper.go index 816683279d..bd26d6b87d 100644 --- a/x/ccv/provider/keeper/keeper.go +++ b/x/ccv/provider/keeper/keeper.go @@ -1187,7 +1187,7 @@ func (k Keeper) IsOptIn(ctx sdk.Context, chainID string) bool { func (k Keeper) SetOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, blockHeight uint64, ) { store := ctx.KVStore(k.storeKey) @@ -1196,25 +1196,25 @@ func (k Keeper) SetOptedIn( blockHeightBytes := make([]byte, 8) binary.BigEndian.PutUint64(blockHeightBytes, blockHeight) - store.Set(types.OptedInKey(chainID, valAddress), blockHeightBytes) + store.Set(types.OptedInKey(chainID, providerAddr), blockHeightBytes) } func (k Keeper) DeleteOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) { store := ctx.KVStore(k.storeKey) - store.Delete(types.OptedInKey(chainID, valAddress)) + store.Delete(types.OptedInKey(chainID, providerAddr)) } func (k Keeper) IsOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) bool { store := ctx.KVStore(k.storeKey) - return store.Get(types.OptedInKey(chainID, valAddress)) != nil + return store.Get(types.OptedInKey(chainID, providerAddr)) != nil } func (k Keeper) GetOptedIn( @@ -1227,8 +1227,8 @@ func (k Keeper) GetOptedIn( for ; iterator.Valid(); iterator.Next() { optedInValidators = append(optedInValidators, OptedInValidator{ - ValAddress: iterator.Key()[len(key):], - BlockHeight: binary.BigEndian.Uint64(iterator.Value()), + ProviderAddr: types.NewProviderConsAddress(iterator.Key()[len(key):]), + BlockHeight: binary.BigEndian.Uint64(iterator.Value()), }) } @@ -1238,33 +1238,33 @@ func (k Keeper) GetOptedIn( func (k Keeper) SetToBeOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) { store := ctx.KVStore(k.storeKey) - store.Set(types.ToBeOptedInKey(chainID, valAddress), []byte{}) + store.Set(types.ToBeOptedInKey(chainID, providerAddr), []byte{}) } func (k Keeper) DeleteToBeOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) { store := ctx.KVStore(k.storeKey) - store.Delete(types.ToBeOptedInKey(chainID, valAddress)) + store.Delete(types.ToBeOptedInKey(chainID, providerAddr)) } func (k Keeper) IsToBeOptedIn( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) bool { store := ctx.KVStore(k.storeKey) - return store.Get(types.ToBeOptedInKey(chainID, valAddress)) != nil + return store.Get(types.ToBeOptedInKey(chainID, providerAddr)) != nil } func (k Keeper) GetToBeOptedIn( ctx sdk.Context, - chainID string) (valAddresses []sdk.ValAddress) { + chainID string) (addresses []types.ProviderConsAddress) { store := ctx.KVStore(k.storeKey) key := types.ChainIdWithLenKey(types.ToBeOptedInBytePrefix, chainID) @@ -1272,42 +1272,43 @@ func (k Keeper) GetToBeOptedIn( defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - valAddresses = append(valAddresses, iterator.Key()[len(key):]) + providerAddr := types.NewProviderConsAddress(iterator.Key()[len(key):]) + addresses = append(addresses, providerAddr) } - return valAddresses + return addresses } func (k Keeper) SetToBeOptedOut( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) { store := ctx.KVStore(k.storeKey) - store.Set(types.ToBeOptedOutKey(chainID, valAddress), []byte{}) + store.Set(types.ToBeOptedOutKey(chainID, providerAddr), []byte{}) } func (k Keeper) DeleteToBeOptedOut( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) { store := ctx.KVStore(k.storeKey) - store.Delete(types.ToBeOptedOutKey(chainID, valAddress)) + store.Delete(types.ToBeOptedOutKey(chainID, providerAddr)) } func (k Keeper) IsToBeOptedOut( ctx sdk.Context, chainID string, - valAddress sdk.ValAddress, + providerAddr types.ProviderConsAddress, ) bool { store := ctx.KVStore(k.storeKey) - return store.Get(types.ToBeOptedOutKey(chainID, valAddress)) != nil + return store.Get(types.ToBeOptedOutKey(chainID, providerAddr)) != nil } func (k Keeper) GetToBeOptedOut( ctx sdk.Context, - chainID string) (valAddresses []sdk.ValAddress) { + chainID string) (addresses []types.ProviderConsAddress) { store := ctx.KVStore(k.storeKey) key := types.ChainIdWithLenKey(types.ToBeOptedOutBytePrefix, chainID) @@ -1315,8 +1316,9 @@ func (k Keeper) GetToBeOptedOut( defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - valAddresses = append(valAddresses, iterator.Key()[len(key):]) + providerAddr := types.NewProviderConsAddress(iterator.Key()[len(key):]) + addresses = append(addresses, providerAddr) } - return valAddresses + return addresses } diff --git a/x/ccv/provider/keeper/keeper_test.go b/x/ccv/provider/keeper/keeper_test.go index 685df27191..6eb28675ba 100644 --- a/x/ccv/provider/keeper/keeper_test.go +++ b/x/ccv/provider/keeper/keeper_test.go @@ -3,7 +3,6 @@ package keeper_test import ( "bytes" "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/interchain-security/v4/x/ccv/provider/keeper" "sort" "testing" @@ -669,22 +668,22 @@ func TestGetOptedIn(t *testing.T) { expectedOptedInValidators := []keeper.OptedInValidator{ { - ValAddress: []byte("valAddr1"), - BlockHeight: 1, + ProviderAddr: types.NewProviderConsAddress([]byte("providerAddr1")), + BlockHeight: 1, }, { - ValAddress: []byte("valAddr2"), - BlockHeight: 2, + ProviderAddr: types.NewProviderConsAddress([]byte("providerAddr2")), + BlockHeight: 2, }, { - ValAddress: []byte("valAddr3"), - BlockHeight: 3, + ProviderAddr: types.NewProviderConsAddress([]byte("providerAddr3")), + BlockHeight: 3, }, } for _, expectedOptedInValidator := range expectedOptedInValidators { providerKeeper.SetOptedIn(ctx, "chainID", - expectedOptedInValidator.ValAddress, expectedOptedInValidator.BlockHeight) + expectedOptedInValidator.ProviderAddr, expectedOptedInValidator.BlockHeight) } actualOptedInValidators := providerKeeper.GetOptedIn(ctx, "chainID") @@ -695,7 +694,7 @@ func TestGetOptedIn(t *testing.T) { a := optedInValidators[i] b := optedInValidators[j] return a.BlockHeight < b.BlockHeight || - (a.BlockHeight == b.BlockHeight && bytes.Compare(a.ValAddress, b.ValAddress) < 0) + (a.BlockHeight == b.BlockHeight && bytes.Compare(a.ProviderAddr.ToSdkConsAddr(), b.ProviderAddr.ToSdkConsAddr()) < 0) }) } sortOptedInValidators(expectedOptedInValidators) @@ -709,25 +708,25 @@ func TestOptedIn(t *testing.T) { defer ctrl.Finish() optedInValidator := keeper.OptedInValidator{ - ValAddress: []byte("valAddr"), - BlockHeight: 1, + ProviderAddr: types.NewProviderConsAddress([]byte("providerAddr")), + BlockHeight: 1, } - require.False(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ValAddress)) - providerKeeper.SetOptedIn(ctx, "chainID", optedInValidator.ValAddress, optedInValidator.BlockHeight) - require.True(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ValAddress)) - providerKeeper.DeleteOptedIn(ctx, "chainID", optedInValidator.ValAddress) - require.False(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ValAddress)) + require.False(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ProviderAddr)) + providerKeeper.SetOptedIn(ctx, "chainID", optedInValidator.ProviderAddr, optedInValidator.BlockHeight) + require.True(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ProviderAddr)) + providerKeeper.DeleteOptedIn(ctx, "chainID", optedInValidator.ProviderAddr) + require.False(t, providerKeeper.IsOptedIn(ctx, "chainID", optedInValidator.ProviderAddr)) } func TestGetToBeOptedIn(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - expectedAddresses := []sdk.ValAddress{ - []byte("valAddr1"), - []byte("valAddr2"), - []byte("valAddr3")} + expectedAddresses := []types.ProviderConsAddress{ + types.NewProviderConsAddress([]byte("providerAddr1")), + types.NewProviderConsAddress([]byte("providerAddr2")), + types.NewProviderConsAddress([]byte("providerAddr3"))} for _, addr := range expectedAddresses { providerKeeper.SetToBeOptedIn(ctx, "chainID", addr) @@ -736,9 +735,45 @@ func TestGetToBeOptedIn(t *testing.T) { actualAddresses := providerKeeper.GetToBeOptedIn(ctx, "chainID") // sort addresses first to be able to compare - sortAddresses := func(addresses []sdk.ValAddress) { + sortAddresses := func(addresses []types.ProviderConsAddress) { sort.Slice(addresses, func(i int, j int) bool { - return bytes.Compare(addresses[i], addresses[j]) < 0 + a := addresses[i] + b := addresses[j] + return bytes.Compare(a.Address.Bytes(), b.Address.Bytes()) < 0 + }) + } + sortAddresses(expectedAddresses) + sortAddresses(actualAddresses) + require.Equal(t, expectedAddresses, actualAddresses) + + for _, addr := range expectedAddresses { + require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", addr)) + providerKeeper.DeleteToBeOptedIn(ctx, "chainID", addr) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", addr)) + } +} + +func TestBeOptedIn(t *testing.T) { + providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) + defer ctrl.Finish() + + expectedAddresses := []types.ProviderConsAddress{ + types.NewProviderConsAddress([]byte("providerAddr1")), + types.NewProviderConsAddress([]byte("providerAddr2")), + types.NewProviderConsAddress([]byte("providerAddr3"))} + + for _, addr := range expectedAddresses { + providerKeeper.SetToBeOptedIn(ctx, "chainID", addr) + } + + actualAddresses := providerKeeper.GetToBeOptedIn(ctx, "chainID") + + // sort addresses first to be able to compare + sortAddresses := func(addresses []types.ProviderConsAddress) { + sort.Slice(addresses, func(i int, j int) bool { + a := addresses[i] + b := addresses[j] + return bytes.Compare(a.Address.Bytes(), b.Address.Bytes()) < 0 }) } sortAddresses(expectedAddresses) @@ -757,23 +792,23 @@ func TestToBeOptedIn(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - valAddress := []byte("valAddr1") + providerAddr := types.NewProviderConsAddress([]byte("providerAddr1")) - require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) - providerKeeper.SetToBeOptedIn(ctx, "chainID", valAddress) - require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) - providerKeeper.DeleteToBeOptedIn(ctx, "chainID", valAddress) - require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) + providerKeeper.SetToBeOptedIn(ctx, "chainID", providerAddr) + require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) + providerKeeper.DeleteToBeOptedIn(ctx, "chainID", providerAddr) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) } func TestGetToBeOptedOut(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - expectedAddresses := []sdk.ValAddress{ - []byte("valAddr1"), - []byte("valAddr2"), - []byte("valAddr3")} + expectedAddresses := []types.ProviderConsAddress{ + types.NewProviderConsAddress([]byte("providerAddr1")), + types.NewProviderConsAddress([]byte("providerAddr2")), + types.NewProviderConsAddress([]byte("providerAddr3"))} for _, addr := range expectedAddresses { providerKeeper.SetToBeOptedOut(ctx, "chainID", addr) @@ -782,9 +817,11 @@ func TestGetToBeOptedOut(t *testing.T) { actualAddresses := providerKeeper.GetToBeOptedOut(ctx, "chainID") // sort addresses first to be able to compare - sortAddresses := func(addresses []sdk.ValAddress) { + sortAddresses := func(addresses []types.ProviderConsAddress) { sort.Slice(addresses, func(i int, j int) bool { - return bytes.Compare(addresses[i], addresses[j]) < 0 + a := addresses[i] + b := addresses[j] + return bytes.Compare(a.Address.Bytes(), b.Address.Bytes()) < 0 }) } sortAddresses(expectedAddresses) @@ -803,11 +840,11 @@ func TestToBeOptedOut(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - valAddress := []byte("valAddr1") + providerAddr := types.NewProviderConsAddress([]byte("providerAddr1")) - require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) - providerKeeper.SetToBeOptedOut(ctx, "chainID", valAddress) - require.True(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) - providerKeeper.DeleteToBeOptedOut(ctx, "chainID", valAddress) - require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) + require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) + providerKeeper.SetToBeOptedOut(ctx, "chainID", providerAddr) + require.True(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) + providerKeeper.DeleteToBeOptedOut(ctx, "chainID", providerAddr) + require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) } diff --git a/x/ccv/provider/keeper/key_assignment_test.go b/x/ccv/provider/keeper/key_assignment_test.go index 272d5f1593..4fab08c981 100644 --- a/x/ccv/provider/keeper/key_assignment_test.go +++ b/x/ccv/provider/keeper/key_assignment_test.go @@ -86,7 +86,7 @@ func TestGetAllValidatorConsumerPubKey(t *testing.T) { expectedGetAllOneConsumerOrder = append(expectedGetAllOneConsumerOrder, assignment) } } - // sorting by ValidatorConsumerPubKey.ValAddress + // sorting by ValidatorConsumerPubKey.ProviderAddr sort.Slice(expectedGetAllOneConsumerOrder, func(i, j int) bool { return bytes.Compare(expectedGetAllOneConsumerOrder[i].ProviderAddr, expectedGetAllOneConsumerOrder[j].ProviderAddr) == -1 }) @@ -228,7 +228,7 @@ func TestGetAllKeyAssignmentReplacements(t *testing.T) { ) } expectedGetAllOrder := testAssignments - // sorting by KeyAssignmentReplacement.ValAddress + // sorting by KeyAssignmentReplacement.ProviderAddr sort.Slice(expectedGetAllOrder, func(i, j int) bool { return bytes.Compare(expectedGetAllOrder[i].ProviderAddr, expectedGetAllOrder[j].ProviderAddr) == -1 }) diff --git a/x/ccv/provider/keeper/msg_server.go b/x/ccv/provider/keeper/msg_server.go index df79e5d4d7..7cac362a04 100644 --- a/x/ccv/provider/keeper/msg_server.go +++ b/x/ccv/provider/keeper/msg_server.go @@ -178,13 +178,17 @@ func (k msgServer) SubmitConsumerDoubleVoting(goCtx context.Context, msg *types. func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.MsgOptInResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - valAddress, err := sdk.ValAddressFromBech32(msg.ProviderAddr) + valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) + if err != nil { + return nil, err + } + providerAddr := types.NewProviderConsAddress(valAddress) if err != nil { return nil, err } if msg.ConsumerKey != "" { - k.Keeper.HandleOptIn(ctx, msg.ChainId, valAddress, &msg.ConsumerKey) + k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, &msg.ConsumerKey) ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( ccvtypes.EventTypeOptIn, @@ -193,7 +197,7 @@ func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.Msg ), }) } else { - k.Keeper.HandleOptIn(ctx, msg.ChainId, valAddress, nil) + k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, nil) ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( @@ -209,12 +213,16 @@ func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.Msg func (k msgServer) OptOut(goCtx context.Context, msg *types.MsgOptOut) (*types.MsgOptOutResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - valAddress, err := sdk.ValAddressFromBech32(msg.ProviderAddr) + valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) + if err != nil { + return nil, err + } + providerAddr := types.NewProviderConsAddress(valAddress) if err != nil { return nil, err } - k.Keeper.HandleOptOut(ctx, msg.ChainId, valAddress) + k.Keeper.HandleOptOut(ctx, msg.ChainId, providerAddr) ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( diff --git a/x/ccv/provider/keeper/partial_set_security.go b/x/ccv/provider/keeper/partial_set_security.go index 0211d4681e..1251eda372 100644 --- a/x/ccv/provider/keeper/partial_set_security.go +++ b/x/ccv/provider/keeper/partial_set_security.go @@ -2,21 +2,22 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/interchain-security/v4/x/ccv/provider/types" ) type OptedInValidator struct { - ValAddress sdk.ValAddress + ProviderAddr types.ProviderConsAddress // block height the validator opted in at BlockHeight uint64 } -func (k Keeper) HandleOptIn(ctx sdk.Context, chainID string, valAddress sdk.ValAddress, consumerKey *string) { - if k.IsToBeOptedOut(ctx, chainID, valAddress) { +func (k Keeper) HandleOptIn(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress, consumerKey *string) { + if k.IsToBeOptedOut(ctx, chainID, providerAddr) { // a validator to be opted in cancels out with a validator to be opted out - k.DeleteToBeOptedOut(ctx, chainID, valAddress) - } else if !k.IsToBeOptedIn(ctx, chainID, valAddress) && !k.IsOptedIn(ctx, chainID, valAddress) { + k.DeleteToBeOptedOut(ctx, chainID, providerAddr) + } else if !k.IsToBeOptedIn(ctx, chainID, providerAddr) && !k.IsOptedIn(ctx, chainID, providerAddr) { // a validator can only be set for opt in if it is not opted in and not already set for opt in - k.SetToBeOptedIn(ctx, chainID, valAddress) + k.SetToBeOptedIn(ctx, chainID, providerAddr) } if consumerKey != nil { @@ -24,12 +25,12 @@ func (k Keeper) HandleOptIn(ctx sdk.Context, chainID string, valAddress sdk.ValA } } -func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, valAddress sdk.ValAddress) { - if k.IsToBeOptedIn(ctx, chainID, valAddress) { +func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) { + if k.IsToBeOptedIn(ctx, chainID, providerAddr) { // a validator to be opted out cancels out a validator to be opted in - k.DeleteToBeOptedIn(ctx, chainID, valAddress) - } else if !k.IsToBeOptedOut(ctx, chainID, valAddress) && k.IsOptedIn(ctx, chainID, valAddress) { + k.DeleteToBeOptedIn(ctx, chainID, providerAddr) + } else if !k.IsToBeOptedOut(ctx, chainID, providerAddr) && k.IsOptedIn(ctx, chainID, providerAddr) { // a validator can only be set for opt out if it is opted in and not already set for opt out - k.SetToBeOptedOut(ctx, chainID, valAddress) + k.SetToBeOptedOut(ctx, chainID, providerAddr) } } diff --git a/x/ccv/provider/keeper/partial_set_security_test.go b/x/ccv/provider/keeper/partial_set_security_test.go index 0bc8993270..34e99f61f9 100644 --- a/x/ccv/provider/keeper/partial_set_security_test.go +++ b/x/ccv/provider/keeper/partial_set_security_test.go @@ -2,6 +2,7 @@ package keeper_test import ( testkeeper "github.com/cosmos/interchain-security/v4/testutil/keeper" + "github.com/cosmos/interchain-security/v4/x/ccv/provider/types" "github.com/stretchr/testify/require" "testing" ) @@ -10,38 +11,38 @@ func TestHandleOptIn(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - valAddress := []byte("valAddr") + providerAddr := types.NewProviderConsAddress([]byte("providerAddr")) - // if validator (`valAddress`) is to be opted out, then we cancel that the validator is about + // if validator (`providerAddr`) is to be opted out, then we cancel that the validator is about // to be opted out and do not consider the validator to opt in - providerKeeper.SetToBeOptedOut(ctx, "chainID", valAddress) - require.True(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) - providerKeeper.HandleOptIn(ctx, "chainID", valAddress, nil) - require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) - require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) - - // if validator (`valAddress`) is already opted in, then the validator cannot be opted in - providerKeeper.SetOptedIn(ctx, "chainID", valAddress, 1) - providerKeeper.HandleOptIn(ctx, "chainID", valAddress, nil) - require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) + providerKeeper.SetToBeOptedOut(ctx, "chainID", providerAddr) + require.True(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) + providerKeeper.HandleOptIn(ctx, "chainID", providerAddr, nil) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) + require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) + + // if validator (`providerAddr`) is already opted in, then the validator cannot be opted in + providerKeeper.SetOptedIn(ctx, "chainID", providerAddr, 1) + providerKeeper.HandleOptIn(ctx, "chainID", providerAddr, nil) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) } func TestHandleOptOut(t *testing.T) { providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() - valAddress := []byte("valAddr") + providerAddr := types.NewProviderConsAddress([]byte("providerAddr")) - // if validator (`valAddress`) is to be opted in, then we cancel that the validator is about + // 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", valAddress) - require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) - providerKeeper.HandleOptOut(ctx, "chainID", valAddress) - require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) - require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", valAddress)) - - // if validator (`valAddress`) is not opted in, then the validator cannot be opted out - providerKeeper.DeleteOptedIn(ctx, "chainID", valAddress) - providerKeeper.HandleOptOut(ctx, "chainID", valAddress) - require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", valAddress)) + providerKeeper.SetToBeOptedIn(ctx, "chainID", providerAddr) + require.True(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) + providerKeeper.HandleOptOut(ctx, "chainID", providerAddr) + require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) + require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) + + // if validator (`providerAddr`) is not opted in, then the validator cannot be opted out + providerKeeper.DeleteOptedIn(ctx, "chainID", providerAddr) + providerKeeper.HandleOptOut(ctx, "chainID", providerAddr) + require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) } diff --git a/x/ccv/provider/types/keys.go b/x/ccv/provider/types/keys.go index c42cbad4b3..20824454cb 100644 --- a/x/ccv/provider/types/keys.go +++ b/x/ccv/provider/types/keys.go @@ -538,21 +538,21 @@ func TopNKey(chainID string) []byte { } // OptedInKey returns the key of consumer chain `chainID` and validator with `providerAddr` -func OptedInKey(chainID string, valAddress sdk.ValAddress) []byte { +func OptedInKey(chainID string, providerAddr ProviderConsAddress) []byte { prefix := ChainIdWithLenKey(OptedInBytePrefix, chainID) - return append(prefix, valAddress...) + return append(prefix, providerAddr.ToSdkConsAddr().Bytes()...) } // ToBeOptedInKey returns the key of consumer chain `chainID` and validator with `providerAddr` -func ToBeOptedInKey(chainID string, valAddress sdk.ValAddress) []byte { +func ToBeOptedInKey(chainID string, providerAddr ProviderConsAddress) []byte { prefix := ChainIdWithLenKey(ToBeOptedInBytePrefix, chainID) - return append(prefix, valAddress...) + return append(prefix, providerAddr.ToSdkConsAddr().Bytes()...) } // ToBeOptedOutKey returns the key of consumer chain `chainID` and validator with `providerAddr` -func ToBeOptedOutKey(chainID string, valAddress sdk.ValAddress) []byte { +func ToBeOptedOutKey(chainID string, providerAddr ProviderConsAddress) []byte { prefix := ChainIdWithLenKey(ToBeOptedOutBytePrefix, chainID) - return append(prefix, valAddress...) + return append(prefix, providerAddr.ToSdkConsAddr().Bytes()...) } // diff --git a/x/ccv/provider/types/provider.pb.go b/x/ccv/provider/types/provider.pb.go index 89339d12ab..fd9d63bc7e 100644 --- a/x/ccv/provider/types/provider.pb.go +++ b/x/ccv/provider/types/provider.pb.go @@ -5813,7 +5813,7 @@ func (m *KeyAssignmentReplacement) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var byteLen int for shift := uint(0); ; shift += 7 { @@ -5984,7 +5984,7 @@ func (m *ValidatorConsumerPubKey) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var byteLen int for shift := uint(0); ; shift += 7 { @@ -6170,7 +6170,7 @@ func (m *ValidatorByConsumerAddr) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var byteLen int for shift := uint(0); ; shift += 7 { diff --git a/x/ccv/provider/types/tx.pb.go b/x/ccv/provider/types/tx.pb.go index 35691c4f91..647f5f49dc 100644 --- a/x/ccv/provider/types/tx.pb.go +++ b/x/ccv/provider/types/tx.pb.go @@ -1272,7 +1272,7 @@ func (m *MsgAssignConsumerKey) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -1840,7 +1840,7 @@ func (m *MsgOptIn) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -2036,7 +2036,7 @@ func (m *MsgOptOut) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ValAddress", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field ProviderAddr", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 {