-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhandler.go
352 lines (303 loc) · 11.6 KB
/
handler.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package stake
import (
"bytes"
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
"github.com/cosmos/cosmos-sdk/x/stake/tags"
"github.com/cosmos/cosmos-sdk/x/stake/types"
)
func NewHandler(k keeper.Keeper, govKeeper gov.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
// NOTE msg already has validate basic run
switch msg := msg.(type) {
case types.MsgCreateValidatorProposal:
return handleMsgCreateValidatorAfterProposal(ctx, msg, k, govKeeper)
case types.MsgRemoveValidator:
return handleMsgRemoveValidatorAfterProposal(ctx, msg, k, govKeeper)
// disabled other msg handling
//case types.MsgEditValidator:
// return handleMsgEditValidator(ctx, msg, k)
//case types.MsgDelegate:
// return handleMsgDelegate(ctx, msg, k)
//case types.MsgBeginRedelegate:
// return handleMsgBeginRedelegate(ctx, msg, k)
//case types.MsgBeginUnbonding:
// return handleMsgBeginUnbonding(ctx, msg, k)
//case MsgSideChain
case types.MsgCreateSideChainValidator:
return handleMsgCreateSideChainValidator(ctx, msg, k)
case types.MsgEditSideChainValidator:
return handleMsgEditSideChainValidator(ctx, msg, k)
case types.MsgSideChainDelegate:
return handleMsgSideChainDelegate(ctx, msg, k)
case types.MsgSideChainRedelegate:
return handleMsgSideChainRedelegate(ctx, msg, k)
case types.MsgSideChainUndelegate:
return handleMsgSideChainUndelegate(ctx, msg, k)
default:
return sdk.ErrTxDecode("invalid message parse in staking module").Result()
}
}
}
func NewStakeHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
// NOTE msg already has validate basic run
switch msg := msg.(type) {
case types.MsgCreateValidator:
return handleMsgCreateValidator(ctx, msg, k)
case types.MsgEditValidator:
return handleMsgEditValidator(ctx, msg, k)
case types.MsgDelegate:
return handleMsgDelegate(ctx, msg, k)
case types.MsgBeginRedelegate:
return handleMsgBeginRedelegate(ctx, msg, k)
case types.MsgBeginUnbonding:
return handleMsgBeginUnbonding(ctx, msg, k)
default:
return sdk.ErrTxDecode("invalid message parse in staking module").Result()
}
}
}
//_____________________________________________________________________
// These functions assume everything has been authenticated,
// now we just perform action and save
func handleMsgCreateValidatorAfterProposal(ctx sdk.Context, msg MsgCreateValidatorProposal, k keeper.Keeper, govKeeper gov.Keeper) sdk.Result {
height := ctx.BlockHeader().Height
// do not checkProposal for the genesis txs
if height != 0 {
if err := checkCreateProposal(ctx, k, govKeeper, msg); err != nil {
return ErrInvalidProposal(k.Codespace(), err.Error()).Result()
}
}
return handleMsgCreateValidator(ctx, msg.MsgCreateValidator, k)
}
func handleMsgRemoveValidatorAfterProposal(ctx sdk.Context, msg MsgRemoveValidator, k keeper.Keeper, govKeeper gov.Keeper) sdk.Result {
if err := checkRemoveProposal(ctx, k, govKeeper, msg); err != nil {
return ErrInvalidProposal(k.Codespace(), err.Error()).Result()
}
var tags sdk.Tags
var result sdk.Result
k.IterateDelegationsToValidator(ctx, msg.ValAddr, func(del sdk.Delegation) (stop bool) {
msgBeginUnbonding := MsgBeginUnbonding{
ValidatorAddr: del.GetValidatorAddr(),
DelegatorAddr: del.GetDelegatorAddr(),
SharesAmount: del.GetShares(),
}
result = handleMsgBeginUnbonding(ctx, msgBeginUnbonding, k)
// handleMsgBeginUnbonding return error, abort execution
if !result.IsOK() {
return true
}
tags = tags.AppendTags(result.Tags)
return false
})
// If there is a failure in handling MsgBeginUnbonding, return an error
if !result.IsOK() {
return result
}
return sdk.Result{Tags: tags}
}
func handleMsgCreateValidator(ctx sdk.Context, msg MsgCreateValidator, k keeper.Keeper) sdk.Result {
// check to see if the pubkey or sender has been registered before
_, found := k.GetValidator(ctx, msg.ValidatorAddr)
if found {
return ErrValidatorOwnerExists(k.Codespace()).Result()
}
_, found = k.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(msg.PubKey))
if found {
return ErrValidatorPubKeyExists(k.Codespace()).Result()
}
if msg.Delegation.Denom != k.BondDenom(ctx) {
return ErrBadDenom(k.Codespace()).Result()
}
// self-delegate address will be used to collect fees.
feeAddr := msg.DelegatorAddr
validator := NewValidatorWithFeeAddr(feeAddr, msg.ValidatorAddr, msg.PubKey, msg.Description)
commission := NewCommissionWithTime(
msg.Commission.Rate, msg.Commission.MaxRate,
msg.Commission.MaxChangeRate, ctx.BlockHeader().Time,
)
validator, err := validator.SetInitialCommission(commission)
if err != nil {
return err.Result()
}
k.SetValidator(ctx, validator)
k.SetValidatorByConsAddr(ctx, validator)
k.SetNewValidatorByPowerIndex(ctx, validator)
k.OnValidatorCreated(ctx, validator.OperatorAddr)
// move coins from the msg.Address account to a (self-delegation) delegator account
// the validator account and global shares are updated within here
_, err = k.Delegate(ctx, msg.DelegatorAddr, msg.Delegation, validator, true)
if err != nil {
return err.Result()
}
tags := sdk.NewTags(
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
tags.Moniker, []byte(msg.Description.Moniker),
tags.Identity, []byte(msg.Description.Identity),
)
return sdk.Result{
Tags: tags,
}
}
func checkCreateProposal(ctx sdk.Context, keeper keeper.Keeper, govKeeper gov.Keeper, msg MsgCreateValidatorProposal) error {
proposal := govKeeper.GetProposal(ctx, msg.ProposalId)
if proposal == nil {
return fmt.Errorf("proposal %d does not exist", msg.ProposalId)
}
if proposal.GetProposalType() != gov.ProposalTypeCreateValidator {
return fmt.Errorf("proposal type %s is not equal to %s",
proposal.GetProposalType().String(), gov.ProposalTypeCreateValidator.String())
}
if proposal.GetStatus() != gov.StatusPassed {
return fmt.Errorf("proposal status %s is not not passed",
proposal.GetStatus().String())
}
var createValidatorJson CreateValidatorJsonMsg
err := json.Unmarshal([]byte(proposal.GetDescription()), &createValidatorJson)
if err != nil {
return fmt.Errorf("unmarshal createValidator params failed, err=%s", err.Error())
}
createValidatorMsgProposal, err := createValidatorJson.ToMsgCreateValidator()
if err != nil {
return fmt.Errorf("invalid pubkey, err=%s", err.Error())
}
if !msg.MsgCreateValidator.Equals(createValidatorMsgProposal) {
return fmt.Errorf("createValidator msg is not identical to the proposal one")
}
return nil
}
func checkRemoveProposal(ctx sdk.Context, keeper keeper.Keeper, govKeeper gov.Keeper, msg MsgRemoveValidator) error {
proposal := govKeeper.GetProposal(ctx, msg.ProposalId)
if proposal == nil {
return fmt.Errorf("proposal %d does not exist", msg.ProposalId)
}
if proposal.GetProposalType() != gov.ProposalTypeRemoveValidator {
return fmt.Errorf("proposal type %s is not equal to %s",
proposal.GetProposalType().String(), gov.ProposalTypeRemoveValidator.String())
}
if proposal.GetStatus() != gov.StatusPassed {
return fmt.Errorf("proposal status %s is not not passed",
proposal.GetStatus().String())
}
// Check proposal description
var proposalRemoveValidator MsgRemoveValidator
err := json.Unmarshal([]byte(proposal.GetDescription()), &proposalRemoveValidator)
if err != nil {
return fmt.Errorf("unmarshal removeValidator params failed, err=%s", err.Error())
}
if !msg.ValAddr.Equals(proposalRemoveValidator.ValAddr) || !msg.ValConsAddr.Equals(proposalRemoveValidator.ValConsAddr) {
return fmt.Errorf("removeValidator msg is not identical to the proposal one")
}
// Check validator information
validatorToRemove, ok := keeper.GetValidator(ctx, msg.ValAddr)
if !ok {
return fmt.Errorf("trying to remove a non-existing validator")
}
if !validatorToRemove.ConsAddress().Equals(msg.ValConsAddr) {
return fmt.Errorf("consensus address can't match actual validator consensus address")
}
// Check launcher authority
if sdk.ValAddress(msg.LauncherAddr).Equals(msg.ValAddr) {
return nil
}
// If the launcher isn't the target validator operator, then the launcher must be the operator of other active validator
launcherValidator, ok := keeper.GetValidator(ctx, sdk.ValAddress(msg.LauncherAddr))
if !ok {
return fmt.Errorf("the launcher is not a validator operator")
}
if launcherValidator.Status != sdk.Bonded {
return fmt.Errorf("the status of launcher validator is not bonded")
}
return nil
}
func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keeper.Keeper) sdk.Result {
// validator must already be registered
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
if !found {
return ErrNoValidatorFound(k.Codespace()).Result()
}
// replace all editable fields (clients should autofill existing values)
description, err := validator.Description.UpdateDescription(msg.Description)
if err != nil {
return err.Result()
}
validator.Description = description
if msg.CommissionRate != nil {
commission, err := k.UpdateValidatorCommission(ctx, validator, *msg.CommissionRate)
if err != nil {
return err.Result()
}
validator.Commission = commission
k.OnValidatorModified(ctx, msg.ValidatorAddr)
}
k.SetValidator(ctx, validator)
tags := sdk.NewTags(
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
tags.Moniker, []byte(description.Moniker),
tags.Identity, []byte(description.Identity),
)
return sdk.Result{
Tags: tags,
}
}
func handleMsgDelegate(ctx sdk.Context, msg types.MsgDelegate, k keeper.Keeper) sdk.Result {
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
if !found {
return ErrNoValidatorFound(k.Codespace()).Result()
}
if msg.Delegation.Denom != k.BondDenom(ctx) {
return ErrBadDenom(k.Codespace()).Result()
}
if bytes.Equal(msg.DelegatorAddr.Bytes(), validator.OperatorAddr.Bytes()) {
// if validator uses a different self-delegator address, the operator address is not allowed to delegate to itself.
if !bytes.Equal(validator.OperatorAddr.Bytes(), validator.FeeAddr.Bytes()) {
return ErrInvalidDelegator(k.Codespace()).Result()
}
}
if validator.Jailed && !bytes.Equal(validator.FeeAddr, msg.DelegatorAddr) {
return ErrValidatorJailed(k.Codespace()).Result()
}
_, err := k.Delegate(ctx, msg.DelegatorAddr, msg.Delegation, validator, true)
if err != nil {
return err.Result()
}
tags := sdk.NewTags(
tags.Delegator, []byte(msg.DelegatorAddr.String()),
tags.DstValidator, []byte(msg.ValidatorAddr.String()),
)
return sdk.Result{
Tags: tags,
}
}
func handleMsgBeginUnbonding(ctx sdk.Context, msg types.MsgBeginUnbonding, k keeper.Keeper) sdk.Result {
ubd, err := k.BeginUnbonding(ctx, msg.DelegatorAddr, msg.ValidatorAddr, msg.SharesAmount)
if err != nil {
return err.Result()
}
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(ubd.MinTime)
tags := sdk.NewTags(
tags.Delegator, []byte(msg.DelegatorAddr.String()),
tags.SrcValidator, []byte(msg.ValidatorAddr.String()),
tags.EndTime, finishTime,
)
return sdk.Result{Data: finishTime, Tags: tags}
}
func handleMsgBeginRedelegate(ctx sdk.Context, msg types.MsgBeginRedelegate, k keeper.Keeper) sdk.Result {
red, err := k.BeginRedelegation(ctx, msg.DelegatorAddr, msg.ValidatorSrcAddr,
msg.ValidatorDstAddr, msg.SharesAmount)
if err != nil {
return err.Result()
}
finishTime := types.MsgCdc.MustMarshalBinaryLengthPrefixed(red.MinTime)
tags := sdk.NewTags(
tags.Delegator, []byte(msg.DelegatorAddr.String()),
tags.SrcValidator, []byte(msg.ValidatorSrcAddr.String()),
tags.DstValidator, []byte(msg.ValidatorDstAddr.String()),
tags.EndTime, finishTime,
)
return sdk.Result{Data: finishTime, Tags: tags}
}