-
Notifications
You must be signed in to change notification settings - Fork 2
/
proposal.go
174 lines (149 loc) · 5.72 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
package keeper
import (
"fmt"
"time"
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
"github.com/fibonacci-chain/fbc/x/gov/types"
)
// SubmitProposal creates new proposal given a content
func (keeper Keeper) SubmitProposal(ctx sdk.Context, content types.Content) (types.Proposal, sdk.Error) {
if !keeper.router.HasRoute(content.ProposalRoute()) {
return types.Proposal{}, types.ErrNoProposalHandlerExists(content)
}
proposalID, err := keeper.GetProposalID(ctx)
if err != nil {
return types.Proposal{}, err
}
// get the time now as the submit time
submitTime := ctx.BlockHeader().Time
// get params for special proposal
var depositPeriod time.Duration
if !keeper.proposalHandlerRouter.HasRoute(content.ProposalRoute()) {
depositPeriod = keeper.GetDepositParams(ctx).MaxDepositPeriod
} else {
proposalParams := keeper.proposalHandlerRouter.GetRoute(content.ProposalRoute())
depositPeriod = proposalParams.GetMaxDepositPeriod(ctx, content)
}
proposal := types.NewProposal(ctx, keeper.totalPower(ctx), content, proposalID, submitTime,
submitTime.Add(depositPeriod))
keeper.SetProposal(ctx, proposal)
keeper.InsertInactiveProposalQueue(ctx, proposalID, proposal.DepositEndTime)
keeper.SetProposalID(ctx, proposalID+1)
if keeper.proposalHandlerRouter.HasRoute(content.ProposalRoute()) {
keeper.proposalHandlerRouter.GetRoute(content.ProposalRoute()).AfterSubmitProposalHandler(ctx, proposal)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)),
),
)
return proposal, nil
}
// GetProposal get Proposal from store by ProposalID
func (keeper Keeper) GetProposal(ctx sdk.Context, proposalID uint64) (proposal types.Proposal, ok bool) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(types.ProposalKey(proposalID))
if bz == nil {
return
}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposal)
return proposal, true
}
// SetProposal set a proposal to store
func (keeper Keeper) SetProposal(ctx sdk.Context, proposal types.Proposal) {
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposal)
store.Set(types.ProposalKey(proposal.ProposalID), bz)
}
// DeleteProposal deletes a proposal from store
func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(keeper.storeKey)
proposal, ok := keeper.GetProposal(ctx, proposalID)
if !ok {
panic(fmt.Sprintf("couldn't find proposal with id#%d", proposalID))
}
keeper.RemoveFromInactiveProposalQueue(ctx, proposalID, proposal.DepositEndTime)
keeper.RemoveFromActiveProposalQueue(ctx, proposalID, proposal.VotingEndTime)
store.Delete(types.ProposalKey(proposalID))
}
// GetProposals returns all the proposals from store
func (keeper Keeper) GetProposals(ctx sdk.Context) (proposals types.Proposals) {
keeper.IterateProposals(ctx, func(proposal types.Proposal) bool {
proposals = append(proposals, proposal)
return false
})
return
}
// GetProposalsFiltered get Proposals from store by ProposalID
// voterAddr will filter proposals by whether or not that address has voted on them
// depositorAddr will filter proposals by whether or not that address has deposited to them
// status will filter proposals by status
// numLatest will fetch a specified number of the most recent proposals, or 0 for all proposals
func (keeper Keeper) GetProposalsFiltered(
ctx sdk.Context, voterAddr sdk.AccAddress, depositorAddr sdk.AccAddress, status types.ProposalStatus,
numLatest uint64,
) []types.Proposal {
maxProposalID, err := keeper.GetProposalID(ctx)
if err != nil {
return []types.Proposal{}
}
matchingProposals := []types.Proposal{}
if numLatest == 0 {
numLatest = maxProposalID
}
for proposalID := maxProposalID - numLatest; proposalID < maxProposalID; proposalID++ {
if voterAddr != nil && len(voterAddr) != 0 {
_, found := keeper.GetVote(ctx, proposalID, voterAddr)
if !found {
continue
}
}
if depositorAddr != nil && len(depositorAddr) != 0 {
_, found := keeper.GetDeposit(ctx, proposalID, depositorAddr)
if !found {
continue
}
}
proposal, ok := keeper.GetProposal(ctx, proposalID)
if !ok {
continue
}
if types.ValidProposalStatus(status) && proposal.Status != status {
continue
}
matchingProposals = append(matchingProposals, proposal)
}
return matchingProposals
}
// GetProposalID gets the highest proposal ID
func (keeper Keeper) GetProposalID(ctx sdk.Context) (proposalID uint64, err sdk.Error) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(types.ProposalIDKey)
if bz == nil {
return 0, types.ErrInvalidGenesis()
}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID)
return proposalID, nil
}
// SetProposalID sets the proposal ID to gov store
func (keeper Keeper) SetProposalID(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID)
store.Set(types.ProposalIDKey, bz)
}
func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal *types.Proposal) {
proposal.VotingStartTime = ctx.BlockHeader().Time
var votingPeriod time.Duration
if !keeper.proposalHandlerRouter.HasRoute(proposal.ProposalRoute()) {
votingPeriod = keeper.GetVotingPeriod(ctx, proposal.Content)
} else {
phr := keeper.proposalHandlerRouter.GetRoute(proposal.ProposalRoute())
votingPeriod = phr.GetVotingPeriod(ctx, proposal.Content)
}
// calculate the end time of voting
proposal.VotingEndTime = proposal.VotingStartTime.Add(votingPeriod)
proposal.Status = types.StatusVotingPeriod
keeper.RemoveFromInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
keeper.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
}