Skip to content

[draft] graph/db+sqldb: graph store SQL implementation + migration #9932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6808596
graph/db: make makeZombiePubkeys reusable
ellemouton Jun 11, 2025
0d7c617
sqldb/sqlc: add zombie index table
ellemouton Jun 11, 2025
aa3c2a4
graph/db+sqldb: implement various zombie index methods
ellemouton Jun 11, 2025
64bc4e7
graph/db+sqldb: implement DeleteChannelEdges
ellemouton Jun 11, 2025
2d6318a
graph/db+sqldb: implement FetchChannelEdgesByOutpoint/SCID
ellemouton Jun 11, 2025
c949230
graph/db+sqldb: implement HasChannelEdge and ChannelID
ellemouton Jun 11, 2025
7f7bad9
graph/db: impl FilterKnownChanIDs
ellemouton Jun 11, 2025
ecbc127
graph/db: implement FetchChanInfos
ellemouton Jun 11, 2025
d93cd4d
graph/db+sqldb: impl IsPublicNode
ellemouton Jun 11, 2025
4578b8a
graph/db+sqldb: impl DisabledChannelIDs
ellemouton Jun 11, 2025
2b7cef4
sqldb/sqlc: prune_log schema
ellemouton Jun 11, 2025
7b1d01c
graph/db+sqldb: impl PruneGraphNodes
ellemouton Jun 11, 2025
112c9cd
graph/db+sqldb: impl PruneGraph, PruneTip, ChannelView
ellemouton Jun 11, 2025
6aac2f9
sqldb+graph/db: impl DisconnectBlockAtHeight
ellemouton Jun 11, 2025
cc90f8a
graph/db+sqldb: imple AddEdgeProof
ellemouton Jun 11, 2025
00d2f52
sqldb: closed_scids table
ellemouton Jun 11, 2025
0c5ef4e
graph/db+sqldb: impl PutClosedScid and IsClosedScid
ellemouton Jun 11, 2025
5ac2072
graph/db: impl GraphSession
ellemouton Jun 11, 2025
e1f3e31
graph/db: impl ForEachChannelCacheable
ellemouton Jun 11, 2025
f4e031c
graph/db: start lastID for pagination at -1
ellemouton Jun 24, 2025
3f6e8fe
graph/db: fix log precision
ellemouton Jun 24, 2025
3025be8
multi: use MakeTestGraphNew for all remaining unit tests
ellemouton May 26, 2025
773ec56
multi: rename MakeTestGraphNew to MakeTestGraph
ellemouton Jun 11, 2025
a498c11
graph/db: remove KVStore fall-back
ellemouton Jun 11, 2025
ad28b35
migrations
ellemouton Apr 17, 2025
a7e1e49
plug into itests behind feature flag
ellemouton May 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,14 @@ jobs:
args: backend=bitcoind dbbackend=sqlite
- name: bitcoind-sqlite-nativesql
args: backend=bitcoind dbbackend=sqlite nativesql=true
- name: bitcoind-sqlite=nativesql-experiment
args: backend=bitcoind dbbackend=sqlite nativesql=true tags=test_native_sql
- name: bitcoind-postgres
args: backend=bitcoind dbbackend=postgres
- name: bitcoind-postgres-nativesql
args: backend=bitcoind dbbackend=postgres nativesql=true
- name: bitcoind-postgres-nativesql-experiment
args: backend=bitcoind dbbackend=postgres nativesql=true tags=test_native_sql
steps:
- name: git checkout
uses: actions/checkout@v4
Expand Down
56 changes: 40 additions & 16 deletions config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,23 +1046,13 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
)
}

graphStore, err := graphdb.NewKVStore(
kvGraphStore, err := graphdb.NewKVStore(
databaseBackends.GraphDB, graphDBOptions...,
)
if err != nil {
return nil, nil, err
}

dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
if err != nil {
cleanUp()

err = fmt.Errorf("unable to open graph DB: %w", err)
d.logger.Error(err)

return nil, nil, err
}

dbOptions := []channeldb.OptionModifier{
channeldb.OptionDryRunMigration(cfg.DryRunMigration),
channeldb.OptionKeepFailedPaymentAttempts(
Expand Down Expand Up @@ -1098,6 +1088,8 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
return nil, nil, err
}

var graphStore graphdb.V1Store

// Instantiate a native SQL store if the flag is set.
if d.cfg.DB.UseNativeSQL {
migrations := sqldb.GetMigrations()
Expand All @@ -1110,7 +1102,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
// migration's version (7), it will be skipped permanently,
// regardless of the flag.
if !d.cfg.DB.SkipNativeSQLMigration {
migrationFn := func(tx *sqlc.Queries) error {
invoiceMigFn := func(tx *sqlc.Queries) error {
err := invoices.MigrateInvoicesToSQL(
ctx, dbs.ChanStateDB.Backend,
dbs.ChanStateDB, tx,
Expand All @@ -1132,11 +1124,21 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
// Make sure we attach the custom migration function to
// the correct migration version.
for i := 0; i < len(migrations); i++ {
if migrations[i].Version != invoiceMigration {
version := migrations[i].Version
if version == invoiceMigration {
migrations[i].MigrationFn = invoiceMigFn

continue
}

migFn, ok := getSQLMigration(
ctx, version, kvGraphStore,
)
if !ok {
continue
}

migrations[i].MigrationFn = migrationFn
migrations[i].MigrationFn = migFn
}
}

Expand All @@ -1156,17 +1158,27 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
// the base DB and transaction executor for the native SQL
// invoice store.
baseDB := dbs.NativeSQLStore.GetBaseDB()
executor := sqldb.NewTransactionExecutor(
invoiceExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) invoices.SQLInvoiceQueries {
return baseDB.WithTx(tx)
},
)

sqlInvoiceDB := invoices.NewSQLStore(
executor, clock.NewDefaultClock(),
invoiceExecutor, clock.NewDefaultClock(),
)

dbs.InvoiceDB = sqlInvoiceDB

graphStore, err = d.getGraphStore(
baseDB, kvGraphStore, graphDBOptions...,
)
if err != nil {
err = fmt.Errorf("unable to get graph store: %w", err)
d.logger.Error(err)

return nil, nil, err
}
} else {
// Check if the invoice bucket tombstone is set. If it is, we
// need to return and ask the user switch back to using the
Expand All @@ -1188,6 +1200,18 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
}

dbs.InvoiceDB = dbs.ChanStateDB

graphStore = kvGraphStore
}

dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...)
if err != nil {
cleanUp()

err = fmt.Errorf("unable to open channel graph: %w", err)
d.logger.Error(err)

return nil, nil, err
}

// Wrap the watchtower client DB and make sure we clean up.
Expand Down
24 changes: 24 additions & 0 deletions config_prod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !test_native_sql

package lnd

import (
"context"

graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/sqlc"
)

func getSQLMigration(ctx context.Context, version int,
kvGraphStore *graphdb.KVStore) (func(tx *sqlc.Queries) error, bool) {

return nil, false
}

func (d *DefaultDatabaseBuilder) getGraphStore(_ *sqldb.BaseDB,
kvGraphStore *graphdb.KVStore,
_ ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {

return kvGraphStore, nil
}
53 changes: 53 additions & 0 deletions config_test_native_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build test_native_sql

package lnd

import (
"context"
"database/sql"
"fmt"

graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/sqlc"
)

const graphSQLMigration = 9

func getSQLMigration(ctx context.Context, version int,
kvGraphStore *graphdb.KVStore) (func(tx *sqlc.Queries) error, bool) {

if version != graphSQLMigration {
return nil, false
}

return func(tx *sqlc.Queries) error {
err := graphdb.MigrateGraphToSQL(
ctx, kvGraphStore, tx,
)
if err != nil {
return fmt.Errorf("failed to migrate "+
"graph to SQL: %w", err)
}

return nil
}, true
}

func (d *DefaultDatabaseBuilder) getGraphStore(baseDB *sqldb.BaseDB,
_ *graphdb.KVStore,
opts ...graphdb.StoreOptionModifier) (graphdb.V1Store, error) {

graphExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
return baseDB.WithTx(tx)
},
)

return graphdb.NewSQLStore(
&graphdb.SQLStoreConfig{
ChainHash: *d.cfg.ActiveNetParams.GenesisHash,
},
graphExecutor, opts...,
)
}
5 changes: 5 additions & 0 deletions docs/release-notes/release-notes-0.20.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ circuit. The indices are only available for forwarding events saved after v0.20.
* [4](https://github.com/lightningnetwork/lnd/pull/9931)
* [5](https://github.com/lightningnetwork/lnd/pull/9935)
* [6](https://github.com/lightningnetwork/lnd/pull/9936)
* [7](https://github.com/lightningnetwork/lnd/pull/9937)
* [8](https://github.com/lightningnetwork/lnd/pull/9938)
* [9](https://github.com/lightningnetwork/lnd/pull/9939)
* [10](https://github.com/lightningnetwork/lnd/pull/9971)
* [11](https://github.com/lightningnetwork/lnd/pull/9972)

## RPC Updates

Expand Down
66 changes: 38 additions & 28 deletions graph/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,15 +892,21 @@ func TestPruneChannelGraphDoubleDisabled(t *testing.T) {
}

func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
timestamp := time.Now()
nextTimeStamp := func() time.Time {
timestamp = timestamp.Add(time.Second)

return timestamp
}

// We'll create the following test graph so that only the last channel
// is pruned. We'll use a fresh timestamp to ensure they're not pruned
// according to that heuristic.
timestamp := time.Now()
testChannels := []*testChannel{
// Channel from self shouldn't be pruned.
symmetricTestChannel(
"self", "a", 100000, &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: true,
}, 99,
),
Expand All @@ -918,7 +924,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
Node1: &testChannelEnd{
Alias: "a",
testChannelPolicy: &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: true,
},
},
Expand All @@ -932,7 +938,7 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
Node1: &testChannelEnd{
Alias: "a",
testChannelPolicy: &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: false,
},
},
Expand All @@ -946,14 +952,14 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {
Node1: &testChannelEnd{
Alias: "a",
testChannelPolicy: &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: true,
},
},
Node2: &testChannelEnd{
Alias: "b",
testChannelPolicy: &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: false,
},
},
Expand All @@ -963,13 +969,13 @@ func testPruneChannelGraphDoubleDisabled(t *testing.T, assumeValid bool) {

// Both edges enabled.
symmetricTestChannel("c", "d", 100000, &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: false,
}, 2),

// Both edges disabled, only one pruned.
symmetricTestChannel("e", "f", 100000, &testChannelPolicy{
LastUpdate: timestamp,
LastUpdate: nextTimeStamp(),
Disabled: true,
}, 3),
}
Expand Down Expand Up @@ -1363,7 +1369,9 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
testAddrs = append(testAddrs, testAddr)

// Next, create a temporary graph database for usage within the test.
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
graph := graphdb.MakeTestGraph(
t, graphdb.WithUseGraphCache(useCache),
)

aliasMap := make(map[string]route.Vertex)
privKeyMap := make(map[string]*btcec.PrivateKey)
Expand Down Expand Up @@ -1441,6 +1449,13 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
}

source = dbNode

// Set the selected source node
if err := graph.SetSourceNode(ctx, source); err != nil {
return nil, err
}

continue
}

// With the node fully parsed, add it as a vertex within the
Expand All @@ -1450,13 +1465,6 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
}
}

if source != nil {
// Set the selected source node
if err := graph.SetSourceNode(ctx, source); err != nil {
return nil, err
}
}

// With all the vertexes inserted, we can now insert the edges into the
// test graph.
for _, edge := range g.Edges {
Expand Down Expand Up @@ -1739,14 +1747,16 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
testAddrs = append(testAddrs, testAddr)

// Next, create a temporary graph database for usage within the test.
graph := graphdb.MakeTestGraph(t, graphdb.WithUseGraphCache(useCache))
graph := graphdb.MakeTestGraph(
t, graphdb.WithUseGraphCache(useCache),
)

aliasMap := make(map[string]route.Vertex)
privKeyMap := make(map[string]*btcec.PrivateKey)

nodeIndex := byte(0)
addNodeWithAlias := func(alias string, features *lnwire.FeatureVector) (
*models.LightningNode, error) {
addNodeWithAlias := func(alias string,
features *lnwire.FeatureVector) error {

keyBytes := []byte{
0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -1776,26 +1786,26 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,

// With the node fully parsed, add it as a vertex within the
// graph.
if err := graph.AddLightningNode(ctx, dbNode); err != nil {
return nil, err
if alias == source {
err = graph.SetSourceNode(ctx, dbNode)
require.NoError(t, err)
} else {
err := graph.AddLightningNode(ctx, dbNode)
require.NoError(t, err)
}

aliasMap[alias] = dbNode.PubKeyBytes
nodeIndex++

return dbNode, nil
return nil
}

// Add the source node.
dbNode, err := addNodeWithAlias(source, lnwire.EmptyFeatureVector())
err = addNodeWithAlias(source, lnwire.EmptyFeatureVector())
if err != nil {
return nil, err
}

if err = graph.SetSourceNode(ctx, dbNode); err != nil {
return nil, err
}

// Initialize variable that keeps track of the next channel id to assign
// if none is specified.
nextUnassignedChannelID := uint64(100000)
Expand All @@ -1813,7 +1823,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
features =
node.testChannelPolicy.Features
}
_, err := addNodeWithAlias(
err := addNodeWithAlias(
node.Alias, features,
)
if err != nil {
Expand Down
Loading
Loading