/
expectations.go
221 lines (198 loc) · 8.55 KB
/
expectations.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
package keeper
import (
time "time"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibctmtypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/golang/mock/gomock"
extra "github.com/oxyno-zeta/gomock-extra-matcher"
math "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
"github.com/cosmos/interchain-security/v5/x/ccv/types"
)
//
// A file containing groups of commonly used mock expectations.
// Note: Each group of mock expectations is associated with a single method
// that may be called during unit tests.
//
// GetMocksForCreateConsumerClient returns mock expectations needed to call CreateConsumerClient().
func GetMocksForCreateConsumerClient(ctx sdk.Context, mocks *MockedKeepers,
expectedChainID string, expectedLatestHeight clienttypes.Height,
) []*gomock.Call {
// append MakeConsumerGenesis and CreateClient expectations
expectations := GetMocksForMakeConsumerGenesis(ctx, mocks, time.Hour)
createClientExp := mocks.MockClientKeeper.EXPECT().CreateClient(
gomock.Any(),
// Allows us to expect a match by field. These are the only two client state values
// that are dependent on parameters passed to CreateConsumerClient.
extra.StructMatcher().Field(
"ChainId", expectedChainID).Field(
"LatestHeight", expectedLatestHeight,
),
gomock.Any(),
).Return("clientID", nil).Times(1)
expectations = append(expectations, createClientExp)
return expectations
}
// GetMocksForMakeConsumerGenesis returns mock expectations needed to call MakeConsumerGenesis().
func GetMocksForMakeConsumerGenesis(ctx sdk.Context, mocks *MockedKeepers,
unbondingTimeToInject time.Duration,
) []*gomock.Call {
return []*gomock.Call{
mocks.MockStakingKeeper.EXPECT().UnbondingTime(gomock.Any()).Return(unbondingTimeToInject, nil).Times(1),
mocks.MockClientKeeper.EXPECT().GetSelfConsensusState(gomock.Any(),
clienttypes.GetSelfHeight(ctx)).Return(&ibctmtypes.ConsensusState{}, nil).Times(1),
mocks.MockStakingKeeper.EXPECT().IterateLastValidatorPowers(gomock.Any(), gomock.Any()).Times(1),
}
}
// GetMocksForSetConsumerChain returns mock expectations needed to call SetConsumerChain().
func GetMocksForSetConsumerChain(ctx sdk.Context, mocks *MockedKeepers,
chainIDToInject string,
) []*gomock.Call {
return []*gomock.Call{
mocks.MockChannelKeeper.EXPECT().GetChannel(ctx, types.ProviderPortID, gomock.Any()).Return(
channeltypes.Channel{
State: channeltypes.OPEN,
ConnectionHops: []string{"connectionID"},
},
true,
).Times(1),
mocks.MockConnectionKeeper.EXPECT().GetConnection(ctx, "connectionID").Return(
conntypes.ConnectionEnd{ClientId: "clientID"}, true,
).Times(1),
mocks.MockClientKeeper.EXPECT().GetClientState(ctx, "clientID").Return(
&ibctmtypes.ClientState{ChainId: chainIDToInject}, true,
).Times(1),
}
}
// GetMocksForStopConsumerChainWithCloseChannel returns mock expectations needed to call StopConsumerChain() when
// `closeChan` is true.
func GetMocksForStopConsumerChainWithCloseChannel(ctx sdk.Context, mocks *MockedKeepers) []*gomock.Call {
dummyCap := &capabilitytypes.Capability{}
return []*gomock.Call{
mocks.MockChannelKeeper.EXPECT().GetChannel(gomock.Any(), types.ProviderPortID, "channelID").Return(
channeltypes.Channel{State: channeltypes.OPEN}, true,
).Times(1),
mocks.MockScopedKeeper.EXPECT().GetCapability(gomock.Any(), gomock.Any()).Return(dummyCap, true).Times(1),
mocks.MockChannelKeeper.EXPECT().ChanCloseInit(gomock.Any(), types.ProviderPortID, "channelID", dummyCap).Times(1),
}
}
func GetMocksForHandleSlashPacket(ctx sdk.Context, mocks MockedKeepers,
expectedProviderValConsAddr providertypes.ProviderConsAddress,
valToReturn stakingtypes.Validator, expectJailing bool,
) []*gomock.Call {
// These first two calls are always made.
calls := []*gomock.Call{
mocks.MockStakingKeeper.EXPECT().GetValidatorByConsAddr(
ctx, expectedProviderValConsAddr.ToSdkConsAddr()).Return(
valToReturn, nil,
).Times(1),
mocks.MockSlashingKeeper.EXPECT().IsTombstoned(ctx,
expectedProviderValConsAddr.ToSdkConsAddr()).Return(false).Times(1),
}
if expectJailing {
calls = append(calls, mocks.MockStakingKeeper.EXPECT().Jail(
gomock.Eq(ctx),
gomock.Eq(expectedProviderValConsAddr.ToSdkConsAddr()),
).Return(nil))
// JailUntil is set in this code path.
calls = append(calls, mocks.MockSlashingKeeper.EXPECT().DowntimeJailDuration(ctx).Return(time.Hour, nil).Times(1))
calls = append(calls, mocks.MockSlashingKeeper.EXPECT().JailUntil(ctx,
expectedProviderValConsAddr.ToSdkConsAddr(), gomock.Any()).Return(nil).Times(1))
}
return calls
}
func ExpectLatestConsensusStateMock(ctx sdk.Context, mocks MockedKeepers, clientID string, consState *ibctmtypes.ConsensusState) *gomock.Call {
return mocks.MockClientKeeper.EXPECT().
GetLatestClientConsensusState(ctx, clientID).Return(consState, true).Times(1)
}
func ExpectCreateClientMock(ctx sdk.Context, mocks MockedKeepers, clientID string, clientState *ibctmtypes.ClientState, consState *ibctmtypes.ConsensusState) *gomock.Call {
return mocks.MockClientKeeper.EXPECT().CreateClient(ctx, clientState, consState).Return(clientID, nil).Times(1)
}
func ExpectGetCapabilityMock(ctx sdk.Context, mocks MockedKeepers, times int) *gomock.Call {
return mocks.MockScopedKeeper.EXPECT().GetCapability(
ctx, host.PortPath(types.ConsumerPortID),
).Return(nil, true).Times(times)
}
func GetMocksForSendIBCPacket(ctx sdk.Context, mocks MockedKeepers, channelID string, times int) []*gomock.Call {
return []*gomock.Call{
mocks.MockChannelKeeper.EXPECT().GetChannel(ctx, types.ConsumerPortID,
"consumerCCVChannelID").Return(channeltypes.Channel{}, true).Times(times),
mocks.MockScopedKeeper.EXPECT().GetCapability(ctx,
host.ChannelCapabilityPath(types.ConsumerPortID, "consumerCCVChannelID")).Return(
capabilitytypes.NewCapability(1), true).Times(times),
mocks.MockChannelKeeper.EXPECT().SendPacket(ctx,
capabilitytypes.NewCapability(1),
types.ConsumerPortID,
"consumerCCVChannelID",
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(uint64(888), nil).Times(times),
}
}
func GetMocksForSlashValidator(
ctx sdk.Context,
mocks MockedKeepers,
validator stakingtypes.Validator,
consAddr sdk.ConsAddress,
undelegations []stakingtypes.UnbondingDelegation,
redelegations []stakingtypes.Redelegation,
powerReduction math.Int,
slashFraction math.LegacyDec,
currentPower,
expectedInfractionHeight,
expectedSlashPower int64,
) []*gomock.Call {
return []*gomock.Call{
mocks.MockStakingKeeper.EXPECT().
GetUnbondingDelegationsFromValidator(ctx, validator.GetOperator()).
Return(undelegations),
mocks.MockStakingKeeper.EXPECT().
GetRedelegationsFromSrcValidator(ctx, validator.GetOperator()).
Return(redelegations),
mocks.MockStakingKeeper.EXPECT().
GetLastValidatorPower(ctx, validator.GetOperator()).
Return(currentPower),
mocks.MockStakingKeeper.EXPECT().
PowerReduction(ctx).
Return(powerReduction),
mocks.MockStakingKeeper.EXPECT().
SlashUnbondingDelegation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(
func(_ sdk.Context, undelegation stakingtypes.UnbondingDelegation, _ int64, _ math.LegacyDec) math.Int {
sum := math.NewInt(0)
for _, r := range undelegation.Entries {
if r.IsMature(ctx.BlockTime()) {
continue
}
sum = sum.Add(math.NewInt(r.InitialBalance.Int64()))
}
return sum
}).AnyTimes(),
mocks.MockStakingKeeper.EXPECT().
SlashRedelegation(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(
func(_ sdk.Context, _ stakingtypes.Validator, redelegation stakingtypes.Redelegation, _ int64, _ math.LegacyDec) math.Int {
sum := math.NewInt(0)
for _, r := range redelegation.Entries {
if r.IsMature(ctx.BlockTime()) {
continue
}
sum = sum.Add(math.NewInt(r.InitialBalance.Int64()))
}
return sum
}).AnyTimes(),
mocks.MockSlashingKeeper.EXPECT().
SlashFractionDoubleSign(ctx).
Return(slashFraction),
mocks.MockStakingKeeper.EXPECT().
SlashWithInfractionReason(ctx, consAddr, expectedInfractionHeight, expectedSlashPower, slashFraction, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN).
Times(1),
}
}