Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 26, 2022
1 parent eaac67b commit 6ed4d3c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
32 changes: 16 additions & 16 deletions x/ccv/provider/keeper/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ func MakeKeyMap(store Store) KeyMap {
}
}

func (e *KeyMap) SetProviderPubKeyToConsumerPubKey(pk ProviderPubKey, ck ConsumerPubKey) error {
if _, ok := e.Store.GetCkToPk(ck); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
if _, ok := e.Store.GetCcaToLastUpdateMemo(PubKeyToConsAddr(ck)); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
pca := PubKeyToConsAddr(pk)
if oldCk, ok := e.Store.GetPcaToCk(pca); ok {
e.Store.DelCkToPk(oldCk)
}
e.Store.SetPcaToCk(pca, ck)
e.Store.SetCkToPk(ck, pk)
return nil
}

func (e *KeyMap) DeleteProviderKey(pca ProviderConsAddr) error {
// TODO: document expensive operation
if ck, ok := e.Store.GetPcaToCk(pca); ok {
Expand All @@ -142,22 +158,6 @@ func (e *KeyMap) DeleteProviderKey(pca ProviderConsAddr) error {
return nil
}

func (e *KeyMap) SetProviderPubKeyToConsumerPubKey(pk ProviderPubKey, ck ConsumerPubKey) error {
if _, ok := e.Store.GetCkToPk(ck); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
if _, ok := e.Store.GetCcaToLastUpdateMemo(PubKeyToConsAddr(ck)); ok {
return errors.New(`cannot reuse key which is in use or was recently in use`)
}
pca := PubKeyToConsAddr(pk)
if oldCk, ok := e.Store.GetPcaToCk(pca); ok {
e.Store.DelCkToPk(oldCk)
}
e.Store.SetPcaToCk(pca, ck)
e.Store.SetCkToPk(ck, pk)
return nil
}

func (e *KeyMap) GetCurrentConsumerPubKeyFromProviderPubKey(pk ProviderPubKey) (ck ConsumerPubKey, found bool) {
return e.Store.GetPcaToCk(PubKeyToConsAddr(pk))
}
Expand Down
12 changes: 3 additions & 9 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
"github.com/cosmos/interchain-security/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
Expand Down Expand Up @@ -203,17 +202,12 @@ func (k Keeper) OnRecvSlashPacket(ctx sdk.Context, packet channeltypes.Packet, d
}

// TODO: should this be a panic or an error?
func GetSlashingProviderConsAddr(keymap *KeyMap, consumerConsAddress sdk.ConsAddress) (sdk.ConsAddress, error) {
func GetProviderConsAddr(keymap *KeyMap, consumerConsAddress sdk.ConsAddress) (sdk.ConsAddress, error) {
providerPublicKey, found := keymap.GetProviderPubKeyFromConsumerConsAddress(consumerConsAddress)
if !found {
return nil, errors.New("could not find provider address for slashing")
}
pk, err := cryptocodec.FromTmProtoPublicKey(providerPublicKey)
if err != nil {
panic("could not get sdk public key from tendermint proto public key for slashing")
}
return sdk.GetConsAddress(pk), nil

return PubKeyToConsAddr(providerPublicKey), nil
}

// HandleSlashPacket slash and jail a misbehaving validator according the infraction type
Expand All @@ -234,7 +228,7 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.Slas

// TODO: document better
consumerConsAddr := sdk.ConsAddress(data.Validator.Address)
providerConsAddr, err := GetSlashingProviderConsAddr(k.KeyMap(ctx, chainID), consumerConsAddr)
providerConsAddr, err := GetProviderConsAddr(k.KeyMap(ctx, chainID), consumerConsAddr)
if err != nil {
return false, nil
}
Expand Down

0 comments on commit 6ed4d3c

Please sign in to comment.