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(rhp): Return ErrNotEnoughFunds when account doesn't exist or has insufficient funds in it #140

Merged
merged 1 commit into from
Dec 17, 2024
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
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
Loading