forked from terra-money/classic-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
210 lines (178 loc) · 6.54 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
package budget
import (
"strconv"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/params"
)
// StoreKey is string representation of the store key for budget
const StoreKey = "budget"
// nolint
type Keeper struct {
key sdk.StoreKey // Key to our module's store
codespace sdk.CodespaceType // Reserves space for error codes
cdc *codec.Codec // Codec to encore/decode structs
valset sdk.ValidatorSet // Needed to compute voting power.
bk bank.Keeper // Needed to handle deposits. This module only requires read/writes to Terra balance
paramSpace params.Subspace
}
// NewKeeper crates a new keeper with write and read access
func NewKeeper(key sdk.StoreKey,
cdc *codec.Codec,
bk bank.Keeper,
codespace sdk.CodespaceType,
valset sdk.ValidatorSet,
paramspace params.Subspace) Keeper {
return Keeper{
key: key,
cdc: cdc,
bk: bk,
valset: valset,
codespace: codespace,
paramSpace: paramspace.WithKeyTable(ParamKeyTable()),
}
}
// Get the last used proposal ID
func (k Keeper) NewProgramID(ctx sdk.Context) (programID uint64) {
store := ctx.KVStore(k.key)
bz := store.Get(KeyNextProgramID)
if bz == nil {
programID = 0
} else {
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &programID)
programID++
}
bz = k.cdc.MustMarshalBinaryLengthPrefixed(programID)
store.Set(KeyNextProgramID, bz)
return
}
// GetProgram gets the Program with the given id from the context.
func (k Keeper) GetProgram(ctx sdk.Context, programID uint64) (res Program, err sdk.Error) {
store := ctx.KVStore(k.key)
bz := store.Get(KeyProgram(programID))
if bz == nil {
err = ErrProgramNotFound(programID)
return
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
return
}
// SetProgram sets a Program to the context
func (k Keeper) SetProgram(ctx sdk.Context, programID uint64, program Program) {
store := ctx.KVStore(k.key)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(program)
store.Set(KeyProgram(programID), bz)
}
// DeleteProgram deletes a program from the context
func (k Keeper) DeleteProgram(ctx sdk.Context, programID uint64) {
store := ctx.KVStore(k.key)
store.Delete(KeyProgram(programID))
}
// IteratePrograms iterates through programs in the store
func (k Keeper) IterateActivePrograms(ctx sdk.Context, handler func(uint64, Program) (stop bool)) {
store := ctx.KVStore(k.key)
iter := sdk.KVStorePrefixIterator(store, PrefixCandidateQueue)
for ; iter.Valid(); iter.Next() {
var programStoreKey string
var program Program
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Key(), &programStoreKey)
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &program)
if programID, err := strconv.Atoi(strings.Split(programStoreKey, ":")[1]); err == nil {
if k.CandidateQueueHas(ctx, program, uint64(programID)) {
continue
}
if handler(uint64(programID), program) {
break
}
}
}
iter.Close()
}
// GetVote returns the given option of a Program stored in the keeper
// Used to check if an address already voted
func (k Keeper) GetVote(ctx sdk.Context, programID uint64, voter sdk.AccAddress) (res bool, err sdk.Error) {
store := ctx.KVStore(k.key)
bz := store.Get(KeyVote(programID, voter))
if bz == nil {
err = ErrVoteNotFound()
return
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
return
}
// SetVote sets the vote option to the Program stored in the context store
func (k Keeper) SetVote(ctx sdk.Context, programID uint64, voter sdk.AccAddress, option bool) {
store := ctx.KVStore(k.key)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(option)
store.Set(KeyVote(programID, voter), bz)
}
func (k Keeper) ClearVotesForProgram(ctx sdk.Context, programID uint64) {
store := ctx.KVStore(k.key)
iter := sdk.KVStorePrefixIterator(store, PrefixVoteForProgram(programID))
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
iter.Close()
}
// RefundDeposit refunds the deposit
func (k Keeper) RefundDeposit(ctx sdk.Context, programID uint64) (err sdk.Error) {
program, err := k.GetProgram(ctx, programID)
if err != nil {
return err
}
_, _, err = k.bk.AddCoins(ctx, program.Submitter, sdk.Coins{program.Deposit})
return
}
//______________________________________________________________________
// GetParams get oralce params from the global param store
func (k Keeper) GetParams(ctx sdk.Context) Params {
var resultParams Params
k.paramSpace.Get(ctx, ParamStoreKeyParams, &resultParams)
return resultParams
}
// SetParams set oracle params from the global param store
func (k Keeper) SetParams(ctx sdk.Context, params Params) {
k.paramSpace.Set(ctx, ParamStoreKeyParams, ¶ms)
}
// =====================================================
// ProgramQueues
// IterateMatureCandidates Returns an iterator for all the Programs in the Candidate Queue that expire by endTime
func (k Keeper) IterateMatureCandidates(ctx sdk.Context, endTime time.Time, handler func(uint64, Program) (stop bool)) {
store := ctx.KVStore(k.key)
iter := store.Iterator(PrefixCandidateQueue, sdk.PrefixEndBytes(PrefixCandidateQueueTime(endTime)))
for ; iter.Valid(); iter.Next() {
var programStoreKey string
var program Program
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Key(), &programStoreKey)
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &program)
if programID, err := strconv.Atoi(strings.Split(programStoreKey, ":")[1]); err == nil {
if handler(uint64(programID), program) {
break
}
}
}
iter.Close()
}
// CandidateQueueInsert Inserts a ProgramID into the Candidate Program queue at endTime
func (k Keeper) CandidateQueueInsert(ctx sdk.Context, program Program, programID uint64) {
votingEndTime := program.getVotingEndTime(k.GetParams(ctx).VotePeriod)
store := ctx.KVStore(k.key)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(programID)
store.Set(KeyCandidate(votingEndTime, programID), bz)
}
// CandidateQueueHas Checks if a progrma exists in accordance with the given parameters
func (k Keeper) CandidateQueueHas(ctx sdk.Context, program Program, programID uint64) (res bool) {
votingEndTime := program.getVotingEndTime(k.GetParams(ctx).VotePeriod)
store := ctx.KVStore(k.key)
bz := store.Get(KeyCandidate(votingEndTime, programID))
return bz != nil
}
// CandidateQueueRemove removes a ProgramID from the Candidate Program Queue
func (k Keeper) CandidateQueueRemove(ctx sdk.Context, program Program, programID uint64) {
votingEndTime := program.getVotingEndTime(k.GetParams(ctx).VotePeriod)
store := ctx.KVStore(k.key)
store.Delete(KeyCandidate(votingEndTime, programID))
}