From 5f03e370c313d492858615af2d61c9af9c0f4a5d Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 31 May 2018 00:19:23 +0200 Subject: [PATCH] Remove ValidatorByPubKey, don't marshal sdk.Address --- types/stake.go | 11 +++++------ x/slashing/keeper_test.go | 10 +++++----- x/stake/keeper.go | 18 +++--------------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/types/stake.go b/types/stake.go index cd1a68533..bfcef7fa0 100644 --- a/types/stake.go +++ b/types/stake.go @@ -56,12 +56,11 @@ type ValidatorSet interface { IterateValidatorsBonded(Context, func(index int64, validator Validator) (stop bool)) - Validator(Context, Address) Validator // get a particular validator by owner address - ValidatorByPubKey(Context, crypto.PubKey) Validator // get a particular validator by public key - TotalPower(Context) Rat // total power of the validator set - Slash(Context, crypto.PubKey, int64, Rat) // slash the validator and delegators of the validator, specifying offence height & slash fraction - Revoke(Context, crypto.PubKey) // revoke a validator - Unrevoke(Context, crypto.PubKey) // unrevoke a validator + Validator(Context, Address) Validator // get a particular validator by owner address + TotalPower(Context) Rat // total power of the validator set + Slash(Context, crypto.PubKey, int64, Rat) // slash the validator and delegators of the validator, specifying offence height & slash fraction + Revoke(Context, crypto.PubKey) // revoke a validator + Unrevoke(Context, crypto.PubKey) // unrevoke a validator } //_______________________________________________________________________________ diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index 2fef0c974..70f42b324 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -62,7 +62,7 @@ func TestHandleAbsentValidator(t *testing.T) { require.Equal(t, int64(0), info.StartHeight) require.Equal(t, SignedBlocksWindow-50, info.SignedBlocksCounter) // validator should be bonded still - validator := sk.ValidatorByPubKey(ctx, val) + validator, _ := sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) pool := sk.GetPool(ctx) require.Equal(t, int64(100), pool.BondedTokens) @@ -74,7 +74,7 @@ func TestHandleAbsentValidator(t *testing.T) { require.Equal(t, int64(0), info.StartHeight) require.Equal(t, SignedBlocksWindow-51, info.SignedBlocksCounter) // validator should have been revoked - validator = sk.ValidatorByPubKey(ctx, val) + validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Unbonded, validator.GetStatus()) // unrevocation should fail prior to jail expiration got = slh(ctx, NewMsgUnrevoke(addr)) @@ -84,7 +84,7 @@ func TestHandleAbsentValidator(t *testing.T) { got = slh(ctx, NewMsgUnrevoke(addr)) require.True(t, got.IsOK()) // validator should be rebonded now - validator = sk.ValidatorByPubKey(ctx, val) + validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) // validator should have been slashed pool = sk.GetPool(ctx) @@ -98,7 +98,7 @@ func TestHandleAbsentValidator(t *testing.T) { height++ ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val, false) - validator = sk.ValidatorByPubKey(ctx, val) + validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) // validator should be revoked again after 100 unsigned blocks nextHeight := height + 100 @@ -106,6 +106,6 @@ func TestHandleAbsentValidator(t *testing.T) { ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val, false) } - validator = sk.ValidatorByPubKey(ctx, val) + validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Unbonded, validator.GetStatus()) } diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 4f93308c7..bab1328be 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -42,12 +42,10 @@ func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.Address) (validator Valid // get a single validator by pubkey func (k Keeper) GetValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) (validator Validator, found bool) { store := ctx.KVStore(k.storeKey) - b := store.Get(GetValidatorByPubKeyKey(pubkey)) - if b == nil { + addr := store.Get(GetValidatorByPubKeyKey(pubkey)) + if addr == nil { return validator, false } - var addr sdk.Address - k.cdc.MustUnmarshalBinary(b, &addr) return k.getValidator(store, addr) } @@ -68,8 +66,7 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) { bz := k.cdc.MustMarshalBinary(validator) store.Set(GetValidatorKey(validator.Owner), bz) // set pointer by pubkey - bz = k.cdc.MustMarshalBinary(validator.Owner) - store.Set(GetValidatorByPubKeyKey(validator.PubKey), bz) + store.Set(GetValidatorByPubKeyKey(validator.PubKey), validator.Owner) } // Get the set of all validators with no limits, used during genesis dump @@ -740,15 +737,6 @@ func (k Keeper) Validator(ctx sdk.Context, addr sdk.Address) sdk.Validator { return val } -// get the sdk.validator for a particular pubkey -func (k Keeper) ValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) sdk.Validator { - val, found := k.GetValidatorByPubKey(ctx, pubkey) - if !found { - return nil - } - return val -} - // total power from the bond func (k Keeper) TotalPower(ctx sdk.Context) sdk.Rat { pool := k.GetPool(ctx)