-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
authz.go
150 lines (127 loc) · 4.82 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
package types
import (
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)
// Normalized Msg type URLs
var (
_ authz.Authorization = &StakeAuthorization{}
)
// NewStakeAuthorization creates a new StakeAuthorization object.
func NewStakeAuthorization(allowed []sdk.ValAddress, denied []sdk.ValAddress, authzType AuthorizationType, amount *sdk.Coin) (*StakeAuthorization, error) {
allowedValidators, deniedValidators, err := validateAndBech32fy(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
}
func (a StakeAuthorization) ValidateBasic() error {
if a.MaxTokens != nil && a.MaxTokens.IsNegative() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "negative coin amount: %v", a.MaxTokens)
}
if a.AuthorizationType == AuthorizationType_AUTHORIZATION_TYPE_UNSPECIFIED {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "unknown authorization type")
}
return nil
}
// Accept implements Authorization.Accept.
func (a StakeAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (authz.AcceptResponse, error) {
var validatorAddress string
var 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
default:
return authz.AcceptResponse{}, sdkerrors.ErrInvalidRequest.Wrap("unknown msg type")
}
isValidatorExists := false
allowedList := a.GetAllowList().GetAddress()
for _, validator := range allowedList {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
isValidatorExists = true
break
}
}
denyList := a.GetDenyList().GetAddress()
for _, validator := range denyList {
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "stake authorization")
if validator == validatorAddress {
return authz.AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf(" cannot delegate/undelegate to %s validator", validator)
}
}
if !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 := a.MaxTokens.Sub(amount)
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 validateAndBech32fy(allowed []sdk.ValAddress, 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
}
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
default:
return "", sdkerrors.ErrInvalidType.Wrapf("unknown authorization type %T", authzType)
}
}