forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams_legacy.go
147 lines (111 loc) · 3.79 KB
/
params_legacy.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
140
141
142
143
144
145
146
147
/*
Deprecated.
Legacy types are defined below to aid in the migration of CometBFT consensus
parameters from use of the now deprecated x/params modules to a new dedicated
x/consensus module.
Application developers should ensure that they implement their upgrade handler
correctly such that app.ConsensusParamsKeeper.Set() is called with the values
returned by GetConsensusParams().
Example:
baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
if cp := baseapp.GetConsensusParams(ctx, baseAppLegacySS); cp != nil {
app.ConsensusParamsKeeper.Set(ctx, cp)
} else {
ctx.Logger().Info("warning: consensus parameters are undefined; skipping migration", "upgrade", UpgradeName)
}
return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
)
Developers can also bypass the use of the legacy Params subspace and set the
values to app.ConsensusParamsKeeper.Set() explicitly.
Note, for new chains this is not necessary as CometBFT's consensus parameters
will automatically be set for you in InitChain.
*/
package baseapp
import (
"errors"
"fmt"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const Paramspace = "baseapp"
var (
ParamStoreKeyBlockParams = []byte("BlockParams")
ParamStoreKeyEvidenceParams = []byte("EvidenceParams")
ParamStoreKeyValidatorParams = []byte("ValidatorParams")
)
type LegacyParamStore interface {
Get(ctx sdk.Context, key []byte, ptr interface{})
Has(ctx sdk.Context, key []byte) bool
}
func ValidateBlockParams(i interface{}) error {
v, ok := i.(cmtproto.BlockParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.MaxBytes <= 0 {
return fmt.Errorf("block maximum bytes must be positive: %d", v.MaxBytes)
}
if v.MaxGas < -1 {
return fmt.Errorf("block maximum gas must be greater than or equal to -1: %d", v.MaxGas)
}
return nil
}
func ValidateEvidenceParams(i interface{}) error {
v, ok := i.(cmtproto.EvidenceParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.MaxAgeNumBlocks <= 0 {
return fmt.Errorf("evidence maximum age in blocks must be positive: %d", v.MaxAgeNumBlocks)
}
if v.MaxAgeDuration <= 0 {
return fmt.Errorf("evidence maximum age time duration must be positive: %v", v.MaxAgeDuration)
}
if v.MaxBytes < 0 {
return fmt.Errorf("maximum evidence bytes must be non-negative: %v", v.MaxBytes)
}
return nil
}
func ValidateValidatorParams(i interface{}) error {
v, ok := i.(cmtproto.ValidatorParams)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if len(v.PubKeyTypes) == 0 {
return errors.New("validator allowed pubkey types must not be empty")
}
return nil
}
func GetConsensusParams(ctx sdk.Context, paramStore LegacyParamStore) *cmtproto.ConsensusParams {
if paramStore == nil {
return nil
}
cp := new(cmtproto.ConsensusParams)
if paramStore.Has(ctx, ParamStoreKeyBlockParams) {
var bp cmtproto.BlockParams
paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp)
cp.Block = &bp
}
if paramStore.Has(ctx, ParamStoreKeyEvidenceParams) {
var ep cmtproto.EvidenceParams
paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep)
cp.Evidence = &ep
}
if paramStore.Has(ctx, ParamStoreKeyValidatorParams) {
var vp cmtproto.ValidatorParams
paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp)
cp.Validator = &vp
}
return cp
}
func MigrateParams(ctx sdk.Context, lps LegacyParamStore, ps ParamStore) {
if cp := GetConsensusParams(ctx, lps); cp != nil {
ps.Set(ctx, cp)
} else {
ctx.Logger().Info("warning: consensus parameters are undefined; skipping migration")
}
}