Skip to content

Commit

Permalink
modify key parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaru Wang committed Sep 7, 2023
1 parent 95cf3b3 commit ab9d082
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
6 changes: 3 additions & 3 deletions x/ccv/provider/keeper/key_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 12 additions & 19 deletions x/ccv/provider/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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),
)
Expand All @@ -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 {
Expand Down

0 comments on commit ab9d082

Please sign in to comment.