-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathparams.go
127 lines (113 loc) · 4.53 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
package auth
import (
"fmt"
"strings"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
)
type AuthParamsContextKey struct{}
// Default parameter values
const (
DefaultMaxMemoBytes int64 = 65536
DefaultTxSigLimit int64 = 7
DefaultTxSizeCostPerByte int64 = 10
DefaultSigVerifyCostED25519 int64 = 590
DefaultSigVerifyCostSecp256k1 int64 = 1000
DefaultGasPricesChangeCompressor int64 = 10
DefaultTargetGasRatio int64 = 70 // 70% of the MaxGas in a block
)
// Params defines the parameters for the auth module.
type Params struct {
MaxMemoBytes int64 `json:"max_memo_bytes" yaml:"max_memo_bytes"`
TxSigLimit int64 `json:"tx_sig_limit" yaml:"tx_sig_limit"`
TxSizeCostPerByte int64 `json:"tx_size_cost_per_byte" yaml:"tx_size_cost_per_byte"`
SigVerifyCostED25519 int64 `json:"sig_verify_cost_ed25519" yaml:"sig_verify_cost_ed25519"`
SigVerifyCostSecp256k1 int64 `json:"sig_verify_cost_secp256k1" yaml:"sig_verify_cost_secp256k1"`
GasPricesChangeCompressor int64 `json:"gas_price_change_compressor" yaml:"gas_price_change_compressor"`
TargetGasRatio int64 `json:"target_gas_ratio" yaml:"target_gas_ratio"`
InitialGasPrice std.GasPrice `json:"initial_gasprice"`
}
// NewParams creates a new Params object
func NewParams(maxMemoBytes, txSigLimit, txSizeCostPerByte,
sigVerifyCostED25519, sigVerifyCostSecp256k1, gasPricesChangeCompressor, targetGasRatio int64,
) Params {
return Params{
MaxMemoBytes: maxMemoBytes,
TxSigLimit: txSigLimit,
TxSizeCostPerByte: txSizeCostPerByte,
SigVerifyCostED25519: sigVerifyCostED25519,
SigVerifyCostSecp256k1: sigVerifyCostSecp256k1,
GasPricesChangeCompressor: gasPricesChangeCompressor,
TargetGasRatio: targetGasRatio,
}
}
// Equals returns a boolean determining if two Params types are identical.
func (p Params) Equals(p2 Params) bool {
return amino.DeepEqual(p, p2)
}
// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return Params{
MaxMemoBytes: DefaultMaxMemoBytes,
TxSigLimit: DefaultTxSigLimit,
TxSizeCostPerByte: DefaultTxSizeCostPerByte,
SigVerifyCostED25519: DefaultSigVerifyCostED25519,
SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1,
GasPricesChangeCompressor: DefaultGasPricesChangeCompressor,
TargetGasRatio: DefaultTargetGasRatio,
}
}
// String implements the stringer interface.
func (p Params) String() string {
var builder strings.Builder
sb := &builder // Pointer for use with fmt.Fprintf
sb.WriteString("Params: \n")
fmt.Fprintf(sb, "MaxMemoBytes: %d\n", p.MaxMemoBytes)
fmt.Fprintf(sb, "TxSigLimit: %d\n", p.TxSigLimit)
fmt.Fprintf(sb, "TxSizeCostPerByte: %d\n", p.TxSizeCostPerByte)
fmt.Fprintf(sb, "SigVerifyCostED25519: %d\n", p.SigVerifyCostED25519)
fmt.Fprintf(sb, "SigVerifyCostSecp256k1: %d\n", p.SigVerifyCostSecp256k1)
fmt.Fprintf(sb, "GasPricesChangeCompressor: %d\n", p.GasPricesChangeCompressor)
fmt.Fprintf(sb, "TargetGasRatio: %d\n", p.TargetGasRatio)
return sb.String()
}
func (p Params) Validate() error {
if p.TxSigLimit == 0 {
return fmt.Errorf("invalid tx signature limit: %d", p.TxSigLimit)
}
if p.SigVerifyCostED25519 == 0 {
return fmt.Errorf("invalid ED25519 signature verification cost: %d", p.SigVerifyCostED25519)
}
if p.SigVerifyCostSecp256k1 == 0 {
return fmt.Errorf("invalid SECK256k1 signature verification cost: %d", p.SigVerifyCostSecp256k1)
}
if p.TxSizeCostPerByte == 0 {
return fmt.Errorf("invalid tx size cost per byte: %d", p.TxSizeCostPerByte)
}
if p.GasPricesChangeCompressor <= 0 {
return fmt.Errorf("invalid gas prices change compressor: %d, it should be larger or equal to 1", p.GasPricesChangeCompressor)
}
if p.TargetGasRatio < 0 || p.TargetGasRatio > 100 {
return fmt.Errorf("invalid target block gas ratio: %d, it should be between 0 and 100, 0 is unlimited", p.TargetGasRatio)
}
return nil
}
func (ak AccountKeeper) SetParams(ctx sdk.Context, params Params) error {
if err := params.Validate(); err != nil {
return err
}
err := ak.paramk.SetParams(ctx, ModuleName, params)
return err
}
func (ak AccountKeeper) GetParams(ctx sdk.Context) Params {
params := &Params{}
ok, err := ak.paramk.GetParams(ctx, ModuleName, params)
if !ok {
panic("params key " + ModuleName + " does not exist")
}
if err != nil {
panic(err.Error())
}
return *params
}