This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
keeper.go
346 lines (285 loc) · 9.13 KB
/
keeper.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package db
import (
tcr "github.com/cosmos/cosmos-academy/example-apps/token_curated_registry/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/tendermint/go-amino"
"reflect"
"container/heap"
)
type BallotKeeper struct {
ListingKey sdk.StoreKey
BallotKey sdk.StoreKey
Cdc *amino.Codec
}
func NewBallotKeeper(listingKey sdk.StoreKey, ballotkey sdk.StoreKey, _cdc *amino.Codec) BallotKeeper {
return BallotKeeper{
ListingKey: listingKey,
BallotKey: ballotkey,
Cdc: _cdc,
}
}
// Will get Ballot using unique identifier. Do not need to specify status
func (bk BallotKeeper) GetBallot(ctx sdk.Context, identifier string) tcr.Ballot {
store := ctx.KVStore(bk.BallotKey)
key := []byte(identifier)
val := store.Get(key)
if val == nil {
return tcr.Ballot{}
}
ballot := &tcr.Ballot{}
err := bk.Cdc.UnmarshalBinary(val, ballot)
if err != nil {
panic(err)
}
return *ballot
}
func (bk BallotKeeper) AddBallot(ctx sdk.Context, identifier string, owner sdk.Address, applyLen int64, bond int64) sdk.Error {
store := ctx.KVStore(bk.BallotKey)
newBallot := tcr.Ballot{
Identifier: identifier,
Owner: owner,
Bond: bond,
EndApplyBlockStamp: ctx.BlockHeight() + applyLen,
}
// Add ballot with Pending Status
key := []byte(identifier)
val, _ := bk.Cdc.MarshalBinary(newBallot)
store.Set(key, val)
return nil
}
func (bk BallotKeeper) ActivateBallot(ctx sdk.Context, accountKeeper bank.Keeper, owner sdk.Address, challenger sdk.Address,
identifier string, commitLen int64, revealLen, minBond int64, challengeBond int64) sdk.Error {
store := ctx.KVStore(bk.BallotKey)
ballot := bk.GetBallot(ctx, identifier)
if ballot.Bond < minBond {
bk.DeleteBallot(ctx, identifier)
refund := sdk.Coin{
Denom: "RegistryCoin",
Amount: challengeBond,
}
_, _, err := accountKeeper.AddCoins(ctx, challenger, []sdk.Coin{refund})
if err != nil {
return err
}
return nil
}
if ballot.Bond != challengeBond {
return sdk.NewError(2, 115, "Must match candidate's bond")
}
ballot.Active = true
ballot.Challenger = challenger
// Reset votes to 0. Could have sme other policy, but this is simple
ballot.Approve = 0
ballot.Deny = 0
// Rest Blockstamps
ballot.EndCommitBlockStamp = ctx.BlockHeight() + commitLen
ballot.EndApplyBlockStamp = ballot.EndCommitBlockStamp + revealLen
newBallot, _ := bk.Cdc.MarshalBinary(ballot)
key := []byte(identifier)
store.Set(key, newBallot)
return nil
}
func (bk BallotKeeper) DeactivateBallot(ctx sdk.Context, identifier string) {
store := ctx.KVStore(bk.BallotKey)
ballot := bk.GetBallot(ctx, identifier)
ballot.Active = false
newBallot, _ := bk.Cdc.MarshalBinary(ballot)
key := []byte(identifier)
store.Set(key, newBallot)
}
func (bk BallotKeeper) CommitBallot(ctx sdk.Context, owner sdk.Address, identifier string, commitment []byte) {
commitKey := []byte(identifier)
commitKey = append(commitKey, []byte("commits")...)
commitKey = append(commitKey, owner...)
ballotStore := ctx.KVStore(bk.BallotKey)
ballotStore.Set(commitKey, commitment)
}
func (bk BallotKeeper) GetCommitment(ctx sdk.Context, owner sdk.Address, identifier string) []byte {
commitKey := []byte(identifier)
commitKey = append(commitKey, []byte("commits")...)
commitKey = append(commitKey, owner...)
ballotStore := ctx.KVStore(bk.BallotKey)
return ballotStore.Get(commitKey)
}
func (bk BallotKeeper) DeleteCommitment(ctx sdk.Context, owner sdk.Address, identifier string) {
commitKey := []byte(identifier)
commitKey = append(commitKey, []byte("commits")...)
commitKey = append(commitKey, owner...)
ballotStore := ctx.KVStore(bk.BallotKey)
ballotStore.Delete(commitKey)
}
func (bk BallotKeeper) VoteBallot(ctx sdk.Context, owner sdk.Address, identifier string, vote bool, power int64) sdk.Error {
ballotStore := ctx.KVStore(bk.BallotKey)
ballotKey := []byte(identifier)
bz := ballotStore.Get(ballotKey)
if bz == nil {
return sdk.NewError(2, 107, "Ballot does not exist")
}
ballot := &tcr.Ballot{}
err := bk.Cdc.UnmarshalBinary(bz, ballot)
if err != nil {
panic(err)
}
if vote {
ballot.Approve += power
} else {
ballot.Deny += power
}
newBallot, _ := bk.Cdc.MarshalBinary(*ballot)
ballotStore.Set(ballotKey, newBallot)
voteStruct := tcr.Vote{Choice: vote, Power: power}
voteVal, err2 := bk.Cdc.MarshalBinary(voteStruct)
if err2 != nil {
panic(err2)
}
// Set voteKey to "{identifier}|votes|{owner_address}|
voteKey := []byte(identifier)
voteKey = append(voteKey, []byte("votes")...)
voteKey = append(voteKey, owner...)
ballotStore.Set(voteKey, voteVal)
return nil
}
func (bk BallotKeeper) GetVote(ctx sdk.Context, owner sdk.Address, identifier string) tcr.Vote {
// Set voteKey to "{identifier}|votes|{owner_address}|
voteKey := []byte(identifier)
voteKey = append(voteKey, []byte("votes")...)
voteKey = append(voteKey, owner...)
vote := &tcr.Vote{}
ballotStore := ctx.KVStore(bk.BallotKey)
bz := ballotStore.Get(voteKey)
if bz == nil {
return tcr.Vote{}
}
err := bk.Cdc.UnmarshalBinary(bz, vote)
if err != nil {
panic(err)
}
return *vote
}
func (bk BallotKeeper) DeleteVote(ctx sdk.Context, owner sdk.Address, identifier string) {
voteKey := []byte(identifier)
voteKey = append(voteKey, []byte("votes")...)
voteKey = append(voteKey, owner...)
ballotStore := ctx.KVStore(bk.BallotKey)
ballotStore.Delete(voteKey)
}
func (bk BallotKeeper) DeleteBallot(ctx sdk.Context, identifier string) {
key := []byte(identifier)
store := ctx.KVStore(bk.BallotKey)
store.Delete(key)
}
func (bk BallotKeeper) AddListing(ctx sdk.Context, identifier string, votes int64) {
key := []byte(identifier)
store := ctx.KVStore(bk.ListingKey)
listing := tcr.Listing{
Identifier: identifier,
Votes: votes,
}
val, _ := bk.Cdc.MarshalBinary(listing)
store.Set(key, val)
}
func (bk BallotKeeper) GetListing(ctx sdk.Context, identifier string) tcr.Listing {
key := []byte(identifier)
store := ctx.KVStore(bk.ListingKey)
bz := store.Get(key)
if bz == nil {
return tcr.Listing{}
}
listing := &tcr.Listing{}
err := bk.Cdc.UnmarshalBinary(bz, listing)
if err != nil {
panic(err)
}
return *listing
}
func (bk BallotKeeper) DeleteListing(ctx sdk.Context, identifier string) {
key := []byte(identifier)
store := ctx.KVStore(bk.ListingKey)
store.Delete(key)
}
// --------------------------------------------------------------------------------------------------
// Queue stored in key: candidateQueue
// getCandidateQueue gets the CandidateQueue from the context
func (bk BallotKeeper) getCandidateQueue(ctx sdk.Context) tcr.PriorityQueue {
store := ctx.KVStore(bk.BallotKey)
bpq := store.Get([]byte("candidateQueue"))
if bpq == nil {
return tcr.PriorityQueue{}
}
candidateQueue := tcr.PriorityQueue{}
err := bk.Cdc.UnmarshalBinaryBare(bpq, &candidateQueue)
if err != nil {
panic(err)
}
return candidateQueue
}
// setProposalQueue sets the CandidateQueue to the context
func (bk BallotKeeper) setCandidateQueue(ctx sdk.Context, candidateQueue tcr.PriorityQueue) {
store := ctx.KVStore(bk.BallotKey)
bpq, err := bk.Cdc.MarshalBinaryBare(candidateQueue)
if err != nil {
panic(err)
}
store.Set([]byte("candidateQueue"), bpq)
}
// ProposalQueueHead returns the head of the PriorityQueue
func (bk BallotKeeper) ProposalQueueHead(ctx sdk.Context) tcr.Ballot {
candidateQueue := bk.getCandidateQueue(ctx)
if reflect.DeepEqual(candidateQueue, tcr.PriorityQueue{}) {
return tcr.Ballot{}
}
if candidateQueue.Len() == 0 {
return tcr.Ballot{}
}
ballot := bk.GetBallot(ctx, candidateQueue.Peek().Value)
return ballot
}
// ProposalQueuePop pops the head from the Proposal queue
func (bk BallotKeeper) ProposalQueuePop(ctx sdk.Context) tcr.Ballot {
candidateQueue := bk.getCandidateQueue(ctx)
if reflect.DeepEqual(candidateQueue, tcr.PriorityQueue{}) {
return tcr.Ballot{}
}
if candidateQueue.Len() == 0 {
return tcr.Ballot{}
}
ballot := bk.GetBallot(ctx, heap.Pop(&candidateQueue).(*tcr.Item).Value)
bk.setCandidateQueue(ctx, candidateQueue)
return ballot
}
// ProposalQueuePush pushes a candidate to Priority Queue
func (bk BallotKeeper) ProposalQueuePush(ctx sdk.Context, identifier string, blockNum int64) {
candidateQueue := bk.getCandidateQueue(ctx)
item := tcr.Item{Value: identifier, Priority: int(blockNum)}
heap.Push(&candidateQueue, &item)
bk.setCandidateQueue(ctx, candidateQueue)
}
// ProposalQueueUpdate updates a candidate with new priority
func (bk BallotKeeper) ProposalQueueUpdate(ctx sdk.Context, identifier string, newBlockNum int64) sdk.Error {
candidateQueue := bk.getCandidateQueue(ctx)
err := candidateQueue.Update(identifier, int(newBlockNum))
if err != nil {
return err
}
bk.setCandidateQueue(ctx, candidateQueue)
return nil
}
func (bk BallotKeeper) ProposalQueueContains(ctx sdk.Context, identifier string) bool {
candidateQueue := bk.getCandidateQueue(ctx)
for _, item := range candidateQueue {
if item.Value == identifier {
return true
}
}
return false
}
func (bk BallotKeeper) ProposalQueueGetPriority(ctx sdk.Context, identifier string) int {
candidateQueue := bk.getCandidateQueue(ctx)
for _, item := range candidateQueue {
if item.Value == identifier {
return item.Priority
}
}
return -1
}