forked from lino-network/lino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.go
83 lines (69 loc) · 2.96 KB
/
test_util.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
package vote
import (
"strconv"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/store"
"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"
"github.com/lino-network/lino/x/global"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
acc "github.com/lino-network/lino/x/account"
rep "github.com/lino-network/lino/x/reputation"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
)
// Construct some global addrs and txs for tests.
var (
testAccountKVStoreKey = sdk.NewKVStoreKey("account")
testVoteKVStoreKey = sdk.NewKVStoreKey("vote")
testGlobalKVStoreKey = sdk.NewKVStoreKey("global")
testParamKVStoreKey = sdk.NewKVStoreKey("param")
testRepKVStoreKey = sdk.NewKVStoreKey("reputation")
c100 = types.NewCoinFromInt64(100 * types.Decimals)
c500 = types.NewCoinFromInt64(500 * types.Decimals)
)
func initGlobalManager(ctx sdk.Context, gm global.GlobalManager) error {
return gm.InitGlobalManager(ctx, types.NewCoinFromInt64(10000*types.Decimals))
}
func setupTest(t *testing.T, height int64) (sdk.Context,
acc.AccountManager, VoteManager, global.GlobalManager, rep.ReputationManager) {
ctx := getContext(height)
ph := param.NewParamHolder(testParamKVStoreKey)
ph.InitParam(ctx)
accManager := acc.NewAccountManager(testAccountKVStoreKey, ph)
voteManager := NewVoteManager(testVoteKVStoreKey, ph)
globalManager := global.NewGlobalManager(testGlobalKVStoreKey, ph)
repManager := rep.NewReputationManager(testRepKVStoreKey, ph)
cdc := globalManager.WireCodec()
cdc.RegisterInterface((*types.Event)(nil), nil)
cdc.RegisterConcrete(acc.ReturnCoinEvent{}, "1", nil)
err := initGlobalManager(ctx, globalManager)
assert.Nil(t, err)
return ctx, accManager, voteManager, globalManager, repManager
}
func getContext(height int64) sdk.Context {
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(testAccountKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testVoteKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testGlobalKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testParamKVStoreKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(testRepKVStoreKey, sdk.StoreTypeIAVL, db)
ms.LoadLatestVersion()
return sdk.NewContext(ms, abci.Header{Height: height, Time: time.Unix(0, 0)}, false, log.NewNopLogger())
}
// helper function to create an account for testing purpose
func createTestAccount(ctx sdk.Context, am acc.AccountManager, username string, initCoin types.Coin) types.AccountKey {
am.CreateAccount(ctx, "referrer", types.AccountKey(username),
secp256k1.GenPrivKey().PubKey(), secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(), initCoin)
return types.AccountKey(username)
}
func coinToString(coin types.Coin) string {
coinInInt64, _ := coin.ToInt64()
return strconv.FormatInt(coinInInt64/types.Decimals, 10)
}