/
test_helper.go
118 lines (100 loc) · 3.11 KB
/
test_helper.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
package testutil
import (
"encoding/json"
"github.com/CosmWasm/wasmd/x/wasm"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/go-bip39"
evmhd "github.com/evmos/ethermint/crypto/hd"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
xapp "github.com/xpladev/xpla/app"
"github.com/xpladev/xpla/app/helpers"
)
const (
DefaultTestGenTxGas = 1000000
TestChainId = "cube_47-5"
)
func Setup(isCheckTx bool, invCheckPeriod uint) *xapp.XplaApp {
app, genesisState := setup(!isCheckTx, invCheckPeriod)
if !isCheckTx {
// InitChain 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{},
ChainId: TestChainId,
ConsensusParams: helpers.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}
return app
}
func setup(withGenesis bool, invCheckPeriod uint) (*xapp.XplaApp, xapp.GenesisState) {
db := dbm.NewMemDB()
encCdc := xapp.MakeTestEncodingConfig()
app := xapp.NewXplaApp(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
xapp.DefaultNodeHome,
invCheckPeriod,
encCdc,
xapp.GetEnabledProposals(),
helpers.EmptyAppOptions{},
[]wasm.Option{},
)
if withGenesis {
return app, xapp.NewDefaultGenesisState()
}
return app, xapp.GenesisState{}
}
// FundAccount is a utility function that funds an account by minting and
// sending the coins to the address. This should be used for testing purposes
// only!
//
// TODO: Instead of using the mint module account, which has the
// permission of minting, create a "faucet" account. (@fdymylja)
func FundAccount(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil {
return err
}
return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
}
func NewTestMnemonic(entropy []byte) (string, error) {
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return "", err
}
return mnemonic, nil
}
func NewTestEthSecpPrivKey(mnemonic string) (cryptotypes.PrivKey, error) {
algo := evmhd.EthSecp256k1
derivedPri, err := algo.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, sdk.GetConfig().GetFullBIP44Path())
if err != nil {
return nil, err
}
privateKey := algo.Generate()(derivedPri)
return privateKey, nil
}
func NewTestSecpPrivKey(mnemonic string) (cryptotypes.PrivKey, error) {
algo := hd.Secp256k1
derivedPri, err := algo.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, sdk.GetConfig().GetFullBIP44Path())
if err != nil {
return nil, err
}
privateKey := algo.Generate()(derivedPri)
return privateKey, nil
}