graph/db: add version parameter to FetchChannelEdgesByID/Outpoint

Make channel edge fetching version-aware by adding gossip version
parameter to FetchChannelEdgesByID and FetchChannelEdgesByOutpoint.
Add corresponding methods to VersionedGraph. V2 policy building is
marked as TODO.
This commit is contained in:
Elle Mouton committed 2026-02-13 07:21:23 +02:00
1 parent 971c0f6eb8
commit d88fa01162
4 files changed
+73 -15

No files matched your search

+23 -2
View File
@@ -711,7 +711,9 @@ func (c *ChannelGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
return c.db.FetchChannelEdgesByOutpoint(op) return c.db.FetchChannelEdgesByOutpoint(
lnwire.GossipVersion1, op,
)
} }
// FetchChannelEdgesByID attempts to lookup directed edges by channel ID. // FetchChannelEdgesByID attempts to lookup directed edges by channel ID.
@@ -719,7 +721,9 @@ func (c *ChannelGraph) FetchChannelEdgesByID(chanID uint64) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
return c.db.FetchChannelEdgesByID(chanID) return c.db.FetchChannelEdgesByID(
lnwire.GossipVersion1, chanID,
)
} }
// ChannelView returns the verifiable edge information for each active channel. // ChannelView returns the verifiable edge information for each active channel.
@@ -785,6 +789,23 @@ func (c *VersionedGraph) FetchNode(ctx context.Context,
return c.db.FetchNode(ctx, c.v, nodePub) return c.db.FetchNode(ctx, c.v, nodePub)
} }
// FetchChannelEdgesByID attempts to lookup directed edges by channel ID.
func (c *VersionedGraph) FetchChannelEdgesByID(chanID uint64) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
return c.db.FetchChannelEdgesByID(c.v, chanID)
}
// FetchChannelEdgesByOutpoint attempts to lookup directed edges by funding
// outpoint.
func (c *VersionedGraph) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
return c.db.FetchChannelEdgesByOutpoint(c.v, op)
}
// AddrsForNode returns all known addresses for the target node public key. // AddrsForNode returns all known addresses for the target node public key.
func (c *VersionedGraph) AddrsForNode(ctx context.Context, func (c *VersionedGraph) AddrsForNode(ctx context.Context,
nodePub *btcec.PublicKey) (bool, []net.Addr, error) { nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
+2 -2
View File
@@ -276,7 +276,7 @@ type Store interface { //nolint:interfacebloat
// houses the general information for the channel itself is returned as // houses the general information for the channel itself is returned as
// well as two structs that contain the routing policies for the channel // well as two structs that contain the routing policies for the channel
// in either direction. // in either direction.
FetchChannelEdgesByOutpoint(op *wire.OutPoint) ( FetchChannelEdgesByOutpoint(v lnwire.GossipVersion, op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) *models.ChannelEdgePolicy, error)
@@ -291,7 +291,7 @@ type Store interface { //nolint:interfacebloat
// zombie within the database. In this case, the ChannelEdgePolicy's // zombie within the database. In this case, the ChannelEdgePolicy's
// will be nil, and the ChannelEdgeInfo will only include the public // will be nil, and the ChannelEdgeInfo will only include the public
// keys of each node. // keys of each node.
FetchChannelEdgesByID(chanID uint64) ( FetchChannelEdgesByID(v lnwire.GossipVersion, chanID uint64) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) *models.ChannelEdgePolicy, error)
+12 -4
View File
@@ -3811,8 +3811,8 @@ func computeEdgePolicyKeys(info *models.ChannelEdgeInfo) ([]byte, []byte) {
// found, then ErrEdgeNotFound is returned. A struct which houses the general // found, then ErrEdgeNotFound is returned. A struct which houses the general
// information for the channel itself is returned as well as two structs that // information for the channel itself is returned as well as two structs that
// contain the routing policies for the channel in either direction. // contain the routing policies for the channel in either direction.
func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) ( func (c *KVStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
var ( var (
@@ -3821,6 +3821,10 @@ func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
policy2 *models.ChannelEdgePolicy policy2 *models.ChannelEdgePolicy
) )
if v != lnwire.GossipVersion1 {
return nil, nil, nil, ErrVersionNotSupportedForKVDB
}
err := kvdb.View(c.db, func(tx kvdb.RTx) error { err := kvdb.View(c.db, func(tx kvdb.RTx) error {
// First, grab the node bucket. This will be used to populate // First, grab the node bucket. This will be used to populate
// the Node pointers in each edge read from disk. // the Node pointers in each edge read from disk.
@@ -3897,10 +3901,14 @@ func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
// ErrZombieEdge an be returned if the edge is currently marked as a zombie // ErrZombieEdge an be returned if the edge is currently marked as a zombie
// within the database. In this case, the ChannelEdgePolicy's will be nil, and // within the database. In this case, the ChannelEdgePolicy's will be nil, and
// the ChannelEdgeInfo will only include the public keys of each node. // the ChannelEdgeInfo will only include the public keys of each node.
func (c *KVStore) FetchChannelEdgesByID(chanID uint64) ( func (c *KVStore) FetchChannelEdgesByID(v lnwire.GossipVersion,
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, chanID uint64) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
if v != lnwire.GossipVersion1 {
return nil, nil, nil, ErrVersionNotSupportedForKVDB
}
var ( var (
edgeInfo *models.ChannelEdgeInfo edgeInfo *models.ChannelEdgeInfo
policy1 *models.ChannelEdgePolicy policy1 *models.ChannelEdgePolicy
+36 -7
View File
@@ -2008,8 +2008,8 @@ func (s *SQLStore) DeleteChannelEdges(v lnwire.GossipVersion,
// the ChannelEdgeInfo will only include the public keys of each node. // the ChannelEdgeInfo will only include the public keys of each node.
// //
// NOTE: part of the Store interface. // NOTE: part of the Store interface.
func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) ( func (s *SQLStore) FetchChannelEdgesByID(v lnwire.GossipVersion,
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, chanID uint64) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
var ( var (
@@ -2018,11 +2018,18 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
policy1, policy2 *models.ChannelEdgePolicy policy1, policy2 *models.ChannelEdgePolicy
chanIDB = channelIDToBytes(chanID) chanIDB = channelIDToBytes(chanID)
) )
if !isKnownGossipVersion(v) {
return nil, nil, nil, fmt.Errorf(
"unsupported gossip version: %d", v,
)
}
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
row, err := db.GetChannelBySCIDWithPolicies( row, err := db.GetChannelBySCIDWithPolicies(
ctx, sqlc.GetChannelBySCIDWithPoliciesParams{ ctx, sqlc.GetChannelBySCIDWithPoliciesParams{
Scid: chanIDB, Scid: chanIDB,
Version: int16(lnwire.GossipVersion1), Version: int16(v),
}, },
) )
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
@@ -2031,7 +2038,7 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
zombie, err := db.GetZombieChannel( zombie, err := db.GetZombieChannel(
ctx, sqlc.GetZombieChannelParams{ ctx, sqlc.GetZombieChannelParams{
Scid: chanIDB, Scid: chanIDB,
Version: int16(lnwire.GossipVersion1), Version: int16(v),
}, },
) )
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
@@ -2118,8 +2125,8 @@ func (s *SQLStore) FetchChannelEdgesByID(chanID uint64) (
// contain the routing policies for the channel in either direction. // contain the routing policies for the channel in either direction.
// //
// NOTE: part of the Store interface. // NOTE: part of the Store interface.
func (s *SQLStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) ( func (s *SQLStore) FetchChannelEdgesByOutpoint(v lnwire.GossipVersion,
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy, op *wire.OutPoint) (*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) { *models.ChannelEdgePolicy, error) {
var ( var (
@@ -2127,11 +2134,18 @@ func (s *SQLStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) (
edge *models.ChannelEdgeInfo edge *models.ChannelEdgeInfo
policy1, policy2 *models.ChannelEdgePolicy policy1, policy2 *models.ChannelEdgePolicy
) )
if !isKnownGossipVersion(v) {
return nil, nil, nil, fmt.Errorf(
"unsupported gossip version: %d", v,
)
}
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
row, err := db.GetChannelByOutpointWithPolicies( row, err := db.GetChannelByOutpointWithPolicies(
ctx, sqlc.GetChannelByOutpointWithPoliciesParams{ ctx, sqlc.GetChannelByOutpointWithPoliciesParams{
Outpoint: op.String(), Outpoint: op.String(),
Version: int16(lnwire.GossipVersion1), Version: int16(v),
}, },
) )
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
@@ -4662,6 +4676,21 @@ func getAndBuildChanPolicies(ctx context.Context, cfg *sqldb.QueryConfig,
return nil, nil, nil return nil, nil, nil
} }
// TODO(elle): update to support v2 policies.
if dbPol1 != nil &&
lnwire.GossipVersion(dbPol1.Version) != lnwire.GossipVersion1 {
return nil, nil, fmt.Errorf("unsupported policy1 version: %d",
dbPol1.Version)
}
if dbPol2 != nil &&
lnwire.GossipVersion(dbPol2.Version) != lnwire.GossipVersion1 {
return nil, nil, fmt.Errorf("unsupported policy2 version: %d",
dbPol2.Version)
}
var policyIDs = make([]int64, 0, 2) var policyIDs = make([]int64, 0, 2)
if dbPol1 != nil { if dbPol1 != nil {
policyIDs = append(policyIDs, dbPol1.ID) policyIDs = append(policyIDs, dbPol1.ID)