Skip to content

accounts: Credit and debit accounts DB additions prep #973

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

Closed
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
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ jobs:
unit_type:
- unit-race
- unit
- unit dbbackend=postgres
- unit dbbackend=sqlite
steps:
- name: git checkout
uses: actions/checkout@v4
Expand Down
36 changes: 34 additions & 2 deletions accounts/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package accounts

import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -55,6 +57,31 @@ func ParseAccountID(idStr string) (*AccountID, error) {
return &id, nil
}

// ToInt64 converts an AccountID to its int64 representation.
func (a AccountID) ToInt64() (int64, error) {
var value int64
buf := bytes.NewReader(a[:])
if err := binary.Read(buf, byteOrder, &value); err != nil {
return 0, err
}

return value, nil
}

// AccountIDFromInt64 converts an int64 to an AccountID.
func AccountIDFromInt64(value int64) (AccountID, error) {
var (
a = AccountID{}
buf = new(bytes.Buffer)
)
if err := binary.Write(buf, binary.BigEndian, value); err != nil {
return a, err
}
copy(a[:], buf.Bytes())

return a, nil
}

// String returns the string representation of the AccountID.
func (a AccountID) String() string {
return hex.EncodeToString(a[:])
Expand Down Expand Up @@ -225,9 +252,14 @@ type Store interface {
AddAccountInvoice(ctx context.Context, id AccountID,
hash lntypes.Hash) error

// IncreaseAccountBalance increases the balance of the account with the
// CreditAccount increases the balance of the account with the
// given ID by the given amount.
CreditAccount(ctx context.Context, id AccountID,
amount lnwire.MilliSatoshi) error

// DebitAccount decreases the balance of the account with the
// given ID by the given amount.
IncreaseAccountBalance(ctx context.Context, id AccountID,
DebitAccount(ctx context.Context, id AccountID,
amount lnwire.MilliSatoshi) error

// UpsertAccountPayment updates or inserts a payment entry for the given
Expand Down
2 changes: 1 addition & 1 deletion accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (s *InterceptorService) invoiceUpdate(ctx context.Context,
// If we get here, the current account has the invoice associated with
// it that was just paid. Credit the amount to the account and update it
// in the DB.
err := s.store.IncreaseAccountBalance(ctx, acctID, invoice.AmountPaid)
err := s.store.CreditAccount(ctx, acctID, invoice.AmountPaid)
if err != nil {
return s.disableAndErrorfUnsafe("error increasing account "+
"balance account: %w", err)
Expand Down
31 changes: 29 additions & 2 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func (s *BoltStore) AddAccountInvoice(_ context.Context, id AccountID,
return s.updateAccount(id, update)
}

// IncreaseAccountBalance increases the balance of the account with the given ID
// CreditAccount increases the balance of the account with the given ID
// by the given amount.
//
// NOTE: This is part of the Store interface.
func (s *BoltStore) IncreaseAccountBalance(_ context.Context, id AccountID,
func (s *BoltStore) CreditAccount(_ context.Context, id AccountID,
amount lnwire.MilliSatoshi) error {

update := func(account *OffChainBalanceAccount) error {
Expand All @@ -244,6 +244,33 @@ func (s *BoltStore) IncreaseAccountBalance(_ context.Context, id AccountID,
return s.updateAccount(id, update)
}

// DebitAccount decreases the balance of the account with the given ID
// by the given amount.
//
// NOTE: This is part of the Store interface.
func (s *BoltStore) DebitAccount(_ context.Context, id AccountID,
amount lnwire.MilliSatoshi) error {

update := func(account *OffChainBalanceAccount) error {
if amount > math.MaxInt64 {
return fmt.Errorf("amount %v exceeds the maximum of %v",
amount, int64(math.MaxInt64))
}

if account.CurrentBalance-int64(amount) < 0 {
return fmt.Errorf("cannot debit %v from the account "+
"balance, as the resulting balance would be "+
"below 0", int64(amount/1000))
}

account.CurrentBalance -= int64(amount)

return nil
}

return s.updateAccount(id, update)
}

// UpsertAccountPayment updates or inserts a payment entry for the given
// account. Various functional options can be passed to modify the behavior of
// the method. The returned boolean is true if the payment was already known
Expand Down
Loading
Loading