cosmos-sdk/x/stake/keeper_keys.go

71 lines
2.5 KiB
Go
Raw Normal View History

2018-03-22 09:00:45 -07:00
package stake
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
2018-03-22 14:47:57 -07:00
// TODO remove some of these prefixes once have working multistore
2018-03-22 09:00:45 -07:00
//nolint
var (
// Keys for store prefixes
2018-03-22 14:47:57 -07:00
ParamKey = []byte{0x00} // key for global parameters relating to staking
PoolKey = []byte{0x01} // key for global parameters relating to staking
CandidatesKey = []byte{0x02} // prefix for each key to a candidate
ValidatorsKey = []byte{0x03} // prefix for each key to a validator
AccUpdateValidatorsKey = []byte{0x04} // prefix for each key to a validator which is being updated
RecentValidatorsKey = []byte{0x05} // prefix for each key to the last updated validator group
2018-03-22 14:47:57 -07:00
2018-04-02 11:37:35 -07:00
ToKickOutValidatorsKey = []byte{0x06} // prefix for each key to the last updated validator group
2018-03-22 14:47:57 -07:00
2018-04-02 11:37:35 -07:00
DelegatorBondKeyPrefix = []byte{0x07} // prefix for each key to a delegator's bond
2018-03-22 09:00:45 -07:00
)
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
2018-03-22 14:47:57 -07:00
// get the key for the candidate with address
2018-03-22 09:00:45 -07:00
func GetCandidateKey(addr sdk.Address) []byte {
2018-03-22 14:47:57 -07:00
return append(CandidatesKey, addr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-03-22 14:47:57 -07:00
// get the key for the validator used in the power-store
2018-03-22 09:00:45 -07:00
func GetValidatorKey(addr sdk.Address, power sdk.Rat, cdc *wire.Codec) []byte {
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount))
return append(ValidatorsKey, append(powerBytes, addr.Bytes()...)...)
2018-03-22 14:47:57 -07:00
}
// get the key for the accumulated update validators
func GetAccUpdateValidatorKey(addr sdk.Address) []byte {
return append(AccUpdateValidatorsKey, addr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-03-22 14:47:57 -07:00
// get the key for the accumulated update validators
func GetRecentValidatorKey(addr sdk.Address) []byte {
return append(RecentValidatorsKey, addr.Bytes()...)
2018-03-22 09:00:45 -07:00
}
2018-04-02 11:37:35 -07:00
// reverse operation of GetRecentValidatorKey
func AddrFromKey(key []byte) sdk.Address {
return key[1:]
}
// get the key for the accumulated update validators
func GetToKickOutValidatorKey(addr sdk.Address) []byte {
return append(ToKickOutValidatorsKey, addr.Bytes()...)
}
2018-03-22 14:47:57 -07:00
// get the key for delegator bond with candidate
2018-03-22 09:00:45 -07:00
func GetDelegatorBondKey(delegatorAddr, candidateAddr sdk.Address, cdc *wire.Codec) []byte {
return append(GetDelegatorBondsKey(delegatorAddr, cdc), candidateAddr.Bytes()...)
}
2018-03-22 14:47:57 -07:00
// get the prefix for a delegator for all candidates
2018-03-22 09:00:45 -07:00
func GetDelegatorBondsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte {
res, err := cdc.MarshalBinary(&delegatorAddr)
if err != nil {
panic(err)
}
return append(DelegatorBondKeyPrefix, res...)
}