Skip to content

Commit

Permalink
refactor: change byte prefixing scheme for provider (#2061)
Browse files Browse the repository at this point in the history
* replace iota with constant map

* rename keys in the constant map with byte prefixes

* use constants for key names

* remove non-determinism from tests

* make mustGetKeyPrefix local

* fix docstrings

* remove changes from ADRs
  • Loading branch information
mpoke authored Jul 24, 2024
1 parent 27acc8a commit dbaf024
Show file tree
Hide file tree
Showing 14 changed files with 679 additions and 378 deletions.
8 changes: 4 additions & 4 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPacke
// GetAllPacketMaturityTimes returns a slice of all PacketMaturityTimes, sorted by maturity times.
//
// Note that PacketMaturityTimes are stored under keys with the following format:
// PacketMaturityTimeBytePrefix | maturityTime.UnixNano() | vscID
// PacketMaturityTimeKeyPrefix | maturityTime.UnixNano() | vscID
// Thus, the returned array is in ascending order of maturityTimes.
// If two entries have the same maturityTime, then they are ordered by vscID.
func (k Keeper) GetAllPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []types.MaturingVSCPacket) {
Expand Down Expand Up @@ -489,7 +489,7 @@ func (k Keeper) DeleteHeightValsetUpdateID(ctx sdk.Context, height uint64) {
// GetAllHeightToValsetUpdateIDs returns a list of all the block heights to valset update IDs in the store
//
// Note that the block height to vscID mapping is stored under keys with the following format:
// HeightValsetUpdateIDBytePrefix | height
// HeightValsetUpdateIDKeyPrefix | height
// Thus, the returned array is in ascending order of heights.
func (k Keeper) GetAllHeightToValsetUpdateIDs(ctx sdk.Context) (heightToValsetUpdateIDs []types.HeightToValsetUpdateID) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -531,7 +531,7 @@ func (k Keeper) DeleteOutstandingDowntime(ctx sdk.Context, address sdk.ConsAddre
// GetAllOutstandingDowntimes gets an array of the validator addresses of outstanding downtime flags
//
// Note that the outstanding downtime flags are stored under keys with the following format:
// OutstandingDowntimeBytePrefix | consAddress
// OutstandingDowntimeKeyPrefix | consAddress
// Thus, the returned array is in ascending order of consAddresses.
func (k Keeper) GetAllOutstandingDowntimes(ctx sdk.Context) (downtimes []types.OutstandingDowntime) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -580,7 +580,7 @@ func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) {
// GetAllCCValidator returns all cross-chain validators
//
// Note that the cross-chain validators are stored under keys with the following format:
// CrossChainValidatorBytePrefix | address
// CrossChainValidatorKeyPrefix | address
// Thus, the returned array is in ascending order of addresses.
func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChainValidator) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k Keeper) DeleteConsumerRewardDenom(

func (k Keeper) GetAllConsumerRewardDenoms(ctx sdk.Context) (consumerRewardDenoms []string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ConsumerRewardDenomsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.ConsumerRewardDenomsKeyPrefix())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()[1:]
Expand Down
3 changes: 1 addition & 2 deletions x/ccv/provider/keeper/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
tmtypes "github.com/cometbft/cometbft/types"

testkeeper "github.com/cosmos/interchain-security/v5/testutil/keeper"
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
)

Expand Down Expand Up @@ -54,7 +53,7 @@ func TestComputeConsumerTotalVotingPower(t *testing.T) {
keeper.SetConsumerValidator(
ctx,
chainID,
types.ConsumerValidator{
providertypes.ConsumerValidator{
ProviderConsAddr: val.Address,
Power: val.VotingPower,
},
Expand Down
3 changes: 1 addition & 2 deletions x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/golang/mock/gomock"

sdk "github.com/cosmos/cosmos-sdk/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
cryptotestutil "github.com/cosmos/interchain-security/v5/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/v5/testutil/keeper"
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
Expand All @@ -22,7 +21,7 @@ import (
func TestQueryAllPairsValConAddrByConsumerChainID(t *testing.T) {
chainID := consumer

providerConsAddress, err := sdktypes.ConsAddressFromBech32("cosmosvalcons1wpex7anfv3jhystyv3eq20r35a")
providerConsAddress, err := sdk.ConsAddressFromBech32("cosmosvalcons1wpex7anfv3jhystyv3eq20r35a")
require.NoError(t, err)
providerAddr := types.NewProviderConsAddress(providerConsAddress)

Expand Down
3 changes: 1 addition & 2 deletions x/ccv/provider/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v5/x/ccv/types"
)
Expand Down Expand Up @@ -95,7 +94,7 @@ func (h Hooks) AfterUnbondingInitiated(goCtx context.Context, id uint64) error {

// get all consumers where the validator is in the validator set
for _, chainID := range h.k.GetAllRegisteredConsumerChainIDs(ctx) {
if h.k.IsConsumerValidator(ctx, chainID, types.NewProviderConsAddress(consAddr)) {
if h.k.IsConsumerValidator(ctx, chainID, providertypes.NewProviderConsAddress(consAddr)) {
consumerChainIDS = append(consumerChainIDS, chainID)
}
}
Expand Down
63 changes: 32 additions & 31 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ func (k Keeper) DeleteProposedConsumerChainInStore(ctx sdk.Context, proposalID u
// GetAllProposedConsumerChainIDs returns the proposed chainID of all gov consumerAddition proposals that are still in the voting period.
func (k Keeper) GetAllProposedConsumerChainIDs(ctx sdk.Context) []types.ProposedChain {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ProposedConsumerChainByteKey})
iterator := storetypes.KVStorePrefixIterator(store, types.ProposedConsumerChainKeyPrefix())
defer iterator.Close()

proposedChains := []types.ProposedChain{}
for ; iterator.Valid(); iterator.Next() {
proposalID, err := types.ParseProposedConsumerChainKey(types.ProposedConsumerChainByteKey, iterator.Key())
proposalID, err := types.ParseProposedConsumerChainKey(iterator.Key())
if err != nil {
panic(fmt.Errorf("proposed chains cannot be parsed: %w", err))
}
Expand Down Expand Up @@ -270,13 +270,13 @@ func (k Keeper) GetAllPendingConsumerChainIDs(ctx sdk.Context) []string {
// created IBC clients. Consumer chains with created clients are also referred to as registered.
//
// Note that the registered consumer chains are stored under keys with the following format:
// ChainToClientBytePrefix | chainID
// ChainToClientKeyPrefix | chainID
// Thus, the returned array is in ascending order of chainIDs.
func (k Keeper) GetAllRegisteredConsumerChainIDs(ctx sdk.Context) []string {
chainIDs := []string{}

store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ChainToClientBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.ChainToClientKeyPrefix())
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -315,11 +315,11 @@ func (k Keeper) DeleteChannelToChain(ctx sdk.Context, channelID string) {
//
// Note that mapping from CCV channel IDs to consumer chainIDs
// is stored under keys with the following format:
// ChannelToChainBytePrefix | channelID
// ChannelToChainKeyPrefix | channelID
// Thus, the returned array is in ascending order of channelIDs.
func (k Keeper) GetAllChannelToChains(ctx sdk.Context) (channels []types.ChannelToChain) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ChannelToChainBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.ChannelToChainKeyPrefix())
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -486,11 +486,11 @@ func (k Keeper) DeleteUnbondingOp(ctx sdk.Context, id uint64) {
// is waiting on.
//
// Note that UnbondingOps are stored under keys with the following format:
// UnbondingOpBytePrefix | ID
// UnbondingOpKeyPrefix | ID
// Thus, the iteration is in ascending order of IDs.
func (k Keeper) GetAllUnbondingOps(ctx sdk.Context) (ops []types.UnbondingOp) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.UnbondingOpBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.UnbondingOpKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -577,11 +577,12 @@ func (k Keeper) SetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint6
// VSCMaturedPackets from a consumer with chainID.
//
// Note that the unbonding indexes for a given chainID are stored under keys with the following format:
// UnbondingOpIndexBytePrefix | len(chainID) | chainID | vscID
// UnbondingOpIndexKeyPrefix | len(chainID) | chainID | vscID
// Thus, the returned array is in ascending order of vscIDs.
func (k Keeper) GetAllUnbondingOpIndexes(ctx sdk.Context, chainID string) (indexes []types.VscUnbondingOps) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.UnbondingOpIndexBytePrefix, chainID))
bytePrefix := types.UnbondingOpIndexKeyPrefix()
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(bytePrefix, chainID))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -776,11 +777,11 @@ func (k Keeper) GetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint6
// GetAllValsetUpdateBlockHeights gets all the block heights for all valset updates
//
// Note that the mapping from vscIDs to block heights is stored under keys with the following format:
// ValsetUpdateBlockHeightBytePrefix | vscID
// ValsetUpdateBlockHeightKeyPrefix | vscID
// Thus, the returned array is in ascending order of vscIDs.
func (k Keeper) GetAllValsetUpdateBlockHeights(ctx sdk.Context) (valsetUpdateBlockHeights []types.ValsetUpdateIdToHeight) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.ValsetUpdateBlockHeightBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.ValsetUpdateBlockHeightKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -982,11 +983,11 @@ func (k Keeper) DeleteInitTimeoutTimestamp(ctx sdk.Context, chainID string) {
// GetAllInitTimeoutTimestamps gets all init timeout timestamps in the store.
//
// Note that the init timeout timestamps are stored under keys with the following format:
// InitTimeoutTimestampBytePrefix | chainID
// InitTimeoutTimestampKeyPrefix | chainID
// Thus, the returned array is in ascending order of chainIDs (NOT in timestamp order).
func (k Keeper) GetAllInitTimeoutTimestamps(ctx sdk.Context) (initTimeoutTimestamps []types.InitTimeoutTimestamp) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.InitTimeoutTimestampBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, types.InitTimeoutTimestampKeyPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -1046,11 +1047,11 @@ func (k Keeper) DeleteVscSendTimestamp(ctx sdk.Context, chainID string, vscID ui
// GetAllVscSendTimestamps gets an array of all the vsc send timestamps of the given chainID.
//
// Note that the vsc send timestamps of a given chainID are stored under keys with the following format:
// VscSendTimestampBytePrefix | len(chainID) | chainID | vscID
// VscSendTimestampKeyPrefix | len(chainID) | chainID | vscID
// Thus, the iteration is in ascending order of vscIDs, and as a result in send timestamp order.
func (k Keeper) GetAllVscSendTimestamps(ctx sdk.Context, chainID string) (vscSendTimestamps []types.VscSendTimestamp) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendingTimestampKeyPrefix(), chainID))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -1079,7 +1080,7 @@ func (k Keeper) GetAllVscSendTimestamps(ctx sdk.Context, chainID string) (vscSen
// DeleteVscSendTimestampsForConsumer deletes all VSC send timestamps for a given consumer chain
func (k Keeper) DeleteVscSendTimestampsForConsumer(ctx sdk.Context, consumerChainID string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, consumerChainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendingTimestampKeyPrefix(), consumerChainID))

defer iterator.Close()

Expand All @@ -1097,7 +1098,7 @@ func (k Keeper) DeleteVscSendTimestampsForConsumer(ctx sdk.Context, consumerChai
// GetFirstVscSendTimestamp gets the vsc send timestamp with the lowest vscID for the given chainID.
func (k Keeper) GetFirstVscSendTimestamp(ctx sdk.Context, chainID string) (vscSendTimestamp types.VscSendTimestamp, found bool) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendTimestampBytePrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.VscSendingTimestampKeyPrefix(), chainID))
defer iterator.Close()

if iterator.Valid() {
Expand Down Expand Up @@ -1243,7 +1244,7 @@ func (k Keeper) GetAllOptedIn(
chainID string,
) (providerConsAddresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.OptedInBytePrefix, chainID)
key := types.ChainIdWithLenKey(types.OptedInKeyPrefix(), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand All @@ -1260,7 +1261,7 @@ func (k Keeper) DeleteAllOptedIn(
chainID string,
) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.OptedInBytePrefix, chainID)
key := types.ChainIdWithLenKey(types.OptedInKeyPrefix(), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)

var keysToDel [][]byte
Expand Down Expand Up @@ -1323,7 +1324,7 @@ func (k Keeper) GetAllCommissionRateValidators(
chainID string,
) (addresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.ConsumerCommissionRatePrefix, chainID)
key := types.ChainIdWithLenKey(types.ConsumerCommissionRateKeyPrefix(), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand Down Expand Up @@ -1425,7 +1426,7 @@ func (k Keeper) SetAllowlist(
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.AllowlistCapKey(chainID, providerAddr), []byte{})
store.Set(types.AllowlistKey(chainID, providerAddr), []byte{})
}

// GetAllowList returns all allowlisted validators
Expand All @@ -1434,7 +1435,7 @@ func (k Keeper) GetAllowList(
chainID string,
) (providerConsAddresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.AllowlistPrefix, chainID)
key := types.ChainIdWithLenKey(types.AllowlistKeyPrefix(), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand All @@ -1452,14 +1453,14 @@ func (k Keeper) IsAllowlisted(
providerAddr types.ProviderConsAddress,
) bool {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.AllowlistCapKey(chainID, providerAddr))
bz := store.Get(types.AllowlistKey(chainID, providerAddr))
return bz != nil
}

// DeleteAllowlist deletes all allowlisted validators
func (k Keeper) DeleteAllowlist(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.AllowlistPrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.AllowlistKeyPrefix(), chainID))
defer iterator.Close()

keysToDel := [][]byte{}
Expand All @@ -1475,7 +1476,7 @@ func (k Keeper) DeleteAllowlist(ctx sdk.Context, chainID string) {
// IsAllowlistEmpty returns `true` if no validator is allowlisted on chain `chainID`
func (k Keeper) IsAllowlistEmpty(ctx sdk.Context, chainID string) bool {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.AllowlistPrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.AllowlistKeyPrefix(), chainID))
defer iterator.Close()

return !iterator.Valid()
Expand All @@ -1488,7 +1489,7 @@ func (k Keeper) SetDenylist(
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.DenylistCapKey(chainID, providerAddr), []byte{})
store.Set(types.DenylistKey(chainID, providerAddr), []byte{})
}

// GetDenyList returns all denylisted validators
Expand All @@ -1497,7 +1498,7 @@ func (k Keeper) GetDenyList(
chainID string,
) (providerConsAddresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.DenylistPrefix, chainID)
key := types.ChainIdWithLenKey(types.DenylistKeyPrefix(), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand All @@ -1515,14 +1516,14 @@ func (k Keeper) IsDenylisted(
providerAddr types.ProviderConsAddress,
) bool {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DenylistCapKey(chainID, providerAddr))
bz := store.Get(types.DenylistKey(chainID, providerAddr))
return bz != nil
}

// DeleteDenylist deletes all denylisted validators
func (k Keeper) DeleteDenylist(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.DenylistPrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.DenylistKeyPrefix(), chainID))
defer iterator.Close()

keysToDel := [][]byte{}
Expand All @@ -1538,7 +1539,7 @@ func (k Keeper) DeleteDenylist(ctx sdk.Context, chainID string) {
// IsDenylistEmpty returns `true` if no validator is denylisted on chain `chainID`
func (k Keeper) IsDenylistEmpty(ctx sdk.Context, chainID string) bool {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.DenylistPrefix, chainID))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.DenylistKeyPrefix(), chainID))
defer iterator.Close()

return !iterator.Valid()
Expand Down
Loading

0 comments on commit dbaf024

Please sign in to comment.