-
Notifications
You must be signed in to change notification settings - Fork 41
/
params.go
139 lines (115 loc) · 3.65 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
134
135
136
137
138
139
package types
import (
"fmt"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
var _ paramtypes.ParamSet = &Params{}
const (
GasForFeeDeduction uint64 = 13419
GasForFeeRefund uint64 = 13419
DefaultGasContractCall uint64 = 63558
MinExecutionGasLimit = GasForFeeDeduction + GasForFeeRefund + DefaultGasContractCall
)
// Wasmx params default values
var (
DefaultIsExecutionEnabled = false
DefaultMaxBeginBlockTotalGas uint64 = 42_000_000 // 42M
DefaultMaxContractGasLimit uint64 = DefaultMaxBeginBlockTotalGas / 12 // 3.5M
DefaultMinGasPrice uint64 = 1_000_000_000 // 1B
)
// Parameter keys
var (
KeyIsExecutionEnabled = []byte("IsExecutionEnabled")
KeyMaxBeginBlockTotalGas = []byte("MaxBeginBlockTotalGas")
KeyMaxContractGasLimit = []byte("MaxContractGasLimit")
KeyMinGasPrice = []byte("MinGasPrice")
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// NewParams creates a new Params instance
func NewParams(
isExecutionEnabled bool,
maxBeginBlockTotalGas uint64,
maxContractGasLimit uint64,
minGasPrice uint64,
) Params {
return Params{
IsExecutionEnabled: isExecutionEnabled,
MaxBeginBlockTotalGas: maxBeginBlockTotalGas,
MaxContractGasLimit: maxContractGasLimit,
MinGasPrice: minGasPrice,
}
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMinGasPrice, &p.MinGasPrice, validateMinGasPrice),
paramtypes.NewParamSetPair(KeyIsExecutionEnabled, &p.IsExecutionEnabled, validateIsExecutionEnabled),
paramtypes.NewParamSetPair(KeyMaxBeginBlockTotalGas, &p.MaxBeginBlockTotalGas, validateMaxBeginBlockTotalGas),
paramtypes.NewParamSetPair(KeyMaxContractGasLimit, &p.MaxContractGasLimit, validateMaxContractGasLimit),
}
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return Params{
IsExecutionEnabled: DefaultIsExecutionEnabled,
MaxBeginBlockTotalGas: DefaultMaxBeginBlockTotalGas,
MaxContractGasLimit: DefaultMaxContractGasLimit,
MinGasPrice: DefaultMinGasPrice,
}
}
// Validate performs basic validation on wasmx parameters.
func (p Params) Validate() error {
if err := validateIsExecutionEnabled(p.IsExecutionEnabled); err != nil {
return err
}
if err := validateMaxBeginBlockTotalGas(p.MaxBeginBlockTotalGas); err != nil {
return err
}
if err := validateMaxContractGasLimit(p.MaxContractGasLimit); err != nil {
return err
}
if err := validateMinGasPrice(p.MinGasPrice); err != nil {
return err
}
return nil
}
func validateMaxBeginBlockTotalGas(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("MaxBeginBlockTotalGas must be positive: %d", v)
}
return nil
}
func validateMaxContractGasLimit(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v < MinExecutionGasLimit {
return fmt.Errorf("MaxContractGasLimit %d must be greater than the MinExecutionGasLimit: %d", v, MinExecutionGasLimit)
}
return nil
}
func validateIsExecutionEnabled(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateMinGasPrice(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("MinGasPrice must be positive: %d", v)
}
return nil
}