forked from baron-chain/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodic_fee.go
113 lines (93 loc) · 4.1 KB
/
periodic_fee.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
package feegrant
import (
"time"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var _ FeeAllowanceI = (*PeriodicAllowance)(nil)
// Accept can use fee payment requested as well as timestamp of the current block
// to determine whether or not to process this. This is checked in
// Keeper.UseGrantedFees and the return values should match how it is handled there.
//
// If it returns an error, the fee payment is rejected, otherwise it is accepted.
// The FeeAllowance implementation is expected to update it's internal state
// and will be saved again after an acceptance.
//
// If remove is true (regardless of the error), the FeeAllowance will be deleted from storage
// (eg. when it is used up). (See call to RevokeAllowance in Keeper.UseGrantedFees)
func (a *PeriodicAllowance) Accept(ctx sdk.Context, fee sdk.Coins, _ []sdk.Msg) (bool, error) {
blockTime := ctx.BlockTime()
if a.Basic.Expiration != nil && blockTime.After(*a.Basic.Expiration) {
return true, errorsmod.Wrap(ErrFeeLimitExpired, "absolute limit")
}
a.tryResetPeriod(blockTime)
// deduct from both the current period and the max amount
var isNeg bool
a.PeriodCanSpend, isNeg = a.PeriodCanSpend.SafeSub(fee...)
if isNeg {
return false, errorsmod.Wrap(ErrFeeLimitExceeded, "period limit")
}
if a.Basic.SpendLimit != nil {
a.Basic.SpendLimit, isNeg = a.Basic.SpendLimit.SafeSub(fee...)
if isNeg {
return false, errorsmod.Wrap(ErrFeeLimitExceeded, "absolute limit")
}
return a.Basic.SpendLimit.IsZero(), nil
}
return false, nil
}
// tryResetPeriod will check if the PeriodReset has been hit. If not, it is a no-op.
// If we hit the reset period, it will top up the PeriodCanSpend amount to
// min(PeriodSpendLimit, Basic.SpendLimit) so it is never more than the maximum allowed.
// It will also update the PeriodReset. If we are within one Period, it will update from the
// last PeriodReset (eg. if you always do one tx per day, it will always reset the same time)
// If we are more then one period out (eg. no activity in a week), reset is one Period from the execution of this method
func (a *PeriodicAllowance) tryResetPeriod(blockTime time.Time) {
if blockTime.Before(a.PeriodReset) {
return
}
// set PeriodCanSpend to the lesser of Basic.SpendLimit and PeriodSpendLimit
if _, isNeg := a.Basic.SpendLimit.SafeSub(a.PeriodSpendLimit...); isNeg && !a.Basic.SpendLimit.Empty() {
a.PeriodCanSpend = a.Basic.SpendLimit
} else {
a.PeriodCanSpend = a.PeriodSpendLimit
}
// If we are within the period, step from expiration (eg. if you always do one tx per day, it will always reset the same time)
// If we are more then one period out (eg. no activity in a week), reset is one period from this time
a.PeriodReset = a.PeriodReset.Add(a.Period)
if blockTime.After(a.PeriodReset) {
a.PeriodReset = blockTime.Add(a.Period)
}
}
// ValidateBasic implements FeeAllowance and enforces basic sanity checks
func (a PeriodicAllowance) ValidateBasic() error {
if err := a.Basic.ValidateBasic(); err != nil {
return err
}
if !a.PeriodSpendLimit.IsValid() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "spend amount is invalid: %s", a.PeriodSpendLimit)
}
if !a.PeriodSpendLimit.IsAllPositive() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "spend limit must be positive")
}
if !a.PeriodCanSpend.IsValid() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "can spend amount is invalid: %s", a.PeriodCanSpend)
}
// We allow 0 for CanSpend
if a.PeriodCanSpend.IsAnyNegative() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "can spend must not be negative")
}
// ensure PeriodSpendLimit can be subtracted from total (same coin types)
if a.Basic.SpendLimit != nil && !a.PeriodSpendLimit.DenomsSubsetOf(a.Basic.SpendLimit) {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "period spend limit has different currency than basic spend limit")
}
// check times
if a.Period.Seconds() < 0 {
return errorsmod.Wrap(ErrInvalidDuration, "negative clock step")
}
return nil
}
func (a PeriodicAllowance) ExpiresAt() (*time.Time, error) {
return a.Basic.ExpiresAt()
}