/
test_common.go
214 lines (181 loc) · 7.32 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package keeper
import (
"bytes"
"encoding/hex"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
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/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/cosmos/peggy/x/oracle/types"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
)
const (
TestID = "oracleID"
AlternateTestID = "altOracleID"
TestString = "{value: 5}"
AlternateTestString = "{value: 7}"
AnotherAlternateTestString = "{value: 9}"
)
// CreateTestKeepers greates an Mock App, OracleKeeper, BankKeeper and ValidatorAddresses to be used for test input
func CreateTestKeepers(t *testing.T, consensusNeeded float64, validatorAmounts []int64, extraMaccPerm string) (
sdk.Context, Keeper, bank.Keeper, supply.Keeper, auth.AccountKeeper, []sdk.ValAddress) {
PKs := CreateTestPubKeys(500)
keyStaking := sdk.NewKVStoreKey(stakingtypes.StoreKey)
tkeyStaking := sdk.NewTransientStoreKey(stakingtypes.TStoreKey)
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
keyParams := sdk.NewKVStoreKey(params.StoreKey)
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
keyOracle := sdk.NewKVStoreKey(types.StoreKey)
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(tkeyStaking, sdk.StoreTypeTransient, nil)
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyOracle, sdk.StoreTypeIAVL, db)
err := ms.LoadLatestVersion()
require.NoError(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, false, nil)
ctx = ctx.WithConsensusParams(
&abci.ConsensusParams{
Validator: &abci.ValidatorParams{
PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519},
},
},
)
ctx = ctx.WithLogger(log.NewNopLogger())
cdc := MakeTestCodec()
feeCollectorAcc := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
notBondedPool := supply.NewEmptyModuleAccount(stakingtypes.NotBondedPoolName, supply.Burner, supply.Staking)
bondPool := supply.NewEmptyModuleAccount(stakingtypes.BondedPoolName, supply.Burner, supply.Staking)
blacklistedAddrs := make(map[string]bool)
blacklistedAddrs[feeCollectorAcc.GetAddress().String()] = true
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
blacklistedAddrs[bondPool.GetAddress().String()] = true
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams)
accountKeeper := auth.NewAccountKeeper(
cdc, // amino codec
keyAcc, // target store
paramsKeeper.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount, // prototype
)
bankKeeper := bank.NewBaseKeeper(
accountKeeper,
paramsKeeper.Subspace(bank.DefaultParamspace),
blacklistedAddrs,
)
maccPerms := map[string][]string{
auth.FeeCollectorName: nil,
stakingtypes.NotBondedPoolName: {supply.Burner, supply.Staking},
stakingtypes.BondedPoolName: {supply.Burner, supply.Staking},
}
if extraMaccPerm != "" {
maccPerms[extraMaccPerm] = []string{supply.Burner, supply.Minter}
}
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bankKeeper, maccPerms)
initTokens := sdk.TokensFromConsensusPower(10000)
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens.MulRaw(int64(100))))
supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
stakingKeeper := staking.NewKeeper(cdc, keyStaking, supplyKeeper, paramsKeeper.Subspace(staking.DefaultParamspace))
stakingKeeper.SetParams(ctx, stakingtypes.DefaultParams())
oracleKeeper := NewKeeper(cdc, keyOracle, stakingKeeper, consensusNeeded)
// set module accounts
err = notBondedPool.SetCoins(totalSupply)
require.NoError(t, err)
supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc)
supplyKeeper.SetModuleAccount(ctx, bondPool)
supplyKeeper.SetModuleAccount(ctx, notBondedPool)
// Setup validators
valAddrs := make([]sdk.ValAddress, len(validatorAmounts))
for i, amount := range validatorAmounts {
valPubKey := PKs[i]
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
valAddrs[i] = valAddr
valTokens := sdk.TokensFromConsensusPower(amount)
// test how the validator is set from a purely unbonbed pool
validator := stakingtypes.NewValidator(valAddr, valPubKey, stakingtypes.Description{})
validator, _ = validator.AddTokensFromDel(valTokens)
stakingKeeper.SetValidator(ctx, validator)
stakingKeeper.SetValidatorByPowerIndex(ctx, validator)
stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
}
return ctx, oracleKeeper, bankKeeper, supplyKeeper, accountKeeper, valAddrs
}
// 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 := stakingkeeper.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, NewPubKey(buffer.String()))
buffer.Reset()
}
return publicKeys
}
func NewPubKey(pk string) (res crypto.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
//res, err = crypto.PubKeyFromBytes(pkBytes)
var pkEd ed25519.PubKeyEd25519
copy(pkEd[:], pkBytes)
return pkEd
}
// create a codec used only for testing
func MakeTestCodec() *codec.Codec {
var cdc = codec.New()
// Register Msgs
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
// Register AppAccount
cdc.RegisterInterface((*authexported.Account)(nil), nil)
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/staking/BaseAccount", nil)
supply.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
staking.RegisterCodec(cdc)
bank.RegisterCodec(cdc)
return cdc
}