-
Notifications
You must be signed in to change notification settings - Fork 111
/
proposal.go
251 lines (213 loc) · 8.62 KB
/
proposal.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
package types
import (
"fmt"
"strings"
time "time"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
ccvtypes "github.com/cosmos/interchain-security/v2/x/ccv/types"
)
const (
ProposalTypeConsumerAddition = "ConsumerAddition"
ProposalTypeConsumerRemoval = "ConsumerRemoval"
ProposalTypeChangeRewardDenoms = "ChangeRewardDenoms"
)
var (
_ govtypes.Content = &ConsumerAdditionProposal{}
_ govtypes.Content = &ConsumerRemovalProposal{}
_ govtypes.Content = &ChangeRewardDenomsProposal{}
)
func init() {
govtypes.RegisterProposalType(ProposalTypeConsumerAddition)
govtypes.RegisterProposalType(ProposalTypeConsumerRemoval)
govtypes.RegisterProposalType(ProposalTypeChangeRewardDenoms)
}
// NewConsumerAdditionProposal creates a new consumer addition proposal.
func NewConsumerAdditionProposal(title, description, chainID string,
initialHeight clienttypes.Height, genesisHash, binaryHash []byte,
spawnTime time.Time,
consumerRedistributionFraction string,
blocksPerDistributionTransmission int64,
distributionTransmissionChannel string,
historicalEntries int64,
ccvTimeoutPeriod time.Duration,
transferTimeoutPeriod time.Duration,
unbondingPeriod time.Duration,
) govtypes.Content {
return &ConsumerAdditionProposal{
Title: title,
Description: description,
ChainId: chainID,
InitialHeight: initialHeight,
GenesisHash: genesisHash,
BinaryHash: binaryHash,
SpawnTime: spawnTime,
ConsumerRedistributionFraction: consumerRedistributionFraction,
BlocksPerDistributionTransmission: blocksPerDistributionTransmission,
DistributionTransmissionChannel: distributionTransmissionChannel,
HistoricalEntries: historicalEntries,
CcvTimeoutPeriod: ccvTimeoutPeriod,
TransferTimeoutPeriod: transferTimeoutPeriod,
UnbondingPeriod: unbondingPeriod,
}
}
// GetTitle returns the title of a consumer addition proposal.
func (cccp *ConsumerAdditionProposal) GetTitle() string { return cccp.Title }
// GetDescription returns the description of a consumer addition proposal.
func (cccp *ConsumerAdditionProposal) GetDescription() string { return cccp.Description }
// ProposalRoute returns the routing key of a consumer addition proposal.
func (cccp *ConsumerAdditionProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a consumer addition proposal.
func (cccp *ConsumerAdditionProposal) ProposalType() string {
return ProposalTypeConsumerAddition
}
// ValidateBasic runs basic stateless validity checks
func (cccp *ConsumerAdditionProposal) ValidateBasic() error {
if err := govtypes.ValidateAbstract(cccp); err != nil {
return err
}
if strings.TrimSpace(cccp.ChainId) == "" {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "consumer chain id must not be blank")
}
if cccp.InitialHeight.IsZero() {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "initial height cannot be zero")
}
if len(cccp.GenesisHash) == 0 {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "genesis hash cannot be empty")
}
if len(cccp.BinaryHash) == 0 {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "binary hash cannot be empty")
}
if cccp.SpawnTime.IsZero() {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "spawn time cannot be zero")
}
if err := ccvtypes.ValidateStringFraction(cccp.ConsumerRedistributionFraction); err != nil {
return errorsmod.Wrapf(ErrInvalidConsumerAdditionProposal, "consumer redistribution fraction is invalid: %s", err)
}
if err := ccvtypes.ValidatePositiveInt64(cccp.BlocksPerDistributionTransmission); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "blocks per distribution transmission cannot be < 1")
}
if err := ccvtypes.ValidateDistributionTransmissionChannel(cccp.DistributionTransmissionChannel); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "distribution transmission channel")
}
if err := ccvtypes.ValidatePositiveInt64(cccp.HistoricalEntries); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "historical entries cannot be < 1")
}
if err := ccvtypes.ValidateDuration(cccp.CcvTimeoutPeriod); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "ccv timeout period cannot be zero")
}
if err := ccvtypes.ValidateDuration(cccp.TransferTimeoutPeriod); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "transfer timeout period cannot be zero")
}
if err := ccvtypes.ValidateDuration(cccp.UnbondingPeriod); err != nil {
return errorsmod.Wrap(ErrInvalidConsumerAdditionProposal, "unbonding period cannot be zero")
}
return nil
}
// String returns the string representation of the ConsumerAdditionProposal.
func (cccp *ConsumerAdditionProposal) String() string {
return fmt.Sprintf(`CreateConsumerChain Proposal
Title: %s
Description: %s
ChainID: %s
InitialHeight: %s
GenesisHash: %s
BinaryHash: %s
SpawnTime: %s
ConsumerRedistributionFraction: %s
BlocksPerDistributionTransmission: %d
DistributionTransmissionChannel: %s
HistoricalEntries: %d
CcvTimeoutPeriod: %d
TransferTimeoutPeriod: %d
UnbondingPeriod: %d`,
cccp.Title,
cccp.Description,
cccp.ChainId,
cccp.InitialHeight,
cccp.GenesisHash,
cccp.BinaryHash,
cccp.SpawnTime,
cccp.ConsumerRedistributionFraction,
cccp.BlocksPerDistributionTransmission,
cccp.DistributionTransmissionChannel,
cccp.HistoricalEntries,
cccp.CcvTimeoutPeriod,
cccp.TransferTimeoutPeriod,
cccp.UnbondingPeriod)
}
// NewConsumerRemovalProposal creates a new consumer removal proposal.
func NewConsumerRemovalProposal(title, description, chainID string, stopTime time.Time) govtypes.Content {
return &ConsumerRemovalProposal{
Title: title,
Description: description,
ChainId: chainID,
StopTime: stopTime,
}
}
// ProposalRoute returns the routing key of a consumer removal proposal.
func (sccp *ConsumerRemovalProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a consumer removal proposal.
func (sccp *ConsumerRemovalProposal) ProposalType() string { return ProposalTypeConsumerRemoval }
// ValidateBasic runs basic stateless validity checks
func (sccp *ConsumerRemovalProposal) ValidateBasic() error {
if err := govtypes.ValidateAbstract(sccp); err != nil {
return err
}
if strings.TrimSpace(sccp.ChainId) == "" {
return errorsmod.Wrap(ErrInvalidConsumerRemovalProp, "consumer chain id must not be blank")
}
if sccp.StopTime.IsZero() {
return errorsmod.Wrap(ErrInvalidConsumerRemovalProp, "spawn time cannot be zero")
}
return nil
}
func NewChangeRewardDenomsProposal(title, description string,
denomsToAdd, denomsToRemove []string,
) govtypes.Content {
return &ChangeRewardDenomsProposal{
Title: title,
Description: description,
DenomsToAdd: denomsToAdd,
DenomsToRemove: denomsToRemove,
}
}
// ProposalRoute returns the routing key of a change reward denoms proposal.
func (crdp *ChangeRewardDenomsProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a change reward denoms proposal.
func (crdp *ChangeRewardDenomsProposal) ProposalType() string {
return ProposalTypeChangeRewardDenoms
}
// ValidateBasic runs basic stateless validity checks on a ChangeRewardDenomsProposal.
func (crdp *ChangeRewardDenomsProposal) ValidateBasic() error {
emptyDenomsToAdd := len(crdp.DenomsToAdd) == 0
emptyDenomsToRemove := len(crdp.DenomsToRemove) == 0
// Return error if both sets are empty or nil
if emptyDenomsToAdd && emptyDenomsToRemove {
return fmt.Errorf(
"invalid change reward denoms proposal: both denoms to add and denoms to remove are empty")
}
// Return error if a denom is in both sets
for _, denomToAdd := range crdp.DenomsToAdd {
for _, denomToRemove := range crdp.DenomsToRemove {
if denomToAdd == denomToRemove {
return fmt.Errorf(
"invalid change reward denoms proposal: %s cannot be both added and removed", denomToAdd)
}
}
}
// Return error if any denom is "invalid"
for _, denom := range crdp.DenomsToAdd {
if !sdk.NewCoin(denom, sdk.NewInt(1)).IsValid() {
return fmt.Errorf("invalid change reward denoms proposal: %s is not a valid denom", denom)
}
}
for _, denom := range crdp.DenomsToRemove {
if !sdk.NewCoin(denom, sdk.NewInt(1)).IsValid() {
return fmt.Errorf("invalid change reward denoms proposal: %s is not a valid denom", denom)
}
}
return nil
}