forked from cosmos/cosmos-sdk
/
keeper.go
95 lines (81 loc) · 2.33 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
package keeper
import (
"container/list"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
const aminoCacheSize = 500
// keeper of the staking store
type Keeper struct {
storeKey sdk.StoreKey
storeTKey sdk.StoreKey
cdc *codec.Codec
bankKeeper types.BankKeeper
hooks sdk.StakingHooks
paramstore params.Subspace
validatorCache map[string]cachedValidator
validatorCacheList *list.List
// codespace
codespace sdk.CodespaceType
}
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, bk types.BankKeeper,
paramstore params.Subspace, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
storeTKey: tkey,
cdc: cdc,
bankKeeper: bk,
paramstore: paramstore.WithKeyTable(ParamKeyTable()),
hooks: nil,
validatorCache: make(map[string]cachedValidator, aminoCacheSize),
validatorCacheList: list.New(),
codespace: codespace,
}
return keeper
}
// Set the validator hooks
func (k *Keeper) SetHooks(sh sdk.StakingHooks) *Keeper {
if k.hooks != nil {
panic("cannot set validator hooks twice")
}
k.hooks = sh
return k
}
// return the codespace
func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}
// get the pool
func (k Keeper) GetPool(ctx sdk.Context) (pool types.Pool) {
store := ctx.KVStore(k.storeKey)
b := store.Get(PoolKey)
if b == nil {
panic("stored pool should not have been nil")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &pool)
return
}
// set the pool
func (k Keeper) SetPool(ctx sdk.Context, pool types.Pool) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(pool)
store.Set(PoolKey, b)
}
// Load the last total validator power.
func (k Keeper) GetLastTotalPower(ctx sdk.Context) (power sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := store.Get(LastTotalPowerKey)
if b == nil {
return sdk.ZeroInt()
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &power)
return
}
// Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(power)
store.Set(LastTotalPowerKey, b)
}