github.com/lino-network/lino@v0.6.11/param/holder.go (about) 1 package param 2 3 import ( 4 "fmt" 5 "time" 6 7 wire "github.com/cosmos/cosmos-sdk/codec" 8 sdk "github.com/cosmos/cosmos-sdk/types" 9 10 "github.com/lino-network/lino/types" 11 ) 12 13 var ( 14 allocationParamSubStore = []byte{0x00} // SubStore for allocation 15 evaluateOfContentValueParamSubStore = []byte{0x02} // Substore for evaluate of content value 16 developerParamSubStore = []byte{0x03} // Substore for developer param 17 voteParamSubStore = []byte{0x04} // Substore for vote param 18 proposalParamSubStore = []byte{0x05} // Substore for proposal param 19 validatorParamSubStore = []byte{0x06} // Substore for validator param 20 coinDayParamSubStore = []byte{0x07} // Substore for coin day param 21 bandwidthParamSubStore = []byte{0x08} // Substore for bandwidth param 22 accountParamSubstore = []byte{0x09} // Substore for account param 23 postParamSubStore = []byte{0x0a} // Substore for evaluate of content value 24 reputationParamSubStore = []byte{0x0b} // Substore for reputation parameters 25 priceParamSubStore = []byte{0x0c} // Substore for price parameters 26 27 // AnnualInflationCeiling - annual inflation upper bound 28 AnnualInflationCeiling = types.NewDecFromRat(98, 1000) 29 // AnnualInflationFloor - annual inflation lower bound 30 AnnualInflationFloor = types.NewDecFromRat(3, 100) 31 ) 32 33 // ParamHolder - parameter KVStore 34 type ParamHolder struct { 35 // The (unexposed) key used to access the store from the Context 36 key sdk.StoreKey 37 cdc *wire.Codec 38 } 39 40 // NewParamHolder - create a new parameter KVStore 41 func NewParamHolder(key sdk.StoreKey) ParamHolder { 42 cdc := wire.New() 43 wire.RegisterCrypto(cdc) 44 return ParamHolder{ 45 key: key, 46 cdc: cdc, 47 } 48 } 49 50 // InitParam - init all parameters based on code 51 func (ph ParamHolder) InitParam(ctx sdk.Context) error { 52 globalAllocationParam := &GlobalAllocationParam{ 53 GlobalGrowthRate: types.NewDecFromRat(98, 1000), 54 ContentCreatorAllocation: types.NewDecFromRat(85, 100), 55 DeveloperAllocation: types.NewDecFromRat(10, 100), 56 ValidatorAllocation: types.NewDecFromRat(5, 100), 57 } 58 if err := ph.setGlobalAllocationParam(ctx, globalAllocationParam); err != nil { 59 return err 60 } 61 62 postParam := &PostParam{ 63 ReportOrUpvoteIntervalSec: 24 * 3600, 64 PostIntervalSec: 600, 65 MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals), 66 } 67 if err := ph.setPostParam(ctx, postParam); err != nil { 68 return err 69 } 70 71 developerParam := &DeveloperParam{ 72 DeveloperMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 73 DeveloperCoinReturnIntervalSec: int64(7 * 24 * 3600), 74 DeveloperCoinReturnTimes: int64(7), 75 } 76 if err := ph.setDeveloperParam(ctx, developerParam); err != nil { 77 return err 78 } 79 80 validatorParam := &ValidatorParam{ 81 ValidatorMinDeposit: types.NewCoinFromInt64(200000 * types.Decimals), 82 ValidatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 83 ValidatorCoinReturnTimes: int64(7), 84 PenaltyMissCommit: types.NewCoinFromInt64(200 * types.Decimals), 85 PenaltyByzantine: types.NewCoinFromInt64(1000 * types.Decimals), 86 AbsentCommitLimitation: int64(600), // 30min 87 OncallSize: int64(22), 88 StandbySize: int64(7), 89 ValidatorRevokePendingSec: int64(7 * 24 * 3600), 90 OncallInflationWeight: int64(2), 91 StandbyInflationWeight: int64(1), 92 MaxVotedValidators: int64(3), 93 SlashLimitation: int64(5), 94 } 95 if err := ph.setValidatorParam(ctx, validatorParam); err != nil { 96 return err 97 } 98 99 voteParam := &VoteParam{ 100 MinStakeIn: types.NewCoinFromInt64(1000 * types.Decimals), 101 VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), 102 VoterCoinReturnTimes: int64(7), 103 DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 104 DelegatorCoinReturnTimes: int64(7), 105 } 106 if err := ph.setVoteParam(ctx, voteParam); err != nil { 107 return err 108 } 109 110 proposalParam := &ProposalParam{ 111 ContentCensorshipDecideSec: int64(7 * 24 * 3600), 112 ContentCensorshipPassRatio: types.NewDecFromRat(50, 100), 113 ContentCensorshipPassVotes: types.NewCoinFromInt64(10000 * types.Decimals), 114 ContentCensorshipMinDeposit: types.NewCoinFromInt64(100 * types.Decimals), 115 116 ChangeParamExecutionSec: int64(24 * 3600), 117 ChangeParamDecideSec: int64(7 * 24 * 3600), 118 ChangeParamPassRatio: types.NewDecFromRat(70, 100), 119 ChangeParamPassVotes: types.NewCoinFromInt64(1000000 * types.Decimals), 120 ChangeParamMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 121 122 ProtocolUpgradeDecideSec: int64(7 * 24 * 3600), 123 ProtocolUpgradePassRatio: types.NewDecFromRat(80, 100), 124 ProtocolUpgradePassVotes: types.NewCoinFromInt64(10000000 * types.Decimals), 125 ProtocolUpgradeMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 126 } 127 if err := ph.setProposalParam(ctx, proposalParam); err != nil { 128 return err 129 } 130 131 coinDayParam := &CoinDayParam{ 132 SecondsToRecoverCoinDay: int64(7 * 24 * 3600), 133 } 134 if err := ph.setCoinDayParam(ctx, coinDayParam); err != nil { 135 return err 136 } 137 138 bandwidthParam := &BandwidthParam{ 139 SecondsToRecoverBandwidth: int64(7 * 24 * 3600), 140 CapacityUsagePerTransaction: types.NewCoinFromInt64(1 * types.Decimals), 141 VirtualCoin: types.NewCoinFromInt64(1 * types.Decimals), 142 GeneralMsgQuotaRatio: types.NewDecFromRat(20, 100), 143 GeneralMsgEMAFactor: types.NewDecFromRat(1, 10), 144 AppMsgQuotaRatio: types.NewDecFromRat(80, 100), 145 AppMsgEMAFactor: types.NewDecFromRat(1, 10), 146 ExpectedMaxMPS: types.NewDecFromRat(300, 1), 147 MsgFeeFactorA: types.NewDecFromRat(6, 1), 148 MsgFeeFactorB: types.NewDecFromRat(10, 1), 149 MaxMPSDecayRate: types.NewDecFromRat(99, 100), 150 AppBandwidthPoolSize: types.NewDecFromRat(10, 1), 151 AppVacancyFactor: types.NewDecFromRat(69, 100), 152 AppPunishmentFactor: types.NewDecFromRat(14, 5), 153 } 154 if err := ph.setBandwidthParam(ctx, bandwidthParam); err != nil { 155 return err 156 } 157 158 accountParam := &AccountParam{ 159 MinimumBalance: types.NewCoinFromInt64(0), 160 RegisterFee: types.NewCoinFromInt64(1 * types.Decimals), 161 FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals), 162 MaxNumFrozenMoney: 10, 163 } 164 if err := ph.setAccountParam(ctx, accountParam); err != nil { 165 return err 166 } 167 168 reputationParam := &ReputationParam{ 169 BestContentIndexN: 200, 170 UserMaxN: 50, 171 } 172 if err := ph.setReputationParam(ctx, reputationParam); err != nil { 173 return err 174 } 175 176 priceParam := &PriceParam{ 177 TestnetMode: true, 178 UpdateEverySec: int64(time.Hour.Seconds()), 179 FeedEverySec: int64((10 * time.Minute).Seconds()), 180 HistoryMaxLen: 71, 181 PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals), 182 } 183 ph.setPriceParam(ctx, priceParam) 184 185 return nil 186 } 187 188 // InitParamFromConfig - init all parameters based on pass in args 189 func (ph ParamHolder) InitParamFromConfig( 190 ctx sdk.Context, 191 globalParam GlobalAllocationParam, 192 postParam PostParam, 193 developerParam DeveloperParam, 194 validatorParam ValidatorParam, 195 voteParam VoteParam, 196 proposalParam ProposalParam, 197 coinDayParam CoinDayParam, 198 bandwidthParam BandwidthParam, 199 accParam AccountParam, 200 repParam ReputationParam, 201 priceParam PriceParam) error { 202 if !globalParam.IsValid() { 203 return fmt.Errorf("invalid global allocation param: %+v", globalParam) 204 } 205 if err := ph.setGlobalAllocationParam(ctx, &globalParam); err != nil { 206 return err 207 } 208 209 if err := ph.setPostParam(ctx, &postParam); err != nil { 210 return err 211 } 212 213 if err := ph.setDeveloperParam(ctx, &developerParam); err != nil { 214 return err 215 } 216 217 if err := ph.setValidatorParam(ctx, &validatorParam); err != nil { 218 return err 219 } 220 if err := ph.setVoteParam(ctx, &voteParam); err != nil { 221 return err 222 } 223 if err := ph.setProposalParam(ctx, &proposalParam); err != nil { 224 return err 225 } 226 if err := ph.setCoinDayParam(ctx, &coinDayParam); err != nil { 227 return err 228 } 229 230 if err := ph.setBandwidthParam(ctx, &bandwidthParam); err != nil { 231 return err 232 } 233 234 if err := ph.setAccountParam(ctx, &accParam); err != nil { 235 return err 236 } 237 238 if err := ph.setReputationParam(ctx, &repParam); err != nil { 239 return err 240 } 241 242 ph.setPriceParam(ctx, &priceParam) 243 return nil 244 } 245 246 // GetGlobalAllocationParam - get global allocation param 247 func (ph ParamHolder) GetGlobalAllocationParam(ctx sdk.Context) *GlobalAllocationParam { 248 store := ctx.KVStore(ph.key) 249 allocationBytes := store.Get(GetAllocationParamKey()) 250 if allocationBytes == nil { 251 panic("Global Allocation Param Not Initialized") 252 } 253 allocation := new(GlobalAllocationParam) 254 ph.cdc.MustUnmarshalBinaryLengthPrefixed(allocationBytes, allocation) 255 return allocation 256 } 257 258 // GetPostParam - get post param 259 func (ph ParamHolder) GetPostParam(ctx sdk.Context) (*PostParam, sdk.Error) { 260 store := ctx.KVStore(ph.key) 261 paramBytes := store.Get(GetPostParamKey()) 262 if paramBytes == nil { 263 return nil, ErrPostParamNotFound() 264 } 265 param := new(PostParam) 266 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(paramBytes, param); err != nil { 267 return nil, ErrFailedToUnmarshalPostParam(err) 268 } 269 return param, nil 270 } 271 272 // GetDeveloperParam - get developer param 273 func (ph ParamHolder) GetDeveloperParam(ctx sdk.Context) (*DeveloperParam, sdk.Error) { 274 store := ctx.KVStore(ph.key) 275 paramBytes := store.Get(GetDeveloperParamKey()) 276 if paramBytes == nil { 277 return nil, ErrDeveloperParamNotFound() 278 } 279 param := new(DeveloperParam) 280 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(paramBytes, param); err != nil { 281 return nil, ErrFailedToUnmarshalDeveloperParam(err) 282 } 283 return param, nil 284 } 285 286 // GetVoteParam - get vote param 287 func (ph ParamHolder) GetVoteParam(ctx sdk.Context) *VoteParam { 288 store := ctx.KVStore(ph.key) 289 paramBytes := store.Get(GetVoteParamKey()) 290 if paramBytes == nil { 291 panic("Vote Param Not Initialized") 292 } 293 param := new(VoteParam) 294 ph.cdc.MustUnmarshalBinaryLengthPrefixed(paramBytes, param) 295 return param 296 } 297 298 // GetProposalParam - get proposal param 299 func (ph ParamHolder) GetProposalParam(ctx sdk.Context) (*ProposalParam, sdk.Error) { 300 store := ctx.KVStore(ph.key) 301 paramBytes := store.Get(GetProposalParamKey()) 302 if paramBytes == nil { 303 return nil, ErrProposalParamNotFound() 304 } 305 param := new(ProposalParam) 306 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(paramBytes, param); err != nil { 307 return nil, ErrFailedToUnmarshalProposalParam(err) 308 } 309 return param, nil 310 } 311 312 // GetValidatorParam - get validator param 313 func (ph ParamHolder) GetValidatorParam(ctx sdk.Context) *ValidatorParam { 314 store := ctx.KVStore(ph.key) 315 paramBytes := store.Get(GetValidatorParamKey()) 316 if paramBytes == nil { 317 panic("Validator Param Not FOund") 318 } 319 param := new(ValidatorParam) 320 ph.cdc.MustUnmarshalBinaryLengthPrefixed(paramBytes, param) 321 return param 322 } 323 324 // GetCoinDayParam - get coin day param 325 func (ph ParamHolder) GetCoinDayParam(ctx sdk.Context) (*CoinDayParam, sdk.Error) { 326 store := ctx.KVStore(ph.key) 327 paramBytes := store.Get(GetCoinDayParamKey()) 328 if paramBytes == nil { 329 return nil, ErrCoinDayParamNotFound() 330 } 331 param := new(CoinDayParam) 332 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(paramBytes, param); err != nil { 333 return nil, ErrFailedToUnmarshalCoinDayParam(err) 334 } 335 return param, nil 336 } 337 338 // GetBandwidthParam - get bandwidth param 339 func (ph ParamHolder) GetBandwidthParam(ctx sdk.Context) (*BandwidthParam, sdk.Error) { 340 store := ctx.KVStore(ph.key) 341 paramBytes := store.Get(GetBandwidthParamKey()) 342 if paramBytes == nil { 343 return nil, ErrBandwidthParamNotFound() 344 } 345 param := new(BandwidthParam) 346 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(paramBytes, param); err != nil { 347 return nil, ErrFailedToUnmarshalBandwidthParam(err) 348 } 349 return param, nil 350 } 351 352 // GetAccountParam - get account param 353 func (ph ParamHolder) GetAccountParam(ctx sdk.Context) *AccountParam { 354 store := ctx.KVStore(ph.key) 355 paramBytes := store.Get(GetAccountParamKey()) 356 if paramBytes == nil { 357 panic("Account Param Not Initialized") 358 } 359 param := new(AccountParam) 360 ph.cdc.MustUnmarshalBinaryLengthPrefixed(paramBytes, param) 361 return param 362 } 363 364 // GetReputationParam - get reputation param 365 func (ph ParamHolder) GetReputationParam(ctx sdk.Context) *ReputationParam { 366 store := ctx.KVStore(ph.key) 367 paramBytes := store.Get(GetReputationParamKey()) 368 if paramBytes == nil { 369 panic("Reputation Param Not Initialized") 370 } 371 param := new(ReputationParam) 372 ph.cdc.MustUnmarshalBinaryLengthPrefixed(paramBytes, param) 373 return param 374 } 375 376 // GetPriceParam - get price param 377 func (ph ParamHolder) GetPriceParam(ctx sdk.Context) *PriceParam { 378 store := ctx.KVStore(ph.key) 379 paramBytes := store.Get(GetPriceParamKey()) 380 if paramBytes == nil { 381 panic("Price Param Not Initialized") 382 } 383 param := new(PriceParam) 384 ph.cdc.MustUnmarshalBinaryLengthPrefixed(paramBytes, param) 385 return param 386 } 387 388 // UpdateGlobalGrowthRate - update global growth rate 389 func (ph ParamHolder) UpdateGlobalGrowthRate(ctx sdk.Context, growthRate sdk.Dec) sdk.Error { 390 store := ctx.KVStore(ph.key) 391 allocationBytes := store.Get(GetAllocationParamKey()) 392 if allocationBytes == nil { 393 return ErrGlobalAllocationParamNotFound() 394 } 395 allocation := new(GlobalAllocationParam) 396 if err := ph.cdc.UnmarshalBinaryLengthPrefixed(allocationBytes, allocation); err != nil { 397 return ErrFailedToUnmarshalGlobalAllocationParam(err) 398 } 399 400 if growthRate.GT(AnnualInflationCeiling) { 401 growthRate = AnnualInflationCeiling 402 } else if growthRate.LT(AnnualInflationFloor) { 403 growthRate = AnnualInflationFloor 404 } 405 allocation.GlobalGrowthRate = growthRate 406 allocationBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*allocation) 407 if err != nil { 408 return ErrFailedToMarshalGlobalAllocationParam(err) 409 } 410 store.Set(GetAllocationParamKey(), allocationBytes) 411 return nil 412 } 413 414 func (ph ParamHolder) setValidatorParam(ctx sdk.Context, param *ValidatorParam) sdk.Error { 415 store := ctx.KVStore(ph.key) 416 paramBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 417 if err != nil { 418 return ErrFailedToMarshalValidatorParam(err) 419 } 420 store.Set(GetValidatorParamKey(), paramBytes) 421 return nil 422 } 423 424 func (ph ParamHolder) setGlobalAllocationParam( 425 ctx sdk.Context, allocation *GlobalAllocationParam) sdk.Error { 426 store := ctx.KVStore(ph.key) 427 allocationBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*allocation) 428 if err != nil { 429 return ErrFailedToMarshalGlobalAllocationParam(err) 430 } 431 store.Set(GetAllocationParamKey(), allocationBytes) 432 return nil 433 } 434 435 func (ph ParamHolder) setPostParam( 436 ctx sdk.Context, para *PostParam) sdk.Error { 437 store := ctx.KVStore(ph.key) 438 paraBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*para) 439 if err != nil { 440 return ErrFailedToMarshalPostParam(err) 441 } 442 store.Set(GetPostParamKey(), paraBytes) 443 return nil 444 } 445 446 func (ph ParamHolder) setDeveloperParam(ctx sdk.Context, param *DeveloperParam) sdk.Error { 447 store := ctx.KVStore(ph.key) 448 paramBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 449 if err != nil { 450 return ErrFailedToMarshalDeveloperParam(err) 451 } 452 store.Set(GetDeveloperParamKey(), paramBytes) 453 return nil 454 } 455 456 func (ph ParamHolder) setVoteParam(ctx sdk.Context, param *VoteParam) sdk.Error { 457 store := ctx.KVStore(ph.key) 458 paramBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 459 if err != nil { 460 return ErrFailedToMarshalVoteParam(err) 461 } 462 store.Set(GetVoteParamKey(), paramBytes) 463 return nil 464 } 465 466 func (ph ParamHolder) setProposalParam(ctx sdk.Context, param *ProposalParam) sdk.Error { 467 store := ctx.KVStore(ph.key) 468 paramBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 469 if err != nil { 470 return ErrFailedToMarshalProposalParam(err) 471 } 472 store.Set(GetProposalParamKey(), paramBytes) 473 return nil 474 } 475 476 func (ph ParamHolder) setCoinDayParam(ctx sdk.Context, param *CoinDayParam) sdk.Error { 477 store := ctx.KVStore(ph.key) 478 paramBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 479 if err != nil { 480 return ErrFailedToMarshalCoinDayParam(err) 481 } 482 store.Set(GetCoinDayParamKey(), paramBytes) 483 return nil 484 } 485 486 func (ph ParamHolder) setBandwidthParam(ctx sdk.Context, param *BandwidthParam) sdk.Error { 487 store := ctx.KVStore(ph.key) 488 bandwidthBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 489 if err != nil { 490 return ErrFailedToMarshalBandwidthParam(err) 491 } 492 store.Set(GetBandwidthParamKey(), bandwidthBytes) 493 return nil 494 } 495 496 func (ph ParamHolder) setAccountParam(ctx sdk.Context, param *AccountParam) sdk.Error { 497 store := ctx.KVStore(ph.key) 498 accountBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 499 if err != nil { 500 return ErrFailedToMarshalAccountParam(err) 501 } 502 store.Set(GetAccountParamKey(), accountBytes) 503 return nil 504 } 505 506 func (ph ParamHolder) setReputationParam(ctx sdk.Context, param *ReputationParam) sdk.Error { 507 store := ctx.KVStore(ph.key) 508 reputationBytes, err := ph.cdc.MarshalBinaryLengthPrefixed(*param) 509 if err != nil { 510 return ErrFailedToMarshalReputationParam(err) 511 } 512 store.Set(GetReputationParamKey(), reputationBytes) 513 return nil 514 } 515 516 func (ph ParamHolder) setPriceParam(ctx sdk.Context, param *PriceParam) { 517 store := ctx.KVStore(ph.key) 518 bytes := ph.cdc.MustMarshalBinaryLengthPrefixed(*param) 519 store.Set(GetPriceParamKey(), bytes) 520 } 521 522 // GetPostParamKey - "post param substore" 523 func GetPostParamKey() []byte { 524 return postParamSubStore 525 } 526 527 // GetEvaluateOfContentValueParamKey - "evaluate of content value param substore" 528 func GetEvaluateOfContentValueParamKey() []byte { 529 return evaluateOfContentValueParamSubStore 530 } 531 532 // GetAllocationParamKey - "allocation param substore" 533 func GetAllocationParamKey() []byte { 534 return allocationParamSubStore 535 } 536 537 // GetDeveloperParamKey - "developer param substore" 538 func GetDeveloperParamKey() []byte { 539 return developerParamSubStore 540 } 541 542 // GetVoteParamKey - "vote param substore" 543 func GetVoteParamKey() []byte { 544 return voteParamSubStore 545 } 546 547 // GetValidatorParamKey - "validator param substore" 548 func GetValidatorParamKey() []byte { 549 return validatorParamSubStore 550 } 551 552 // GetProposalParamKey - "proposal param substore" 553 func GetProposalParamKey() []byte { 554 return proposalParamSubStore 555 } 556 557 // GetCoinDayParamKey - "coin day param substore" 558 func GetCoinDayParamKey() []byte { 559 return coinDayParamSubStore 560 } 561 562 // GetBandwidthParamKey - "bandwidth param substore" 563 func GetBandwidthParamKey() []byte { 564 return bandwidthParamSubStore 565 } 566 567 // GetAccountParamKey - "account param substore" 568 func GetAccountParamKey() []byte { 569 return accountParamSubstore 570 } 571 572 // GetAccountParamKey - "account param substore" 573 func GetReputationParamKey() []byte { 574 return reputationParamSubStore 575 } 576 577 // GetPriceParamKey 578 func GetPriceParamKey() []byte { 579 return priceParamSubStore 580 }