diff --git a/x/ccv/provider/keeper/key_assignment.go b/x/ccv/provider/keeper/key_assignment.go index 21162ebb8a..6a7a296955 100644 --- a/x/ccv/provider/keeper/key_assignment.go +++ b/x/ccv/provider/keeper/key_assignment.go @@ -145,11 +145,11 @@ func (k Keeper) SetValidatorByConsumerAddr( func (k Keeper) GetValidatorsByConsumerAddr(ctx sdk.Context, consumerConsAddr sdk.ConsAddress) (validatorConsumerAddrs []types.ValidatorByConsumerAddr) { store := ctx.KVStore(k.storeKey) - prefix := types.PrefixWithConsAddress(types.ValidatorsByConsumerAddrBytePrefix, consumerConsAddr) + prefix := types.PrefixWithLenConsAddress(types.ValidatorsByConsumerAddrBytePrefix, consumerConsAddr) iterator := sdk.KVStorePrefixIterator(store, prefix) defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - chainID, consumerAddrTmp, err := types.ParseConsAddrKeyAndChainID(types.ValidatorsByConsumerAddrBytePrefix, consumerConsAddr, iterator.Key()) + chainID, consumerAddrTmp, err := types.ParseConsAddrKeyAndChainID(types.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. @@ -174,7 +174,7 @@ func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string) prefix := []byte{types.ValidatorsByConsumerAddrBytePrefix} iterator := sdk.KVStorePrefixIterator(store, prefix) for ; iterator.Valid(); iterator.Next() { - cID, consumerAddrTmp, err := types.ParseConsAddrKeyAndChainID(types.ValidatorsByConsumerAddrBytePrefix, consumerConsAddr, iterator.Key()) + cID, consumerAddrTmp, err := types.ParseConsAddrKeyAndChainID(types.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. diff --git a/x/ccv/provider/keeper/migration.go b/x/ccv/provider/keeper/migration.go index 4d268be498..5af01806bb 100644 --- a/x/ccv/provider/keeper/migration.go +++ b/x/ccv/provider/keeper/migration.go @@ -37,7 +37,7 @@ func (k Keeper) MigrateConsumerAddrStoreKey(ctx sdk.Context) error { consumerAddr := types.NewConsumerConsAddress(consumerAddrTmp) providerAddr := types.NewProviderConsAddress(iteratorCon.Value()) - // bytePrefix | ConsAddress | len(chainID) | chainID + // bytePrefix | len(ConsAddress) | ConsAddress | chainID k.SetValidatorByConsumerAddr(ctx, chainID, consumerAddr, providerAddr) // delete old kv diff --git a/x/ccv/provider/types/keys.go b/x/ccv/provider/types/keys.go index 9287056627..d533aa582f 100644 --- a/x/ccv/provider/types/keys.go +++ b/x/ccv/provider/types/keys.go @@ -352,7 +352,7 @@ func ConsumerValidatorsKeyLegacy(chainID string, addr ProviderConsAddress) []byt // ConsumerValidatorsKey returns the key under which the // validator assigned keys for every consumer chain are stored -// key: bytePrefix | ConsAddress | len(chainID) | chainID +// key bytePrefix | len(ConsAddress) | ConsAddress | chainID func ConsumerValidatorsKey(chainID string, addr ProviderConsAddress) []byte { return ChainIdAndConsAddrKey(ConsumerValidatorsBytePrefix, chainID, addr.ToSdkConsAddr()) } @@ -426,11 +426,13 @@ func ChainIdWithLenKey(prefix byte, chainID string) []byte { ) } -// PrefixWithConsAddress returns the key with the following format: +// PrefixWithLenConsAddress returns the key with the following format: // bytePrefix | Address -func PrefixWithConsAddress(prefix byte, addr sdk.ConsAddress) []byte { +func PrefixWithLenConsAddress(prefix byte, addr sdk.ConsAddress) []byte { + addrL := len(addr) return ccvtypes.AppendMany( []byte{prefix}, + sdk.Uint64ToBigEndian(uint64(addrL)), addr, ) } @@ -491,11 +493,9 @@ func ChainIdAndConsAddrKeyLegacy(prefix byte, chainID string, addr sdk.ConsAddre // ChainIdAndConsAddrKey returns the key with the following format: // bytePrefix | ConsAddress | len(chainID) | chainID func ChainIdAndConsAddrKey(prefix byte, chainID string, addr sdk.ConsAddress) []byte { - chainIdL := len(chainID) - partialKey := PrefixWithConsAddress(prefix, addr) + partialKey := PrefixWithLenConsAddress(prefix, addr) return ccvtypes.AppendMany( partialKey, - sdk.Uint64ToBigEndian(uint64(chainIdL)), // Append the chainID []byte(chainID), ) @@ -514,25 +514,18 @@ func ParseChainIdAndConsAddrKey(prefix byte, bz []byte) (string, sdk.ConsAddress return chainID, addr, nil } -// ParseConsAddrKeyAndChainID returns the chain ID and ConsAddress for a ConsAddrKeyAndChainID key: bytePrefix | ConsAddress | len(chainID) | chainID -func ParseConsAddrKeyAndChainID(prefix byte, consAddr sdk.ConsAddress, bz []byte) (string, sdk.ConsAddress, error) { +// ParseConsAddrKeyAndChainID returns the chain ID and ConsAddress for a ConsAddrKeyAndChainID key: bytePrefix | len(ConsAddress) | ConsAddress | chainID +func ParseConsAddrKeyAndChainID(prefix byte, bz []byte) (string, sdk.ConsAddress, error) { expectedPrefix := []byte{prefix} prefixL := len(expectedPrefix) - addrL := len(consAddr) - if prefix := bz[:prefixL]; !bytes.Equal(prefix, expectedPrefix) { return "", nil, fmt.Errorf("invalid prefix; expected: %X, got: %X", expectedPrefix, prefix) } + addrL := sdk.BigEndianToUint64(bz[prefixL : prefixL+8]) + addr := bz[prefixL+8 : prefixL+8+int(addrL)] + chainID := string(bz[prefixL+8+int(addrL):]) - if addr := bz[prefixL:addrL]; bytes.Equal(addr, consAddr) { - return "", nil, fmt.Errorf("invalid prefix concensus address; expected: %X, got: %X", addr, consAddr) - } - prefixAddrL := len(expectedPrefix) + len(consAddr) - - chainIdL := sdk.BigEndianToUint64(bz[prefixAddrL : prefixAddrL+8]) - chainID := string(bz[prefixAddrL+8 : prefixAddrL+8+int(chainIdL)]) - - return chainID, consAddr, nil + return chainID, addr, nil } func VSCMaturedHandledThisBlockKey() []byte {