forked from lumtis/poa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.go
87 lines (70 loc) · 2.39 KB
/
test_utils.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
package poa
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/ltacker/poa/keeper"
"github.com/ltacker/poa/types"
)
// This package contains various mocks for testing purpose
// Context and keeper used for mocking purpose
func MockContext() (sdk.Context, keeper.Keeper) {
// Store keys
keys := sdk.NewKVStoreKeys(types.StoreKey, params.StoreKey)
tKeys := sdk.NewTransientStoreKeys(params.TStoreKey)
cdc := codec.New()
// Create the params keeper
paramsKeeper := params.NewKeeper(cdc, keys[params.StoreKey], tKeys[params.TStoreKey])
// Create a poa keeper
poaKeeper := keeper.NewKeeper(cdc, keys[types.StoreKey], paramsKeeper.Subspace(types.ModuleName))
// Create multiStore in memory
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
// Mount stores
cms.MountStoreWithDB(keys[types.StoreKey], sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(keys[params.StoreKey], sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(tKeys[params.TStoreKey], sdk.StoreTypeTransient, db)
cms.LoadLatestVersion()
// Create context
ctx := sdk.NewContext(cms, abci.Header{}, false, log.NewNopLogger())
return ctx, poaKeeper
}
// Create a validator for test
func MockValidator() (types.Validator, string) {
// Junk description
validatorDescription := types.Description{
Moniker: "Moniker",
Identity: "Identity",
Website: "Website",
SecurityContact: "SecurityContact",
Details: "Details",
}
// Generate operator address
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
operatorAddress := sdk.ValAddress(addr)
// Generate a consPubKey
pk = ed25519.GenPrivKey().PubKey()
consPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pk)
if err != nil {
panic(fmt.Sprintf("Cannot create a consPubKey: %v", err))
}
validator := types.Validator{
OperatorAddress: operatorAddress,
ConsensusPubkey: consPubKey,
Description: validatorDescription,
}
return validator, consPubKey
}
// Create an validator address
func MockValAddress() sdk.ValAddress {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.ValAddress(addr)
}