-
Notifications
You must be signed in to change notification settings - Fork 206
/
testapp.go
165 lines (138 loc) · 5.1 KB
/
testapp.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
package simapp
import (
"encoding/json"
"os"
"path/filepath"
"time"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
"github.com/NibiruChain/nibiru/x/common"
pricefeedtypes "github.com/NibiruChain/nibiru/x/pricefeed/types"
)
// NewTestNibiruApp creates an application instance ('app.NibiruApp') with an in-memory
// database ('tmdb.MemDB') and disabled logging. It either uses the application's
// default genesis state or a blank one.
func NewTestNibiruApp(shouldUseDefaultGenesis bool) *NibiruTestApp {
encoding := simapp.MakeTestEncodingConfig()
var appGenesis GenesisState
if shouldUseDefaultGenesis {
appGenesis = NewDefaultGenesisState(encoding.Marshaler)
}
return NewTestNibiruAppWithGenesis(appGenesis)
}
// NewTestNibiruAppAndContext creates an 'app.NibiruApp' instance with an in-memory
// 'tmdb.MemDB' and fresh 'sdk.Context'.
func NewTestNibiruAppAndContext(shouldUseDefaultGenesis bool) (*NibiruTestApp, sdk.Context) {
newNibiruApp := NewTestNibiruApp(shouldUseDefaultGenesis)
ctx := newNibiruApp.NewContext(false, tmproto.Header{})
return newNibiruApp, ctx
}
// NewTestNibiruAppWithGenesis initializes a chain with the given genesis state to
// creates an application instance ('app.NibiruApp'). This app uses an
// in-memory database ('tmdb.MemDB') and has logging disabled.
func NewTestNibiruAppWithGenesis(gen GenesisState) *NibiruTestApp {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
nodeHome := filepath.Join(userHomeDir, ".nibid")
db := tmdb.NewMemDB()
logger := log.NewNopLogger()
encoding := MakeTestEncodingConfig()
nibiruApp := NewNibiruTestApp(
logger,
db,
/*traceStore=*/ nil,
/*loadLatest=*/ true,
/*skipUpgradeHeights=*/ map[int64]bool{},
/*homePath=*/ nodeHome,
/*invCheckPeriod=*/ 0,
/*encodingConfig=*/ encoding,
/*appOpts=*/ simapp.EmptyAppOptions{},
)
stateBytes, err := json.MarshalIndent(gen, "", " ")
if err != nil {
panic(err)
}
nibiruApp.InitChain(abci.RequestInitChain{
ConsensusParams: simapp.DefaultConsensusParams,
AppStateBytes: stateBytes,
})
return nibiruApp
}
// ----------------------------------------------------------------------------
// Genesis
// ----------------------------------------------------------------------------
const (
GenOracleAddress = "nibi1zuxt7fvuxgj69mjxu3auca96zemqef5u2yemly"
)
/*
NewTestGenesisStateFromDefault returns 'NewGenesisState' using the default
genesis as input. The blockchain genesis state is represented as a map from module
identifier strings to raw json messages.
*/
func NewTestGenesisStateFromDefault() GenesisState {
encodingConfig := MakeTestEncodingConfig()
codec := encodingConfig.Marshaler
genState := NewDefaultGenesisState(codec)
return NewTestGenesisState(codec, genState)
}
/*
NewTestGenesisState transforms 'inGenState' to add genesis parameter changes
that are well suited to integration testing, then returns the transformed genesis.
The blockchain genesis state is represented as a map from module identifier strings
to raw json messages.
Args:
- codec: Serializer for the module genesis state proto.Messages
- inGenState: Input genesis state before the custom test setup is applied
*/
func NewTestGenesisState(codec codec.Codec, inGenState GenesisState,
) (testGenState GenesisState) {
testGenState = inGenState
// Set short voting period to allow fast gov proposals in tests
var govGenState govtypes.GenesisState
codec.MustUnmarshalJSON(testGenState[govtypes.ModuleName], &govGenState)
govGenState.VotingParams.VotingPeriod = time.Second * 20
govGenState.DepositParams.MinDeposit = sdk.NewCoins(sdk.NewInt64Coin(common.DenomNIBI, 1_000_000)) // min deposit of 1 NIBI
testGenState[govtypes.ModuleName] = codec.MustMarshalJSON(&govGenState)
// pricefeed genesis state
pfGenState := PricefeedGenesis()
testGenState[pricefeedtypes.ModuleName] = codec.MustMarshalJSON(&pfGenState)
return testGenState
}
// ----------------------------------------------------------------------------
// Module types.GenesisState functions
/*
PricefeedGenesis returns an x/pricefeed GenesisState with additional
configuration for convenience during integration tests.
*/
func PricefeedGenesis() pricefeedtypes.GenesisState {
oracle := sdk.MustAccAddressFromBech32(GenOracleAddress)
oracleStrings := []string{oracle.String()}
var gen pricefeedtypes.GenesisState
pairs := pricefeedtypes.DefaultPairs
gen.Params.Pairs = pairs
gen.Params.TwapLookbackWindow = 15 * time.Minute
gen.PostedPrices = []pricefeedtypes.PostedPrice{
{
PairID: pairs[0].String(), // PairGovStable
Oracle: oracle.String(),
Price: sdk.NewDec(10),
Expiry: time.Now().Add(1 * time.Hour),
},
{
PairID: pairs[1].String(), // PairCollStable
Oracle: oracle.String(),
Price: sdk.OneDec(),
Expiry: time.Now().Add(1 * time.Hour),
},
}
gen.GenesisOracles = oracleStrings
return gen
}