github.com/cosmos/cosmos-sdk@v0.50.10/x/distribution/migrations/v1/types.go (about)

     1  package legacy
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	"github.com/cosmos/cosmos-sdk/types/kv"
     8  	v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
     9  )
    10  
    11  const (
    12  	// ModuleName is the module name constant used in many places
    13  	ModuleName = "distribution"
    14  
    15  	// StoreKey is the store key string for distribution
    16  	StoreKey = ModuleName
    17  
    18  	// RouterKey is the message route for distribution
    19  	RouterKey = ModuleName
    20  
    21  	// QuerierRoute is the querier route for distribution
    22  	QuerierRoute = ModuleName
    23  )
    24  
    25  // Keys for distribution store
    26  // Items are stored with the following key: values
    27  //
    28  // - 0x00<proposalID_Bytes>: FeePol
    29  //
    30  // - 0x01: sdk.ConsAddress
    31  //
    32  // - 0x02<valAddr_Bytes>: ValidatorOutstandingRewards
    33  //
    34  // - 0x03<accAddr_Bytes>: sdk.AccAddress
    35  //
    36  // - 0x04<valAddr_Bytes><accAddr_Bytes>: DelegatorStartingInfo
    37  //
    38  // - 0x05<valAddr_Bytes><period_Bytes>: ValidatorHistoricalRewards
    39  //
    40  // - 0x06<valAddr_Bytes>: ValidatorCurrentRewards
    41  //
    42  // - 0x07<valAddr_Bytes>: ValidatorCurrentRewards
    43  //
    44  // - 0x08<valAddr_Bytes><height>: ValidatorSlashEvent
    45  var (
    46  	FeePoolKey                        = []byte{0x00} // key for global distribution state
    47  	ProposerKey                       = []byte{0x01} // key for the proposer operator address
    48  	ValidatorOutstandingRewardsPrefix = []byte{0x02} // key for outstanding rewards
    49  
    50  	DelegatorWithdrawAddrPrefix          = []byte{0x03} // key for delegator withdraw address
    51  	DelegatorStartingInfoPrefix          = []byte{0x04} // key for delegator starting info
    52  	ValidatorHistoricalRewardsPrefix     = []byte{0x05} // key for historical validators rewards / stake
    53  	ValidatorCurrentRewardsPrefix        = []byte{0x06} // key for current validator rewards
    54  	ValidatorAccumulatedCommissionPrefix = []byte{0x07} // key for accumulated validator commission
    55  	ValidatorSlashEventPrefix            = []byte{0x08} // key for validator slash fraction
    56  )
    57  
    58  // gets an address from a validator's outstanding rewards key
    59  func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
    60  	kv.AssertKeyAtLeastLength(key, 2)
    61  	addr := key[1:]
    62  	kv.AssertKeyLength(addr, v1auth.AddrLen)
    63  	return sdk.ValAddress(addr)
    64  }
    65  
    66  // gets an address from a delegator's withdraw info key
    67  func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) {
    68  	kv.AssertKeyAtLeastLength(key, 2)
    69  	addr := key[1:]
    70  	kv.AssertKeyLength(addr, v1auth.AddrLen)
    71  	return sdk.AccAddress(addr)
    72  }
    73  
    74  // gets the addresses from a delegator starting info key
    75  func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) {
    76  	kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
    77  	addr := key[1 : 1+v1auth.AddrLen]
    78  	kv.AssertKeyLength(addr, v1auth.AddrLen)
    79  	valAddr = sdk.ValAddress(addr)
    80  	addr = key[1+v1auth.AddrLen:]
    81  	kv.AssertKeyLength(addr, v1auth.AddrLen)
    82  	delAddr = sdk.AccAddress(addr)
    83  	return
    84  }
    85  
    86  // gets the address & period from a validator's historical rewards key
    87  func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) {
    88  	kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
    89  	addr := key[1 : 1+v1auth.AddrLen]
    90  	kv.AssertKeyLength(addr, v1auth.AddrLen)
    91  	valAddr = sdk.ValAddress(addr)
    92  	b := key[1+v1auth.AddrLen:]
    93  	kv.AssertKeyLength(addr, 8)
    94  	period = binary.LittleEndian.Uint64(b)
    95  	return
    96  }
    97  
    98  // gets the address from a validator's current rewards key
    99  func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
   100  	kv.AssertKeyAtLeastLength(key, 2)
   101  	addr := key[1:]
   102  	kv.AssertKeyLength(addr, v1auth.AddrLen)
   103  	return sdk.ValAddress(addr)
   104  }
   105  
   106  // gets the address from a validator's accumulated commission key
   107  func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) {
   108  	kv.AssertKeyAtLeastLength(key, 2)
   109  	addr := key[1:]
   110  	kv.AssertKeyLength(addr, v1auth.AddrLen)
   111  	return sdk.ValAddress(addr)
   112  }
   113  
   114  // gets the height from a validator's slash event key
   115  func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) {
   116  	kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
   117  	addr := key[1 : 1+v1auth.AddrLen]
   118  	kv.AssertKeyLength(addr, v1auth.AddrLen)
   119  	valAddr = sdk.ValAddress(addr)
   120  	startB := 1 + v1auth.AddrLen
   121  	kv.AssertKeyAtLeastLength(key, startB+9)
   122  	b := key[startB : startB+8] // the next 8 bytes represent the height
   123  	height = binary.BigEndian.Uint64(b)
   124  	return
   125  }
   126  
   127  // gets the outstanding rewards key for a validator
   128  func GetValidatorOutstandingRewardsKey(valAddr sdk.ValAddress) []byte {
   129  	return append(ValidatorOutstandingRewardsPrefix, valAddr.Bytes()...)
   130  }
   131  
   132  // gets the key for a delegator's withdraw addr
   133  func GetDelegatorWithdrawAddrKey(delAddr sdk.AccAddress) []byte {
   134  	return append(DelegatorWithdrawAddrPrefix, delAddr.Bytes()...)
   135  }
   136  
   137  // gets the key for a delegator's starting info
   138  func GetDelegatorStartingInfoKey(v sdk.ValAddress, d sdk.AccAddress) []byte {
   139  	return append(append(DelegatorStartingInfoPrefix, v.Bytes()...), d.Bytes()...)
   140  }
   141  
   142  // gets the prefix key for a validator's historical rewards
   143  func GetValidatorHistoricalRewardsPrefix(v sdk.ValAddress) []byte {
   144  	return append(ValidatorHistoricalRewardsPrefix, v.Bytes()...)
   145  }
   146  
   147  // gets the key for a validator's historical rewards
   148  func GetValidatorHistoricalRewardsKey(v sdk.ValAddress, k uint64) []byte {
   149  	b := make([]byte, 8)
   150  	binary.LittleEndian.PutUint64(b, k)
   151  	return append(append(ValidatorHistoricalRewardsPrefix, v.Bytes()...), b...)
   152  }
   153  
   154  // gets the key for a validator's current rewards
   155  func GetValidatorCurrentRewardsKey(v sdk.ValAddress) []byte {
   156  	return append(ValidatorCurrentRewardsPrefix, v.Bytes()...)
   157  }
   158  
   159  // gets the key for a validator's current commission
   160  func GetValidatorAccumulatedCommissionKey(v sdk.ValAddress) []byte {
   161  	return append(ValidatorAccumulatedCommissionPrefix, v.Bytes()...)
   162  }
   163  
   164  // gets the prefix key for a validator's slash fractions
   165  func GetValidatorSlashEventPrefix(v sdk.ValAddress) []byte {
   166  	return append(ValidatorSlashEventPrefix, v.Bytes()...)
   167  }
   168  
   169  // gets the prefix key for a validator's slash fraction (ValidatorSlashEventPrefix + height)
   170  func GetValidatorSlashEventKeyPrefix(v sdk.ValAddress, height uint64) []byte {
   171  	heightBz := make([]byte, 8)
   172  	binary.BigEndian.PutUint64(heightBz, height)
   173  	return append(
   174  		ValidatorSlashEventPrefix,
   175  		append(v.Bytes(), heightBz...)...,
   176  	)
   177  }
   178  
   179  // gets the key for a validator's slash fraction
   180  func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte {
   181  	periodBz := make([]byte, 8)
   182  	binary.BigEndian.PutUint64(periodBz, period)
   183  	prefix := GetValidatorSlashEventKeyPrefix(v, height)
   184  	return append(prefix, periodBz...)
   185  }