diff --git a/x/ccv/provider/keeper/distribution.go b/x/ccv/provider/keeper/distribution.go index ca16831ee8..93843c8e46 100644 --- a/x/ccv/provider/keeper/distribution.go +++ b/x/ccv/provider/keeper/distribution.go @@ -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:] diff --git a/x/ccv/provider/keeper/keeper.go b/x/ccv/provider/keeper/keeper.go index 22f5f33626..f80ee316a8 100644 --- a/x/ccv/provider/keeper/keeper.go +++ b/x/ccv/provider/keeper/keeper.go @@ -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)) } @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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() @@ -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() { @@ -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() @@ -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 @@ -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() @@ -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() @@ -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{} @@ -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() @@ -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() @@ -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{} @@ -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() diff --git a/x/ccv/provider/keeper/key_assignment.go b/x/ccv/provider/keeper/key_assignment.go index 596c3be606..085c21fc66 100644 --- a/x/ccv/provider/keeper/key_assignment.go +++ b/x/ccv/provider/keeper/key_assignment.go @@ -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. @@ -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. @@ -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. diff --git a/x/ccv/provider/keeper/proposal.go b/x/ccv/provider/keeper/proposal.go index 3cc8d0b9a8..e975b1166e 100644 --- a/x/ccv/provider/keeper/proposal.go +++ b/x/ccv/provider/keeper/proposal.go @@ -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() @@ -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() @@ -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() { @@ -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() { diff --git a/x/ccv/provider/keeper/throttle_legacy.go b/x/ccv/provider/keeper/throttle_legacy.go index 53c6f39c72..749afb8e08 100644 --- a/x/ccv/provider/keeper/throttle_legacy.go +++ b/x/ccv/provider/keeper/throttle_legacy.go @@ -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() @@ -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() diff --git a/x/ccv/provider/keeper/validator_set_update.go b/x/ccv/provider/keeper/validator_set_update.go index d7f764171e..a6ec166f07 100644 --- a/x/ccv/provider/keeper/validator_set_update.go +++ b/x/ccv/provider/keeper/validator_set_update.go @@ -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 @@ -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() diff --git a/x/ccv/provider/types/keys.go b/x/ccv/provider/types/keys.go index 47231c219f..a32633293a 100644 --- a/x/ccv/provider/types/keys.go +++ b/x/ccv/provider/types/keys.go @@ -33,222 +33,243 @@ const ( ConsumerRewardsPool = "consumer_rewards_pool" ) -// Iota generated keys/byte prefixes (as a byte), supports 256 possible values -const ( +func getKeyPrefixes() map[string]byte { + return map[string]byte{ + // ParametersKey is the is the single byte key for storing provider's parameters. + // note that this was set to the max uint8 type value 0xFF in order to protect + // from using the ICS v5.0.0 provider module by mistake + "ParametersByteKey": byte(0xFF), - // PortKey defines the key to store the port ID in store - PortByteKey byte = iota + // PortKey defines the key to store the port ID in store + "PortByteKey": 0, - // MaturedUnbondingOpsByteKey is the byte key that stores the list of all unbonding operations ids - // that have matured from a consumer chain perspective, - // i.e., no longer waiting on the unbonding period to elapse on any consumer chain - MaturedUnbondingOpsByteKey + // MaturedUnbondingOpsByteKey is the byte key that stores the list of all unbonding operations ids + // that have matured from a consumer chain perspective, + // i.e., no longer waiting on the unbonding period to elapse on any consumer chain + "MaturedUnbondingOpsByteKey": 1, - // ValidatorSetUpdateIdByteKey is the byte key that stores the current validator set update id - ValidatorSetUpdateIdByteKey + // ValidatorSetUpdateIdByteKey is the byte key that stores the current validator set update id + "ValidatorSetUpdateIdByteKey": 2, - // SlashMeterByteKey is the byte key for storing the slash meter - SlashMeterByteKey + // SlashMeterByteKey is the byte key for storing the slash meter + "SlashMeterByteKey": 3, - // SlashMeterReplenishTimeCandidateByteKey is the byte key for storing the slash meter replenish time candidate - SlashMeterReplenishTimeCandidateByteKey + // SlashMeterReplenishTimeCandidateByteKey is the byte key for storing the slash meter replenish time candidate + "SlashMeterReplenishTimeCandidateByteKey": 4, - // ChainToChannelBytePrefix is the byte prefix for storing mapping - // from chainID to the channel ID that is used to send over validator set changes. - ChainToChannelBytePrefix + // ChainToChannelBytePrefix is the byte prefix for storing mapping + // from chainID to the channel ID that is used to send over validator set changes. + "ChainToChannelBytePrefix": 5, - // ChannelToChainBytePrefix is the byte prefix for storing mapping - // from the CCV channel ID to the consumer chain ID. - ChannelToChainBytePrefix + // ChannelToChainBytePrefix is the byte prefix for storing mapping + // from the CCV channel ID to the consumer chain ID. + "ChannelToChainBytePrefix": 6, - // ChainToClientBytePrefix is the byte prefix for storing the client ID for a given consumer chainID. - ChainToClientBytePrefix + // ChainToClientBytePrefix is the byte prefix for storing the client ID for a given consumer chainID. + "ChainToClientBytePrefix": 7, - // InitTimeoutTimestampBytePrefix is the byte prefix for storing - // the init timeout timestamp for a given consumer chainID. - InitTimeoutTimestampBytePrefix + // InitTimeoutTimestampBytePrefix is the byte prefix for storing + // the init timeout timestamp for a given consumer chainID. + "InitTimeoutTimestampBytePrefix": 8, - // PendingCAPBytePrefix is the byte prefix for storing pending consumer addition proposals before the spawn time occurs. - // The key includes the BigEndian timestamp to allow for efficient chronological iteration - PendingCAPBytePrefix + // PendingCAPBytePrefix is the byte prefix for storing pending consumer addition proposals before the spawn time occurs. + // The key includes the BigEndian timestamp to allow for efficient chronological iteration + "PendingCAPBytePrefix": 9, - // PendingCRPBytePrefix is the byte prefix for storing pending consumer removal proposals before the stop time occurs. - // The key includes the BigEndian timestamp to allow for efficient chronological iteration - PendingCRPBytePrefix + // PendingCRPBytePrefix is the byte prefix for storing pending consumer removal proposals before the stop time occurs. + // The key includes the BigEndian timestamp to allow for efficient chronological iteration + "PendingCRPBytePrefix": 10, - // UnbondingOpBytePrefix is the byte prefix that stores a record of all the ids of consumer chains that - // need to unbond before a given unbonding operation can unbond on this chain. - UnbondingOpBytePrefix + // UnbondingOpBytePrefix is the byte prefix that stores a record of all the ids of consumer chains that + // need to unbond before a given unbonding operation can unbond on this chain. + "UnbondingOpBytePrefix": 11, - // UnbondingOpIndexBytePrefix is byte prefix of the index for looking up which unbonding - // operations are waiting for a given consumer chain to unbond - UnbondingOpIndexBytePrefix + // UnbondingOpIndexBytePrefix is byte prefix of the index for looking up which unbonding + // operations are waiting for a given consumer chain to unbond + "UnbondingOpIndexBytePrefix": 12, - // ValsetUpdateBlockHeightBytePrefix is the byte prefix that will store the mapping from vscIDs to block heights - ValsetUpdateBlockHeightBytePrefix + // ValsetUpdateBlockHeightBytePrefix is the byte prefix that will store the mapping from vscIDs to block heights + "ValsetUpdateBlockHeightBytePrefix": 13, - // ConsumerGenesisBytePrefix stores consumer genesis state material (consensus state and client state) indexed by consumer chain id - ConsumerGenesisBytePrefix + // ConsumerGenesisBytePrefix stores consumer genesis state material (consensus state and client state) indexed by consumer chain id + "ConsumerGenesisBytePrefix": 14, - // SlashAcksBytePrefix is the byte prefix that will store consensus address of consumer chain validators successfully slashed on the provider chain - SlashAcksBytePrefix + // SlashAcksBytePrefix is the byte prefix that will store consensus address of consumer chain validators successfully slashed on the provider chain + "SlashAcksBytePrefix": 15, - // InitChainHeightBytePrefix is the byte prefix that will store the mapping from a chain id to the corresponding block height on the provider - // this consumer chain was initialized - InitChainHeightBytePrefix + // InitChainHeightBytePrefix is the byte prefix that will store the mapping from a chain id to the corresponding block height on the provider + // this consumer chain was initialized + "InitChainHeightBytePrefix": 16, - // PendingVSCsBytePrefix is the byte prefix that will store pending ValidatorSetChangePacket data - PendingVSCsBytePrefix + // PendingVSCsBytePrefix is the byte prefix that will store pending ValidatorSetChangePacket data + "PendingVSCsBytePrefix": 17, - // VscSendTimestampBytePrefix is the byte prefix for storing - // the list of VSC sending timestamps for a given consumer chainID. - VscSendTimestampBytePrefix + // VscSendTimestampBytePrefix is the byte prefix for storing + // the list of VSC sending timestamps for a given consumer chainID. + "VscSendTimestampBytePrefix": 18, - // ThrottledPacketDataSizeBytePrefix is the byte prefix for storing the size of chain-specific throttled packet data queues - ThrottledPacketDataSizeBytePrefix + // ThrottledPacketDataSizeBytePrefix is the byte prefix for storing the size of chain-specific throttled packet data queues + "ThrottledPacketDataSizeBytePrefix": 19, - // ThrottledPacketDataBytePrefix is the byte prefix storing throttled packet data - ThrottledPacketDataBytePrefix + // ThrottledPacketDataBytePrefix is the byte prefix storing throttled packet data + "ThrottledPacketDataBytePrefix": 20, - // GlobalSlashEntryBytePrefix is the byte prefix storing global slash queue entries - GlobalSlashEntryBytePrefix + // GlobalSlashEntryBytePrefix is the byte prefix storing global slash queue entries + "GlobalSlashEntryBytePrefix": 21, - // ConsumerValidatorsBytePrefix is the byte prefix that will store the validator assigned keys for every consumer chain - ConsumerValidatorsBytePrefix + // ConsumerValidatorsBytePrefix is the byte prefix that will store the validator assigned keys for every consumer chain + "ConsumerValidatorsBytePrefix": 22, - // ValidatorsByConsumerAddrBytePrefix is the byte prefix that will store the mapping from validator addresses - // on consumer chains to validator addresses on the provider chain - ValidatorsByConsumerAddrBytePrefix + // ValidatorsByConsumerAddrBytePrefix is the byte prefix that will store the mapping from validator addresses + // on consumer chains to validator addresses on the provider chain + "ValidatorsByConsumerAddrBytePrefix": 23, - // KeyAssignmentReplacementsBytePrefix was the byte prefix used to store the key assignments that needed to be replaced in the current block - // NOTE: This prefix is deprecated, but left in place to avoid consumer state migrations - // [DEPRECATED] - KeyAssignmentReplacementsBytePrefix + // KeyAssignmentReplacementsBytePrefix was the byte prefix used to store the key assignments that needed to be replaced in the current block + // NOTE: This prefix is deprecated, but left in place to avoid consumer state migrations + // [DEPRECATED] + "KeyAssignmentReplacementsBytePrefix": 24, - // ConsumerAddrsToPruneBytePrefix is the byte prefix that will store the mapping from VSC ids - // to consumer validators addresses needed for pruning - ConsumerAddrsToPruneBytePrefix + // ConsumerAddrsToPruneBytePrefix is the byte prefix that will store the mapping from VSC ids + // to consumer validators addresses needed for pruning + "ConsumerAddrsToPruneBytePrefix": 25, - // SlashLogBytePrefix is the byte prefix that will store the mapping from provider address to boolean - // denoting whether the provider address has committed any double signign infractions - SlashLogBytePrefix + // SlashLogBytePrefix is the byte prefix that will store the mapping from provider address to boolean + // denoting whether the provider address has committed any double signign infractions + "SlashLogBytePrefix": 26, - // ConsumerRewardDenomsBytePrefix is the byte prefix that will store a list of consumer reward denoms - ConsumerRewardDenomsBytePrefix + // ConsumerRewardDenomsBytePrefix is the byte prefix that will store a list of consumer reward denoms + "ConsumerRewardDenomsBytePrefix": 27, - // VSCMaturedHandledThisBlockBytePrefix is the byte prefix storing the number of vsc matured packets - // handled in the current block - VSCMaturedHandledThisBlockBytePrefix + // VSCMaturedHandledThisBlockBytePrefix is the byte prefix storing the number of vsc matured packets + // handled in the current block + "VSCMaturedHandledThisBlockBytePrefix": 28, - // EquivocationEvidenceMinHeightBytePrefix is the byte prefix storing the mapping from consumer chain IDs - // to the minimum height of a valid consumer equivocation evidence - EquivocationEvidenceMinHeightBytePrefix + // EquivocationEvidenceMinHeightBytePrefix is the byte prefix storing the mapping from consumer chain IDs + // to the minimum height of a valid consumer equivocation evidence + "EquivocationEvidenceMinHeightBytePrefix": 29, - // ProposedConsumerChainByteKey is the byte prefix storing the consumer chainId in consumerAddition gov proposal submitted before voting finishes - ProposedConsumerChainByteKey + // ProposedConsumerChainByteKey is the byte prefix storing the consumer chainId in consumerAddition gov proposal submitted before voting finishes + "ProposedConsumerChainByteKey": 30, - // ConsumerValidatorBytePrefix is the byte prefix used when storing for each consumer chain all the consumer - // validators in this epoch that are validating the consumer chain - ConsumerValidatorBytePrefix + // ConsumerValidatorBytePrefix is the byte prefix used when storing for each consumer chain all the consumer + // validators in this epoch that are validating the consumer chain + "ConsumerValidatorBytePrefix": 31, - // OptedInBytePrefix is the byte prefix for storing whether a validator is opted in to validate on a consumer chain - OptedInBytePrefix + // OptedInBytePrefix is the byte prefix for storing whether a validator is opted in to validate on a consumer chain + "OptedInBytePrefix": 32, - // TopNBytePrefix is the byte prefix storing the mapping from a consumer chain to the N value of this chain, - // that corresponds to the N% of the top validators that have to validate this consumer chain - TopNBytePrefix + // TopNBytePrefix is the byte prefix storing the mapping from a consumer chain to the N value of this chain, + // that corresponds to the N% of the top validators that have to validate this consumer chain + "TopNBytePrefix": 33, - // ValidatorsPowerCapPrefix is the byte prefix storing the mapping from a consumer chain to the power-cap value of this chain, - // that corresponds to p% such that no validator can have more than p% of the voting power on the consumer chain. - // Operates on a best-effort basis. - ValidatorsPowerCapPrefix + // ValidatorsPowerCapPrefix is the byte prefix storing the mapping from a consumer chain to the power-cap value of this chain, + // that corresponds to p% such that no validator can have more than p% of the voting power on the consumer chain. + // Operates on a best-effort basis. + "ValidatorsPowerCapPrefix": 34, - // ValidatorSetCapPrefix is the byte prefix storing the mapping from a consumer chain to the validator-set cap value - // of this chain. - ValidatorSetCapPrefix + // ValidatorSetCapPrefix is the byte prefix storing the mapping from a consumer chain to the validator-set cap value + // of this chain. + "ValidatorSetCapPrefix": 35, - // AllowlistPrefix is the byte prefix storing the mapping from a consumer chain to the set of validators that are - // allowlisted. - AllowlistPrefix + // AllowlistPrefix is the byte prefix storing the mapping from a consumer chain to the set of validators that are + // allowlisted. + "AllowlistPrefix": 36, - // DenylistPrefix is the byte prefix storing the mapping from a consumer chain to the set of validators that are - // denylisted. - DenylistPrefix + // DenylistPrefix is the byte prefix storing the mapping from a consumer chain to the set of validators that are + // denylisted. + "DenylistPrefix": 37, - // ConsumerRewardsAllocationBytePrefix is the byte prefix for storing for each consumer the ICS rewards - // allocated to the consumer rewards pool - ConsumerRewardsAllocationBytePrefix + // ConsumerRewardsAllocationBytePrefix is the byte prefix for storing for each consumer the ICS rewards + // allocated to the consumer rewards pool + "ConsumerRewardsAllocationBytePrefix": 38, - // ConsumerCommissionRatePrefix is the byte prefix for storing the commission rate - // per validator per consumer chain - ConsumerCommissionRatePrefix + // ConsumerCommissionRatePrefix is the byte prefix for storing the commission rate + // per validator per consumer chain + "ConsumerCommissionRatePrefix": 39, - // MinimumPowerInTopNBytePrefix is the byte prefix for storing the - // minimum power required to be in the top N per consumer chain. - MinimumPowerInTopNBytePrefix + // MinimumPowerInTopNBytePrefix is the byte prefix for storing the + // minimum power required to be in the top N per consumer chain. + "MinimumPowerInTopNBytePrefix": 40, - // NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go -) + // NOTE: DO NOT ADD NEW BYTE PREFIXES HERE WITHOUT ADDING THEM TO getAllKeyPrefixes() IN keys_test.go + } +} + +// MustGetKeyPrefix returns the key prefix for a given index. +// It panics if there is not byte prefix for the index. +func MustGetKeyPrefix(index string) byte { + keyPrefixes := getKeyPrefixes() + if prefix, found := keyPrefixes[index]; !found { + panic(fmt.Sprintf("could not find key prefix for index %s", index)) + } else { + return prefix + } +} + +// GetAllKeyPrefixes returns all the key prefixes. +// Only used for testing +func GetAllKeyPrefixes() []byte { + prefixMap := getKeyPrefixes() + prefixList := make([]byte, 0, len(prefixMap)) + for _, prefix := range prefixMap { + prefixList = append(prefixList, prefix) + } + return prefixList +} // // Fully defined key func section // -const ( - // ParametersKey is the is the single byte key for storing provider's parameters. - // note that this was set to the max uint8 type value 0xFF in order to protect - // from using the ICS v5.0.0 provider module by mistake - ParametersByteKey = byte(0xFF) -) - // ParametersKey returns the key for the parameters of the provider module in the store func ParametersKey() []byte { - return []byte{ParametersByteKey} + return []byte{MustGetKeyPrefix("ParametersByteKey")} } // PortKey returns the key to the port ID in the store func PortKey() []byte { - return []byte{PortByteKey} + return []byte{MustGetKeyPrefix("PortByteKey")} } // MaturedUnbondingOpsKey returns the key for storing the list of matured unbonding operations. func MaturedUnbondingOpsKey() []byte { - return []byte{MaturedUnbondingOpsByteKey} + return []byte{MustGetKeyPrefix("MaturedUnbondingOpsByteKey")} } // ValidatorSetUpdateIdKey is the key that stores the current validator set update id func ValidatorSetUpdateIdKey() []byte { - return []byte{ValidatorSetUpdateIdByteKey} + return []byte{MustGetKeyPrefix("ValidatorSetUpdateIdByteKey")} } // SlashMeterKey returns the key storing the slash meter func SlashMeterKey() []byte { - return []byte{SlashMeterByteKey} + return []byte{MustGetKeyPrefix("SlashMeterByteKey")} } // SlashMeterReplenishTimeCandidateKey returns the key storing the slash meter replenish time candidate func SlashMeterReplenishTimeCandidateKey() []byte { - return []byte{SlashMeterReplenishTimeCandidateByteKey} + return []byte{MustGetKeyPrefix("SlashMeterReplenishTimeCandidateByteKey")} } // ChainToChannelKey returns the key under which the CCV channel ID will be stored for the given consumer chain. func ChainToChannelKey(chainID string) []byte { - return append([]byte{ChainToChannelBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("ChainToChannelBytePrefix")}, []byte(chainID)...) } // ChannelToChainKey returns the key under which the consumer chain ID will be stored for the given channelID. func ChannelToChainKey(channelID string) []byte { - return append([]byte{ChannelToChainBytePrefix}, []byte(channelID)...) + return append([]byte{MustGetKeyPrefix("ChannelToChainBytePrefix")}, []byte(channelID)...) + } // ChainToClientKey returns the key under which the clientID for the given chainID is stored. func ChainToClientKey(chainID string) []byte { - return append([]byte{ChainToClientBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("ChainToClientBytePrefix")}, []byte(chainID)...) } // InitTimeoutTimestampKey returns the key under which the init timeout timestamp for the given chainID is stored. func InitTimeoutTimestampKey(chainID string) []byte { - return append([]byte{InitTimeoutTimestampBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("InitTimeoutTimestampBytePrefix")}, []byte(chainID)...) } // PendingCAPKey returns the key under which a pending consumer addition proposal is stored. @@ -257,7 +278,7 @@ func PendingCAPKey(timestamp time.Time, chainID string) []byte { ts := uint64(timestamp.UTC().UnixNano()) return ccvtypes.AppendMany( // Append the prefix - []byte{PendingCAPBytePrefix}, + []byte{MustGetKeyPrefix("PendingCAPBytePrefix")}, // Append the time sdk.Uint64ToBigEndian(ts), // Append the chainId @@ -271,7 +292,7 @@ func PendingCRPKey(timestamp time.Time, chainID string) []byte { ts := uint64(timestamp.UTC().UnixNano()) return ccvtypes.AppendMany( // Append the prefix - []byte{PendingCRPBytePrefix}, + []byte{MustGetKeyPrefix("PendingCRPBytePrefix")}, // Append the time sdk.Uint64ToBigEndian(ts), // Append the chainId @@ -284,71 +305,71 @@ func PendingCRPKey(timestamp time.Time, chainID string) []byte { func UnbondingOpKey(id uint64) []byte { bz := make([]byte, 8) binary.BigEndian.PutUint64(bz, id) - return append([]byte{UnbondingOpBytePrefix}, bz...) + return append([]byte{MustGetKeyPrefix("UnbondingOpBytePrefix")}, bz...) } // UnbondingOpIndexKey returns an unbonding op index key // Note: chainId is hashed to a fixed length sequence of bytes here to prevent // injection attack between chainIDs. func UnbondingOpIndexKey(chainID string, vscID uint64) []byte { - return ChainIdAndUintIdKey(UnbondingOpIndexBytePrefix, chainID, vscID) + return ChainIdAndUintIdKey(MustGetKeyPrefix("UnbondingOpIndexBytePrefix"), chainID, vscID) } // ParseUnbondingOpIndexKey parses an unbonding op index key for VSC ID // Removes the prefix + chainID from index key and returns only the key part. func ParseUnbondingOpIndexKey(key []byte) (string, uint64, error) { - return ParseChainIdAndUintIdKey(UnbondingOpIndexBytePrefix, key) + return ParseChainIdAndUintIdKey(MustGetKeyPrefix("UnbondingOpIndexBytePrefix"), key) } // ValsetUpdateBlockHeightKey returns the key that storing the mapping from valset update ID to block height func ValsetUpdateBlockHeightKey(valsetUpdateId uint64) []byte { vuidBytes := make([]byte, 8) binary.BigEndian.PutUint64(vuidBytes, valsetUpdateId) - return append([]byte{ValsetUpdateBlockHeightBytePrefix}, vuidBytes...) + return append([]byte{MustGetKeyPrefix("ValsetUpdateBlockHeightBytePrefix")}, vuidBytes...) } // ConsumerGenesisKey returns the key corresponding to consumer genesis state material // (consensus state and client state) indexed by consumer chain id func ConsumerGenesisKey(chainID string) []byte { - return append([]byte{ConsumerGenesisBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("ConsumerGenesisBytePrefix")}, []byte(chainID)...) } // SlashAcksKey returns the key under which slashing acks are stored for a given chain ID func SlashAcksKey(chainID string) []byte { - return append([]byte{SlashAcksBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("SlashAcksBytePrefix")}, []byte(chainID)...) } // InitChainHeightKey returns the key under which the block height for a given chain ID is stored func InitChainHeightKey(chainID string) []byte { - return append([]byte{InitChainHeightBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("InitChainHeightBytePrefix")}, []byte(chainID)...) } // PendingVSCsKey returns the key under which // pending ValidatorSetChangePacket data is stored for a given chain ID func PendingVSCsKey(chainID string) []byte { - return append([]byte{PendingVSCsBytePrefix}, []byte(chainID)...) + return append([]byte{MustGetKeyPrefix("PendingVSCsBytePrefix")}, []byte(chainID)...) } // VscSendingTimestampKey returns the key under which the // sending timestamp of the VSCPacket with vsc ID is stored func VscSendingTimestampKey(chainID string, vscID uint64) []byte { - return ChainIdAndUintIdKey(VscSendTimestampBytePrefix, chainID, vscID) + return ChainIdAndUintIdKey(MustGetKeyPrefix("VscSendTimestampBytePrefix"), chainID, vscID) } // ParseVscTimeoutTimestampKey returns chain ID and vsc ID // for a VscSendingTimestampKey or an error if unparsable func ParseVscSendingTimestampKey(bz []byte) (string, uint64, error) { - return ParseChainIdAndUintIdKey(VscSendTimestampBytePrefix, bz) + return ParseChainIdAndUintIdKey(MustGetKeyPrefix("VscSendTimestampBytePrefix"), bz) } // ThrottledPacketDataSizeKey returns the key storing the size of the throttled packet data queue for a given chain ID func ThrottledPacketDataSizeKey(consumerChainID string) []byte { - return append([]byte{ThrottledPacketDataSizeBytePrefix}, []byte(consumerChainID)...) + return append([]byte{MustGetKeyPrefix("ThrottledPacketDataSizeBytePrefix")}, []byte(consumerChainID)...) } // ThrottledPacketDataKey returns the key storing the throttled packet data queue for a given chain ID and ibc seq num func ThrottledPacketDataKey(consumerChainID string, ibcSeqNum uint64) []byte { - return ChainIdAndUintIdKey(ThrottledPacketDataBytePrefix, consumerChainID, ibcSeqNum) + return ChainIdAndUintIdKey(MustGetKeyPrefix("ThrottledPacketDataBytePrefix"), consumerChainID, ibcSeqNum) } // MustParseThrottledPacketDataKey parses a throttled packet data key or panics upon failure @@ -362,7 +383,7 @@ func MustParseThrottledPacketDataKey(key []byte) (string, uint64) { // ParseThrottledPacketDataKey parses a throttled packet data key func ParseThrottledPacketDataKey(key []byte) (chainId string, ibcSeqNum uint64, err error) { - return ParseChainIdAndUintIdKey(ThrottledPacketDataBytePrefix, key) + return ParseChainIdAndUintIdKey(MustGetKeyPrefix("ThrottledPacketDataBytePrefix"), key) } // GlobalSlashEntryKey returns the key for storing a global slash queue entry. @@ -370,7 +391,7 @@ func GlobalSlashEntryKey(entry GlobalSlashEntry) []byte { recvTime := uint64(entry.RecvTime.UTC().UnixNano()) return ccvtypes.AppendMany( // Append byte prefix - []byte{GlobalSlashEntryBytePrefix}, + []byte{MustGetKeyPrefix("GlobalSlashEntryBytePrefix")}, // Append time bz sdk.Uint64ToBigEndian(recvTime), // Append ibc seq num @@ -386,7 +407,7 @@ func MustParseGlobalSlashEntryKey(bz []byte) ( recvTime time.Time, consumerChainID string, ibcSeqNum uint64, ) { // Prefix is in first byte - expectedPrefix := []byte{GlobalSlashEntryBytePrefix} + expectedPrefix := []byte{MustGetKeyPrefix("GlobalSlashEntryBytePrefix")} if prefix := bz[:1]; !bytes.Equal(prefix, expectedPrefix) { panic(fmt.Sprintf("invalid prefix; expected: %X, got: %X", expectedPrefix, prefix)) } @@ -407,35 +428,116 @@ func MustParseGlobalSlashEntryKey(bz []byte) ( // ConsumerValidatorsKey returns the key under which the // validator assigned keys for every consumer chain are stored func ConsumerValidatorsKey(chainID string, addr ProviderConsAddress) []byte { - return ChainIdAndConsAddrKey(ConsumerValidatorsBytePrefix, chainID, addr.ToSdkConsAddr()) + return ChainIdAndConsAddrKey(MustGetKeyPrefix("ConsumerValidatorsBytePrefix"), chainID, addr.ToSdkConsAddr()) + } // ValidatorsByConsumerAddrKey returns the key under which the mapping from validator addresses // on consumer chains to validator addresses on the provider chain is stored func ValidatorsByConsumerAddrKey(chainID string, addr ConsumerConsAddress) []byte { - return ChainIdAndConsAddrKey(ValidatorsByConsumerAddrBytePrefix, chainID, addr.ToSdkConsAddr()) + return ChainIdAndConsAddrKey(MustGetKeyPrefix("ValidatorsByConsumerAddrBytePrefix"), chainID, addr.ToSdkConsAddr()) } // ConsumerAddrsToPruneKey returns the key under which the // mapping from VSC ids to consumer validators addresses is stored func ConsumerAddrsToPruneKey(chainID string, vscID uint64) []byte { - return ChainIdAndUintIdKey(ConsumerAddrsToPruneBytePrefix, chainID, vscID) + return ChainIdAndUintIdKey(MustGetKeyPrefix("ConsumerAddrsToPruneBytePrefix"), chainID, vscID) } // SlashLogKey returns the key to a validator's slash log func SlashLogKey(providerAddr ProviderConsAddress) []byte { - return append([]byte{SlashLogBytePrefix}, providerAddr.ToSdkConsAddr().Bytes()...) + return append([]byte{MustGetKeyPrefix("SlashLogBytePrefix")}, providerAddr.ToSdkConsAddr().Bytes()...) +} + +func VSCMaturedHandledThisBlockKey() []byte { + return []byte{MustGetKeyPrefix("VSCMaturedHandledThisBlockBytePrefix")} } // ConsumerRewardDenomsKey returns the key under which consumer reward denoms are stored func ConsumerRewardDenomsKey(denom string) []byte { - return append([]byte{ConsumerRewardDenomsBytePrefix}, []byte(denom)...) + return append([]byte{MustGetKeyPrefix("ConsumerRewardDenomsBytePrefix")}, []byte(denom)...) } // EquivocationEvidenceMinHeightKey returns the key storing the minimum height // of a valid consumer equivocation evidence for a given consumer chain ID func EquivocationEvidenceMinHeightKey(consumerChainID string) []byte { - return append([]byte{EquivocationEvidenceMinHeightBytePrefix}, []byte(consumerChainID)...) + return append([]byte{MustGetKeyPrefix("EquivocationEvidenceMinHeightBytePrefix")}, []byte(consumerChainID)...) +} + +// ProposedConsumerChainKey returns the key of proposed consumer chainId in consumerAddition gov proposal before voting finishes, the stored key format is prefix|proposalID, value is chainID +func ProposedConsumerChainKey(proposalID uint64) []byte { + return ccvtypes.AppendMany( + []byte{MustGetKeyPrefix("ProposedConsumerChainByteKey")}, + sdk.Uint64ToBigEndian(proposalID), + ) +} + +// ParseProposedConsumerChainKey get the proposalID in the key +func ParseProposedConsumerChainKey(prefix byte, bz []byte) (uint64, error) { + expectedPrefix := []byte{prefix} + prefixL := len(expectedPrefix) + if prefix := bz[:prefixL]; !bytes.Equal(prefix, expectedPrefix) { + return 0, fmt.Errorf("invalid prefix; expected: %X, got: %X", expectedPrefix, prefix) + } + proposalID := sdk.BigEndianToUint64(bz[prefixL:]) + + return proposalID, nil +} + +// ConsumerValidatorKey returns the key of consumer chain `chainID` and validator with `providerAddr` +func ConsumerValidatorKey(chainID string, providerAddr []byte) []byte { + prefix := ChainIdWithLenKey(MustGetKeyPrefix("ConsumerValidatorBytePrefix"), chainID) + return append(prefix, providerAddr...) +} + +// TopNKey returns the key used to store the Top N value per consumer chain. +// This value corresponds to the N% of the top validators that have to validate the consumer chain. +func TopNKey(chainID string) []byte { + return ChainIdWithLenKey(MustGetKeyPrefix("TopNBytePrefix"), chainID) +} + +// ValidatorSetPowerKey returns the key of consumer chain `chainID` +func ValidatorsPowerCapKey(chainID string) []byte { + return ChainIdWithLenKey(MustGetKeyPrefix("ValidatorsPowerCapPrefix"), chainID) +} + +// ValidatorSetCapKey returns the key of consumer chain `chainID` +func ValidatorSetCapKey(chainID string) []byte { + return ChainIdWithLenKey(MustGetKeyPrefix("ValidatorSetCapPrefix"), chainID) +} + +// AllowlistCapKey returns the key to a validator's slash log +func AllowlistCapKey(chainID string, providerAddr ProviderConsAddress) []byte { + return append(ChainIdWithLenKey(MustGetKeyPrefix("AllowlistPrefix"), chainID), providerAddr.ToSdkConsAddr().Bytes()...) +} + +// DenylistCapKey returns the key to a validator's slash log +func DenylistCapKey(chainID string, providerAddr ProviderConsAddress) []byte { + return append(ChainIdWithLenKey(MustGetKeyPrefix("DenylistPrefix"), chainID), providerAddr.ToSdkConsAddr().Bytes()...) +} + +// OptedInKey returns the key used to store whether a validator is opted in on a consumer chain. +func OptedInKey(chainID string, providerAddr ProviderConsAddress) []byte { + prefix := ChainIdWithLenKey(MustGetKeyPrefix("OptedInBytePrefix"), chainID) + return append(prefix, providerAddr.ToSdkConsAddr().Bytes()...) +} + +// ConsumerRewardsAllocationKey returns the key used to store the ICS rewards per consumer chain +func ConsumerRewardsAllocationKey(chainID string) []byte { + return append([]byte{MustGetKeyPrefix("ConsumerRewardsAllocationBytePrefix")}, []byte(chainID)...) +} + +// ConsumerCommissionRateKey returns the key used to store the commission rate per validator per consumer chain. +func ConsumerCommissionRateKey(chainID string, providerAddr ProviderConsAddress) []byte { + return ChainIdAndConsAddrKey( + MustGetKeyPrefix("ConsumerCommissionRatePrefix"), + chainID, + providerAddr.ToSdkConsAddr(), + ) +} + +func MinimumPowerInTopNKey(chainID string) []byte { + return ChainIdWithLenKey(MustGetKeyPrefix("MinimumPowerInTopNBytePrefix"), chainID) } // NOTE: DO NOT ADD FULLY DEFINED KEY FUNCTIONS WITHOUT ADDING THEM TO getAllFullyDefinedKeys() IN keys_test.go @@ -541,86 +643,6 @@ func ParseChainIdAndConsAddrKey(prefix byte, bz []byte) (string, sdk.ConsAddress return chainID, addr, nil } -func VSCMaturedHandledThisBlockKey() []byte { - return []byte{VSCMaturedHandledThisBlockBytePrefix} -} - -// ProposedConsumerChainKey returns the key of proposed consumer chainId in consumerAddition gov proposal before voting finishes, the stored key format is prefix|proposalID, value is chainID -func ProposedConsumerChainKey(proposalID uint64) []byte { - return ccvtypes.AppendMany( - []byte{ProposedConsumerChainByteKey}, - sdk.Uint64ToBigEndian(proposalID), - ) -} - -// ParseProposedConsumerChainKey get the proposalID in the key -func ParseProposedConsumerChainKey(prefix byte, bz []byte) (uint64, error) { - expectedPrefix := []byte{prefix} - prefixL := len(expectedPrefix) - if prefix := bz[:prefixL]; !bytes.Equal(prefix, expectedPrefix) { - return 0, fmt.Errorf("invalid prefix; expected: %X, got: %X", expectedPrefix, prefix) - } - proposalID := sdk.BigEndianToUint64(bz[prefixL:]) - - return proposalID, nil -} - -// ConsumerValidatorKey returns the key of consumer chain `chainID` and validator with `providerAddr` -func ConsumerValidatorKey(chainID string, providerAddr []byte) []byte { - prefix := ChainIdWithLenKey(ConsumerValidatorBytePrefix, chainID) - return append(prefix, providerAddr...) -} - -// TopNKey returns the key used to store the Top N value per consumer chain. -// This value corresponds to the N% of the top validators that have to validate the consumer chain. -func TopNKey(chainID string) []byte { - return ChainIdWithLenKey(TopNBytePrefix, chainID) -} - -// ValidatorSetPowerKey returns the key of consumer chain `chainID` -func ValidatorsPowerCapKey(chainID string) []byte { - return ChainIdWithLenKey(ValidatorsPowerCapPrefix, chainID) -} - -// ValidatorSetCapKey returns the key of consumer chain `chainID` -func ValidatorSetCapKey(chainID string) []byte { - return ChainIdWithLenKey(ValidatorSetCapPrefix, chainID) -} - -// AllowlistCapKey returns the key to a validator's slash log -func AllowlistCapKey(chainID string, providerAddr ProviderConsAddress) []byte { - return append(ChainIdWithLenKey(AllowlistPrefix, chainID), providerAddr.ToSdkConsAddr().Bytes()...) -} - -// DenylistCapKey returns the key to a validator's slash log -func DenylistCapKey(chainID string, providerAddr ProviderConsAddress) []byte { - return append(ChainIdWithLenKey(DenylistPrefix, chainID), providerAddr.ToSdkConsAddr().Bytes()...) -} - -// OptedInKey returns the key used to store whether a validator is opted in on a consumer chain. -func OptedInKey(chainID string, providerAddr ProviderConsAddress) []byte { - prefix := ChainIdWithLenKey(OptedInBytePrefix, chainID) - return append(prefix, providerAddr.ToSdkConsAddr().Bytes()...) -} - -// ConsumerRewardsAllocationKey returns the key used to store the ICS rewards per consumer chain -func ConsumerRewardsAllocationKey(chainID string) []byte { - return append([]byte{ConsumerRewardsAllocationBytePrefix}, []byte(chainID)...) -} - -// ConsumerCommissionRateKey returns the key used to store the commission rate per validator per consumer chain. -func ConsumerCommissionRateKey(chainID string, providerAddr ProviderConsAddress) []byte { - return ChainIdAndConsAddrKey( - ConsumerCommissionRatePrefix, - chainID, - providerAddr.ToSdkConsAddr(), - ) -} - -func MinimumPowerInTopNKey(chainID string) []byte { - return ChainIdWithLenKey(MinimumPowerInTopNBytePrefix, chainID) -} - // // End of generic helpers section // diff --git a/x/ccv/provider/types/keys_test.go b/x/ccv/provider/types/keys_test.go index 3f6df5cb0a..30758de587 100644 --- a/x/ccv/provider/types/keys_test.go +++ b/x/ccv/provider/types/keys_test.go @@ -14,7 +14,7 @@ import ( // Tests that all singular keys, or prefixes to fully resolves keys are non duplicate byte values. func TestNoDuplicates(t *testing.T) { - prefixes := getAllKeyPrefixes() + prefixes := providertypes.GetAllKeyPrefixes() seen := []byte{} for _, prefix := range prefixes { @@ -23,47 +23,50 @@ func TestNoDuplicates(t *testing.T) { } } -// Returns all key prefixes to fully resolved keys, any of which should be a single, unique byte. -func getAllKeyPrefixes() []byte { - return []byte{ - providertypes.PortByteKey, - providertypes.MaturedUnbondingOpsByteKey, - providertypes.ValidatorSetUpdateIdByteKey, - providertypes.SlashMeterByteKey, - providertypes.SlashMeterReplenishTimeCandidateByteKey, - providertypes.ChainToChannelBytePrefix, - providertypes.ChannelToChainBytePrefix, - providertypes.ChainToClientBytePrefix, - providertypes.InitTimeoutTimestampBytePrefix, - providertypes.PendingCAPBytePrefix, - providertypes.PendingCRPBytePrefix, - providertypes.UnbondingOpBytePrefix, - providertypes.UnbondingOpIndexBytePrefix, - providertypes.ValsetUpdateBlockHeightBytePrefix, - providertypes.ConsumerGenesisBytePrefix, - providertypes.SlashAcksBytePrefix, - providertypes.InitChainHeightBytePrefix, - providertypes.PendingVSCsBytePrefix, - providertypes.VscSendTimestampBytePrefix, - providertypes.ThrottledPacketDataSizeBytePrefix, - providertypes.ThrottledPacketDataBytePrefix, - providertypes.GlobalSlashEntryBytePrefix, - providertypes.ConsumerValidatorsBytePrefix, - providertypes.ValidatorsByConsumerAddrBytePrefix, - providertypes.KeyAssignmentReplacementsBytePrefix, - providertypes.ConsumerAddrsToPruneBytePrefix, - providertypes.SlashLogBytePrefix, - providertypes.VSCMaturedHandledThisBlockBytePrefix, - providertypes.EquivocationEvidenceMinHeightBytePrefix, - providertypes.ProposedConsumerChainByteKey, - providertypes.ConsumerValidatorBytePrefix, - providertypes.OptedInBytePrefix, - providertypes.TopNBytePrefix, - providertypes.ConsumerRewardsAllocationBytePrefix, - providertypes.ConsumerCommissionRatePrefix, - providertypes.MinimumPowerInTopNBytePrefix, - providertypes.ParametersByteKey, - } +// Test that the value of all byte prefixes is preserved +func TestPreserveBytePrefix(t *testing.T) { + require.Equal(t, uint8(0xFF), providertypes.MustGetKeyPrefix("ParametersByteKey")) + require.Equal(t, uint8(0), providertypes.MustGetKeyPrefix("PortByteKey")) + require.Equal(t, uint8(1), providertypes.MustGetKeyPrefix("MaturedUnbondingOpsByteKey")) + require.Equal(t, uint8(2), providertypes.MustGetKeyPrefix("ValidatorSetUpdateIdByteKey")) + require.Equal(t, uint8(3), providertypes.MustGetKeyPrefix("SlashMeterByteKey")) + require.Equal(t, uint8(4), providertypes.MustGetKeyPrefix("SlashMeterReplenishTimeCandidateByteKey")) + require.Equal(t, uint8(5), providertypes.MustGetKeyPrefix("ChainToChannelBytePrefix")) + require.Equal(t, uint8(6), providertypes.MustGetKeyPrefix("ChannelToChainBytePrefix")) + require.Equal(t, uint8(7), providertypes.MustGetKeyPrefix("ChainToClientBytePrefix")) + require.Equal(t, uint8(8), providertypes.MustGetKeyPrefix("InitTimeoutTimestampBytePrefix")) + require.Equal(t, uint8(9), providertypes.MustGetKeyPrefix("PendingCAPBytePrefix")) + require.Equal(t, uint8(10), providertypes.MustGetKeyPrefix("PendingCRPBytePrefix")) + require.Equal(t, uint8(11), providertypes.MustGetKeyPrefix("UnbondingOpBytePrefix")) + require.Equal(t, uint8(12), providertypes.MustGetKeyPrefix("UnbondingOpIndexBytePrefix")) + require.Equal(t, uint8(13), providertypes.MustGetKeyPrefix("ValsetUpdateBlockHeightBytePrefix")) + require.Equal(t, uint8(14), providertypes.MustGetKeyPrefix("ConsumerGenesisBytePrefix")) + require.Equal(t, uint8(15), providertypes.MustGetKeyPrefix("SlashAcksBytePrefix")) + require.Equal(t, uint8(16), providertypes.MustGetKeyPrefix("InitChainHeightBytePrefix")) + require.Equal(t, uint8(17), providertypes.MustGetKeyPrefix("PendingVSCsBytePrefix")) + require.Equal(t, uint8(18), providertypes.MustGetKeyPrefix("VscSendTimestampBytePrefix")) + require.Equal(t, uint8(19), providertypes.MustGetKeyPrefix("ThrottledPacketDataSizeBytePrefix")) + require.Equal(t, uint8(20), providertypes.MustGetKeyPrefix("ThrottledPacketDataBytePrefix")) + require.Equal(t, uint8(21), providertypes.MustGetKeyPrefix("GlobalSlashEntryBytePrefix")) + require.Equal(t, uint8(22), providertypes.MustGetKeyPrefix("ConsumerValidatorsBytePrefix")) + require.Equal(t, uint8(23), providertypes.MustGetKeyPrefix("ValidatorsByConsumerAddrBytePrefix")) + require.Equal(t, uint8(24), providertypes.MustGetKeyPrefix("KeyAssignmentReplacementsBytePrefix")) + require.Equal(t, uint8(25), providertypes.MustGetKeyPrefix("ConsumerAddrsToPruneBytePrefix")) + require.Equal(t, uint8(26), providertypes.MustGetKeyPrefix("SlashLogBytePrefix")) + require.Equal(t, uint8(27), providertypes.MustGetKeyPrefix("ConsumerRewardDenomsBytePrefix")) + require.Equal(t, uint8(28), providertypes.MustGetKeyPrefix("VSCMaturedHandledThisBlockBytePrefix")) + require.Equal(t, uint8(29), providertypes.MustGetKeyPrefix("EquivocationEvidenceMinHeightBytePrefix")) + require.Equal(t, uint8(30), providertypes.MustGetKeyPrefix("ProposedConsumerChainByteKey")) + require.Equal(t, uint8(31), providertypes.MustGetKeyPrefix("ConsumerValidatorBytePrefix")) + require.Equal(t, uint8(32), providertypes.MustGetKeyPrefix("OptedInBytePrefix")) + require.Equal(t, uint8(33), providertypes.MustGetKeyPrefix("TopNBytePrefix")) + require.Equal(t, uint8(34), providertypes.MustGetKeyPrefix("ValidatorsPowerCapPrefix")) + require.Equal(t, uint8(35), providertypes.MustGetKeyPrefix("ValidatorSetCapPrefix")) + require.Equal(t, uint8(36), providertypes.MustGetKeyPrefix("AllowlistPrefix")) + require.Equal(t, uint8(37), providertypes.MustGetKeyPrefix("DenylistPrefix")) + require.Equal(t, uint8(38), providertypes.MustGetKeyPrefix("ConsumerRewardsAllocationBytePrefix")) + require.Equal(t, uint8(39), providertypes.MustGetKeyPrefix("ConsumerCommissionRatePrefix")) + require.Equal(t, uint8(40), providertypes.MustGetKeyPrefix("MinimumPowerInTopNBytePrefix")) } func TestNoPrefixOverlap(t *testing.T) { @@ -79,6 +82,7 @@ func TestNoPrefixOverlap(t *testing.T) { // Note we only care about checking prefixes here, so parameters into the key functions are arbitrary. func getAllFullyDefinedKeys() [][]byte { return [][]byte{ + providertypes.ParametersKey(), providertypes.PortKey(), providertypes.MaturedUnbondingOpsKey(), providertypes.ValidatorSetUpdateIdKey(), @@ -106,7 +110,19 @@ func getAllFullyDefinedKeys() [][]byte { providertypes.ConsumerAddrsToPruneKey("chainID", 88), providertypes.SlashLogKey(providertypes.NewProviderConsAddress([]byte{0x05})), providertypes.VSCMaturedHandledThisBlockKey(), + providertypes.ConsumerRewardDenomsKey("uatom"), providertypes.EquivocationEvidenceMinHeightKey("chainID"), + providertypes.ProposedConsumerChainKey(1), + providertypes.ConsumerValidatorKey("chainID", providertypes.NewProviderConsAddress([]byte{0x05}).Address.Bytes()), + providertypes.TopNKey("chainID"), + providertypes.ValidatorsPowerCapKey("chainID"), + providertypes.ValidatorSetCapKey("chainID"), + providertypes.AllowlistCapKey("chainID", providertypes.NewProviderConsAddress([]byte{0x05})), + providertypes.DenylistCapKey("chainID", providertypes.NewProviderConsAddress([]byte{0x05})), + providertypes.OptedInKey("chainID", providertypes.NewProviderConsAddress([]byte{0x05})), + providertypes.ConsumerRewardsAllocationKey("chainID"), + providertypes.ConsumerCommissionRateKey("chainID", providertypes.NewProviderConsAddress([]byte{0x05})), + providertypes.MinimumPowerInTopNKey("chainID"), } } @@ -263,14 +279,14 @@ func TestKeysWithPrefixAndId(t *testing.T) { } expectedBytePrefixes := []byte{ - providertypes.ChainToChannelBytePrefix, - providertypes.ChannelToChainBytePrefix, - providertypes.ChainToClientBytePrefix, - providertypes.InitTimeoutTimestampBytePrefix, - providertypes.ConsumerGenesisBytePrefix, - providertypes.SlashAcksBytePrefix, - providertypes.InitChainHeightBytePrefix, - providertypes.PendingVSCsBytePrefix, + providertypes.MustGetKeyPrefix("ChainToChannelBytePrefix"), + providertypes.MustGetKeyPrefix("ChannelToChainBytePrefix"), + providertypes.MustGetKeyPrefix("ChainToClientBytePrefix"), + providertypes.MustGetKeyPrefix("InitTimeoutTimestampBytePrefix"), + providertypes.MustGetKeyPrefix("ConsumerGenesisBytePrefix"), + providertypes.MustGetKeyPrefix("SlashAcksBytePrefix"), + providertypes.MustGetKeyPrefix("InitChainHeightBytePrefix"), + providertypes.MustGetKeyPrefix("PendingVSCsBytePrefix"), } tests := []struct { @@ -297,8 +313,8 @@ func TestKeysWithUint64Payload(t *testing.T) { } expectedBytePrefixes := []byte{ - providertypes.UnbondingOpBytePrefix, - providertypes.ValsetUpdateBlockHeightBytePrefix, + providertypes.MustGetKeyPrefix("UnbondingOpBytePrefix"), + providertypes.MustGetKeyPrefix("ValsetUpdateBlockHeightBytePrefix"), } tests := []struct { @@ -332,7 +348,7 @@ func TestParseProposedConsumerChainKey(t *testing.T) { for _, test := range tests { key := providertypes.ProposedConsumerChainKey(test.proposalID) pID, err := providertypes.ParseProposedConsumerChainKey( - providertypes.ProposedConsumerChainByteKey, key) + providertypes.MustGetKeyPrefix("ProposedConsumerChainByteKey"), key) require.NoError(t, err) require.Equal(t, pID, test.proposalID) }