forked from CosmWasm/wasmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams_legacy.go
142 lines (123 loc) · 3.54 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
/*
NOTE: Usage of x/params to manage parameters is deprecated in favor of x/gov
controlled execution of MsgUpdateParams messages. These types remains solely
for migration purposes and will be removed in a future release.
*/
package v2
import (
"encoding/json"
"fmt"
"github.com/cosmos/gogoproto/jsonpb"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
var (
ParamStoreKeyUploadAccess = []byte("uploadAccess")
ParamStoreKeyInstantiateAccess = []byte("instantiateAccess")
)
// Deprecated: Type declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyUploadAccess, &p.CodeUploadAccess, validateAccessConfig),
paramtypes.NewParamSetPair(ParamStoreKeyInstantiateAccess, &p.InstantiateDefaultPermission, validateAccessType),
}
}
func validateAccessConfig(i interface{}) error {
v, ok := i.(AccessConfig)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return v.ValidateBasic()
}
var AllAccessTypes = []AccessType{
AccessTypeNobody,
AccessTypeOnlyAddress,
AccessTypeAnyOfAddresses,
AccessTypeEverybody,
}
func validateAccessType(i interface{}) error {
a, ok := i.(AccessType)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if a == AccessTypeUnspecified {
return errorsmod.Wrap(types.ErrEmpty, "type")
}
for _, v := range AllAccessTypes {
if v == a {
return nil
}
}
return errorsmod.Wrapf(types.ErrInvalid, "unknown type: %q", a)
}
func (a AccessConfig) ValidateBasic() error {
switch a.Permission {
case AccessTypeUnspecified:
return errorsmod.Wrap(types.ErrEmpty, "type")
case AccessTypeNobody, AccessTypeEverybody:
return nil
case AccessTypeOnlyAddress:
_, err := sdk.AccAddressFromBech32(a.Address)
return errorsmod.Wrap(err, "only address")
case AccessTypeAnyOfAddresses:
return errorsmod.Wrap(assertValidAddresses(a.Addresses), "addresses")
}
return errorsmod.Wrapf(types.ErrInvalid, "unknown type: %q", a.Permission)
}
func assertValidAddresses(addrs []string) error {
if len(addrs) == 0 {
return types.ErrEmpty
}
idx := make(map[string]struct{}, len(addrs))
for _, a := range addrs {
if _, err := sdk.AccAddressFromBech32(a); err != nil {
return errorsmod.Wrapf(err, "address: %s", a)
}
if _, exists := idx[a]; exists {
return types.ErrDuplicate.Wrapf("address: %s", a)
}
idx[a] = struct{}{}
}
return nil
}
func (a AccessType) String() string {
switch a {
case AccessTypeNobody:
return "Nobody"
case AccessTypeOnlyAddress:
return "OnlyAddress"
case AccessTypeEverybody:
return "Everybody"
case AccessTypeAnyOfAddresses:
return "AnyOfAddresses"
}
return "Unspecified"
}
func (a *AccessType) UnmarshalText(text []byte) error {
for _, v := range AllAccessTypes {
if v.String() == string(text) {
*a = v
return nil
}
}
*a = AccessTypeUnspecified
return nil
}
func (a AccessType) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
func (a *AccessType) MarshalJSONPB(_ *jsonpb.Marshaler) ([]byte, error) {
return json.Marshal(a)
}
func (a *AccessType) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, data []byte) error {
return json.Unmarshal(data, a)
}
func (a AccessConfig) Equals(o AccessConfig) bool {
return a.Permission == o.Permission && a.Address == o.Address
}