github.com/Finschia/finschia-sdk@v0.48.1/x/staking/legacy/v040/keys.go (about) 1 // Package v040 is copy-pasted from: 2 // https://github.com/cosmos/cosmos-sdk/blob/v0.41.0/x/staking/types/keys.go 3 package v040 4 5 import ( 6 "bytes" 7 "encoding/binary" 8 "fmt" 9 "strconv" 10 "time" 11 12 sdk "github.com/Finschia/finschia-sdk/types" 13 "github.com/Finschia/finschia-sdk/types/kv" 14 v040auth "github.com/Finschia/finschia-sdk/x/auth/legacy/v040" 15 "github.com/Finschia/finschia-sdk/x/staking/types" 16 ) 17 18 const ( 19 // ModuleName is the name of the staking module 20 ModuleName = "staking" 21 22 // StoreKey is the string store representation 23 StoreKey = ModuleName 24 25 // QuerierRoute is the querier route for the staking module 26 QuerierRoute = ModuleName 27 28 // RouterKey is the msg router key for the staking module 29 RouterKey = ModuleName 30 ) 31 32 //nolint:golint 33 var ( 34 // Keys for store prefixes 35 // Last* values are constant during a block. 36 LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators 37 LastTotalPowerKey = []byte{0x12} // prefix for the total power 38 39 ValidatorsKey = []byte{0x21} // prefix for each key to a validator 40 ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey 41 ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power 42 43 DelegationKey = []byte{0x31} // key for a delegation 44 UnbondingDelegationKey = []byte{0x32} // key for an unbonding-delegation 45 UnbondingDelegationByValIndexKey = []byte{0x33} // prefix for each key for an unbonding-delegation, by validator operator 46 RedelegationKey = []byte{0x34} // key for a redelegation 47 RedelegationByValSrcIndexKey = []byte{0x35} // prefix for each key for an redelegation, by source validator operator 48 RedelegationByValDstIndexKey = []byte{0x36} // prefix for each key for an redelegation, by destination validator operator 49 50 UnbondingQueueKey = []byte{0x41} // prefix for the timestamps in unbonding queue 51 RedelegationQueueKey = []byte{0x42} // prefix for the timestamps in redelegations queue 52 ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue 53 54 HistoricalInfoKey = []byte{0x50} // prefix for the historical info 55 ) 56 57 // gets the key for the validator with address 58 // VALUE: staking/Validator 59 func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { 60 return append(ValidatorsKey, operatorAddr.Bytes()...) 61 } 62 63 // gets the key for the validator with pubkey 64 // VALUE: validator operator address ([]byte) 65 func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { 66 return append(ValidatorsByConsAddrKey, addr.Bytes()...) 67 } 68 69 // Get the validator operator address from LastValidatorPowerKey 70 func AddressFromLastValidatorPowerKey(key []byte) []byte { 71 kv.AssertKeyAtLeastLength(key, 2) 72 return key[1:] // remove prefix bytes 73 } 74 75 // get the validator by power index. 76 // Power index is the key used in the power-store, and represents the relative 77 // power ranking of the validator. 78 // VALUE: validator operator address ([]byte) 79 func GetValidatorsByPowerIndexKey(validator types.Validator) []byte { 80 // NOTE the address doesn't need to be stored because counter bytes must always be different 81 // NOTE the larger values are of higher value 82 83 consensusPower := sdk.TokensToConsensusPower(validator.Tokens, sdk.DefaultPowerReduction) 84 consensusPowerBytes := make([]byte, 8) 85 binary.BigEndian.PutUint64(consensusPowerBytes, uint64(consensusPower)) 86 87 powerBytes := consensusPowerBytes 88 powerBytesLen := len(powerBytes) // 8 89 90 // key is of format prefix || powerbytes || addrBytes 91 key := make([]byte, 1+powerBytesLen+v040auth.AddrLen) 92 93 key[0] = ValidatorsByPowerIndexKey[0] 94 copy(key[1:powerBytesLen+1], powerBytes) 95 addr, err := sdk.ValAddressFromBech32(validator.OperatorAddress) 96 if err != nil { 97 panic(err) 98 } 99 operAddrInvr := sdk.CopyBytes(addr) 100 101 for i, b := range operAddrInvr { 102 operAddrInvr[i] = ^b 103 } 104 105 copy(key[powerBytesLen+1:], operAddrInvr) 106 107 return key 108 } 109 110 // get the bonded validator index key for an operator address 111 func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { 112 return append(LastValidatorPowerKey, operator...) 113 } 114 115 // parse the validators operator address from power rank key 116 func ParseValidatorPowerRankKey(key []byte) (operAddr []byte) { 117 powerBytesLen := 8 118 kv.AssertKeyLength(key, 1+powerBytesLen+v040auth.AddrLen) 119 120 operAddr = sdk.CopyBytes(key[powerBytesLen+1:]) 121 122 for i, b := range operAddr { 123 operAddr[i] = ^b 124 } 125 126 return operAddr 127 } 128 129 // GetValidatorQueueKey returns the prefix key used for getting a set of unbonding 130 // validators whose unbonding completion occurs at the given time and height. 131 func GetValidatorQueueKey(timestamp time.Time, height int64) []byte { 132 heightBz := sdk.Uint64ToBigEndian(uint64(height)) 133 timeBz := sdk.FormatTimeBytes(timestamp) 134 timeBzL := len(timeBz) 135 prefixL := len(ValidatorQueueKey) 136 137 bz := make([]byte, prefixL+8+timeBzL+8) 138 139 // copy the prefix 140 copy(bz[:prefixL], ValidatorQueueKey) 141 142 // copy the encoded time bytes length 143 copy(bz[prefixL:prefixL+8], sdk.Uint64ToBigEndian(uint64(timeBzL))) 144 145 // copy the encoded time bytes 146 copy(bz[prefixL+8:prefixL+8+timeBzL], timeBz) 147 148 // copy the encoded height 149 copy(bz[prefixL+8+timeBzL:], heightBz) 150 151 return bz 152 } 153 154 // ParseValidatorQueueKey returns the encoded time and height from a key created 155 // from GetValidatorQueueKey. 156 func ParseValidatorQueueKey(bz []byte) (time.Time, int64, error) { 157 prefixL := len(ValidatorQueueKey) 158 if prefix := bz[:prefixL]; !bytes.Equal(prefix, ValidatorQueueKey) { 159 return time.Time{}, 0, fmt.Errorf("invalid prefix; expected: %X, got: %X", ValidatorQueueKey, prefix) 160 } 161 162 timeBzL := sdk.BigEndianToUint64(bz[prefixL : prefixL+8]) 163 ts, err := sdk.ParseTimeBytes(bz[prefixL+8 : prefixL+8+int(timeBzL)]) 164 if err != nil { 165 return time.Time{}, 0, err 166 } 167 168 height := sdk.BigEndianToUint64(bz[prefixL+8+int(timeBzL):]) 169 170 return ts, int64(height), nil 171 } 172 173 // gets the key for delegator bond with validator 174 // VALUE: staking/Delegation 175 func GetDelegationKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { 176 return append(GetDelegationsKey(delAddr), valAddr.Bytes()...) 177 } 178 179 // gets the prefix for a delegator for all validators 180 func GetDelegationsKey(delAddr sdk.AccAddress) []byte { 181 return append(DelegationKey, delAddr.Bytes()...) 182 } 183 184 // gets the key for an unbonding delegation by delegator and validator addr 185 // VALUE: staking/UnbondingDelegation 186 func GetUBDKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { 187 return append( 188 GetUBDsKey(delAddr.Bytes()), 189 valAddr.Bytes()...) 190 } 191 192 // gets the index-key for an unbonding delegation, stored by validator-index 193 // VALUE: none (key rearrangement used) 194 func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { 195 return append(GetUBDsByValIndexKey(valAddr), delAddr.Bytes()...) 196 } 197 198 // rearranges the ValIndexKey to get the UBDKey 199 func GetUBDKeyFromValIndexKey(indexKey []byte) []byte { 200 kv.AssertKeyAtLeastLength(indexKey, 2) 201 addrs := indexKey[1:] // remove prefix bytes 202 kv.AssertKeyLength(addrs, 2*v040auth.AddrLen) 203 204 kv.AssertKeyAtLeastLength(addrs, v040auth.AddrLen+1) 205 valAddr := addrs[:v040auth.AddrLen] 206 delAddr := addrs[v040auth.AddrLen:] 207 208 return GetUBDKey(delAddr, valAddr) 209 } 210 211 // gets the prefix for all unbonding delegations from a delegator 212 func GetUBDsKey(delAddr sdk.AccAddress) []byte { 213 return append(UnbondingDelegationKey, delAddr.Bytes()...) 214 } 215 216 // gets the prefix keyspace for the indexes of unbonding delegations for a validator 217 func GetUBDsByValIndexKey(valAddr sdk.ValAddress) []byte { 218 return append(UnbondingDelegationByValIndexKey, valAddr.Bytes()...) 219 } 220 221 // gets the prefix for all unbonding delegations from a delegator 222 func GetUnbondingDelegationTimeKey(timestamp time.Time) []byte { 223 bz := sdk.FormatTimeBytes(timestamp) 224 return append(UnbondingQueueKey, bz...) 225 } 226 227 // GetREDKey returns a key prefix for indexing a redelegation from a delegator 228 // and source validator to a destination validator. 229 func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { 230 key := make([]byte, 1+v040auth.AddrLen*3) 231 232 copy(key[0:v040auth.AddrLen+1], GetREDsKey(delAddr.Bytes())) 233 copy(key[v040auth.AddrLen+1:2*v040auth.AddrLen+1], valSrcAddr.Bytes()) 234 copy(key[2*v040auth.AddrLen+1:3*v040auth.AddrLen+1], valDstAddr.Bytes()) 235 236 return key 237 } 238 239 // gets the index-key for a redelegation, stored by source-validator-index 240 // VALUE: none (key rearrangement used) 241 func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { 242 REDSFromValsSrcKey := GetREDsFromValSrcIndexKey(valSrcAddr) 243 offset := len(REDSFromValsSrcKey) 244 245 // key is of the form REDSFromValsSrcKey || delAddr || valDstAddr 246 key := make([]byte, len(REDSFromValsSrcKey)+2*v040auth.AddrLen) 247 copy(key[0:offset], REDSFromValsSrcKey) 248 copy(key[offset:offset+v040auth.AddrLen], delAddr.Bytes()) 249 copy(key[offset+v040auth.AddrLen:offset+2*v040auth.AddrLen], valDstAddr.Bytes()) 250 251 return key 252 } 253 254 // gets the index-key for a redelegation, stored by destination-validator-index 255 // VALUE: none (key rearrangement used) 256 func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte { 257 REDSToValsDstKey := GetREDsToValDstIndexKey(valDstAddr) 258 offset := len(REDSToValsDstKey) 259 260 // key is of the form REDSToValsDstKey || delAddr || valSrcAddr 261 key := make([]byte, len(REDSToValsDstKey)+2*v040auth.AddrLen) 262 copy(key[0:offset], REDSToValsDstKey) 263 copy(key[offset:offset+v040auth.AddrLen], delAddr.Bytes()) 264 copy(key[offset+v040auth.AddrLen:offset+2*v040auth.AddrLen], valSrcAddr.Bytes()) 265 266 return key 267 } 268 269 // GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey 270 func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte { 271 // note that first byte is prefix byte 272 kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) 273 274 valSrcAddr := indexKey[1 : v040auth.AddrLen+1] 275 delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] 276 valDstAddr := indexKey[2*v040auth.AddrLen+1 : 3*v040auth.AddrLen+1] 277 278 return GetREDKey(delAddr, valSrcAddr, valDstAddr) 279 } 280 281 // GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey 282 func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte { 283 // note that first byte is prefix byte 284 kv.AssertKeyLength(indexKey, 3*v040auth.AddrLen+1) 285 286 valDstAddr := indexKey[1 : v040auth.AddrLen+1] 287 delAddr := indexKey[v040auth.AddrLen+1 : 2*v040auth.AddrLen+1] 288 valSrcAddr := indexKey[2*v040auth.AddrLen+1 : 3*v040auth.AddrLen+1] 289 290 return GetREDKey(delAddr, valSrcAddr, valDstAddr) 291 } 292 293 // GetRedelegationTimeKey returns a key prefix for indexing an unbonding 294 // redelegation based on a completion time. 295 func GetRedelegationTimeKey(timestamp time.Time) []byte { 296 bz := sdk.FormatTimeBytes(timestamp) 297 return append(RedelegationQueueKey, bz...) 298 } 299 300 // GetREDsKey returns a key prefix for indexing a redelegation from a delegator 301 // address. 302 func GetREDsKey(delAddr sdk.AccAddress) []byte { 303 return append(RedelegationKey, delAddr.Bytes()...) 304 } 305 306 // GetREDsFromValSrcIndexKey returns a key prefix for indexing a redelegation to 307 // a source validator. 308 func GetREDsFromValSrcIndexKey(valSrcAddr sdk.ValAddress) []byte { 309 return append(RedelegationByValSrcIndexKey, valSrcAddr.Bytes()...) 310 } 311 312 // GetREDsToValDstIndexKey returns a key prefix for indexing a redelegation to a 313 // destination (target) validator. 314 func GetREDsToValDstIndexKey(valDstAddr sdk.ValAddress) []byte { 315 return append(RedelegationByValDstIndexKey, valDstAddr.Bytes()...) 316 } 317 318 // GetREDsByDelToValDstIndexKey returns a key prefix for indexing a redelegation 319 // from an address to a source validator. 320 func GetREDsByDelToValDstIndexKey(delAddr sdk.AccAddress, valDstAddr sdk.ValAddress) []byte { 321 return append(GetREDsToValDstIndexKey(valDstAddr), delAddr.Bytes()...) 322 } 323 324 // GetHistoricalInfoKey returns a key prefix for indexing HistoricalInfo objects. 325 func GetHistoricalInfoKey(height int64) []byte { 326 return append(HistoricalInfoKey, []byte(strconv.FormatInt(height, 10))...) 327 }