Skip to content

Commit

Permalink
replace iota with constant map
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Jul 18, 2024
1 parent 31b5d99 commit 23490ca
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 324 deletions.
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, []byte{types.MustGetKeyPrefix("ConsumerRewardDenomsBytePrefix")})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()[1:]
Expand Down
41 changes: 21 additions & 20 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,13 @@ 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})
bytePrefix := types.MustGetKeyPrefix("ProposedConsumerChainByteKey")
iterator := storetypes.KVStorePrefixIterator(store, []byte{bytePrefix})
defer iterator.Close()

proposedChains := []types.ProposedChain{}
for ; iterator.Valid(); iterator.Next() {
proposalID, err := types.ParseProposedConsumerChainKey(types.ProposedConsumerChainByteKey, iterator.Key())
proposalID, err := types.ParseProposedConsumerChainKey(bytePrefix, iterator.Key())
if err != nil {
panic(fmt.Errorf("proposed chains cannot be parsed: %w", err))
}
Expand Down Expand Up @@ -276,7 +277,7 @@ 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, []byte{types.MustGetKeyPrefix("ChainToClientBytePrefix")})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -319,7 +320,7 @@ func (k Keeper) DeleteChannelToChain(ctx sdk.Context, channelID string) {
// 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, []byte{types.MustGetKeyPrefix("ChannelToChainBytePrefix")})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -490,7 +491,7 @@ func (k Keeper) DeleteUnbondingOp(ctx sdk.Context, id uint64) {
// 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, []byte{types.MustGetKeyPrefix("UnbondingOpBytePrefix")})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -581,7 +582,7 @@ func (k Keeper) SetUnbondingOpIndex(ctx sdk.Context, chainID string, vscID uint6
// 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))
iterator := storetypes.KVStorePrefixIterator(store, types.ChainIdWithLenKey(types.MustGetKeyPrefix("UnbondingOpIndexBytePrefix"), chainID))
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -780,7 +781,7 @@ func (k Keeper) GetValsetUpdateBlockHeight(ctx sdk.Context, valsetUpdateId uint6
// 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, []byte{types.MustGetKeyPrefix("ValsetUpdateBlockHeightBytePrefix")})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -986,7 +987,7 @@ func (k Keeper) DeleteInitTimeoutTimestamp(ctx sdk.Context, chainID string) {
// 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, []byte{types.MustGetKeyPrefix("InitTimeoutTimestampBytePrefix")})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -1050,7 +1051,7 @@ func (k Keeper) DeleteVscSendTimestamp(ctx sdk.Context, chainID string, vscID ui
// 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.MustGetKeyPrefix("VscSendTimestampBytePrefix"), 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.MustGetKeyPrefix("VscSendTimestampBytePrefix"), 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.MustGetKeyPrefix("VscSendTimestampBytePrefix"), 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.MustGetKeyPrefix("OptedInBytePrefix"), 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.MustGetKeyPrefix("OptedInBytePrefix"), 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.MustGetKeyPrefix("ConsumerCommissionRatePrefix"), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand Down Expand Up @@ -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.MustGetKeyPrefix("AllowlistPrefix"), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand All @@ -1459,7 +1460,7 @@ func (k Keeper) IsAllowlisted(
// 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.MustGetKeyPrefix("AllowlistPrefix"), 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.MustGetKeyPrefix("AllowlistPrefix"), chainID))
defer iterator.Close()

return !iterator.Valid()
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.MustGetKeyPrefix("DenylistPrefix"), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand All @@ -1522,7 +1523,7 @@ func (k Keeper) IsDenylisted(
// 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.MustGetKeyPrefix("DenylistPrefix"), 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.MustGetKeyPrefix("DenylistPrefix"), chainID))
defer iterator.Close()

return !iterator.Valid()
Expand Down
19 changes: 11 additions & 8 deletions x/ccv/provider/keeper/key_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,19 @@ func (k Keeper) SetValidatorConsumerPubKey(
func (k Keeper) GetAllValidatorConsumerPubKeys(ctx sdk.Context, chainID *string) (validatorConsumerPubKeys []types.ValidatorConsumerPubKey) {
store := ctx.KVStore(k.storeKey)
var prefix []byte
consumerValidatorsBytePrefix := types.MustGetKeyPrefix("ConsumerValidatorsBytePrefix")
if chainID == nil {
// iterate over the validators public keys assigned for all consumer chains
prefix = []byte{types.ConsumerValidatorsBytePrefix}
prefix = []byte{consumerValidatorsBytePrefix}
} else {
// iterate over the validators public keys assigned for chainID
prefix = types.ChainIdWithLenKey(types.ConsumerValidatorsBytePrefix, *chainID)
prefix = types.ChainIdWithLenKey(consumerValidatorsBytePrefix, *chainID)
}
iterator := storetypes.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// TODO: store chainID and provider cons address in value bytes, marshaled as protobuf type
chainID, providerAddrTmp, err := types.ParseChainIdAndConsAddrKey(types.ConsumerValidatorsBytePrefix, iterator.Key())
chainID, providerAddrTmp, err := types.ParseChainIdAndConsAddrKey(consumerValidatorsBytePrefix, iterator.Key())
if err != nil {
// An error here would indicate something is very wrong,
// the store key is assumed to be correctly serialized in SetValidatorConsumerPubKey.
Expand Down Expand Up @@ -195,18 +196,19 @@ func (k Keeper) SetValidatorByConsumerAddr(
func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string) (validatorConsumerAddrs []types.ValidatorByConsumerAddr) {
store := ctx.KVStore(k.storeKey)
var prefix []byte
validatorsByConsumerAddrBytePrefix := types.MustGetKeyPrefix("ValidatorsByConsumerAddrBytePrefix")
if chainID == nil {
// iterate over the mappings from consensus addresses on all consumer chains
prefix = []byte{types.ValidatorsByConsumerAddrBytePrefix}
prefix = []byte{validatorsByConsumerAddrBytePrefix}
} else {
// iterate over the mappings from consensus addresses on chainID
prefix = types.ChainIdWithLenKey(types.ValidatorsByConsumerAddrBytePrefix, *chainID)
prefix = types.ChainIdWithLenKey(validatorsByConsumerAddrBytePrefix, *chainID)
}
iterator := storetypes.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
// TODO: store chainID and consumer cons address in value bytes, marshaled as protobuf type
chainID, consumerAddrTmp, err := types.ParseChainIdAndConsAddrKey(types.ValidatorsByConsumerAddrBytePrefix, iterator.Key())
chainID, consumerAddrTmp, err := types.ParseChainIdAndConsAddrKey(validatorsByConsumerAddrBytePrefix, iterator.Key())
if err != nil {
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in SetValidatorByConsumerAddr.
Expand Down Expand Up @@ -290,11 +292,12 @@ func (k Keeper) GetConsumerAddrsToPrune(
// Thus, the returned array is in ascending order of vscIDs.
func (k Keeper) GetAllConsumerAddrsToPrune(ctx sdk.Context, chainID string) (consumerAddrsToPrune []types.ConsumerAddrsToPrune) {
store := ctx.KVStore(k.storeKey)
iteratorPrefix := types.ChainIdWithLenKey(types.ConsumerAddrsToPruneBytePrefix, chainID)
consumerAddrsToPruneBytePrefix := types.MustGetKeyPrefix("ConsumerAddrsToPruneBytePrefix")
iteratorPrefix := types.ChainIdWithLenKey(consumerAddrsToPruneBytePrefix, chainID)
iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
_, vscID, err := types.ParseChainIdAndUintIdKey(types.ConsumerAddrsToPruneBytePrefix, iterator.Key())
_, vscID, err := types.ParseChainIdAndUintIdKey(consumerAddrsToPruneBytePrefix, iterator.Key())
if err != nil {
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in AppendConsumerAddrsToPrune.
Expand Down
8 changes: 4 additions & 4 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (k Keeper) BeginBlockInit(ctx sdk.Context) {
// Note: this method is split out from BeginBlockInit to be easily unit tested.
func (k Keeper) GetConsumerAdditionPropsToExecute(ctx sdk.Context) (propsToExecute []types.ConsumerAdditionProposal) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCAPBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.MustGetKeyPrefix("PendingCAPBytePrefix")})

defer iterator.Close()

Expand Down Expand Up @@ -484,7 +484,7 @@ func (k Keeper) GetConsumerAdditionPropsToExecute(ctx sdk.Context) (propsToExecu
// then they are ordered by chainID.
func (k Keeper) GetAllPendingConsumerAdditionProps(ctx sdk.Context) (props []types.ConsumerAdditionProposal) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCAPBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.MustGetKeyPrefix("PendingCAPBytePrefix")})

defer iterator.Close()

Expand Down Expand Up @@ -593,7 +593,7 @@ func (k Keeper) GetConsumerRemovalPropsToExecute(ctx sdk.Context) []types.Consum
propsToExecute := []types.ConsumerRemovalProposal{}

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

for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -624,7 +624,7 @@ func (k Keeper) GetConsumerRemovalPropsToExecute(ctx sdk.Context) []types.Consum
// Thus, the returned array is in stopTime order.
func (k Keeper) GetAllPendingConsumerRemovalProps(ctx sdk.Context) (props []types.ConsumerRemovalProposal) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingCRPBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.MustGetKeyPrefix("PendingCRPBytePrefix")})
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/throttle_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k Keeper) LegacyGetAllThrottledPacketData(ctx sdktypes.Context, consumerCh
vscMaturedData = []ccvtypes.VSCMaturedPacketData{}

store := ctx.KVStore(k.storeKey)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.MustGetKeyPrefix("ThrottledPacketDataBytePrefix"), consumerChainID)
iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix)
defer iterator.Close()

Expand Down Expand Up @@ -60,7 +60,7 @@ func (k Keeper) LegacyGetAllThrottledPacketData(ctx sdktypes.Context, consumerCh
// LegacyDeleteThrottledPacketDataForConsumer removes all throttled packet data that was queued on the provider for a given consumer chain.
func (k Keeper) LegacyDeleteThrottledPacketDataForConsumer(ctx sdktypes.Context, consumerChainID string) {
store := ctx.KVStore(k.storeKey)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.ThrottledPacketDataBytePrefix, consumerChainID)
iteratorPrefix := providertypes.ChainIdWithLenKey(providertypes.MustGetKeyPrefix("ThrottledPacketDataBytePrefix"), consumerChainID)
iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix)
defer iterator.Close()

Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/validator_set_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (k Keeper) DeleteConsumerValSet(
chainID string,
) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.ConsumerValidatorBytePrefix, chainID)
key := types.ChainIdWithLenKey(types.MustGetKeyPrefix("ConsumerValidatorBytePrefix"), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)

var keysToDel [][]byte
Expand Down Expand Up @@ -96,7 +96,7 @@ func (k Keeper) GetConsumerValSet(
chainID string,
) (validators []types.ConsumerValidator) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.ConsumerValidatorBytePrefix, chainID)
key := types.ChainIdWithLenKey(types.MustGetKeyPrefix("ConsumerValidatorBytePrefix"), chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

Expand Down
Loading

0 comments on commit 23490ca

Please sign in to comment.