-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
authz.go
195 lines (165 loc) · 5.76 KB
/
authz.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package types
import (
context "context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
)
// TODO: Revisit this once we have propoer gas fee framework.
// Tracking issues https://github.com/cosmos/cosmos-sdk/issues/9054, https://github.com/cosmos/cosmos-sdk/discussions/9072
const gasCostPerIteration = uint64(10)
var _ authz.Authorization = &StakeAuthorization{}
// NewStakeAuthorization creates a new StakeAuthorization object.
func NewStakeAuthorization(allowed, denied []sdk.ValAddress, authzType AuthorizationType, amount *sdk.Coin) (*StakeAuthorization, error) {
allowedValidators, deniedValidators, err := validateAllowAndDenyValidators(allowed, denied)
if err != nil {
return nil, err
}
a := StakeAuthorization{}
if allowedValidators != nil {
a.Validators = &StakeAuthorization_AllowList{
AllowList: &StakeAuthorization_Validators{
Address: allowedValidators,
},
}
} else {
a.Validators = &StakeAuthorization_DenyList{
DenyList: &StakeAuthorization_Validators{
Address: deniedValidators,
},
}
}
if amount != nil {
a.MaxTokens = amount
}
a.AuthorizationType = authzType
return &a, nil
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a StakeAuthorization) MsgTypeURL() string {
authzType, err := normalizeAuthzType(a.AuthorizationType)
if err != nil {
panic(err)
}
return authzType
}
// ValidateBasic performs a stateless validation of the fields.
// It fails if MaxTokens is either undefined or negative or if the authorization
// is unspecified.
func (a StakeAuthorization) ValidateBasic() error {
if a.MaxTokens != nil && a.MaxTokens.IsNegative() {
return errorsmod.Wrapf(authz.ErrNegativeMaxTokens, "negative coin amount: %v", a.MaxTokens)
}
if a.AuthorizationType == AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED {
return authz.ErrUnknownAuthorizationType
}
return nil
}
// Accept implements Authorization.Accept. It checks, that the validator is not in the denied list,
// and, should the allowed list not be empty, if the validator is in the allowed list.
// If these conditions are met, the authorization amount is validated and if successful, the
// corresponding AcceptResponse is returned.
func (a StakeAuthorization) Accept(ctx context.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
var (
validatorAddress string
amount sdk.Coin
)
switch msg := msg.(type) {
case *MsgDelegate:
validatorAddress = msg.ValidatorAddress
amount = msg.Amount
case *MsgUndelegate:
validatorAddress = msg.ValidatorAddress
amount = msg.Amount
case *MsgBeginRedelegate:
validatorAddress = msg.ValidatorDstAddress
amount = msg.Amount
case *MsgCancelUnbondingDelegation:
validatorAddress = msg.ValidatorAddress
amount = msg.Amount
default:
return authz.AcceptResponse{}, sdkerrors.ErrInvalidRequest.Wrap("unknown msg type")
}
isValidatorExists := false
allowedList := a.GetAllowList().GetAddress()
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, validator := range allowedList {
sdkCtx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
isValidatorExists = true
break
}
}
denyList := a.GetDenyList().GetAddress()
for _, validator := range denyList {
sdkCtx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("cannot delegate/undelegate to %s validator", validator)
}
}
if len(allowedList) > 0 && !isValidatorExists {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf("cannot delegate/undelegate to %s validator", validatorAddress)
}
if a.MaxTokens == nil {
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &StakeAuthorization{
Validators: a.GetValidators(),
AuthorizationType: a.GetAuthorizationType(),
},
}, nil
}
limitLeft, err := a.MaxTokens.SafeSub(amount)
if err != nil {
return authz.AcceptResponse{}, err
}
if limitLeft.IsZero() {
return authz.AcceptResponse{Accept: true, Delete: true}, nil
}
return authz.AcceptResponse{
Accept: true,
Delete: false,
Updated: &StakeAuthorization{
Validators: a.GetValidators(),
AuthorizationType: a.GetAuthorizationType(),
MaxTokens: &limitLeft,
},
}, nil
}
func validateAllowAndDenyValidators(allowed, denied []sdk.ValAddress) ([]string, []string, error) {
if len(allowed) == 0 && len(denied) == 0 {
return nil, nil, sdkerrors.ErrInvalidRequest.Wrap("both allowed & deny list cannot be empty")
}
if len(allowed) > 0 && len(denied) > 0 {
return nil, nil, sdkerrors.ErrInvalidRequest.Wrap("cannot set both allowed & deny list")
}
allowedValidators := make([]string, len(allowed))
if len(allowed) > 0 {
for i, validator := range allowed {
allowedValidators[i] = validator.String()
}
return allowedValidators, nil, nil
}
deniedValidators := make([]string, len(denied))
for i, validator := range denied {
deniedValidators[i] = validator.String()
}
return nil, deniedValidators, nil
}
// Normalized Msg type URLs
func normalizeAuthzType(authzType AuthorizationType) (string, error) {
switch authzType {
case AuthorizationType_AUTHORIZATION_TYPE_DELEGATE:
return sdk.MsgTypeURL(&MsgDelegate{}), nil
case AuthorizationType_AUTHORIZATION_TYPE_UNDELEGATE:
return sdk.MsgTypeURL(&MsgUndelegate{}), nil
case AuthorizationType_AUTHORIZATION_TYPE_REDELEGATE:
return sdk.MsgTypeURL(&MsgBeginRedelegate{}), nil
case AuthorizationType_AUTHORIZATION_TYPE_CANCEL_UNBONDING_DELEGATION:
return sdk.MsgTypeURL(&MsgCancelUnbondingDelegation{}), nil
default:
return "", errorsmod.Wrapf(authz.ErrUnknownAuthorizationType, "cannot normalize authz type with %T", authzType)
}
}