-
Notifications
You must be signed in to change notification settings - Fork 7
/
keeper.go
111 lines (90 loc) · 3.23 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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/anathatech/project-anatha/x/governance/internal/types"
"time"
"github.com/tendermint/tendermint/libs/log"
)
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
paramSpace params.Subspace
router gov.Router
AccountKeeper auth.AccountKeeper
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, router gov.Router, accountKeeper auth.AccountKeeper,) Keeper {
return Keeper{
storeKey: key,
paramSpace: paramSpace,
cdc: cdc,
router: router,
AccountKeeper: accountKeeper,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) Router() gov.Router {
return k.router
}
// Proposal Queue
func (k Keeper) InsertActiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) {
store := ctx.KVStore(k.storeKey)
bz := types.GetProposalIDBytes(proposalID)
store.Set(types.ActiveProposalQueueKey(proposalID, endTime), bz)
}
func (k Keeper) RemoveFromActiveProposalQueue(ctx sdk.Context, proposalID uint64, endTime time.Time) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ActiveProposalQueueKey(proposalID, endTime))
}
func (k Keeper) InsertExpeditedProposalQueue(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(k.storeKey)
bz := types.GetProposalIDBytes(proposalID)
store.Set(types.ExpeditedProposalQueueKey(proposalID), bz)
}
func (k Keeper) RemoveFromExpeditedProposalQueue(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ExpeditedProposalQueueKey(proposalID))
}
// Iterators
func (k Keeper) IterateActiveProposalsQueue(ctx sdk.Context, endTime time.Time, cb func(proposal types.Proposal) (stop bool)) {
iterator := k.ActiveProposalQueueIterator(ctx, endTime)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
proposalID, _ := types.SplitActiveProposalQueueKey(iterator.Key())
proposal, err := k.GetProposal(ctx, proposalID)
if err != nil {
panic(fmt.Sprintf("proposal %d does not exist", proposalID))
}
if cb(proposal) {
break
}
}
}
func (k Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return store.Iterator(types.ActiveProposalQueuePrefix, sdk.PrefixEndBytes(types.ActiveProposalByTimeKey(endTime)))
}
func (k Keeper) IterateExpeditedProposalsQueue(ctx sdk.Context, cb func(proposal types.Proposal) (stop bool)) {
iterator := k.ExpeditedProposalQueueIterator(ctx)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
proposalID := types.SplitExpeditedProposalQueueKey(iterator.Key())
proposal, err := k.GetProposal(ctx, proposalID)
if err != nil {
panic(fmt.Sprintf("proposal %d does not exist", proposalID))
}
if cb(proposal) {
break
}
}
}
func (k Keeper) ExpeditedProposalQueueIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.ExpeditedProposalQueuePrefix)
}