-
Notifications
You must be signed in to change notification settings - Fork 112
/
proposal_handler.go
442 lines (364 loc) · 14.2 KB
/
proposal_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
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
442
package client
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/interchain-security/v3/x/ccv/provider/types"
)
var (
ConsumerAdditionProposalHandler = govclient.NewProposalHandler(SubmitConsumerAdditionPropTxCmd)
ConsumerRemovalProposalHandler = govclient.NewProposalHandler(SubmitConsumerRemovalProposalTxCmd)
ChangeRewardDenomsProposalHandler = govclient.NewProposalHandler(SubmitChangeRewardDenomsProposalTxCmd)
)
// SubmitConsumerAdditionPropTxCmd returns a CLI command handler for submitting
// a consumer addition proposal via a transaction.
func SubmitConsumerAdditionPropTxCmd() *cobra.Command {
return &cobra.Command{
Use: "consumer-addition [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a consumer addition proposal",
Long: `
Submit a consumer addition proposal along with an initial deposit.
The proposal details must be supplied via a JSON file.
Unbonding period, transfer timeout period and ccv timeout period should be provided as nanosecond time periods.
Example:
$ <appd> tx gov submit-legacy-proposal consumer-addition <path/to/proposal.json> --from=<key_or_address>
Where proposal.json contains:
{
"title": "Create the FooChain",
"summary": "Gonna be a great chain",
"chain_id": "foochain",
"initial_height": {
"revision_number": 2,
"revision_height": 3
},
"genesis_hash": "Z2VuZXNpcyBoYXNo",
"binary_hash": "YmluYXJ5IGhhc2g=",
"spawn_time": "2022-01-27T15:59:50.121607-08:00",
"blocks_per_distribution_transmission": 1000,
"consumer_redistribution_fraction": "0.75",
"distribution_transmission_channel": "",
"historical_entries": 10000,
"transfer_timeout_period": 3600000000000,
"ccv_timeout_period": 2419200000000000,
"unbonding_period": 1728000000000000,
"deposit": "10000stake"
}
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := ParseConsumerAdditionProposalJSON(args[0])
if err != nil {
return err
}
// do not fail for errors regarding the unbonding period, but just log a warning
CheckPropUnbondingPeriod(clientCtx, proposal.UnbondingPeriod)
content := types.NewConsumerAdditionProposal(
proposal.Title, proposal.Summary, proposal.ChainId, proposal.InitialHeight,
proposal.GenesisHash, proposal.BinaryHash, proposal.SpawnTime,
proposal.ConsumerRedistributionFraction, proposal.BlocksPerDistributionTransmission,
proposal.DistributionTransmissionChannel, proposal.HistoricalEntries,
proposal.CcvTimeoutPeriod, proposal.TransferTimeoutPeriod, proposal.UnbondingPeriod)
from := clientCtx.GetFromAddress()
deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return err
}
msgContent, err := govv1.NewLegacyContent(content, authtypes.NewModuleAddress(govtypes.ModuleName).String())
if err != nil {
return err
}
msg, err := govv1.NewMsgSubmitProposal([]sdk.Msg{msgContent}, deposit, from.String(), "", content.GetTitle(), proposal.Summary)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
// SubmitConsumerRemovalPropTxCmd returns a CLI command handler for submitting
// a consumer addition proposal via a transaction.
func SubmitConsumerRemovalProposalTxCmd() *cobra.Command {
return &cobra.Command{
Use: "consumer-removal [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a consumer chain removal proposal",
Long: `
Submit a consumer chain removal proposal along with an initial deposit.
The proposal details must be supplied via a JSON file.
Example:
$ <appd> tx gov submit-legacy-proposal consumer-removal <path/to/proposal.json> --from=<key_or_address>
Where proposal.json contains:
{
"title": "Stop the FooChain",
"summary": "It was a great chain",
"chain_id": "foochain",
"stop_time": "2022-01-27T15:59:50.121607-08:00",
"deposit": "10000stake"
}
`, RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := ParseConsumerRemovalProposalJSON(args[0])
if err != nil {
return err
}
content := types.NewConsumerRemovalProposal(proposal.Title, proposal.Summary, proposal.ChainId, proposal.StopTime)
from := clientCtx.GetFromAddress()
msgContent, err := govv1.NewLegacyContent(content, authtypes.NewModuleAddress(govtypes.ModuleName).String())
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return err
}
msg, err := govv1.NewMsgSubmitProposal([]sdk.Msg{msgContent}, deposit, from.String(), "", content.GetTitle(), proposal.Summary)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
// SubmitChangeRewardDenomsProposalTxCmd returns a CLI command handler for submitting
// a change reward denoms proposal via a transaction.
func SubmitChangeRewardDenomsProposalTxCmd() *cobra.Command {
return &cobra.Command{
Use: "change-reward-denoms [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a change reward denoms proposal",
Long: `Submit an change reward denoms proposal with an initial deposit.
The proposal details must be supplied via a JSON file.
Example:
$ <appd> tx gov submit-legacy-proposal change-reward-denoms <path/to/proposal.json> --from=<key_or_address>
Where proposal.json contains:
{
"title": "Change reward denoms",
"summary": "Change reward denoms",
"denoms_to_add": ["untrn"],
"denoms_to_remove": ["stake"],
"deposit": "10000stake"
}
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := ParseChangeRewardDenomsProposalJSON(args[0])
if err != nil {
return err
}
content := types.NewChangeRewardDenomsProposal(proposal.Title, proposal.Summary, proposal.DenomsToAdd, proposal.DenomsToRemove)
from := clientCtx.GetFromAddress()
msgContent, err := govv1.NewLegacyContent(content, authtypes.NewModuleAddress(govtypes.ModuleName).String())
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return err
}
msg, err := govv1.NewMsgSubmitProposal([]sdk.Msg{msgContent}, deposit, from.String(), "", content.GetTitle(), proposal.Summary)
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
type ConsumerAdditionProposalJSON struct {
Title string `json:"title"`
Summary string `json:"summary"`
ChainId string `json:"chain_id"`
InitialHeight clienttypes.Height `json:"initial_height"`
GenesisHash []byte `json:"genesis_hash"`
BinaryHash []byte `json:"binary_hash"`
SpawnTime time.Time `json:"spawn_time"`
ConsumerRedistributionFraction string `json:"consumer_redistribution_fraction"`
BlocksPerDistributionTransmission int64 `json:"blocks_per_distribution_transmission"`
DistributionTransmissionChannel string `json:"distribution_transmission_channel"`
HistoricalEntries int64 `json:"historical_entries"`
CcvTimeoutPeriod time.Duration `json:"ccv_timeout_period"`
TransferTimeoutPeriod time.Duration `json:"transfer_timeout_period"`
UnbondingPeriod time.Duration `json:"unbonding_period"`
Deposit string `json:"deposit"`
}
type ConsumerAdditionProposalReq struct {
Proposer sdk.AccAddress `json:"proposer"`
Title string `json:"title"`
Description string `json:"description"`
ChainId string `json:"chainId"`
InitialHeight clienttypes.Height `json:"initialHeight"`
GenesisHash []byte `json:"genesisHash"`
BinaryHash []byte `json:"binaryHash"`
SpawnTime time.Time `json:"spawnTime"`
ConsumerRedistributionFraction string `json:"consumer_redistribution_fraction"`
BlocksPerDistributionTransmission int64 `json:"blocks_per_distribution_transmission"`
DistributionTransmissionChannel string `json:"distribution_transmission_channel"`
HistoricalEntries int64 `json:"historical_entries"`
CcvTimeoutPeriod time.Duration `json:"ccv_timeout_period"`
TransferTimeoutPeriod time.Duration `json:"transfer_timeout_period"`
UnbondingPeriod time.Duration `json:"unbonding_period"`
Deposit sdk.Coins `json:"deposit"`
}
func ParseConsumerAdditionProposalJSON(proposalFile string) (ConsumerAdditionProposalJSON, error) {
proposal := ConsumerAdditionProposalJSON{}
contents, err := os.ReadFile(filepath.Clean(proposalFile))
if err != nil {
return proposal, err
}
if err := json.Unmarshal(contents, &proposal); err != nil {
return proposal, err
}
return proposal, nil
}
type ConsumerRemovalProposalJSON struct {
Title string `json:"title"`
Summary string `json:"summary"`
ChainId string `json:"chain_id"`
StopTime time.Time `json:"stop_time"`
Deposit string `json:"deposit"`
}
type ConsumerRemovalProposalReq struct {
Proposer sdk.AccAddress `json:"proposer"`
Title string `json:"title"`
Description string `json:"description"`
ChainId string `json:"chainId"`
StopTime time.Time `json:"stopTime"`
Deposit sdk.Coins `json:"deposit"`
}
func ParseConsumerRemovalProposalJSON(proposalFile string) (ConsumerRemovalProposalJSON, error) {
proposal := ConsumerRemovalProposalJSON{}
contents, err := os.ReadFile(filepath.Clean(proposalFile))
if err != nil {
return proposal, err
}
if err := json.Unmarshal(contents, &proposal); err != nil {
return proposal, err
}
return proposal, nil
}
type ChangeRewardDenomsProposalJSON struct {
Summary string `json:"summary"`
types.ChangeRewardDenomsProposal
Deposit string `json:"deposit"`
}
type ChangeRewardDenomsProposalReq struct {
Proposer sdk.AccAddress `json:"proposer"`
types.ChangeRewardDenomsProposal
Deposit sdk.Coins `json:"deposit"`
}
func ParseChangeRewardDenomsProposalJSON(proposalFile string) (ChangeRewardDenomsProposalJSON, error) {
proposal := ChangeRewardDenomsProposalJSON{}
contents, err := os.ReadFile(filepath.Clean(proposalFile))
if err != nil {
return proposal, err
}
if err := json.Unmarshal(contents, &proposal); err != nil {
return proposal, err
}
return proposal, nil
}
func CheckPropUnbondingPeriod(clientCtx client.Context, propUnbondingPeriod time.Duration) {
queryClient := stakingtypes.NewQueryClient(clientCtx)
res, err := queryClient.Params(context.Background(), &stakingtypes.QueryParamsRequest{})
if err != nil {
fmt.Println(err.Error())
return
}
providerUnbondingTime := res.Params.UnbondingTime
if providerUnbondingTime < propUnbondingPeriod {
fmt.Printf(
`consumer unbonding period is advised to be smaller than provider unbonding period, but is longer.
This is not a security risk, but will effectively lengthen the unbonding period on the provider.
consumer unbonding: %s
provider unbonding: %s`,
propUnbondingPeriod,
providerUnbondingTime)
}
}
/* Proposal REST handlers: NOT NEEDED POST 47, BUT PLEASE CHECK THAT ALL FUNCTIONALITY EXISTS IN THE 47 VERSION.
// ConsumerAdditionProposalRESTHandler returns a ProposalRESTHandler that exposes the consumer addition rest handler.
func ConsumerAdditionProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "consumer_addition",
Handler: postConsumerAdditionProposalHandlerFn(clientCtx),
}
}
// ConsumerRemovalProposalRESTHandler returns a ProposalRESTHandler that exposes the consumer removal rest handler.
func ConsumerRemovalProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "consumer_removal",
Handler: postConsumerRemovalProposalHandlerFn(clientCtx),
}
}
func postConsumerAdditionProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req ConsumerAdditionProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
content := types.NewConsumerAdditionProposal(
req.Title, req.Description, req.ChainId, req.InitialHeight,
req.GenesisHash, req.BinaryHash, req.SpawnTime,
req.ConsumerRedistributionFraction, req.BlocksPerDistributionTransmission,
req.DistributionTransmissionChannel, req.HistoricalEntries,
req.CcvTimeoutPeriod, req.TransferTimeoutPeriod, req.UnbondingPeriod)
msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, req.Proposer)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
func postConsumerRemovalProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req ConsumerRemovalProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
content := types.NewConsumerRemovalProposal(
req.Title, req.Description, req.ChainId, req.StopTime,
)
msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, req.Proposer)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
}
}
*/