Skip to content

Commit 21726f0

Browse files
authored
Merge pull request #9826 from ziggie1984/refactor-payments-code-02
Refactor Payment PR 2
2 parents 8f94929 + 2b3b27f commit 21726f0

File tree

10 files changed

+2824
-2724
lines changed

10 files changed

+2824
-2724
lines changed

channeldb/payment_control.go

Lines changed: 0 additions & 836 deletions
This file was deleted.

channeldb/payments.go

Lines changed: 0 additions & 1328 deletions
Large diffs are not rendered by default.

channeldb/payments_kv_store.go

Lines changed: 2181 additions & 0 deletions
Large diffs are not rendered by default.

channeldb/payment_control_test.go renamed to channeldb/payments_kv_store_test.go

Lines changed: 578 additions & 164 deletions
Large diffs are not rendered by default.

channeldb/payments_test.go

Lines changed: 21 additions & 369 deletions
Large diffs are not rendered by default.

config_builder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ type DatabaseInstances struct {
924924
// InvoiceDB is the database that stores information about invoices.
925925
InvoiceDB invoices.InvoiceDB
926926

927+
// KVPaymentsDB is the database that stores all payment related
928+
// information.
929+
KVPaymentsDB *channeldb.KVPaymentsDB
930+
927931
// MacaroonDB is the database that stores macaroon root keys.
928932
MacaroonDB kvdb.Backend
929933

@@ -1215,6 +1219,14 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
12151219
return nil, nil, err
12161220
}
12171221

1222+
// Mount the payments DB which is only KV for now.
1223+
//
1224+
// TODO(ziggie): Add support for SQL payments DB.
1225+
kvPaymentsDB := channeldb.NewKVPaymentsDB(
1226+
dbs.ChanStateDB,
1227+
)
1228+
dbs.KVPaymentsDB = kvPaymentsDB
1229+
12181230
// Wrap the watchtower client DB and make sure we clean up.
12191231
if cfg.WtClient.Active {
12201232
dbs.TowerClientDB, err = wtdb.OpenClientDB(

routing/control_tower.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (s *controlTowerSubscriberImpl) Updates() <-chan interface{} {
151151
// controlTower is persistent implementation of ControlTower to restrict
152152
// double payment sending.
153153
type controlTower struct {
154-
db *channeldb.PaymentControl
154+
db *channeldb.KVPaymentsDB
155155

156156
// subscriberIndex is used to provide a unique id for each subscriber
157157
// to all payments. This is used to easily remove the subscriber when
@@ -168,7 +168,7 @@ type controlTower struct {
168168
}
169169

170170
// NewControlTower creates a new instance of the controlTower.
171-
func NewControlTower(db *channeldb.PaymentControl) ControlTower {
171+
func NewControlTower(db *channeldb.KVPaymentsDB) ControlTower {
172172
return &controlTower{
173173
db: db,
174174
subscribersAllPayments: make(

routing/control_tower_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestControlTowerSubscribeUnknown(t *testing.T) {
4949

5050
db := initDB(t, false)
5151

52-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
52+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
5353

5454
// Subscription should fail when the payment is not known.
5555
_, err := pControl.SubscribePayment(lntypes.Hash{1})
@@ -63,7 +63,7 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
6363

6464
db := initDB(t, false)
6565

66-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
66+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
6767

6868
// Initiate a payment.
6969
info, attempt, preimg, err := genInfo()
@@ -156,33 +156,33 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
156156
}
157157
}
158158

159-
// TestPaymentControlSubscribeFail tests that payment updates for a
159+
// TestKVPaymentsDBSubscribeFail tests that payment updates for a
160160
// failed payment are properly sent to subscribers.
161-
func TestPaymentControlSubscribeFail(t *testing.T) {
161+
func TestKVPaymentsDBSubscribeFail(t *testing.T) {
162162
t.Parallel()
163163

164164
t.Run("register attempt, keep failed payments", func(t *testing.T) {
165-
testPaymentControlSubscribeFail(t, true, true)
165+
testKVPaymentsDBSubscribeFail(t, true, true)
166166
})
167167
t.Run("register attempt, delete failed payments", func(t *testing.T) {
168-
testPaymentControlSubscribeFail(t, true, false)
168+
testKVPaymentsDBSubscribeFail(t, true, false)
169169
})
170170
t.Run("no register attempt, keep failed payments", func(t *testing.T) {
171-
testPaymentControlSubscribeFail(t, false, true)
171+
testKVPaymentsDBSubscribeFail(t, false, true)
172172
})
173173
t.Run("no register attempt, delete failed payments", func(t *testing.T) {
174-
testPaymentControlSubscribeFail(t, false, false)
174+
testKVPaymentsDBSubscribeFail(t, false, false)
175175
})
176176
}
177177

178-
// TestPaymentControlSubscribeAllSuccess tests that multiple payments are
178+
// TestKVPaymentsDBSubscribeAllSuccess tests that multiple payments are
179179
// properly sent to subscribers of TrackPayments.
180-
func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
180+
func TestKVPaymentsDBSubscribeAllSuccess(t *testing.T) {
181181
t.Parallel()
182182

183183
db := initDB(t, true)
184184

185-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
185+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
186186

187187
// Initiate a payment.
188188
info1, attempt1, preimg1, err := genInfo()
@@ -288,14 +288,14 @@ func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
288288
require.Equal(t, attempt2.Route, htlc2.Route, "unexpected htlc route.")
289289
}
290290

291-
// TestPaymentControlSubscribeAllImmediate tests whether already inflight
291+
// TestKVPaymentsDBSubscribeAllImmediate tests whether already inflight
292292
// payments are reported at the start of the SubscribeAllPayments subscription.
293-
func TestPaymentControlSubscribeAllImmediate(t *testing.T) {
293+
func TestKVPaymentsDBSubscribeAllImmediate(t *testing.T) {
294294
t.Parallel()
295295

296296
db := initDB(t, true)
297297

298-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
298+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
299299

300300
// Initiate a payment.
301301
info, attempt, _, err := genInfo()
@@ -325,14 +325,14 @@ func TestPaymentControlSubscribeAllImmediate(t *testing.T) {
325325
}
326326
}
327327

328-
// TestPaymentControlUnsubscribeSuccess tests that when unsubscribed, there are
328+
// TestKVPaymentsDBUnsubscribeSuccess tests that when unsubscribed, there are
329329
// no more notifications to that specific subscription.
330-
func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
330+
func TestKVPaymentsDBUnsubscribeSuccess(t *testing.T) {
331331
t.Parallel()
332332

333333
db := initDB(t, true)
334334

335-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
335+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
336336

337337
subscription1, err := pControl.SubscribeAllPayments()
338338
require.NoError(t, err, "expected subscribe to succeed, but got: %v")
@@ -396,12 +396,12 @@ func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
396396
require.Len(t, subscription2.Updates(), 0)
397397
}
398398

399-
func testPaymentControlSubscribeFail(t *testing.T, registerAttempt,
399+
func testKVPaymentsDBSubscribeFail(t *testing.T, registerAttempt,
400400
keepFailedPaymentAttempts bool) {
401401

402402
db := initDB(t, keepFailedPaymentAttempts)
403403

404-
pControl := NewControlTower(channeldb.NewPaymentControl(db))
404+
pControl := NewControlTower(channeldb.NewKVPaymentsDB(db))
405405

406406
// Initiate a payment.
407407
info, attempt, _, err := genInfo()

rpcserver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7527,7 +7527,7 @@ func (r *rpcServer) ListPayments(ctx context.Context,
75277527
query.MaxPayments = math.MaxUint64
75287528
}
75297529

7530-
paymentsQuerySlice, err := r.server.miscDB.QueryPayments(query)
7530+
paymentsQuerySlice, err := r.server.kvPaymentsDB.QueryPayments(query)
75317531
if err != nil {
75327532
return nil, err
75337533
}
@@ -7608,7 +7608,7 @@ func (r *rpcServer) DeletePayment(ctx context.Context,
76087608
rpcsLog.Infof("[DeletePayment] payment_identifier=%v, "+
76097609
"failed_htlcs_only=%v", hash, req.FailedHtlcsOnly)
76107610

7611-
err = r.server.miscDB.DeletePayment(hash, req.FailedHtlcsOnly)
7611+
err = r.server.kvPaymentsDB.DeletePayment(hash, req.FailedHtlcsOnly)
76127612
if err != nil {
76137613
return nil, err
76147614
}
@@ -7648,7 +7648,7 @@ func (r *rpcServer) DeleteAllPayments(ctx context.Context,
76487648
"failed_htlcs_only=%v", req.FailedPaymentsOnly,
76497649
req.FailedHtlcsOnly)
76507650

7651-
numDeletedPayments, err := r.server.miscDB.DeletePayments(
7651+
numDeletedPayments, err := r.server.kvPaymentsDB.DeletePayments(
76527652
req.FailedPaymentsOnly, req.FailedHtlcsOnly,
76537653
)
76547654
if err != nil {

server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ type server struct {
335335

336336
invoicesDB invoices.InvoiceDB
337337

338+
// kvPaymentsDB is the DB that contains all functions for managing
339+
// payments.
340+
//
341+
// TODO(ziggie): Replace with interface.
342+
kvPaymentsDB *channeldb.KVPaymentsDB
343+
338344
aliasMgr *aliasmgr.Manager
339345

340346
htlcSwitch *htlcswitch.Switch
@@ -678,6 +684,7 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
678684
addrSource: addrSource,
679685
miscDB: dbs.ChanStateDB,
680686
invoicesDB: dbs.InvoiceDB,
687+
kvPaymentsDB: dbs.KVPaymentsDB,
681688
cc: cc,
682689
sigPool: lnwallet.NewSigPool(cfg.Workers.Sig, cc.Signer),
683690
writePool: writePool,
@@ -1127,9 +1134,7 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
11271134
PathFindingConfig: pathFindingConfig,
11281135
}
11291136

1130-
paymentControl := channeldb.NewPaymentControl(dbs.ChanStateDB)
1131-
1132-
s.controlTower = routing.NewControlTower(paymentControl)
1137+
s.controlTower = routing.NewControlTower(dbs.KVPaymentsDB)
11331138

11341139
strictPruning := cfg.Bitcoin.Node == "neutrino" ||
11351140
cfg.Routing.StrictZombiePruning

0 commit comments

Comments
 (0)