-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.go
239 lines (193 loc) · 7.4 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// nolint
// DONTCOVER
package gov
import (
"bytes"
"errors"
"log"
"sort"
"testing"
"github.com/evoblockchain/evochain/libs/tendermint/crypto/ed25519"
"github.com/stretchr/testify/require"
abci "github.com/evoblockchain/evochain/libs/tendermint/abci/types"
"github.com/evoblockchain/evochain/libs/tendermint/crypto"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
sdkerrors "github.com/evoblockchain/evochain/libs/cosmos-sdk/types/errors"
authexported "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth/exported"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/bank"
keep "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/gov/keeper"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/gov/types"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/mock"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/staking"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/supply"
supplyexported "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/supply/exported"
)
var (
valTokens = sdk.TokensFromConsensusPower(42)
initTokens = sdk.TokensFromConsensusPower(100000)
valCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, valTokens))
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
)
type testInput struct {
mApp *mock.App
keeper keep.Keeper
router types.Router
sk staking.Keeper
addrs []sdk.AccAddress
pubKeys []crypto.PubKey
privKeys []crypto.PrivKey
}
func getMockApp(
t *testing.T, numGenAccs int, genState types.GenesisState, genAccs []authexported.Account,
handler func(ctx sdk.Context, c types.Content) error,
) testInput {
mApp := mock.NewApp()
staking.RegisterCodec(mApp.Cdc.GetCdc())
types.RegisterCodec(mApp.Cdc.GetCdc())
supply.RegisterCodec(mApp.Cdc.GetCdc())
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
keyGov := sdk.NewKVStoreKey(types.StoreKey)
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
govAcc := supply.NewEmptyModuleAccount(types.ModuleName, supply.Burner)
notBondedPool := supply.NewEmptyModuleAccount(staking.NotBondedPoolName, supply.Burner, supply.Staking)
bondPool := supply.NewEmptyModuleAccount(staking.BondedPoolName, supply.Burner, supply.Staking)
blacklistedAddrs := make(map[string]bool)
blacklistedAddrs[govAcc.GetAddress().String()] = true
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
blacklistedAddrs[bondPool.GetAddress().String()] = true
pk := mApp.ParamsKeeper
rtr := types.NewRouter().
AddRoute(types.RouterKey, handler)
bk := bank.NewBaseKeeper(mApp.AccountKeeper, mApp.ParamsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs)
maccPerms := map[string][]string{
types.ModuleName: {supply.Burner},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
staking.BondedPoolName: {supply.Burner, supply.Staking},
}
supplyKeeper := supply.NewKeeper(mApp.Cdc.GetCdc(), keySupply, mApp.AccountKeeper, bk, maccPerms)
sk := staking.NewKeeper(
mApp.Cdc.GetCdc(), keyStaking, supplyKeeper, pk.Subspace(staking.DefaultParamspace),
)
keeper := keep.NewKeeper(
mApp.Cdc.GetCdc(), keyGov, pk.Subspace(DefaultParamspace).WithKeyTable(ParamKeyTable()), supplyKeeper, sk, rtr,
)
mApp.Router().AddRoute(types.RouterKey, NewHandler(keeper))
mApp.QueryRouter().AddRoute(types.QuerierRoute, keep.NewQuerier(keeper))
mApp.SetEndBlocker(getEndBlocker(keeper))
mApp.SetInitChainer(getInitChainer(mApp, keeper, sk, supplyKeeper, genAccs, genState,
[]supplyexported.ModuleAccountI{govAcc, notBondedPool, bondPool}))
require.NoError(t, mApp.CompleteSetup(keyStaking, keyGov, keySupply))
var (
addrs []sdk.AccAddress
pubKeys []crypto.PubKey
privKeys []crypto.PrivKey
)
if genAccs == nil || len(genAccs) == 0 {
genAccs, addrs, pubKeys, privKeys = mock.CreateGenAccounts(numGenAccs, valCoins)
}
mock.SetGenesis(mApp, genAccs)
return testInput{mApp, keeper, rtr, sk, addrs, pubKeys, privKeys}
}
// gov and staking endblocker
func getEndBlocker(keeper Keeper) sdk.EndBlocker {
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
EndBlocker(ctx, keeper)
return abci.ResponseEndBlock{}
}
}
// gov and staking initchainer
func getInitChainer(mapp *mock.App, keeper Keeper, stakingKeeper staking.Keeper, supplyKeeper supply.Keeper, accs []authexported.Account, genState GenesisState,
blacklistedAddrs []supplyexported.ModuleAccountI) sdk.InitChainer {
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
mapp.InitChainer(ctx, req)
stakingGenesis := staking.DefaultGenesisState()
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens.MulRaw(int64(len(mapp.GenesisAccounts)))))
supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
// set module accounts
for _, macc := range blacklistedAddrs {
supplyKeeper.SetModuleAccount(ctx, macc)
}
validators := staking.InitGenesis(ctx, stakingKeeper, mapp.AccountKeeper, supplyKeeper, stakingGenesis)
if genState.IsEmpty() {
InitGenesis(ctx, keeper, supplyKeeper, types.DefaultGenesisState())
} else {
InitGenesis(ctx, keeper, supplyKeeper, genState)
}
return abci.ResponseInitChain{
Validators: validators,
}
}
}
// SortAddresses - Sorts Addresses
func SortAddresses(addrs []sdk.AccAddress) {
var byteAddrs [][]byte
for _, addr := range addrs {
byteAddrs = append(byteAddrs, addr.Bytes())
}
SortByteArrays(byteAddrs)
for i, byteAddr := range byteAddrs {
addrs[i] = byteAddr
}
}
// implement `Interface` in sort package.
type sortByteArrays [][]byte
func (b sortByteArrays) Len() int {
return len(b)
}
func (b sortByteArrays) Less(i, j int) bool {
// bytes package already implements Comparable for []byte.
switch bytes.Compare(b[i], b[j]) {
case -1:
return true
case 0, 1:
return false
default:
log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
return false
}
}
func (b sortByteArrays) Swap(i, j int) {
b[j], b[i] = b[i], b[j]
}
// SortByteArrays - sorts the provided byte array
func SortByteArrays(src [][]byte) [][]byte {
sorted := sortByteArrays(src)
sort.Sort(sorted)
return sorted
}
const contextKeyBadProposal = "contextKeyBadProposal"
// badProposalHandler implements a governance proposal handler that is identical
// to the actual handler except this fails if the context doesn't contain a value
// for the key contextKeyBadProposal or if the value is false.
func badProposalHandler(ctx sdk.Context, c types.Content) error {
switch c.ProposalType() {
case types.ProposalTypeText:
v := ctx.Context().Value(contextKeyBadProposal)
if v == nil || !v.(bool) {
return errors.New("proposal failed")
}
return nil
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized gov proposal type: %s", c.ProposalType())
}
}
var (
pubkeys = []crypto.PubKey{
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
ed25519.GenPrivKey().PubKey(),
}
)
func createValidators(t *testing.T, stakingHandler sdk.Handler, ctx sdk.Context, addrs []sdk.ValAddress, powerAmt []int64) {
require.True(t, len(addrs) <= len(pubkeys), "Not enough pubkeys specified at top of file.")
for i := 0; i < len(addrs); i++ {
valTokens := sdk.TokensFromConsensusPower(powerAmt[i])
valCreateMsg := staking.NewMsgCreateValidator(
addrs[i], pubkeys[i], sdk.NewCoin(sdk.DefaultBondDenom, valTokens),
keep.TestDescription, keep.TestCommissionRates, sdk.OneInt(),
)
res, err := stakingHandler(ctx, valCreateMsg)
require.NoError(t, err)
require.NotNil(t, res)
}
}