Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key assignment related panic cleanup #642

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions x/ccv/provider/keeper/key_assignment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand All @@ -25,7 +27,9 @@ func (k Keeper) GetValidatorConsumerPubKey(
}
err := consumerKey.Unmarshal(bz)
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the consumer key is assumed to be correctly serialized in SetValidatorConsumerPubKey.
panic(fmt.Sprintf("failed to unmarshal consumer key: %v", err))
}
return consumerKey, true
}
Expand All @@ -40,7 +44,10 @@ func (k Keeper) SetValidatorConsumerPubKey(
store := ctx.KVStore(k.storeKey)
bz, err := consumerKey.Marshal()
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the consumer key is obtained from GetValidatorConsumerPubKey, called from
// TODO: not sure that consumerKey is actually validated by all possible code paths
danwt marked this conversation as resolved.
Show resolved Hide resolved
danwt marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Sprintf("failed to marshal consumer key: %v", err))
}
store.Set(types.ConsumerValidatorsKey(chainID, providerAddr), bz)
}
Expand Down Expand Up @@ -68,12 +75,16 @@ func (k Keeper) GetAllValidatorConsumerPubKeys(ctx sdk.Context, chainID *string)
for ; iterator.Valid(); iterator.Next() {
chainID, providerAddr, err := types.ParseChainIdAndConsAddrKey(types.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.
panic(err)
}
var consumerKey tmprotocrypto.PublicKey
err = consumerKey.Unmarshal(iterator.Value())
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the consumer key is assumed to be correctly serialized in SetValidatorConsumerPubKey.
panic(fmt.Sprintf("failed to unmarshal consumer key: %v", err))
}

validatorConsumerPubKeys = append(validatorConsumerPubKeys, types.ValidatorConsumerPubKey{
Expand Down Expand Up @@ -106,7 +117,9 @@ func (k Keeper) GetValidatorByConsumerAddr(
}
err := providerAddr.Unmarshal(bz)
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the provider address is assumed to be correctly serialized in SetValidatorByConsumerAddr.
panic(fmt.Sprintf("failed to unmarshal provider address: %v", err))
}
return providerAddr, true
}
Expand All @@ -120,10 +133,8 @@ func (k Keeper) SetValidatorByConsumerAddr(
providerAddr sdk.ConsAddress,
) {
store := ctx.KVStore(k.storeKey)
bz, err := providerAddr.Marshal()
if err != nil {
panic(err)
}
// Cons address is a type alias for a byte string, no marshaling needed
bz := providerAddr
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
store.Set(types.ValidatorsByConsumerAddrKey(chainID, consumerAddr), bz)
}

Expand Down Expand Up @@ -151,12 +162,16 @@ func (k Keeper) GetAllValidatorsByConsumerAddr(ctx sdk.Context, chainID *string)
for ; iterator.Valid(); iterator.Next() {
chainID, consumerAddr, err := types.ParseChainIdAndConsAddrKey(types.ValidatorsByConsumerAddrBytePrefix, iterator.Key())
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in SetValidatorByConsumerAddr.
panic(fmt.Sprintf("failed to parse chainID and consumer address: %v", err))
}
var providerAddr sdk.ConsAddress
err = providerAddr.Unmarshal(iterator.Value())
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the provider address is assumed to be correctly serialized in SetValidatorByConsumerAddr.
panic(fmt.Sprintf("failed to unmarshal provider address: %v", err))
}

validatorConsumerAddrs = append(validatorConsumerAddrs, types.ValidatorByConsumerAddr{
Expand Down Expand Up @@ -193,7 +208,9 @@ func (k Keeper) GetKeyAssignmentReplacement(

err := pubKeyAndPower.Unmarshal(bz)
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the public key and power are assumed to be correctly serialized in SetKeyAssignmentReplacement.
panic(fmt.Sprintf("failed to unmarshal public key and power: %v", err))
}
return pubKeyAndPower.PubKey, pubKeyAndPower.Power, true
}
Expand All @@ -212,7 +229,11 @@ func (k Keeper) SetKeyAssignmentReplacement(
pubKeyAndPower := abci.ValidatorUpdate{PubKey: prevCKey, Power: power}
bz, err := pubKeyAndPower.Marshal()
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// prevCKey is obtained from GetValidatorConsumerPubKey (called from AssignConsumerKey),
// and power is obtained from GetLastValidatorPower (called from AssignConsumerKey).
// Both of which are assumed to return valid values.
panic(fmt.Sprintf("failed to marshal public key and power: %v", err))
}
store.Set(types.KeyAssignmentReplacementsKey(chainID, providerAddr), bz)
}
Expand All @@ -231,12 +252,16 @@ func (k Keeper) GetAllKeyAssignmentReplacements(ctx sdk.Context, chainID string)
for ; iterator.Valid(); iterator.Next() {
_, providerAddr, err := types.ParseChainIdAndConsAddrKey(types.KeyAssignmentReplacementsBytePrefix, iterator.Key())
if err != nil {
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in SetKeyAssignmentReplacement.
panic(err)
}
var pubKeyAndPower abci.ValidatorUpdate
err = pubKeyAndPower.Unmarshal(iterator.Value())
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the public key and power are assumed to be correctly serialized in SetKeyAssignmentReplacement.
panic(fmt.Sprintf("failed to unmarshal public key and power: %v", err))
}

replacements = append(replacements, types.KeyAssignmentReplacement{
Expand Down Expand Up @@ -272,12 +297,17 @@ func (k Keeper) AppendConsumerAddrsToPrune(ctx sdk.Context, chainID string, vscI
if bz != nil {
err := consumerAddrsToPrune.Unmarshal(bz)
if err != nil {
// An error here would indicate something is very wrong,
// the data bytes are assumed to be correctly serialized by previous calls to this method.
panic(err)
}
}
// TODO: do we need to validate the consumerAddr we're appending here?
danwt marked this conversation as resolved.
Show resolved Hide resolved
consumerAddrsToPrune.Addresses = append(consumerAddrsToPrune.Addresses, consumerAddr)
danwt marked this conversation as resolved.
Show resolved Hide resolved
bz, err := consumerAddrsToPrune.Marshal()
if err != nil {
// An error here would indicate something is very wrong,
// consumerAddrsToPrune is instantiated in this method and should be able to be marshaled.
panic(err)
}
store.Set(types.ConsumerAddrsToPruneKey(chainID, vscID), bz)
Expand All @@ -297,7 +327,9 @@ func (k Keeper) GetConsumerAddrsToPrune(
}
err := consumerAddrsToPrune.Unmarshal(bz)
if err != nil {
panic(err)
// An error here would indicate something is very wrong,
// the list of consumer addresses is assumed to be correctly serialized in AppendConsumerAddrsToPrune.
panic(fmt.Sprintf("failed to unmarshal consumer addresses to prune: %v", err))
}
return
}
Expand All @@ -315,11 +347,15 @@ func (k Keeper) GetAllConsumerAddrsToPrune(ctx sdk.Context, chainID string) (con
for ; iterator.Valid(); iterator.Next() {
_, vscID, err := types.ParseChainIdAndUintIdKey(types.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.
panic(err)
}
var addrs types.AddressList
err = addrs.Unmarshal(iterator.Value())
if err != nil {
// An error here would indicate something is very wrong,
// the list of consumer addresses is assumed to be correctly serialized in AppendConsumerAddrsToPrune.
panic(err)
}

Expand Down
15 changes: 9 additions & 6 deletions x/ccv/provider/keeper/key_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,19 @@ func TestConsumerAddrsToPruneCRUD(t *testing.T) {
keeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

addrsToPrune := keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID).Addresses
require.Empty(t, addrsToPrune)

keeper.AppendConsumerAddrsToPrune(ctx, chainID, vscID, consumerAddr)

addrToPrune := keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID).Addresses
require.NotEmpty(t, addrToPrune, "address to prune is empty")
require.Len(t, addrToPrune, 1, "address to prune is not len 1")
require.Equal(t, sdk.ConsAddress(addrToPrune[0]), consumerAddr)
addrsToPrune = keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID).Addresses
require.NotEmpty(t, addrsToPrune, "addresses to prune is empty")
require.Len(t, addrsToPrune, 1, "addresses to prune is not len 1")
require.Equal(t, sdk.ConsAddress(addrsToPrune[0]), consumerAddr)

keeper.DeleteConsumerAddrsToPrune(ctx, chainID, vscID)
addrToPrune = keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID).Addresses
require.Empty(t, addrToPrune, "address to prune was returned")
addrsToPrune = keeper.GetConsumerAddrsToPrune(ctx, chainID, vscID).Addresses
require.Empty(t, addrsToPrune, "addresses to prune was returned")
}

func TestGetAllConsumerAddrsToPrune(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions x/ccv/provider/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (gs GenesisState) Validate() error {
return err
}

for _, pk := range gs.ValidatorConsumerPubkeys {
if err := sdk.VerifyAddressFormat(pk.ProviderAddr); err != nil {
return sdkerrors.Wrap(ccv.ErrInvalidGenesis, fmt.Sprintf("invalid provider address: %s", pk.ProviderAddr))
}
// TODO: do we need to validate the consumer key?
danwt marked this conversation as resolved.
Show resolved Hide resolved
danwt marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

Expand Down