-
Notifications
You must be signed in to change notification settings - Fork 202
/
testapp.go
129 lines (107 loc) · 4.2 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
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"
)
// 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{})
newNibiruApp.OracleKeeper.SetPrice(ctx, common.Pair_BTC_NUSD.String(), sdk.NewDec(20000))
// newNibiruApp.OracleKeeper.SetPrice(ctx, common.Pair_NIBI_NUSD.String(), sdk.NewDec(10))
newNibiruApp.OracleKeeper.SetPrice(ctx, "xxx:yyy", sdk.NewDec(20000))
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*common.Precision)) // min deposit of 1 NIBI
testGenState[govtypes.ModuleName] = codec.MustMarshalJSON(&govGenState)
return testGenState
}