2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-07-24 19:12:48 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Implements ValidatorSet
|
|
|
|
var _ sdk.ValidatorSet = Keeper{}
|
|
|
|
|
|
|
|
// iterate through the active validator set and perform the provided function
|
|
|
|
func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, ValidatorsKey)
|
|
|
|
i := int64(0)
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2018-07-03 22:32:49 -07:00
|
|
|
addr := iterator.Key()[1:]
|
2018-07-04 14:07:06 -07:00
|
|
|
validator := types.MustUnmarshalValidator(k.cdc, addr, iterator.Value())
|
2018-06-26 19:00:12 -07:00
|
|
|
stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
|
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
iterator.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through the active validator set and perform the provided function
|
|
|
|
func (k Keeper) IterateValidatorsBonded(ctx sdk.Context, fn func(index int64, validator sdk.Validator) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, ValidatorsBondedIndexKey)
|
|
|
|
i := int64(0)
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2018-07-02 17:58:22 -07:00
|
|
|
address := GetAddressFromValBondedIndexKey(iterator.Key())
|
2018-06-26 19:00:12 -07:00
|
|
|
validator, found := k.GetValidator(ctx, address)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("validator record not found for address: %v\n", address))
|
|
|
|
}
|
|
|
|
|
|
|
|
stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
|
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
iterator.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the sdk.validator for a particular address
|
2018-07-06 00:06:53 -07:00
|
|
|
func (k Keeper) Validator(ctx sdk.Context, address sdk.AccAddress) sdk.Validator {
|
2018-06-26 19:00:12 -07:00
|
|
|
val, found := k.GetValidator(ctx, address)
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:12:48 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// total power from the bond
|
|
|
|
func (k Keeper) TotalPower(ctx sdk.Context) sdk.Rat {
|
|
|
|
pool := k.GetPool(ctx)
|
2018-07-13 13:46:14 -07:00
|
|
|
return pool.BondedTokens
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//__________________________________________________________________________
|
|
|
|
|
|
|
|
// Implements DelegationSet
|
|
|
|
|
|
|
|
var _ sdk.DelegationSet = Keeper{}
|
|
|
|
|
|
|
|
// Returns self as it is both a validatorset and delegationset
|
|
|
|
func (k Keeper) GetValidatorSet() sdk.ValidatorSet {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the delegation for a particular set of delegator and validator addresses
|
2018-07-06 00:06:53 -07:00
|
|
|
func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.AccAddress, addrVal sdk.AccAddress) sdk.Delegation {
|
2018-06-26 19:00:12 -07:00
|
|
|
bond, ok := k.GetDelegation(ctx, addrDel, addrVal)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return bond
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through the active validator set and perform the provided function
|
2018-07-06 00:06:53 -07:00
|
|
|
func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress, fn func(index int64, delegation sdk.Delegation) (stop bool)) {
|
2018-06-26 19:00:12 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2018-07-02 15:16:47 -07:00
|
|
|
key := GetDelegationsKey(delAddr)
|
2018-06-26 19:00:12 -07:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, key)
|
|
|
|
i := int64(0)
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2018-07-04 14:07:06 -07:00
|
|
|
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
|
2018-06-26 19:00:12 -07:00
|
|
|
stop := fn(i, delegation) // XXX is this safe will the fields be able to get written to?
|
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
iterator.Close()
|
|
|
|
}
|