forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_test.go
199 lines (176 loc) · 6.27 KB
/
module_test.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
package provider_test
import (
"testing"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/interchain-security/v6/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/v6/testutil/keeper"
"github.com/cosmos/interchain-security/v6/x/ccv/provider"
"github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/v6/x/ccv/types"
)
// Tests the provider's InitGenesis implementation against the spec.
// See: https://github.com/cosmos/ibc/blob/main/spec/app/ics-028-cross-chain-validation/methods.md#ccv-pcf-initg1
// Spec tag: [CCV-PCF-INITG.1]
//
// Note: Genesis validation for the provider is tested in TestValidateGenesisState
func TestInitGenesis(t *testing.T) {
type testCase struct {
name string
// Whether port capability is already bound to the CCV provider module
isBound bool
// Provider's storage of consumer state to test against
consumerStates []types.ConsumerState
// Error returned from ClaimCapability during port binding, default: nil
errFromClaimCap error
// Whether method call should panic, default: false
expPanic bool
}
tests := []testCase{
{
name: "already bound port, no consumer states",
isBound: true,
consumerStates: []types.ConsumerState{},
},
{
name: "no bound port, no consumer states",
isBound: false,
consumerStates: []types.ConsumerState{},
},
{
name: "no bound port, multiple consumer states",
isBound: false,
consumerStates: []types.ConsumerState{
{
ChainId: "chainId1",
ChannelId: "channelIdToChain1",
},
{
ChainId: "chainId2",
ChannelId: "channelIdToChain2",
},
{
ChainId: "chainId3",
ChannelId: "channelIdToChain3",
},
},
},
{
name: "already bound port, one consumer state",
isBound: true,
consumerStates: []types.ConsumerState{
{
ChainId: "chainId77",
ChannelId: "channelIdToChain77",
},
},
},
{
name: "capability not owned, method should panic",
isBound: false,
consumerStates: []types.ConsumerState{
{
ChainId: "chainId77",
ChannelId: "channelIdToChain77",
},
},
errFromClaimCap: capabilitytypes.ErrCapabilityNotOwned,
expPanic: true,
},
}
for _, tc := range tests {
//
// Setup
//
keeperParams := testkeeper.NewInMemKeeperParams(t)
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, keeperParams)
appModule := provider.NewAppModule(&providerKeeper, *keeperParams.ParamsSubspace, keeperParams.StoreKey)
genState := types.NewGenesisState(
providerKeeper.GetValidatorSetUpdateId(ctx),
nil,
tc.consumerStates,
types.DefaultParams(),
nil,
nil,
nil,
)
cdc := keeperParams.Cdc
jsonBytes := cdc.MustMarshalJSON(genState)
//
// Assert mocked logic before method executes
//
orderedCalls := []*gomock.Call{
mocks.MockScopedKeeper.EXPECT().GetCapability(
ctx, host.PortPath(ccv.ProviderPortID),
).Return(
&capabilitytypes.Capability{},
tc.isBound, // Capability is returned successfully if port capability is already bound to this module.
),
}
// If port capability is not already bound, port will be bound and capability claimed.
if !tc.isBound {
dummyCap := &capabilitytypes.Capability{}
orderedCalls = append(orderedCalls,
mocks.MockPortKeeper.EXPECT().BindPort(ctx, ccv.ProviderPortID).Return(dummyCap),
mocks.MockScopedKeeper.EXPECT().ClaimCapability(
ctx, dummyCap, host.PortPath(ccv.ProviderPortID)).Return(tc.errFromClaimCap),
)
}
// Last total power is queried in InitGenesis, only if method has not
// already panicked from unowned capability.
if !tc.expPanic {
// create a mock validator
cId := crypto.NewCryptoIdentityFromIntSeed(234234)
validator := cId.SDKStakingValidator()
valAddr, err := sdk.ValAddressFromBech32(validator.GetOperator())
require.NoError(t, err)
orderedCalls = append(orderedCalls,
mocks.MockStakingKeeper.EXPECT().GetLastTotalPower(
ctx).Return(math.NewInt(100), nil).Times(1), // Return total voting power as 100
mocks.MockStakingKeeper.EXPECT().GetBondedValidatorsByPower(
ctx).Return([]stakingtypes.Validator{validator}, nil).Times(1), // Return a single validator
mocks.MockStakingKeeper.EXPECT().GetLastValidatorPower(
ctx, valAddr).Return(int64(100), nil).Times(1), // Return total power as power of the single validator
)
}
gomock.InOrder(orderedCalls...)
//
// Execute method, then assert expected results
//
if tc.expPanic {
require.Panics(t, assert.PanicTestFunc(func() {
appModule.InitGenesis(ctx, cdc, jsonBytes)
}), tc.name)
continue // Nothing else to verify
}
appModule.InitGenesis(ctx, cdc, jsonBytes)
numStatesCounted := 0
for _, state := range tc.consumerStates {
numStatesCounted += 1
channelID, found := providerKeeper.GetConsumerIdToChannelId(ctx, state.ChainId)
require.True(t, found)
require.Equal(t, state.ChannelId, channelID)
chainID, found := providerKeeper.GetChannelIdToConsumerId(ctx, state.ChannelId)
require.True(t, found)
require.Equal(t, state.ChainId, chainID)
}
require.Equal(t, len(tc.consumerStates), numStatesCounted)
// Expect slash meter to be initialized to it's allowance value
// (replenish fraction * mocked value defined above)
slashMeter := providerKeeper.GetSlashMeter(ctx)
replenishFraction, err := math.LegacyNewDecFromStr(providerKeeper.GetParams(ctx).SlashMeterReplenishFraction)
require.NoError(t, err)
expectedSlashMeterValue := math.NewInt(replenishFraction.MulInt(math.NewInt(100)).RoundInt64())
require.Equal(t, expectedSlashMeterValue, slashMeter)
// Expect slash meter replenishment time candidate to be set to the current block time + replenish period
expectedNextReplenishTime := ctx.BlockTime().Add(providerKeeper.GetSlashMeterReplenishPeriod(ctx))
require.Equal(t, expectedNextReplenishTime, providerKeeper.GetSlashMeterReplenishTimeCandidate(ctx))
ctrl.Finish()
}
}