forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcount_authorization.go
50 lines (42 loc) · 1.44 KB
/
count_authorization.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
package authz
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
_ Authorization = &CountAuthorization{}
errMsgGt0 = "allowed authorizations must be greater than 0"
)
// NewCountAuthorization creates a new CountAuthorization object.
func NewCountAuthorization(msgTypeURL string, allowedAuthorizations int32) *CountAuthorization {
return &CountAuthorization{
Msg: msgTypeURL,
AllowedAuthorizations: allowedAuthorizations,
}
}
// MsgTypeURL implements Authorization.MsgTypeURL.
func (a CountAuthorization) MsgTypeURL() string {
return a.Msg
}
// Accept implements Authorization.Accept.
func (a CountAuthorization) Accept(ctx sdk.Context, msg sdk.Msg) (AcceptResponse, error) {
remaining, isNegative := a.decrement()
if isNegative {
return AcceptResponse{}, sdkerrors.ErrUnauthorized.Wrapf(errMsgGt0)
}
if remaining == 0 {
return AcceptResponse{Accept: true, Delete: true}, nil
}
return AcceptResponse{Accept: true, Delete: false, Updated: &CountAuthorization{Msg: a.Msg, AllowedAuthorizations: remaining}}, nil
}
// ValidateBasic implements Authorization.ValidateBasic.
func (a CountAuthorization) ValidateBasic() error {
if a.AllowedAuthorizations <= 0 {
return sdkerrors.ErrInvalidRequest.Wrapf(errMsgGt0)
}
return nil
}
func (a CountAuthorization) decrement() (int32, bool) {
cnt := a.AllowedAuthorizations - 1
return cnt, cnt < 0
}