forked from crescent-network/crescent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
117 lines (101 loc) 路 3.28 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
var _ paramstypes.ParamSet = (*Params)(nil)
var (
KeyPrivatePlanCreationFee = []byte("PrivatePlanCreationFee")
KeyFeeCollector = []byte("FeeCollector")
KeyMaxNumPrivatePlans = []byte("MaxNumPrivatePlans")
KeyMaxBlockDuration = []byte("MaxBlockDuration")
)
const (
DefaultMaxNumPrivatePlans = 50
DefaultMaxBlockDuration = 10 * time.Second
MaxPlanDescriptionLen = 200 // Maximum length of a plan's description
)
var (
DefaultPrivatePlanCreationFee = sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 100_000000))
DefaultFeeCollector = sdk.AccAddress(address.Module(ModuleName, []byte("FeeCollector")))
RewardsPoolAddress = address.Module(ModuleName, []byte("RewardsPool"))
)
func ParamKeyTable() paramstypes.KeyTable {
return paramstypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns a default params for the module.
func DefaultParams() Params {
return Params{
PrivatePlanCreationFee: DefaultPrivatePlanCreationFee,
FeeCollector: DefaultFeeCollector.String(),
MaxNumPrivatePlans: DefaultMaxNumPrivatePlans,
MaxBlockDuration: DefaultMaxBlockDuration,
}
}
// ParamSetPairs implements ParamSet.
func (params *Params) ParamSetPairs() paramstypes.ParamSetPairs {
return paramstypes.ParamSetPairs{
paramstypes.NewParamSetPair(KeyPrivatePlanCreationFee, ¶ms.PrivatePlanCreationFee, validatePrivatePlanCreationFee),
paramstypes.NewParamSetPair(KeyFeeCollector, ¶ms.FeeCollector, validateFeeCollector),
paramstypes.NewParamSetPair(KeyMaxNumPrivatePlans, ¶ms.MaxNumPrivatePlans, validateMaxNumPrivatePlans),
paramstypes.NewParamSetPair(KeyMaxBlockDuration, ¶ms.MaxBlockDuration, validateMaxBlockDuration),
}
}
// Validate validates Params.
func (params Params) Validate() error {
for _, field := range []struct {
val interface{}
validateFunc func(i interface{}) error
}{
{params.PrivatePlanCreationFee, validatePrivatePlanCreationFee},
{params.FeeCollector, validateFeeCollector},
{params.MaxNumPrivatePlans, validateMaxNumPrivatePlans},
{params.MaxBlockDuration, validateMaxBlockDuration},
} {
if err := field.validateFunc(field.val); err != nil {
return err
}
}
return nil
}
func validatePrivatePlanCreationFee(i interface{}) error {
v, ok := i.(sdk.Coins)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if err := v.Validate(); err != nil {
return fmt.Errorf("invalid private plan creation fee: %w", err)
}
return nil
}
func validateFeeCollector(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
_, err := sdk.AccAddressFromBech32(v)
if err != nil {
return fmt.Errorf("invalid fee collector address: %v", v)
}
return nil
}
func validateMaxNumPrivatePlans(i interface{}) error {
_, ok := i.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateMaxBlockDuration(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v <= 0 {
return fmt.Errorf("max block duration must be positive")
}
return nil
}