Skip to content

Commit cab40b4

Browse files
ryanchristotechnicallytyamaury1093mergify[bot]
authored
fix: feegrant grant period not resetting (cosmos#9450)
* fix grant period reset * add period reset test * consolidate condition Co-authored-by: ryanchrypto <[email protected]> Co-authored-by: Tyler <[email protected]> Co-authored-by: Amaury <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent a55b6ed commit cab40b4

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

x/feegrant/periodic_fee.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ func (a *PeriodicAllowance) Accept(ctx sdk.Context, fee sdk.Coins, _ []sdk.Msg)
4949

5050
// tryResetPeriod will check if the PeriodReset has been hit. If not, it is a no-op.
5151
// If we hit the reset period, it will top up the PeriodCanSpend amount to
52-
// min(PeriodicSpendLimit, a.Basic.SpendLimit) so it is never more than the maximum allowed.
52+
// min(PeriodSpendLimit, Basic.SpendLimit) so it is never more than the maximum allowed.
5353
// It will also update the PeriodReset. If we are within one Period, it will update from the
5454
// last PeriodReset (eg. if you always do one tx per day, it will always reset the same time)
5555
// If we are more then one period out (eg. no activity in a week), reset is one Period from the execution of this method
5656
func (a *PeriodicAllowance) tryResetPeriod(blockTime time.Time) {
5757
if blockTime.Before(a.PeriodReset) {
5858
return
5959
}
60-
// set CanSpend to the lesser of PeriodSpendLimit and the TotalLimit
61-
if _, isNeg := a.Basic.SpendLimit.SafeSub(a.PeriodSpendLimit); isNeg {
60+
61+
// set PeriodCanSpend to the lesser of Basic.SpendLimit and PeriodSpendLimit
62+
if _, isNeg := a.Basic.SpendLimit.SafeSub(a.PeriodSpendLimit); isNeg && !a.Basic.SpendLimit.Empty() {
6263
a.PeriodCanSpend = a.Basic.SpendLimit
6364
} else {
6465
a.PeriodCanSpend = a.PeriodSpendLimit

x/feegrant/periodic_fee_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
3131
tenMinutes := time.Duration(10) * time.Minute
3232

3333
cases := map[string]struct {
34-
allow feegrant.PeriodicAllowance
35-
// all other checks are ignored if valid=false
34+
allow feegrant.PeriodicAllowance
3635
fee sdk.Coins
3736
blockTime time.Time
38-
valid bool
37+
valid bool // all other checks are ignored if valid=false
3938
accept bool
4039
remove bool
4140
remains sdk.Coins
@@ -115,7 +114,7 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
115114
remove: false,
116115
remainsPeriod: nil,
117116
remains: smallAtom,
118-
periodReset: oneHour.Add(10 * time.Minute), // one step from last reset, not now
117+
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
119118
},
120119
"step limited by global allowance": {
121120
allow: feegrant.PeriodicAllowance{
@@ -134,7 +133,20 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
134133
remove: false,
135134
remainsPeriod: smallAtom.Sub(oneAtom),
136135
remains: smallAtom.Sub(oneAtom),
137-
periodReset: oneHour.Add(10 * time.Minute), // one step from last reset, not now
136+
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
137+
},
138+
"period reset no spend limit": {
139+
allow: feegrant.PeriodicAllowance{
140+
Period: tenMinutes,
141+
PeriodReset: now,
142+
PeriodSpendLimit: atom,
143+
},
144+
valid: true,
145+
fee: atom,
146+
blockTime: oneHour,
147+
accept: true,
148+
remove: false,
149+
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
138150
},
139151
"expired": {
140152
allow: feegrant.PeriodicAllowance{

0 commit comments

Comments
 (0)