/
proposal.go
222 lines (189 loc) · 7.46 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
package types
import (
"errors"
"fmt"
"strings"
time "time"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/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/x/ccv/types"
)
const (
ProposalTypeConsumerAddition = "ConsumerAddition"
ProposalTypeConsumerRemoval = "ConsumerRemoval"
ProposalTypeEquivocation = "Equivocation"
)
var (
_ govtypes.Content = &ConsumerAdditionProposal{}
_ govtypes.Content = &ConsumerRemovalProposal{}
_ govtypes.Content = &EquivocationProposal{}
)
func init() {
govtypes.RegisterProposalType(ProposalTypeConsumerAddition)
govtypes.RegisterProposalType(ProposalTypeConsumerRemoval)
govtypes.RegisterProposalType(ProposalTypeEquivocation)
}
// 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,
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,
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 sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "consumer chain id must not be blank")
}
if cccp.InitialHeight.IsZero() {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "initial height cannot be zero")
}
if len(cccp.GenesisHash) == 0 {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "genesis hash cannot be empty")
}
if len(cccp.BinaryHash) == 0 {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "binary hash cannot be empty")
}
if cccp.SpawnTime.IsZero() {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "spawn time cannot be zero")
}
if err := ccvtypes.ValidateStringFraction(cccp.ConsumerRedistributionFraction); err != nil {
return sdkerrors.Wrapf(ErrInvalidConsumerAdditionProposal, "consumer redistribution fraction is invalid: %s", err)
}
if err := ccvtypes.ValidatePositiveInt64(cccp.BlocksPerDistributionTransmission); err != nil {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "blocks per distribution transmission cannot be < 1")
}
if err := ccvtypes.ValidatePositiveInt64(cccp.HistoricalEntries); err != nil {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "historical entries cannot be < 1")
}
if err := ccvtypes.ValidateDuration(cccp.CcvTimeoutPeriod); err != nil {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "ccv timeout period cannot be zero")
}
if err := ccvtypes.ValidateDuration(cccp.TransferTimeoutPeriod); err != nil {
return sdkerrors.Wrap(ErrInvalidConsumerAdditionProposal, "transfer timeout period cannot be zero")
}
if err := ccvtypes.ValidateDuration(cccp.UnbondingPeriod); err != nil {
return sdkerrors.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
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.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 sdkerrors.Wrap(ErrInvalidConsumerRemovalProp, "consumer chain id must not be blank")
}
if sccp.StopTime.IsZero() {
return sdkerrors.Wrap(ErrInvalidConsumerRemovalProp, "spawn time cannot be zero")
}
return nil
}
// NewEquivocationProposal creates a new equivocation proposal.
func NewEquivocationProposal(title, description string, equivocations []*evidencetypes.Equivocation) govtypes.Content {
return &EquivocationProposal{
Title: title,
Description: description,
Equivocations: equivocations,
}
}
// ProposalRoute returns the routing key of an equivocation proposal.
func (sp *EquivocationProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns the type of a equivocation proposal.
func (sp *EquivocationProposal) ProposalType() string {
return ProposalTypeEquivocation
}
// ValidateBasic runs basic stateless validity checks
func (sp *EquivocationProposal) ValidateBasic() error {
if err := govtypes.ValidateAbstract(sp); err != nil {
return err
}
if len(sp.Equivocations) == 0 {
return errors.New("invalid equivocation proposal: empty equivocations")
}
for i := 0; i < len(sp.Equivocations); i++ {
if err := sp.Equivocations[i].ValidateBasic(); err != nil {
return err
}
}
return nil
}