/
simapp.go
385 lines (328 loc) · 12.3 KB
/
simapp.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package simapp
import (
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/json"
"testing"
"time"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdksimapp "github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingKeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/playerfury/furyx/app"
"github.com/playerfury/furyx/app/params"
"github.com/playerfury/furyx/utils"
dvmtypes "github.com/playerfury/furyx/x/dvm/types"
mintmoduletypes "github.com/playerfury/furyx/x/mint/types"
strategicreservetypes "github.com/playerfury/furyx/x/strategicreserve/types"
"github.com/spf13/cast"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)
// TestApp is used as a container of the furyx app
type TestApp struct {
app.FuryApp
}
// Options defines options related to simapp initialization
type Options struct {
CreateGenesisValidators bool
}
// setup initializes new test app instance
func setup(withGenesis bool, invCheckPeriod uint) (*TestApp, app.GenesisState) {
db := tmdb.NewMemDB()
encCdc := app.MakeEncodingConfig()
appInstance := app.NewFuryApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, "", invCheckPeriod, encCdc,
sdksimapp.EmptyAppOptions{})
if withGenesis {
return &TestApp{FuryApp: *appInstance}, app.NewDefaultGenesisState()
}
return &TestApp{FuryApp: *appInstance}, app.GenesisState{}
}
// Setup initializes genesis the same as simapp
func Setup(isCheckTx bool) *TestApp {
app, genesisState := setup(!isCheckTx, 5)
if !isCheckTx {
// init chain must be called to stop deliverState from being nil
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
return app
}
// SetupWithGenesisAccounts sets up the genesis accounts for testing
func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, options Options, balances ...banktypes.Balance) *TestApp {
appInstance, genesisState := setup(true, 0)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = appInstance.AppCodec().MustMarshalJSON(authGenesis)
var validatorUpdates []abci.ValidatorUpdate
if options.CreateGenesisValidators {
var moduleBalance banktypes.Balance
var stakingGenesis *stakingtypes.GenesisState
stakingGenesis, validatorUpdates, moduleBalance = stakingDefaultTestGenesis(appInstance)
genesisState[stakingtypes.ModuleName] = appInstance.AppCodec().MustMarshalJSON(stakingGenesis)
balances = append(balances, moduleBalance)
}
totalSupply := sdk.NewCoins()
for _, b := range balances {
totalSupply = totalSupply.Add(b.Coins...)
}
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
genesisState[banktypes.ModuleName] = appInstance.AppCodec().MustMarshalJSON(bankGenesis)
{
publicKeys := GenerateDvmPublicKeys(5)
dvmGenesisState := &dvmtypes.GenesisState{
KeyVault: dvmtypes.KeyVault{
PublicKeys: publicKeys,
},
}
genesisState[dvmtypes.ModuleName] = appInstance.AppCodec().MustMarshalJSON(dvmGenesisState)
}
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
appInstance.InitChain(
abci.RequestInitChain{
Validators: validatorUpdates,
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
appInstance.Commit()
appInstance.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: appInstance.LastBlockHeight() + 1}})
return appInstance
}
// GetTestObjects gets the test objects and ingredients for testing phase start with default options
func GetTestObjects() (*TestApp, sdk.Context, error) {
// return
return GetTestObjectsWithOptions(Options{
CreateGenesisValidators: true,
})
}
// GetTestObjectsWithOptions gets the test objects and ingredients for testing phase start with custom options
func GetTestObjectsWithOptions(options Options) (*TestApp, sdk.Context, error) {
generateSimappUsers()
// Initialize test app by genesis account
genAccs := generateSimappGenesisAccounts()
// Create testapp instance
balances := generateSimappUserBalances()
tApp := SetupWithGenesisAccounts(genAccs, options, balances...)
// Create the context
ctx := tApp.NewContext(true, tmproto.Header{Height: tApp.LastBlockHeight()})
setMinterParams(tApp, ctx)
if err := generateSimappAccountCoins(&ctx, tApp); err != nil {
return &TestApp{}, sdk.Context{}, err
}
err := SetModuleAccountCoins(&ctx, tApp.BankKeeper, strategicreservetypes.SRPoolName, 150000000000000)
if err != nil {
return &TestApp{}, sdk.Context{}, err
}
err = SetModuleAccountCoins(&ctx, tApp.BankKeeper, strategicreservetypes.BetReserveName, 200000)
if err != nil {
return &TestApp{}, sdk.Context{}, err
}
return tApp, ctx, nil
}
func setMinterParams(tApp *TestApp, ctx sdk.Context) {
tApp.MintKeeper.SetParams(ctx, mintmoduletypes.DefaultParams())
tApp.MintKeeper.SetMinter(ctx, mintmoduletypes.DefaultInitialMinter())
}
func generateSimappUsers() {
createIncrementalAccounts(10)
for i := 0; i < 10; i++ {
prvKey := secp256k1.GenPrivKey()
TestParamUsers[usernamePrefix+cast.ToString(i)] = TestUser{
PrvKey: prvKey,
Address: sdk.AccAddress(prvKey.PubKey().Address()),
Balance: 1000000000,
}
}
}
func generateSimappUserBalances() (balances []banktypes.Balance) {
genTokens := sdk.TokensFromConsensusPower(1, sdk.DefaultPowerReduction)
genCoin := sdk.NewCoin(params.DefaultBondDenom, genTokens)
sdkgenCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000000000))
for _, v := range TestParamUsers {
balances = append(balances, banktypes.Balance{
Address: v.Address.String(),
Coins: sdk.Coins{sdkgenCoin, genCoin},
})
}
return balances
}
func generateSimappGenesisAccounts() (genAccs []authtypes.GenesisAccount) {
for _, v := range TestParamUsers {
genAccs = append(genAccs, &authtypes.BaseAccount{Address: v.Address.String()})
}
return genAccs
}
func generateSimappAccountCoins(ctx *sdk.Context, tApp *TestApp) error {
for _, v := range TestParamUsers {
if err := SetAccountCoins(ctx, tApp.BankKeeper, v.Address, v.Balance); err != nil {
return err
}
}
return nil
}
// SetAccountCoins sets the balance of accounts for testing
func SetAccountCoins(ctx *sdk.Context, k bankkeeper.Keeper, addr sdk.AccAddress, amount int64) error {
coin := sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, sdk.NewInt(amount)))
err := k.MintCoins(*ctx, mintmoduletypes.ModuleName, coin)
if err != nil {
return err
}
err = k.SendCoinsFromModuleToAccount(*ctx, mintmoduletypes.ModuleName, addr, coin)
if err != nil {
return err
}
return nil
}
// SetModuleAccountCoins sets the balance of accounts for testing
func SetModuleAccountCoins(ctx *sdk.Context, k bankkeeper.Keeper, moduleName string, amount int64) error {
coin := sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, sdk.NewInt(amount)))
err := k.MintCoins(*ctx, mintmoduletypes.ModuleName, coin)
if err != nil {
return err
}
err = k.SendCoinsFromModuleToModule(*ctx, mintmoduletypes.ModuleName, moduleName, coin)
if err != nil {
return err
}
return nil
}
// DefaultConsensusParams parameters for tendermint consensus
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,
},
},
}
func stakingDefaultTestGenesis(tApp *TestApp) (*stakingtypes.GenesisState, []abci.ValidatorUpdate, banktypes.Balance) {
defaultParams := stakingtypes.DefaultParams()
defaultParams.BondDenom = params.DefaultBondDenom
addr1 := TestParamUsers["user1"].Address
addr2 := TestParamUsers["user2"].Address
p1 := int64(8)
p2 := int64(2)
pks := sdksimapp.CreateTestPubKeys(2)
valConsPk1 := pks[0]
valConsPk2 := pks[1]
valPower1 := sdk.TokensFromConsensusPower(p1, sdk.DefaultPowerReduction)
valPower2 := sdk.TokensFromConsensusPower(p2, sdk.DefaultPowerReduction)
var validators []stakingtypes.Validator
var delegations []stakingtypes.Delegation
pk0, err := codectypes.NewAnyWithValue(valConsPk1)
if err != nil {
panic(err)
}
pk1, err := codectypes.NewAnyWithValue(valConsPk2)
if err != nil {
panic(err)
}
// initialize the validators
bondedVal1 := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(addr1).String(),
ConsensusPubkey: pk0,
Status: stakingtypes.Bonded,
Tokens: valPower1,
DelegatorShares: valPower1.ToDec(),
Description: stakingtypes.NewDescription("hoop", "", "", "", ""),
Commission: stakingtypes.NewCommission(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)),
}
bondedVal2 := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(addr2).String(),
ConsensusPubkey: pk1,
Status: stakingtypes.Bonded,
Tokens: valPower2,
DelegatorShares: valPower2.ToDec(),
Description: stakingtypes.NewDescription("bloop", "", "", "", ""),
Commission: stakingtypes.NewCommission(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)),
}
// append new bonded validators to the list
validators = append(validators, bondedVal1, bondedVal2)
// mint coins in the bonded pool representing the validators coins
var valudatorUpdates []abci.ValidatorUpdate
valudatorUpdates = append(valudatorUpdates, bondedVal1.ABCIValidatorUpdate(sdk.DefaultPowerReduction))
delegations = append(delegations, stakingtypes.Delegation{
DelegatorAddress: addr1.String(),
ValidatorAddress: bondedVal1.OperatorAddress,
Shares: bondedVal1.DelegatorShares,
})
valudatorUpdates = append(valudatorUpdates, bondedVal2.ABCIValidatorUpdate(sdk.DefaultPowerReduction))
delegations = append(delegations, stakingtypes.Delegation{
DelegatorAddress: addr2.String(),
ValidatorAddress: bondedVal2.OperatorAddress,
Shares: bondedVal2.DelegatorShares,
})
moduleAddress := tApp.AccountKeeper.GetModuleAddress(stakingtypes.BondedPoolName)
moduleBalance := banktypes.Balance{
Address: moduleAddress.String(),
Coins: sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, valPower1.Add(valPower2))),
}
TestParamValidatorAddresses["val1"] = TestValidator{
PubKey: valConsPk1,
Address: bondedVal1.GetOperator(),
ConsAddress: sdk.ConsAddress(valConsPk1.Address()),
Power: valPower1,
}
TestParamValidatorAddresses["val2"] = TestValidator{
PubKey: valConsPk2,
Address: bondedVal2.GetOperator(),
ConsAddress: sdk.ConsAddress(valConsPk2.Address()),
Power: valPower2,
}
genesisState := stakingtypes.NewGenesisState(defaultParams, validators, delegations)
return genesisState, valudatorUpdates, moduleBalance
}
// NewStakingHelper creates staking Handler wrapper for tests
func NewStakingHelper(t *testing.T, ctx sdk.Context, k stakingKeeper.Keeper) *teststaking.Helper {
helper := teststaking.NewHelper(t, ctx, k)
helper.Commission = validatorDefaultCommission()
helper.Denom = params.DefaultBondDenom
return helper
}
func validatorDefaultCommission() stakingtypes.CommissionRates {
return stakingtypes.NewCommissionRates(sdk.MustNewDecFromStr("0.1"), sdk.MustNewDecFromStr("0.2"), sdk.MustNewDecFromStr("0.01"))
}
func GenerateDvmPublicKeys(n int) (pubKeys []string) {
TestDVMPublicKeys = make([]ed25519.PublicKey, n)
TestDVMPrivateKeys = make([]ed25519.PrivateKey, n)
for i := 0; i < n; i++ {
TestDVMPublicKeys[i], TestDVMPrivateKeys[i], _ = ed25519.GenerateKey(rand.Reader)
bs, err := x509.MarshalPKIXPublicKey(TestDVMPublicKeys[i])
if err != nil {
panic(err)
}
pubKeys = append(pubKeys, string(utils.NewPubKeyMemory(bs)))
}
return pubKeys
}