Skip to content

Commit

Permalink
extend TestAccounts to check for ErrNotEnoughFunds
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Dec 17, 2024
1 parent 7399960 commit 3a7ea88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
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 3a7ea88

Please sign in to comment.