/
proposal.go
187 lines (154 loc) · 5.1 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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sge-network/sge/x/ovm/types"
)
// SetPubkeysChangeProposal sets a pubkey list change proposal in the store.
func (k Keeper) SetPubkeysChangeProposal(ctx sdk.Context, proposal types.PublicKeysChangeProposal) {
store := k.getPubKeysChangeProposalStore(ctx)
b := k.cdc.MustMarshal(&proposal)
store.Set(types.PubkeysChangeProposalKey(proposal.Status, proposal.Id), b)
}
// GetPubkeysChangeProposal returns a pubkeys change proposat by its id
func (k Keeper) GetPubkeysChangeProposal(
ctx sdk.Context,
status types.ProposalStatus,
id uint64,
) (val types.PublicKeysChangeProposal, found bool) {
store := k.getPubKeysChangeProposalStore(ctx)
b := store.Get(types.PubkeysChangeProposalKey(status, id))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetAllPubkeysChangeProposalsByStatus returns list of all pubkeys change proposals
func (k Keeper) GetAllPubkeysChangeProposalsByStatus(
ctx sdk.Context,
status types.ProposalStatus,
) (list []types.PublicKeysChangeProposal, err error) {
store := k.getPubKeysChangeProposalStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, types.PubkeysChangeProposalPrefix(status))
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.PublicKeysChangeProposal
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllPubkeysChangeProposals returns list of all pubkeys change proposals
func (k Keeper) GetAllPubkeysChangeProposals(
ctx sdk.Context,
) (list []types.PublicKeysChangeProposal, err error) {
store := k.getPubKeysChangeProposalStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.PublicKeysChangeProposal
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// RemoveProposal removes a pubkeys change proposal.
func (k Keeper) RemoveProposal(ctx sdk.Context, status types.ProposalStatus, id uint64) {
store := k.getPubKeysChangeProposalStore(ctx)
store.Delete(types.PubkeysChangeProposalKey(status, id))
}
// FinishProposals sets all of active proposals as finished.
func (k Keeper) FinishProposals(ctx sdk.Context) error {
return k.finishPubkeysChangeProposals(ctx)
}
// finishPubkeysChangeProposals sets all active pubkeys change proposals as finished.
func (k Keeper) finishPubkeysChangeProposals(ctx sdk.Context) error {
activeProposals, err := k.GetAllPubkeysChangeProposalsByStatus(
ctx,
types.ProposalStatus_PROPOSAL_STATUS_ACTIVE,
)
if err != nil {
return fmt.Errorf("error while retrieving the active pubkeys change proposals: %s", err)
}
if len(activeProposals) == 0 {
return nil
}
blockTime := ctx.BlockTime().Unix()
keyVault, found := k.GetKeyVault(ctx)
if !found {
fmt.Printf("there is no publick keys record")
}
for _, proposal := range activeProposals {
if proposal.IsExpired(blockTime) {
// proposal is expired
if err = k.finishPubkeysChangeProposal(
ctx,
proposal.Id,
types.ProposalResult_PROPOSAL_RESULT_EXPIRED,
fmt.Sprintf("current block time is %d, more than %d minutes is passed since start time.", blockTime, types.MaxValidProposalMinutes),
); err != nil {
return fmt.Errorf("error while setting the proposal as expired: %s", err)
}
// this proposal is expired go for next proposal
continue
}
result := proposal.DecideResult(&keyVault)
switch result {
case types.ProposalResult_PROPOSAL_RESULT_REJECTED:
if err = k.finishPubkeysChangeProposal(
ctx,
proposal.Id,
types.ProposalResult_PROPOSAL_RESULT_REJECTED,
"rejected with more 'no' votes than 'yes' votes",
); err != nil {
return fmt.Errorf("error while setting the proposal as rejected: %s", err)
}
// this proposal is rejected go for next proposal
continue
case types.ProposalResult_PROPOSAL_RESULT_APPROVED:
keyVault, found := k.GetKeyVault(ctx)
if !found {
fmt.Printf("there is no publick keys record")
}
keyVault.PublicKeys = proposal.Modifications.PublicKeys
keyVault.SetLeader(proposal.Modifications.LeaderIndex)
if err = k.finishPubkeysChangeProposal(
ctx,
proposal.Id,
types.ProposalResult_PROPOSAL_RESULT_APPROVED,
"",
); err != nil {
return fmt.Errorf("error while setting the proposal as approved: %s", err)
}
k.SetKeyVault(ctx, keyVault)
}
}
return nil
}
func (k Keeper) finishPubkeysChangeProposal(
ctx sdk.Context,
proposalID uint64,
result types.ProposalResult,
resultMetadata string,
) error {
proposal, found := k.GetPubkeysChangeProposal(
ctx,
types.ProposalStatus_PROPOSAL_STATUS_ACTIVE,
proposalID,
)
if !found {
return fmt.Errorf("proposal not found with id %d", proposalID)
}
proposal.Result = result
proposal.ResultMeta = resultMetadata
proposal.FinishTS = ctx.BlockTime().Unix()
proposal.Status = types.ProposalStatus_PROPOSAL_STATUS_FINISHED
k.RemoveProposal(ctx, types.ProposalStatus_PROPOSAL_STATUS_ACTIVE, proposalID)
k.SetPubkeysChangeProposal(ctx, proposal)
return nil
}