Skip to content

Commit

Permalink
Merge pull request #174 from SiaFoundation/nate/fix-budget-panic
Browse files Browse the repository at this point in the history
Fix budget panic
  • Loading branch information
n8maninger authored Sep 28, 2023
2 parents db5f29a + 549795d commit ffee0c3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion host/accounts/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (b *Budget) Spend(usage Usage) error {
newUsage := b.usage.Add(usage)
spent := newUsage.Total()
if b.max.Cmp(spent) < 0 {
return fmt.Errorf("unable to spend %v, %v remaining: %w", usage.Total(), b.max.Sub(spent), ErrInsufficientFunds)
return fmt.Errorf("unable to spend %v, %v remaining: %w", usage.Total(), b.max.Sub(b.usage.Total()), ErrInsufficientFunds)
}
b.usage = newUsage
return nil
Expand Down
6 changes: 6 additions & 0 deletions host/accounts/budget_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package accounts_test

import (
"errors"
"math"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -184,6 +185,11 @@ func TestBudget(t *testing.T) {
}
defer budget.Rollback()

// try to spend more than the budget
if err := budget.Spend(accounts.Usage{RPCRevenue: budgetAmount.Mul64(2)}); !errors.Is(err, accounts.ErrInsufficientFunds) {
t.Fatal("expected insufficient funds error, got", err)
}

// check that the in-memory state is consistent
expectedBalance = expectedBalance.Sub(budgetAmount)
balance, err := am.Balance(accountID)
Expand Down

0 comments on commit ffee0c3

Please sign in to comment.