-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
types.go
133 lines (109 loc) · 3.68 KB
/
types.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
// DONTCOVER
// nolint
package v0_36
import (
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v034gov "github.com/cosmos/cosmos-sdk/x/gov/legacy/v0_34"
)
const (
ModuleName = "gov"
RouterKey = ModuleName
ProposalTypeText string = "Text"
MaxDescriptionLength int = 5000
MaxTitleLength int = 140
)
var (
_ Content = TextProposal{}
)
type (
Proposals []Proposal
ProposalQueue []uint64
TextProposal struct {
Title string `json:"title"`
Description string `json:"description"`
}
Content interface {
GetTitle() string
GetDescription() string
ProposalRoute() string
ProposalType() string
ValidateBasic() error
String() string
}
Proposal struct {
Content `json:"content"`
ProposalID uint64 `json:"id"`
Status v034gov.ProposalStatus `json:"proposal_status"`
FinalTallyResult v034gov.TallyResult `json:"final_tally_result"`
SubmitTime time.Time `json:"submit_time"`
DepositEndTime time.Time `json:"deposit_end_time"`
TotalDeposit sdk.Coins `json:"total_deposit"`
VotingStartTime time.Time `json:"voting_start_time"`
VotingEndTime time.Time `json:"voting_end_time"`
}
GenesisState struct {
StartingProposalID uint64 `json:"starting_proposal_id"`
Deposits v034gov.Deposits `json:"deposits"`
Votes v034gov.Votes `json:"votes"`
Proposals []Proposal `json:"proposals"`
DepositParams v034gov.DepositParams `json:"deposit_params"`
VotingParams v034gov.VotingParams `json:"voting_params"`
TallyParams v034gov.TallyParams `json:"tally_params"`
}
)
func NewGenesisState(
startingProposalID uint64, deposits v034gov.Deposits, votes v034gov.Votes, proposals []Proposal,
depositParams v034gov.DepositParams, votingParams v034gov.VotingParams, tallyParams v034gov.TallyParams,
) GenesisState {
return GenesisState{
StartingProposalID: startingProposalID,
Deposits: deposits,
Votes: votes,
Proposals: proposals,
DepositParams: depositParams,
VotingParams: votingParams,
TallyParams: tallyParams,
}
}
func NewTextProposal(title, description string) Content {
return TextProposal{title, description}
}
func (tp TextProposal) GetTitle() string { return tp.Title }
func (tp TextProposal) GetDescription() string { return tp.Description }
func (tp TextProposal) ProposalRoute() string { return RouterKey }
func (tp TextProposal) ProposalType() string { return ProposalTypeText }
func (tp TextProposal) ValidateBasic() error { return ValidateAbstract(tp) }
func (tp TextProposal) String() string {
return fmt.Sprintf(`Text Proposal:
Title: %s
Description: %s
`, tp.Title, tp.Description)
}
func ErrInvalidProposalContent(msg string) error {
return fmt.Errorf("invalid proposal content: %s", msg)
}
func ValidateAbstract(c Content) error {
title := c.GetTitle()
if len(strings.TrimSpace(title)) == 0 {
return ErrInvalidProposalContent("proposal title cannot be blank")
}
if len(title) > MaxTitleLength {
return ErrInvalidProposalContent(fmt.Sprintf("proposal title is longer than max length of %d", MaxTitleLength))
}
description := c.GetDescription()
if len(description) == 0 {
return ErrInvalidProposalContent("proposal description cannot be blank")
}
if len(description) > MaxDescriptionLength {
return ErrInvalidProposalContent(fmt.Sprintf("proposal description is longer than max length of %d", MaxDescriptionLength))
}
return nil
}
func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterInterface((*Content)(nil), nil)
cdc.RegisterConcrete(TextProposal{}, "cosmos-sdk/TextProposal", nil)
}