forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_consumer.go
165 lines (137 loc) · 5.77 KB
/
stop_consumer.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
package integration
import (
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ccv "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
// Tests the functionality of stopping a consumer chain at a higher level than unit tests
func (s *CCVTestSuite) TestStopConsumerChain() {
providerKeeper := s.providerApp.GetProviderKeeper()
providerStakingKeeper := s.providerApp.GetTestStakingKeeper()
firstBundle := s.getFirstBundle()
// choose a validator
tmValidator := s.providerChain.Vals.Validators[0]
valAddr, err := sdk.ValAddressFromHex(tmValidator.Address.String())
s.Require().NoError(err)
validator, found := providerStakingKeeper.GetValidator(s.providerCtx(), valAddr)
s.Require().True(found)
// get delegator address
delAddr := s.providerChain.SenderAccount.GetAddress()
// define variables required for test setup
var (
// bond amount
bondAmt = sdk.NewInt(1000000)
// number of unbonding operations performed
ubdOpsNum = 4
// store new shares created
testShares sdk.Dec
)
// populate the provider chain states to setup the test using the following operations:
// - setup CCV channel; establish CCV channel and set channelToChain, chainToChannel and initHeight mapping for the consumer chain ID
// - delegate the total bond amount to the chose validator
// - undelegate the shares in four consecutive blocks evenly; create UnbondigOp and UnbondingOpIndex entries for the consumer chain ID
// - set SlashAck state for the consumer chain ID
setupOperations := []struct {
fn func(suite *CCVTestSuite) error
}{
{
func(suite *CCVTestSuite) error {
suite.SetupAllCCVChannels()
suite.SetupTransferChannel()
return nil
},
},
{
func(suite *CCVTestSuite) error {
testShares, err = providerStakingKeeper.Delegate(s.providerCtx(), delAddr, bondAmt, stakingtypes.Unbonded, validator, true)
return err
},
},
{
func(suite *CCVTestSuite) error {
for i := 0; i < ubdOpsNum; i++ {
// undelegate one quarter of the shares
_, err := providerStakingKeeper.Undelegate(s.providerCtx(), delAddr, valAddr, testShares.QuoInt64(int64(ubdOpsNum)))
if err != nil {
return err
}
// increment block
s.providerChain.NextBlock()
}
return nil
},
},
{
func(suite *CCVTestSuite) error {
providerKeeper.SetSlashAcks(s.providerCtx(), firstBundle.Chain.ChainID, []string{"validator-1", "validator-2", "validator-3"})
providerKeeper.AppendPendingVSCPackets(s.providerCtx(), firstBundle.Chain.ChainID, ccv.ValidatorSetChangePacketData{ValsetUpdateId: 1})
return nil
},
},
}
for _, so := range setupOperations {
err := so.fn(s)
s.Require().NoError(err)
}
// stop the consumer chain
err = providerKeeper.StopConsumerChain(s.providerCtx(), firstBundle.Chain.ChainID, true)
s.Require().NoError(err)
// check all states are removed and the unbonding operation released
s.checkConsumerChainIsRemoved(firstBundle.Chain.ChainID, true)
}
// TODO Simon: implement OnChanCloseConfirm in IBC-GO testing to close the consumer chain's channel end
func (s *CCVTestSuite) TestStopConsumerOnChannelClosed() {
// init the CCV channel states
s.SetupCCVChannel(s.path)
s.SetupTransferChannel()
s.SendEmptyVSCPacket()
providerKeeper := s.providerApp.GetProviderKeeper()
// stop the consumer chain
err := providerKeeper.StopConsumerChain(s.providerCtx(), s.consumerChain.ChainID, true)
s.Require().NoError(err)
err = s.path.EndpointA.UpdateClient()
s.Require().NoError(err)
// check that provider chain's channel end is closed
s.Require().Equal(channeltypes.CLOSED, s.path.EndpointB.GetChannel().State)
// simulate a relayer behaviour
// err = s.path.EndpointA.OnChanCloseConfirm()
// s.Require().NoError(err)
// expect to panic in consumer chain's BeginBlock due to the above
// s.consumerChain.NextBlock()
// check that the provider's channel is removed
// _, found := s.consumerApp.GetConsumerKeeper().GetProviderChannel(s.consumerCtx())
// s.Require().False(found)
}
func (s *CCVTestSuite) checkConsumerChainIsRemoved(chainID string, checkChannel bool) {
channelID := s.path.EndpointB.ChannelID
providerKeeper := s.providerApp.GetProviderKeeper()
providerStakingKeeper := s.providerApp.GetTestStakingKeeper()
if checkChannel {
// check channel's state is closed
s.Require().Equal(channeltypes.CLOSED, s.path.EndpointB.GetChannel().State)
}
// check UnbondingOps were deleted and undelegation entries aren't onHold
for _, unbondingOpsIndex := range providerKeeper.GetAllUnbondingOpIndexes(s.providerCtx(), chainID) {
_, found := providerKeeper.GetUnbondingOpIndex(s.providerCtx(), chainID, unbondingOpsIndex.VscId)
s.Require().False(found)
for _, ubdID := range unbondingOpsIndex.UnbondingOpIds {
_, found = providerKeeper.GetUnbondingOp(s.providerCtx(), unbondingOpsIndex.UnbondingOpIds[ubdID])
s.Require().False(found)
ubd, _ := providerStakingKeeper.GetUnbondingDelegationByUnbondingID(s.providerCtx(), unbondingOpsIndex.UnbondingOpIds[ubdID])
s.Require().Zero(ubd.Entries[ubdID].UnbondingOnHoldRefCount)
}
}
// verify consumer chain's states are removed
_, found := providerKeeper.GetConsumerGenesis(s.providerCtx(), chainID)
s.Require().False(found)
_, found = providerKeeper.GetConsumerClientId(s.providerCtx(), chainID)
s.Require().False(found)
_, found = providerKeeper.GetChainToChannel(s.providerCtx(), chainID)
s.Require().False(found)
_, found = providerKeeper.GetChannelToChain(s.providerCtx(), channelID)
s.Require().False(found)
s.Require().Nil(providerKeeper.GetSlashAcks(s.providerCtx(), chainID))
s.Require().Zero(providerKeeper.GetInitChainHeight(s.providerCtx(), chainID))
s.Require().Empty(providerKeeper.GetPendingVSCPackets(s.providerCtx(), chainID))
}