forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
103 lines (83 loc) · 2.5 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
package keeper
import (
"fmt"
"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// Implements ValidatorSet interface
var _ types.ValidatorSet = Keeper{}
// Implements DelegationSet interface
var _ types.DelegationSet = Keeper{}
// Keeper of the x/staking store
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
hooks types.StakingHooks
authority string
}
// NewKeeper creates a new staking Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
ak types.AccountKeeper,
bk types.BankKeeper,
authority string,
) *Keeper {
// ensure bonded and not bonded module accounts are set
if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
}
if addr := ak.GetModuleAddress(types.NotBondedPoolName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}
// ensure that authority is a valid AccAddress
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(("authority is not a valid acc address"))
}
return &Keeper{
storeKey: key,
cdc: cdc,
authKeeper: ak,
bankKeeper: bk,
hooks: nil,
authority: authority,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
// SetHooks Set the validator hooks
func (k *Keeper) SetHooks(sh types.StakingHooks) {
if k.hooks != nil {
panic("cannot set validator hooks twice")
}
k.hooks = sh
}
// GetLastTotalPower Load the last total validator power.
func (k Keeper) GetLastTotalPower(ctx sdk.Context) math.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastTotalPowerKey)
if bz == nil {
return math.ZeroInt()
}
ip := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &ip)
return ip.Int
}
// SetLastTotalPower Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power math.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
store.Set(types.LastTotalPowerKey, bz)
}
// GetAuthority returns the x/staking module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}