github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/types/keys.go (about) 1 package types 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "time" 7 8 cryptoAmino "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/encoding/amino" 9 10 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 11 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/bech32" 12 13 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 14 ) 15 16 const ( 17 // ModuleName is the name of the staking module 18 ModuleName = "staking" 19 20 // StoreKey is the string store representation 21 StoreKey = ModuleName 22 23 // TStoreKey is the string transient store representation 24 TStoreKey = "transient_" + ModuleName 25 26 // QuerierRoute is the querier route for the staking module 27 QuerierRoute = ModuleName 28 29 // RouterKey is the msg router key for the staking module 30 RouterKey = ModuleName 31 ) 32 33 // nolint 34 var ( 35 // Keys for store prefixes 36 // Last* values are constant during a block. 37 LastValidatorPowerKey = []byte{0x11} // prefix for each key to a validator index, for bonded validators 38 LastTotalPowerKey = []byte{0x12} // prefix for the total power 39 40 ValidatorsKey = []byte{0x21} // prefix for each key to a validator 41 ValidatorsByConsAddrKey = []byte{0x22} // prefix for each key to a validator index, by pubkey 42 ValidatorsByPowerIndexKey = []byte{0x23} // prefix for each key to a validator index, sorted by power 43 44 ValidatorQueueKey = []byte{0x43} // prefix for the timestamps in validator queue 45 46 SharesKey = []byte{0x51} 47 DelegatorKey = []byte{0x52} 48 UnDelegationInfoKey = []byte{0x53} 49 UnDelegateQueueKey = []byte{0x54} 50 ProxyKey = []byte{0x55} 51 52 // prefix key for vals info to enforce the update of validator-set 53 ValidatorAbandonedKey = []byte{0x60} 54 55 lenTime = len(sdk.FormatTimeBytes(time.Now())) 56 ) 57 58 // GetValidatorKey gets the key for the validator with address 59 // VALUE: staking/Validator 60 func GetValidatorKey(operatorAddr sdk.ValAddress) []byte { 61 return append(ValidatorsKey, operatorAddr.Bytes()...) 62 } 63 64 // GetValidatorByConsAddrKey gets the key for the validator with pubkey 65 // VALUE: validator operator address ([]byte) 66 func GetValidatorByConsAddrKey(addr sdk.ConsAddress) []byte { 67 return append(ValidatorsByConsAddrKey, addr.Bytes()...) 68 } 69 70 // AddressFromLastValidatorPowerKey gets the validator operator address from LastValidatorPowerKey 71 func AddressFromLastValidatorPowerKey(key []byte) []byte { 72 return key[1:] // remove prefix bytes 73 } 74 75 // GetValidatorsByPowerIndexKey gets the validator by power index 76 // Power index is the key used in the power-store, and represents the relative power ranking of the validator 77 // VALUE: validator operator address ([]byte) 78 func GetValidatorsByPowerIndexKey(validator Validator) []byte { 79 // NOTE the address doesn't need to be stored because counter bytes must always be different 80 return getValidatorPowerRank(validator) 81 } 82 83 // GetLastValidatorPowerKey gets the bonded validator index key for an operator address 84 func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte { 85 return append(LastValidatorPowerKey, operator...) 86 } 87 88 // GetValidatorQueueTimeKey gets the prefix for all unbonding delegations from a delegator 89 func GetValidatorQueueTimeKey(timestamp time.Time) []byte { 90 bz := sdk.FormatTimeBytes(timestamp) 91 return append(ValidatorQueueKey, bz...) 92 } 93 94 // getValidatorPowerRank gets the power ranking of a validator by fbexchain's rule 95 // just according to the shares instead of tokens on a validator 96 func getValidatorPowerRank(validator Validator) []byte { 97 // consensus power based on the shares on a validator 98 consensusPower := sharesToConsensusPower(validator.DelegatorShares) 99 consensusPowerBytes := make([]byte, 8) 100 binary.BigEndian.PutUint64(consensusPowerBytes[:], uint64(consensusPower)) 101 102 powerBytes := consensusPowerBytes 103 powerBytesLen := len(powerBytes) // 8 104 105 // key is of format prefix || powerbytes || addrBytes 106 key := make([]byte, 1+powerBytesLen+sdk.AddrLen) 107 108 key[0] = ValidatorsByPowerIndexKey[0] 109 copy(key[1:powerBytesLen+1], powerBytes) 110 operAddrInvr := sdk.CopyBytes(validator.OperatorAddress) 111 for i, b := range operAddrInvr { 112 operAddrInvr[i] = ^b 113 } 114 copy(key[powerBytesLen+1:], operAddrInvr) 115 116 return key 117 } 118 119 // GetDelegatorKey gets the key for Delegator 120 func GetDelegatorKey(delAddr sdk.AccAddress) []byte { 121 return append(DelegatorKey, delAddr.Bytes()...) 122 } 123 124 // GetProxyDelegatorKey gets the key for the relationship between delegator and proxy 125 func GetProxyDelegatorKey(proxyAddr, delAddr sdk.AccAddress) []byte { 126 return append(append(ProxyKey, proxyAddr...), delAddr...) 127 } 128 129 // GetSharesKey gets the whole key for an item of shares info 130 func GetSharesKey(valAddr sdk.ValAddress, delAddr sdk.AccAddress) []byte { 131 return append(GetSharesToValidatorsKey(valAddr), delAddr.Bytes()...) 132 } 133 134 // GetSharesToValidatorsKey gets the first-prefix for an item of shares info 135 func GetSharesToValidatorsKey(valAddr sdk.ValAddress) []byte { 136 return append(SharesKey, valAddr.Bytes()...) 137 } 138 139 // GetUndelegationInfoKey gets the key for UndelegationInfo 140 func GetUndelegationInfoKey(delAddr sdk.AccAddress) []byte { 141 return append(UnDelegationInfoKey, delAddr.Bytes()...) 142 } 143 144 // GetCompleteTimeKey get the key for the prefix of time 145 func GetCompleteTimeKey(timestamp time.Time) []byte { 146 bz := sdk.FormatTimeBytes(timestamp) 147 return append(UnDelegateQueueKey, bz...) 148 } 149 150 // GetCompleteTimeWithAddrKey get the key for the complete time with delegator address 151 func GetCompleteTimeWithAddrKey(timestamp time.Time, delAddr sdk.AccAddress) []byte { 152 return append(GetCompleteTimeKey(timestamp), delAddr.Bytes()...) 153 } 154 155 // SplitCompleteTimeWithAddrKey splits the key and returns the endtime and delegator address 156 func SplitCompleteTimeWithAddrKey(key []byte) (time.Time, sdk.AccAddress) { 157 if len(key[1:]) != lenTime+sdk.AddrLen { 158 panic(fmt.Sprintf("unexpected key length (%d ≠ %d)", len(key[1:]), lenTime+sdk.AddrLen)) 159 } 160 endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime]) 161 if err != nil { 162 panic(err) 163 } 164 delAddr := sdk.AccAddress(key[1+lenTime:]) 165 return endTime, delAddr 166 } 167 168 // Bech32ifyConsPub returns a Bech32 encoded string containing the 169 // Bech32PrefixConsPub prefixfor a given consensus node's PubKey. 170 func Bech32ifyConsPub(pub crypto.PubKey) (string, error) { 171 bech32PrefixConsPub := sdk.GetConfig().GetBech32ConsensusPubPrefix() 172 return bech32.ConvertAndEncode(bech32PrefixConsPub, pub.Bytes()) 173 } 174 175 func MustBech32ifyConsPub(pub crypto.PubKey) string { 176 enc, err := Bech32ifyConsPub(pub) 177 if err != nil { 178 panic(err) 179 } 180 181 return enc 182 } 183 184 // GetConsPubKeyBech32 creates a PubKey for a consensus node with a given public 185 // key string using the Bech32 Bech32PrefixConsPub prefix. 186 func GetConsPubKeyBech32(pubkey string) (pk crypto.PubKey, err error) { 187 bech32PrefixConsPub := sdk.GetConfig().GetBech32ConsensusPubPrefix() 188 bz, err := sdk.GetFromBech32(pubkey, bech32PrefixConsPub) 189 if err != nil { 190 return nil, err 191 } 192 193 pk, err = cryptoAmino.PubKeyFromBytes(bz) 194 if err != nil { 195 return nil, err 196 } 197 198 return pk, nil 199 } 200 201 // MustGetConsPubKeyBech32 returns the result of GetConsPubKeyBech32 panicing on 202 // failure. 203 func MustGetConsPubKeyBech32(pubkey string) (pk crypto.PubKey) { 204 pk, err := GetConsPubKeyBech32(pubkey) 205 if err != nil { 206 panic(err) 207 } 208 209 return pk 210 }