-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.go
167 lines (146 loc) · 6.04 KB
/
test_common.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package keeper
import (
"bytes"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/peggy/x/oracle/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
cmtypes "github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingKeeperLib "github.com/cosmos/cosmos-sdk/x/staking/keeper"
abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
tmtypes "github.com/tendermint/tendermint/types"
)
const (
TestID = "oracleID"
AlternateTestID = "altOracleID"
TestString = "{value: 5}"
AlternateTestString = "{value: 7}"
AnotherAlternateTestString = "{value: 9}"
)
// CreateTestKeepers greates an OracleKeeper, AccountKeeper and Context to be used for test input
func CreateTestKeepers(t *testing.T, consensusNeeded float64, validatorPowers []int64) (sdk.Context, auth.AccountKeeper, Keeper, bank.Keeper, []sdk.ValAddress) {
keyOracle := sdk.NewKVStoreKey(types.StoreKey)
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
keyParams := sdk.NewKVStoreKey(params.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
tkeyStaking := sdk.NewTransientStoreKey(staking.TStoreKey)
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(tkeyStaking, sdk.StoreTypeTransient, nil)
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyOracle, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "testchainid"}, false, log.NewNopLogger())
ctx = ctx.WithConsensusParams(
&abci.ConsensusParams{
Validator: &abci.ValidatorParams{
PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519},
},
},
)
cdc := MakeTestCodec()
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
accountKeeper := auth.NewAccountKeeper(
cdc, // amino codec
keyAcc, // target store
pk.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount, // prototype
)
bankKeeper := bank.NewBaseKeeper(
accountKeeper,
pk.Subspace(bank.DefaultParamspace),
bank.DefaultCodespace,
)
stakingKeeper := staking.NewKeeper(cdc, keyStaking, tkeyStaking, bankKeeper, pk.Subspace(staking.DefaultParamspace), staking.DefaultCodespace)
stakingKeeper.SetPool(ctx, staking.InitialPool())
stakingKeeper.SetParams(ctx, staking.DefaultParams())
keeper := NewKeeper(stakingKeeper, keyOracle, cdc, types.DefaultCodespace, consensusNeeded)
//construct the validators
numValidators := len(validatorPowers)
validators := make([]staking.Validator, numValidators)
accountAddresses, valAddresses := CreateTestAddrs(len(validatorPowers))
publicKeys := createTestPubKeys(len(validatorPowers))
// create the validators addresses desired and fill them with the expected amount of coins
for i, power := range validatorPowers {
coins := cmtypes.TokensFromTendermintPower(power)
pool := stakingKeeper.GetPool(ctx)
err := error(nil)
_, _, err = bankKeeper.AddCoins(ctx, accountAddresses[i], sdk.Coins{
{stakingKeeper.BondDenom(ctx), coins},
})
require.Nil(t, err)
pool.NotBondedTokens = pool.NotBondedTokens.Add(coins)
stakingKeeper.SetPool(ctx, pool)
}
pool := stakingKeeper.GetPool(ctx)
for i, power := range validatorPowers {
validators[i] = staking.NewValidator(valAddresses[i], publicKeys[i], staking.Description{})
validators[i].Status = sdk.Bonded
validators[i].Tokens = sdk.ZeroInt()
tokens := sdk.TokensFromTendermintPower(power)
validators[i], pool, _ = validators[i].AddTokensFromDel(pool, tokens)
stakingKeeper.SetPool(ctx, pool)
stakingKeeperLib.TestingUpdateValidator(stakingKeeper, ctx, validators[i], true)
}
return ctx, accountKeeper, keeper, bankKeeper, valAddresses
}
// nolint: unparam
func CreateTestAddrs(numAddrs int) ([]sdk.AccAddress, []sdk.ValAddress) {
var addresses []sdk.AccAddress
var valAddresses []sdk.ValAddress
var buffer bytes.Buffer
// start at 100 so we can make up to 999 test addresses with valid test addresses
for i := 100; i < (numAddrs + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string
buffer.WriteString(numString) //adding on final two digits to make addresses unique
res, _ := sdk.AccAddressFromHex(buffer.String())
bech := res.String()
address := stakingKeeperLib.TestAddr(buffer.String(), bech)
valAddress := sdk.ValAddress(address)
addresses = append(addresses, address)
valAddresses = append(valAddresses, valAddress)
buffer.Reset()
}
return addresses, valAddresses
}
// nolint: unparam
func createTestPubKeys(numPubKeys int) []crypto.PubKey {
var publicKeys []crypto.PubKey
var buffer bytes.Buffer
//start at 10 to avoid changing 1 to 01, 2 to 02, etc
for i := 100; i < (numPubKeys + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") //base pubkey string
buffer.WriteString(numString) //adding on final two digits to make pubkeys unique
publicKeys = append(publicKeys, stakingKeeperLib.NewPubKey(buffer.String()))
buffer.Reset()
}
return publicKeys
}
// MakeTestCodec creates a codec used only for testing
func MakeTestCodec() *codec.Codec {
var cdc = codec.New()
// Register Msgs
auth.RegisterCodec(cdc)
bank.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
staking.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}