Skip to content

Commit 09c0d03

Browse files
committed
multi: add new payments package
We add a new payments package and create a db interface for the paymentDB there. This is already implemented for the KV db side and will be implemented for the SQL side in future PRS.
1 parent 7726333 commit 09c0d03

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
lines changed

config_builder.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/lightningnetwork/lnd/lnwallet/rpcwallet"
4949
"github.com/lightningnetwork/lnd/macaroons"
5050
"github.com/lightningnetwork/lnd/msgmux"
51+
pymtpkg "github.com/lightningnetwork/lnd/payments"
5152
"github.com/lightningnetwork/lnd/rpcperms"
5253
"github.com/lightningnetwork/lnd/signal"
5354
"github.com/lightningnetwork/lnd/sqldb"
@@ -924,9 +925,9 @@ type DatabaseInstances struct {
924925
// InvoiceDB is the database that stores information about invoices.
925926
InvoiceDB invoices.InvoiceDB
926927

927-
// KVPaymentsDB is the database that stores all payment related
928+
// PaymentDB is the database that stores all payment related
928929
// information.
929-
KVPaymentsDB *channeldb.KVPaymentsDB
930+
PaymentDB pymtpkg.PaymentDB
930931

931932
// MacaroonDB is the database that stores macaroon root keys.
932933
MacaroonDB kvdb.Backend
@@ -1194,7 +1195,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11941195
kvPaymentsDB := channeldb.NewKVPaymentsDB(
11951196
dbs.ChanStateDB,
11961197
)
1197-
dbs.KVPaymentsDB = kvPaymentsDB
1198+
dbs.PaymentDB = kvPaymentsDB
11981199

11991200
// Wrap the watchtower client DB and make sure we clean up.
12001201
if cfg.WtClient.Active {

payments/interface.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package payments
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightningnetwork/lnd/channeldb"
7+
"github.com/lightningnetwork/lnd/lntypes"
8+
)
9+
10+
// PaymentDB is the interface that represents the underlying payments database.
11+
type PaymentDB interface {
12+
// QueryPayments queries the payments database and should support
13+
// pagination.
14+
QueryPayments(ctx context.Context,
15+
query channeldb.PaymentsQuery) (channeldb.PaymentsResponse,
16+
error)
17+
18+
// DeletePayment deletes a payment from the DB given its payment hash.
19+
DeletePayment(paymentHash lntypes.Hash, failedHtlcsOnly bool) error
20+
21+
// DeletePayments deletes all payments from the DB given the specified
22+
// flags.
23+
//
24+
// TODO(ziggie): use DeletePayment here as well to make the interface
25+
// more leaner.
26+
DeletePayments(failedOnly, failedHtlcsOnly bool) (int, error)
27+
28+
// This method checks that no succeeded payment exist for this payment
29+
// hash.
30+
InitPayment(lntypes.Hash, *channeldb.PaymentCreationInfo) error
31+
32+
// DeleteFailedAttempts removes all failed HTLCs from the db. It should
33+
// be called for a given payment whenever all inflight htlcs are
34+
// completed, and the payment has reached a final settled state.
35+
DeleteFailedAttempts(lntypes.Hash) error
36+
37+
// RegisterAttempt atomically records the provided HTLCAttemptInfo.
38+
RegisterAttempt(lntypes.Hash,
39+
*channeldb.HTLCAttemptInfo) (*channeldb.MPPayment, error)
40+
41+
// SettleAttempt marks the given attempt settled with the preimage. If
42+
// this is a multi shard payment, this might implicitly mean the the
43+
// full payment succeeded.
44+
//
45+
// After invoking this method, InitPayment should always return an
46+
// error to prevent us from making duplicate payments to the same
47+
// payment hash. The provided preimage is atomically saved to the DB
48+
// for record keeping.
49+
SettleAttempt(lntypes.Hash, uint64, *channeldb.HTLCSettleInfo) (
50+
*channeldb.MPPayment, error)
51+
52+
// FailAttempt marks the given payment attempt failed.
53+
FailAttempt(lntypes.Hash, uint64, *channeldb.HTLCFailInfo) (
54+
*channeldb.MPPayment, error)
55+
56+
// FetchPayment fetches the payment corresponding to the given payment
57+
// hash.
58+
FetchPayment(paymentHash lntypes.Hash) (*channeldb.MPPayment, error)
59+
60+
// Fail transitions a payment into the Failed state, and records
61+
// the ultimate reason the payment failed. Note that this should only
62+
// be called when all active attempts are already failed. After
63+
// invoking this method, InitPayment should return nil on its next call
64+
// for this payment hash, allowing the user to make a subsequent
65+
// payment.
66+
Fail(lntypes.Hash, channeldb.FailureReason) (*channeldb.MPPayment,
67+
error)
68+
69+
// FetchInFlightPayments returns all payments with status InFlight.
70+
FetchInFlightPayments() ([]*channeldb.MPPayment, error)
71+
}

routing/control_tower.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/lightningnetwork/lnd/channeldb"
77
"github.com/lightningnetwork/lnd/lntypes"
88
"github.com/lightningnetwork/lnd/multimutex"
9+
pymtpkg "github.com/lightningnetwork/lnd/payments"
910
"github.com/lightningnetwork/lnd/queue"
1011
)
1112

@@ -151,7 +152,7 @@ func (s *controlTowerSubscriberImpl) Updates() <-chan interface{} {
151152
// controlTower is persistent implementation of ControlTower to restrict
152153
// double payment sending.
153154
type controlTower struct {
154-
db *channeldb.KVPaymentsDB
155+
db pymtpkg.PaymentDB
155156

156157
// subscriberIndex is used to provide a unique id for each subscriber
157158
// to all payments. This is used to easily remove the subscriber when
@@ -168,7 +169,7 @@ type controlTower struct {
168169
}
169170

170171
// NewControlTower creates a new instance of the controlTower.
171-
func NewControlTower(db *channeldb.KVPaymentsDB) ControlTower {
172+
func NewControlTower(db pymtpkg.PaymentDB) ControlTower {
172173
return &controlTower{
173174
db: db,
174175
subscribersAllPayments: make(

rpcserver.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7403,9 +7403,7 @@ func (r *rpcServer) ListPayments(ctx context.Context,
74037403
query.MaxPayments = math.MaxUint64
74047404
}
74057405

7406-
paymentsQuerySlice, err := r.server.kvPaymentsDB.QueryPayments(
7407-
ctx, query,
7408-
)
7406+
paymentsQuerySlice, err := r.server.paymentsDB.QueryPayments(ctx, query)
74097407
if err != nil {
74107408
return nil, err
74117409
}
@@ -7447,7 +7445,7 @@ func (r *rpcServer) DeletePayment(ctx context.Context,
74477445
rpcsLog.Infof("[DeletePayment] payment_identifier=%v, "+
74487446
"failed_htlcs_only=%v", hash, req.FailedHtlcsOnly)
74497447

7450-
err = r.server.kvPaymentsDB.DeletePayment(hash, req.FailedHtlcsOnly)
7448+
err = r.server.paymentsDB.DeletePayment(hash, req.FailedHtlcsOnly)
74517449
if err != nil {
74527450
return nil, err
74537451
}
@@ -7487,7 +7485,7 @@ func (r *rpcServer) DeleteAllPayments(ctx context.Context,
74877485
"failed_htlcs_only=%v", req.FailedPaymentsOnly,
74887486
req.FailedHtlcsOnly)
74897487

7490-
numDeletedPayments, err := r.server.kvPaymentsDB.DeletePayments(
7488+
numDeletedPayments, err := r.server.paymentsDB.DeletePayments(
74917489
req.FailedPaymentsOnly, req.FailedHtlcsOnly,
74927490
)
74937491
if err != nil {

server.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import (
6464
"github.com/lightningnetwork/lnd/lnwire"
6565
"github.com/lightningnetwork/lnd/nat"
6666
"github.com/lightningnetwork/lnd/netann"
67+
pymtpkg "github.com/lightningnetwork/lnd/payments"
6768
"github.com/lightningnetwork/lnd/peer"
6869
"github.com/lightningnetwork/lnd/peernotifier"
6970
"github.com/lightningnetwork/lnd/pool"
@@ -317,11 +318,9 @@ type server struct {
317318

318319
invoicesDB invoices.InvoiceDB
319320

320-
// kvPaymentsDB is the DB that contains all functions for managing
321+
// paymentsDB is the DB that contains all functions for managing
321322
// payments.
322-
//
323-
// TODO(ziggie): Replace with interface.
324-
kvPaymentsDB *channeldb.KVPaymentsDB
323+
paymentsDB pymtpkg.PaymentDB
325324

326325
aliasMgr *aliasmgr.Manager
327326

@@ -664,7 +663,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
664663
addrSource: addrSource,
665664
miscDB: dbs.ChanStateDB,
666665
invoicesDB: dbs.InvoiceDB,
667-
kvPaymentsDB: dbs.KVPaymentsDB,
666+
paymentsDB: dbs.PaymentDB,
668667
cc: cc,
669668
sigPool: lnwallet.NewSigPool(cfg.Workers.Sig, cc.Signer),
670669
writePool: writePool,
@@ -1088,7 +1087,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
10881087
PathFindingConfig: pathFindingConfig,
10891088
}
10901089

1091-
s.controlTower = routing.NewControlTower(dbs.KVPaymentsDB)
1090+
s.controlTower = routing.NewControlTower(dbs.PaymentDB)
10921091

10931092
strictPruning := cfg.Bitcoin.Node == "neutrino" ||
10941093
cfg.Routing.StrictZombiePruning

0 commit comments

Comments
 (0)