/
mint_test.go
143 lines (115 loc) · 4.7 KB
/
mint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package test
import (
"context"
"fmt"
"testing"
"time"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/test/util/testnode"
minttypes "github.com/celestiaorg/celestia-app/x/mint/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/metadata"
)
type IntegrationTestSuite struct {
suite.Suite
cctx testnode.Context
}
func (s *IntegrationTestSuite) SetupSuite() {
t := s.T()
t.Log("setting up mint integration test suite")
cparams := testnode.DefaultParams()
oneDay := time.Hour * 24
oneMonth := oneDay * 30
sixMonths := oneMonth * 6
// Set the minimum time between blocks to six months. This will make the
// timestamps between blocks increase by six months each block despite that
// much time not actually passing. We do this to test the inflation rate
// over time without having to wait one year for the test to complete.
//
// Note: if TimeIotaMs is removed from CometBFT, this technique will no
// longer work.
cparams.Block.TimeIotaMs = int64(sixMonths.Milliseconds())
cfg := testnode.DefaultConfig().
WithConsensusParams(cparams)
cctx, _, _ := testnode.NewNetwork(t, cfg)
s.cctx = cctx
}
// TestTotalSupplyIncreasesOverTime tests that the total supply of tokens
// increases over time as new blocks are added to the chain.
func (s *IntegrationTestSuite) TestTotalSupplyIncreasesOverTime() {
require := s.Require()
initialHeight := int64(1)
laterHeight := int64(20)
err := s.cctx.WaitForNextBlock()
require.NoError(err)
initalSupply := s.getTotalSupply(initialHeight)
_, err = s.cctx.WaitForHeight(laterHeight)
require.NoError(err)
laterSupply := s.getTotalSupply(laterHeight)
require.True(initalSupply.AmountOf(app.BondDenom).LT(laterSupply.AmountOf(app.BondDenom)))
}
// TestInflationRate verifies that the inflation rate each year matches the
// expected rate of inflation. See the README.md for the expected rate of
// inflation.
func (s *IntegrationTestSuite) TestInflationRate() {
require := s.Require()
type testCase struct {
year int64
want sdktypes.Dec
}
testCases := []testCase{
{year: 0, want: sdktypes.MustNewDecFromStr("8.00")},
{year: 1, want: sdktypes.MustNewDecFromStr("7.20")},
{year: 2, want: sdktypes.MustNewDecFromStr("6.48")},
{year: 3, want: sdktypes.MustNewDecFromStr("5.832")},
{year: 4, want: sdktypes.MustNewDecFromStr("5.2488")},
{year: 5, want: sdktypes.MustNewDecFromStr("4.72392")},
// Note: since the testnode takes time to create blocks, test cases
// for years 6+ will time out give the current TimeIotaMs.
}
genesisTime, err := s.cctx.GenesisTime()
require.NoError(err)
lastYear := testCases[len(testCases)-1].year
lastTimestamp := genesisTime.Add(time.Duration((lastYear + 1) * minttypes.NanosecondsPerYear))
_, err = s.cctx.WaitForTimestamp(lastTimestamp)
require.NoError(err)
for _, tc := range testCases {
startTimestamp := genesisTime.Add(time.Duration(tc.year * minttypes.NanosecondsPerYear))
endTimestamp := genesisTime.Add(time.Duration((tc.year + 1) * minttypes.NanosecondsPerYear))
startHeight, err := s.cctx.HeightForTimestamp(startTimestamp)
require.NoError(err)
endHeight, err := s.cctx.HeightForTimestamp(endTimestamp)
require.NoError(err)
inflationRate := s.estimateInflationRate(startHeight, endHeight)
actualError := inflationRate.Sub(tc.want).Abs()
marginOfError := sdktypes.MustNewDecFromStr("0.01")
if marginOfError.GT(actualError) {
s.Failf("TestInflationRate failure", "inflation rate for year %v is %v, want %v with a .01 margin of error", tc.year, inflationRate, tc.want)
}
}
}
func (s *IntegrationTestSuite) getTotalSupply(height int64) sdktypes.Coins {
require := s.Require()
bqc := banktypes.NewQueryClient(s.cctx.GRPCClient)
ctx := metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, fmt.Sprintf("%d", height))
resp, err := bqc.TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
require.NoError(err)
return resp.Supply
}
func (s *IntegrationTestSuite) estimateInflationRate(startHeight int64, endHeight int64) sdktypes.Dec {
startSupply := s.getTotalSupply(startHeight).AmountOf(app.BondDenom)
endSupply := s.getTotalSupply(endHeight).AmountOf(app.BondDenom)
diffSupply := endSupply.Sub(startSupply)
return sdktypes.NewDecFromBigInt(diffSupply.BigInt()).QuoInt(startSupply)
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestIntegrationTestSuite(t *testing.T) {
if testing.Short() {
t.Skip("skipping mint integration test in short mode.")
}
suite.Run(t, new(IntegrationTestSuite))
}