forked from cosmos/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_test.go
162 lines (151 loc) · 5.53 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
package globalfee
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/simapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/gaia/v15/x/globalfee/types"
)
func TestDefaultGenesis(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
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 := simapp.MakeTestEncodingConfig()
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, simappparams.EncodingConfig, paramstypes.Subspace) {
t.Helper()
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
encCfg := simapp.MakeTestEncodingConfig()
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())
paramsKeeper := paramskeeper.NewKeeper(encCfg.Marshaler, encCfg.Amino, keyParams, tkeyParams)
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 := paramsKeeper.Subspace(ModuleName).WithKeyTable(types.ParamKeyTable())
return ctx, encCfg, subspace
}