github.com/lino-network/lino@v0.6.11/param/holder_test.go (about) 1 //nolint:unused 2 package param 3 4 import ( 5 "testing" 6 "time" 7 8 "github.com/cosmos/cosmos-sdk/store" 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 "github.com/stretchr/testify/assert" 11 abci "github.com/tendermint/tendermint/abci/types" 12 "github.com/tendermint/tendermint/libs/log" 13 dbm "github.com/tendermint/tm-db" 14 15 "github.com/lino-network/lino/types" 16 ) 17 18 var ( 19 TestKVStoreKey = sdk.NewKVStoreKey("param") 20 ) 21 22 func getContext() sdk.Context { 23 db := dbm.NewMemDB() 24 ms := store.NewCommitMultiStore(db) 25 ms.MountStoreWithDB(TestKVStoreKey, sdk.StoreTypeIAVL, db) 26 err := ms.LoadLatestVersion() 27 if err != nil { 28 panic(err) 29 } 30 31 return sdk.NewContext(ms, abci.Header{}, false, log.NewNopLogger()) 32 } 33 34 func TestGlobalAllocationParam(t *testing.T) { 35 ph := NewParamHolder(TestKVStoreKey) 36 ctx := getContext() 37 parameter := GlobalAllocationParam{ 38 GlobalGrowthRate: types.NewDecFromRat(98, 1000), 39 ContentCreatorAllocation: types.NewDecFromRat(1, 100), 40 DeveloperAllocation: types.NewDecFromRat(1, 100), 41 ValidatorAllocation: types.NewDecFromRat(97, 100), 42 } 43 err := ph.setGlobalAllocationParam(ctx, ¶meter) 44 assert.Nil(t, err) 45 46 resultPtr := ph.GetGlobalAllocationParam(ctx) 47 assert.Equal(t, parameter, *resultPtr, "Global allocation param should be equal") 48 } 49 50 func TestDeveloperParam(t *testing.T) { 51 ph := NewParamHolder(TestKVStoreKey) 52 ctx := getContext() 53 parameter := DeveloperParam{ 54 DeveloperMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 55 DeveloperCoinReturnIntervalSec: int64(7 * 24 * 3600), 56 DeveloperCoinReturnTimes: int64(7), 57 } 58 err := ph.setDeveloperParam(ctx, ¶meter) 59 assert.Nil(t, err) 60 61 resultPtr, err := ph.GetDeveloperParam(ctx) 62 assert.Nil(t, err) 63 assert.Equal(t, parameter, *resultPtr, "Developer param should be equal") 64 } 65 66 func TestValidatorParam(t *testing.T) { 67 ph := NewParamHolder(TestKVStoreKey) 68 ctx := getContext() 69 parameter := ValidatorParam{ 70 ValidatorMinDeposit: types.NewCoinFromInt64(200000 * types.Decimals), 71 ValidatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 72 ValidatorCoinReturnTimes: int64(7), 73 PenaltyMissCommit: types.NewCoinFromInt64(200 * types.Decimals), 74 PenaltyByzantine: types.NewCoinFromInt64(1000 * types.Decimals), 75 AbsentCommitLimitation: int64(600), // 30min 76 OncallSize: int64(22), 77 StandbySize: int64(7), 78 ValidatorRevokePendingSec: int64(7 * 24 * 3600), 79 OncallInflationWeight: int64(2), 80 StandbyInflationWeight: int64(1), 81 MaxVotedValidators: int64(3), 82 SlashLimitation: int64(5), 83 } 84 err := ph.setValidatorParam(ctx, ¶meter) 85 assert.Nil(t, err) 86 87 resultPtr := ph.GetValidatorParam(ctx) 88 assert.Equal(t, parameter, *resultPtr, "Validator param should be equal") 89 } 90 91 func TestVoteParam(t *testing.T) { 92 ph := NewParamHolder(TestKVStoreKey) 93 ctx := getContext() 94 parameter := VoteParam{ 95 MinStakeIn: types.NewCoinFromInt64(1000 * types.Decimals), 96 VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), 97 VoterCoinReturnTimes: int64(7), 98 DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 99 DelegatorCoinReturnTimes: int64(7), 100 } 101 err := ph.setVoteParam(ctx, ¶meter) 102 assert.Nil(t, err) 103 104 resultPtr := ph.GetVoteParam(ctx) 105 assert.Equal(t, parameter, *resultPtr, "Voter param should be equal") 106 } 107 108 func TestProposalParam(t *testing.T) { 109 ph := NewParamHolder(TestKVStoreKey) 110 ctx := getContext() 111 parameter := ProposalParam{ 112 ContentCensorshipDecideSec: int64(7 * 24 * 3600), 113 ContentCensorshipPassRatio: types.NewDecFromRat(50, 100), 114 ContentCensorshipPassVotes: types.NewCoinFromInt64(10000 * types.Decimals), 115 ContentCensorshipMinDeposit: types.NewCoinFromInt64(100 * types.Decimals), 116 117 ChangeParamExecutionSec: int64(24 * 3600), 118 ChangeParamDecideSec: int64(7 * 24 * 3600), 119 ChangeParamPassRatio: types.NewDecFromRat(70, 100), 120 ChangeParamPassVotes: types.NewCoinFromInt64(1000000 * types.Decimals), 121 ChangeParamMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 122 123 ProtocolUpgradeDecideSec: int64(7 * 24 * 3600), 124 ProtocolUpgradePassRatio: types.NewDecFromRat(80, 100), 125 ProtocolUpgradePassVotes: types.NewCoinFromInt64(10000000 * types.Decimals), 126 ProtocolUpgradeMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 127 } 128 err := ph.setProposalParam(ctx, ¶meter) 129 assert.Nil(t, err) 130 131 resultPtr, err := ph.GetProposalParam(ctx) 132 assert.Nil(t, err) 133 assert.Equal(t, parameter, *resultPtr, "Proposal param should be equal") 134 } 135 136 func TestCoinDayParam(t *testing.T) { 137 ph := NewParamHolder(TestKVStoreKey) 138 ctx := getContext() 139 parameter := CoinDayParam{ 140 SecondsToRecoverCoinDay: int64(7 * 24 * 3600), 141 } 142 err := ph.setCoinDayParam(ctx, ¶meter) 143 assert.Nil(t, err) 144 145 resultPtr, err := ph.GetCoinDayParam(ctx) 146 assert.Nil(t, err) 147 assert.Equal(t, parameter, *resultPtr, "Coin day param should be equal") 148 } 149 150 func TestBandwidthParam(t *testing.T) { 151 ph := NewParamHolder(TestKVStoreKey) 152 ctx := getContext() 153 parameter := BandwidthParam{ 154 SecondsToRecoverBandwidth: int64(7 * 24 * 3600), 155 CapacityUsagePerTransaction: types.NewCoinFromInt64(1 * types.Decimals), 156 VirtualCoin: types.NewCoinFromInt64(1 * types.Decimals), 157 GeneralMsgQuotaRatio: types.NewDecFromRat(20, 100), 158 GeneralMsgEMAFactor: types.NewDecFromRat(1, 10), 159 AppMsgQuotaRatio: types.NewDecFromRat(80, 100), 160 AppMsgEMAFactor: types.NewDecFromRat(1, 10), 161 ExpectedMaxMPS: types.NewDecFromRat(300, 1), 162 MsgFeeFactorA: types.NewDecFromRat(6, 1), 163 MsgFeeFactorB: types.NewDecFromRat(10, 1), 164 MaxMPSDecayRate: types.NewDecFromRat(99, 100), 165 AppBandwidthPoolSize: types.NewDecFromRat(10, 1), 166 AppVacancyFactor: types.NewDecFromRat(69, 100), 167 AppPunishmentFactor: types.NewDecFromRat(14, 5), 168 } 169 err := ph.setBandwidthParam(ctx, ¶meter) 170 assert.Nil(t, err) 171 172 resultPtr, err := ph.GetBandwidthParam(ctx) 173 assert.Nil(t, err) 174 assert.Equal(t, parameter, *resultPtr, "Bandwidth param should be equal") 175 } 176 177 func TestAccountParam(t *testing.T) { 178 ph := NewParamHolder(TestKVStoreKey) 179 ctx := getContext() 180 parameter := AccountParam{ 181 MinimumBalance: types.NewCoinFromInt64(1 * types.Decimals), 182 RegisterFee: types.NewCoinFromInt64(1 * types.Decimals), 183 FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals), 184 MaxNumFrozenMoney: 10, 185 } 186 err := ph.setAccountParam(ctx, ¶meter) 187 assert.Nil(t, err) 188 189 resultPtr := ph.GetAccountParam(ctx) 190 assert.Equal(t, parameter, *resultPtr, "Account param should be equal") 191 } 192 193 func TestInitParam(t *testing.T) { 194 ph := NewParamHolder(TestKVStoreKey) 195 ctx := getContext() 196 197 err := ph.InitParam(ctx) 198 if err != nil { 199 panic(err) 200 } 201 202 globalAllocationParam := GlobalAllocationParam{ 203 GlobalGrowthRate: types.NewDecFromRat(98, 1000), 204 ContentCreatorAllocation: types.NewDecFromRat(85, 100), 205 DeveloperAllocation: types.NewDecFromRat(10, 100), 206 ValidatorAllocation: types.NewDecFromRat(5, 100), 207 } 208 209 developerParam := DeveloperParam{ 210 DeveloperMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 211 DeveloperCoinReturnIntervalSec: int64(7 * 24 * 3600), 212 DeveloperCoinReturnTimes: int64(7), 213 } 214 215 validatorParam := ValidatorParam{ 216 ValidatorMinDeposit: types.NewCoinFromInt64(200000 * types.Decimals), 217 ValidatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 218 ValidatorCoinReturnTimes: int64(7), 219 PenaltyMissCommit: types.NewCoinFromInt64(200 * types.Decimals), 220 PenaltyByzantine: types.NewCoinFromInt64(1000 * types.Decimals), 221 AbsentCommitLimitation: int64(600), // 30min 222 OncallSize: int64(22), 223 StandbySize: int64(7), 224 ValidatorRevokePendingSec: int64(7 * 24 * 3600), 225 OncallInflationWeight: int64(2), 226 StandbyInflationWeight: int64(1), 227 MaxVotedValidators: int64(3), 228 SlashLimitation: int64(5), 229 } 230 231 voteParam := VoteParam{ 232 MinStakeIn: types.NewCoinFromInt64(1000 * types.Decimals), 233 VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), 234 VoterCoinReturnTimes: int64(7), 235 DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 236 DelegatorCoinReturnTimes: int64(7), 237 } 238 proposalParam := ProposalParam{ 239 ContentCensorshipDecideSec: int64(7 * 24 * 3600), 240 ContentCensorshipPassRatio: types.NewDecFromRat(50, 100), 241 ContentCensorshipPassVotes: types.NewCoinFromInt64(10000 * types.Decimals), 242 ContentCensorshipMinDeposit: types.NewCoinFromInt64(100 * types.Decimals), 243 244 ChangeParamExecutionSec: int64(24 * 3600), 245 ChangeParamDecideSec: int64(7 * 24 * 3600), 246 ChangeParamPassRatio: types.NewDecFromRat(70, 100), 247 ChangeParamPassVotes: types.NewCoinFromInt64(1000000 * types.Decimals), 248 ChangeParamMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 249 250 ProtocolUpgradeDecideSec: int64(7 * 24 * 3600), 251 ProtocolUpgradePassRatio: types.NewDecFromRat(80, 100), 252 ProtocolUpgradePassVotes: types.NewCoinFromInt64(10000000 * types.Decimals), 253 ProtocolUpgradeMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 254 } 255 256 coinDayParam := CoinDayParam{ 257 SecondsToRecoverCoinDay: int64(7 * 24 * 3600), 258 } 259 bandwidthParam := BandwidthParam{ 260 SecondsToRecoverBandwidth: int64(7 * 24 * 3600), 261 CapacityUsagePerTransaction: types.NewCoinFromInt64(1 * types.Decimals), 262 VirtualCoin: types.NewCoinFromInt64(1 * types.Decimals), 263 GeneralMsgQuotaRatio: types.NewDecFromRat(20, 100), 264 GeneralMsgEMAFactor: types.NewDecFromRat(1, 10), 265 AppMsgQuotaRatio: types.NewDecFromRat(80, 100), 266 AppMsgEMAFactor: types.NewDecFromRat(1, 10), 267 ExpectedMaxMPS: types.NewDecFromRat(300, 1), 268 MsgFeeFactorA: types.NewDecFromRat(6, 1), 269 MsgFeeFactorB: types.NewDecFromRat(10, 1), 270 MaxMPSDecayRate: types.NewDecFromRat(99, 100), 271 AppBandwidthPoolSize: types.NewDecFromRat(10, 1), 272 AppVacancyFactor: types.NewDecFromRat(69, 100), 273 AppPunishmentFactor: types.NewDecFromRat(14, 5), 274 } 275 accountParam := AccountParam{ 276 MinimumBalance: types.NewCoinFromInt64(0), 277 RegisterFee: types.NewCoinFromInt64(1 * types.Decimals), 278 FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals), 279 MaxNumFrozenMoney: 10, 280 } 281 postParam := PostParam{ 282 ReportOrUpvoteIntervalSec: int64(24 * 3600), 283 PostIntervalSec: int64(600), 284 MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals), 285 } 286 repParam := ReputationParam{ 287 BestContentIndexN: 200, 288 UserMaxN: 50, 289 } 290 priceParam := PriceParam{ 291 TestnetMode: true, 292 UpdateEverySec: int64(time.Hour.Seconds()), 293 FeedEverySec: int64((10 * time.Minute).Seconds()), 294 HistoryMaxLen: 71, 295 PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals), 296 } 297 298 checkStorage(t, ctx, ph, globalAllocationParam, 299 developerParam, validatorParam, voteParam, 300 proposalParam, coinDayParam, bandwidthParam, accountParam, postParam, repParam, priceParam) 301 } 302 303 func TestInitParamFromConfig(t *testing.T) { 304 ph := NewParamHolder(TestKVStoreKey) 305 ctx := getContext() 306 globalAllocationParam := GlobalAllocationParam{ 307 GlobalGrowthRate: types.NewDecFromRat(98, 1000), 308 ContentCreatorAllocation: types.NewDecFromRat(85, 100), 309 DeveloperAllocation: types.NewDecFromRat(10, 100), 310 ValidatorAllocation: types.NewDecFromRat(5, 100), 311 } 312 313 developerParam := DeveloperParam{ 314 DeveloperMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 315 DeveloperCoinReturnIntervalSec: int64(7 * 24 * 3600), 316 DeveloperCoinReturnTimes: int64(7), 317 } 318 319 validatorParam := ValidatorParam{ 320 ValidatorMinDeposit: types.NewCoinFromInt64(200000 * types.Decimals), 321 ValidatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 322 ValidatorCoinReturnTimes: int64(7), 323 PenaltyMissCommit: types.NewCoinFromInt64(200 * types.Decimals), 324 PenaltyByzantine: types.NewCoinFromInt64(1000 * types.Decimals), 325 AbsentCommitLimitation: int64(600), // 30min 326 OncallSize: int64(22), 327 StandbySize: int64(7), 328 ValidatorRevokePendingSec: int64(7 * 24 * 3600), 329 OncallInflationWeight: int64(2), 330 StandbyInflationWeight: int64(1), 331 MaxVotedValidators: int64(3), 332 SlashLimitation: int64(5), 333 } 334 335 voteParam := VoteParam{ 336 MinStakeIn: types.NewCoinFromInt64(1000 * types.Decimals), 337 VoterCoinReturnIntervalSec: int64(7 * 24 * 3600), 338 VoterCoinReturnTimes: int64(7), 339 DelegatorCoinReturnIntervalSec: int64(7 * 24 * 3600), 340 DelegatorCoinReturnTimes: int64(7), 341 } 342 proposalParam := ProposalParam{ 343 ContentCensorshipDecideSec: int64(7 * 24 * 3600), 344 ContentCensorshipPassRatio: types.NewDecFromRat(50, 100), 345 ContentCensorshipPassVotes: types.NewCoinFromInt64(10000 * types.Decimals), 346 ContentCensorshipMinDeposit: types.NewCoinFromInt64(100 * types.Decimals), 347 348 ChangeParamExecutionSec: int64(24 * 3600), 349 ChangeParamDecideSec: int64(7 * 24 * 3600), 350 ChangeParamPassRatio: types.NewDecFromRat(70, 100), 351 ChangeParamPassVotes: types.NewCoinFromInt64(1000000 * types.Decimals), 352 ChangeParamMinDeposit: types.NewCoinFromInt64(100000 * types.Decimals), 353 354 ProtocolUpgradeDecideSec: int64(7 * 24 * 3600), 355 ProtocolUpgradePassRatio: types.NewDecFromRat(80, 100), 356 ProtocolUpgradePassVotes: types.NewCoinFromInt64(10000000 * types.Decimals), 357 ProtocolUpgradeMinDeposit: types.NewCoinFromInt64(1000000 * types.Decimals), 358 } 359 360 coinDayParam := CoinDayParam{ 361 SecondsToRecoverCoinDay: int64(7 * 24 * 3600), 362 } 363 bandwidthParam := BandwidthParam{ 364 SecondsToRecoverBandwidth: int64(7 * 24 * 3600), 365 CapacityUsagePerTransaction: types.NewCoinFromInt64(1 * types.Decimals), 366 VirtualCoin: types.NewCoinFromInt64(1 * types.Decimals), 367 GeneralMsgQuotaRatio: types.NewDecFromRat(20, 100), 368 GeneralMsgEMAFactor: types.NewDecFromRat(1, 10), 369 AppMsgQuotaRatio: types.NewDecFromRat(80, 100), 370 AppMsgEMAFactor: types.NewDecFromRat(1, 10), 371 ExpectedMaxMPS: types.NewDecFromRat(1000, 1), 372 MsgFeeFactorA: types.NewDecFromRat(6, 1), 373 MsgFeeFactorB: types.NewDecFromRat(10, 1), 374 MaxMPSDecayRate: types.NewDecFromRat(99, 100), 375 AppBandwidthPoolSize: types.NewDecFromRat(10, 1), 376 AppVacancyFactor: types.NewDecFromRat(69, 100), 377 AppPunishmentFactor: types.NewDecFromRat(14, 5), 378 } 379 accountParam := AccountParam{ 380 MinimumBalance: types.NewCoinFromInt64(0), 381 RegisterFee: types.NewCoinFromInt64(1 * types.Decimals), 382 FirstDepositFullCoinDayLimit: types.NewCoinFromInt64(1 * types.Decimals), 383 MaxNumFrozenMoney: 10, 384 } 385 postParam := PostParam{ 386 ReportOrUpvoteIntervalSec: int64(24 * 3600), 387 PostIntervalSec: int64(600), 388 MaxReportReputation: types.NewCoinFromInt64(100 * types.Decimals), 389 } 390 repParam := ReputationParam{ 391 BestContentIndexN: 200, 392 UserMaxN: 40, 393 } 394 priceParam := PriceParam{ 395 UpdateEverySec: int64(time.Hour.Seconds()), 396 FeedEverySec: int64((10 * time.Minute).Seconds()), 397 HistoryMaxLen: 123, 398 PenaltyMissFeed: types.NewCoinFromInt64(10000 * types.Decimals), 399 } 400 401 err := ph.InitParamFromConfig( 402 ctx, globalAllocationParam, 403 postParam, 404 developerParam, 405 validatorParam, 406 voteParam, 407 proposalParam, 408 coinDayParam, 409 bandwidthParam, 410 accountParam, 411 repParam, 412 priceParam, 413 ) 414 assert.Nil(t, err) 415 416 checkStorage(t, ctx, ph, globalAllocationParam, 417 developerParam, validatorParam, voteParam, 418 proposalParam, coinDayParam, bandwidthParam, accountParam, postParam, repParam, priceParam) 419 } 420 421 func checkStorage(t *testing.T, ctx sdk.Context, ph ParamHolder, expectGlobalAllocationParam GlobalAllocationParam, 422 expectDeveloperParam DeveloperParam, 423 expectValidatorParam ValidatorParam, expectVoteParam VoteParam, 424 expectProposalParam ProposalParam, expectCoinDayParam CoinDayParam, 425 expectBandwidthParam BandwidthParam, expectAccountParam AccountParam, 426 expectPostParam PostParam, 427 expectedRepParam ReputationParam, 428 expectedPriceParam PriceParam, 429 ) { 430 globalAllocationParam := ph.GetGlobalAllocationParam(ctx) 431 assert.Equal(t, expectGlobalAllocationParam, *globalAllocationParam) 432 433 developerParam, err := ph.GetDeveloperParam(ctx) 434 assert.Nil(t, err) 435 assert.Equal(t, expectDeveloperParam, *developerParam) 436 437 validatorParam := ph.GetValidatorParam(ctx) 438 assert.Equal(t, expectValidatorParam, *validatorParam) 439 440 voteParam := ph.GetVoteParam(ctx) 441 assert.Equal(t, expectVoteParam, *voteParam) 442 443 proposalParam, err := ph.GetProposalParam(ctx) 444 assert.Nil(t, err) 445 assert.Equal(t, expectProposalParam, *proposalParam) 446 447 coinDayParam, err := ph.GetCoinDayParam(ctx) 448 assert.Nil(t, err) 449 assert.Equal(t, expectCoinDayParam, *coinDayParam) 450 451 bandwidthParam, err := ph.GetBandwidthParam(ctx) 452 assert.Nil(t, err) 453 assert.Equal(t, expectBandwidthParam, *bandwidthParam) 454 455 accountParam := ph.GetAccountParam(ctx) 456 assert.Equal(t, expectAccountParam, *accountParam) 457 458 postParam, err := ph.GetPostParam(ctx) 459 assert.Nil(t, err) 460 assert.Equal(t, expectPostParam, *postParam) 461 462 repParam := ph.GetReputationParam(ctx) 463 assert.Equal(t, expectedRepParam, *repParam) 464 465 priceParam := ph.GetPriceParam(ctx) 466 assert.Equal(t, expectedPriceParam, *priceParam) 467 } 468 469 func TestUpdateGlobalGrowthRate(t *testing.T) { 470 ph := NewParamHolder(TestKVStoreKey) 471 ctx := getContext() 472 473 testCases := []struct { 474 testName string 475 ceiling sdk.Dec 476 floor sdk.Dec 477 updateGrowthRate sdk.Dec 478 expectGrowthRate sdk.Dec 479 }{ 480 { 481 testName: "normal update", 482 updateGrowthRate: types.NewDecFromRat(98, 1000), 483 expectGrowthRate: types.NewDecFromRat(98, 1000), 484 }, 485 { 486 testName: "update to ceiling", 487 updateGrowthRate: types.NewDecFromRat(99, 1000), 488 expectGrowthRate: types.NewDecFromRat(98, 1000), 489 }, 490 { 491 testName: "update to floor", 492 updateGrowthRate: types.NewDecFromRat(29, 1000), 493 expectGrowthRate: types.NewDecFromRat(3, 100), 494 }, 495 } 496 for _, tc := range testCases { 497 globalParam := &GlobalAllocationParam{} 498 err := ph.setGlobalAllocationParam(ctx, globalParam) 499 assert.Nil(t, err) 500 err = ph.UpdateGlobalGrowthRate(ctx, tc.updateGrowthRate) 501 assert.Nil(t, err) 502 globalParam = ph.GetGlobalAllocationParam(ctx) 503 assert.Equal(t, globalParam.GlobalGrowthRate, tc.expectGrowthRate) 504 } 505 }