-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
126 lines (101 loc) · 4.63 KB
/
validator.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
package integration
import (
"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var PKs = simtestutil.CreateTestPubKeys(500)
func (s *IntegrationTestSuite) consumerAddValidator(valPubKey cryptotypes.PubKey) sdk.ValAddress {
ctx := s.rsConsumerChain.GetContext()
consumerApp := getConsumerApp(s.rsConsumerChain)
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
valTokens := consumerApp.StakingKeeper.TokensFromConsensusPower(ctx, 10)
newValidator, err := stakingtypes.NewValidator(valAddr, valPubKey, stakingtypes.Description{})
s.Require().NoError(err)
newValidator.AddTokensFromDel(valTokens)
consumerApp.StakingKeeper.SetValidator(ctx, newValidator)
consumerApp.StakingKeeper.SetValidatorByPowerIndex(ctx, newValidator)
consumerApp.StakingKeeper.SetValidatorByConsAddr(ctx, newValidator)
return valAddr
}
func (s *IntegrationTestSuite) TestConsumerAddValidator() {
proposal := CreateConsumerAdditionalProposal(s.path, s.rsConsumerChain)
coordApp := getCoordinatorApp(s.rsCoordinatorChain)
coordCtx := s.rsCoordinatorChain.GetContext()
coordKeeper := coordApp.RestakingCoordinatorKeeper
relayerAddr := s.path.EndpointA.Chain.SenderAccount.GetAddress()
coordKeeper.SetConsumerAdditionProposal(coordCtx, proposal)
s.SetupRestakingPath()
consumerCtx := s.rsConsumerChain.GetContext()
consumerApp := getConsumerApp(s.rsConsumerChain)
valPubKey := PKs[0]
s.consumerAddValidator(valPubKey)
addingValAddr := sdk.ValAddress(valPubKey.Address().Bytes())
consumerApp.StakingKeeper.Hooks().AfterValidatorCreated(consumerCtx, addingValAddr)
pendingVCSList := consumerApp.RestakingConsumerKeeper.GetPendingVSCList(consumerCtx)
s.Require().Equal(len(pendingVCSList), 1)
s.Require().Equal(len(pendingVCSList[0].ValidatorAddresses), 1)
s.Require().Equal(pendingVCSList[0].ValidatorAddresses[0], addingValAddr.String())
events := NextBlockWithEvents(s.rsConsumerChain)
s.path.EndpointA.UpdateClient()
s.path.EndpointB.UpdateClient()
consumerTmClientID, found := coordApp.RestakingCoordinatorKeeper.GetConsumerClientID(coordCtx, s.rsConsumerChain.ChainID)
s.Require().True(found)
valSetBefore := coordKeeper.GetConsumerValidators(coordCtx, string(consumerTmClientID), 100)
valSetBeforeLen := len(valSetBefore)
s.RelayIBCPacket(s.path, events, relayerAddr.String())
coordCtx = s.rsCoordinatorChain.GetContext()
valSetAfter := coordKeeper.GetConsumerValidators(coordCtx, string(consumerTmClientID), 100)
valSetAfterLen := len(valSetAfter)
s.Require().Equal(valSetAfterLen-valSetBeforeLen, 1)
added := false
for _, v := range valSetAfter {
if v.Address == addingValAddr.String() {
added = true
break
}
}
s.Require().True(added)
}
func (s *IntegrationTestSuite) TestConsumerRemoveValidator() {
proposal := CreateConsumerAdditionalProposal(s.path, s.rsConsumerChain)
coordApp := getCoordinatorApp(s.rsCoordinatorChain)
coordCtx := s.rsCoordinatorChain.GetContext()
coordKeeper := coordApp.RestakingCoordinatorKeeper
relayerAddr := s.path.EndpointA.Chain.SenderAccount.GetAddress()
coordKeeper.SetConsumerAdditionProposal(coordCtx, proposal)
s.SetupRestakingPath()
consumerCtx := s.rsConsumerChain.GetContext()
consumerApp := getConsumerApp(s.rsConsumerChain)
valPubKey := PKs[0]
valAddr := s.consumerAddValidator(valPubKey)
addingValAddr := sdk.ValAddress(valPubKey.Address().Bytes())
consumerApp.StakingKeeper.Hooks().AfterValidatorCreated(consumerCtx, addingValAddr)
events := NextBlockWithEvents(s.rsConsumerChain)
s.path.EndpointA.UpdateClient()
s.path.EndpointB.UpdateClient()
s.RelayIBCPacket(s.path, events, relayerAddr.String())
consumerCtx = s.rsConsumerChain.GetContext()
validator, found := consumerApp.StakingKeeper.GetValidator(consumerCtx, valAddr)
s.Require().True(found)
validator.DelegatorShares = math.LegacyZeroDec()
consumerApp.StakingKeeper.SetValidator(consumerCtx, validator)
consumerApp.StakingKeeper.RemoveValidator(consumerCtx, valAddr)
events = NextBlockWithEvents(s.rsConsumerChain)
s.path.EndpointA.UpdateClient()
s.path.EndpointB.UpdateClient()
s.RelayIBCPacket(s.path, events, relayerAddr.String())
coordCtx = s.rsCoordinatorChain.GetContext()
consumerTmClientID, found := coordApp.RestakingCoordinatorKeeper.GetConsumerClientID(coordCtx, s.rsConsumerChain.ChainID)
s.Require().True(found)
valSetAfter := coordKeeper.GetConsumerValidators(coordCtx, string(consumerTmClientID), 100)
removed := true
for _, v := range valSetAfter {
if v.Address == valAddr.String() {
removed = false
}
}
s.Require().True(removed)
}