-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathhooks.go
219 lines (181 loc) · 7.69 KB
/
hooks.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkgov "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
providertypes "github.com/cosmos/interchain-security/v4/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types"
)
// Wrapper struct
type Hooks struct {
k *Keeper
}
var (
_ stakingtypes.StakingHooks = Hooks{}
_ sdkgov.GovHooks = Hooks{}
)
// Returns new provider hooks
func (k *Keeper) Hooks() Hooks {
return Hooks{k}
}
//
// staking hooks
//
// This stores a record of each unbonding op from staking, allowing us to track which consumer chains have unbonded
func (h Hooks) AfterUnbondingInitiated(ctx sdk.Context, id uint64) error {
var consumerChainIDS []string
for _, chain := range h.k.GetAllConsumerChains(ctx) {
consumerChainIDS = append(consumerChainIDS, chain.ChainId)
}
if len(consumerChainIDS) == 0 {
// Do not put the unbonding op on hold if there are no consumer chains
return nil
}
// Call back into staking to tell it to stop this op from unbonding when the unbonding period is complete
if err := h.k.stakingKeeper.PutUnbondingOnHold(ctx, id); err != nil {
// Note: that in the case of a validator unbonding, AfterUnbondingInitiated is called
// from staking.EndBlock.
// In this case PutUnbondingOnHold fails if either the unbonding operation was
// not found or the UnbondingOnHoldRefCount is negative.
// This change should be updated for SDK v0.48 because it will include changes in handling
// check: https://github.com/cosmos/cosmos-sdk/pull/16043
ctx.Logger().Error("unbonding could not be put on hold: %w", err)
return nil
}
valsetUpdateID := h.k.GetValidatorSetUpdateId(ctx)
unbondingOp := providertypes.UnbondingOp{
Id: id,
UnbondingConsumerChains: consumerChainIDS,
}
// Add to indexes
for _, consumerChainID := range consumerChainIDS {
index, _ := h.k.GetUnbondingOpIndex(ctx, consumerChainID, valsetUpdateID)
index = append(index, id)
h.k.SetUnbondingOpIndex(ctx, consumerChainID, valsetUpdateID, index)
}
h.k.SetUnbondingOp(ctx, unbondingOp)
// NOTE: This is a temporary fix for v0.47 -> we should not panic in this edge case
// since the AfterUnbondInitiatedHook can be called with a non-existing UnbondingEntry.id
// check: https://github.com/cosmos/cosmos-sdk/pull/16043
//
// Call back into staking to tell it to stop this op from unbonding when the unbonding period is complete
// if err := h.k.stakingKeeper.PutUnbondingOnHold(ctx, id); err != nil {
// // If there was an error putting the unbonding on hold, panic to end execution for
// // the current tx and prevent committal of this invalid state.
// //
// // Note: that in the case of a validator unbonding, AfterUnbondingInitiated is called
// // from staking.EndBlock, thus the following panic would halt the chain.
// // In this case PutUnbondingOnHold fails if either the unbonding operation was
// // not found or the UnbondingOnHoldRefCount is negative. In either cases,
// // the state of the x/staking module of cosmos-sdk is invalid.
// panic(fmt.Errorf("unbonding could not be put on hold: %w", err))
// }
return nil
}
func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) error {
if h.k.ValidatorConsensusKeyInUse(ctx, valAddr) {
// Abort TX, do NOT allow validator to be created
panic("cannot create a validator with a consensus key that is already in use or was recently in use as an assigned consumer chain key")
}
return nil
}
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, valConsAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
for _, validatorConsumerPubKey := range h.k.GetAllValidatorConsumerPubKeys(ctx, nil) {
if sdk.ConsAddress(validatorConsumerPubKey.ProviderAddr).Equals(valConsAddr) {
consumerAddrTmp, err := ccvtypes.TMCryptoPublicKeyToConsAddr(*validatorConsumerPubKey.ConsumerKey)
if err != nil {
// An error here would indicate something is very wrong
panic("cannot get address of consumer key")
}
consumerAddr := providertypes.NewConsumerConsAddress(consumerAddrTmp)
h.k.DeleteValidatorByConsumerAddr(ctx, validatorConsumerPubKey.ChainId, consumerAddr)
providerAddr := providertypes.NewProviderConsAddress(validatorConsumerPubKey.ProviderAddr)
h.k.DeleteValidatorConsumerPubKey(ctx, validatorConsumerPubKey.ChainId, providerAddr)
}
}
return nil
}
func (h Hooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) error {
return nil
}
func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
//
// gov hooks
//
// AfterProposalSubmission - call hook if registered
// After a consumerAddition proposal submission, a record is created
// that maps the proposal ID to the consumer chain ID.
func (h Hooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) {
if p, ok := h.GetConsumerAdditionLegacyPropFromProp(ctx, proposalID); ok {
h.k.SetProposedConsumerChain(ctx, p.ChainId, proposalID)
}
}
// AfterProposalVotingPeriodEnded - call hook if registered
// After proposal voting ends, the consumer chainID in store is deleted.
// When a consumerAddition proposal passes, the consumer chainID is available in providerKeeper.GetAllPendingConsumerAdditionProps
// or providerKeeper.GetAllConsumerChains(ctx).
func (h Hooks) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) {
if _, ok := h.GetConsumerAdditionLegacyPropFromProp(ctx, proposalID); ok {
h.k.DeleteProposedConsumerChainInStore(ctx, proposalID)
}
}
func (h Hooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) {
}
func (h Hooks) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) {
}
func (h Hooks) AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) {
}
// GetConsumerAdditionLegacyPropFromProp extracts a consumer addition legacy proposal from
// the proposal with the given ID
func (h Hooks) GetConsumerAdditionLegacyPropFromProp(
ctx sdk.Context,
proposalID uint64,
) (providertypes.ConsumerAdditionProposal, bool) {
p, ok := h.k.govKeeper.GetProposal(ctx, proposalID)
if !ok {
panic(fmt.Errorf("failed to get proposal %d from store", proposalID))
}
// Iterate over the messages in the proposal
// Note that it's assumed that at most ONE message can contain a consumer addition proposal
for _, msg := range p.GetMessages() {
sdkMsg, isLegacyProposal := msg.GetCachedValue().(*v1.MsgExecLegacyContent)
if !isLegacyProposal {
continue
}
content, err := v1.LegacyContentFromMessage(sdkMsg)
if err != nil {
panic(fmt.Errorf("failed to get legacy proposal %d from prop message", proposalID))
}
// returns if legacy prop is of ConsumerAddition proposal type
prop, ok := content.(*providertypes.ConsumerAdditionProposal)
if ok {
return *prop, true
}
}
return providertypes.ConsumerAdditionProposal{}, false
}
func (h Hooks) BeforeTokenizeShareRecordRemoved(_ sdk.Context, _ uint64) error {
return nil
}