github.com/KiraCore/sekai@v0.3.43/x/gov/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/KiraCore/sekai/x/gov/types" 8 "github.com/cosmos/cosmos-sdk/codec" 9 "github.com/cosmos/cosmos-sdk/store/prefix" 10 storetypes "github.com/cosmos/cosmos-sdk/store/types" 11 sdk "github.com/cosmos/cosmos-sdk/types" 12 ) 13 14 type Keeper struct { 15 cdc codec.BinaryCodec 16 storeKey storetypes.StoreKey 17 bk types.BankKeeper 18 proposalRouter types.ProposalRouter 19 } 20 21 func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec, bk types.BankKeeper) Keeper { 22 return Keeper{ 23 cdc: cdc, 24 storeKey: storeKey, 25 bk: bk, 26 } 27 } 28 29 func (k *Keeper) SetProposalRouter(proposalRouter types.ProposalRouter) { 30 k.proposalRouter = proposalRouter 31 } 32 33 func (k Keeper) GetProposalRouter() types.ProposalRouter { 34 return k.proposalRouter 35 } 36 37 // SetNetworkProperties set network properties on KVStore 38 func (k Keeper) SetNetworkProperties(ctx sdk.Context, properties *types.NetworkProperties) error { 39 err := k.ValidateNetworkProperties(ctx, properties) 40 if err != nil { 41 return err 42 } 43 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNetworkProperties) 44 prefixStore.Set([]byte("property"), k.cdc.MustMarshal(properties)) 45 return nil 46 } 47 48 // GetNetworkProperties get network properties from KVStore 49 func (k Keeper) GetNetworkProperties(ctx sdk.Context) *types.NetworkProperties { 50 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNetworkProperties) 51 bz := prefixStore.Get([]byte("property")) 52 53 properties := new(types.NetworkProperties) 54 k.cdc.MustUnmarshal(bz, properties) 55 return properties 56 } 57 58 func (k Keeper) ValidateNetworkProperties(ctx sdk.Context, properties *types.NetworkProperties) error { 59 60 if properties.MinTxFee == 0 { 61 return fmt.Errorf("min_tx_fee should not be ZERO") 62 } 63 if properties.MaxTxFee == 0 { 64 return fmt.Errorf("max_tx_fee should not be ZERO") 65 } 66 if properties.MaxTxFee < properties.MinTxFee { 67 return fmt.Errorf("max_tx_fee should not be lower than min_tx_fee") 68 } 69 // TODO: for now skipping few of validations 70 // if properties.VoteQuorum == 0 { 71 // return fmt.Errorf("vote_quorum should not be zero") 72 // } 73 // if properties.ProposalEndTime == 0 { 74 // return fmt.Errorf("proposal_end_time should not be zero") 75 // } 76 // if properties.ProposalEnactmentTime == 0 { 77 // return fmt.Errorf("proposal_enactment_time should not be zero") 78 // } 79 // if properties.MinProposalEndBlocks == 0 { 80 // return fmt.Errorf("min_proposal_end_blocks should not be zero") 81 // } 82 // if properties.MinProposalEnactmentBlocks == 0 { 83 // return fmt.Errorf("min_proposal_enactment_blocks should not be zero") 84 // } 85 // if properties.MischanceRankDecreaseAmount == 0 { 86 // return fmt.Errorf("mischance_rank_decrease_amount should not be zero") 87 // } 88 // if properties.MaxMischance == 0 { 89 // return fmt.Errorf("max_mischance should not be zero") 90 // } 91 // if properties.InactiveRankDecreasePercent == 0 { 92 // return fmt.Errorf("inactive_rank_decrease_percent should not be zero") 93 // } 94 // if properties.InactiveRankDecreasePercent == 0 { 95 // return fmt.Errorf("inactive_rank_decrease_percent should not be zero") 96 // } 97 if !properties.InactiveRankDecreasePercent.IsNil() && properties.InactiveRankDecreasePercent.GT(sdk.OneDec()) { 98 return fmt.Errorf("inactive_rank_decrease_percent should not be lower than 100%%") 99 } 100 // if properties.MinValidators == 0 { 101 // return fmt.Errorf("min_validators should not be zero") 102 // } 103 // if properties.PoorNetworkMaxBankSend == 0 { 104 // return fmt.Errorf("min_validators should not be zero") 105 // } 106 // if properties.UnjailMaxTime == 0 { 107 // return fmt.Errorf("unjail_max_time should not be zero") 108 // } 109 // fee := k.GetExecutionFee(ctx, (&types.MsgHandleIdentityRecordsVerifyRequest{}).Type()) 110 // maxFee := properties.MinTxFee 111 // if fee != nil { 112 // if maxFee < fee.ExecutionFee { 113 // maxFee = fee.ExecutionFee 114 // } 115 // if maxFee < fee.FailureFee { 116 // maxFee = fee.FailureFee 117 // } 118 // } 119 // if properties.MinIdentityApprovalTip < maxFee*2 { 120 // return fmt.Errorf("min_identity_approval_tip should not be bigger or equal than 2x approval fee") 121 // } 122 // if properties.UniqueIdentityKeys == "" { 123 // return fmt.Errorf("unique_identity_keys should not be empty") 124 // } 125 // monikerExists := false 126 // if properties.UniqueIdentityKeys != FormalizeIdentityRecordKey(properties.UniqueIdentityKeys) { 127 // return fmt.Errorf("unique identity keys on network property should be formailzed with lowercase keys") 128 // } 129 // uniqueKeys := strings.Split(properties.UniqueIdentityKeys, ",") 130 // for _, key := range uniqueKeys { 131 // if !ValidateIdentityRecordKey(key) { 132 // return fmt.Errorf("invalid identity record key exists, key=%s", key) 133 // } 134 // if key == "moniker" { 135 // monikerExists = true 136 // } 137 // } 138 // if !monikerExists { 139 // return fmt.Errorf("moniker should be exist in unique keys list") 140 // } 141 return nil 142 } 143 144 // GetNetworkProperty get single network property by key 145 func (k Keeper) GetNetworkProperty(ctx sdk.Context, property types.NetworkProperty) (types.NetworkPropertyValue, error) { 146 properties := k.GetNetworkProperties(ctx) 147 switch property { 148 case types.MinTxFee: 149 return types.NetworkPropertyValue{Value: properties.MinTxFee}, nil 150 case types.MaxTxFee: 151 return types.NetworkPropertyValue{Value: properties.MaxTxFee}, nil 152 case types.VoteQuorum: 153 return types.NetworkPropertyValue{Value: properties.VoteQuorum}, nil 154 case types.MinimumProposalEndTime: 155 return types.NetworkPropertyValue{Value: properties.MinimumProposalEndTime}, nil 156 case types.ProposalEnactmentTime: 157 return types.NetworkPropertyValue{Value: properties.ProposalEnactmentTime}, nil 158 case types.MinProposalEndBlocks: 159 return types.NetworkPropertyValue{Value: properties.MinProposalEndBlocks}, nil 160 case types.MinProposalEnactmentBlocks: 161 return types.NetworkPropertyValue{Value: properties.MinProposalEnactmentBlocks}, nil 162 case types.EnableForeignFeePayments: 163 return types.NetworkPropertyValue{Value: BoolToInt(properties.EnableForeignFeePayments)}, nil 164 case types.MischanceRankDecreaseAmount: 165 return types.NetworkPropertyValue{Value: properties.MischanceRankDecreaseAmount}, nil 166 case types.MaxMischance: 167 return types.NetworkPropertyValue{Value: properties.MaxMischance}, nil 168 case types.MischanceConfidence: 169 return types.NetworkPropertyValue{Value: properties.MischanceConfidence}, nil 170 case types.InactiveRankDecreasePercent: 171 return types.NetworkPropertyValue{StrValue: properties.InactiveRankDecreasePercent.String()}, nil 172 case types.PoorNetworkMaxBankSend: 173 return types.NetworkPropertyValue{Value: properties.PoorNetworkMaxBankSend}, nil 174 case types.MinValidators: 175 return types.NetworkPropertyValue{Value: properties.MinValidators}, nil 176 case types.UnjailMaxTime: 177 return types.NetworkPropertyValue{Value: properties.UnjailMaxTime}, nil 178 case types.EnableTokenWhitelist: 179 return types.NetworkPropertyValue{Value: BoolToInt(properties.EnableTokenWhitelist)}, nil 180 case types.EnableTokenBlacklist: 181 return types.NetworkPropertyValue{Value: BoolToInt(properties.EnableTokenBlacklist)}, nil 182 case types.MinIdentityApprovalTip: 183 return types.NetworkPropertyValue{Value: properties.MinIdentityApprovalTip}, nil 184 case types.UniqueIdentityKeys: 185 return types.NetworkPropertyValue{StrValue: properties.UniqueIdentityKeys}, nil 186 case types.UbiHardcap: 187 return types.NetworkPropertyValue{Value: properties.UbiHardcap}, nil 188 case types.ValidatorsFeeShare: 189 return types.NetworkPropertyValue{StrValue: properties.ValidatorsFeeShare.String()}, nil 190 case types.InflationRate: 191 return types.NetworkPropertyValue{StrValue: properties.InflationRate.String()}, nil 192 case types.InflationPeriod: 193 return types.NetworkPropertyValue{Value: properties.InflationPeriod}, nil 194 case types.UnstakingPeriod: 195 return types.NetworkPropertyValue{Value: properties.UnstakingPeriod}, nil 196 case types.MaxDelegators: 197 return types.NetworkPropertyValue{Value: properties.MaxDelegators}, nil 198 case types.MinDelegationPushout: 199 return types.NetworkPropertyValue{Value: properties.MinDelegationPushout}, nil 200 case types.SlashingPeriod: 201 return types.NetworkPropertyValue{Value: properties.SlashingPeriod}, nil 202 case types.MaxJailedPercentage: 203 return types.NetworkPropertyValue{StrValue: properties.MaxJailedPercentage.String()}, nil 204 case types.MaxSlashingPercentage: 205 return types.NetworkPropertyValue{StrValue: properties.MaxSlashingPercentage.String()}, nil 206 case types.MinCustodyReward: 207 return types.NetworkPropertyValue{Value: properties.MinCustodyReward}, nil 208 case types.MaxCustodyBufferSize: 209 return types.NetworkPropertyValue{Value: properties.MaxCustodyBufferSize}, nil 210 case types.MaxCustodyTxSize: 211 return types.NetworkPropertyValue{Value: properties.MaxCustodyTxSize}, nil 212 case types.AbstentionRankDecreaseAmount: 213 return types.NetworkPropertyValue{Value: properties.AbstentionRankDecreaseAmount}, nil 214 case types.MaxAbstention: 215 return types.NetworkPropertyValue{Value: properties.MaxAbstention}, nil 216 case types.MinCollectiveBond: 217 return types.NetworkPropertyValue{Value: properties.MinCollectiveBond}, nil 218 case types.MinCollectiveBondingTime: 219 return types.NetworkPropertyValue{Value: properties.MinCollectiveBondingTime}, nil 220 case types.MaxCollectiveOutputs: 221 return types.NetworkPropertyValue{Value: properties.MaxCollectiveOutputs}, nil 222 case types.MinCollectiveClaimPeriod: 223 return types.NetworkPropertyValue{Value: properties.MinCollectiveClaimPeriod}, nil 224 case types.ValidatorRecoveryBond: 225 return types.NetworkPropertyValue{Value: properties.ValidatorRecoveryBond}, nil 226 case types.MaxAnnualInflation: 227 return types.NetworkPropertyValue{StrValue: properties.MaxAnnualInflation.String()}, nil 228 case types.MinDappBond: 229 return types.NetworkPropertyValue{Value: properties.MinDappBond}, nil 230 case types.MaxDappBond: 231 return types.NetworkPropertyValue{Value: properties.MaxDappBond}, nil 232 case types.DappBondDuration: 233 return types.NetworkPropertyValue{Value: properties.DappBondDuration}, nil 234 case types.DappVerifierBond: 235 return types.NetworkPropertyValue{StrValue: properties.DappVerifierBond.String()}, nil 236 case types.DappAutoDenounceTime: 237 return types.NetworkPropertyValue{Value: properties.DappAutoDenounceTime}, nil 238 case types.DappMischanceRankDecreaseAmount: 239 return types.NetworkPropertyValue{Value: properties.DappMischanceRankDecreaseAmount}, nil 240 case types.DappMaxMischance: 241 return types.NetworkPropertyValue{Value: properties.DappMaxMischance}, nil 242 case types.DappInactiveRankDecreasePercent: 243 return types.NetworkPropertyValue{Value: properties.DappInactiveRankDecreasePercent}, nil 244 case types.DappPoolSlippageDefault: 245 return types.NetworkPropertyValue{StrValue: properties.DappPoolSlippageDefault.String()}, nil 246 case types.MintingFtFee: 247 return types.NetworkPropertyValue{Value: properties.MintingFtFee}, nil 248 case types.MintingNftFee: 249 return types.NetworkPropertyValue{Value: properties.MintingNftFee}, nil 250 case types.VetoThreshold: 251 return types.NetworkPropertyValue{StrValue: properties.VetoThreshold.String()}, nil 252 case types.AutocompoundIntervalNumBlocks: 253 return types.NetworkPropertyValue{Value: properties.AutocompoundIntervalNumBlocks}, nil 254 default: 255 return types.NetworkPropertyValue{}, errors.New("trying to fetch network property that does not exist") 256 } 257 } 258 259 // SetNetworkProperty set single network property by key 260 func (k Keeper) SetNetworkProperty(ctx sdk.Context, property types.NetworkProperty, value types.NetworkPropertyValue) error { 261 properties := k.GetNetworkProperties(ctx) 262 switch property { 263 case types.MinTxFee: 264 properties.MinTxFee = value.Value 265 case types.MaxTxFee: 266 properties.MaxTxFee = value.Value 267 case types.VoteQuorum: 268 properties.VoteQuorum = value.Value 269 case types.MinimumProposalEndTime: 270 properties.MinimumProposalEndTime = value.Value 271 case types.ProposalEnactmentTime: 272 properties.ProposalEnactmentTime = value.Value 273 case types.MinProposalEndBlocks: 274 properties.MinProposalEndBlocks = value.Value 275 case types.MinProposalEnactmentBlocks: 276 properties.MinProposalEnactmentBlocks = value.Value 277 case types.EnableForeignFeePayments: 278 if value.Value > 0 { 279 properties.EnableForeignFeePayments = true 280 } 281 properties.EnableForeignFeePayments = false 282 case types.MischanceRankDecreaseAmount: 283 properties.MischanceRankDecreaseAmount = value.Value 284 case types.MaxMischance: 285 properties.MaxMischance = value.Value 286 case types.MischanceConfidence: 287 properties.MischanceConfidence = value.Value 288 case types.InactiveRankDecreasePercent: 289 decValue, err := sdk.NewDecFromStr(value.StrValue) 290 if err != nil { 291 return err 292 } 293 properties.InactiveRankDecreasePercent = decValue 294 case types.PoorNetworkMaxBankSend: 295 properties.PoorNetworkMaxBankSend = value.Value 296 case types.MinValidators: 297 properties.MinValidators = value.Value 298 case types.UnjailMaxTime: 299 properties.UnjailMaxTime = value.Value 300 case types.EnableTokenBlacklist: 301 properties.EnableTokenBlacklist = IntToBool(value.Value) 302 case types.EnableTokenWhitelist: 303 properties.EnableTokenWhitelist = IntToBool(value.Value) 304 case types.MinIdentityApprovalTip: 305 properties.MinIdentityApprovalTip = value.Value 306 case types.UniqueIdentityKeys: 307 properties.UniqueIdentityKeys = value.StrValue 308 case types.UbiHardcap: 309 properties.UbiHardcap = value.Value 310 case types.ValidatorsFeeShare: 311 decValue, err := sdk.NewDecFromStr(value.StrValue) 312 if err != nil { 313 return err 314 } 315 properties.ValidatorsFeeShare = decValue 316 case types.InflationRate: 317 decValue, err := sdk.NewDecFromStr(value.StrValue) 318 if err != nil { 319 return err 320 } 321 properties.InflationRate = decValue 322 case types.InflationPeriod: 323 properties.InflationPeriod = value.Value 324 case types.UnstakingPeriod: 325 properties.UnstakingPeriod = value.Value 326 case types.MaxDelegators: 327 properties.MaxDelegators = value.Value 328 case types.MinDelegationPushout: 329 properties.MinDelegationPushout = value.Value 330 case types.SlashingPeriod: 331 properties.SlashingPeriod = value.Value 332 case types.MaxJailedPercentage: 333 decValue, err := sdk.NewDecFromStr(value.StrValue) 334 if err != nil { 335 return err 336 } 337 properties.MaxJailedPercentage = decValue 338 case types.MaxSlashingPercentage: 339 decValue, err := sdk.NewDecFromStr(value.StrValue) 340 if err != nil { 341 return err 342 } 343 properties.MaxSlashingPercentage = decValue 344 case types.MinCustodyReward: 345 properties.MinCustodyReward = value.Value 346 case types.MaxCustodyBufferSize: 347 properties.MaxCustodyBufferSize = value.Value 348 case types.MaxCustodyTxSize: 349 properties.MaxCustodyTxSize = value.Value 350 case types.AbstentionRankDecreaseAmount: 351 properties.AbstentionRankDecreaseAmount = value.Value 352 case types.MaxAbstention: 353 properties.MaxAbstention = value.Value 354 case types.MinCollectiveBond: 355 properties.MinCollectiveBond = value.Value 356 case types.MinCollectiveBondingTime: 357 properties.MinCollectiveBondingTime = value.Value 358 case types.MaxCollectiveOutputs: 359 properties.MaxCollectiveOutputs = value.Value 360 case types.MinCollectiveClaimPeriod: 361 properties.MinCollectiveClaimPeriod = value.Value 362 case types.ValidatorRecoveryBond: 363 properties.ValidatorRecoveryBond = value.Value 364 case types.MaxAnnualInflation: 365 decValue, err := sdk.NewDecFromStr(value.StrValue) 366 if err != nil { 367 return err 368 } 369 properties.MaxAnnualInflation = decValue 370 case types.MinDappBond: 371 properties.MinDappBond = value.Value 372 case types.MaxDappBond: 373 properties.MaxDappBond = value.Value 374 case types.DappBondDuration: 375 properties.DappBondDuration = value.Value 376 case types.DappVerifierBond: 377 decValue, err := sdk.NewDecFromStr(value.StrValue) 378 if err != nil { 379 return err 380 } 381 properties.DappVerifierBond = decValue 382 case types.DappAutoDenounceTime: 383 properties.DappAutoDenounceTime = value.Value 384 case types.DappMischanceRankDecreaseAmount: 385 properties.DappMischanceRankDecreaseAmount = value.Value 386 case types.DappMaxMischance: 387 properties.DappMaxMischance = value.Value 388 case types.DappInactiveRankDecreasePercent: 389 properties.DappInactiveRankDecreasePercent = value.Value 390 case types.DappPoolSlippageDefault: 391 decValue, err := sdk.NewDecFromStr(value.StrValue) 392 if err != nil { 393 return err 394 } 395 properties.DappPoolSlippageDefault = decValue 396 case types.MintingFtFee: 397 properties.MintingFtFee = value.Value 398 case types.MintingNftFee: 399 properties.MintingNftFee = value.Value 400 case types.VetoThreshold: 401 decValue, err := sdk.NewDecFromStr(value.StrValue) 402 if err != nil { 403 return err 404 } 405 properties.VetoThreshold = decValue 406 407 case types.AutocompoundIntervalNumBlocks: 408 properties.AutocompoundIntervalNumBlocks = value.Value 409 default: 410 return errors.New("trying to set network property that does not exist") 411 } 412 return k.SetNetworkProperties(ctx, properties) 413 } 414 415 // SetExecutionFee set fee by execution function name 416 func (k Keeper) SetExecutionFee(ctx sdk.Context, fee types.ExecutionFee) { 417 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixExecutionFee) 418 key := []byte(fee.TransactionType) 419 prefixStore.Set(key, k.cdc.MustMarshal(&fee)) 420 } 421 422 // GetExecutionFee get fee from execution function name 423 func (k Keeper) GetExecutionFee(ctx sdk.Context, txType string) *types.ExecutionFee { 424 prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixExecutionFee) 425 key := []byte(txType) 426 if !prefixStore.Has(key) { 427 return nil 428 } 429 bz := prefixStore.Get([]byte(txType)) 430 431 fee := new(types.ExecutionFee) 432 k.cdc.MustUnmarshal(bz, fee) 433 return fee 434 } 435 436 // GetExecutionFees get all execution fees 437 func (k Keeper) GetExecutionFees(ctx sdk.Context) []types.ExecutionFee { 438 iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.KeyPrefixExecutionFee) 439 defer iterator.Close() 440 fees := []types.ExecutionFee{} 441 for ; iterator.Valid(); iterator.Next() { 442 bz := iterator.Value() 443 fee := types.ExecutionFee{} 444 k.cdc.MustUnmarshal(bz, &fee) 445 fees = append(fees, fee) 446 } 447 return fees 448 }