Skip to content

Commit bc7192f

Browse files
committed
lnd+paymentsdb: introduce harness for the payment sql backend
We prepare the code for the sql payment backend. However no payment db interface method for the sql backend is implemented yet. This will be done in the following commits. They currently use the embedded KVStore to satify the build environment.
1 parent 096ab65 commit bc7192f

File tree

4 files changed

+144
-22
lines changed

4 files changed

+144
-22
lines changed

config_builder.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,26 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
12251225

12261226
return nil, nil, err
12271227
}
1228+
1229+
// Create the payments DB.
1230+
//
1231+
// NOTE: In the regular build, this will construct a kvdb
1232+
// backed payments backend. With the test_native_sql tag, it
1233+
// will build a SQL payments backend.
1234+
sqlPaymentsDB, err := d.getPaymentsStore(
1235+
baseDB, dbs.ChanStateDB.Backend,
1236+
paymentsdb.WithKeepFailedPaymentAttempts(
1237+
cfg.KeepFailedPaymentAttempts,
1238+
),
1239+
)
1240+
if err != nil {
1241+
err = fmt.Errorf("unable to get payments store: %w",
1242+
err)
1243+
1244+
return nil, nil, err
1245+
}
1246+
1247+
dbs.PaymentsDB = sqlPaymentsDB
12281248
} else {
12291249
// Check if the invoice bucket tombstone is set. If it is, we
12301250
// need to return and ask the user switch back to using the
@@ -1253,40 +1273,35 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
12531273
if err != nil {
12541274
return nil, nil, err
12551275
}
1256-
}
12571276

1258-
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
1259-
if err != nil {
1260-
cleanUp()
1277+
// Create the payments DB.
1278+
kvPaymentsDB, err := paymentsdb.NewKVStore(
1279+
dbs.ChanStateDB,
1280+
paymentsdb.WithKeepFailedPaymentAttempts(
1281+
cfg.KeepFailedPaymentAttempts,
1282+
),
1283+
)
1284+
if err != nil {
1285+
cleanUp()
12611286

1262-
err = fmt.Errorf("unable to open channel graph DB: %w", err)
1263-
d.logger.Error(err)
1287+
err = fmt.Errorf("unable to open payments DB: %w", err)
1288+
d.logger.Error(err)
12641289

1265-
return nil, nil, err
1266-
}
1290+
return nil, nil, err
1291+
}
12671292

1268-
// Mount the payments DB which is only KV for now.
1269-
//
1270-
// TODO(ziggie): Add support for SQL payments DB.
1271-
// Mount the payments DB for the KV store.
1272-
paymentsDBOptions := []paymentsdb.OptionModifier{
1273-
paymentsdb.WithKeepFailedPaymentAttempts(
1274-
cfg.KeepFailedPaymentAttempts,
1275-
),
1293+
dbs.PaymentsDB = kvPaymentsDB
12761294
}
1277-
kvPaymentsDB, err := paymentsdb.NewKVStore(
1278-
dbs.ChanStateDB,
1279-
paymentsDBOptions...,
1280-
)
1295+
1296+
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
12811297
if err != nil {
12821298
cleanUp()
12831299

1284-
err = fmt.Errorf("unable to open payments DB: %w", err)
1300+
err = fmt.Errorf("unable to open channel graph DB: %w", err)
12851301
d.logger.Error(err)
12861302

12871303
return nil, nil, err
12881304
}
1289-
dbs.PaymentsDB = kvPaymentsDB
12901305

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

config_prod.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"context"
77

88
"github.com/lightningnetwork/lnd/kvdb"
9+
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
10+
"github.com/lightningnetwork/lnd/sqldb"
911
"github.com/lightningnetwork/lnd/sqldb/sqlc"
1012
)
1113

@@ -24,3 +26,12 @@ func (d *DefaultDatabaseBuilder) getSQLMigration(ctx context.Context,
2426

2527
return nil, false
2628
}
29+
30+
// getPaymentsStore returns a paymentsdb.DB backed by a paymentsdb.KVStore
31+
// implementation.
32+
func (d *DefaultDatabaseBuilder) getPaymentsStore(_ *sqldb.BaseDB,
33+
kvBackend kvdb.Backend,
34+
opts ...paymentsdb.OptionModifier) (paymentsdb.DB, error) {
35+
36+
return paymentsdb.NewKVStore(kvBackend, opts...)
37+
}

config_test_native_sql.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ package lnd
44

55
import (
66
"context"
7+
"database/sql"
78

89
"github.com/lightningnetwork/lnd/kvdb"
10+
"github.com/lightningnetwork/lnd/lncfg"
11+
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
12+
"github.com/lightningnetwork/lnd/sqldb"
913
"github.com/lightningnetwork/lnd/sqldb/sqlc"
1014
)
1115

@@ -25,3 +29,28 @@ func (d *DefaultDatabaseBuilder) getSQLMigration(_ context.Context,
2529
return nil, false
2630
}
2731
}
32+
33+
// getPaymentsStore returns a paymentsdb.DB backed by a paymentsdb.SQLStore
34+
// implementation.
35+
func (d *DefaultDatabaseBuilder) getPaymentsStore(baseDB *sqldb.BaseDB,
36+
kvBackend kvdb.Backend,
37+
opts ...paymentsdb.OptionModifier) (paymentsdb.DB, error) {
38+
39+
paymentsExecutor := sqldb.NewTransactionExecutor(
40+
baseDB, func(tx *sql.Tx) paymentsdb.SQLQueries {
41+
return baseDB.WithTx(tx)
42+
},
43+
)
44+
45+
queryConfig := d.cfg.DB.Sqlite.QueryConfig
46+
if d.cfg.DB.Backend == lncfg.PostgresBackend {
47+
queryConfig = d.cfg.DB.Postgres.QueryConfig
48+
}
49+
50+
return paymentsdb.NewSQLStore(
51+
&paymentsdb.SQLStoreConfig{
52+
QueryCfg: &queryConfig,
53+
},
54+
paymentsExecutor, opts...,
55+
)
56+
}

payments/db/sql_store.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package paymentsdb
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/lightningnetwork/lnd/sqldb"
7+
)
8+
9+
// SQLQueries is a subset of the sqlc.Querier interface that can be used to
10+
// execute queries against the SQL payments tables.
11+
type SQLQueries interface {
12+
}
13+
14+
// BatchedSQLQueries is a version of the SQLQueries that's capable
15+
// of batched database operations.
16+
type BatchedSQLQueries interface {
17+
SQLQueries
18+
sqldb.BatchedTx[SQLQueries]
19+
}
20+
21+
// SQLStore represents a storage backend.
22+
type SQLStore struct {
23+
// TODO(ziggie): Remove the KVStore once all the interface functions are
24+
// implemented.
25+
KVStore
26+
27+
cfg *SQLStoreConfig
28+
db BatchedSQLQueries
29+
30+
// keepFailedPaymentAttempts is a flag that indicates whether we should
31+
// keep failed payment attempts in the database.
32+
keepFailedPaymentAttempts bool
33+
}
34+
35+
// A compile-time constraint to ensure SQLStore implements DB.
36+
var _ DB = (*SQLStore)(nil)
37+
38+
// SQLStoreConfig holds the configuration for the SQLStore.
39+
type SQLStoreConfig struct {
40+
// QueryConfig holds configuration values for SQL queries.
41+
QueryCfg *sqldb.QueryConfig
42+
}
43+
44+
// NewSQLStore creates a new SQLStore instance given an open
45+
// BatchedSQLPaymentsQueries storage backend.
46+
func NewSQLStore(cfg *SQLStoreConfig, db BatchedSQLQueries,
47+
options ...OptionModifier) (*SQLStore, error) {
48+
49+
opts := DefaultOptions()
50+
for _, applyOption := range options {
51+
applyOption(opts)
52+
}
53+
54+
if opts.NoMigration {
55+
return nil, fmt.Errorf("the NoMigration option is not yet " +
56+
"supported for SQL stores")
57+
}
58+
59+
return &SQLStore{
60+
cfg: cfg,
61+
db: db,
62+
keepFailedPaymentAttempts: opts.KeepFailedPaymentAttempts,
63+
}, nil
64+
}
65+
66+
// A compile-time constraint to ensure SQLStore implements DB.
67+
var _ DB = (*SQLStore)(nil)

0 commit comments

Comments
 (0)