-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmsg_server.go
More file actions
441 lines (365 loc) · 15.1 KB
/
msg_server.go
File metadata and controls
441 lines (365 loc) · 15.1 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package keeper
import (
"context"
"fmt"
"cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkgovtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/atomone-hub/atomone/x/coredaos/types"
govtypes "github.com/atomone-hub/atomone/x/gov/types"
govtypesv1 "github.com/atomone-hub/atomone/x/gov/types/v1"
)
var _ types.MsgServer = (*MsgServer)(nil)
type MsgServer struct {
k *Keeper
}
// NewMsgServer returns the MsgServer implementation.
func NewMsgServer(k *Keeper) types.MsgServer {
return &MsgServer{k: k}
}
// UpdateParams defines a method that updates the module's parameters. The signer of the message must
// be the module authority.
func (ms MsgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if ms.k.GetAuthority() != msg.Authority {
return nil, types.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", ms.k.GetAuthority(), msg.Authority)
}
params := msg.Params
// check if any of the core DAOs has bonded or unbonding tokens, and if so return an error
if params.SteeringDaoAddress != "" {
delegatorBonded, err := ms.k.stakingKeeper.GetDelegatorBonded(ctx, sdk.MustAccAddressFromBech32(params.SteeringDaoAddress))
if err != nil {
return nil, err
}
delegatorUnbonded, err := ms.k.stakingKeeper.GetDelegatorUnbonding(ctx, sdk.MustAccAddressFromBech32(params.SteeringDaoAddress))
if err != nil {
return nil, err
}
if delegatorBonded.GT(math.ZeroInt()) ||
delegatorUnbonded.GT(math.ZeroInt()) {
return nil, types.ErrCannotStake.Wrapf("cannot update params while Steering DAO have bonded or unbonding tokens")
}
}
if params.OversightDaoAddress != "" {
delegatorBonded, err := ms.k.stakingKeeper.GetDelegatorBonded(ctx, sdk.MustAccAddressFromBech32(params.OversightDaoAddress))
if err != nil {
return nil, err
}
delegatorUnbonded, err := ms.k.stakingKeeper.GetDelegatorUnbonding(ctx, sdk.MustAccAddressFromBech32(params.OversightDaoAddress))
if err != nil {
return nil, err
}
if delegatorBonded.GT(math.ZeroInt()) ||
delegatorUnbonded.GT(math.ZeroInt()) {
return nil, types.ErrCannotStake.Wrapf("cannot update params while Oversight DAO have bonded or unbonding tokens")
}
}
if err := ms.k.Params.Set(ctx, params); err != nil {
return nil, errors.Wrapf(err, "error setting params")
}
return &types.MsgUpdateParamsResponse{}, nil
}
// AnnotateProposal adds an annotation to the proposal with the given ID.
// The annotation is a string that can be used to provide additional context or information about the proposal.
// The proposal must be in the voting period for the annotation to be added.
// It is only available to the Steering DAO.
func (ms MsgServer) AnnotateProposal(goCtx context.Context, msg *types.MsgAnnotateProposal) (*types.MsgAnnotateProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := ms.k.GetParams(ctx)
logger := ms.k.Logger(ctx)
if params.SteeringDaoAddress == "" {
logger.Info("Steering DAO address is not set, function is disabled")
return nil, types.ErrFunctionDisabled.Wrapf("Steering DAO address is not set")
}
if msg.Annotator != params.SteeringDaoAddress {
logger.Error(
"invalid authority for annotating proposal",
"expected", params.SteeringDaoAddress,
"got", msg.Annotator,
)
return nil, types.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", params.SteeringDaoAddress, msg.Annotator)
}
proposal, found := ms.k.govKeeper.GetProposal(ctx, msg.ProposalId)
if !found {
logger.Error(
"proposal not found",
"proposal_id", msg.ProposalId,
"authority", msg.Annotator,
)
return nil, govtypes.ErrUnknownProposal.Wrapf("proposal with ID %d not found", msg.ProposalId)
}
if proposal.Status != govtypesv1.StatusVotingPeriod {
logger.Error(
"proposal is not in voting period",
"proposal", proposal.Id,
"status", proposal.Status,
"authority", msg.Annotator,
)
return nil, sdkgovtypes.ErrInactiveProposal.Wrapf("proposal with ID %d is not in voting period", msg.ProposalId)
}
// Check if the proposal already has an annotation, if so, allow overwriting only if the `overwrite` flag is set to true.
if proposal.Annotation != "" && !msg.Overwrite {
logger.Error(
"proposal already has an annotation and overwrite is set to false",
"proposal", proposal.Id,
"authority", msg.Annotator,
)
return nil, types.ErrAnnotationAlreadyPresent.Wrapf("proposal with ID %d already has an annotation", msg.ProposalId)
}
proposal.Annotation = msg.Annotation
ms.k.govKeeper.SetProposal(ctx, proposal)
logger.Info(
"proposal annotated",
"proposal", proposal.Id,
"authority", msg.Annotator,
)
// Emit event for proposal annotation
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAnnotateProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.Id)),
sdk.NewAttribute(types.AttributeKeySigner, msg.Annotator),
),
})
return &types.MsgAnnotateProposalResponse{}, nil
}
// EndorseProposal allows the Steering DAO to endorse a proposal.
// It requires the proposal to be in the voting period, and the endorsing account must be the Steering DAO.
// A proposal can only be endorsed once.
func (ms MsgServer) EndorseProposal(goCtx context.Context, msg *types.MsgEndorseProposal) (*types.MsgEndorseProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := ms.k.GetParams(ctx)
logger := ms.k.Logger(ctx)
if params.SteeringDaoAddress == "" {
logger.Info("Steering DAO address is not set, function is disabled")
return nil, types.ErrFunctionDisabled.Wrapf("Steering DAO address is not set")
}
if msg.Endorser != params.SteeringDaoAddress {
logger.Error(
"invalid authority for endorsing proposal",
"expected", params.SteeringDaoAddress,
"got", msg.Endorser,
)
return nil, types.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", params.SteeringDaoAddress, msg.Endorser)
}
proposal, found := ms.k.govKeeper.GetProposal(ctx, msg.ProposalId)
if !found {
logger.Error(
"proposal not found",
"proposal_id", msg.ProposalId,
"authority", msg.Endorser,
)
return nil, govtypes.ErrUnknownProposal.Wrapf("proposal with ID %d not found", msg.ProposalId)
}
if proposal.Status != govtypesv1.StatusVotingPeriod {
logger.Error(
"proposal is not in voting period",
"proposal", proposal.Id,
"status", proposal.Status,
"authority", msg.Endorser,
)
return nil, sdkgovtypes.ErrInactiveProposal.Wrapf("proposal with ID %d is not in voting period", msg.ProposalId)
}
if proposal.Endorsed {
logger.Error(
"proposal has already been endorsed",
"proposal", proposal.Id,
"authority", msg.Endorser,
)
return nil, types.ErrProposalAlreadyEndorsed.Wrapf("proposal with ID %d has already been endorsed", msg.ProposalId)
}
proposal.Endorsed = true
ms.k.govKeeper.SetProposal(ctx, proposal)
logger.Info(
"proposal endorsed",
"proposal", proposal.Id,
"authority", msg.Endorser,
)
// Emit event for proposal endorsement
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEndorseProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.Id)),
sdk.NewAttribute(types.AttributeKeySigner, msg.Endorser),
),
})
return &types.MsgEndorseProposalResponse{}, nil
}
// ExtendVotingPeriod allows the signer to extend the voting period of a proposal.
// The proposal must be in the voting period, and the signer must be the designated
// Steering DAO or Oversight DAO.
// The voting period cannot be extended further than the maximum defined in the module parameters.
// The extension duration is defined in the module parameters.
func (ms MsgServer) ExtendVotingPeriod(goCtx context.Context, msg *types.MsgExtendVotingPeriod) (*types.MsgExtendVotingPeriodResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := ms.k.GetParams(ctx)
logger := ms.k.Logger(ctx)
if params.SteeringDaoAddress == "" && params.OversightDaoAddress == "" {
logger.Info("Steering DAO address and Oversight DAO address are not set, function is disabled")
return nil, types.ErrFunctionDisabled.Wrapf("Steering DAO address and Oversight DAO address are not set")
}
if msg.Extender != params.SteeringDaoAddress && msg.Extender != params.OversightDaoAddress {
// one of the two addresses must be set otherwise it would have been caught earlier
addressesString := fmt.Sprintf("%s or %s", params.SteeringDaoAddress, params.OversightDaoAddress)
if params.SteeringDaoAddress == "" {
addressesString = params.OversightDaoAddress
} else if params.OversightDaoAddress == "" {
addressesString = params.SteeringDaoAddress
}
logger.Error(
"invalid authority for extending voting period",
"expected", addressesString,
"got", msg.Extender,
)
return nil, types.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", addressesString, msg.Extender)
}
proposal, found := ms.k.govKeeper.GetProposal(ctx, msg.ProposalId)
if !found {
logger.Error(
"proposal not found",
"proposal_id", msg.ProposalId,
"authority", msg.Extender,
)
return nil, govtypes.ErrUnknownProposal.Wrapf("proposal with ID %d not found", msg.ProposalId)
}
if proposal.Status != govtypesv1.StatusVotingPeriod {
logger.Error(
"proposal is not in voting period",
"proposal", proposal.Id,
"status", proposal.Status,
"authority", msg.Extender,
)
return nil, sdkgovtypes.ErrInactiveProposal.Wrapf("proposal with ID %d is not in voting period", msg.ProposalId)
}
if proposal.TimesVotingPeriodExtended >= params.VotingPeriodExtensionsLimit {
logger.Error(
"proposal has reached the maximum number of voting period extensions",
"proposal", proposal.Id,
"times_extended", proposal.TimesVotingPeriodExtended,
"authority", msg.Extender,
)
return nil, sdkgovtypes.ErrInvalidProposalContent.Wrapf("proposal with ID %d has reached the maximum number of voting period extensions", msg.ProposalId)
}
newEndTime := proposal.VotingEndTime.Add(*params.VotingPeriodExtensionDuration)
// Update ActiveProposalsQueue with new VotingEndTime
ms.k.govKeeper.RemoveFromActiveProposalQueue(ctx, proposal.Id, *proposal.VotingEndTime)
proposal.VotingEndTime = &newEndTime
ms.k.govKeeper.InsertActiveProposalQueue(ctx, proposal.Id, *proposal.VotingEndTime)
proposal.TimesVotingPeriodExtended++
ms.k.govKeeper.SetProposal(ctx, proposal)
logger.Info(
"voting period extended",
"proposal", proposal.Id,
"new_end_time", proposal.VotingEndTime,
"authority", msg.Extender,
"times_extended", proposal.TimesVotingPeriodExtended,
)
// Emit event for voting period extension
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeExtendVotingPeriod,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.Id)),
sdk.NewAttribute(types.AttributeKeySigner, msg.Extender),
sdk.NewAttribute(types.AttributeKeyNewEndTime, proposal.VotingEndTime.String()),
sdk.NewAttribute(types.AttributeKeyTimesExtended, fmt.Sprintf("%d", proposal.TimesVotingPeriodExtended)),
),
})
return &types.MsgExtendVotingPeriodResponse{}, nil
}
// VetoProposal allows the signer to veto a proposal.
// The proposal must be in the voting period, and the signer must be the designated Oversight DAO.
// If the proposal is vetoed, it will be removed from the active proposals queue and rejected.
func (ms MsgServer) VetoProposal(goCtx context.Context, msg *types.MsgVetoProposal) (*types.MsgVetoProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := ms.k.GetParams(ctx)
logger := ms.k.Logger(ctx)
if params.OversightDaoAddress == "" {
logger.Info("Oversight DAO address is not set, function is disabled")
return nil, types.ErrFunctionDisabled.Wrapf("Oversight DAO address is not set")
}
if msg.Vetoer != params.OversightDaoAddress {
logger.Error(
"invalid authority for vetoing proposal",
"expected", params.OversightDaoAddress,
"got", msg.Vetoer,
)
return nil, types.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", params.OversightDaoAddress, msg.Vetoer)
}
proposal, found := ms.k.govKeeper.GetProposal(ctx, msg.ProposalId)
if !found {
logger.Error(
"proposal not found",
"proposal_id", msg.ProposalId,
"authority", msg.Vetoer,
)
return nil, govtypes.ErrUnknownProposal.Wrapf("proposal with ID %d not found", msg.ProposalId)
}
if proposal.Status != govtypesv1.StatusVotingPeriod {
logger.Error(
"proposal is not in voting period",
"proposal", proposal.Id,
"status", proposal.Status,
"authority", msg.Vetoer,
)
return nil, sdkgovtypes.ErrInactiveProposal.Wrapf("proposal with ID %d is not in voting period", msg.ProposalId)
}
// Check if the proposal contains a change of the oversight DAO address.
// If so, vetoing the proposal would create a scenario where the current oversight DAO can prevent its own replacement
for _, msg := range proposal.Messages {
if msg.GetTypeUrl() == sdk.MsgTypeURL(&types.MsgUpdateParams{}) {
var updateParamsMsg types.MsgUpdateParams
if err := updateParamsMsg.Unmarshal(msg.GetValue()); err != nil {
logger.Error(
"failed to unmarshal MsgUpdateParams from proposal messages",
"proposal", proposal.Id,
"error", err,
)
return nil, err
}
if updateParamsMsg.Params.OversightDaoAddress != params.OversightDaoAddress {
logger.Error(
"proposal contains a change of the oversight DAO address, vetoing it would prevent the replacement of the current oversight DAO",
"proposal", proposal.Id,
"current_oversight_dao_address", params.OversightDaoAddress,
"new_oversight_dao_address", updateParamsMsg.Params.OversightDaoAddress,
)
return nil, types.ErrInvalidVeto.Wrapf("proposal with ID %d contains a change of the oversight DAO address, vetoing it would prevent the replacement of the current oversight DAO", proposal.Id)
}
}
}
// follows the same logic as in x/gov/abci.go for rejected proposals
if msg.BurnDeposit {
ms.k.govKeeper.DeleteAndBurnDeposits(ctx, proposal.Id)
} else {
ms.k.govKeeper.RefundAndDeleteDeposits(ctx, proposal.Id)
}
proposal.Status = govtypesv1.StatusVetoed
// Since the proposal is vetoed, we set the final tally result to an empty tally
// and the voting period ends immediately
emptyTally := govtypesv1.EmptyTallyResult()
proposal.FinalTallyResult = &emptyTally
origEndTime := proposal.VotingEndTime
blockTime := ctx.BlockTime()
proposal.VotingEndTime = &blockTime
ms.k.govKeeper.SetProposal(ctx, proposal)
ms.k.govKeeper.DeleteVotes(ctx, proposal.Id)
ms.k.govKeeper.RemoveFromActiveProposalQueue(ctx, proposal.Id, *origEndTime)
ms.k.govKeeper.UpdateMinInitialDeposit(ctx, true)
ms.k.govKeeper.UpdateMinDeposit(ctx, true)
logger.Info(
"proposal vetoed",
"proposal", proposal.Id,
"authority", msg.Vetoer,
)
// Emit event for proposal veto
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeVetoProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.Id)),
sdk.NewAttribute(types.AttributeKeySigner, msg.Vetoer),
sdk.NewAttribute(sdkgovtypes.AttributeKeyProposalResult, types.AttributeValueProposalVetoed),
),
})
return &types.MsgVetoProposalResponse{}, nil
}