Skip to content

Commit

Permalink
Merge pull request #140 from SiaFoundation/chris/not-enough-funds-error
Browse files Browse the repository at this point in the history
fix(rhp): Return ErrNotEnoughFunds when account doesn't exist or has insufficient funds in it
  • Loading branch information
n8maninger authored Dec 17, 2024
2 parents 7399960 + 5d6fc37 commit 87b2d4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Fix rhp4 server not returning ErrNotEnoughFunds when account has insufficient balance
20 changes: 20 additions & 0 deletions rhp/v4/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,19 @@ func TestAccounts(t *testing.T) {

cs := cm.TipState()

// test operations against unknown account
token := proto4.AccountToken{
Account: account,
ValidUntil: time.Now().Add(time.Hour),
}

tokenSigHash := token.SigHash()
token.Signature = renterKey.SignHash(tokenSigHash)
_, err = rhp4.RPCVerifySector(context.Background(), transport, settings.Prices, token, types.Hash256{1})
if err == nil || !strings.Contains(err.Error(), proto4.ErrNotEnoughFunds.Error()) {
t.Fatal(err)
}

balance, err := rhp4.RPCAccountBalance(context.Background(), transport, account)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -803,6 +816,13 @@ func TestAccounts(t *testing.T) {
} else if !balance.Equals(accountFundAmount) {
t.Fatalf("expected %v, got %v", accountFundAmount, balance)
}

// drain account and try using it
_ = c.DebitAccount(account, proto4.Usage{RPC: accountFundAmount})
_, err = rhp4.RPCVerifySector(context.Background(), transport, settings.Prices, token, types.Hash256{1})
if err == nil || !strings.Contains(err.Error(), proto4.ErrNotEnoughFunds.Error()) {
t.Fatal(err)
}
}

func TestReadWriteSector(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions testutil/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ func (ec *EphemeralContractor) DebitAccount(account proto4.Account, usage proto4
defer ec.mu.Unlock()

balance, ok := ec.accounts[account]
if !ok {
return errors.New("account not found")
} else if balance.Cmp(usage.RenterCost()) < 0 {
return errors.New("insufficient funds")
if !ok || balance.Cmp(usage.RenterCost()) < 0 {
return proto4.ErrNotEnoughFunds
}
ec.accounts[account] = balance.Sub(usage.RenterCost())
return nil
Expand Down

0 comments on commit 87b2d4d

Please sign in to comment.