From 0034e4845826bf97032d08a13c37d68d414cc573 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 19 Nov 2020 01:10:11 +0100 Subject: [PATCH] Bech32 PubKey: use protobuf in slashing keeper --- x/slashing/keeper/keeper.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 999e82404b..5264e45948 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -42,33 +42,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) + 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)) + bz := store.Get(types.AddrPubkeyRelationKey(a)) + if bz == nil { + return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a)) } - - pkStr, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubkey.Value) - if err != nil { - return pkStr, err - } - - 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