Skip to content

Commit

Permalink
Revert "changed cons to val address"
Browse files Browse the repository at this point in the history
This reverts commit a32e882.
  • Loading branch information
insumity committed Feb 5, 2024
1 parent a32e882 commit b0d2337
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 120 deletions.
54 changes: 28 additions & 26 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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()),
})
}

Expand All @@ -1238,85 +1238,87 @@ 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)
iterator := sdk.KVStorePrefixIterator(store, key)
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)
iterator := sdk.KVStorePrefixIterator(store, key)
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
}
117 changes: 77 additions & 40 deletions x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/key_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -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
})
Expand Down
Loading

0 comments on commit b0d2337

Please sign in to comment.