chanevents: add short-chan-id store lookup

Add GetChannelByShortChanID, the inverse of AddChannel. The
forwarding-ability analyzer receives scids from lnd's forwarding history
and must map them back to the chanevents store's internal channel id to
query events.
This commit is contained in:
bitromortac 2026-05-12 10:51:42 +02:00
parent 2363173325
commit be671db05f
2 changed files with 38 additions and 0 deletions

View file

@ -192,6 +192,30 @@ func (s *Store) GetChannel(ctx context.Context, channelPoint string) (*Channel,
}, nil
}
// GetChannelByShortChanID retrieves a channel by its short channel ID,
// returning ErrUnknownChannel if no row matches.
func (s *Store) GetChannelByShortChanID(ctx context.Context,
shortChannelID uint64) (*Channel, error) {
dbChannel, err := s.db.GetChannelByShortChanID(
ctx, scidToInt64(shortChannelID),
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUnknownChannel
}
return nil, err
}
return &Channel{
ID: dbChannel.ID,
ChannelPoint: dbChannel.ChannelPoint,
ShortChannelID: int64ToSCID(dbChannel.ShortChannelID),
PeerID: dbChannel.PeerID,
}, nil
}
// AddChannelEvent adds a new channel event.
func (s *Store) AddChannelEvent(ctx context.Context,
event *ChannelEvent) error {

View file

@ -93,6 +93,20 @@ func TestStore(t *testing.T) {
require.Equal(t, testShortChanID1, dbChannel.ShortChannelID)
require.Equal(t, peerID, dbChannel.PeerID)
// Look up the same channel by its scid; the analyzer relies on this
// inverse of AddChannel.
dbChannel, err = store.GetChannelByShortChanID(ctx, testShortChanID1)
require.NoError(t, err)
require.Equal(t, channelID, dbChannel.ID)
require.Equal(t, testChanPoint1, dbChannel.ChannelPoint)
require.Equal(t, testShortChanID1, dbChannel.ShortChannelID)
require.Equal(t, peerID, dbChannel.PeerID)
// An unknown scid surfaces the typed sentinel, not raw sql.ErrNoRows.
dbChannel, err = store.GetChannelByShortChanID(ctx, 9999)
require.ErrorIs(t, err, ErrUnknownChannel)
require.Nil(t, dbChannel)
// Add a second channel for the same peer.
channel2ID, err := store.AddChannel(
ctx, testChanPoint2, testShortChanID2, peerID,