Skip to content

Commit ee39fdd

Browse files
authored
Add more mint unit tests for mint module (#856)
* Improve coverage for mint methods * Ongoing mint
1 parent 0453c1a commit ee39fdd

File tree

9 files changed

+447
-167
lines changed

9 files changed

+447
-167
lines changed

x/mint/client/rest/query.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
1616
"/minting/parameters",
1717
queryParamsHandlerFn(clientCtx),
1818
).Methods("GET")
19-
20-
r.HandleFunc(
21-
"/minting/inflation",
22-
queryInflationHandlerFn(clientCtx),
23-
).Methods("GET")
24-
2519
r.HandleFunc(
26-
"/minting/annual-provisions",
27-
queryAnnualProvisionsHandlerFn(clientCtx),
20+
"/minting/minter",
21+
queryMinterHandlerFn(clientCtx),
2822
).Methods("GET")
2923
}
3024

@@ -47,28 +41,9 @@ func queryParamsHandlerFn(clientCtx client.Context) http.HandlerFunc {
4741
}
4842
}
4943

50-
func queryInflationHandlerFn(clientCtx client.Context) http.HandlerFunc {
51-
return func(w http.ResponseWriter, r *http.Request) {
52-
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation)
53-
54-
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
55-
if !ok {
56-
return
57-
}
58-
59-
res, height, err := clientCtx.QueryWithData(route, nil)
60-
if rest.CheckInternalServerError(w, err) {
61-
return
62-
}
63-
64-
clientCtx = clientCtx.WithHeight(height)
65-
rest.PostProcessResponse(w, clientCtx, res)
66-
}
67-
}
68-
69-
func queryAnnualProvisionsHandlerFn(clientCtx client.Context) http.HandlerFunc {
44+
func queryMinterHandlerFn(clientCtx client.Context) http.HandlerFunc {
7045
return func(w http.ResponseWriter, r *http.Request) {
71-
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions)
46+
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryMinter)
7247

7348
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
7449
if !ok {

x/mint/keeper/genesis_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package keeper_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/sei-protocol/sei-chain/app"
8+
"github.com/sei-protocol/sei-chain/testutil/nullify"
9+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
10+
11+
"github.com/sei-protocol/sei-chain/x/mint/types"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestGenesis(t *testing.T) {
16+
app := app.Setup(false)
17+
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
18+
19+
now := time.Now()
20+
21+
params := types.DefaultParams()
22+
params.TokenReleaseSchedule = []types.ScheduledTokenRelease{
23+
{
24+
StartDate: now.Format(types.TokenReleaseDateFormat),
25+
EndDate: now.Format(types.TokenReleaseDateFormat),
26+
TokenReleaseAmount: 100,
27+
},
28+
}
29+
genesisState := types.GenesisState{
30+
Params: params,
31+
Minter: types.Minter{
32+
StartDate: now.Format(types.TokenReleaseDateFormat),
33+
EndDate: now.Format(types.TokenReleaseDateFormat),
34+
Denom: "usei",
35+
TotalMintAmount: 100,
36+
RemainingMintAmount: 0,
37+
LastMintAmount: 100,
38+
LastMintDate: "2023-04-01",
39+
LastMintHeight: 0,
40+
},
41+
}
42+
43+
app.MintKeeper.InitGenesis(ctx, &genesisState)
44+
got := app.MintKeeper.ExportGenesis(ctx)
45+
require.NotNil(t, got)
46+
require.Equal(t, got, &genesisState)
47+
48+
nullify.Fill(&genesisState)
49+
nullify.Fill(got)
50+
}

x/mint/keeper/hooks.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
epochTypes "github.com/sei-protocol/sei-chain/x/epoch/types"
66
)
77

8-
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epoch epochTypes.Epoch) {
9-
}
8+
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epoch epochTypes.Epoch) {}
109

1110
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epoch epochTypes.Epoch) {
1211
latestMinter := k.GetOrUpdateLatestMinter(ctx, epoch)

x/mint/keeper/keeper.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func NewKeeper(
3232
// ensure mint module account is set
3333
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
3434
panic("the mint module account has not been set")
35+
} else {
36+
println(addr.String())
3537
}
3638

3739
// set KeyTable if it has not already been set
@@ -120,11 +122,6 @@ func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error {
120122
return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.feeCollectorName, fees)
121123
}
122124

123-
// GetProportions gets the balance of the `MintedDenom` from minted coins and returns coins according to the `AllocationRatio`.
124-
func (k Keeper) GetProportions(ctx sdk.Context, mintedCoin sdk.Coin, ratio sdk.Dec) sdk.Coin {
125-
return sdk.NewCoin(mintedCoin.Denom, mintedCoin.Amount.ToDec().Mul(ratio).TruncateInt())
126-
}
127-
128125
// GetProportions gets the balance of the `MintedDenom` from minted coins and returns coins according to the `AllocationRatio`.
129126
func (k Keeper) GetOrUpdateLatestMinter(
130127
ctx sdk.Context,

x/mint/keeper/keeper_test.go

Lines changed: 134 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,76 @@ import (
44
"testing"
55
"time"
66

7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
79
epochTypes "github.com/sei-protocol/sei-chain/x/epoch/types"
10+
"github.com/sei-protocol/sei-chain/x/mint/keeper"
811
mintKeeper "github.com/sei-protocol/sei-chain/x/mint/keeper"
912
"github.com/sei-protocol/sei-chain/x/mint/types"
1013
mintTypes "github.com/sei-protocol/sei-chain/x/mint/types"
14+
1115
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
1216
"github.com/stretchr/testify/require"
1317
)
1418

19+
type MockAccountKeeper struct {
20+
ModuleAddress sdk.AccAddress
21+
ModuleAccount authtypes.ModuleAccountI
22+
moduleNameToAddress map[string]string
23+
}
24+
25+
func (m MockAccountKeeper) GetModuleAddress(name string) sdk.AccAddress {
26+
if addrStr, ok := m.moduleNameToAddress[name]; ok {
27+
addr, _ := sdk.AccAddressFromBech32(addrStr)
28+
return addr
29+
}
30+
return nil
31+
}
32+
33+
func (m MockAccountKeeper) SetModuleAccount(ctx sdk.Context, account authtypes.ModuleAccountI) {
34+
m.ModuleAccount = account
35+
}
36+
37+
func (m MockAccountKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI {
38+
return m.ModuleAccount
39+
}
40+
41+
func (m MockAccountKeeper) SetModuleAddress(name, address string) {
42+
m.moduleNameToAddress[name] = address
43+
}
44+
45+
type MockMintHooks struct {
46+
afterDistributeMintedCoinCalled bool
47+
}
48+
49+
func (h *MockMintHooks) AfterDistributeMintedCoin(ctx sdk.Context, mintedCoin sdk.Coin) {
50+
h.afterDistributeMintedCoinCalled = true
51+
}
52+
1553
func TestGetNextScheduledTokenRelease(t *testing.T) {
1654
t.Parallel()
1755

1856
currentTime := time.Now().UTC()
1957
epoch := epochTypes.Epoch{
2058
CurrentEpochStartTime: currentTime,
21-
CurrentEpochHeight: 100,
59+
CurrentEpochHeight: 100,
2260
}
2361
currentMinter := mintTypes.DefaultInitialMinter()
2462

2563
tokenReleaseSchedule := []mintTypes.ScheduledTokenRelease{
2664
{
27-
StartDate: currentTime.AddDate(0, 0, 30).Format(minttypes.TokenReleaseDateFormat),
28-
EndDate: currentTime.AddDate(0, 2, 0).Format(minttypes.TokenReleaseDateFormat),
65+
StartDate: currentTime.AddDate(0, 0, 30).Format(minttypes.TokenReleaseDateFormat),
66+
EndDate: currentTime.AddDate(0, 2, 0).Format(minttypes.TokenReleaseDateFormat),
2967
TokenReleaseAmount: 200,
3068
},
3169
{
32-
StartDate: currentTime.AddDate(1, 0, 0).Format(minttypes.TokenReleaseDateFormat),
33-
EndDate: currentTime.AddDate(2, 0, 0).Format(minttypes.TokenReleaseDateFormat),
70+
StartDate: currentTime.AddDate(1, 0, 0).Format(minttypes.TokenReleaseDateFormat),
71+
EndDate: currentTime.AddDate(2, 0, 0).Format(minttypes.TokenReleaseDateFormat),
3472
TokenReleaseAmount: 300,
3573
},
3674
{
37-
StartDate: currentTime.AddDate(0, 0, 1).Format(minttypes.TokenReleaseDateFormat),
38-
EndDate: currentTime.AddDate(0, 0, 15).Format(minttypes.TokenReleaseDateFormat),
75+
StartDate: currentTime.AddDate(0, 0, 1).Format(minttypes.TokenReleaseDateFormat),
76+
EndDate: currentTime.AddDate(0, 0, 15).Format(minttypes.TokenReleaseDateFormat),
3977
TokenReleaseAmount: 100,
4078
},
4179
}
@@ -81,6 +119,21 @@ func TestGetNextScheduledTokenRelease(t *testing.T) {
81119
require.Equal(t, uint64(100), nextScheduledRelease.GetTokenReleaseAmount())
82120
require.Equal(t, currentTime.AddDate(0, 0, 1).Format(minttypes.TokenReleaseDateFormat), nextScheduledRelease.GetStartDate())
83121
})
122+
123+
t.Run("Panic on invalid foramt", func(t *testing.T) {
124+
// No next scheduled token release intially
125+
tokenReleaseSchedule := []mintTypes.ScheduledTokenRelease{
126+
{
127+
StartDate: "Bad Start Date",
128+
EndDate: currentTime.AddDate(0, 2, 0).Format(minttypes.TokenReleaseDateFormat),
129+
TokenReleaseAmount: 200,
130+
},
131+
}
132+
epoch.CurrentEpochStartTime = currentTime.AddDate(0, 0, 0)
133+
require.Panics(t, func() {
134+
mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, currentMinter)
135+
})
136+
})
84137
}
85138

86139
func TestGetOrUpdateLatestMinter(t *testing.T) {
@@ -100,7 +153,7 @@ func TestGetOrUpdateLatestMinter(t *testing.T) {
100153
t.Run("No ongoing release, but there's a scheduled release", func(t *testing.T) {
101154
mintKeeper.SetMinter(ctx, mintTypes.NewMinter(
102155
currentTime.Format(minttypes.TokenReleaseDateFormat),
103-
currentTime.AddDate(1,0,0).Format(minttypes.TokenReleaseDateFormat),
156+
currentTime.AddDate(1, 0, 0).Format(minttypes.TokenReleaseDateFormat),
104157
"usei",
105158
1000,
106159
))
@@ -115,22 +168,22 @@ func TestGetOrUpdateLatestMinter(t *testing.T) {
115168
params := mintKeeper.GetParams(ctx)
116169
params.TokenReleaseSchedule = []types.ScheduledTokenRelease{
117170
{
118-
StartDate: currentTime.AddDate(0,0,0).Format(minttypes.TokenReleaseDateFormat),
119-
EndDate: currentTime.AddDate(0,0,0).Format(minttypes.TokenReleaseDateFormat),
171+
StartDate: currentTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
172+
EndDate: currentTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
120173
TokenReleaseAmount: 1000,
121174
},
122175
}
123176
mintKeeper.SetParams(ctx, params)
124177

125178
minter := types.Minter{
126-
StartDate: currentTime.Format(minttypes.TokenReleaseDateFormat),
127-
EndDate: currentTime.Format(minttypes.TokenReleaseDateFormat),
128-
Denom: "usei",
129-
TotalMintAmount: 100,
179+
StartDate: currentTime.Format(minttypes.TokenReleaseDateFormat),
180+
EndDate: currentTime.Format(minttypes.TokenReleaseDateFormat),
181+
Denom: "usei",
182+
TotalMintAmount: 100,
130183
RemainingMintAmount: 0,
131-
LastMintAmount: 100,
132-
LastMintDate: "2023-04-01",
133-
LastMintHeight: 0,
184+
LastMintAmount: 100,
185+
LastMintDate: "2023-04-01",
186+
LastMintHeight: 0,
134187
}
135188
mintKeeper.SetMinter(ctx, minter)
136189

@@ -144,18 +197,17 @@ func TestGetOrUpdateLatestMinter(t *testing.T) {
144197
mintKeeper.SetMinter(ctx, mintTypes.DefaultInitialMinter())
145198
})
146199

147-
148200
t.Run("TokenReleaseSchedule not sorted", func(t *testing.T) {
149201
params := mintKeeper.GetParams(ctx)
150202
params.TokenReleaseSchedule = []types.ScheduledTokenRelease{
151203
{
152-
StartDate: currentTime.AddDate(0,20,0).Format(minttypes.TokenReleaseDateFormat),
153-
EndDate: currentTime.AddDate(0,45,0).Format(minttypes.TokenReleaseDateFormat),
204+
StartDate: currentTime.AddDate(0, 20, 0).Format(minttypes.TokenReleaseDateFormat),
205+
EndDate: currentTime.AddDate(0, 45, 0).Format(minttypes.TokenReleaseDateFormat),
154206
TokenReleaseAmount: 2000,
155207
},
156208
{
157209
StartDate: currentTime.Format(minttypes.TokenReleaseDateFormat),
158-
EndDate: currentTime.AddDate(0,15,0).Format(minttypes.TokenReleaseDateFormat),
210+
EndDate: currentTime.AddDate(0, 15, 0).Format(minttypes.TokenReleaseDateFormat),
159211
TokenReleaseAmount: 1000,
160212
},
161213
}
@@ -167,3 +219,64 @@ func TestGetOrUpdateLatestMinter(t *testing.T) {
167219
require.Equal(t, currentTime.Format(minttypes.TokenReleaseDateFormat), currentMinter.StartDate)
168220
})
169221
}
222+
223+
func TestBaseCases(t *testing.T) {
224+
t.Parallel()
225+
app, ctx := createTestApp(false)
226+
mintKeeper := app.MintKeeper
227+
228+
t.Run("invalid module name", func(t *testing.T) {
229+
mockAccountKeeper := MockAccountKeeper{}
230+
231+
require.Panics(t, func() {
232+
keeper.NewKeeper(
233+
mintKeeper.GetCdc(),
234+
mintKeeper.GetStoreKey(),
235+
mintKeeper.GetParamSpace(),
236+
nil,
237+
mockAccountKeeper,
238+
nil,
239+
nil,
240+
"invalid module",
241+
)
242+
})
243+
})
244+
245+
t.Run("set hooks", func(t *testing.T) {
246+
newHook := &MockMintHooks{}
247+
mintKeeper.SetHooks(newHook)
248+
249+
require.PanicsWithValue(t, "cannot set mint hooks twice", func() {
250+
mintKeeper.SetHooks(newHook)
251+
})
252+
})
253+
254+
t.Run("nil minter", func(t *testing.T) {
255+
nilApp, nilCtx := createTestApp(false)
256+
257+
store := nilCtx.KVStore(nilApp.MintKeeper.GetStoreKey())
258+
store.Delete(types.MinterKey)
259+
require.PanicsWithValue(t, "stored minter should not have been nil", func() {
260+
nilApp.MintKeeper.GetMinter(nilCtx)
261+
})
262+
})
263+
264+
t.Run("staking keeper calls", func(t *testing.T) {
265+
require.False(t, mintKeeper.StakingTokenSupply(ctx).IsNil())
266+
require.False(t, mintKeeper.BondedRatio(ctx).IsNil())
267+
})
268+
269+
t.Run("mint keeper calls", func(t *testing.T) {
270+
require.NotNil(t, mintKeeper.GetStoreKey())
271+
require.NotNil(t, mintKeeper.GetCdc())
272+
require.NotNil(t, mintKeeper.GetParamSpace())
273+
require.NotPanics(t, func() {
274+
mintKeeper.SetParamSpace(mintKeeper.GetParamSpace())
275+
})
276+
})
277+
278+
t.Run("staking keeper calls", func(t *testing.T) {
279+
require.Nil(t, mintKeeper.MintCoins(ctx, sdk.NewCoins()))
280+
})
281+
282+
}

0 commit comments

Comments
 (0)