pool using time and end block handler staking

This commit is contained in:
rigelrozanski 2018-04-04 23:22:13 -04:00
parent 7d31ba835a
commit ccb71a2afa
4 changed files with 34 additions and 19 deletions

View File

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

View File

@ -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())
}

View File

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

View File

@ -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
}
}
*/
//_________________________________________________________________________