Skip to content

Commit 933ab3c

Browse files
committed
graph/db+sqldb: impl PutClosedScid and IsClosedScid
1 parent 5effa96 commit 933ab3c

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

graph/db/graph_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4325,7 +4325,7 @@ func TestGraphLoading(t *testing.T) {
43254325
func TestClosedScid(t *testing.T) {
43264326
t.Parallel()
43274327

4328-
graph := MakeTestGraph(t)
4328+
graph := MakeTestGraphNew(t)
43294329

43304330
scid := lnwire.ShortChannelID{}
43314331

graph/db/sql_store.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ type SQLQueries interface {
137137
GetPruneTip(ctx context.Context) (sqlc.PruneLog, error)
138138
UpsertPruneLogEntry(ctx context.Context, arg sqlc.UpsertPruneLogEntryParams) error
139139
DeletePruneLogEntriesInRange(ctx context.Context, arg sqlc.DeletePruneLogEntriesInRangeParams) error
140+
141+
/*
142+
Closed SCID table queries.
143+
*/
144+
InsertClosedChannel(ctx context.Context, scid []byte) error
145+
IsClosedChannel(ctx context.Context, scid []byte) (bool, error)
140146
}
141147

142148
// BatchedSQLQueries is a version of SQLQueries that's capable of batched
@@ -2624,6 +2630,50 @@ func (s *SQLStore) AddEdgeProof(scid lnwire.ShortChannelID,
26242630
return nil
26252631
}
26262632

2633+
// PutClosedScid stores a SCID for a closed channel in the database. This is so
2634+
// that we can ignore channel announcements that we know to be closed without
2635+
// having to validate them and fetch a block.
2636+
//
2637+
// NOTE: part of the V1Store interface.
2638+
func (s *SQLStore) PutClosedScid(scid lnwire.ShortChannelID) error {
2639+
var (
2640+
ctx = context.TODO()
2641+
chanIDB = channelIDToBytes(scid.ToUint64())
2642+
)
2643+
2644+
return s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
2645+
return db.InsertClosedChannel(ctx, chanIDB[:])
2646+
}, sqldb.NoOpReset)
2647+
}
2648+
2649+
// IsClosedScid checks whether a channel identified by the passed in scid is
2650+
// closed. This helps avoid having to perform expensive validation checks.
2651+
//
2652+
// NOTE: part of the V1Store interface.
2653+
func (s *SQLStore) IsClosedScid(scid lnwire.ShortChannelID) (bool, error) {
2654+
var (
2655+
ctx = context.TODO()
2656+
isClosed bool
2657+
chanIDB = channelIDToBytes(scid.ToUint64())
2658+
)
2659+
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
2660+
var err error
2661+
isClosed, err = db.IsClosedChannel(ctx, chanIDB[:])
2662+
if err != nil {
2663+
return fmt.Errorf("unable to fetch closed channel: %w",
2664+
err)
2665+
}
2666+
2667+
return nil
2668+
}, sqldb.NoOpReset)
2669+
if err != nil {
2670+
return false, fmt.Errorf("unable to fetch closed channel: %w",
2671+
err)
2672+
}
2673+
2674+
return isClosed, nil
2675+
}
2676+
26272677
// forEachNodeDirectedChannel iterates through all channels of a given
26282678
// node, executing the passed callback on the directed edge representing the
26292679
// channel and its incoming policy. If the node is not found, no error is

sqldb/sqlc/graph.sql.go

Lines changed: 31 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: 2 additions & 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,20 @@ LIMIT 1;
709709
DELETE FROM prune_log
710710
WHERE block_height >= @start_height
711711
AND block_height <= @end_height;
712+
713+
/* ─────────────────────────────────────────────
714+
closed_scid table queries
715+
────────────────────────────────────────────-
716+
*/
717+
718+
-- name: InsertClosedChannel :exec
719+
INSERT INTO closed_scids (scid)
720+
VALUES ($1)
721+
ON CONFLICT (scid) DO NOTHING;
722+
723+
-- name: IsClosedChannel :one
724+
SELECT EXISTS (
725+
SELECT 1
726+
FROM closed_scids
727+
WHERE scid = $1
728+
);

0 commit comments

Comments
 (0)