-
Notifications
You must be signed in to change notification settings - Fork 40
/
testutils.go
101 lines (89 loc) · 3.24 KB
/
testutils.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
package testutils
import (
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/bnb-chain/node/common"
"github.com/bnb-chain/node/common/types"
)
func SetupMultiStoreForUnitTest() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
_, ms, capKey, capKey2, _ := SetupMultiStoreWithDBForUnitTest()
return ms, capKey, capKey2
}
func SetupThreeMultiStoreForUnitTest() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey, *sdk.KVStoreKey) {
_, ms, capKey, capKey2, capKey3 := SetupMultiStoreWithDBForUnitTest()
return ms, capKey, capKey2, capKey3
}
func SetupMultiStoreWithDBForUnitTest() (dbm.DB, sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey, *sdk.KVStoreKey) {
db := dbm.NewMemDB()
capKey := sdk.NewKVStoreKey("capkey")
capKey2 := sdk.NewKVStoreKey("capkey2")
capKey3 := sdk.NewKVStoreKey("capkey3")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(capKey3, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(common.PairStoreKey, sdk.StoreTypeIAVL, db)
err := ms.LoadLatestVersion()
if err != nil {
panic(err)
}
return db, ms, capKey, capKey2, capKey3
}
// coins to more than cover the fee
func NewNativeTokens(amount int64) sdk.Coins {
return sdk.Coins{
sdk.NewCoin(types.NativeTokenSymbol, amount),
}
}
// generate a priv key and return it with its address
func PrivAndAddr() (crypto.PrivKey, sdk.AccAddress) {
priv := secp256k1.GenPrivKey()
addr := sdk.AccAddress(priv.PubKey().Address())
return priv, addr
}
func NewAccount(ctx sdk.Context, am auth.AccountKeeper, free int64) (crypto.PrivKey, sdk.Account) {
privKey, addr := PrivAndAddr()
acc := am.NewAccountWithAddress(ctx, addr)
_ = acc.SetCoins(NewNativeTokens(free))
am.SetAccount(ctx, acc)
return privKey, acc
}
func NewNamedAccount(ctx sdk.Context, am auth.AccountKeeper, free int64) (crypto.PrivKey, types.NamedAccount) {
privKey, addr := PrivAndAddr()
acc := am.NewAccountWithAddress(ctx, addr)
_ = acc.SetCoins(NewNativeTokens(free))
baseAcc := auth.BaseAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
PubKey: acc.GetPubKey(),
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
}
appAcc := &types.AppAccount{
BaseAccount: baseAcc,
Name: "",
Flags: 0x0,
}
am.SetAccount(ctx, appAcc)
return privKey, appAcc
}
func NewAccountForPub(ctx sdk.Context, am auth.AccountKeeper, free, locked, freeze int64, symbol string) (crypto.PrivKey, sdk.Account) {
privKey, addr := PrivAndAddr()
acc := am.NewAccountWithAddress(ctx, addr)
coins := NewNativeTokens(free)
coins = append(coins, sdk.NewCoin(symbol, free))
_ = acc.SetCoins(coins)
appAcc := acc.(*types.AppAccount)
lockedCoins := NewNativeTokens(locked)
lockedCoins = append(lockedCoins, sdk.NewCoin(symbol, locked))
appAcc.SetLockedCoins(lockedCoins)
freezeCoins := NewNativeTokens(freeze)
freezeCoins = append(freezeCoins, sdk.NewCoin(symbol, freeze))
appAcc.SetFrozenCoins(freezeCoins)
am.SetAccount(ctx, acc)
return privKey, acc
}