-
Notifications
You must be signed in to change notification settings - Fork 170
/
test_helpers.go
186 lines (161 loc) · 5.42 KB
/
test_helpers.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package app
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/baseapp"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
leveragetypes "github.com/umee-network/umee/v2/x/leverage/types"
oracletypes "github.com/umee-network/umee/v2/x/oracle/types"
)
// DefaultConsensusParams defines the default Tendermint consensus params used
// in UmeeApp testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxBytes: 200000,
MaxGas: 2000000,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
}
type EmptyAppOptions struct{}
func (EmptyAppOptions) Get(o string) interface{} { return nil }
func Setup(t *testing.T, isCheckTx bool, invCheckPeriod uint) *UmeeApp {
t.Helper()
app, genesisState := setup(!isCheckTx, invCheckPeriod)
if !isCheckTx {
// InitChain must be called to stop deliverState from being nil
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
return app
}
func setup(withGenesis bool, invCheckPeriod uint) (*UmeeApp, GenesisState) {
db := dbm.NewMemDB()
encCdc := MakeEncodingConfig()
app := New(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
DefaultNodeHome,
invCheckPeriod,
encCdc,
EmptyAppOptions{},
)
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
}
return app, GenesisState{}
}
// IntegrationTestNetworkConfig returns a networking configuration used for
// integration tests using the SDK's in-process network test suite.
func IntegrationTestNetworkConfig() network.Config {
cfg := network.DefaultConfig()
encCfg := MakeEncodingConfig()
cdc := encCfg.Marshaler
// Start with the default genesis state
appGenState := ModuleBasics.DefaultGenesis(encCfg.Marshaler)
// Extract the x/leverage part of the genesis state to be modified
var leverageGenState leveragetypes.GenesisState
if err := cdc.UnmarshalJSON(appGenState[leveragetypes.ModuleName], &leverageGenState); err != nil {
panic(err)
}
// Modify the x/leverage genesis state
leverageGenState.Registry = append(leverageGenState.Registry, leveragetypes.Token{
BaseDenom: BondDenom,
SymbolDenom: DisplayDenom,
Exponent: 6,
ReserveFactor: sdk.MustNewDecFromStr("0.100000000000000000"),
CollateralWeight: sdk.MustNewDecFromStr("0.050000000000000000"),
LiquidationThreshold: sdk.MustNewDecFromStr("0.050000000000000000"),
BaseBorrowRate: sdk.MustNewDecFromStr("0.020000000000000000"),
KinkBorrowRate: sdk.MustNewDecFromStr("0.200000000000000000"),
MaxBorrowRate: sdk.MustNewDecFromStr("1.50000000000000000"),
KinkUtilizationRate: sdk.MustNewDecFromStr("0.200000000000000000"),
LiquidationIncentive: sdk.MustNewDecFromStr("0.180000000000000000"),
})
// Marshal the modified state and add it back into appGenState
bz, err := cdc.MarshalJSON(&leverageGenState)
if err != nil {
panic(err)
}
appGenState[leveragetypes.ModuleName] = bz
var oracleGenState oracletypes.GenesisState
if err := cdc.UnmarshalJSON(appGenState[oracletypes.ModuleName], &oracleGenState); err != nil {
panic(err)
}
// Set mock exchange rates and a large enough vote period such that we won't
// execute ballot voting and thus clear out previous exchange rates, since we
// are not running a price-feeder.
oracleGenState.Params.VotePeriod = 1000
oracleGenState.ExchangeRates = append(oracleGenState.ExchangeRates, oracletypes.NewExchangeRateTuple(
DisplayDenom, sdk.MustNewDecFromStr("34.21"),
))
bz, err = cdc.MarshalJSON(&oracleGenState)
if err != nil {
panic(err)
}
appGenState[oracletypes.ModuleName] = bz
var govGenState govtypes.GenesisState
if err := cdc.UnmarshalJSON(appGenState[govtypes.ModuleName], &govGenState); err != nil {
panic(err)
}
govGenState.VotingParams.VotingPeriod = time.Minute
bz, err = cdc.MarshalJSON(&govGenState)
if err != nil {
panic(err)
}
appGenState[govtypes.ModuleName] = bz
cfg.Codec = encCfg.Marshaler
cfg.TxConfig = encCfg.TxConfig
cfg.LegacyAmino = encCfg.Amino
cfg.InterfaceRegistry = encCfg.InterfaceRegistry
cfg.GenesisState = appGenState
cfg.BondDenom = BondDenom
cfg.MinGasPrices = fmt.Sprintf("0.000006%s", BondDenom)
cfg.AppConstructor = func(val network.Validator) servertypes.Application {
return New(
val.Ctx.Logger,
dbm.NewMemDB(),
nil,
true,
make(map[int64]bool),
val.Ctx.Config.RootDir,
0,
encCfg,
EmptyAppOptions{},
baseapp.SetPruning(storetypes.NewPruningOptionsFromString(val.AppConfig.Pruning)),
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
)
}
return cfg
}