-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
proposal.go
131 lines (111 loc) · 3.4 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
package v1
import (
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
)
const (
// DefaultStartingProposalID is 1
DefaultStartingProposalID uint64 = 1
StatusNil = ProposalStatus_PROPOSAL_STATUS_UNSPECIFIED
StatusDepositPeriod = ProposalStatus_PROPOSAL_STATUS_DEPOSIT_PERIOD
StatusVotingPeriod = ProposalStatus_PROPOSAL_STATUS_VOTING_PERIOD
StatusPassed = ProposalStatus_PROPOSAL_STATUS_PASSED
StatusRejected = ProposalStatus_PROPOSAL_STATUS_REJECTED
StatusFailed = ProposalStatus_PROPOSAL_STATUS_FAILED
)
// NewProposal creates a new Proposal instance
func NewProposal(messages []sdk.Msg, id uint64, metadata string, submitTime, depositEndTime time.Time) (Proposal, error) {
msgs, err := sdktx.SetMsgs(messages)
if err != nil {
return Proposal{}, err
}
tally := EmptyTallyResult()
p := Proposal{
Id: id,
Messages: msgs,
Metadata: metadata,
Status: StatusDepositPeriod,
FinalTallyResult: &tally,
SubmitTime: &submitTime,
DepositEndTime: &depositEndTime,
}
return p, nil
}
// GetMessages returns the proposal messages
func (p Proposal) GetMsgs() ([]sdk.Msg, error) {
return sdktx.GetMsgs(p.Messages, "sdk.MsgProposal")
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
return sdktx.UnpackInterfaces(unpacker, p.Messages)
}
// Proposals is an array of proposal
type Proposals []*Proposal
var _ types.UnpackInterfacesMessage = Proposals{}
// String implements stringer interface
func (p Proposals) String() string {
out := "ID - (Status) [Type] Title\n"
for _, prop := range p {
out += fmt.Sprintf("%d - %s\n",
prop.Id, prop.Status)
}
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 defines a queue for 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
}
// Marshal needed for protobuf compatibility
func (status ProposalStatus) Marshal() ([]byte, error) {
return []byte{byte(status)}, nil
}
// Unmarshal needed for protobuf compatibility
func (status *ProposalStatus) Unmarshal(data []byte) error {
*status = ProposalStatus(data[0])
return nil
}
// Format implements the fmt.Formatter interface.
// nolint: errcheck
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))))
}
}
// ValidProposalStatus returns true if the proposal status is valid and false
// otherwise.
func ValidProposalStatus(status ProposalStatus) bool {
if status == StatusDepositPeriod ||
status == StatusVotingPeriod ||
status == StatusPassed ||
status == StatusRejected ||
status == StatusFailed {
return true
}
return false
}