github.com/Finschia/finschia-sdk@v0.48.1/x/distribution/types/keys.go (about) 1 package types 2 3 import ( 4 "encoding/binary" 5 6 sdk "github.com/Finschia/finschia-sdk/types" 7 "github.com/Finschia/finschia-sdk/types/address" 8 "github.com/Finschia/finschia-sdk/types/kv" 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<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorOutstandingRewards 33 // 34 // - 0x03<accAddrLen (1 Byte)><accAddr_Bytes>: sdk.AccAddress 35 // 36 // - 0x04<valAddrLen (1 Byte)><valAddr_Bytes><accAddrLen (1 Byte)><accAddr_Bytes>: DelegatorStartingInfo 37 // 38 // - 0x05<valAddrLen (1 Byte)><valAddr_Bytes><period_Bytes>: ValidatorHistoricalRewards 39 // 40 // - 0x06<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorCurrentRewards 41 // 42 // - 0x07<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorCurrentCommission 43 // 44 // - 0x08<valAddrLen (1 Byte)><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 // GetValidatorOutstandingRewardsAddress creates an address from a validator's outstanding rewards key. 59 func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) { 60 // key is in the format: 61 // 0x02<valAddrLen (1 Byte)><valAddr_Bytes> 62 63 // Remove prefix and address length. 64 kv.AssertKeyAtLeastLength(key, 3) 65 addr := key[2:] 66 kv.AssertKeyLength(addr, int(key[1])) 67 68 return sdk.ValAddress(addr) 69 } 70 71 // GetDelegatorWithdrawInfoAddress creates an address from a delegator's withdraw info key. 72 func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) { 73 // key is in the format: 74 // 0x03<accAddrLen (1 Byte)><accAddr_Bytes> 75 76 // Remove prefix and address length. 77 kv.AssertKeyAtLeastLength(key, 3) 78 addr := key[2:] 79 kv.AssertKeyLength(addr, int(key[1])) 80 81 return sdk.AccAddress(addr) 82 } 83 84 // GetDelegatorStartingInfoAddresses creates the addresses from a delegator starting info key. 85 func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) { 86 // key is in the format: 87 // 0x04<valAddrLen (1 Byte)><valAddr_Bytes><accAddrLen (1 Byte)><accAddr_Bytes> 88 kv.AssertKeyAtLeastLength(key, 2) 89 valAddrLen := int(key[1]) 90 kv.AssertKeyAtLeastLength(key, 3+valAddrLen) 91 valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) 92 delAddrLen := int(key[2+valAddrLen]) 93 kv.AssertKeyAtLeastLength(key, 4+valAddrLen) 94 delAddr = sdk.AccAddress(key[3+valAddrLen:]) 95 kv.AssertKeyLength(delAddr.Bytes(), delAddrLen) 96 97 return 98 } 99 100 // GetValidatorHistoricalRewardsAddressPeriod creates the address & period from a validator's historical rewards key. 101 func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) { 102 // key is in the format: 103 // 0x05<valAddrLen (1 Byte)><valAddr_Bytes><period_Bytes> 104 kv.AssertKeyAtLeastLength(key, 2) 105 valAddrLen := int(key[1]) 106 kv.AssertKeyAtLeastLength(key, 3+valAddrLen) 107 valAddr = sdk.ValAddress(key[2 : 2+valAddrLen]) 108 b := key[2+valAddrLen:] 109 kv.AssertKeyLength(b, 8) 110 period = binary.LittleEndian.Uint64(b) 111 return 112 } 113 114 // GetValidatorCurrentRewardsAddress creates the address from a validator's current rewards key. 115 func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) { 116 // key is in the format: 117 // 0x06<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorCurrentRewards 118 119 // Remove prefix and address length. 120 kv.AssertKeyAtLeastLength(key, 3) 121 addr := key[2:] 122 kv.AssertKeyLength(addr, int(key[1])) 123 124 return sdk.ValAddress(addr) 125 } 126 127 // GetValidatorAccumulatedCommissionAddress creates the address from a validator's accumulated commission key. 128 func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) { 129 // key is in the format: 130 // 0x07<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorCurrentRewards 131 132 // Remove prefix and address length. 133 kv.AssertKeyAtLeastLength(key, 3) 134 addr := key[2:] 135 kv.AssertKeyLength(addr, int(key[1])) 136 137 return sdk.ValAddress(addr) 138 } 139 140 // GetValidatorSlashEventAddressHeight creates the height from a validator's slash event key. 141 func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) { 142 // key is in the format: 143 // 0x08<valAddrLen (1 Byte)><valAddr_Bytes><height>: ValidatorSlashEvent 144 kv.AssertKeyAtLeastLength(key, 2) 145 valAddrLen := int(key[1]) 146 kv.AssertKeyAtLeastLength(key, 3+valAddrLen) 147 valAddr = key[2 : 2+valAddrLen] 148 startB := 2 + valAddrLen 149 kv.AssertKeyAtLeastLength(key, startB+9) 150 b := key[startB : startB+8] // the next 8 bytes represent the height 151 height = binary.BigEndian.Uint64(b) 152 return 153 } 154 155 // GetValidatorOutstandingRewardsKey creates the outstanding rewards key for a validator. 156 func GetValidatorOutstandingRewardsKey(valAddr sdk.ValAddress) []byte { 157 return append(ValidatorOutstandingRewardsPrefix, address.MustLengthPrefix(valAddr.Bytes())...) 158 } 159 160 // GetDelegatorWithdrawAddrKey creates the key for a delegator's withdraw addr. 161 func GetDelegatorWithdrawAddrKey(delAddr sdk.AccAddress) []byte { 162 return append(DelegatorWithdrawAddrPrefix, address.MustLengthPrefix(delAddr.Bytes())...) 163 } 164 165 // GetDelegatorStartingInfoKey creates the key for a delegator's starting info. 166 func GetDelegatorStartingInfoKey(v sdk.ValAddress, d sdk.AccAddress) []byte { 167 return append(append(DelegatorStartingInfoPrefix, address.MustLengthPrefix(v.Bytes())...), address.MustLengthPrefix(d.Bytes())...) 168 } 169 170 // GetValidatorHistoricalRewardsPrefix creates the prefix key for a validator's historical rewards. 171 func GetValidatorHistoricalRewardsPrefix(v sdk.ValAddress) []byte { 172 return append(ValidatorHistoricalRewardsPrefix, address.MustLengthPrefix(v.Bytes())...) 173 } 174 175 // GetValidatorHistoricalRewardsKey creates the key for a validator's historical rewards. 176 func GetValidatorHistoricalRewardsKey(v sdk.ValAddress, k uint64) []byte { 177 b := make([]byte, 8) 178 binary.LittleEndian.PutUint64(b, k) 179 return append(append(ValidatorHistoricalRewardsPrefix, address.MustLengthPrefix(v.Bytes())...), b...) 180 } 181 182 // GetValidatorCurrentRewardsKey creates the key for a validator's current rewards. 183 func GetValidatorCurrentRewardsKey(v sdk.ValAddress) []byte { 184 return append(ValidatorCurrentRewardsPrefix, address.MustLengthPrefix(v.Bytes())...) 185 } 186 187 // GetValidatorAccumulatedCommissionKey creates the key for a validator's current commission. 188 func GetValidatorAccumulatedCommissionKey(v sdk.ValAddress) []byte { 189 return append(ValidatorAccumulatedCommissionPrefix, address.MustLengthPrefix(v.Bytes())...) 190 } 191 192 // GetValidatorSlashEventPrefix creates the prefix key for a validator's slash fractions. 193 func GetValidatorSlashEventPrefix(v sdk.ValAddress) []byte { 194 return append(ValidatorSlashEventPrefix, address.MustLengthPrefix(v.Bytes())...) 195 } 196 197 // GetValidatorSlashEventKeyPrefix creates the prefix key for a validator's slash fraction (ValidatorSlashEventPrefix + height). 198 func GetValidatorSlashEventKeyPrefix(v sdk.ValAddress, height uint64) []byte { 199 heightBz := make([]byte, 8) 200 binary.BigEndian.PutUint64(heightBz, height) 201 202 return append( 203 ValidatorSlashEventPrefix, 204 append(address.MustLengthPrefix(v.Bytes()), heightBz...)..., 205 ) 206 } 207 208 // GetValidatorSlashEventKey creates the key for a validator's slash fraction. 209 func GetValidatorSlashEventKey(v sdk.ValAddress, height, period uint64) []byte { 210 periodBz := make([]byte, 8) 211 binary.BigEndian.PutUint64(periodBz, period) 212 prefix := GetValidatorSlashEventKeyPrefix(v, height) 213 214 return append(prefix, periodBz...) 215 }