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

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