Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: feegrant grant period not resetting #9450

Merged
merged 6 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions x/feegrant/periodic_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ func (a *PeriodicAllowance) Accept(ctx sdk.Context, fee sdk.Coins, _ []sdk.Msg)

// 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(PeriodicSpendLimit, a.Basic.SpendLimit) so it is never more than the maximum allowed.
// 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 CanSpend to the lesser of PeriodSpendLimit and the TotalLimit
if _, isNeg := a.Basic.SpendLimit.SafeSub(a.PeriodSpendLimit); isNeg {

// 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
Expand Down
22 changes: 17 additions & 5 deletions x/feegrant/periodic_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
tenMinutes := time.Duration(10) * time.Minute

cases := map[string]struct {
allow feegrant.PeriodicAllowance
// all other checks are ignored if valid=false
allow feegrant.PeriodicAllowance
fee sdk.Coins
blockTime time.Time
valid bool
valid bool // all other checks are ignored if valid=false
accept bool
remove bool
remains sdk.Coins
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
remove: false,
remainsPeriod: nil,
remains: smallAtom,
periodReset: oneHour.Add(10 * time.Minute), // one step from last reset, not now
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
},
"step limited by global allowance": {
allow: feegrant.PeriodicAllowance{
Expand All @@ -134,7 +133,20 @@ func TestPeriodicFeeValidAllow(t *testing.T) {
remove: false,
remainsPeriod: smallAtom.Sub(oneAtom),
remains: smallAtom.Sub(oneAtom),
periodReset: oneHour.Add(10 * time.Minute), // one step from last reset, not now
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
},
"period reset no spend limit": {
allow: feegrant.PeriodicAllowance{
Period: tenMinutes,
PeriodReset: now,
PeriodSpendLimit: atom,
},
valid: true,
fee: atom,
blockTime: oneHour,
accept: true,
remove: false,
periodReset: oneHour.Add(tenMinutes), // one step from last reset, not now
},
"expired": {
allow: feegrant.PeriodicAllowance{
Expand Down