-
Notifications
You must be signed in to change notification settings - Fork 179
/
store.go
129 lines (112 loc) · 4.51 KB
/
store.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/okexchain/x/distribution/types"
)
// GetDelegatorWithdrawAddr returns the delegator withdraw address, defaulting to the delegator address
func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetDelegatorWithdrawAddrKey(delAddr))
if b == nil {
return delAddr
}
return sdk.AccAddress(b)
}
// SetDelegatorWithdrawAddr sets the delegator withdraw address
func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
}
// IterateDelegatorWithdrawAddrs iterates over delegator withdraw addrs
func (k Keeper) IterateDelegatorWithdrawAddrs(ctx sdk.Context,
handler func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.DelegatorWithdrawAddrPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
addr := sdk.AccAddress(iter.Value())
del := types.GetDelegatorWithdrawInfoAddress(iter.Key())
if handler(del, addr) {
break
}
}
}
// GetFeePool returns the global fee pool distribution info
func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.FeePoolKey)
if b == nil {
panic("Stored fee pool should not have been nil")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &feePool)
return
}
// SetFeePool sets the global fee pool distribution info
func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(feePool)
store.Set(types.FeePoolKey, b)
}
// GetFeePoolCommunityCoins returns the community coins
func (k Keeper) GetFeePoolCommunityCoins(ctx sdk.Context) sdk.SysCoins {
return k.GetFeePool(ctx).CommunityPool
}
// GetPreviousProposerConsAddr returns the proposer public key for this block
func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.ProposerKey)
if b == nil {
panic("Previous proposer not set")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &consAddr)
return consAddr
}
// SetPreviousProposerConsAddr sets the proposer public key for this block
func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(consAddr)
store.Set(types.ProposerKey, b)
}
// GetValidatorAccumulatedCommission returns accumulated commission for a validator
func (k Keeper) GetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) (
commission types.ValidatorAccumulatedCommission) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetValidatorAccumulatedCommissionKey(val))
if b == nil {
return types.ValidatorAccumulatedCommission{}
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &commission)
return commission
}
// SetValidatorAccumulatedCommission sets accumulated commission for a validator
func (k Keeper) SetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress,
commission types.ValidatorAccumulatedCommission) {
var bz []byte
store := ctx.KVStore(k.storeKey)
if commission.IsZero() {
bz = k.cdc.MustMarshalBinaryLengthPrefixed(types.InitialValidatorAccumulatedCommission())
} else {
bz = k.cdc.MustMarshalBinaryLengthPrefixed(commission)
}
store.Set(types.GetValidatorAccumulatedCommissionKey(val), bz)
}
// deleteValidatorAccumulatedCommission deletes accumulated commission for a validator
func (k Keeper) deleteValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetValidatorAccumulatedCommissionKey(val))
}
// IterateValidatorAccumulatedCommissions iterates over accumulated commissions
func (k Keeper) IterateValidatorAccumulatedCommissions(ctx sdk.Context,
handler func(val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.ValidatorAccumulatedCommissionPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var commission types.ValidatorAccumulatedCommission
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &commission)
addr := types.GetValidatorAccumulatedCommissionAddress(iter.Key())
if handler(addr, commission) {
break
}
}
}