From ccb71a2afafdc19261dccf2ca82f6425da37f161 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 4 Apr 2018 23:22:13 -0400 Subject: [PATCH] pool using time and end block handler staking --- x/stake/handler.go | 12 ++++++++++++ x/stake/keeper.go | 19 +++++++++---------- x/stake/tick.go | 4 +++- x/stake/types.go | 18 ++++++++++-------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/x/stake/handler.go b/x/stake/handler.go index 094d01aea..2f351a608 100644 --- a/x/stake/handler.go +++ b/x/stake/handler.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + abci "github.com/tendermint/abci/types" ) //nolint @@ -35,6 +36,17 @@ func NewHandler(k Keeper, ck bank.CoinKeeper) sdk.Handler { } } +//_______________________________________________ + +// NewEndBlocker generates sdk.EndBlocker +// Performs tick functionality +func NewEndBlocker(k Keeper) sdk.EndBlocker { + return func(ctx sdk.Context, req abci.RequestEndBlock) (res abci.ResponseEndBlock) { + res.ValidatorUpdates = k.Tick(ctx) + return + } +} + //_____________________________________________________________________ // These functions assume everything has been authenticated, diff --git a/x/stake/keeper.go b/x/stake/keeper.go index d377df21f..77f423fec 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/bank" + abci "github.com/tendermint/abci/types" ) // keeper of the staking store @@ -146,7 +147,7 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) { if store.Get(GetRecentValidatorKey(address)) == nil { return } - bz, err := k.cdc.MarshalBinary(Validator{address, sdk.ZeroRat}) + bz, err := k.cdc.MarshalBinary(abci.Validator{address, 0}) if err != nil { panic(err) } @@ -167,7 +168,7 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { iterator := store.Iterator(subspace(RecentValidatorsKey)) for ; iterator.Valid(); iterator.Next() { addr := AddrFromKey(iterator.Key()) - store.Set(GetToKickOutValidatorKey(addr), []byte{}) + store.Set(GetToKickOutValidatorKey(addr), iterator.Value()) // iterator.Value is the validator pubkey store.Delete(iterator.Key()) } iterator.Close() @@ -194,7 +195,7 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { store.Delete(GetToKickOutValidatorKey(val.Address)) // also add to the recent validators group - store.Set(GetRecentValidatorKey(val.Address), bz) // XXX should store nothing + store.Set(GetRecentValidatorKey(val.Address), bz) iterator.Next() } @@ -203,7 +204,8 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { iterator = store.Iterator(subspace(ToKickOutValidatorsKey)) for ; iterator.Valid(); iterator.Next() { addr := AddrFromKey(iterator.Key()) - bz, err := k.cdc.MarshalBinary(Validator{addr, sdk.ZeroRat}) + validator = iterator.Key() + bz, err := k.cdc.MarshalBinary(abci.Validator{addr, 0}) if err != nil { panic(err) } @@ -255,7 +257,7 @@ func (k Keeper) IsRecentValidator(ctx sdk.Context, address sdk.Address) bool { // Accumulated updates to the validator set // get the most recently updated validators -func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []Validator) { +func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validator) { store := ctx.KVStore(k.storeKey) iterator := store.Iterator(subspace(AccUpdateValidatorsKey)) //smallest to largest @@ -275,12 +277,9 @@ func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []Validator) { // remove all validator update entries func (k Keeper) clearAccUpdateValidators(ctx sdk.Context) { store := ctx.KVStore(k.storeKey) - k.deleteSubSpace(store, AccUpdateValidatorsKey) -} -// TODO move to common functionality somewhere -func (k Keeper) deleteSubSpace(store sdk.KVStore, key []byte) { - iterator := store.Iterator(subspace(key)) + // delete subspace + iterator := store.Iterator(subspace(AccUpdateValidatorsKey)) for ; iterator.Valid(); iterator.Next() { store.Delete(iterator.Key()) } diff --git a/x/stake/tick.go b/x/stake/tick.go index 0a85cd865..129be58f0 100644 --- a/x/stake/tick.go +++ b/x/stake/tick.go @@ -2,6 +2,7 @@ package stake import ( sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/abci/types" ) const ( @@ -12,7 +13,7 @@ const ( var hrsPerYrRat = sdk.NewRat(hrsPerYr) // as defined by a julian year of 365.25 days // Tick - called at the end of every block -func (k Keeper) Tick(ctx sdk.Context) (change []Validator) { +func (k Keeper) Tick(ctx sdk.Context) (change []abci.Validator) { p := k.GetPool(ctx) // Process Validator Provisions @@ -26,6 +27,7 @@ func (k Keeper) Tick(ctx sdk.Context) (change []Validator) { k.setPool(ctx, p) change = k.getAccUpdateValidators(ctx) + return } diff --git a/x/stake/types.go b/x/stake/types.go index a5d85bc7a..d13a64967 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -2,6 +2,8 @@ package stake import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + abci "github.com/tendermint/abci/types" crypto "github.com/tendermint/go-crypto" ) @@ -105,6 +107,7 @@ func (c Candidate) delegatorShareExRate() sdk.Rat { func (c Candidate) validator() Validator { return Validator{ Address: c.Address, + PubKey: c.PubKey, VotingPower: c.Assets, } } @@ -116,23 +119,22 @@ func (c Candidate) validator() Validator { // Validator is one of the top Candidates type Validator struct { - Address sdk.Address `json:"address"` // Address of validator - VotingPower sdk.Rat `json:"voting_power"` // Voting power if considered a validator + Address sdk.Address `json:"address"` + PubKey sdk.PubKey `json:"PubKey"` + VotingPower sdk.Rat `json:"voting_power"` } // ABCIValidator - Get the validator from a bond value -/* TODO -func (v Validator) ABCIValidator() (*abci.Validator, error) { +func (v Validator) ABCIValidator() abci.Validator { pkBytes, err := wire.MarshalBinary(v.PubKey) if err != nil { - return nil, err + panic(err) } - return &abci.Validator{ + return abci.Validator{ PubKey: pkBytes, Power: v.VotingPower.Evaluate(), - }, nil + } } -*/ //_________________________________________________________________________