graph/db+sqldb: add HighestChanID to SQLStore

This commit is contained in:
Elle Mouton
2025-05-24 15:21:56 +02:00
parent d93d104a66
commit cf542458bd
5 changed files with 54 additions and 1 deletions

View File

@@ -1861,7 +1861,7 @@ func TestGraphPruning(t *testing.T) {
func TestHighestChanID(t *testing.T) { func TestHighestChanID(t *testing.T) {
t.Parallel() t.Parallel()
graph := MakeTestGraph(t) graph := MakeTestGraphNew(t)
// If we don't yet have any channels in the database, then we should // If we don't yet have any channels in the database, then we should
// get a channel ID of zero if we ask for the highest channel ID. // get a channel ID of zero if we ask for the highest channel ID.

View File

@@ -75,6 +75,7 @@ type SQLQueries interface {
*/ */
CreateChannel(ctx context.Context, arg sqlc.CreateChannelParams) (int64, error) CreateChannel(ctx context.Context, arg sqlc.CreateChannelParams) (int64, error)
GetChannelBySCID(ctx context.Context, arg sqlc.GetChannelBySCIDParams) (sqlc.Channel, error) GetChannelBySCID(ctx context.Context, arg sqlc.GetChannelBySCIDParams) (sqlc.Channel, error)
HighestSCID(ctx context.Context, version int16) ([]byte, error)
CreateChannelExtraType(ctx context.Context, arg sqlc.CreateChannelExtraTypeParams) error CreateChannelExtraType(ctx context.Context, arg sqlc.CreateChannelExtraTypeParams) error
InsertChannelFeature(ctx context.Context, arg sqlc.InsertChannelFeatureParams) error InsertChannelFeature(ctx context.Context, arg sqlc.InsertChannelFeatureParams) error
@@ -512,6 +513,35 @@ func (s *SQLStore) AddChannelEdge(edge *models.ChannelEdgeInfo,
return s.chanScheduler.Execute(ctx, r) return s.chanScheduler.Execute(ctx, r)
} }
// HighestChanID returns the "highest" known channel ID in the channel graph.
// This represents the "newest" channel from the PoV of the chain. This method
// can be used by peers to quickly determine if their graphs are in sync.
//
// NOTE: This is part of the V1Store interface.
func (s *SQLStore) HighestChanID() (uint64, error) {
ctx := context.TODO()
var highestChanID uint64
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
chanID, err := db.HighestSCID(ctx, int16(ProtocolV1))
if errors.Is(err, sql.ErrNoRows) {
return nil
} else if err != nil {
return fmt.Errorf("unable to fetch highest chan ID: %w",
err)
}
highestChanID = byteOrder.Uint64(chanID)
return nil
}, sqldb.NoOpReset)
if err != nil {
return 0, fmt.Errorf("unable to fetch highest chan ID: %w", err)
}
return highestChanID, nil
}
// getNodeByPubKey attempts to look up a target node by its public key. // getNodeByPubKey attempts to look up a target node by its public key.
func getNodeByPubKey(ctx context.Context, db SQLQueries, func getNodeByPubKey(ctx context.Context, db SQLQueries,
pubKey route.Vertex) (int64, *models.LightningNode, error) { pubKey route.Vertex) (int64, *models.LightningNode, error) {

View File

@@ -429,6 +429,21 @@ func (q *Queries) GetSourceNodesByVersion(ctx context.Context, version int16) ([
return items, nil return items, nil
} }
const highestSCID = `-- name: HighestSCID :one
SELECT scid
FROM channels
WHERE version = $1
ORDER BY scid DESC
LIMIT 1
`
func (q *Queries) HighestSCID(ctx context.Context, version int16) ([]byte, error) {
row := q.db.QueryRowContext(ctx, highestSCID, version)
var scid []byte
err := row.Scan(&scid)
return scid, err
}
const insertChannelFeature = `-- name: InsertChannelFeature :exec const insertChannelFeature = `-- name: InsertChannelFeature :exec
/* ───────────────────────────────────────────── /* ─────────────────────────────────────────────
channel_features table queries channel_features table queries

View File

@@ -46,6 +46,7 @@ type Querier interface {
GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error) GetNodeFeaturesByPubKey(ctx context.Context, arg GetNodeFeaturesByPubKeyParams) ([]int32, error)
GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]Node, error) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]Node, error)
GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error) GetSourceNodesByVersion(ctx context.Context, version int16) ([]GetSourceNodesByVersionRow, error)
HighestSCID(ctx context.Context, version int16) ([]byte, error)
InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error InsertAMPSubInvoice(ctx context.Context, arg InsertAMPSubInvoiceParams) error
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error InsertChannelFeature(ctx context.Context, arg InsertChannelFeatureParams) error

View File

@@ -153,6 +153,13 @@ RETURNING id;
SELECT * FROM channels SELECT * FROM channels
WHERE scid = $1 AND version = $2; WHERE scid = $1 AND version = $2;
-- name: HighestSCID :one
SELECT scid
FROM channels
WHERE version = $1
ORDER BY scid DESC
LIMIT 1;
/* ───────────────────────────────────────────── /* ─────────────────────────────────────────────
channel_features table queries channel_features table queries
───────────────────────────────────────────── ─────────────────────────────────────────────