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