-
Notifications
You must be signed in to change notification settings - Fork 50
/
proposal.go
222 lines (190 loc) · 5.9 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 v220
import (
"encoding/json"
"fmt"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
proto "github.com/gogo/protobuf/proto"
certtypes "github.com/shentufoundation/shentu/v2/x/cert/types"
shieldtypes "github.com/shentufoundation/shentu/v2/x/shield/types"
)
// Proposal types implement UnpackInterfaceMessages to unpack
// Content fields.
var _ types.UnpackInterfacesMessage = Proposal{}
var _ types.UnpackInterfacesMessage = Proposals{}
// NewProposal creates a new Proposal instance
func NewProposal(content govtypes.Content, id uint64, proposerAddress sdk.AccAddress, isProposerCouncilMember bool, submitTime time.Time, depositEndTime time.Time) (Proposal, error) {
p := Proposal{
ProposalId: id,
Status: StatusDepositPeriod,
IsProposerCouncilMember: isProposerCouncilMember,
ProposerAddress: proposerAddress.String(),
FinalTallyResult: govtypes.EmptyTallyResult(),
TotalDeposit: sdk.NewCoins(),
SubmitTime: submitTime,
DepositEndTime: depositEndTime,
}
msg, ok := content.(proto.Message)
if !ok {
return Proposal{}, fmt.Errorf("%T does not implement proto.Message", content)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return Proposal{}, err
}
p.Content = any
return p, nil
}
// GetContent returns the proposal Content
func (p Proposal) GetContent() govtypes.Content {
content, ok := p.Content.GetCachedValue().(govtypes.Content)
if !ok {
return nil
}
return content
}
// String implements stringer interface
func (p Proposal) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var content govtypes.Content
return unpacker.UnpackAny(p.Content, &content)
}
func (p Proposal) GetTitle() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.GetTitle()
}
func (p Proposal) ProposalType() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalType()
}
func (p Proposal) ProposalRoute() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalRoute()
}
// HasSecurityVoting returns true if the proposal needs to go through security
// (certifier) voting before stake (validator) voting.
// nolint
func (p Proposal) HasSecurityVoting() bool {
switch p.GetContent().(type) {
case *upgradetypes.SoftwareUpgradeProposal, *certtypes.CertifierUpdateProposal, shieldtypes.ShieldClaimProposal:
return true
default:
return false
}
}
// Proposals is an array of proposals.
type Proposals []Proposal
// String implements stringer interface.
func (p Proposals) String() string {
out := "ID - (Status) [Type] Title\n"
for _, prop := range p {
out += fmt.Sprintf("%d - (%s) [%s] %s\n",
prop.ProposalId, prop.Status,
prop.ProposalType(), prop.GetTitle())
}
return strings.TrimSpace(out)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposals) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, x := range p {
err := x.UnpackInterfaces(unpacker)
if err != nil {
return err
}
}
return nil
}
type (
// ProposalQueue is a type alias that represents a list of proposal IDs.
ProposalQueue []uint64
)
// ProposalStatusFromString turns a string into a ProposalStatus.
func ProposalStatusFromString(str string) (ProposalStatus, error) {
num, ok := ProposalStatus_value[str]
if !ok {
return StatusNil, fmt.Errorf("'%s' is not a valid proposal status", str)
}
return ProposalStatus(num), nil
}
// ValidProposalStatus returns true if the proposal status is valid and false otherwise.
func ValidProposalStatus(status ProposalStatus) bool {
if status == StatusDepositPeriod ||
status == StatusCertifierVotingPeriod ||
status == StatusPassed ||
status == StatusRejected ||
status == StatusFailed ||
status == StatusValidatorVotingPeriod {
return true
}
return false
}
// Marshal implements the Marshal method for protobuf compatibility.
func (status ProposalStatus) Marshal() ([]byte, error) {
return []byte{byte(status)}, nil
}
// Unmarshal implements the Unmarshal method for protobuf compatibility.
func (status *ProposalStatus) Unmarshal(data []byte) error {
*status = ProposalStatus(data[0])
return nil
}
// MarshalJSON marshals to JSON using string.
func (status ProposalStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(status.String())
}
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
func (status *ProposalStatus) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
bz2, err := ProposalStatusFromString(s)
if err != nil {
return err
}
*status = bz2
return nil
}
// Format implements the fmt.Formatter interface.
func (status ProposalStatus) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(status.String()))
default:
// TODO: Do this conversion more directly
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
}
}
// ProposalHandler implements the Handler interface for governance module-based
// proposals (ie. TextProposal and SoftwareUpgradeProposal). Since these are
// merely signaling mechanisms at the moment and do not affect state, it
// performs a no-op.
func ProposalHandler(_ sdk.Context, c govtypes.Content) error {
switch c.ProposalType() {
case govtypes.ProposalTypeText:
// both proposal types do not change state so this performs a no-op
return nil
default:
errMsg := fmt.Sprintf("unrecognized gov proposal type: %s", c.ProposalType())
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}