-
Notifications
You must be signed in to change notification settings - Fork 182
/
test_common.go
236 lines (199 loc) · 6.96 KB
/
test_common.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package stream
import (
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/codec"
appCfg "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/cosmos/cosmos-sdk/x/supply/exported"
"github.com/okex/exchain/x/common"
"github.com/okex/exchain/x/common/monitor"
"github.com/okex/exchain/x/dex"
"github.com/okex/exchain/x/order"
"github.com/okex/exchain/x/order/keeper"
ordertypes "github.com/okex/exchain/x/order/types"
stakingtypes "github.com/okex/exchain/x/staking/types"
"github.com/okex/exchain/x/token"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
)
type MockApp struct {
*mock.App
keyOrder *sdk.KVStoreKey
keyToken *sdk.KVStoreKey
keyFreeze *sdk.KVStoreKey
keyLock *sdk.KVStoreKey
keyDex *sdk.KVStoreKey
keyTokenPair *sdk.KVStoreKey
keySupply *sdk.KVStoreKey
BankKeeper bank.Keeper
OrderKeeper keeper.Keeper
DexKeeper dex.Keeper
TokenKeeper token.Keeper
supplyKeeper supply.Keeper
streamKeeper Keeper
}
func registerCodec(cdc *codec.Codec) {
supply.RegisterCodec(cdc)
}
func GetMockApp(t *testing.T, numGenAccs int, cfg *appCfg.Config) (mockApp *MockApp, addrKeysSlice mock.AddrKeysSlice) {
return getMockAppWithBalance(t, numGenAccs, 100, cfg)
}
// initialize the mock application for this module
func getMockAppWithBalance(t *testing.T, numGenAccs int, balance int64, cfg *appCfg.Config) (mockApp *MockApp, addrKeysSlice mock.AddrKeysSlice) {
mapp := mock.NewApp()
registerCodec(mapp.Cdc)
mockApp = &MockApp{
App: mapp,
keyOrder: sdk.NewKVStoreKey(ordertypes.OrderStoreKey),
keyToken: sdk.NewKVStoreKey("token"),
keyFreeze: sdk.NewKVStoreKey("freeze"),
keyLock: sdk.NewKVStoreKey("lock"),
keyDex: sdk.NewKVStoreKey(dex.StoreKey),
keyTokenPair: sdk.NewKVStoreKey(dex.TokenPairStoreKey),
keySupply: sdk.NewKVStoreKey(supply.StoreKey),
}
feeCollector := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
blacklistedAddrs := make(map[string]bool)
blacklistedAddrs[feeCollector.String()] = true
mockApp.BankKeeper = bank.NewBaseKeeper(mockApp.AccountKeeper, mockApp.ParamsKeeper.Subspace(bank.DefaultParamspace),
blacklistedAddrs)
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
}
mockApp.supplyKeeper = supply.NewKeeper(mockApp.Cdc, mockApp.keySupply, mockApp.AccountKeeper,
mockApp.BankKeeper, maccPerms)
mockApp.TokenKeeper = token.NewKeeper(
mockApp.BankKeeper,
mockApp.ParamsKeeper.Subspace(token.DefaultParamspace),
auth.FeeCollectorName,
mockApp.supplyKeeper,
mockApp.keyToken,
mockApp.keyLock,
mockApp.Cdc,
true, mockApp.AccountKeeper)
mockApp.DexKeeper = dex.NewKeeper(
auth.FeeCollectorName,
mockApp.supplyKeeper,
mockApp.ParamsKeeper.Subspace(dex.DefaultParamspace),
mockApp.TokenKeeper,
nil,
mockApp.BankKeeper,
mockApp.keyDex,
mockApp.keyTokenPair,
mockApp.Cdc)
mockApp.OrderKeeper = keeper.NewKeeper(
mockApp.TokenKeeper,
mockApp.supplyKeeper,
mockApp.DexKeeper,
mockApp.ParamsKeeper.Subspace(ordertypes.DefaultParamspace),
auth.FeeCollectorName,
mockApp.keyOrder,
mockApp.Cdc,
true,
monitor.NopOrderMetrics())
mockApp.streamKeeper = NewKeeper(mockApp.OrderKeeper, mockApp.TokenKeeper, &mockApp.DexKeeper, &mockApp.AccountKeeper,
nil, nil, mockApp.Cdc, mockApp.Logger(), cfg, monitor.NopStreamMetrics())
mockApp.Router().AddRoute(ordertypes.RouterKey, order.NewOrderHandler(mockApp.OrderKeeper))
mockApp.QueryRouter().AddRoute(order.QuerierRoute, keeper.NewQuerier(mockApp.OrderKeeper))
mockApp.SetBeginBlocker(getBeginBlocker(mockApp))
mockApp.SetEndBlocker(getEndBlocker(mockApp))
mockApp.SetInitChainer(getInitChainer(mockApp.App, mockApp.supplyKeeper,
[]exported.ModuleAccountI{feeCollector}))
coins, err := sdk.ParseDecCoins(fmt.Sprintf("%d%s,%d%s",
balance, common.NativeToken, balance, common.TestToken))
if err != nil {
panic(err)
}
keysSlice, genAccs := CreateGenAccounts(numGenAccs, coins)
addrKeysSlice = keysSlice
mockApp.SetAnteHandler(nil)
app := mockApp
mockApp.MountStores(
app.keyOrder,
app.keyToken,
app.keyDex,
app.keyTokenPair,
app.keyFreeze,
app.keyLock,
app.keySupply,
)
require.NoError(t, mockApp.CompleteSetup(mockApp.keyOrder))
mock.SetGenesis(mockApp.App, genAccs)
return mockApp, addrKeysSlice
}
func getBeginBlocker(mapp *MockApp) sdk.BeginBlocker {
return func(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
order.BeginBlocker(ctx, mapp.OrderKeeper)
return abci.ResponseBeginBlock{}
}
}
func getEndBlocker(mapp *MockApp) sdk.EndBlocker {
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
order.EndBlocker(ctx, mapp.OrderKeeper)
EndBlocker(ctx, mapp.streamKeeper)
return abci.ResponseEndBlock{}
}
}
func getInitChainer(mapp *mock.App, supplyKeeper stakingtypes.SupplyKeeper,
blacklistedAddrs []exported.ModuleAccountI) sdk.InitChainer {
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
mapp.InitChainer(ctx, req)
// set module accounts
for _, macc := range blacklistedAddrs {
supplyKeeper.SetModuleAccount(ctx, macc)
}
return abci.ResponseInitChain{}
}
}
func ProduceOrderTxs(app *MockApp, ctx sdk.Context, numToGenerate int, addrKeys mock.AddrKeys,
orderMsg *ordertypes.MsgNewOrders) []auth.StdTx {
txs := make([]auth.StdTx, numToGenerate)
orderMsg.Sender = addrKeys.Address
for i := 0; i < numToGenerate; i++ {
txs[i] = buildTx(app, ctx, addrKeys, *orderMsg)
}
return txs
}
func buildTx(app *MockApp, ctx sdk.Context, addrKeys mock.AddrKeys, msg sdk.Msg) auth.StdTx {
accs := app.AccountKeeper.GetAccount(ctx, addrKeys.Address)
accNum := accs.GetAccountNumber()
seqNum := accs.GetSequence()
tx := mock.GenTx([]sdk.Msg{msg}, []uint64{accNum}, []uint64{seqNum}, addrKeys.PrivKey)
_, _, err := app.Check(tx)
if err != nil {
panic(fmt.Sprintf("something wrong in checking transaction: %v", err))
}
return tx
}
func MockApplyBlock(app *MockApp, blockHeight int64, txs []auth.StdTx) {
app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: blockHeight}})
newCtx := app.NewContext(false, abci.Header{})
param := ordertypes.DefaultParams()
app.OrderKeeper.SetParams(newCtx, ¶m)
for _, tx := range txs {
app.Deliver(tx)
}
app.EndBlock(abci.RequestEndBlock{Height: blockHeight})
app.Commit()
}
func CreateGenAccounts(numAccs int, genCoins sdk.Coins) (addrKeysSlice mock.AddrKeysSlice, genAccs []auth.Account) {
for i := 0; i < numAccs; i++ {
privKey := secp256k1.GenPrivKey()
pubKey := privKey.PubKey()
addr := sdk.AccAddress(pubKey.Address())
addrKeys := mock.NewAddrKeys(addr, pubKey, privKey)
account := &auth.BaseAccount{
Address: addr,
Coins: genCoins,
}
genAccs = append(genAccs, account)
addrKeysSlice = append(addrKeysSlice, addrKeys)
}
return
}