/
unit_test_helpers.go
260 lines (234 loc) · 8.81 KB
/
unit_test_helpers.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package keeper
import (
"crypto/rand"
"encoding/binary"
"testing"
"time"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
consumerkeeper "github.com/cosmos/interchain-security/v2/x/ccv/consumer/keeper"
consumertypes "github.com/cosmos/interchain-security/v2/x/ccv/consumer/types"
providerkeeper "github.com/cosmos/interchain-security/v2/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v2/x/ccv/provider/types"
"github.com/cosmos/interchain-security/v2/x/ccv/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
)
// Parameters needed to instantiate an in-memory keeper
type InMemKeeperParams struct {
Cdc *codec.ProtoCodec
StoreKey *storetypes.KVStoreKey
ParamsSubspace *paramstypes.Subspace
Ctx sdk.Context
}
// NewInMemKeeperParams instantiates in-memory keeper params with default values
func NewInMemKeeperParams(tb testing.TB) InMemKeeperParams {
tb.Helper()
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
require.NoError(tb, stateStore.LoadLatestVersion())
registry := codectypes.NewInterfaceRegistry()
cryptocodec.RegisterInterfaces(registry) // Public key implementation registered here
cdc := codec.NewProtoCodec(registry)
paramsSubspace := paramstypes.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
paramstypes.ModuleName,
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
return InMemKeeperParams{
Cdc: cdc,
StoreKey: storeKey,
ParamsSubspace: ¶msSubspace,
Ctx: ctx,
}
}
// A struct holding pointers to any mocked external keeper needed for provider/consumer keeper setup.
type MockedKeepers struct {
*MockScopedKeeper
*MockChannelKeeper
*MockPortKeeper
*MockConnectionKeeper
*MockClientKeeper
*MockStakingKeeper
*MockSlashingKeeper
*MockAccountKeeper
*MockBankKeeper
*MockIBCTransferKeeper
*MockIBCCoreKeeper
*MockEvidenceKeeper
*MockDistributionKeeper
}
// NewMockedKeepers instantiates a struct with pointers to properly instantiated mocked keepers.
func NewMockedKeepers(ctrl *gomock.Controller) MockedKeepers {
return MockedKeepers{
MockScopedKeeper: NewMockScopedKeeper(ctrl),
MockChannelKeeper: NewMockChannelKeeper(ctrl),
MockPortKeeper: NewMockPortKeeper(ctrl),
MockConnectionKeeper: NewMockConnectionKeeper(ctrl),
MockClientKeeper: NewMockClientKeeper(ctrl),
MockStakingKeeper: NewMockStakingKeeper(ctrl),
MockSlashingKeeper: NewMockSlashingKeeper(ctrl),
MockAccountKeeper: NewMockAccountKeeper(ctrl),
MockBankKeeper: NewMockBankKeeper(ctrl),
MockIBCTransferKeeper: NewMockIBCTransferKeeper(ctrl),
MockIBCCoreKeeper: NewMockIBCCoreKeeper(ctrl),
MockEvidenceKeeper: NewMockEvidenceKeeper(ctrl),
MockDistributionKeeper: NewMockDistributionKeeper(ctrl),
}
}
// NewInMemProviderKeeper instantiates an in-mem provider keeper from params and mocked keepers
func NewInMemProviderKeeper(params InMemKeeperParams, mocks MockedKeepers) providerkeeper.Keeper {
return providerkeeper.NewKeeper(
params.Cdc,
params.StoreKey,
*params.ParamsSubspace,
mocks.MockScopedKeeper,
mocks.MockChannelKeeper,
mocks.MockPortKeeper,
mocks.MockConnectionKeeper,
mocks.MockClientKeeper,
mocks.MockStakingKeeper,
mocks.MockSlashingKeeper,
mocks.MockAccountKeeper,
mocks.MockEvidenceKeeper,
mocks.MockDistributionKeeper,
mocks.MockBankKeeper,
authtypes.FeeCollectorName,
)
}
// NewInMemConsumerKeeper instantiates an in-mem consumer keeper from params and mocked keepers
func NewInMemConsumerKeeper(params InMemKeeperParams, mocks MockedKeepers) consumerkeeper.Keeper {
return consumerkeeper.NewKeeper(
params.Cdc,
params.StoreKey,
*params.ParamsSubspace,
mocks.MockScopedKeeper,
mocks.MockChannelKeeper,
mocks.MockPortKeeper,
mocks.MockConnectionKeeper,
mocks.MockClientKeeper,
mocks.MockSlashingKeeper,
mocks.MockBankKeeper,
mocks.MockAccountKeeper,
mocks.MockIBCTransferKeeper,
mocks.MockIBCCoreKeeper,
authtypes.FeeCollectorName,
)
}
// Returns an in-memory provider keeper, context, controller, and mocks, given a test instance and parameters.
//
// Note: Calling ctrl.Finish() at the end of a test function ensures that
// no unexpected calls to external keepers are made.
func GetProviderKeeperAndCtx(t *testing.T, params InMemKeeperParams) (
providerkeeper.Keeper, sdk.Context, *gomock.Controller, MockedKeepers,
) {
t.Helper()
ctrl := gomock.NewController(t)
mocks := NewMockedKeepers(ctrl)
return NewInMemProviderKeeper(params, mocks), params.Ctx, ctrl, mocks
}
// Return an in-memory consumer keeper, context, controller, and mocks, given a test instance and parameters.
//
// Note: Calling ctrl.Finish() at the end of a test function ensures that
// no unexpected calls to external keepers are made.
func GetConsumerKeeperAndCtx(t *testing.T, params InMemKeeperParams) (
consumerkeeper.Keeper, sdk.Context, *gomock.Controller, MockedKeepers,
) {
t.Helper()
ctrl := gomock.NewController(t)
mocks := NewMockedKeepers(ctrl)
return NewInMemConsumerKeeper(params, mocks), params.Ctx, ctrl, mocks
}
type PrivateKey struct {
PrivKey cryptotypes.PrivKey
}
// Obtains slash packet data with a newly generated key, and randomized field values
func GetNewSlashPacketData() types.SlashPacketData {
b1 := make([]byte, 8)
_, _ = rand.Read(b1)
b2 := make([]byte, 8)
_, _ = rand.Read(b2)
b3 := make([]byte, 8)
_, _ = rand.Read(b3)
return types.SlashPacketData{
Validator: abci.Validator{
Address: ed25519.GenPrivKey().PubKey().Address(),
Power: int64(binary.BigEndian.Uint64(b1)),
},
ValsetUpdateId: binary.BigEndian.Uint64(b2),
Infraction: stakingtypes.InfractionType(binary.BigEndian.Uint64(b2) % 3),
}
}
// Obtains vsc matured packet data with a newly generated key
func GetNewVSCMaturedPacketData() types.VSCMaturedPacketData {
b := make([]byte, 8)
_, _ = rand.Read(b)
return types.VSCMaturedPacketData{ValsetUpdateId: binary.BigEndian.Uint64(b)}
}
// SetupForStoppingConsumerChain registers expected mock calls and corresponding state setup
// which asserts that a consumer chain was properly stopped from StopConsumerChain().
func SetupForStoppingConsumerChain(t *testing.T, ctx sdk.Context,
providerKeeper *providerkeeper.Keeper, mocks MockedKeepers,
) {
t.Helper()
expectations := GetMocksForCreateConsumerClient(ctx, &mocks,
"chainID", clienttypes.NewHeight(4, 5))
expectations = append(expectations, GetMocksForSetConsumerChain(ctx, &mocks, "chainID")...)
expectations = append(expectations, GetMocksForStopConsumerChain(ctx, &mocks)...)
gomock.InOrder(expectations...)
prop := GetTestConsumerAdditionProp()
err := providerKeeper.CreateConsumerClient(ctx, prop)
require.NoError(t, err)
err = providerKeeper.SetConsumerChain(ctx, "channelID")
require.NoError(t, err)
}
func GetTestConsumerAdditionProp() *providertypes.ConsumerAdditionProposal {
prop := providertypes.NewConsumerAdditionProposal(
"chainID",
"description",
"chainID",
clienttypes.NewHeight(4, 5),
[]byte("gen_hash"),
[]byte("bin_hash"),
time.Now(),
consumertypes.DefaultConsumerRedistributeFrac,
consumertypes.DefaultBlocksPerDistributionTransmission,
"",
consumertypes.DefaultHistoricalEntries,
types.DefaultCCVTimeoutPeriod,
consumertypes.DefaultTransferTimeoutPeriod,
consumertypes.DefaultConsumerUnbondingPeriod,
).(*providertypes.ConsumerAdditionProposal)
return prop
}
// Obtains a CrossChainValidator with a newly generated key, and randomized field values
func GetNewCrossChainValidator(t *testing.T) consumertypes.CrossChainValidator {
t.Helper()
b1 := make([]byte, 8)
_, _ = rand.Read(b1)
power := int64(binary.BigEndian.Uint64(b1))
privKey := ed25519.GenPrivKey()
validator, err := consumertypes.NewCCValidator(privKey.PubKey().Address(), power, privKey.PubKey())
require.NoError(t, err)
return validator
}