-
Notifications
You must be signed in to change notification settings - Fork 257
/
params.go
95 lines (78 loc) · 2.48 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
package types
import (
"fmt"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)
var _ paramtypes.ParamSet = (*Params)(nil)
var (
KeyGasPerBlobByte = []byte("GasPerBlobByte")
DefaultGasPerBlobByte uint32 = appconsts.DefaultGasPerBlobByte
KeyGovMaxSquareSize = []byte("GovMaxSquareSize")
DefaultGovMaxSquareSize uint64 = appconsts.DefaultGovMaxSquareSize
)
// ParamKeyTable returns the param key table for the blob module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params instance
func NewParams(gasPerBlobByte uint32, govMaxSquareSize uint64) Params {
return Params{
GasPerBlobByte: gasPerBlobByte,
GovMaxSquareSize: govMaxSquareSize,
}
}
// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultGasPerBlobByte, appconsts.DefaultGovMaxSquareSize)
}
// ParamSetPairs gets the list of param key-value pairs
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyGasPerBlobByte, &p.GasPerBlobByte, validateGasPerBlobByte),
paramtypes.NewParamSetPair(KeyGovMaxSquareSize, &p.GovMaxSquareSize, validateGovMaxSquareSize),
}
}
// Validate validates the set of params
func (p Params) Validate() error {
err := validateGasPerBlobByte(p.GasPerBlobByte)
if err != nil {
return err
}
return validateGovMaxSquareSize(p.GovMaxSquareSize)
}
// String implements the Stringer interface.
func (p Params) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// validateGasPerBlobByte validates the GasPerBlobByte param
func validateGasPerBlobByte(v interface{}) error {
gasPerBlobByte, ok := v.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if gasPerBlobByte == 0 {
return fmt.Errorf("gas per blob byte cannot be 0")
}
return nil
}
// validateGovMaxSquareSize validates the GovMaxSquareSize param
func validateGovMaxSquareSize(v interface{}) error {
govMaxSquareSize, ok := v.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}
if govMaxSquareSize == 0 {
return fmt.Errorf("gov max square size cannot be zero")
}
if !shares.IsPowerOfTwo(govMaxSquareSize) {
return fmt.Errorf(
"gov max square size must be a power of two: %d",
govMaxSquareSize,
)
}
return nil
}