github.com/Finschia/finschia-sdk@v0.48.1/x/mint/types/minter_test.go (about) 1 package types 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 sdk "github.com/Finschia/finschia-sdk/types" 10 ) 11 12 func TestNextInflation(t *testing.T) { 13 minter := DefaultInitialMinter() 14 params := DefaultParams() 15 blocksPerYr := sdk.NewDec(int64(params.BlocksPerYear)) 16 17 // Governing Mechanism: 18 // inflationRateChangePerYear = (1- BondedRatio/ GoalBonded) * MaxInflationRateChange 19 20 tests := []struct { 21 bondedRatio, setInflation, expChange sdk.Dec 22 }{ 23 // with 0% bonded atom supply the inflation should increase by InflationRateChange 24 {sdk.ZeroDec(), sdk.NewDecWithPrec(7, 2), params.InflationRateChange.Quo(blocksPerYr)}, 25 26 // 100% bonded, starting at 20% inflation and being reduced 27 // (1 - (1/0.67))*(0.13/8667) 28 { 29 sdk.OneDec(), sdk.NewDecWithPrec(20, 2), 30 sdk.OneDec().Sub(sdk.OneDec().Quo(params.GoalBonded)).Mul(params.InflationRateChange).Quo(blocksPerYr), 31 }, 32 33 // 50% bonded, starting at 10% inflation and being increased 34 { 35 sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(10, 2), 36 sdk.OneDec().Sub(sdk.NewDecWithPrec(5, 1).Quo(params.GoalBonded)).Mul(params.InflationRateChange).Quo(blocksPerYr), 37 }, 38 39 // test 7% minimum stop (testing with 100% bonded) 40 {sdk.OneDec(), sdk.NewDecWithPrec(7, 2), sdk.ZeroDec()}, 41 {sdk.OneDec(), sdk.NewDecWithPrec(700000001, 10), sdk.NewDecWithPrec(-1, 10)}, 42 43 // test 20% maximum stop (testing with 0% bonded) 44 {sdk.ZeroDec(), sdk.NewDecWithPrec(20, 2), sdk.ZeroDec()}, 45 {sdk.ZeroDec(), sdk.NewDecWithPrec(1999999999, 10), sdk.NewDecWithPrec(1, 10)}, 46 47 // perfect balance shouldn't change inflation 48 {sdk.NewDecWithPrec(67, 2), sdk.NewDecWithPrec(15, 2), sdk.ZeroDec()}, 49 } 50 for i, tc := range tests { 51 minter.Inflation = tc.setInflation 52 53 inflation := minter.NextInflationRate(params, tc.bondedRatio) 54 diffInflation := inflation.Sub(tc.setInflation) 55 56 require.True(t, diffInflation.Equal(tc.expChange), 57 "Test Index: %v\nDiff: %v\nExpected: %v\n", i, diffInflation, tc.expChange) 58 } 59 } 60 61 func TestBlockProvision(t *testing.T) { 62 minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) 63 params := DefaultParams() 64 65 secondsPerYear := int64(60 * 60 * 8766) 66 67 tests := []struct { 68 annualProvisions int64 69 expProvisions int64 70 }{ 71 {secondsPerYear / 5, 1}, 72 {secondsPerYear/5 + 1, 1}, 73 {(secondsPerYear / 5) * 2, 2}, 74 {(secondsPerYear / 5) / 2, 0}, 75 } 76 for i, tc := range tests { 77 minter.AnnualProvisions = sdk.NewDec(tc.annualProvisions) 78 provisions := minter.BlockProvision(params) 79 80 expProvisions := sdk.NewCoin(params.MintDenom, 81 sdk.NewInt(tc.expProvisions)) 82 83 require.True(t, expProvisions.IsEqual(provisions), 84 "test: %v\n\tExp: %v\n\tGot: %v\n", 85 i, tc.expProvisions, provisions) 86 } 87 } 88 89 // Benchmarking :) 90 // previously using sdk.Int operations: 91 // BenchmarkBlockProvision-4 5000000 220 ns/op 92 // 93 // using sdk.Dec operations: (current implementation) 94 // BenchmarkBlockProvision-4 3000000 429 ns/op 95 func BenchmarkBlockProvision(b *testing.B) { 96 b.ReportAllocs() 97 minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) 98 params := DefaultParams() 99 100 s1 := rand.NewSource(100) 101 r1 := rand.New(s1) 102 minter.AnnualProvisions = sdk.NewDec(r1.Int63n(1000000)) 103 104 // run the BlockProvision function b.N times 105 for n := 0; n < b.N; n++ { 106 minter.BlockProvision(params) 107 } 108 } 109 110 // Next inflation benchmarking 111 // BenchmarkNextInflation-4 1000000 1828 ns/op 112 func BenchmarkNextInflation(b *testing.B) { 113 b.ReportAllocs() 114 minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) 115 params := DefaultParams() 116 bondedRatio := sdk.NewDecWithPrec(1, 1) 117 118 // run the NextInflationRate function b.N times 119 for n := 0; n < b.N; n++ { 120 minter.NextInflationRate(params, bondedRatio) 121 } 122 } 123 124 // Next annual provisions benchmarking 125 // BenchmarkNextAnnualProvisions-4 5000000 251 ns/op 126 func BenchmarkNextAnnualProvisions(b *testing.B) { 127 b.ReportAllocs() 128 minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) 129 params := DefaultParams() 130 totalSupply := sdk.NewInt(100000000000000) 131 132 // run the NextAnnualProvisions function b.N times 133 for n := 0; n < b.N; n++ { 134 minter.NextAnnualProvisions(params, totalSupply) 135 } 136 }