-
Notifications
You must be signed in to change notification settings - Fork 105
/
hooks.go
220 lines (178 loc) · 7.13 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
220
package keeper
import (
"context"
"fmt"
"cosmossdk.io/math"
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/v5/x/ccv/provider/types"
ccvtypes "github.com/cosmos/interchain-security/v5/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(goCtx context.Context, id uint64) error {
var consumerChainIDS []string
ctx := sdk.UnwrapSDKContext(goCtx)
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
}
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)
// 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(goCtx context.Context, valAddr sdk.ValAddress) error {
ctx := sdk.UnwrapSDKContext(goCtx)
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(goCtx context.Context, valConsAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
ctx := sdk.UnwrapSDKContext(goCtx)
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(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationSharesModified(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterDelegationModified(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeValidatorSlashed(_ context.Context, _ sdk.ValAddress, _ math.LegacyDec) error {
return nil
}
func (h Hooks) BeforeValidatorModified(_ context.Context, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBonded(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBeginUnbonding(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationRemoved(_ context.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(goCtx context.Context, proposalID uint64) error {
ctx := sdk.UnwrapSDKContext(goCtx)
if p, ok := h.GetConsumerAdditionLegacyPropFromProp(ctx, proposalID); ok {
h.k.SetProposedConsumerChain(ctx, p.ChainId, proposalID)
}
return nil
}
// 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(goCtx context.Context, proposalID uint64) error {
ctx := sdk.UnwrapSDKContext(goCtx)
if _, ok := h.GetConsumerAdditionLegacyPropFromProp(ctx, proposalID); ok {
h.k.DeleteProposedConsumerChainInStore(ctx, proposalID)
}
return nil
}
func (h Hooks) AfterProposalDeposit(ctx context.Context, proposalID uint64, depositorAddr sdk.AccAddress) error {
return nil
}
func (h Hooks) AfterProposalVote(ctx context.Context, proposalID uint64, voterAddr sdk.AccAddress) error {
return nil
}
func (h Hooks) AfterProposalFailedMinDeposit(ctx context.Context, proposalID uint64) error {
return nil
}
// 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, err := h.k.govKeeper.Proposals.Get(ctx, proposalID)
if err != nil {
return providertypes.ConsumerAdditionProposal{}, false
}
// 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
}