2018-09-03 17:46:33 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2019-01-16 13:38:05 -08:00
|
|
|
"encoding/binary"
|
|
|
|
|
2018-09-03 17:46:33 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-03-28 16:27:47 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2018-09-03 17:46:33 -07:00
|
|
|
)
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
const (
|
|
|
|
// default paramspace for params keeper
|
2019-03-28 16:27:47 -07:00
|
|
|
DefaultParamspace = types.ModuleName
|
2019-01-16 13:38:05 -08:00
|
|
|
)
|
|
|
|
|
2019-06-28 11:20:36 -07:00
|
|
|
// Keys for distribution store
|
|
|
|
// Items are stored with the following key: values
|
|
|
|
//
|
|
|
|
// - 0x00<proposalID_Bytes>: FeePol
|
|
|
|
//
|
|
|
|
// - 0x01: sdk.ConsAddress
|
|
|
|
//
|
|
|
|
// - 0x02<valAddr_Bytes>: ValidatorOutstandingRewards
|
|
|
|
//
|
|
|
|
// - 0x03<accAddr_Bytes>: sdk.AccAddress
|
|
|
|
//
|
|
|
|
// - 0x04<valAddr_Bytes><accAddr_Bytes>: DelegatorStartingInfo
|
|
|
|
//
|
|
|
|
// - 0x05<valAddr_Bytes><period_Bytes>: ValidatorHistoricalRewards
|
|
|
|
//
|
|
|
|
// - 0x06<valAddr_Bytes>: ValidatorCurrentRewards
|
|
|
|
//
|
|
|
|
// - 0x07<valAddr_Bytes>: ValidatorCurrentRewards
|
|
|
|
//
|
|
|
|
// - 0x08<valAddr_Bytes><height>: ValidatorSlashEvent
|
2018-09-03 17:46:33 -07:00
|
|
|
var (
|
2019-03-06 10:54:12 -08:00
|
|
|
FeePoolKey = []byte{0x00} // key for global distribution state
|
|
|
|
ProposerKey = []byte{0x01} // key for the proposer operator address
|
|
|
|
ValidatorOutstandingRewardsPrefix = []byte{0x02} // key for outstanding rewards
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
DelegatorWithdrawAddrPrefix = []byte{0x03} // key for delegator withdraw address
|
|
|
|
DelegatorStartingInfoPrefix = []byte{0x04} // key for delegator starting info
|
|
|
|
ValidatorHistoricalRewardsPrefix = []byte{0x05} // key for historical validators rewards / stake
|
|
|
|
ValidatorCurrentRewardsPrefix = []byte{0x06} // key for current validator rewards
|
|
|
|
ValidatorAccumulatedCommissionPrefix = []byte{0x07} // key for accumulated validator commission
|
|
|
|
ValidatorSlashEventPrefix = []byte{0x08} // key for validator slash fraction
|
2018-10-15 12:51:51 -07:00
|
|
|
|
2018-10-16 10:27:16 -07:00
|
|
|
ParamStoreKeyCommunityTax = []byte("communitytax")
|
|
|
|
ParamStoreKeyBaseProposerReward = []byte("baseproposerreward")
|
|
|
|
ParamStoreKeyBonusProposerReward = []byte("bonusproposerreward")
|
2019-01-16 14:17:56 -08:00
|
|
|
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
|
2018-10-10 14:43:47 -07:00
|
|
|
)
|
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
// gets an address from a validator's outstanding rewards key
|
|
|
|
func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
|
|
|
addr := key[1:]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
return sdk.ValAddress(addr)
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets an address from a delegator's withdraw info key
|
|
|
|
func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) {
|
|
|
|
addr := key[1:]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
return sdk.AccAddress(addr)
|
2018-09-03 17:46:33 -07:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the addresses from a delegator starting info key
|
|
|
|
func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) {
|
|
|
|
addr := key[1 : 1+sdk.AddrLen]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
valAddr = sdk.ValAddress(addr)
|
|
|
|
addr = key[1+sdk.AddrLen:]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
delAddr = sdk.AccAddress(addr)
|
|
|
|
return
|
2018-09-03 17:46:33 -07:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the address & period from a validator's historical rewards key
|
|
|
|
func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) {
|
|
|
|
addr := key[1 : 1+sdk.AddrLen]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
valAddr = sdk.ValAddress(addr)
|
|
|
|
b := key[1+sdk.AddrLen:]
|
|
|
|
if len(b) != 8 {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
period = binary.LittleEndian.Uint64(b)
|
|
|
|
return
|
2018-09-03 17:46:33 -07:00
|
|
|
}
|
2018-09-17 20:02:15 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the address from a validator's current rewards key
|
|
|
|
func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
|
|
|
addr := key[1:]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
return sdk.ValAddress(addr)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
2018-11-08 16:28:28 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the address from a validator's accumulated commission key
|
|
|
|
func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) {
|
2018-11-08 16:28:28 -08:00
|
|
|
addr := key[1:]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
return sdk.ValAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the height from a validator's slash event key
|
|
|
|
func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) {
|
|
|
|
addr := key[1 : 1+sdk.AddrLen]
|
|
|
|
if len(addr) != sdk.AddrLen {
|
|
|
|
panic("unexpected key length")
|
|
|
|
}
|
|
|
|
valAddr = sdk.ValAddress(addr)
|
2019-07-01 09:03:34 -07:00
|
|
|
startB := 1 + sdk.AddrLen
|
|
|
|
b := key[startB : startB+8] // the next 8 bytes represent the height
|
2019-01-16 13:38:05 -08:00
|
|
|
height = binary.BigEndian.Uint64(b)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
// gets the outstanding rewards key for a validator
|
|
|
|
func GetValidatorOutstandingRewardsKey(valAddr sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorOutstandingRewardsPrefix, valAddr.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the key for a delegator's withdraw addr
|
|
|
|
func GetDelegatorWithdrawAddrKey(delAddr sdk.AccAddress) []byte {
|
|
|
|
return append(DelegatorWithdrawAddrPrefix, delAddr.Bytes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the key for a delegator's starting info
|
|
|
|
func GetDelegatorStartingInfoKey(v sdk.ValAddress, d sdk.AccAddress) []byte {
|
|
|
|
return append(append(DelegatorStartingInfoPrefix, v.Bytes()...), d.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// gets the prefix key for a validator's historical rewards
|
|
|
|
func GetValidatorHistoricalRewardsPrefix(v sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorHistoricalRewardsPrefix, v.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the key for a validator's historical rewards
|
|
|
|
func GetValidatorHistoricalRewardsKey(v sdk.ValAddress, k uint64) []byte {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(b, k)
|
|
|
|
return append(append(ValidatorHistoricalRewardsPrefix, v.Bytes()...), b...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the key for a validator's current rewards
|
|
|
|
func GetValidatorCurrentRewardsKey(v sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorCurrentRewardsPrefix, v.Bytes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the key for a validator's current commission
|
|
|
|
func GetValidatorAccumulatedCommissionKey(v sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorAccumulatedCommissionPrefix, v.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// gets the prefix key for a validator's slash fractions
|
|
|
|
func GetValidatorSlashEventPrefix(v sdk.ValAddress) []byte {
|
|
|
|
return append(ValidatorSlashEventPrefix, v.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:03:34 -07:00
|
|
|
// gets the prefix key for a validator's slash fraction (ValidatorSlashEventPrefix + height)
|
|
|
|
func GetValidatorSlashEventKeyPrefix(v sdk.ValAddress, height uint64) []byte {
|
|
|
|
heightBz := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(heightBz, height)
|
|
|
|
return append(
|
|
|
|
ValidatorSlashEventPrefix,
|
|
|
|
append(
|
|
|
|
v.Bytes(),
|
|
|
|
heightBz...,
|
|
|
|
)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// gets the key for a validator's slash fraction
|
2019-07-01 09:03:34 -07:00
|
|
|
func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte {
|
|
|
|
periodBz := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(periodBz, period)
|
|
|
|
prefix := GetValidatorSlashEventKeyPrefix(v, height)
|
|
|
|
return append(prefix, periodBz...)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|