forked from cosmos/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_test.go
164 lines (153 loc) · 5.37 KB
/
genesis_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package globalfee
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
gaiaparams "github.com/cosmos/gaia/v16/app/params"
"github.com/cosmos/gaia/v16/x/globalfee/types"
)
func TestDefaultGenesis(t *testing.T) {
encCfg := gaiaparams.MakeEncodingConfig()
gotJSON := AppModuleBasic{}.DefaultGenesis(encCfg.Marshaler)
assert.JSONEq(t,
`{"params":{"minimum_gas_prices":[],"bypass_min_fee_msg_types":["/ibc.core.channel.v1.MsgRecvPacket","/ibc.core.channel.v1.MsgAcknowledgement","/ibc.core.client.v1.MsgUpdateClient","/ibc.core.channel.v1.MsgTimeout","/ibc.core.channel.v1.MsgTimeoutOnClose"], "max_total_bypass_min_fee_msg_gas_usage":"1000000"}}`,
string(gotJSON), string(gotJSON))
}
func TestValidateGenesis(t *testing.T) {
encCfg := gaiaparams.MakeEncodingConfig()
specs := map[string]struct {
src string
expErr bool
}{
"all good": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}], "bypass_min_fee_msg_types":["/ibc.core.channel.v1.MsgRecvPacket"]}}`,
expErr: false,
},
"empty minimum": {
src: `{"params":{"minimum_gas_prices":[], "bypass_min_fee_msg_types":[]}}`,
expErr: false,
},
"minimum and bypass not set": {
src: `{"params":{}}`,
expErr: false,
},
"minimum not set": {
src: `{"params":{"bypass_min_fee_msg_types":[]}}`,
expErr: false,
},
"bypass not set": {
src: `{"params":{"minimum_gas_prices":[]}}`,
expErr: false,
},
"zero amount allowed": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"0"}]}}`,
expErr: false,
},
"duplicate denoms not allowed": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"},{"denom":"ALX", "amount":"2"}]}}`,
expErr: true,
},
"negative amounts not allowed": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"-1"}]}}`,
expErr: true,
},
"denom must be sorted": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ZLX", "amount":"1"},{"denom":"ALX", "amount":"2"}]}}`,
expErr: true,
},
"empty bypass msg types not allowed": {
src: `{"params":{"bypass_min_fee_msg_types":[""]}}`,
expErr: true,
},
"sorted denoms is allowed": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"},{"denom":"ZLX", "amount":"2"}]}}`,
expErr: false,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
gotErr := AppModuleBasic{}.ValidateGenesis(encCfg.Marshaler, nil, []byte(spec.src))
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
})
}
}
func TestInitExportGenesis(t *testing.T) {
specs := map[string]struct {
src string
exp types.GenesisState
}{
"single fee": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}], "bypass_min_fee_msg_types":["/ibc.core.channel.v1.MsgRecvPacket"]}}`,
exp: types.GenesisState{
Params: types.Params{
MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1))),
BypassMinFeeMsgTypes: []string{"/ibc.core.channel.v1.MsgRecvPacket"},
},
},
},
"multiple fee options": {
src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}, {"denom":"BLX", "amount":"0.001"}], "bypass_min_fee_msg_types":["/ibc.core.channel.v1.MsgRecvPacket","/ibc.core.channel.v1.MsgTimeoutOnClose"]}}`,
exp: types.GenesisState{
Params: types.Params{
MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1)),
sdk.NewDecCoinFromDec("BLX", sdk.NewDecWithPrec(1, 3))),
BypassMinFeeMsgTypes: []string{"/ibc.core.channel.v1.MsgRecvPacket", "/ibc.core.channel.v1.MsgTimeoutOnClose"},
},
},
},
"no fee set": {
src: `{"params":{}}`,
exp: types.GenesisState{
Params: types.Params{
MinimumGasPrices: sdk.DecCoins{},
BypassMinFeeMsgTypes: []string{},
},
},
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
ctx, encCfg, subspace := setupTestStore(t)
m := NewAppModule(subspace)
m.InitGenesis(ctx, encCfg.Marshaler, []byte(spec.src))
gotJSON := m.ExportGenesis(ctx, encCfg.Marshaler)
var got types.GenesisState
require.NoError(t, encCfg.Marshaler.UnmarshalJSON(gotJSON, &got))
assert.Equal(t, spec.exp, got, string(gotJSON))
})
}
}
func setupTestStore(t *testing.T) (sdk.Context, gaiaparams.EncodingConfig, paramstypes.Subspace) {
t.Helper()
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
encCfg := gaiaparams.MakeEncodingConfig()
keyParams := sdk.NewKVStoreKey(paramstypes.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(paramstypes.TStoreKey)
ms.MountStoreWithDB(keyParams, storetypes.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, storetypes.StoreTypeTransient, db)
require.NoError(t, ms.LoadLatestVersion())
ctx := sdk.NewContext(ms, tmproto.Header{
Height: 1234567,
Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC),
}, false, log.NewNopLogger())
subspace := paramstypes.NewSubspace(encCfg.Marshaler,
encCfg.Amino,
keyParams,
tkeyParams,
paramstypes.ModuleName,
)
return ctx, encCfg, subspace
}