forked from babylonlabs-io/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
133 lines (107 loc) · 3 KB
/
params.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
package keeper
import (
"context"
"encoding/binary"
"fmt"
"cosmossdk.io/math"
"cosmossdk.io/store/prefix"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/cosmos/cosmos-sdk/runtime"
)
// cosmos-sdk does not have utils for uint32
func uint32ToBytes(v uint32) []byte {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], v)
return buf[:]
}
func uint32FromBytes(b []byte) (uint32, error) {
if len(b) != 4 {
return 0, fmt.Errorf("invalid uint32 bytes length: %d", len(b))
}
return binary.BigEndian.Uint32(b), nil
}
func mustUint32FromBytes(b []byte) uint32 {
v, err := uint32FromBytes(b)
if err != nil {
panic(err)
}
return v
}
func (k Keeper) paramsStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.ParamsKey)
}
func (k Keeper) nextParamsVersion(ctx context.Context) uint32 {
paramsStore := k.paramsStore(ctx)
it := paramsStore.ReverseIterator(nil, nil)
defer it.Close()
if !it.Valid() {
return 0
}
return mustUint32FromBytes(it.Key()) + 1
}
func (k Keeper) getLastParams(ctx context.Context) *types.StoredParams {
paramsStore := k.paramsStore(ctx)
it := paramsStore.ReverseIterator(nil, nil)
defer it.Close()
if !it.Valid() {
return nil
}
var sp types.StoredParams
k.cdc.MustUnmarshal(it.Value(), &sp)
return &sp
}
// SetParams sets the x/btcstaking module parameters.
func (k Keeper) SetParams(ctx context.Context, p types.Params) error {
if err := p.Validate(); err != nil {
return err
}
nextVersion := k.nextParamsVersion(ctx)
paramsStore := k.paramsStore(ctx)
sp := types.StoredParams{
Params: p,
Version: nextVersion,
}
paramsStore.Set(uint32ToBytes(nextVersion), k.cdc.MustMarshal(&sp))
return nil
}
func (k Keeper) GetAllParams(ctx context.Context) []*types.Params {
paramsStore := k.paramsStore(ctx)
it := paramsStore.Iterator(nil, nil)
defer it.Close()
var p []*types.Params
for ; it.Valid(); it.Next() {
var sp types.StoredParams
k.cdc.MustUnmarshal(it.Value(), &sp)
p = append(p, &sp.Params)
}
return p
}
func (k Keeper) GetParamsByVersion(ctx context.Context, v uint32) *types.Params {
paramsStore := k.paramsStore(ctx)
spBytes := paramsStore.Get(uint32ToBytes(v))
if len(spBytes) == 0 {
return nil
}
var sp types.StoredParams
k.cdc.MustUnmarshal(spBytes, &sp)
return &sp.Params
}
func mustGetLastParams(ctx context.Context, k Keeper) types.StoredParams {
sp := k.getLastParams(ctx)
if sp == nil {
panic("last params not found")
}
return *sp
}
// GetParams returns the latest x/btcstaking module parameters.
func (k Keeper) GetParams(ctx context.Context) types.Params {
return mustGetLastParams(ctx, k).Params
}
func (k Keeper) GetParamsWithVersion(ctx context.Context) types.StoredParams {
return mustGetLastParams(ctx, k)
}
// MinCommissionRate returns the minimal commission rate of finality providers
func (k Keeper) MinCommissionRate(ctx context.Context) math.LegacyDec {
return k.GetParams(ctx).MinCommissionRate
}