Skip to content

Commit fe333cb

Browse files
ViktorTigerstromellemouton
authored andcommitted
accounts: deduct in-flight balance by account ID
This commit ensures that a pending payment is associated with the specific account ID before deducting the in-flight balance from the available balance.
1 parent 9d9d658 commit fe333cb

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

accounts/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,13 @@ func (s *InterceptorService) CheckBalance(id AccountID,
406406
}
407407

408408
var inFlightAmt int64
409-
for _, pendingPayment := range s.pendingPayments {
410-
inFlightAmt += int64(pendingPayment.fullAmount)
409+
for _, payment := range account.Payments {
410+
if inflightState(payment.Status) {
411+
// If a payment is in-flight and associated with the
412+
// account, the user should not be able to spend that
413+
// amount while it's in-flight.
414+
inFlightAmt += int64(payment.FullAmount)
415+
}
411416
}
412417

413418
availableAmount := account.CurrentBalance - inFlightAmt

accounts/service_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ var (
2020
testTimeout = time.Millisecond * 500
2121
testInterval = time.Millisecond * 20
2222

23-
testHash2 = lntypes.Hash{99, 88, 77}
23+
testID2 = AccountID{22, 22, 22}
24+
25+
testHash2 = lntypes.Hash{2, 2, 2, 2, 2}
26+
testHash3 = lntypes.Hash{3, 3, 3, 3, 3}
2427
)
2528

2629
type mockLnd struct {
@@ -623,8 +626,10 @@ func TestAccountService(t *testing.T) {
623626
}, {
624627
name: "in-flight payments",
625628
setup: func(t *testing.T, lnd *mockLnd, s *InterceptorService) {
626-
// We set up our account with a balance of 5k msats and
627-
// two in-flight payments with a total or 3k msats.
629+
// We set up two accounts with a balance of 5k msats.
630+
631+
// The first account has two in-flight payments, one of
632+
// 2k msats and one of 1k msats, totaling 3k msats.
628633
acct := &OffChainBalanceAccount{
629634
ID: testID,
630635
Type: TypeInitialBalance,
@@ -646,12 +651,34 @@ func TestAccountService(t *testing.T) {
646651

647652
err := s.store.UpdateAccount(acct)
648653
require.NoError(t, err)
654+
655+
// The second account has one in-flight payment of 4k
656+
// msats.
657+
acct2 := &OffChainBalanceAccount{
658+
ID: testID2,
659+
Type: TypeInitialBalance,
660+
CurrentBalance: 5000,
661+
Invoices: AccountInvoices{
662+
testHash: {},
663+
},
664+
Payments: AccountPayments{
665+
testHash3: {
666+
Status: lnrpc.Payment_IN_FLIGHT,
667+
FullAmount: 4000,
668+
},
669+
},
670+
}
671+
672+
err = s.store.UpdateAccount(acct2)
673+
require.NoError(t, err)
649674
},
650675
validate: func(t *testing.T, lnd *mockLnd,
651676
s *InterceptorService) {
652677

653-
// We should be able to initiate another payment with an
654-
// amount smaller or equal to 2k msats.
678+
// The first should be able to initiate another payment
679+
// with an amount smaller or equal to 2k msats. This
680+
// also asserts that the second accounts in-flight
681+
// payment doesn't affect the first account.
655682
err := s.CheckBalance(testID, 2000)
656683
require.NoError(t, err)
657684

@@ -670,6 +697,15 @@ func TestAccountService(t *testing.T) {
670697
err = s.CheckBalance(testID, 4000)
671698
return err == nil
672699
})
700+
701+
// The second account should be able to initiate a
702+
// payment of 1k msats.
703+
err = s.CheckBalance(testID2, 1000)
704+
require.NoError(t, err)
705+
706+
// But exactly one sat over it should fail.
707+
err = s.CheckBalance(testID2, 1001)
708+
require.ErrorIs(t, err, ErrAccBalanceInsufficient)
673709
},
674710
}}
675711

0 commit comments

Comments
 (0)