From bad0a352152bde1d0a679eabdf560a8ad4e1bfa6 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 28 Sep 2023 08:49:47 -0600 Subject: [PATCH 1/2] accounts: extend budget test --- host/accounts/budget_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/host/accounts/budget_test.go b/host/accounts/budget_test.go index f463de54..32d6342e 100644 --- a/host/accounts/budget_test.go +++ b/host/accounts/budget_test.go @@ -1,6 +1,7 @@ package accounts_test import ( + "errors" "math" "path/filepath" "reflect" @@ -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) From 549795d8dfbb454ded7b303dec39e9efe94f75ea Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Thu, 28 Sep 2023 08:50:20 -0600 Subject: [PATCH 2/2] accounts: fix underflow panic --- host/accounts/budget.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/accounts/budget.go b/host/accounts/budget.go index b19973a3..f12a1bef 100644 --- a/host/accounts/budget.go +++ b/host/accounts/budget.go @@ -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