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
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]

### Improvements
- (SDK) [\#7925](https://github.com/cosmos/cosmos-sdk/pull/7925) Updated dependencies to use gRPC v1.33.2
* (SDK) [\#7925](https://github.com/cosmos/cosmos-sdk/pull/7925) Updated dependencies to use gRPC v1.33.2
* Updated gRPC dependency to v1.33.2
* Updated iavl dependency to v0.15-rc2
* (version) [\#7848](https://github.com/cosmos/cosmos-sdk/pull/7848) [\#7941](https://github.com/cosmos/cosmos-sdk/pull/7941) `version --long` output now shows the list of build dependencies and replaced build dependencies.

### State Machine Breaking Changes
* (x/upgrade) [\#7979](https://github.com/cosmos/cosmos-sdk/pull/7979) keeper pubkey storage serialization migration from bech32 to protobuf.

### Bug Fixes

* (crypto) [\#7966](https://github.com/cosmos/cosmos-sdk/issues/7966) `Bip44Params` `String()` function now correctly returns the absolute HD path by adding the `m/` prefix.
Expand Down
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