forked from lumtis/poa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
380 lines (333 loc) · 12 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package poa
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ltacker/poa/keeper"
"github.com/ltacker/poa/types"
)
// NewHandler creates an sdk.Handler for all the poa type messages
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgSubmitApplication:
return handleMsgSubmitApplication(ctx, k, msg)
case types.MsgVote:
return handleMsgVote(ctx, k, msg)
case types.MsgProposeKick:
return handleMsgProposeKick(ctx, k, msg)
case types.MsgLeaveValidatorSet:
return handleMsgLeaveValidatorSet(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
// handleMsgSubmitApplication create a new application to become a validator
func handleMsgSubmitApplication(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitApplication) (*sdk.Result, error) {
// Check max validator is not reached
allValidators := k.GetAllValidators(ctx)
maxValidator := k.MaxValidators(ctx)
if uint16(len(allValidators)) == maxValidator {
return nil, types.ErrMaxValidatorsReached
}
// Candidate should not be a validator
_, found := k.GetValidator(ctx, msg.Candidate.GetOperator())
if found {
return nil, types.ErrAlreadyValidator
}
_, found = k.GetValidatorByConsAddr(ctx, msg.Candidate.GetConsAddr())
if found {
return nil, types.ErrAlreadyValidator
}
// If quorum is 0 the application is immediately approved
if k.Quorum(ctx) == 0 {
// The validator is directly appended in the validator set
k.AppendValidator(ctx, msg.Candidate)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeAppendValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.Candidate.GetOperator().String()),
),
)
} else {
// If quorum is more than 0, we create a application vote
// Candidate should not be already applying
_, found = k.GetApplication(ctx, msg.Candidate.GetOperator())
if found {
return nil, types.ErrAlreadyApplying
}
_, found = k.GetApplicationByConsAddr(ctx, msg.Candidate.GetConsAddr())
if found {
return nil, types.ErrAlreadyApplying
}
// Create the new application
k.AppendApplication(ctx, msg.Candidate)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitApplication,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.Candidate.GetOperator().String()),
),
)
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
// handleMsgProposeKick creates a new vote in the kick proposal pools if all conditions are met
func handleMsgProposeKick(ctx sdk.Context, k keeper.Keeper, msg types.MsgProposeKick) (*sdk.Result, error) {
// The candidate of the kick proposal can't be the proposer
if msg.ProposerAddr.Equals(msg.CandidateAddr) {
return nil, types.ErrProposerIsCandidate
}
// The proposer must be a validator
_, found := k.GetValidator(ctx, msg.ProposerAddr)
if !found {
return nil, types.ErrProposerNotValidator
}
// Candidate should be a validator
candidate, found := k.GetValidator(ctx, msg.CandidateAddr)
if !found {
return nil, types.ErrNotValidator
}
// Can't create a kick proposal if the candidate is leaving the validator set
valState, found := k.GetValidatorState(ctx, msg.CandidateAddr)
if !found {
panic("A validator has no state")
}
if valState == types.ValidatorStateLeaving {
return nil, types.ErrValidatorLeaving
}
// If quorum is 0 the candidate is immediatelly kicked from the validator set
if k.Quorum(ctx) == 0 {
// We set the validator state to leaving, the End Blocker will update the keeper
k.SetValidatorState(ctx, candidate, types.ValidatorStateLeaving)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeKickValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
),
)
} else {
// If quorum is more than 0, we create a kick proposal vote
// Candidate should not be already in a kick proposal
_, found = k.GetKickProposal(ctx, msg.CandidateAddr)
if found {
return nil, types.ErrAlreadyInKickProposal
}
// Create the new application
k.AppendKickProposal(ctx, candidate)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeProposeKick,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
sdk.NewAttribute(types.AttributeKeyProposer, msg.ProposerAddr.String()),
),
)
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
// handleMsgVote handles a vote performed by a validator
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) (*sdk.Result, error) {
switch msg.VoteType {
case types.VoteTypeApplication:
return handleMsgVoteApplication(ctx, k, msg)
case types.VoteTypeKickProposal:
return handleMsgVoteTypeKickProposal(ctx, k, msg)
default:
return nil, types.ErrInvalidVoteMsg
}
}
func handleMsgVoteApplication(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) (*sdk.Result, error) {
// Check max validator is not reached. If max validator is reached, not application can be voted
allValidators := k.GetAllValidators(ctx)
validatorCount := len(allValidators)
maxValidator := k.MaxValidators(ctx)
if uint16(validatorCount) == maxValidator {
return nil, types.ErrMaxValidatorsReached
}
// The voter must be a validator
_, found := k.GetValidator(ctx, msg.VoterAddr)
if !found {
return nil, types.ErrVoterNotValidator
}
// Check the application exist
application, found := k.GetApplication(ctx, msg.CandidateAddr)
if !found {
return nil, types.ErrNoApplicationFound
}
// Check if already voted and vote
alreadyVoted := application.AddVote(msg.VoterAddr, msg.Approve)
if alreadyVoted {
return nil, types.ErrAlreadyVoted
}
// Emit the vote event
if msg.Approve {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeApproveApplication,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyVoter, msg.VoterAddr.String()),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.CandidateAddr.String()),
),
)
} else {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRejectApplication,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyVoter, msg.VoterAddr.String()),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.CandidateAddr.String()),
),
)
}
// Check if the quorum has been reached
reached, approved, err := application.CheckQuorum(uint64(validatorCount), uint64(k.Quorum(ctx)))
if err != nil {
return nil, err
}
if reached {
if approved {
// Candidate is appended to the validator set
k.RemoveApplication(ctx, msg.CandidateAddr)
k.AppendValidator(ctx, application.GetSubject())
// Emit approved event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeAppendValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.CandidateAddr.String()),
),
)
} else {
// Candidate is rejected from joining the validator set
k.RemoveApplication(ctx, msg.CandidateAddr)
// Emit rejected event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRejectValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyCandidate, msg.CandidateAddr.String()),
),
)
}
} else {
// Quorum has not been reached yet, update the vote
k.SetApplication(ctx, application)
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgVoteTypeKickProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) (*sdk.Result, error) {
// The candidate of the kick proposal can't be the voter
if msg.VoterAddr.Equals(msg.CandidateAddr) {
return nil, types.ErrVoterIsCandidate
}
// The voter must be a validator
_, found := k.GetValidator(ctx, msg.VoterAddr)
if !found {
return nil, types.ErrVoterNotValidator
}
// Check the kick proposal exist
kickProposal, found := k.GetKickProposal(ctx, msg.CandidateAddr)
if !found {
return nil, types.ErrNoKickProposalFound
}
// Check if already voted and vote
alreadyVoted := kickProposal.AddVote(msg.VoterAddr, msg.Approve)
if alreadyVoted {
return nil, types.ErrAlreadyVoted
}
// Emit the vote event
if msg.Approve {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeApproveKickProposal,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyVoter, msg.VoterAddr.String()),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
),
)
} else {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRejectKickProposal,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyVoter, msg.VoterAddr.String()),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
),
)
}
// Get validator count
allValidators := k.GetAllValidators(ctx)
validatorCount := len(allValidators)
// Check if the quorum has been reached
// We decrement validator count, the candidate of the kick proposal cannot vote
reached, approved, err := kickProposal.CheckQuorum(uint64(validatorCount)-1, uint64(k.Quorum(ctx)))
if err != nil {
return nil, err
}
if reached {
if approved {
// The validator leave the validator set
// The state is set to leave, End Blocker will remove definitely the validator
k.RemoveKickProposal(ctx, msg.CandidateAddr)
k.SetValidatorState(ctx, kickProposal.GetSubject(), types.ValidatorStateLeaving)
// Emit approved event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeKickValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
),
)
} else {
// Kick proposal rejected, validator is not removed
k.RemoveKickProposal(ctx, msg.CandidateAddr)
// Emit rejected event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeKeepValidator,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidator, msg.CandidateAddr.String()),
),
)
}
} else {
// Quorum has not been reached yet, update the vote
k.SetKickProposal(ctx, kickProposal)
}
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgLeaveValidatorSet(ctx sdk.Context, k keeper.Keeper, msg types.MsgLeaveValidatorSet) (*sdk.Result, error) {
// Sender must be a validator
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
if !found {
return nil, types.ErrNotValidator
}
// Get validator count
allValidators := k.GetAllValidators(ctx)
validatorCount := len(allValidators)
if validatorCount == 1 {
return nil, types.ErrOnlyOneValidator
}
// If a kick proposal exist for this validator, remove it
_, found = k.GetKickProposal(ctx, msg.ValidatorAddr)
if found {
k.RemoveKickProposal(ctx, msg.ValidatorAddr)
}
// Set the state of the validator to leaving, End Blocker will remove the validator from the keeper
k.SetValidatorState(ctx, validator, types.ValidatorStateLeaving)
// Emit approved event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeLeaveValidatorSet,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyValidator, msg.ValidatorAddr.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}