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

Bech32 PubKey: use protobuf in slashing keeper #7979

Merged
merged 4 commits into from
Nov 20, 2020
Merged
Changes from 2 commits
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
43 changes: 14 additions & 29 deletions x/slashing/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package keeper
import (
"fmt"

gogotypes "github.com/gogo/protobuf/types"

"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -42,33 +40,27 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// AddPubkey sets a address-pubkey relation
func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) {
addr := pubkey.Address()

pkStr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pubkey)
func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error {
bz, err := codec.MarshalAny(k.cdc, pubkey)
if err != nil {
panic(fmt.Errorf("error while setting address-pubkey relation: %s", addr))
return err
}

k.setAddrPubkeyRelation(ctx, addr, pkStr)
store := ctx.KVStore(k.storeKey)
key := types.AddrPubkeyRelationKey(pubkey.Address())
store.Set(key, bz)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// GetPubkey returns the pubkey from the adddress-pubkey relation
func (k Keeper) GetPubkey(ctx sdk.Context, address cryptotypes.Address) (cryptotypes.PubKey, error) {
func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
store := ctx.KVStore(k.storeKey)

var pubkey gogotypes.StringValue
err := k.cdc.UnmarshalBinaryBare(store.Get(types.AddrPubkeyRelationKey(address)), &pubkey)
if err != nil {
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address))
}

pkStr, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubkey.Value)
if err != nil {
return pkStr, err
bz := store.Get(types.AddrPubkeyRelationKey(a))
if bz == nil {
return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a))
}

return pkStr, nil
var pk cryptotypes.PubKey
err := codec.UnmarshalAny(k.cdc, &pk, bz)
return pk, err
}

// Slash attempts to slash a validator. The slash is delegated to the staking
Expand Down Expand Up @@ -99,13 +91,6 @@ func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) {
k.sk.Jail(ctx, consAddr)
}

func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address, pubkey string) {
store := ctx.KVStore(k.storeKey)

bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: pubkey})
store.Set(types.AddrPubkeyRelationKey(addr), bz)
}

func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.AddrPubkeyRelationKey(addr))
Expand Down