cosmos-sdk/x/stake/mapper.go

367 lines
10 KiB
Go
Raw Normal View History

2018-01-11 21:30:39 -08:00
package stake
import (
crypto "github.com/tendermint/go-crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
2018-01-11 21:30:39 -08:00
)
//nolint
2018-01-11 21:30:39 -08:00
var (
// Keys for store prefixes
CandidatesPubKeysKey = []byte{0x01} // key for all candidates' pubkeys
ParamKey = []byte{0x02} // key for global parameters relating to staking
GlobalStateKey = []byte{0x03} // key for global parameters relating to staking
// Key prefixes
CandidateKeyPrefix = []byte{0x04} // prefix for each key to a candidate
ValidatorKeyPrefix = []byte{0x05} // prefix for each key to a candidate
ValidatorUpdatesKeyPrefix = []byte{0x06} // prefix for each key to a candidate
DelegatorBondKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond
DelegatorBondsKeyPrefix = []byte{0x08} // prefix for each key to a delegator's bond
2018-01-11 21:30:39 -08:00
)
// GetCandidateKey - get the key for the candidate with pubKey
func GetCandidateKey(pubKey crypto.PubKey) []byte {
return append(CandidateKeyPrefix, pubKey.Bytes()...)
}
2018-01-25 01:06:25 -08:00
// GetValidatorKey - get the key for the validator used in the power-store
func GetValidatorKey(pubKey crypto.PubKey, power sdk.Rational) []byte {
b, _ := cdc.MarshalJSON(power) // TODO need to handle error here?
2018-01-25 01:06:25 -08:00
return append(ValidatorKeyPrefix, append(b, pubKey.Bytes()...)...) // TODO does this need prefix if its in its own store
}
// GetValidatorUpdatesKey - get the key for the validator used in the power-store
func GetValidatorUpdatesKey(pubKey crypto.PubKey) []byte {
return append(ValidatorUpdatesKeyPrefix, pubKey.Bytes()...) // TODO does this need prefix if its in its own store
}
2018-01-11 21:30:39 -08:00
// GetDelegatorBondKey - get the key for delegator bond with candidate
2018-01-18 00:39:16 -08:00
func GetDelegatorBondKey(delegator crypto.Address, candidate crypto.PubKey) []byte {
2018-01-11 21:30:39 -08:00
return append(GetDelegatorBondKeyPrefix(delegator), candidate.Bytes()...)
}
// GetDelegatorBondKeyPrefix - get the prefix for a delegator for all candidates
2018-01-18 00:39:16 -08:00
func GetDelegatorBondKeyPrefix(delegator crypto.Address) []byte {
res, err := cdc.MarshalJSON(&delegator)
2018-01-18 00:39:16 -08:00
if err != nil {
panic(err)
}
return append(DelegatorBondKeyPrefix, res...)
2018-01-11 21:30:39 -08:00
}
// GetDelegatorBondsKey - get the key for list of all the delegator's bonds
2018-01-18 00:39:16 -08:00
func GetDelegatorBondsKey(delegator crypto.Address) []byte {
res, err := cdc.MarshalJSON(&delegator)
2018-01-18 00:39:16 -08:00
if err != nil {
panic(err)
}
return append(DelegatorBondsKeyPrefix, res...)
2018-01-11 21:30:39 -08:00
}
//___________________________________________________________________________
// mapper of the staking store
type Mapper struct {
store sdk.KVStore
cdc *wire.Codec
}
2018-01-11 21:30:39 -08:00
func NewMapper(ctx sdk.Context, key sdk.StoreKey) Mapper {
cdc := wire.NewCodec()
cdc.RegisterInterface((*sdk.Rational)(nil), nil) // XXX make like crypto.RegisterWire()
cdc.RegisterConcrete(sdk.Rat{}, "rat", nil)
crypto.RegisterWire(cdc)
return StakeMapper{
store: ctx.KVStore(m.key),
cdc: cdc,
}
}
func (m Mapper) loadCandidate(pubKey crypto.PubKey) *Candidate {
b := m.store.Get(GetCandidateKey(pubKey))
2018-01-11 21:30:39 -08:00
if b == nil {
return nil
}
candidate := new(Candidate)
err := cdc.UnmarshalJSON(b, candidate)
2018-01-11 21:30:39 -08:00
if err != nil {
2018-01-25 01:06:25 -08:00
panic(err) // This error should never occur big problem if does
2018-01-11 21:30:39 -08:00
}
return candidate
}
func (m Mapper) saveCandidate(candidate *Candidate) {
2018-01-11 21:30:39 -08:00
// XXX should only remove validator if we know candidate is a validator
removeValidator(m.store, candidate.PubKey)
2018-01-25 01:06:25 -08:00
validator := &Validator{candidate.PubKey, candidate.VotingPower}
updateValidator(m.store, validator)
2018-01-11 21:30:39 -08:00
b, err := cdc.MarshalJSON(*candidate)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
m.store.Set(GetCandidateKey(candidate.PubKey), b)
2018-01-11 21:30:39 -08:00
}
func (m Mapper) removeCandidate(pubKey crypto.PubKey) {
// XXX should only remove validator if we know candidate is a validator
removeValidator(m.store, pubKey)
m.store.Delete(GetCandidateKey(pubKey))
2018-01-11 21:30:39 -08:00
}
//___________________________________________________________________________
2018-01-11 21:30:39 -08:00
//func loadValidator(m.store sdk.KVStore, pubKey crypto.PubKey, votingPower sdk.Rational) *Validator {
//b := m.store.Get(GetValidatorKey(pubKey, votingPower))
//if b == nil {
//return nil
//}
//validator := new(Validator)
//err := cdc.UnmarshalJSON(b, validator)
//if err != nil {
//panic(err) // This error should never occur big problem if does
//}
//return validator
//}
// updateValidator - update a validator and create accumulate any changes
// in the changed validator substore
func (m Mapper) updateValidator(validator *Validator) {
2018-01-25 01:06:25 -08:00
b, err := cdc.MarshalJSON(*validator)
2018-01-23 01:04:36 -08:00
if err != nil {
panic(err)
}
// add to the validators to update list if necessary
m.store.Set(GetValidatorUpdatesKey(validator.PubKey), b)
// update the list ordered by voting power
m.store.Set(GetValidatorKey(validator.PubKey, validator.VotingPower), b)
2018-01-23 01:04:36 -08:00
}
func (m Mapper) removeValidator(pubKey crypto.PubKey) {
//add validator with zero power to the validator updates
b, err := cdc.MarshalJSON(Validator{pubKey, sdk.ZeroRat})
if err != nil {
panic(err)
}
m.store.Set(GetValidatorUpdatesKey(pubKey), b)
2018-01-25 01:06:25 -08:00
// now actually delete from the validator set
candidate := loadCandidate(m.store, pubKey)
2018-01-25 01:06:25 -08:00
if candidate != nil {
m.store.Delete(GetValidatorKey(pubKey, candidate.VotingPower))
2018-01-23 01:04:36 -08:00
}
}
// get the most recent updated validator set from the Candidates. These bonds
// are already sorted by VotingPower from the UpdateVotingPower function which
// is the only function which is to modify the VotingPower
func (m Mapper) getValidators(maxVal int) (validators []Validator) {
2018-01-23 01:04:36 -08:00
iterator := m.store.Iterator(subspace(ValidatorKeyPrefix)) //smallest to largest
2018-01-23 01:04:36 -08:00
validators = make([]Validator, maxVal)
2018-01-23 01:04:36 -08:00
for i := 0; ; i++ {
if !iterator.Valid() || i > maxVal {
iterator.Close()
break
}
2018-01-25 01:06:25 -08:00
valBytes := iterator.Value()
var val Validator
err := cdc.UnmarshalJSON(valBytes, &val)
2018-01-23 01:04:36 -08:00
if err != nil {
panic(err)
}
2018-01-25 01:06:25 -08:00
validators[i] = val
2018-01-23 01:04:36 -08:00
iterator.Next()
}
return
}
//_________________________________________________________________________
// get the most updated validators
func (m Mapper) getValidatorUpdates() (updates []Validator) {
iterator := m.store.Iterator(subspace(ValidatorUpdatesKeyPrefix)) //smallest to largest
for ; iterator.Valid(); iterator.Next() {
valBytes := iterator.Value()
var val Validator
err := cdc.UnmarshalJSON(valBytes, &val)
if err != nil {
panic(err)
}
updates = append(updates, val)
}
iterator.Close()
return
}
// remove all validator update entries
func (m Mapper) clearValidatorUpdates(maxVal int) {
iterator := m.store.Iterator(subspace(ValidatorUpdatesKeyPrefix))
for ; iterator.Valid(); iterator.Next() {
m.store.Delete(iterator.Key()) // XXX write test for this, may need to be in a second loop
}
iterator.Close()
2018-01-23 01:04:36 -08:00
}
//---------------------------------------------------------------------
2018-01-25 01:06:25 -08:00
// loadCandidates - get the active list of all candidates TODO replace with multistore
func (m Mapper) loadCandidates() (candidates Candidates) {
2018-01-25 01:06:25 -08:00
iterator := m.store.Iterator(subspace(CandidateKeyPrefix))
//iterator := m.store.Iterator(CandidateKeyPrefix, []byte(nil))
//iterator := m.store.Iterator([]byte{}, []byte(nil))
2018-01-25 01:06:25 -08:00
for ; iterator.Valid(); iterator.Next() {
2018-01-25 01:06:25 -08:00
candidateBytes := iterator.Value()
var candidate Candidate
err := cdc.UnmarshalJSON(candidateBytes, &candidate)
2018-01-25 01:06:25 -08:00
if err != nil {
panic(err)
}
candidates = append(candidates, &candidate)
2018-01-25 01:06:25 -08:00
}
iterator.Close()
2018-01-25 01:06:25 -08:00
return candidates
}
//_____________________________________________________________________
2018-01-25 01:06:25 -08:00
2018-01-11 21:30:39 -08:00
// load the pubkeys of all candidates a delegator is delegated too
func (m Mapper) loadDelegatorCandidates(delegator crypto.Address) (candidates []crypto.PubKey) {
2018-01-11 21:30:39 -08:00
candidateBytes := m.store.Get(GetDelegatorBondsKey(delegator))
2018-01-11 21:30:39 -08:00
if candidateBytes == nil {
return nil
}
err := cdc.UnmarshalJSON(candidateBytes, &candidates)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
return
}
//_____________________________________________________________________
2018-01-11 21:30:39 -08:00
func (m Mapper) loadDelegatorBond(delegator crypto.Address,
candidate crypto.PubKey) *DelegatorBond {
2018-01-11 21:30:39 -08:00
delegatorBytes := m.store.Get(GetDelegatorBondKey(delegator, candidate))
2018-01-11 21:30:39 -08:00
if delegatorBytes == nil {
return nil
}
bond := new(DelegatorBond)
err := cdc.UnmarshalJSON(delegatorBytes, bond)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
return bond
}
func (m Mapper) saveDelegatorBond(delegator crypto.Address,
bond *DelegatorBond) {
2018-01-11 21:30:39 -08:00
// if a new bond add to the list of bonds
if loadDelegatorBond(m.store, delegator, bond.PubKey) == nil {
pks := loadDelegatorCandidates(m.store, delegator)
2018-01-11 21:30:39 -08:00
pks = append(pks, (*bond).PubKey)
b, err := cdc.MarshalJSON(pks)
2018-01-18 00:39:16 -08:00
if err != nil {
panic(err)
}
m.store.Set(GetDelegatorBondsKey(delegator), b)
2018-01-11 21:30:39 -08:00
}
// now actually save the bond
b, err := cdc.MarshalJSON(*bond)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
m.store.Set(GetDelegatorBondKey(delegator, bond.PubKey), b)
2018-01-11 21:30:39 -08:00
//updateDelegatorBonds(store, delegator)
}
func (m Mapper) removeDelegatorBond(delegator crypto.Address, candidate crypto.PubKey) {
2018-01-11 21:30:39 -08:00
// TODO use list queries on multistore to remove iterations here!
// first remove from the list of bonds
pks := loadDelegatorCandidates(m.store, delegator)
2018-01-11 21:30:39 -08:00
for i, pk := range pks {
if candidate.Equals(pk) {
pks = append(pks[:i], pks[i+1:]...)
}
}
b, err := cdc.MarshalJSON(pks)
2018-01-18 00:39:16 -08:00
if err != nil {
panic(err)
}
m.store.Set(GetDelegatorBondsKey(delegator), b)
2018-01-11 21:30:39 -08:00
// now remove the actual bond
m.store.Delete(GetDelegatorBondKey(delegator, candidate))
2018-01-11 21:30:39 -08:00
//updateDelegatorBonds(store, delegator)
}
//_______________________________________________________________________
// load/save the global staking params
func (m Mapper) loadParams() (params Params) {
b := m.store.Get(ParamKey)
2018-01-11 21:30:39 -08:00
if b == nil {
return defaultParams()
}
err := cdc.UnmarshalJSON(b, &params)
2018-01-11 21:30:39 -08:00
if err != nil {
2018-01-25 01:06:25 -08:00
panic(err) // This error should never occur big problem if does
2018-01-11 21:30:39 -08:00
}
return
}
func (m Mapper) saveParams(params Params) {
b, err := cdc.MarshalJSON(params)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
m.store.Set(ParamKey, b)
2018-01-11 21:30:39 -08:00
}
//_______________________________________________________________________
// load/save the global staking state
func (m Mapper) loadGlobalState() (gs *GlobalState) {
b := m.store.Get(GlobalStateKey)
2018-01-11 21:30:39 -08:00
if b == nil {
return initialGlobalState()
}
gs = new(GlobalState)
err := cdc.UnmarshalJSON(b, gs)
2018-01-11 21:30:39 -08:00
if err != nil {
2018-01-25 01:06:25 -08:00
panic(err) // This error should never occur big problem if does
2018-01-11 21:30:39 -08:00
}
return
}
func (m Mapper) saveGlobalState(gs *GlobalState) {
b, err := cdc.MarshalJSON(*gs)
2018-01-11 21:30:39 -08:00
if err != nil {
panic(err)
}
m.store.Set(GlobalStateKey, b)
2018-01-11 21:30:39 -08:00
}