/
test_helper.go
132 lines (109 loc) · 3.85 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package vm
import (
"os"
"time"
"github.com/tendermint/tendermint/crypto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
bam "github.com/netcloth/netcloth-chain/baseapp"
"github.com/netcloth/netcloth-chain/codec"
"github.com/netcloth/netcloth-chain/modules/auth"
"github.com/netcloth/netcloth-chain/modules/cipal"
distr "github.com/netcloth/netcloth-chain/modules/distribution"
"github.com/netcloth/netcloth-chain/modules/gov"
"github.com/netcloth/netcloth-chain/modules/ipal"
"github.com/netcloth/netcloth-chain/modules/mint"
"github.com/netcloth/netcloth-chain/modules/params"
"github.com/netcloth/netcloth-chain/modules/slashing"
"github.com/netcloth/netcloth-chain/modules/staking"
"github.com/netcloth/netcloth-chain/modules/supply"
"github.com/netcloth/netcloth-chain/modules/vm/types"
"github.com/netcloth/netcloth-chain/store"
sdk "github.com/netcloth/netcloth-chain/types"
)
var (
maccPerms = map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: {supply.Minter},
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
gov.ModuleName: {supply.Burner},
}
)
func ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[supply.NewModuleAddress(acc).String()] = true
}
return modAccAddrs
}
func KeyTestPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
key := secp256k1.GenPrivKey()
pub := key.PubKey()
addr := sdk.AccAddress(pub.Address())
return key, pub, addr
}
func setupTest() (vmKeeper Keeper, ctx sdk.Context) {
cdc := codec.New()
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
keys := sdk.NewKVStoreKeys(
bam.MainStoreKey,
auth.StoreKey,
auth.FeeKey,
staking.StoreKey,
supply.StoreKey,
mint.StoreKey,
distr.StoreKey,
slashing.StoreKey,
gov.StoreKey,
params.StoreKey,
cipal.StoreKey,
ipal.StoreKey,
StoreKey,
CodeKey,
)
tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, staking.TStoreKey, params.TStoreKey)
paramsKeeper := params.NewKeeper(cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace)
authSubspace := paramsKeeper.Subspace(auth.DefaultParamspace)
vmSubspace := paramsKeeper.Subspace(DefaultParamspace)
// add keepers
accountKeeper := auth.NewAccountKeeper(cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount)
vmKeeper = NewKeeper(
cdc,
keys[StoreKey],
keys[CodeKey],
DefaultCodespace,
vmSubspace,
accountKeeper)
for _, key := range keys {
ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil) // db nil
}
for _, key := range tkeys {
ms.MountStoreWithDB(key, sdk.StoreTypeTransient, nil) // db nil
}
ms.LoadLatestVersion()
ctx = sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout))
vmKeeper.SetParams(ctx, types.DefaultParams())
return
}
func GetTestAccount() auth.BaseAccount {
_, pubKey, addr := KeyTestPubAddr()
acc := auth.NewBaseAccountWithAddress(addr)
acc.SetPubKey(pubKey)
acc.SetSequence(0)
acc.SetCoins(sdk.NewCoins(sdk.NewCoin(sdk.NativeTokenName, sdk.NewInt(10000000000))))
return acc
}
func newEVM() *EVM {
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
keyParams := sdk.NewKVStoreKey(params.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
paramsKeeper := params.NewKeeper(types.ModuleCdc, keyParams, tkeyParams, params.DefaultCodespace)
accountKeeper := auth.NewAccountKeeper(types.ModuleCdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
keys := sdk.NewKVStoreKeys(auth.StoreKey, StoreKey, CodeKey)
return NewEVM(Context{}, NewCommitStateDB(accountKeeper, keys[StoreKey], keys[CodeKey]), Config{})
}