Skip to content

Commit cf54245

Browse files
committed
graph/db+sqldb: add HighestChanID to SQLStore
1 parent d93d104 commit cf54245

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

graph/db/graph_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ func TestGraphPruning(t *testing.T) {
18611861
func TestHighestChanID(t *testing.T) {
18621862
t.Parallel()
18631863

1864-
graph := MakeTestGraph(t)
1864+
graph := MakeTestGraphNew(t)
18651865

18661866
// If we don't yet have any channels in the database, then we should
18671867
// get a channel ID of zero if we ask for the highest channel ID.

graph/db/sql_store.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type SQLQueries interface {
7575
*/
7676
CreateChannel(ctx context.Context, arg sqlc.CreateChannelParams) (int64, error)
7777
GetChannelBySCID(ctx context.Context, arg sqlc.GetChannelBySCIDParams) (sqlc.Channel, error)
78+
HighestSCID(ctx context.Context, version int16) ([]byte, error)
7879

7980
CreateChannelExtraType(ctx context.Context, arg sqlc.CreateChannelExtraTypeParams) error
8081
InsertChannelFeature(ctx context.Context, arg sqlc.InsertChannelFeatureParams) error
@@ -512,6 +513,35 @@ func (s *SQLStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
512513
return s.chanScheduler.Execute(ctx, r)
513514
}
514515

516+
// HighestChanID returns the "highest" known channel ID in the channel graph.
517+
// This represents the "newest" channel from the PoV of the chain. This method
518+
// can be used by peers to quickly determine if their graphs are in sync.
519+
//
520+
// NOTE: This is part of the V1Store interface.
521+
func (s *SQLStore) HighestChanID() (uint64, error) {
522+
ctx := context.TODO()
523+
524+
var highestChanID uint64
525+
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
526+
chanID, err := db.HighestSCID(ctx, int16(ProtocolV1))
527+
if errors.Is(err, sql.ErrNoRows) {
528+
return nil
529+
} else if err != nil {
530+
return fmt.Errorf("unable to fetch highest chan ID: %w",
531+
err)
532+
}
533+
534+
highestChanID = byteOrder.Uint64(chanID)
535+
536+
return nil
537+
}, sqldb.NoOpReset)
538+
if err != nil {
539+
return 0, fmt.Errorf("unable to fetch highest chan ID: %w", err)
540+
}
541+
542+
return highestChanID, nil
543+
}
544+
515545
// getNodeByPubKey attempts to look up a target node by its public key.
516546
func getNodeByPubKey(ctx context.Context, db SQLQueries,
517547
pubKey route.Vertex) (int64, *models.LightningNode, error) {

sqldb/sqlc/graph.sql.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqldb/sqlc/queries/graph.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ RETURNING id;
153153
SELECT * FROM channels
154154
WHERE scid = $1 AND version = $2;
155155

156+
-- name: HighestSCID :one
157+
SELECT scid
158+
FROM channels
159+
WHERE version = $1
160+
ORDER BY scid DESC
161+
LIMIT 1;
162+
156163
/* ─────────────────────────────────────────────
157164
channel_features table queries
158165
─────────────────────────────────────────────

0 commit comments

Comments
 (0)