Bech32 PubKey: use protobuf in slashing keeper

This commit is contained in:
Robert Zaremba 2020-11-19 01:10:11 +01:00
parent 35183a37e6
commit 0034e48458

View File

@ -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