Skip to content

Commit 5318385

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 af6816b commit 5318385

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
@@ -1228,6 +1228,26 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
12281228

12291229
return nil, nil, err
12301230
}
1231+
1232+
// Create the payments DB.
1233+
//
1234+
// NOTE: In the regular build, this will construct a kvdb
1235+
// backed payments backend. With the test_native_sql tag, it
1236+
// will build a SQL payments backend.
1237+
sqlPaymentsDB, err := d.getPaymentsStore(
1238+
baseDB, dbs.ChanStateDB.Backend,
1239+
paymentsdb.WithKeepFailedPaymentAttempts(
1240+
cfg.KeepFailedPaymentAttempts,
1241+
),
1242+
)
1243+
if err != nil {
1244+
err = fmt.Errorf("unable to get payments store: %w",
1245+
err)
1246+
1247+
return nil, nil, err
1248+
}
1249+
1250+
dbs.PaymentsDB = sqlPaymentsDB
12311251
} else {
12321252
// Check if the invoice bucket tombstone is set. If it is, we
12331253
// need to return and ask the user switch back to using the
@@ -1256,40 +1276,35 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
12561276
if err != nil {
12571277
return nil, nil, err
12581278
}
1259-
}
12601279

1261-
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
1262-
if err != nil {
1263-
cleanUp()
1280+
// Create the payments DB.
1281+
kvPaymentsDB, err := paymentsdb.NewKVStore(
1282+
dbs.ChanStateDB,
1283+
paymentsdb.WithKeepFailedPaymentAttempts(
1284+
cfg.KeepFailedPaymentAttempts,
1285+
),
1286+
)
1287+
if err != nil {
1288+
cleanUp()
12641289

1265-
err = fmt.Errorf("unable to open channel graph DB: %w", err)
1266-
d.logger.Error(err)
1290+
err = fmt.Errorf("unable to open payments DB: %w", err)
1291+
d.logger.Error(err)
12671292

1268-
return nil, nil, err
1269-
}
1293+
return nil, nil, err
1294+
}
12701295

1271-
// Mount the payments DB which is only KV for now.
1272-
//
1273-
// TODO(ziggie): Add support for SQL payments DB.
1274-
// Mount the payments DB for the KV store.
1275-
paymentsDBOptions := []paymentsdb.OptionModifier{
1276-
paymentsdb.WithKeepFailedPaymentAttempts(
1277-
cfg.KeepFailedPaymentAttempts,
1278-
),
1296+
dbs.PaymentsDB = kvPaymentsDB
12791297
}
1280-
kvPaymentsDB, err := paymentsdb.NewKVStore(
1281-
dbs.ChanStateDB,
1282-
paymentsDBOptions...,
1283-
)
1298+
1299+
dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
12841300
if err != nil {
12851301
cleanUp()
12861302

1287-
err = fmt.Errorf("unable to open payments DB: %w", err)
1303+
err = fmt.Errorf("unable to open channel graph DB: %w", err)
12881304
d.logger.Error(err)
12891305

12901306
return nil, nil, err
12911307
}
1292-
dbs.PaymentsDB = kvPaymentsDB
12931308

12941309
// Wrap the watchtower client DB and make sure we clean up.
12951310
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)