Merge pull request #11175 from ViktorT-11/2026-08-gossip-version-agnostic-store
What changed, and why it matters
This commit refactors how the Lightning Network Daemon (LND) stores and queries channel graph data so that the same channel or node can exist under both old (v1) and new (v2) gossip protocols without the database returning stale, missing, or mixed-up records. It adds 'preferred' lookups that pick the best available version, keeps the in-memory cache consistent with those lookups, and fixes several bugs where deleting or updating one version could leave the other version invisible or inconsistent. The changes are framed as architectural groundwork for the upcoming gossip v2 feature rather than as a fix for an active security vulnerability.
Treat this as a correctness and forward-compatibility patch rather than an emergency security fix. Operators running LND with the SQL backend should upgrade through normal release channels to benefit from the cross-version graph consistency fixes. Reviewers should pay attention to the new preferred-mapping upsert logic during node/channel deletion to ensure no orphaned mappings remain, and monitor for any regressions in pathfinding or RPC channel visibility.
Security signals we found
Cross-version data consistency: prevents a v2 channel shell from shadowing a usable v1 channel, which could affect routing decisions and RPC output.
Cache consistency: ensures the in-memory pathfinding cache and the SQL-backed RPC reads agree on which channel version and policies are active.
Bug fix: zombie edge info now carries the correct channel ID instead of 0, which could have caused incorrect channel identification during resurrection.
Bug fix: ForEachNodeCached no longer hardcodes GossipVersion1, so v2-only channels are visible to callers requesting v2.
Bug fix: buffered cache updates during startup use the population context, avoiding dropped cache entries when a caller cancels early.
No explicit security disclosure or CVE referenced in the commit or supplied materials.
Evidence from the diff
The patch makes the graph Store interface version-agnostic for ForEachNode, ForEachChannel, and related helpers. It introduces SQL preferred-mapping tables (graph_preferred_lookups) and UpsertPreferredChannel/UpsertPreferredNode routines so that each pubkey/SCID resolves to a single preferred record: a version with policies outranks a bare version, and higher gossip version wins ties. The in-memory GraphCache is updated to track the gossip version per channel entry and per node feature set, with ranking logic (preferIncomingEdge, preferredFeatures) matching the SQL mappings. Delete, zombie, prune, and policy-update paths now recompute preferred mappings and reload or replace cache entries to avoid cross-version inconsistency. Several bugs are fixed: KV/SQL zombie-edge construction now includes the correct chanID; ForEachNodeCached uses the requested version instead of hardcoding v1; cache updates buffered during startup replay with the population context rather than a possibly-canceled caller context.
Changed components
graph/db/Store interface and SQL/KV implementationsgraph/db/ChannelGraph and VersionedGraph wrappersgraph/db/GraphCache and graphCacheStategraph/db/sql_store.go SQL queries and transaction logicsqldb/sqlc migrations 000016/000017 and generated query codegraph/builder.go GetChannelByIDrpcserver.go and server.go (callers updated to use new APIs)Inspect captured patch +3752 / −287
### docs/release-notes/unreleased/11175.md
@@ -0,0 +1,9 @@
+# Technical and Architectural Updates
+
+## Database
+
+* Make the [graph `Store` interface
+ cross-version](https://github.com/lightningnetwork/lnd/pull/11175) so that
+ `ForEachNode`, `ForEachChannel`, and `ForEachNodeDirectedChannel` work across
+ gossip v1 and v2. Add `Preferred` fetch helpers so callers can retrieve
+ channels without knowing which gossip version announced them.
### graph/builder.go
@@ -1304,15 +1304,25 @@ func (b *Builder) SyncedHeight() uint32 {
return b.bestHeight.Load()
}
-// GetChannelByID return the channel by the channel id.
+// GetChannelByID return the v1 channel by the channel id. The callers, the v1
+// gossiper among them, build v1 channel updates from the returned policies, so
+// the lookup stays on v1 rather than returning the preferred version.
+//
+// TODO(gossip-v2): once lnd can build and sign v2 channel updates, take the
+// gossip version as an argument instead of hardcoding v1, and don't switch to
+// the preferred version: when a channel exists on both versions, each caller
+// needs a specific one. The gossiper should validate a received update against
+// the channel of the update's own version, refresh our own updates on every
+// version the channel is announced on, and fetchLastChanUpdate should use the
+// gossip version the peer speaks.
//
// NOTE: This method is part of the ChannelGraphSource interface.
func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
- return b.cfg.Graph.FetchChannelEdgesByID(
+ return b.v1Graph.FetchChannelEdgesByID(
context.TODO(), chanID.ToUint64(),
)
}
### graph/db/benchmark_test.go
@@ -347,8 +347,7 @@ func TestPopulateDBs(t *testing.T) {
// graph.
countNodes := func(graph *ChannelGraph) int {
numNodes := 0
- v1Graph := NewVersionedGraph(graph, lnwire.GossipVersion1)
- err := v1Graph.ForEachNode(
+ err := graph.ForEachNode(
ctx,
func(node *models.Node) error {
numNodes++
@@ -371,7 +370,7 @@ func TestPopulateDBs(t *testing.T) {
numPolicies = 0
)
err := graph.ForEachChannel(
- ctx, lnwire.GossipVersion1,
+ ctx,
func(info *models.ChannelEdgeInfo, policy,
policy2 *models.ChannelEdgePolicy) error {
@@ -499,7 +498,7 @@ func syncGraph(t *testing.T, src, dest *ChannelGraph) {
}
var wgChans sync.WaitGroup
- err = src.ForEachChannel(ctx, lnwire.GossipVersion1,
+ err = src.ForEachChannel(ctx,
func(info *models.ChannelEdgeInfo,
policy1, policy2 *models.ChannelEdgePolicy) error {
@@ -623,7 +622,7 @@ func BenchmarkGraphReadMethods(b *testing.B) {
name: "ForEachNode",
fn: func(b testing.TB, store Store) {
err := store.ForEachNode(
- ctx, lnwire.GossipVersion1,
+ ctx,
func(_ *models.Node) error {
// Increment the counter to
// ensure the callback is doing
@@ -639,12 +638,11 @@ func BenchmarkGraphReadMethods(b *testing.B) {
{
name: "ForEachChannel",
fn: func(b testing.TB, store Store) {
- //nolint:ll
- err := store.ForEachChannel(
- ctx, lnwire.GossipVersion1,
+ err := store.ForEachChannel(ctx,
func(_ *models.ChannelEdgeInfo,
+ _,
_ *models.ChannelEdgePolicy,
- _ *models.ChannelEdgePolicy) error {
+ ) error {
// Increment the counter to
// ensure the callback is doing
@@ -994,7 +992,7 @@ func BenchmarkFindOptimalSQLQueryConfig(b *testing.B) {
)
err := store.ForEachNode(
- ctx, lnwire.GossipVersion1,
+ ctx,
func(_ *models.Node) error {
numNodes++
@@ -1005,7 +1003,7 @@ func BenchmarkFindOptimalSQLQueryConfig(b *testing.B) {
//nolint:ll
err = store.ForEachChannel(
- ctx, lnwire.GossipVersion1,
+ ctx,
func(_ *models.ChannelEdgeInfo,
_,
_ *models.ChannelEdgePolicy) error {
### graph/db/graph.go
@@ -226,7 +226,7 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error {
loaded := false
defer func() {
- c.cache.finishPopulation(loaded)
+ c.cache.finishPopulation(ctx, loaded)
}()
cache := c.cache.graphCache
@@ -238,13 +238,15 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error {
for _, v := range []lnwire.GossipVersion{
gossipV1, gossipV2,
} {
- // TODO(elle): If we have both v1 and v2 entries for the same
- // node/channel, prefer v2 when merging.
+ // AddNodeFeatures keeps the features of each version apart
+ // and AddChannel ranks a colliding SCID itself, using the same
+ // rule as the preferred lookup tables, so the iteration order
+ // here does not decide which version's data is cached.
err := c.db.ForEachNodeCacheable(ctx, v,
func(node route.Vertex,
features *lnwire.FeatureVector) error {
- cache.AddNodeFeatures(node, features)
+ cache.AddNodeFeatures(node, v, features)
return nil
}, func() {},
@@ -299,12 +301,11 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(ctx context.Context,
return c.cache.graphCache.ForEachChannel(node, cb)
}
- // TODO(elle): once the no-cache path needs to support
- // pathfinding across gossip versions, this should iterate
- // across all versions rather than defaulting to v1.
- return c.db.ForEachNodeDirectedChannel(
- ctx, gossipV1, node, cb, reset,
- )
+ // The cache is disabled, still loading or failed to load. A graph
+ // session applies the same cross-version precedence as the cache.
+ return c.db.GraphSession(ctx, func(graph NodeTraverser) error {
+ return graph.ForEachNodeDirectedChannel(ctx, node, cb, reset)
+ }, reset)
}
// FetchNodeFeatures returns the features of the given node. If no features are
@@ -320,13 +321,30 @@ func (c *ChannelGraph) FetchNodeFeatures(ctx context.Context,
return c.cache.graphCache.GetFeatures(node), nil
}
- return c.db.FetchNodeFeatures(ctx, lnwire.GossipVersion1, node)
+ // The cache is disabled, still loading or failed to load. A graph
+ // session applies the same cross-version precedence as the cache.
+ var features *lnwire.FeatureVector
+ err := c.db.GraphSession(ctx, func(graph NodeTraverser) error {
+ var err error
+ features, err = graph.FetchNodeFeatures(ctx, node)
+
+ return err
+ }, func() {
+ features = nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return features, nil
}
// GraphSession will provide the call-back with access to a NodeTraverser
// instance which can be used to perform queries against the channel graph. If
-// the graph cache is not enabled, then the call-back will be provided with
-// access to the graph via a consistent read-only transaction.
+// the graph cache is not enabled, still loading or failed to load, then the
+// call-back will be provided with access to the graph via a consistent
+// read-only transaction, which applies the same cross-version precedence as
+// the cache.
func (c *ChannelGraph) GraphSession(ctx context.Context,
cb func(graph NodeTraverser) error, reset func()) error {
@@ -374,9 +392,11 @@ func (c *ChannelGraph) AddNode(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(_ context.Context,
+ cache *GraphCache) {
+
cache.AddNodeFeatures(
- node.PubKeyBytes, node.Features,
+ node.PubKeyBytes, node.Version, node.Features,
)
})
}
@@ -405,7 +425,9 @@ func (c *ChannelGraph) AddChannelEdge(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(_ context.Context,
+ cache *GraphCache) {
+
cache.AddChannel(models.NewCachedEdge(edge), nil, nil)
})
}
@@ -452,7 +474,9 @@ func (c *ChannelGraph) MarkEdgeLive(ctx context.Context,
policy2 = models.NewCachedPolicy(info.Policy2)
}
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(_ context.Context,
+ cache *GraphCache) {
+
cache.AddChannel(
models.NewCachedEdge(info.Info),
policy1, policy2,
@@ -483,11 +507,13 @@ func (c *ChannelGraph) DeleteChannelEdges(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
for _, info := range infos {
- cache.RemoveChannel(
+ c.removeCachedChannelVersion(
+ ctx, cache, v, info.ChannelID,
info.NodeKey1Bytes, info.NodeKey2Bytes,
- info.ChannelID,
)
}
})
@@ -512,7 +538,9 @@ func (c *ChannelGraph) DisconnectBlockAtHeight(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(_ context.Context,
+ cache *GraphCache) {
+
for _, edge := range edges {
cache.RemoveChannel(
edge.NodeKey1Bytes, edge.NodeKey2Bytes,
@@ -545,15 +573,17 @@ func (c *ChannelGraph) PruneGraph(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
for _, edge := range edges {
cache.RemoveChannel(
edge.NodeKey1Bytes, edge.NodeKey2Bytes,
edge.ChannelID,
)
}
for _, node := range nodes {
- cache.RemoveNode(node)
+ c.removeCachedNodeVersion(ctx, cache, node)
}
})
@@ -590,9 +620,11 @@ func (c *ChannelGraph) PruneGraphNodes(ctx context.Context) error {
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
for _, node := range nodes {
- cache.RemoveNode(node)
+ c.removeCachedNodeVersion(ctx, cache, node)
}
})
}
@@ -613,8 +645,12 @@ func (c *ChannelGraph) MarkEdgeZombie(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
- cache.RemoveChannel(pubKey1, pubKey2, chanID)
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
+ c.removeCachedChannelVersion(
+ ctx, cache, v, chanID, pubKey1, pubKey2,
+ )
})
}
@@ -637,9 +673,21 @@ func (c *ChannelGraph) UpdateEdgePolicy(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
- cache.UpdatePolicy(
- models.NewCachedPolicy(edge), from, to,
+ policy := models.NewCachedPolicy(edge)
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
+ if cache.UpdatePolicy(policy, from, to) {
+ return
+ }
+
+ // The channel is cached from another gossip version,
+ // which this policy may have displaced as the one the
+ // Store prefers. Reload the preferred channel, so that
+ // its edge and both of its policies replace the cached
+ // entry as a whole.
+ c.reloadCachedChannel(
+ ctx, cache, edge.ChannelID, from, to,
)
})
}
@@ -653,6 +701,112 @@ func (c *ChannelGraph) UpdateEdgePolicy(ctx context.Context,
return nil
}
+// reloadCachedChannel replaces the cached entry of a channel with the version
+// of it that the Store prefers. If that can't be loaded, the channel is
+// removed from the cache rather than left with a mix of versions.
+func (c *ChannelGraph) reloadCachedChannel(ctx context.Context,
+ cache *GraphCache, chanID uint64, node1, node2 route.Vertex) {
+
+ info, policy1, policy2, err := c.db.FetchChannelEdgesByIDPreferred(
+ ctx, chanID,
+ )
+ if err != nil {
+ if !errors.Is(err, ErrEdgeNotFound) &&
+ !errors.Is(err, ErrZombieEdge) {
+
+ log.Errorf("Unable to reload channel %v into the "+
+ "graph cache: %v", chanID, err)
+ }
+
+ cache.RemoveChannel(node1, node2, chanID)
+
+ return
+ }
+
+ cache.ReplaceChannel(
+ models.NewCachedEdge(info), newCachedPolicy(policy1),
+ newCachedPolicy(policy2),
+ )
+}
+
+// removeCachedChannelVersion updates the cached entry of a channel once its
+// gossip version v is deleted or marked as a zombie. An entry built from
+// another version doesn't depend on v, so it stays. Otherwise the channel's
+// other version takes its place if the Store has it live, and the channel is
+// removed from the cache if not.
+func (c *ChannelGraph) removeCachedChannelVersion(ctx context.Context,
+ cache *GraphCache, v lnwire.GossipVersion, chanID uint64,
+ node1, node2 route.Vertex) {
+
+ version, ok := cache.channelVersion(node1, chanID)
+ if !ok || version != v {
+ return
+ }
+
+ other := gossipV2
+ if v == gossipV2 {
+ other = gossipV1
+ }
+
+ info, policy1, policy2, err := c.db.FetchChannelEdgesByID(
+ ctx, other, chanID,
+ )
+ if err == nil {
+ isZombie, _, _, err := c.db.IsZombieEdge(ctx, other, chanID)
+ if err == nil && !isZombie {
+ cache.ReplaceChannel(
+ models.NewCachedEdge(info),
+ newCachedPolicy(policy1),
+ newCachedPolicy(policy2),
+ )
+
+ return
+ }
+ }
+
+ cache.RemoveChannel(node1, node2, chanID)
+}
+
+// removeCachedNodeVersion updates the cache once some gossip version of a
+// node is deleted. It drops the features of the versions that are gone, and
+// the node itself, with its channels, once no version of it is left. A node
+// version is only deleted once it has no channels, so the channels of a
+// version that remains stay.
+func (c *ChannelGraph) removeCachedNodeVersion(ctx context.Context,
+ cache *GraphCache, node route.Vertex) {
+
+ var remains bool
+ for _, v := range []lnwire.GossipVersion{gossipV1, gossipV2} {
+ if exists, err := c.db.HasNode(ctx, v, node); err == nil &&
+ exists {
+
+ remains = true
+
+ continue
+ }
+
+ // Clearing the features of a version reselects the node's
+ // features from the version that remains.
+ cache.AddNodeFeatures(node, v, nil)
+ }
+
+ if !remains {
+ cache.RemoveNode(node)
+ }
+}
+
+// newCachedPolicy returns the cached form of a policy, or nil if the policy
+// is unknown.
+func newCachedPolicy(
+ policy *models.ChannelEdgePolicy) *models.CachedEdgePolicy {
+
+ if policy == nil {
+ return nil
+ }
+
+ return models.NewCachedPolicy(policy)
+}
+
// ForEachNodeChannel iterates through all channels of the given node.
func (c *ChannelGraph) ForEachNodeChannel(ctx context.Context,
v lnwire.GossipVersion, nodePub route.Vertex,
@@ -679,13 +833,22 @@ func (c *ChannelGraph) HasV1Node(ctx context.Context,
return c.db.HasV1Node(ctx, nodePub)
}
-// ForEachChannel iterates through all channel edges stored within the graph.
+// ForEachChannel iterates through all channel edges stored within the graph
+// across all gossip versions.
func (c *ChannelGraph) ForEachChannel(ctx context.Context,
- v lnwire.GossipVersion, cb func(*models.ChannelEdgeInfo,
- *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error,
+ cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy) error,
reset func()) error {
- return c.db.ForEachChannel(ctx, v, cb, reset)
+ return c.db.ForEachChannel(ctx, cb, reset)
+}
+
+// ForEachNode iterates through all stored vertices/nodes in the graph across
+// all gossip versions.
+func (c *ChannelGraph) ForEachNode(ctx context.Context,
+ cb func(*models.Node) error, reset func()) error {
+
+ return c.db.ForEachNode(ctx, cb, reset)
}
// DisabledChannelIDs returns the channel ids of disabled channels.
@@ -817,26 +980,23 @@ func (c *ChannelGraph) FetchChanInfos(ctx context.Context,
}
// FetchChannelEdgesByOutpoint attempts to lookup directed edges by funding
-// outpoint.
+// outpoint, returning the highest available gossip version.
func (c *ChannelGraph) FetchChannelEdgesByOutpoint(ctx context.Context,
op *wire.OutPoint) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
- return c.db.FetchChannelEdgesByOutpoint(
- ctx, lnwire.GossipVersion1, op,
- )
+ return c.db.FetchChannelEdgesByOutpointPreferred(ctx, op)
}
-// FetchChannelEdgesByID attempts to lookup directed edges by channel ID.
+// FetchChannelEdgesByID attempts to lookup directed edges by channel ID,
+// returning the highest available gossip version.
func (c *ChannelGraph) FetchChannelEdgesByID(ctx context.Context,
chanID uint64) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {
- return c.db.FetchChannelEdgesByID(
- ctx, lnwire.GossipVersion1, chanID,
- )
+ return c.db.FetchChannelEdgesByIDPreferred(ctx, chanID)
}
// PutClosedScid stores a SCID for a closed channel in the database.
@@ -869,6 +1029,14 @@ func (c *ChannelGraph) PruneTip(ctx context.Context) (*chainhash.Hash,
// VersionedGraph is a wrapper around ChannelGraph that will call underlying
// Store methods with a specific gossip version.
+//
+// NOTE: ForEachNode and ForEachChannel are the exception. They are promoted
+// from the embedded ChannelGraph and iterate every gossip version, yielding
+// the preferred record per pub key or SCID. Both callers that reach them
+// through this wrapper want exactly that: autopilot ranks every candidate node
+// for channel opening, and the discovery bootstrapper dials every known
+// address, so neither has a reason to ignore a v2 announcement. A caller that
+// does need one version must use a version-taking Store method instead.
type VersionedGraph struct {
*ChannelGraph
v lnwire.GossipVersion
@@ -926,13 +1094,6 @@ func (c *VersionedGraph) ForEachNodeCached(ctx context.Context,
return c.ChannelGraph.ForEachNodeCached(ctx, c.v, cb, reset)
}
-// ForEachNode iterates through all stored vertices/nodes in the graph.
-func (c *VersionedGraph) ForEachNode(ctx context.Context,
- cb func(*models.Node) error, reset func()) error {
-
- return c.db.ForEachNode(ctx, c.v, cb, reset)
-}
-
// NumZombies returns the current number of zombie channels in the graph.
func (c *VersionedGraph) NumZombies(ctx context.Context) (uint64, error) {
return c.db.NumZombies(ctx, c.v)
@@ -964,24 +1125,6 @@ func (c *VersionedGraph) ChannelView(ctx context.Context) ([]EdgePoint,
return c.db.ChannelView(ctx, c.v)
}
-// GraphSession provides the callback with access to a NodeTraverser instance
-// for performing queries against the channel graph. If the graph cache is
-// enabled, the callback receives the VersionedGraph directly (which implements
-// NodeTraverser using the cache). Otherwise a read-only database session is
-// used.
-func (c *VersionedGraph) GraphSession(ctx context.Context,
- cb func(graph NodeTraverser) error, reset func()) error {
-
- if c.cache != nil && c.cache.isLoaded() {
- return cb(c)
- }
-
- // TODO(elle): the underlying GraphSession currently creates a
- // NodeTraverser that is hardcoded to GossipVersion1. This needs to be
- // updated to pass the version through for v2 support.
- return c.db.GraphSession(ctx, cb, reset)
-}
-
// FetchNode attempts to look up a target node by its identity public key.
func (c *VersionedGraph) FetchNode(ctx context.Context,
nodePub route.Vertex) (*models.Node, error) {
@@ -1033,8 +1176,34 @@ func (c *VersionedGraph) DeleteNode(ctx context.Context,
}
if c.cache != nil {
- c.cache.applyUpdate(func(cache *GraphCache) {
- cache.RemoveNode(nodePub)
+ c.cache.applyUpdate(ctx, func(ctx context.Context,
+ cache *GraphCache) {
+
+ // Deleting this version of the node also deleted its
+ // channels on this version, but the other version of
+ // a channel can survive, and the cache may hold a
+ // channel from either version. So rather than
+ // removing all of the node's cached channels, update
+ // each one: entries of the other version stay, and
+ // entries of this version are replaced by the other
+ // version if the Store still has it. The callback
+ // never fails, so ForEachChannel can't either.
+ _ = cache.ForEachChannel(nodePub,
+ func(channel *DirectedChannel) error {
+ c.removeCachedChannelVersion(
+ ctx, cache, c.v,
+ channel.ChannelID, nodePub,
+ channel.OtherNode,
+ )
+
+ return nil
+ },
+ )
+
+ // The node itself may still exist on the other
+ // version. Only drop the features of this version,
+ // and the node once no version of it is left.
+ c.removeCachedNodeVersion(ctx, cache, nodePub)
})
}
@@ -1106,14 +1275,6 @@ func (c *VersionedGraph) ForEachNodeChannel(ctx context.Context,
return c.db.ForEachNodeChannel(ctx, c.v, nodePub, cb, reset)
}
-// ForEachChannel iterates through all channel edges stored within the graph.
-func (c *VersionedGraph) ForEachChannel(ctx context.Context,
- cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
- *models.ChannelEdgePolicy) error, reset func()) error {
-
- return c.db.ForEachChannel(ctx, c.v, cb, reset)
-}
-
// ForEachNodeCacheable iterates through all stored vertices/nodes in the graph.
func (c *VersionedGraph) ForEachNodeCacheable(ctx context.Context,
cb func(route.Vertex, *lnwire.FeatureVector) error,
### graph/db/graph_cache.go
@@ -39,6 +39,20 @@ type DirectedChannel struct {
// Inbound fees of this node.
InboundFee lnwire.Fee
+
+ // version is the gossip version that announced the channel this entry
+ // was built from. The cache keys channels by SCID alone, so entries
+ // for the same channel on two gossip versions collide, and this field
+ // is what ranks them. Entries built for the no-cache read paths leave
+ // it unset, because only the cache ranks entries.
+ version lnwire.GossipVersion
+
+ // hasPolicies reports whether the version this entry was built from
+ // has a policy, which ranks the entry along with its version. It is
+ // tracked apart from OutPolicySet and InPolicy, since the cache leaves
+ // out the policies of a channel that are both disabled, while the
+ // preferred lookup tables still count them.
+ hasPolicies bool
}
// DeepCopy creates a deep copy of the channel, including the incoming policy.
@@ -60,11 +74,35 @@ func (c *DirectedChannel) DeepCopy() *DirectedChannel {
return &channelCopy
}
+// nodeFeatureSet holds the feature vector each gossip version announced for a
+// node, together with the vector the cache serves for it.
+type nodeFeatureSet struct {
+ v1, v2 *lnwire.FeatureVector
+
+ // preferred is the vector that preferredFeatures selects from v1 and
+ // v2. It is recomputed whenever either of them changes, so that the
+ // result doesn't depend on the order the announcements arrived in.
+ preferred *lnwire.FeatureVector
+}
+
+// preferredFeatures selects the features to advertise for a node that may be
+// announced on both gossip versions. The v2 features win, unless they are
+// empty and the node also has a v1 announcement, since an empty v2 vector
+// would otherwise hide the node's v1 features. A v2-only node keeps its empty
+// vector, so withdrawing its last feature bit clears its features.
+func preferredFeatures(v1, v2 *lnwire.FeatureVector) *lnwire.FeatureVector {
+ if v1 != nil && (v2 == nil || v2.IsEmpty()) {
+ return v1
+ }
+
+ return v2
+}
+
// GraphCache is a type that holds a minimal set of information of the public
// channel graph that can be used for pathfinding.
type GraphCache struct {
nodeChannels map[route.Vertex]map[uint64]*DirectedChannel
- nodeFeatures map[route.Vertex]*lnwire.FeatureVector
+ nodeFeatures map[route.Vertex]*nodeFeatureSet
mtx sync.RWMutex
}
@@ -80,8 +118,7 @@ func NewGraphCache(preAllocNumNodes int) *GraphCache {
preAllocNumNodes*2,
),
nodeFeatures: make(
- map[route.Vertex]*lnwire.FeatureVector,
- preAllocNumNodes,
+ map[route.Vertex]*nodeFeatureSet, preAllocNumNodes,
),
}
}
@@ -100,14 +137,48 @@ func (c *GraphCache) Stats() string {
numChannels)
}
-// AddNodeFeatures adds a graph node and its features to the cache.
+// AddNodeFeatures adds a graph node and the features it announced on the
+// given gossip version to the cache. The features the cache serves for the
+// node are reselected from both versions, see preferredFeatures.
func (c *GraphCache) AddNodeFeatures(node route.Vertex,
- features *lnwire.FeatureVector) {
+ v lnwire.GossipVersion, features *lnwire.FeatureVector) {
c.mtx.Lock()
defer c.mtx.Unlock()
- c.nodeFeatures[node] = features
+ set, ok := c.nodeFeatures[node]
+ if !ok {
+ set = &nodeFeatureSet{}
+ c.nodeFeatures[node] = set
+ }
+
+ if v == lnwire.GossipVersion2 {
+ set.v2 = features
+ } else {
+ set.v1 = features
+ }
+
+ set.preferred = preferredFeatures(set.v1, set.v2)
+}
+
+// ReplaceChannel caches the given channel in place of any entry for its SCID,
+// whichever gossip version that entry was built from. It is used when the
+// version that the Store prefers for a channel changes, so that the edge and
+// both policies of the new version replace the old ones as a whole, instead of
+// leaving a policy of the old version behind.
+func (c *GraphCache) ReplaceChannel(info *models.CachedEdgeInfo,
+ policy1, policy2 *models.CachedEdgePolicy) {
+
+ if info == nil {
+ return
+ }
+
+ c.mtx.Lock()
+ defer c.mtx.Unlock()
+
+ c.removeChannelIfFound(info.NodeKey1Bytes, info.ChannelID)
+ c.removeChannelIfFound(info.NodeKey2Bytes, info.ChannelID)
+ c.addChannel(info, policy1, policy2)
}
// AddChannel adds a non-directed channel, meaning that the order of policy 1
@@ -117,27 +188,41 @@ func (c *GraphCache) AddNodeFeatures(node route.Vertex,
func (c *GraphCache) AddChannel(info *models.CachedEdgeInfo,
policy1, policy2 *models.CachedEdgePolicy) {
+ c.mtx.Lock()
+ defer c.mtx.Unlock()
+
+ c.addChannel(info, policy1, policy2)
+}
+
+// addChannel implements AddChannel.
+//
+// NOTE: The write lock must be held when calling this function.
+func (c *GraphCache) addChannel(info *models.CachedEdgeInfo,
+ policy1, policy2 *models.CachedEdgePolicy) {
+
if info == nil {
return
}
// Create the edge entry for both nodes. We always add the channel
// structure to the cache, even if both policies are currently disabled,
// so that later policy updates can find and update the channel entry.
- c.mtx.Lock()
+ hasPolicies := policy1 != nil || policy2 != nil
+
c.updateOrAddEdge(info.NodeKey1Bytes, &DirectedChannel{
ChannelID: info.ChannelID,
IsNode1: true,
OtherNode: info.NodeKey2Bytes,
Capacity: info.Capacity,
- })
+ version: info.Version,
+ }, hasPolicies)
c.updateOrAddEdge(info.NodeKey2Bytes, &DirectedChannel{
ChannelID: info.ChannelID,
IsNode1: false,
OtherNode: info.NodeKey1Bytes,
Capacity: info.Capacity,
- })
- c.mtx.Unlock()
+ version: info.Version,
+ }, hasPolicies)
// Skip adding policies if both are disabled, as the channel is
// currently unusable for routing. However, we still add the channel
@@ -153,43 +238,89 @@ func (c *GraphCache) AddChannel(info *models.CachedEdgeInfo,
}
// The policy's node is always the to_node. So if policy 1 has to_node
- // of node 2 then we have the policy 1 as seen from node 1.
+ // of node 2 then we have the policy 1 as seen from node 1. If the
+ // entry of another gossip version was kept above, updatePolicy leaves
+ // it alone, so the policies of this version can't leak into it.
if policy1 != nil {
fromNode, toNode := info.NodeKey1Bytes, info.NodeKey2Bytes
if !policy1.IsNode1 {
fromNode, toNode = toNode, fromNode
}
- c.UpdatePolicy(policy1, fromNode, toNode)
+ c.updatePolicy(policy1, fromNode, toNode)
}
if policy2 != nil {
fromNode, toNode := info.NodeKey2Bytes, info.NodeKey1Bytes
if policy2.IsNode1 {
fromNode, toNode = toNode, fromNode
}
- c.UpdatePolicy(policy2, fromNode, toNode)
+ c.updatePolicy(policy2, fromNode, toNode)
}
}
// updateOrAddEdge makes sure the edge information for a node is either updated
-// if it already exists or is added to that node's list of channels.
-func (c *GraphCache) updateOrAddEdge(node route.Vertex, edge *DirectedChannel) {
+// if it already exists or is added to that node's list of channels. An entry
+// that a lower ranked gossip version would replace is kept, see
+// preferIncomingEdge.
+func (c *GraphCache) updateOrAddEdge(node route.Vertex, edge *DirectedChannel,
+ hasPolicies bool) {
+
if len(c.nodeChannels[node]) == 0 {
c.nodeChannels[node] = make(map[uint64]*DirectedChannel)
}
+ existing, ok := c.nodeChannels[node][edge.ChannelID]
+ if ok && !preferIncomingEdge(existing, edge, hasPolicies) {
+ return
+ }
+
+ edge.hasPolicies = hasPolicies
c.nodeChannels[node][edge.ChannelID] = edge
}
+// preferIncomingEdge reports whether an incoming edge outranks the cached
+// edge. An edge with policies wins, followed by the higher gossip version.
+func preferIncomingEdge(existing, incoming *DirectedChannel,
+ incomingHasPolicies bool) bool {
+
+ // A repeated announcement on the same version always wins, so that a
+ // channel whose policies were withdrawn also loses them here.
+ if existing.version == incoming.version {
+ return true
+ }
+
+ if existing.hasPolicies != incomingHasPolicies {
+ return incomingHasPolicies
+ }
+
+ return incoming.version > existing.version
+}
+
// UpdatePolicy updates a single policy on both the from and to node. The order
// of the from and to node is not strictly important. But we assume that a
// channel edge was added beforehand so that the directed channel struct already
// exists in the cache.
+//
+// The policy is only applied to an entry built from the policy's gossip
+// version. It returns false if the channel is cached from another version
+// that the policy's version now outranks, see preferIncomingEdge. The cached
+// entry is left untouched in that case, and the caller has to replace it with
+// the channel the Store now prefers, using ReplaceChannel.
func (c *GraphCache) UpdatePolicy(policy *models.CachedEdgePolicy, fromNode,
- toNode route.Vertex) {
+ toNode route.Vertex) bool {
c.mtx.Lock()
defer c.mtx.Unlock()
+ return c.updatePolicy(policy, fromNode, toNode)
+}
+
+// updatePolicy implements UpdatePolicy.
+//
+// NOTE: The write lock must be held when calling this function.
+func (c *GraphCache) updatePolicy(policy *models.CachedEdgePolicy, fromNode,
+ toNode route.Vertex) bool {
+
+ upToDate := true
updatePolicy := func(nodeKey route.Vertex) {
if len(c.nodeChannels[nodeKey]) == 0 {
log.Debugf("Node=%v not found in graph cache", nodeKey)
@@ -205,6 +336,21 @@ func (c *GraphCache) UpdatePolicy(policy *models.CachedEdgePolicy, fromNode,
return
}
+ // A policy of another gossip version must not be mixed into
+ // this entry. If its version doesn't outrank the cached one,
+ // the cached entry stays the preferred one and the policy is
+ // ignored here. Otherwise, the whole entry is stale.
+ if channel.version != policy.Version {
+ incoming := &DirectedChannel{version: policy.Version}
+ if preferIncomingEdge(channel, incoming, true) {
+ upToDate = false
+ }
+
+ return
+ }
+
+ channel.hasPolicies = true
+
// Edge 1 is defined as the policy for the direction of node1 to
// node2.
switch {
@@ -233,6 +379,8 @@ func (c *GraphCache) UpdatePolicy(policy *models.CachedEdgePolicy, fromNode,
updatePolicy(fromNode)
updatePolicy(toNode)
+
+ return upToDate
}
// RemoveNode completely removes a node and all its channels (including the
@@ -262,6 +410,23 @@ func (c *GraphCache) RemoveChannel(node1, node2 route.Vertex, chanID uint64) {
c.removeChannelIfFound(node2, chanID)
}
+// channelVersion returns the gossip version that the cached entry of a
+// channel was built from, as seen from the given node, and false if the node
+// has no cached entry for the channel.
+func (c *GraphCache) channelVersion(node route.Vertex,
+ chanID uint64) (lnwire.GossipVersion, bool) {
+
+ c.mtx.RLock()
+ defer c.mtx.RUnlock()
+
+ channel, ok := c.nodeChannels[node][chanID]
+ if !ok {
+ return 0, false
+ }
+
+ return channel.version, true
+}
+
// removeChannelIfFound removes a single channel from one side.
func (c *GraphCache) removeChannelIfFound(node route.Vertex, chanID uint64) {
if len(c.nodeChannels[node]) == 0 {
@@ -282,8 +447,10 @@ func (c *GraphCache) getChannels(node route.Vertex) []*DirectedChannel {
return nil
}
- features, ok := c.nodeFeatures[node]
- if !ok {
+ var features *lnwire.FeatureVector
+ if set, ok := c.nodeFeatures[node]; ok {
+ features = set.preferred
+ } else {
// If the features were set to nil explicitly, that's fine here.
// The router will overwrite the features of the destination
// node with those found in the invoice if necessary. But if we
@@ -368,12 +535,12 @@ func (c *GraphCache) GetFeatures(node route.Vertex) *lnwire.FeatureVector {
c.mtx.RLock()
defer c.mtx.RUnlock()
- features, ok := c.nodeFeatures[node]
- if !ok || features == nil {
+ set, ok := c.nodeFeatures[node]
+ if !ok || set.preferred == nil {
// The router expects the features to never be nil, so we return
// an empty feature set instead.
return lnwire.EmptyFeatureVector()
}
- return features
+ return set.preferred
}
### graph/db/graph_cache_state.go
@@ -1,6 +1,7 @@
package graphdb
import (
+ "context"
"sync"
"sync/atomic"
)
@@ -21,7 +22,7 @@ type graphCacheState struct {
updateMtx sync.Mutex
loading bool
- pendingUpdates []func(*GraphCache)
+ pendingUpdates []func(context.Context, *GraphCache)
}
// newGraphCacheState constructs a graph cache state with a new cache instance.
@@ -65,13 +66,15 @@ func (s *graphCacheState) beginPopulation() {
// finishPopulation replays any buffered mutations and marks the cache as ready
// when the initial population completed successfully. If population failed,
// buffered mutations are discarded since the cache won't be used for reads.
-func (s *graphCacheState) finishPopulation(loaded bool) {
+// The mutations are replayed with the population's context, since the
+// contexts of the calls that buffered them may be done by now.
+func (s *graphCacheState) finishPopulation(ctx context.Context, loaded bool) {
s.updateMtx.Lock()
defer s.updateMtx.Unlock()
if loaded {
for _, update := range s.pendingUpdates {
- update(s.graphCache)
+ update(ctx, s.graphCache)
}
s.loaded.Store(true)
@@ -83,9 +86,12 @@ func (s *graphCacheState) finishPopulation(loaded bool) {
s.loading = false
}
-// applyUpdate applies a cache mutation immediately or buffers it when the
-// cache is still being populated.
-func (s *graphCacheState) applyUpdate(update func(cache *GraphCache)) {
+// applyUpdate applies a cache mutation immediately, with the given context, or
+// buffers it when the cache is still being populated. A buffered mutation is
+// replayed with the population's context instead, see finishPopulation.
+func (s *graphCacheState) applyUpdate(ctx context.Context,
+ update func(ctx context.Context, cache *GraphCache)) {
+
s.updateMtx.Lock()
defer s.updateMtx.Unlock()
@@ -101,5 +107,5 @@ func (s *graphCacheState) applyUpdate(update func(cache *GraphCache)) {
return
}
- update(s.graphCache)
+ update(ctx, s.graphCache)
}
### graph/db/graph_cache_test.go
@@ -62,7 +62,10 @@ func TestGraphCacheAddNode(t *testing.T) {
},
}
cache := NewGraphCache(10)
- cache.AddNodeFeatures(nodeA, lnwire.EmptyFeatureVector())
+ cache.AddNodeFeatures(
+ nodeA, lnwire.GossipVersion1,
+ lnwire.EmptyFeatureVector(),
+ )
cache.AddChannel(&models.CachedEdgeInfo{
ChannelID: 1000,
// Those are direction independent!
@@ -254,3 +257,71 @@ func TestGraphCacheDisabledPoliciesRegression(t *testing.T) {
"incoming policy should be set after policy update")
require.Equal(t, uint16(40), foundChannels[0].InPolicy.TimeLockDelta)
}
+
+// TestGraphCacheVersionedPolicies asserts that AddChannel doesn't apply the
+// policies of a channel announcement to a cached entry of another gossip
+// version that it doesn't replace. This happens when MarkEdgeLive re-adds the
+// v1 version of a channel whose policed v2 version is already cached.
+func TestGraphCacheVersionedPolicies(t *testing.T) {
+ t.Parallel()
+
+ const chanID = 1000
+
+ newInfo := func(v lnwire.GossipVersion) *models.CachedEdgeInfo {
+ return &models.CachedEdgeInfo{
+ ChannelID: chanID,
+ NodeKey1Bytes: pubKey1,
+ NodeKey2Bytes: pubKey2,
+ Capacity: 500,
+ Version: v,
+ }
+ }
+ newPolicy := func(v lnwire.GossipVersion, isNode1 bool,
+ fee lnwire.MilliSatoshi) *models.CachedEdgePolicy {
+
+ return &models.CachedEdgePolicy{
+ ChannelID: chanID,
+ IsNode1: isNode1,
+ Version: v,
+ FeeBaseMSat: fee,
+ }
+ }
+ cachedChan := func(cache *GraphCache,
+ node route.Vertex) *DirectedChannel {
+
+ var found *DirectedChannel
+ err := cache.ForEachChannel(
+ node, func(c *DirectedChannel) error {
+ found = c
+
+ return nil
+ },
+ )
+ require.NoError(t, err)
+ require.NotNil(t, found)
+
+ return found
+ }
+
+ cache := NewGraphCache(10)
+ cache.AddChannel(
+ newInfo(lnwire.GossipVersion2),
+ newPolicy(lnwire.GossipVersion2, true, 200), nil,
+ )
+
+ // The v1 announcement loses against the policed v2 entry, so its
+ // policies must not be applied to that entry.
+ cache.AddChannel(
+ newInfo(lnwire.GossipVersion1),
+ newPolicy(lnwire.GossipVersion1, true, 100),
+ newPolicy(lnwire.GossipVersion1, false, 100),
+ )
+
+ node1Chan := cachedChan(cache, pubKey1)
+ require.Nil(t, node1Chan.InPolicy)
+
+ node2Chan := cachedChan(cache, pubKey2)
+ require.NotNil(t, node2Chan.InPolicy)
+ require.Equal(t, lnwire.GossipVersion2, node2Chan.InPolicy.Version)
+ require.EqualValues(t, 200, node2Chan.InPolicy.FeeBaseMSat)
+}
### graph/db/graph_test.go
@@ -1343,10 +1343,11 @@ func assertNodeInCache(t *testing.T, g *ChannelGraph, n *models.Node,
expectedFeatures *lnwire.FeatureVector) {
// Let's check the internal view first.
- nodeFeatures := g.cache.graphCache.nodeFeatures
- require.Equal(
- t, expectedFeatures, nodeFeatures[n.PubKeyBytes],
- )
+ var nodeFeatures *lnwire.FeatureVector
+ if set, ok := g.cache.graphCache.nodeFeatures[n.PubKeyBytes]; ok {
+ nodeFeatures = set.preferred
+ }
+ require.Equal(t, expectedFeatures, nodeFeatures)
// The external view should reflect this as well. Except when we expect
// the features to be nil internally, we return an empty feature vector
@@ -1384,6 +1385,7 @@ func assertEdgeWithNoPoliciesInCache(t *testing.T, g *ChannelGraph,
Capacity: e.Capacity,
OutPolicySet: false,
InPolicy: nil,
+ version: e.Version,
}
nodeChannels := g.cache.graphCache.nodeChannels
require.Contains(
@@ -1401,6 +1403,7 @@ func assertEdgeWithNoPoliciesInCache(t *testing.T, g *ChannelGraph,
Capacity: e.Capacity,
OutPolicySet: false,
InPolicy: nil,
+ version: e.Version,
}
require.Contains(
t, nodeChannels[e.NodeKey2Bytes], e.ChannelID,
@@ -1806,7 +1809,7 @@ func TestGraphTraversal(t *testing.T) {
// Iterate through all the known channels within the graph DB, once
// again if the map is empty that indicates that all edges have
// properly been reached.
- err = graph.ForEachChannel(ctx, lnwire.GossipVersion1,
+ err = graph.ForEachChannel(ctx,
func(ei *models.ChannelEdgeInfo,
_ *models.ChannelEdgePolicy,
_ *models.ChannelEdgePolicy) error {
@@ -2160,7 +2163,7 @@ func assertPruneTip(t *testing.T, graph *ChannelGraph,
func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
numChans := 0
err := graph.ForEachChannel(
- t.Context(), lnwire.GossipVersion1,
+ t.Context(),
func(*models.ChannelEdgeInfo,
*models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error {
@@ -2177,8 +2180,7 @@ func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
func assertNumNodes(t *testing.T, graph *ChannelGraph, n int) {
numNodes := 0
- v1Graph := NewVersionedGraph(graph, lnwire.GossipVersion1)
- err := v1Graph.ForEachNode(t.Context(), func(_ *models.Node) error {
+ err := graph.ForEachNode(t.Context(), func(_ *models.Node) error {
numNodes++
return nil
@@ -4142,12 +4144,11 @@ func TestStressTestChannelGraphAPI(t *testing.T) {
executionCount = 100
)
- for i := 0; i < concurrencyLevel; i++ {
-
+ for i := range concurrencyLevel {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()
- for j := 0; j < executionCount; j++ {
+ for range executionCount {
// Randomly select a method to execute.
methodIndex := rand.Intn(len(methods))
@@ -4230,7 +4231,7 @@ func TestFilterChannelRange(t *testing.T) {
return updateTime
}
- for i := 0; i < numChans/2; i++ {
+ for i := range numChans / 2 {
chanHeight := endHeight
channel1, chanID1 := createEdge(
lnwire.GossipVersion1, chanHeight, uint32(i+1), 0,
@@ -4347,7 +4348,6 @@ func TestFilterChannelRange(t *testing.T) {
}
for _, test := range tests {
-
t.Run(test.name, func(t *testing.T) {
t.Parallel()
@@ -4433,7 +4433,7 @@ func testFetchChanInfos(t *testing.T, v lnwire.GossipVersion) {
endTime := startTime
edges := make([]ChannelEdge, 0, numChans)
edgeQuery := make([]uint64, 0, numChans)
- for i := 0; i < numChans; i++ {
+ for i := range numChans {
channel, chanID := createEdge(
v, uint32(i*10), 0, 0, 0, node1, node2,
)
@@ -5695,6 +5695,7 @@ func testFetchZombieEdgeVersioning(t *testing.T, v lnwire.GossipVersion) {
require.ErrorIs(t, err, ErrZombieEdge)
require.NotNil(t, info)
require.Equal(t, v, info.Version)
+ require.Equal(t, edge.ChannelID, info.ChannelID)
require.Equal(t, edge.NodeKey1Bytes, info.NodeKey1Bytes)
require.Equal(t, edge.NodeKey2Bytes, info.NodeKey2Bytes)
}
@@ -5892,7 +5893,7 @@ func testBatchedAddChannelEdge(t *testing.T, v lnwire.GossipVersion) {
}
wg.Wait()
- for i := 0; i < len(edges); i++ {
+ for range edges {
err := <-errChan
require.Nil(t, err)
}
@@ -6960,3 +6961,1776 @@ func TestUpdateRangeValidateForVersion(t *testing.T) {
})
}
}
+
+// TestPreferredChannelFetch tests the two new Store methods:
+// FetchChannelEdgesByIDPreferred and FetchChannelEdgesByOutpointPreferred.
+func TestPreferredChannelFetch(t *testing.T) {
+ t.Parallel()
+ ctx := t.Context()
+
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ node1Priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ node2Priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ node1V1 := createNode(t, lnwire.GossipVersion1, node1Priv)
+ node2V1 := createNode(t, lnwire.GossipVersion1, node2Priv)
+
+ require.NoError(t, graph.AddNode(ctx, node1V1))
+ require.NoError(t, graph.AddNode(ctx, node2V1))
+
+ // Create and add a v1 channel edge.
+ edgeInfo, scid := createEdge(
+ lnwire.GossipVersion1, 100, 1, 0, 1, node1V1, node2V1,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, edgeInfo))
+
+ chanID := scid.ToUint64()
+ op := edgeInfo.ChannelPoint
+
+ // FetchChannelEdgesByIDPreferred should return the v1 channel.
+ info, _, _, err := store.FetchChannelEdgesByIDPreferred(
+ ctx, chanID,
+ )
+ require.NoError(t, err)
+ require.Equal(t, chanID, info.ChannelID)
+
+ // FetchChannelEdgesByOutpointPreferred should also return it.
+ info, _, _, err = store.FetchChannelEdgesByOutpointPreferred(
+ ctx, &op,
+ )
+ require.NoError(t, err)
+ require.Equal(t, chanID, info.ChannelID)
+
+ // Querying a non-existent channel should return ErrEdgeNotFound for
+ // both the SCID and outpoint lookups.
+ _, _, _, err = store.FetchChannelEdgesByIDPreferred(ctx, 999999)
+ require.ErrorIs(t, err, ErrEdgeNotFound)
+
+ unknownOutpoint := wire.OutPoint{
+ Hash: chainhash.Hash{1},
+ Index: 99,
+ }
+ _, _, _, err = store.FetchChannelEdgesByOutpointPreferred(
+ ctx, &unknownOutpoint,
+ )
+ require.ErrorIs(t, err, ErrEdgeNotFound)
+
+ zombieChanID := uint64(888888)
+ err = store.MarkEdgeZombie(
+ ctx, lnwire.GossipVersion1, zombieChanID,
+ node1V1.PubKeyBytes, node2V1.PubKeyBytes,
+ )
+ require.NoError(t, err)
+
+ info, _, _, err = store.FetchChannelEdgesByIDPreferred(
+ ctx, zombieChanID,
+ )
+ require.ErrorIs(t, err, ErrZombieEdge)
+ require.NotNil(t, info)
+ require.Equal(t, route.Vertex(node1V1.PubKeyBytes), info.NodeKey1Bytes)
+ require.Equal(t, route.Vertex(node2V1.PubKeyBytes), info.NodeKey2Bytes)
+
+ if !isSQLDB {
+ return
+ }
+
+ node1V2 := createNode(t, lnwire.GossipVersion2, node1Priv)
+ node2V2 := createNode(t, lnwire.GossipVersion2, node2Priv)
+ require.NoError(t, graph.AddNode(ctx, node1V2))
+ require.NoError(t, graph.AddNode(ctx, node2V2))
+
+ // Add a duplicate v1/v2 channel and verify preferred lookup chooses
+ // the v2 edge for both the SCID and outpoint lookups.
+ dupV1, dupSCID := createEdge(
+ lnwire.GossipVersion1, 101, 1, 0, 2, node1V1, node2V1,
+ )
+ dupV2, _ := createEdge(
+ lnwire.GossipVersion2, 101, 1, 0, 2, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, dupV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, dupV2))
+
+ dupChanID := dupSCID.ToUint64()
+ dupOutpoint := dupV1.ChannelPoint
+
+ info, _, _, err = store.FetchChannelEdgesByIDPreferred(
+ ctx, dupChanID,
+ )
+ require.NoError(t, err)
+ require.Equal(t, dupChanID, info.ChannelID)
+ require.Equal(t, lnwire.GossipVersion2, info.Version)
+
+ info, _, _, err = store.FetchChannelEdgesByOutpointPreferred(
+ ctx, &dupOutpoint,
+ )
+ require.NoError(t, err)
+ require.Equal(t, dupChanID, info.ChannelID)
+ require.Equal(t, lnwire.GossipVersion2, info.Version)
+
+ // Add another duplicate v1/v2 channel where only the v1 version has a
+ // policy. Preferred lookup should return the lower version with usable
+ // policy data instead of the higher version shell.
+ policyPrefV1, policyPrefSCID := createEdge(
+ lnwire.GossipVersion1, 102, 1, 0, 3, node1V1, node2V1,
+ )
+ policyPrefV2, _ := createEdge(
+ lnwire.GossipVersion2, 102, 1, 0, 3, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, policyPrefV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, policyPrefV2))
+
+ policyOnlyV1 := newEdgePolicy(
+ lnwire.GossipVersion1, policyPrefV1.ChannelID, 1000, true,
+ )
+ policyOnlyV1.ToNode = node2V1.PubKeyBytes
+ policyOnlyV1.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policyOnlyV1))
+
+ policyPrefChanID := policyPrefSCID.ToUint64()
+ policyPrefOutpoint := policyPrefV1.ChannelPoint
+
+ info, p1, p2, err := store.FetchChannelEdgesByIDPreferred(
+ ctx, policyPrefChanID,
+ )
+ require.NoError(t, err)
+ require.Equal(t, policyPrefChanID, info.ChannelID)
+ require.Equal(t, lnwire.GossipVersion1, info.Version)
+ require.NotNil(t, p1)
+ require.Nil(t, p2)
+
+ info, p1, p2, err = store.FetchChannelEdgesByOutpointPreferred(
+ ctx, &policyPrefOutpoint,
+ )
+ require.NoError(t, err)
+ require.Equal(t, policyPrefChanID, info.ChannelID)
+ require.Equal(t, lnwire.GossipVersion1, info.Version)
+ require.NotNil(t, p1)
+ require.Nil(t, p2)
+}
+
+// TestDeleteNodePreferredRecomputation verifies that deleting one gossip
+// version of a dual-version node correctly recomputes the preferred-node
+// mapping so the surviving version remains visible via ForEachNode.
+func TestDeleteNodePreferredRecomputation(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ // Create a node with both v1 and v2 announcements.
+ priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ nodeV1 := createNode(t, lnwire.GossipVersion1, priv)
+ nodeV2 := createNode(t, lnwire.GossipVersion2, priv)
+
+ require.NoError(t, graph.AddNode(ctx, nodeV1))
+ require.NoError(t, graph.AddNode(ctx, nodeV2))
+
+ // Attach a channel that exists on both versions. Deleting the v2 node
+ // cascades to the v2 channel, and with it to the preferred-channel
+ // mapping, so this is what catches a mapping that is not recomputed.
+ peerPriv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ peerV1 := createNode(t, lnwire.GossipVersion1, peerPriv)
+ peerV2 := createNode(t, lnwire.GossipVersion2, peerPriv)
+ require.NoError(t, graph.AddNode(ctx, peerV1))
+ require.NoError(t, graph.AddNode(ctx, peerV2))
+
+ chanV1, scid := createEdge(
+ lnwire.GossipVersion1, 300, 0, 0, 1, nodeV1, peerV1,
+ )
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 300, 0, 0, 1, nodeV2, peerV2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ // channelVersion reports the version ForEachChannel returns for the
+ // shared channel, or zero if the channel is not returned at all.
+ channelVersion := func() lnwire.GossipVersion {
+ var version lnwire.GossipVersion
+ err := store.ForEachChannel(ctx, func(
+ info *models.ChannelEdgeInfo, _,
+ _ *models.ChannelEdgePolicy) error {
+
+ if info.ChannelID == scid.ToUint64() {
+ version = info.Version
+ }
+
+ return nil
+ }, func() { version = 0 })
+ require.NoError(t, err)
+
+ return version
+ }
+ require.Equal(t, lnwire.GossipVersion2, channelVersion())
+
+ // ForEachNode should return the node (v2 preferred).
+ var count int
+ err = store.ForEachNode(ctx, func(n *models.Node) error {
+ if n.PubKeyBytes == nodeV1.PubKeyBytes {
+ require.Equal(t, lnwire.GossipVersion2, n.Version)
+ count++
+ }
+
+ return nil
+ }, func() { count = 0 })
+ require.NoError(t, err)
+ require.Equal(t, 1, count, "node should be visible before delete")
+
+ // Delete the v2 version.
+ require.NoError(t, store.DeleteNode(
+ ctx, lnwire.GossipVersion2, nodeV1.PubKeyBytes,
+ ))
+
+ // The node should still be visible via ForEachNode, now as v1.
+ count = 0
+ err = store.ForEachNode(ctx, func(n *models.Node) error {
+ if n.PubKeyBytes == nodeV1.PubKeyBytes {
+ require.Equal(t, lnwire.GossipVersion1, n.Version)
+ count++
+ }
+
+ return nil
+ }, func() { count = 0 })
+ require.NoError(t, err)
+ require.Equal(t, 1, count,
+ "node should still be visible after deleting one version")
+
+ // The v1 channel survives the cascade, so both the cross-version
+ // iteration and the preferred lookup must now return it.
+ require.Equal(t, lnwire.GossipVersion1, channelVersion(),
+ "v1 channel should still be visible after deleting the v2 node")
+
+ info, _, _, err := store.FetchChannelEdgesByIDPreferred(
+ ctx, scid.ToUint64(),
+ )
+ require.NoError(t, err)
+ require.Equal(t, lnwire.GossipVersion1, info.Version)
+
+ // Delete the remaining v1 version.
+ require.NoError(t, store.DeleteNode(
+ ctx, lnwire.GossipVersion1, nodeV1.PubKeyBytes,
+ ))
+
+ // The node should now be gone.
+ count = 0
+ err = store.ForEachNode(ctx, func(n *models.Node) error {
+ if n.PubKeyBytes == nodeV1.PubKeyBytes {
+ count++
+ }
+
+ return nil
+ }, func() { count = 0 })
+ require.NoError(t, err)
+ require.Equal(t, 0, count,
+ "node should be gone after deleting all versions")
+}
+
+// TestNoCacheNodeTraversal verifies that the no-cache fallback paths of
+// ChannelGraph.FetchNodeFeatures and ChannelGraph.ForEachNodeDirectedChannel
+// return v1 data correctly. The no-cache path is only ever reached on the KV
+// backend in production (which is v1-only), so cross-version semantics are
+// the cache path's job, not this one's.
+func TestNoCacheNodeTraversal(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("test only meaningful for SQL backend")
+ }
+
+ ctx := t.Context()
+
+ // Disable the cache so we exercise the no-cache code paths.
+ graph := MakeTestGraph(t, WithUseGraphCache(false))
+
+ priv1, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ priv2, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ node1 := createNode(t, lnwire.GossipVersion1, priv1)
+ node2 := createNode(t, lnwire.GossipVersion1, priv2)
+ require.NoError(t, graph.AddNode(ctx, node1))
+ require.NoError(t, graph.AddNode(ctx, node2))
+
+ // FetchNodeFeatures should return the node's v1 features.
+ features, err := graph.FetchNodeFeatures(ctx, node1.PubKeyBytes)
+ require.NoError(t, err)
+ require.False(t, features.IsEmpty(),
+ "v1 node should have features")
+
+ // Add a v1 channel and verify ForEachNodeDirectedChannel sees it.
+ edge, _ := createEdge(
+ lnwire.GossipVersion1, 100, 0, 0, 0, node1, node2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, edge))
+
+ pol := newEdgePolicy(
+ lnwire.GossipVersion1, edge.ChannelID, 1000, true,
+ )
+ pol.ToNode = node2.PubKeyBytes
+ pol.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, pol))
+
+ var foundChannels int
+ err = graph.ForEachNodeDirectedChannel(
+ ctx, node1.PubKeyBytes,
+ func(_ *DirectedChannel) error {
+ foundChannels++
+
+ return nil
+ }, func() {
+ foundChannels = 0
+ },
+ )
+ require.NoError(t, err)
+ require.Equal(t, 1, foundChannels,
+ "expected 1 channel for v1 node")
+}
+
+// TestPreferredForEachNode verifies that SQLStore.ForEachNode returns one
+// node per pubkey, preferring the highest announced version and otherwise
+// falling back to the highest-version shell node.
+func TestPreferredForEachNode(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ v1Only := createTestVertex(t, lnwire.GossipVersion1)
+ v1Only.Alias = fn.Some("v1-only")
+ require.NoError(t, graph.AddNode(ctx, v1Only))
+
+ bothPriv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ bothV1 := createNode(t, lnwire.GossipVersion1, bothPriv)
+ bothV1.Alias = fn.Some("both-v1")
+ require.NoError(t, graph.AddNode(ctx, bothV1))
+
+ bothV2 := createNode(t, lnwire.GossipVersion2, bothPriv)
+ bothV2.Alias = fn.Some("both-v2")
+ require.NoError(t, graph.AddNode(ctx, bothV2))
+
+ shellPriv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ shellPub, err := route.NewVertexFromBytes(
+ shellPriv.PubKey().SerializeCompressed(),
+ )
+ require.NoError(t, err)
+
+ require.NoError(t, graph.AddNode(
+ ctx, models.NewShellNode(lnwire.GossipVersion1, shellPub),
+ ))
+ require.NoError(t, graph.AddNode(
+ ctx, models.NewShellNode(lnwire.GossipVersion2, shellPub),
+ ))
+
+ var nodeCount int
+ nodesByPub := make(map[route.Vertex]*models.Node)
+ err = store.ForEachNode(ctx, func(node *models.Node) error {
+ nodesByPub[node.PubKeyBytes] = node
+ nodeCount++
+
+ return nil
+ }, func() {
+ clear(nodesByPub)
+ nodeCount = 0
+ })
+ require.NoError(t, err)
+ require.Len(t, nodesByPub, 3)
+ require.Equal(t, 3, nodeCount, "unexpected duplicate nodes")
+
+ gotV1Only := nodesByPub[v1Only.PubKeyBytes]
+ require.NotNil(t, gotV1Only)
+ require.Equal(t, lnwire.GossipVersion1, gotV1Only.Version)
+ require.Equal(t, "v1-only", gotV1Only.Alias.UnwrapOr(""))
+ require.True(t, gotV1Only.HaveAnnouncement())
+
+ gotBoth := nodesByPub[bothV1.PubKeyBytes]
+ require.NotNil(t, gotBoth)
+ require.Equal(t, lnwire.GossipVersion2, gotBoth.Version)
+ require.Equal(t, "both-v2", gotBoth.Alias.UnwrapOr(""))
+ require.True(t, gotBoth.HaveAnnouncement())
+
+ gotShell := nodesByPub[shellPub]
+ require.NotNil(t, gotShell)
+ require.Equal(t, lnwire.GossipVersion2, gotShell.Version)
+ require.False(t, gotShell.HaveAnnouncement())
+}
+
+// addDualVersionNodes creates two nodes that are each announced on both gossip
+// versions, adds all four node records to the graph, and returns them. Tests
+// use it to put channels of either version between the same pair of nodes.
+func addDualVersionNodes(t *testing.T, graph *ChannelGraph) (*models.Node,
+ *models.Node, *models.Node, *models.Node) {
+
+ t.Helper()
+
+ node1Priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ node2Priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ node1V1 := createNode(t, lnwire.GossipVersion1, node1Priv)
+ node1V2 := createNode(t, lnwire.GossipVersion2, node1Priv)
+ node2V1 := createNode(t, lnwire.GossipVersion1, node2Priv)
+ node2V2 := createNode(t, lnwire.GossipVersion2, node2Priv)
+
+ for _, n := range []*models.Node{node1V1, node1V2, node2V1, node2V2} {
+ require.NoError(t, graph.AddNode(t.Context(), n))
+ }
+
+ return node1V1, node1V2, node2V1, node2V2
+}
+
+// TestPreferredForEachChannel verifies that SQLStore.ForEachChannel returns
+// one channel per SCID, preferring a higher-version channel when both versions
+// have policies, preserving lower-version policy data when the higher version
+// has none, and otherwise falling back to the highest-version no-policy
+// channel.
+func TestPreferredForEachChannel(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ v1Only, _ := createEdge(
+ lnwire.GossipVersion1, 200, 0, 0, 1, node1V1, node2V1,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, v1Only))
+
+ policyPrefV1, _ := createEdge(
+ lnwire.GossipVersion1, 201, 0, 0, 2, node1V1, node2V1,
+ )
+ policyPrefV2, _ := createEdge(
+ lnwire.GossipVersion2, 201, 0, 0, 2, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, policyPrefV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, policyPrefV2))
+
+ policyOnlyV1 := newEdgePolicy(
+ lnwire.GossipVersion1, policyPrefV1.ChannelID, 1000, true,
+ )
+ policyOnlyV1.ToNode = node2V1.PubKeyBytes
+ policyOnlyV1.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policyOnlyV1))
+
+ versionPrefV1, _ := createEdge(
+ lnwire.GossipVersion1, 202, 0, 0, 3, node1V1, node2V1,
+ )
+ versionPrefV2, _ := createEdge(
+ lnwire.GossipVersion2, 202, 0, 0, 3, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, versionPrefV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, versionPrefV2))
+
+ versionPolicyV1 := newEdgePolicy(
+ lnwire.GossipVersion1, versionPrefV1.ChannelID, 1001, true,
+ )
+ versionPolicyV1.ToNode = node2V1.PubKeyBytes
+ versionPolicyV1.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, versionPolicyV1))
+
+ versionPolicyV2 := newEdgePolicy(
+ lnwire.GossipVersion2, versionPrefV2.ChannelID, 1002, true,
+ )
+ versionPolicyV2.ToNode = node2V2.PubKeyBytes
+ versionPolicyV2.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, versionPolicyV2))
+
+ shellPrefV1, _ := createEdge(
+ lnwire.GossipVersion1, 203, 0, 0, 4, node1V1, node2V1,
+ )
+ shellPrefV2, _ := createEdge(
+ lnwire.GossipVersion2, 203, 0, 0, 4, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, shellPrefV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, shellPrefV2))
+
+ type channelResult struct {
+ info *models.ChannelEdgeInfo
+ p1 *models.ChannelEdgePolicy
+ p2 *models.ChannelEdgePolicy
+ }
+ var chanCount int
+ channelsByID := make(map[uint64]channelResult)
+ err := store.ForEachChannel(ctx, func(info *models.ChannelEdgeInfo,
+ p1, p2 *models.ChannelEdgePolicy) error {
+
+ channelsByID[info.ChannelID] = channelResult{
+ info: info,
+ p1: p1,
+ p2: p2,
+ }
+ chanCount++
+
+ return nil
+ }, func() {
+ clear(channelsByID)
+ chanCount = 0
+ })
+ require.NoError(t, err)
+ require.Len(t, channelsByID, 4)
+ require.Equal(t, 4, chanCount, "unexpected duplicate channels")
+
+ gotV1Only := channelsByID[v1Only.ChannelID]
+ require.Equal(t, lnwire.GossipVersion1, gotV1Only.info.Version)
+ require.Nil(t, gotV1Only.p1)
+ require.Nil(t, gotV1Only.p2)
+
+ // Assert the policy content too, not just its presence, so that a
+ // policy taken from the wrong version or attached to the wrong
+ // direction fails the test.
+ gotPolicyPref := channelsByID[policyPrefV1.ChannelID]
+ require.Equal(t, lnwire.GossipVersion1, gotPolicyPref.info.Version)
+ require.NotNil(t, gotPolicyPref.p1)
+ require.Equal(t, lnwire.GossipVersion1, gotPolicyPref.p1.Version)
+ require.Equal(
+ t, policyOnlyV1.FeeBaseMSat, gotPolicyPref.p1.FeeBaseMSat,
+ )
+ require.Nil(t, gotPolicyPref.p2)
+
+ gotVersionPref := channelsByID[versionPrefV1.ChannelID]
+ require.Equal(t, lnwire.GossipVersion2, gotVersionPref.info.Version)
+ require.NotNil(t, gotVersionPref.p1)
+ require.Equal(t, lnwire.GossipVersion2, gotVersionPref.p1.Version)
+ require.Equal(
+ t, versionPolicyV2.FeeBaseMSat, gotVersionPref.p1.FeeBaseMSat,
+ )
+ require.Nil(t, gotVersionPref.p2)
+
+ gotShellPref := channelsByID[shellPrefV1.ChannelID]
+ require.Equal(t, lnwire.GossipVersion2, gotShellPref.info.Version)
+ require.Nil(t, gotShellPref.p1)
+ require.Nil(t, gotShellPref.p2)
+}
+
+// cachedChannel returns the cached directed channel that the given node holds
+// for the given SCID, or nil if the cache holds no such channel.
+func cachedChannel(t *testing.T, g *ChannelGraph, node route.Vertex,
+ scid uint64) *DirectedChannel {
+
+ t.Helper()
+
+ var found *DirectedChannel
+ err := g.ForEachNodeDirectedChannel(
+ t.Context(), node, func(c *DirectedChannel) error {
+ if c.ChannelID == scid {
+ found = c
+ }
+
+ return nil
+ }, func() {},
+ )
+ require.NoError(t, err)
+
+ return found
+}
+
+// TestCachePrefersPolicedChannelVersion asserts that the graph cache resolves a
+// channel announced on both gossip versions the same way the preferred lookup
+// tables do: the version that carries policies outranks the bare one. If the
+// two views disagreed, the channel would be routable in RPC output and
+// unroutable for pathfinding.
+func TestCachePrefersPolicedChannelVersion(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ // The channel is announced on v1 with a policy from node 1, and then a
+ // second time on v2 with no policy at all.
+ policedV1, _ := createEdge(
+ lnwire.GossipVersion1, 300, 0, 0, 1, node1V1, node2V1,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, policedV1))
+
+ policy := newEdgePolicy(
+ lnwire.GossipVersion1, policedV1.ChannelID, 1000, true,
+ )
+ policy.ToNode = node2V1.PubKeyBytes
+ policy.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policy))
+
+ bareV2, _ := createEdge(
+ lnwire.GossipVersion2, 300, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, bareV2))
+
+ // The preferred lookup keeps the policed v1 announcement.
+ var (
+ gotInfo *models.ChannelEdgeInfo
+ gotPolicy *models.ChannelEdgePolicy
+ )
+ err := store.ForEachChannel(ctx, func(info *models.ChannelEdgeInfo,
+ p1, _ *models.ChannelEdgePolicy) error {
+
+ if info.ChannelID != policedV1.ChannelID {
+ return nil
+ }
+
+ gotInfo, gotPolicy = info, p1
+
+ return nil
+ }, func() {
+ gotInfo, gotPolicy = nil, nil
+ })
+ require.NoError(t, err)
+ require.NotNil(t, gotInfo)
+ require.Equal(t, lnwire.GossipVersion1, gotInfo.Version)
+ require.NotNil(t, gotPolicy)
+
+ // The cache must reach the same verdict, both for the writes applied
+ // while it was live and for a fresh load of the same store.
+ assertPolicyCached := func(g *ChannelGraph) {
+ t.Helper()
+
+ node1Chan := cachedChannel(
+ t, g, node1V1.PubKeyBytes, policedV1.ChannelID,
+ )
+ require.NotNil(t, node1Chan)
+ require.True(
+ t, node1Chan.OutPolicySet, "node 1 lost its outgoing "+
+ "policy to the bare v2 announcement",
+ )
+
+ node2Chan := cachedChannel(
+ t, g, node2V1.PubKeyBytes, policedV1.ChannelID,
+ )
+ require.NotNil(t, node2Chan)
+ require.NotNil(
+ t, node2Chan.InPolicy, "node 2 lost its incoming "+
+ "policy to the bare v2 announcement",
+ )
+ }
+
+ assertPolicyCached(graph)
+
+ reloaded, err := NewChannelGraph(store, WithSyncGraphCachePopulation())
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ assertPolicyCached(reloaded)
+}
+
+// TestDeleteChannelPreferredRecomputation asserts that deleting one gossip
+// version of a channel leaves the other version reachable through the
+// cross-version iteration. The cascade drops the preferred mapping row along
+// with the deleted version, so the delete path has to re-insert it.
+func TestDeleteChannelPreferredRecomputation(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ chanV1, _ := createEdge(
+ lnwire.GossipVersion1, 310, 0, 0, 1, node1V1, node2V1,
+ )
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 310, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ // fetchVersions returns the gossip version of every channel that the
+ // cross-version iteration yields for our SCID.
+ fetchVersions := func() []lnwire.GossipVersion {
+ var versions []lnwire.GossipVersion
+ err := store.ForEachChannel(
+ ctx, func(info *models.ChannelEdgeInfo, _,
+ _ *models.ChannelEdgePolicy) error {
+
+ if info.ChannelID == chanV1.ChannelID {
+ versions = append(
+ versions, info.Version,
+ )
+ }
+
+ return nil
+ }, func() {
+ versions = nil
+ },
+ )
+ require.NoError(t, err)
+
+ return versions
+ }
+
+ // Both versions exist, so the channel is yielded once, as v2.
+ require.Equal(
+ t, []lnwire.GossipVersion{lnwire.GossipVersion2},
+ fetchVersions(),
+ )
+
+ // After deleting the v2 announcement the v1 one must take over.
+ require.NoError(t, graph.DeleteChannelEdges(
+ ctx, lnwire.GossipVersion2, false, false, chanV2.ChannelID,
+ ))
+ require.Equal(
+ t, []lnwire.GossipVersion{lnwire.GossipVersion1},
+ fetchVersions(),
+ )
+
+ // With both versions gone the channel disappears.
+ require.NoError(t, graph.DeleteChannelEdges(
+ ctx, lnwire.GossipVersion1, false, false, chanV1.ChannelID,
+ ))
+ require.Empty(t, fetchVersions())
+}
+
+// TestCacheKeepsFeaturesOverEmptyV2 asserts that a node announced on v2 with
+// no features does not shadow the feature vector it announced on v1. The cache
+// keys features by pub key alone, so both the live write path and a fresh
+// cache load have to rank the two announcements the same way.
+func TestCacheKeepsFeaturesOverEmptyV2(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version nodes require SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ nodePriv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ nodeV1 := createNode(t, lnwire.GossipVersion1, nodePriv)
+ require.False(t, nodeV1.Features.IsEmpty())
+ require.NoError(t, graph.AddNode(ctx, nodeV1))
+
+ // The same node re-announces on v2 without any feature bits.
+ pubKey := route.NewVertex(nodePriv.PubKey())
+ nodeV2 := models.NewV2Node(pubKey, &models.NodeV2Fields{
+ Signature: testSig.Serialize(),
+ LastBlockHeight: nextBlockHeight(),
+ Features: lnwire.NewRawFeatureVector(),
+ Addresses: testAddrs,
+ })
+ require.True(t, nodeV2.Features.IsEmpty())
+ require.NoError(t, graph.AddNode(ctx, nodeV2))
+
+ assertV1Features := func(g *ChannelGraph) {
+ t.Helper()
+
+ features, err := g.FetchNodeFeatures(ctx, pubKey)
+ require.NoError(t, err)
+ require.False(
+ t, features.IsEmpty(), "empty v2 features shadowed "+
+ "the v1 announcement",
+ )
+ require.Equal(
+ t, nodeV1.Features.RawFeatureVector,
+ features.RawFeatureVector,
+ )
+ }
+
+ assertV1Features(graph)
+
+ reloaded, err := NewChannelGraph(store, WithSyncGraphCachePopulation())
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ assertV1Features(reloaded)
+}
+
+// TestCacheNodeFeaturePrecedence asserts that the features the cache serves
+// for a node follow the cross-version precedence, whatever order the node's
+// announcements arrive in, and that a fresh cache load of the same store
+// agrees. The v2 features win unless they are empty, in which case the v1
+// features are used, and a v2-only node that withdraws its features is left
+// with none.
+func TestCacheNodeFeaturePrecedence(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version nodes require SQL backend")
+ }
+
+ v1Features := lnwire.NewRawFeatureVector(lnwire.GossipQueriesRequired)
+ v2Features := lnwire.NewRawFeatureVector(lnwire.TLVOnionPayloadRequired)
+ noFeatures := lnwire.NewRawFeatureVector()
+
+ type announcement struct {
+ v lnwire.GossipVersion
+ features *lnwire.RawFeatureVector
+ }
+
+ tests := []struct {
+ name string
+ announcements []announcement
+ want *lnwire.RawFeatureVector
+ }{
+ {
+ name: "v1 after v2",
+ announcements: []announcement{
+ {lnwire.GossipVersion2, v2Features},
+ {lnwire.GossipVersion1, v1Features},
+ },
+ want: v2Features,
+ },
+ {
+ name: "empty v2 after v2 with v1",
+ announcements: []announcement{
+ {lnwire.GossipVersion1, v1Features},
+ {lnwire.GossipVersion2, v2Features},
+ {lnwire.GossipVersion2, noFeatures},
+ },
+ want: v1Features,
+ },
+ {
+ name: "empty v2 after v2 without v1",
+ announcements: []announcement{
+ {lnwire.GossipVersion2, v2Features},
+ {lnwire.GossipVersion2, noFeatures},
+ },
+ want: noFeatures,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ t.Parallel()
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+
+ priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ pubKey := route.NewVertex(priv.PubKey())
+
+ for _, a := range test.announcements {
+ node := createNode(t, a.v, priv)
+ node.Features = lnwire.NewFeatureVector(
+ a.features, lnwire.Features,
+ )
+ require.NoError(t, graph.AddNode(ctx, node))
+ }
+
+ assertFeatures := func(g *ChannelGraph) {
+ t.Helper()
+
+ features, err := g.FetchNodeFeatures(
+ ctx, pubKey,
+ )
+ require.NoError(t, err)
+ require.Equal(
+ t, test.want, features.RawFeatureVector,
+ )
+ }
+
+ assertFeatures(graph)
+
+ reloaded, err := NewChannelGraph(
+ graph.db, WithSyncGraphCachePopulation(),
+ )
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ assertFeatures(reloaded)
+ })
+ }
+}
+
+// TestCachePolicyUpdatesAcrossVersions asserts that policy updates keep the
+// cache on the channel version that the Store prefers, with the policies of
+// that version only. A policy of the version that the Store doesn't select
+// must not leak into the cached entry, and a policy that changes the selection
+// must replace the cached entry as a whole, including the policy of the other
+// direction.
+func TestCachePolicyUpdatesAcrossVersions(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ // The channel is announced on v1 with a policy in each direction, and
+ // a second time on v2 without any policy.
+ chanV1, _ := createEdge(
+ lnwire.GossipVersion1, 330, 0, 0, 1, node1V1, node2V1,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 330, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ scid := chanV1.ChannelID
+ node1, node2 := node1V1.PubKeyBytes, node2V1.PubKeyBytes
+
+ updatePolicy := func(v lnwire.GossipVersion, updateTime int64,
+ isNode1 bool) *models.ChannelEdgePolicy {
+
+ t.Helper()
+
+ policy := newEdgePolicy(v, scid, updateTime, isNode1)
+ policy.ToNode = node2
+ if !isNode1 {
+ policy.ToNode = node1
+ }
+ policy.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policy))
+
+ return policy
+ }
+
+ updatePolicy(lnwire.GossipVersion1, 1000, true)
+ updatePolicy(lnwire.GossipVersion1, 1000, false)
+
+ // The first v2 policy makes v2 the preferred version, so the cached
+ // v1 entry, including the v1 policy of node 2, has to go.
+ policyV2 := updatePolicy(lnwire.GossipVersion2, 0, true)
+
+ // A later v1 policy doesn't change the selection and must not
+ // overwrite the v2 policy.
+ updatePolicy(lnwire.GossipVersion1, 2000, true)
+
+ assertV2Cached := func(g *ChannelGraph) {
+ t.Helper()
+
+ node1Chan := cachedChannel(t, g, node1, scid)
+ require.NotNil(t, node1Chan)
+ require.True(t, node1Chan.OutPolicySet)
+ require.Nil(
+ t, node1Chan.InPolicy, "v1 policy of node 2 left in "+
+ "the cache after v2 became preferred",
+ )
+
+ node2Chan := cachedChannel(t, g, node2, scid)
+ require.NotNil(t, node2Chan)
+ require.False(t, node2Chan.OutPolicySet)
+ require.NotNil(t, node2Chan.InPolicy)
+ require.Equal(
+ t, lnwire.GossipVersion2, node2Chan.InPolicy.Version,
+ )
+ require.Equal(
+ t, policyV2.FeeBaseMSat, node2Chan.InPolicy.FeeBaseMSat,
+ )
+ }
+
+ assertV2Cached(graph)
+
+ reloaded, err := NewChannelGraph(
+ graph.db, WithSyncGraphCachePopulation(),
+ )
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ assertV2Cached(reloaded)
+}
+
+// TestCacheFollowsVersionRemoval asserts that removing one gossip version of a
+// channel or node from the graph leaves the cache with what the Store still
+// has. The cache holds one version per SCID and per node, so a removal that
+// ignored the version would drop a channel or features that are still live on
+// the other version.
+func TestCacheFollowsVersionRemoval(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version graph requires SQL backend")
+ }
+
+ // newDualChannel returns a graph with a channel announced on both
+ // versions, where the given version has a policy from node 1.
+ newDualChannel := func(t *testing.T, policed lnwire.GossipVersion) (
+ *ChannelGraph, uint64, route.Vertex, route.Vertex) {
+
+ t.Helper()
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(
+ t, graph,
+ )
+
+ chanV1, scid := createEdge(
+ lnwire.GossipVersion1, 370, 0, 0, 1, node1V1, node2V1,
+ )
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 370, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ policy := newEdgePolicy(policed, scid.ToUint64(), 1000, true)
+ policy.ToNode = node2V1.PubKeyBytes
+ policy.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policy))
+
+ return graph, scid.ToUint64(), node1V1.PubKeyBytes,
+ node2V1.PubKeyBytes
+ }
+
+ // cachedVersion returns the version the graph caches the channel
+ // from, or zero if it doesn't cache the channel.
+ cachedVersion := func(t *testing.T, g *ChannelGraph,
+ node route.Vertex, scid uint64) lnwire.GossipVersion {
+
+ t.Helper()
+
+ channel := cachedChannel(t, g, node, scid)
+ if channel == nil {
+ return 0
+ }
+
+ return channel.version
+ }
+
+ // reload returns a graph with a fresh cache load of g's store.
+ reload := func(t *testing.T, g *ChannelGraph) *ChannelGraph {
+ t.Helper()
+
+ reloaded, err := NewChannelGraph(
+ g.db, WithSyncGraphCachePopulation(),
+ )
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ return reloaded
+ }
+
+ t.Run("delete uncached version", func(t *testing.T) {
+ t.Parallel()
+
+ graph, scid, node1, _ := newDualChannel(
+ t, lnwire.GossipVersion2,
+ )
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ cachedVersion(t, graph, node1, scid),
+ )
+
+ require.NoError(t, graph.DeleteChannelEdges(
+ t.Context(), lnwire.GossipVersion1, false, false, scid,
+ ))
+
+ for _, g := range []*ChannelGraph{graph, reload(t, graph)} {
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ cachedVersion(t, g, node1, scid),
+ )
+ }
+ })
+
+ t.Run("delete cached version", func(t *testing.T) {
+ t.Parallel()
+
+ graph, scid, node1, _ := newDualChannel(
+ t, lnwire.GossipVersion1,
+ )
+ require.Equal(
+ t, lnwire.GossipVersion1,
+ cachedVersion(t, graph, node1, scid),
+ )
+
+ require.NoError(t, graph.DeleteChannelEdges(
+ t.Context(), lnwire.GossipVersion1, false, false, scid,
+ ))
+
+ for _, g := range []*ChannelGraph{graph, reload(t, graph)} {
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ cachedVersion(t, g, node1, scid),
+ )
+ }
+ })
+
+ t.Run("mark cached version zombie", func(t *testing.T) {
+ t.Parallel()
+
+ graph, scid, node1, node2 := newDualChannel(
+ t, lnwire.GossipVersion1,
+ )
+
+ require.NoError(t, graph.MarkEdgeZombie(
+ t.Context(), lnwire.GossipVersion1, scid, node1, node2,
+ ))
+
+ // The zombie v1 row stays in the Store, so only the live
+ // cache is checked here.
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ cachedVersion(t, graph, node1, scid),
+ )
+ })
+
+ t.Run("delete node version", func(t *testing.T) {
+ t.Parallel()
+
+ graph, scid, node1, _ := newDualChannel(
+ t, lnwire.GossipVersion2,
+ )
+
+ // Deleting the v2 node deletes the v2 channel with it.
+ require.NoError(t, NewVersionedGraph(
+ graph, lnwire.GossipVersion2,
+ ).DeleteNode(t.Context(), node1))
+
+ for _, g := range []*ChannelGraph{graph, reload(t, graph)} {
+ require.Equal(
+ t, lnwire.GossipVersion1,
+ cachedVersion(t, g, node1, scid),
+ )
+ }
+ })
+
+ t.Run("prune node version", func(t *testing.T) {
+ t.Parallel()
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+
+ // Node X is announced on both versions, with features on v1
+ // only, and has a channel on v2 only, so pruning removes its
+ // unconnected v1 record.
+ xPriv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ xV1 := createNode(t, lnwire.GossipVersion1, xPriv)
+ xV2 := createNode(t, lnwire.GossipVersion2, xPriv)
+ xV2.Features = lnwire.EmptyFeatureVector()
+ y := createTestVertex(t, lnwire.GossipVersion2)
+ for _, node := range []*models.Node{xV1, xV2, y} {
+ require.NoError(t, graph.AddNode(ctx, node))
+ }
+
+ channel, _ := createEdge(
+ lnwire.GossipVersion2, 371, 0, 0, 1, xV2, y,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, channel))
+
+ x := route.Vertex(xV1.PubKeyBytes)
+ features, err := graph.FetchNodeFeatures(ctx, x)
+ require.NoError(t, err)
+ require.False(t, features.IsEmpty())
+
+ require.NoError(t, graph.PruneGraphNodes(ctx))
+
+ for _, g := range []*ChannelGraph{graph, reload(t, graph)} {
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ cachedVersion(t, g, x, channel.ChannelID),
+ )
+
+ features, err := g.FetchNodeFeatures(ctx, x)
+ require.NoError(t, err)
+ require.True(
+ t, features.IsEmpty(), "features of the "+
+ "pruned v1 record are still served",
+ )
+ }
+ })
+}
+
+// TestCacheReplayOutlivesCallerContext asserts that a cache update buffered
+// while the cache is being populated is replayed with the population's
+// context rather than the caller's. A caller may cancel its context as soon as
+// its write returns, and a reload with that context would fail and drop the
+// committed channel from the cache.
+func TestCacheReplayOutlivesCallerContext(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version channels require SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ // The channel is cached from its v1 version, which has a policy, while
+ // its v2 version has none.
+ chanV1, scid := createEdge(
+ lnwire.GossipVersion1, 380, 0, 0, 1, node1V1, node2V1,
+ )
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 380, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ newPolicy := func(v lnwire.GossipVersion) *models.ChannelEdgePolicy {
+ policy := newEdgePolicy(v, scid.ToUint64(), 1000, true)
+ policy.ToNode = node2V1.PubKeyBytes
+ policy.SigBytes = testSig.Serialize()
+
+ return policy
+ }
+ require.NoError(t, graph.UpdateEdgePolicy(
+ ctx, newPolicy(lnwire.GossipVersion1),
+ ))
+
+ // Buffer cache updates, as during the asynchronous population at
+ // startup.
+ graph.cache.beginPopulation()
+
+ // The first v2 policy makes v2 the preferred version, which the cache
+ // only picks up by reloading the channel from the Store. The caller
+ // cancels its context right after the write.
+ callerCtx, cancel := context.WithCancel(ctx)
+ require.NoError(t, graph.UpdateEdgePolicy(
+ callerCtx, newPolicy(lnwire.GossipVersion2),
+ ))
+ cancel()
+
+ graph.cache.finishPopulation(ctx, true)
+
+ channel := cachedChannel(t, graph, node1V1.PubKeyBytes, scid.ToUint64())
+ require.NotNil(t, channel, "committed channel dropped from the cache")
+ require.Equal(t, lnwire.GossipVersion2, channel.version)
+}
+
+// TestCacheRanksDisabledPolicies asserts that the cache ranks a channel
+// version whose policies are all disabled as one with policies, as the
+// preferred lookup tables do, even though it leaves the disabled policies out
+// of its routing entry. Otherwise reviving the other version of the channel
+// would displace the version that the Store prefers.
+func TestCacheRanksDisabledPolicies(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version channels require SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+ node1, node2 := node1V1.PubKeyBytes, node2V1.PubKeyBytes
+
+ chanV1, scid := createEdge(
+ lnwire.GossipVersion1, 390, 0, 0, 1, node1V1, node2V1,
+ )
+ chanV2, _ := createEdge(
+ lnwire.GossipVersion2, 390, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, chanV2))
+
+ // Both versions get a policy in each direction, the v2 ones disabled.
+ for _, v := range []lnwire.GossipVersion{
+ lnwire.GossipVersion1, lnwire.GossipVersion2,
+ } {
+ for _, isNode1 := range []bool{true, false} {
+ policy := newEdgePolicy(
+ v, scid.ToUint64(), 1000, isNode1,
+ )
+ policy.ToNode = node2
+ if !isNode1 {
+ policy.ToNode = node1
+ }
+ policy.SigBytes = testSig.Serialize()
+ if v == lnwire.GossipVersion2 {
+ policy.DisableFlags =
+ lnwire.ChanUpdateDisableIncoming |
+ lnwire.ChanUpdateDisableOutgoing
+ }
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policy))
+ }
+ }
+
+ // Both versions have policies, so the Store prefers v2.
+ info, _, _, err := graph.db.FetchChannelEdgesByIDPreferred(
+ ctx, scid.ToUint64(),
+ )
+ require.NoError(t, err)
+ require.Equal(t, lnwire.GossipVersion2, info.Version)
+
+ // Mark v1 as a zombie and load a fresh cache, which caches v2 without
+ // its disabled policies.
+ require.NoError(t, graph.MarkEdgeZombie(
+ ctx, lnwire.GossipVersion1, scid.ToUint64(), node1, node2,
+ ))
+
+ reloaded, err := NewChannelGraph(
+ graph.db, WithSyncGraphCachePopulation(),
+ )
+ require.NoError(t, err)
+ require.NoError(t, reloaded.Start())
+ t.Cleanup(func() {
+ require.NoError(t, reloaded.Stop())
+ })
+
+ // Reviving v1 must not displace v2 from the cache.
+ require.NoError(t, reloaded.MarkEdgeLive(
+ ctx, lnwire.GossipVersion1, scid.ToUint64(),
+ ))
+
+ channel := cachedChannel(t, reloaded, node1, scid.ToUint64())
+ require.NotNil(t, channel)
+ require.Equal(
+ t, lnwire.GossipVersion2, channel.version, "revived v1 "+
+ "displaced the v2 version that the Store prefers",
+ )
+}
+
+// TestGraphReadsWhileCacheLoading asserts that the reads ChannelGraph serves
+// from the database while its graph cache is still loading apply the same
+// cross-version precedence as the loaded cache. The SQL backend always runs
+// with the cache enabled, but it populates the cache asynchronously at
+// startup, and pathfinding runs on these reads until that is done.
+func TestGraphReadsWhileCacheLoading(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("cross-version reads require SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+
+ addPolicy := func(v lnwire.GossipVersion, scid uint64,
+ toNode route.Vertex) *models.ChannelEdgePolicy {
+
+ t.Helper()
+
+ policy := newEdgePolicy(v, scid, 1000, true)
+ policy.ToNode = toNode
+ policy.SigBytes = testSig.Serialize()
+ require.NoError(t, graph.UpdateEdgePolicy(ctx, policy))
+
+ return policy
+ }
+
+ // A v2-only channel between two v2-only nodes.
+ nodeA := createTestVertex(t, lnwire.GossipVersion2)
+ nodeB := createTestVertex(t, lnwire.GossipVersion2)
+ require.NoError(t, graph.AddNode(ctx, nodeA))
+ require.NoError(t, graph.AddNode(ctx, nodeB))
+
+ v2Only, _ := createEdge(
+ lnwire.GossipVersion2, 340, 0, 0, 1, nodeA, nodeB,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, v2Only))
+ v2OnlyPolicy := addPolicy(
+ lnwire.GossipVersion2, v2Only.ChannelID, nodeB.PubKeyBytes,
+ )
+
+ // A channel announced on both versions with a policy on each, which
+ // the Store resolves to v2.
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ dualV1, _ := createEdge(
+ lnwire.GossipVersion1, 341, 0, 0, 1, node1V1, node2V1,
+ )
+ dualV2, _ := createEdge(
+ lnwire.GossipVersion2, 341, 0, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, dualV1))
+ require.NoError(t, graph.AddChannelEdge(ctx, dualV2))
+ addPolicy(lnwire.GossipVersion1, dualV1.ChannelID, node2V1.PubKeyBytes)
+ dualPolicy := addPolicy(
+ lnwire.GossipVersion2, dualV2.ChannelID, node2V2.PubKeyBytes,
+ )
+
+ // Open a second graph on the same store and hold its cache in the
+ // state the asynchronous population starts in.
+ loading, err := NewChannelGraph(graph.db)
+ require.NoError(t, err)
+ loading.cache.beginPopulation()
+ require.Equal(t, GraphCacheStatusLoading, loading.GraphCacheStatus())
+
+ // channelView is the part of a directed channel that pathfinding
+ // relies on.
+ type channelView struct {
+ otherNode route.Vertex
+ capacity btcutil.Amount
+ outPolicySet bool
+ inVersion lnwire.GossipVersion
+ inFee lnwire.MilliSatoshi
+ inFeatures *lnwire.RawFeatureVector
+ }
+
+ viewOf := func(g NodeTraverser,
+ node route.Vertex) map[uint64]channelView {
+
+ t.Helper()
+
+ view := make(map[uint64]channelView)
+ err := g.ForEachNodeDirectedChannel(
+ ctx, node, func(c *DirectedChannel) error {
+ v := channelView{
+ otherNode: c.OtherNode,
+ capacity: c.Capacity,
+ outPolicySet: c.OutPolicySet,
+ }
+ if c.InPolicy != nil {
+ v.inVersion = c.InPolicy.Version
+ v.inFee = c.InPolicy.FeeBaseMSat
+ v.inFeatures = c.InPolicy.
+ ToNodeFeatures.RawFeatureVector
+ }
+ view[c.ChannelID] = v
+
+ return nil
+ }, func() {
+ clear(view)
+ },
+ )
+ require.NoError(t, err)
+
+ return view
+ }
+
+ nodes := []route.Vertex{
+ nodeA.PubKeyBytes, nodeB.PubKeyBytes,
+ node1V1.PubKeyBytes, node2V1.PubKeyBytes,
+ }
+
+ // assertMatchesCache asserts that the traverser sees each node the
+ // way the loaded cache of the first graph does.
+ assertMatchesCache := func(g NodeTraverser) {
+ t.Helper()
+
+ for _, node := range nodes {
+ require.Equal(t, viewOf(graph, node), viewOf(g, node))
+
+ want, err := graph.FetchNodeFeatures(ctx, node)
+ require.NoError(t, err)
+ got, err := g.FetchNodeFeatures(ctx, node)
+ require.NoError(t, err)
+ require.Equal(
+ t, want.RawFeatureVector, got.RawFeatureVector,
+ )
+ }
+
+ // The v2-only channel is visible, and the channel announced
+ // on both versions carries its v2 policy.
+ nodeBView := viewOf(g, nodeB.PubKeyBytes)
+ require.Contains(t, nodeBView, v2Only.ChannelID)
+ require.Equal(
+ t, v2OnlyPolicy.FeeBaseMSat,
+ nodeBView[v2Only.ChannelID].inFee,
+ )
+
+ node2View := viewOf(g, node2V1.PubKeyBytes)
+ require.Equal(
+ t, lnwire.GossipVersion2,
+ node2View[dualV1.ChannelID].inVersion,
+ )
+ require.Equal(
+ t, dualPolicy.FeeBaseMSat,
+ node2View[dualV1.ChannelID].inFee,
+ )
+ }
+
+ assertMatchesCache(loading)
+
+ err = loading.GraphSession(ctx, func(g NodeTraverser) error {
+ assertMatchesCache(g)
+
+ return nil
+ }, func() {})
+ require.NoError(t, err)
+}
+
+// TestPreferredIterationPaging asserts that the byte cursors used by the
+// preferred node and channel iteration hand over correctly between pages. Both
+// queries page on a pub key or SCID instead of an integer id, so a cursor bug
+// shows up as a node or channel that is skipped or yielded twice.
+func TestPreferredIterationPaging(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+ store := graph.db
+
+ // Page after two rows so that a handful of rows already spans several
+ // pages.
+ sqlStore, ok := store.(*SQLStore)
+ require.True(t, ok)
+ sqlStore.cfg.QueryCfg.MaxPageSize = 2
+
+ const numNodes = 5
+
+ nodes := make([]*models.Node, 0, numNodes)
+ for i := range numNodes {
+ priv, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ node := createNode(t, lnwire.GossipVersion1, priv)
+ require.NoError(t, graph.AddNode(ctx, node))
+ nodes = append(nodes, node)
+
+ // Announce every second node on v2 as well, so that the
+ // preferred mapping has to deduplicate across a page boundary.
+ if i%2 == 0 {
+ nodeV2 := createNode(t, lnwire.GossipVersion2, priv)
+ require.NoError(t, graph.AddNode(ctx, nodeV2))
+ }
+ }
+
+ // Fan the channels out from the first node so that we get one channel
+ // per remaining node.
+ scids := make(map[uint64]struct{})
+ for i := 1; i < numNodes; i++ {
+ edge, _ := createEdge(
+ lnwire.GossipVersion1, uint32(320+i), 0, 0, uint32(i),
+ nodes[0], nodes[i],
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, edge))
+ scids[edge.ChannelID] = struct{}{}
+ }
+
+ seenNodes := make(map[route.Vertex]int)
+ err := store.ForEachNode(ctx, func(node *models.Node) error {
+ seenNodes[node.PubKeyBytes]++
+
+ return nil
+ }, func() {
+ clear(seenNodes)
+ })
+ require.NoError(t, err)
+ require.Len(t, seenNodes, numNodes)
+
+ for pub, count := range seenNodes {
+ require.Equal(t, 1, count, "node %x yielded %d times", pub[:],
+ count)
+ }
+
+ seenChans := make(map[uint64]int)
+ err = store.ForEachChannel(ctx, func(info *models.ChannelEdgeInfo, _,
+ _ *models.ChannelEdgePolicy) error {
+
+ seenChans[info.ChannelID]++
+
+ return nil
+ }, func() {
+ clear(seenChans)
+ })
+ require.NoError(t, err)
+ require.Len(t, seenChans, len(scids))
+
+ for scid, count := range seenChans {
+ require.Contains(t, scids, scid)
+ require.Equal(t, 1, count, "channel %d yielded %d times", scid,
+ count)
+ }
+}
+
+// TestPruneAndDisconnectRemoveAllChannelVersions pins the property that the
+// cascade-only maintenance paths rely on. PruneGraph and
+// DisconnectBlockAtHeight match channels by outpoint and by SCID range, and
+// neither query filters on gossip version, so every version of a matched
+// channel is deleted in one statement and the cascade takes the preferred
+// mapping row with it. If either query ever gained a version predicate, the
+// cascade would strand the surviving version: its row would stay in the
+// database while it vanished from every cross-version read.
+func TestPruneAndDisconnectRemoveAllChannelVersions(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("preferred lookup requires SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t)
+
+ require.NoError(t, graph.SetSourceNode(
+ ctx, createTestVertex(t, lnwire.GossipVersion1),
+ ))
+
+ node1V1, node1V2, node2V1, node2V2 := addDualVersionNodes(t, graph)
+
+ // Both channels are announced under both gossip versions, so each SCID
+ // owns two channel rows and one preferred mapping row.
+ prunedV1, _ := createEdge(
+ lnwire.GossipVersion1, 201, 0, 0, 1, node1V1, node2V1,
+ )
+ prunedV2, _ := createEdge(
+ lnwire.GossipVersion2, 201, 0, 0, 1, node1V2, node2V2,
+ )
+ disconnectedV1, _ := createEdge(
+ lnwire.GossipVersion1, 202, 0, 0, 2, node1V1, node2V1,
+ )
+ disconnectedV2, _ := createEdge(
+ lnwire.GossipVersion2, 202, 0, 0, 2, node1V2, node2V2,
+ )
+ for _, e := range []*models.ChannelEdgeInfo{
+ prunedV1, prunedV2, disconnectedV1, disconnectedV2,
+ } {
+ require.NoError(t, graph.AddChannelEdge(ctx, e))
+ }
+
+ // assertGone checks both halves of the invariant: the SCID is absent
+ // from the cross-version iteration, which reads through the preferred
+ // mapping, and no channel row survives under either version.
+ assertGone := func(chanID uint64) {
+ t.Helper()
+
+ var seen bool
+ err := graph.db.ForEachChannel(
+ ctx, func(info *models.ChannelEdgeInfo, _,
+ _ *models.ChannelEdgePolicy) error {
+
+ if info.ChannelID == chanID {
+ seen = true
+ }
+
+ return nil
+ }, func() { seen = false },
+ )
+ require.NoError(t, err)
+ require.False(t, seen, "channel still yielded after delete")
+
+ for _, v := range []lnwire.GossipVersion{
+ lnwire.GossipVersion1, lnwire.GossipVersion2,
+ } {
+ has, _, err := graph.HasChannelEdge(ctx, v, chanID)
+ require.NoError(t, err)
+ require.Falsef(
+ t, has, "%v channel row survived the delete "+
+ "that dropped its preferred mapping", v,
+ )
+ }
+ }
+
+ // Seed the prune log so that DisconnectBlockAtHeight has something to
+ // roll back later on.
+ var blockHash chainhash.Hash
+ copy(blockHash[:], bytes.Repeat([]byte{1}, 32))
+ _, err := graph.PruneGraph(ctx, nil, &blockHash, 200)
+ require.NoError(t, err)
+
+ // Spending the funding outpoint must take both versions with it.
+ var blockHash2 chainhash.Hash
+ copy(blockHash2[:], bytes.Repeat([]byte{2}, 32))
+ pruned, err := graph.PruneGraph(
+ ctx, []*wire.OutPoint{&prunedV1.ChannelPoint}, &blockHash2, 203,
+ )
+ require.NoError(t, err)
+ require.NotEmpty(t, pruned)
+ assertGone(prunedV1.ChannelID)
+
+ // Disconnecting the block that funded the channel must do the same.
+ _, err = graph.DisconnectBlockAtHeight(ctx, 202)
+ require.NoError(t, err)
+ assertGone(disconnectedV1.ChannelID)
+}
+
+// TestForEachNodeCachedHonoursVersion verifies that
+// SQLStore.ForEachNodeCached uses the requested gossip version when looking
+// up channels for the iterated nodes, rather than silently falling back to
+// v1 channels for v2 nodes.
+func TestForEachNodeCachedHonoursVersion(t *testing.T) {
+ t.Parallel()
+
+ if !isSQLDB {
+ t.Skip("test only meaningful for SQL backend")
+ }
+
+ ctx := t.Context()
+ graph := MakeTestGraph(t, WithUseGraphCache(false))
+
+ // Create two nodes that exist under both v1 and v2.
+ priv1, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+ priv2, err := btcec.NewPrivateKey()
+ require.NoError(t, err)
+
+ require.NoError(t, graph.AddNode(
+ ctx, createNode(t, lnwire.GossipVersion1, priv1),
+ ))
+ require.NoError(t, graph.AddNode(
+ ctx, createNode(t, lnwire.GossipVersion1, priv2),
+ ))
+ node1V2 := createNode(t, lnwire.GossipVersion2, priv1)
+ node2V2 := createNode(t, lnwire.GossipVersion2, priv2)
+ require.NoError(t, graph.AddNode(ctx, node1V2))
+ require.NoError(t, graph.AddNode(ctx, node2V2))
+
+ // Add a channel only under v2 between the two nodes. If
+ // ForEachNodeCached honours its version parameter, callers asking for
+ // v2 nodes must see this channel; if it instead hardcodes v1 in the
+ // channel lookup, the channel will be missing.
+ edgeV2, _ := createEdge(
+ lnwire.GossipVersion2, 100, 1, 0, 1, node1V2, node2V2,
+ )
+ require.NoError(t, graph.AddChannelEdge(ctx, edgeV2))
+
+ store := graph.db
+ chansSeen := 0
+ err = store.ForEachNodeCached(
+ ctx, lnwire.GossipVersion2,
+ func(_ context.Context, n route.Vertex,
+ chans map[uint64]*DirectedChannel) error {
+
+ if n == node1V2.PubKeyBytes ||
+ n == node2V2.PubKeyBytes {
+
+ chansSeen += len(chans)
+ }
+
+ return nil
+ }, func() { chansSeen = 0 },
+ )
+ require.NoError(t, err)
+ require.Equal(t, 2, chansSeen,
+ "v2 ForEachNodeCached should report the v2 channel for "+
+ "each endpoint")
+}
### graph/db/interfaces.go
@@ -89,11 +89,11 @@ type Store interface { //nolint:interfacebloat
chans map[uint64]*DirectedChannel) error,
reset func()) error
- // ForEachNode iterates through all the stored vertices/nodes in the
- // graph, executing the passed callback with each node encountered. If
- // the callback returns an error, then the transaction is aborted and
- // the iteration stops early.
- ForEachNode(ctx context.Context, v lnwire.GossipVersion,
+ // ForEachNode iterates through all nodes in the graph across all
+ // gossip versions, yielding each unique node exactly once. The
+ // callback receives the best available Node (highest advertised
+ // version preferred, falling back to shell nodes).
+ ForEachNode(ctx context.Context,
cb func(*models.Node) error, reset func()) error
// ForEachNodeCacheable iterates through all the stored vertices/nodes
@@ -156,21 +156,18 @@ type Store interface { //nolint:interfacebloat
GraphSession(ctx context.Context,
cb func(graph NodeTraverser) error, reset func()) error
- // ForEachChannel iterates through all the channel edges stored within
- // the graph and invokes the passed callback for each edge. The callback
- // takes two edges as since this is a directed graph, both the in/out
- // edges are visited. If the callback returns an error, then the
- // transaction is aborted and the iteration stops early.
- //
- // NOTE: If an edge can't be found, or wasn't advertised, then a nil
- // pointer for that particular channel edge routing policy will be
- // passed into the callback.
- //
- // TODO(elle): add a cross-version iteration API and make this iterate
- // over all versions.
- ForEachChannel(ctx context.Context, v lnwire.GossipVersion,
+ // ForEachChannel iterates through all channel edges stored within the
+ // graph across all gossip versions, yielding each unique channel
+ // exactly once. The callback receives the edge info and both
+ // directional policies. When a channel is present on more than one
+ // version, a version that carries policies outranks a bare one, and
+ // the higher version only breaks a tie between two versions of equal
+ // policy state. Nil pointers are passed for policies that haven't
+ // been advertised.
+ ForEachChannel(ctx context.Context,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
- *models.ChannelEdgePolicy) error, reset func()) error
+ *models.ChannelEdgePolicy) error,
+ reset func()) error
// ForEachChannelCacheable iterates through all the channel edges stored
// within the graph and invokes the passed callback for each edge. The
@@ -318,6 +315,25 @@ type Store interface { //nolint:interfacebloat
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error)
+ // FetchChannelEdgesByIDPreferred behaves like FetchChannelEdgesByID
+ // but is version-agnostic: if the channel exists under multiple gossip
+ // versions it returns the preferred record. Preferred means the highest
+ // version with policies, falling back to the highest bare version.
+ FetchChannelEdgesByIDPreferred(ctx context.Context,
+ chanID uint64) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error)
+
+ // FetchChannelEdgesByOutpointPreferred behaves like
+ // FetchChannelEdgesByOutpoint but is version-agnostic: if the channel
+ // exists under multiple gossip versions it returns the preferred
+ // record. Preferred means the highest version with policies, falling
+ // back to the highest bare version.
+ FetchChannelEdgesByOutpointPreferred(ctx context.Context,
+ op *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error)
+
// ChannelView returns the verifiable edge information for each active
// channel within the known channel graph for the given gossip version.
// The set of UTXO's (along with their scripts) returned are the ones
### graph/db/kv_store.go
@@ -417,13 +417,10 @@ func (c *KVStore) AddrsForNode(ctx context.Context, v lnwire.GossipVersion,
// NOTE: If an edge can't be found, or wasn't advertised, then a nil pointer
// for that particular channel edge routing policy will be passed into the
// callback.
-func (c *KVStore) ForEachChannel(_ context.Context, v lnwire.GossipVersion,
+func (c *KVStore) ForEachChannel(_ context.Context,
cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
- *models.ChannelEdgePolicy) error, reset func()) error {
-
- if v != lnwire.GossipVersion1 {
- return ErrVersionNotSupportedForKVDB
- }
+ *models.ChannelEdgePolicy) error,
+ reset func()) error {
return forEachChannel(c.db, cb, reset)
}
@@ -846,13 +843,9 @@ func (c *KVStore) DisabledChannelIDs(
// early.
//
// NOTE: this is part of the Store interface.
-func (c *KVStore) ForEachNode(_ context.Context, v lnwire.GossipVersion,
+func (c *KVStore) ForEachNode(_ context.Context,
cb func(*models.Node) error, reset func()) error {
- if v != lnwire.GossipVersion1 {
- return ErrVersionNotSupportedForKVDB
- }
-
return forEachNode(c.db, func(tx kvdb.RTx,
node *models.Node) error {
@@ -4173,7 +4166,7 @@ func (c *KVStore) FetchChannelEdgesByID(_ context.Context,
// party as this is the only information we have about
// it and return an error signaling so.
zombieEdge, err := models.NewV1Channel(
- 0, chainhash.Hash{}, pubKey1, pubKey2,
+ chanID, chainhash.Hash{}, pubKey1, pubKey2,
&models.ChannelV1Fields{},
)
if err != nil {
@@ -4219,6 +4212,33 @@ func (c *KVStore) FetchChannelEdgesByID(_ context.Context,
return edgeInfo, policy1, policy2, nil
}
+// FetchChannelEdgesByIDPreferred looks up the channel by ID. The KV store
+// only supports gossip v1, so this simply delegates to the versioned fetch.
+//
+// NOTE: part of the Store interface.
+func (c *KVStore) FetchChannelEdgesByIDPreferred(ctx context.Context,
+ chanID uint64) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ return c.FetchChannelEdgesByID(ctx, lnwire.GossipVersion1, chanID)
+}
+
+// FetchChannelEdgesByOutpointPreferred looks up the channel by funding
+// outpoint. The KV store only supports gossip v1, so this simply delegates to
+// the versioned fetch.
+//
+// NOTE: part of the Store interface.
+func (c *KVStore) FetchChannelEdgesByOutpointPreferred(
+ ctx context.Context, op *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ return c.FetchChannelEdgesByOutpoint(
+ ctx, lnwire.GossipVersion1, op,
+ )
+}
+
// IsPublicNode is a helper method that determines whether the node with the
// given public key is seen as a public node in the graph from the graph's
// source node's point of view.
### graph/db/models/cached_edge_info.go
@@ -1,6 +1,9 @@
package models
-import "github.com/btcsuite/btcd/btcutil/v2"
+import (
+ "github.com/btcsuite/btcd/btcutil/v2"
+ "github.com/lightningnetwork/lnd/lnwire"
+)
// CachedEdgeInfo is a struct that only caches the information of a
// ChannelEdgeInfo that we actually use for pathfinding and therefore need to
@@ -20,6 +23,11 @@ type CachedEdgeInfo struct {
// Capacity is the total capacity of the channel, this is determined by
// the value output in the outpoint that created this channel.
Capacity btcutil.Amount
+
+ // Version is the gossip version that announced this channel. A channel
+ // can be announced on more than one version, and consumers that key
+ // channels by SCID alone need this field to tell the two apart.
+ Version lnwire.GossipVersion
}
// NewCachedEdge creates a new CachedEdgeInfo from the provided ChannelEdgeInfo.
@@ -29,5 +37,6 @@ func NewCachedEdge(edgeInfo *ChannelEdgeInfo) *CachedEdgeInfo {
NodeKey1Bytes: edgeInfo.NodeKey1Bytes,
NodeKey2Bytes: edgeInfo.NodeKey2Bytes,
Capacity: edgeInfo.Capacity,
+ Version: edgeInfo.Version,
}
}
### graph/db/models/cached_edge_policy.go
@@ -30,6 +30,11 @@ type CachedEdgePolicy struct {
// IsDisabled indicates whether the policy disables forwarding.
IsDisabled bool
+ // Version is the gossip version that announced this policy. The graph
+ // cache only applies a policy to a channel entry built from the same
+ // version.
+ Version lnwire.GossipVersion
+
// TimeLockDelta is the number of blocks this node will subtract from
// the expiry of an incoming HTLC. This value expresses the time buffer
// the node would like to HTLC exchanges.
@@ -87,6 +92,7 @@ func NewCachedPolicy(policy *ChannelEdgePolicy) *CachedEdgePolicy {
lnwire.ChanUpdateDisabled != 0,
IsNode1: policy.ChannelFlags&
lnwire.ChanUpdateDirection == 0,
+ Version: policy.Version,
TimeLockDelta: policy.TimeLockDelta,
MinHTLC: policy.MinHTLC,
MaxHTLC: policy.MaxHTLC,
@@ -106,6 +112,7 @@ func NewCachedPolicy(policy *ChannelEdgePolicy) *CachedEdgePolicy {
HasMaxHTLC: policy.MaxHTLC != 0,
IsNode1: !policy.SecondPeer,
IsDisabled: !policy.DisableFlags.IsEnabled(),
+ Version: policy.Version,
TimeLockDelta: policy.TimeLockDelta,
MinHTLC: policy.MinHTLC,
MaxHTLC: policy.MaxHTLC,
### graph/db/sql_store.go
@@ -54,7 +54,8 @@ type SQLQueries interface {
GetNodesByLastUpdateRange(ctx context.Context, arg sqlc.GetNodesByLastUpdateRangeParams) ([]sqlc.GraphNode, error)
GetNodesByBlockHeightRange(ctx context.Context, arg sqlc.GetNodesByBlockHeightRangeParams) ([]sqlc.GraphNode, error)
GetPublicNodesByLastUpdateRange(ctx context.Context, arg sqlc.GetPublicNodesByLastUpdateRangeParams) ([]sqlc.GraphNode, error)
- ListNodesPaginated(ctx context.Context, arg sqlc.ListNodesPaginatedParams) ([]sqlc.GraphNode, error)
+ ListPreferredNodesPaginated(ctx context.Context, arg sqlc.ListPreferredNodesPaginatedParams) ([]sqlc.ListPreferredNodesPaginatedRow, error)
+ UpsertPreferredNode(ctx context.Context, pubKey []byte) error
ListNodeIDsAndPubKeys(ctx context.Context, arg sqlc.ListNodeIDsAndPubKeysParams) ([]sqlc.ListNodeIDsAndPubKeysRow, error)
IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error)
IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error)
@@ -105,6 +106,10 @@ type SQLQueries interface {
ListChannelsByNodeID(ctx context.Context, arg sqlc.ListChannelsByNodeIDParams) ([]sqlc.ListChannelsByNodeIDRow, error)
ListChannelsForNodeIDs(ctx context.Context, arg sqlc.ListChannelsForNodeIDsParams) ([]sqlc.ListChannelsForNodeIDsRow, error)
ListChannelsWithPoliciesPaginated(ctx context.Context, arg sqlc.ListChannelsWithPoliciesPaginatedParams) ([]sqlc.ListChannelsWithPoliciesPaginatedRow, error)
+ ListPreferredChannelsPaginated(ctx context.Context, arg sqlc.ListPreferredChannelsPaginatedParams) ([]sqlc.ListPreferredChannelsPaginatedRow, error)
+ UpsertPreferredChannel(ctx context.Context, scid []byte) error
+ GetPreferredChannelVersionBySCID(ctx context.Context, scid []byte) (int16, error)
+ GetPreferredChannelVersionByOutpoint(ctx context.Context, arg sqlc.GetPreferredChannelVersionByOutpointParams) (int16, error)
ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg sqlc.ListChannelsWithPoliciesForCachePaginatedParams) ([]sqlc.ListChannelsWithPoliciesForCachePaginatedRow, error)
ListChannelsPaginated(ctx context.Context, arg sqlc.ListChannelsPaginatedParams) ([]sqlc.ListChannelsPaginatedRow, error)
ListChannelsPaginatedV2(ctx context.Context, arg sqlc.ListChannelsPaginatedV2Params) ([]sqlc.ListChannelsPaginatedV2Row, error)
@@ -418,6 +423,15 @@ func (s *SQLStore) DeleteNode(ctx context.Context, v lnwire.GossipVersion,
pubKey route.Vertex) error {
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
+ // Deleting the node cascades to its channels of this version,
+ // and from those to any preferred-channel mapping that points
+ // at them. Collect the SCIDs first, so that the mapping can be
+ // pointed at a version of the channel that survives.
+ scids, err := nodeChannelSCIDs(ctx, db, v, pubKey)
+ if err != nil {
+ return err
+ }
+
res, err := db.DeleteNodeByPubKey(
ctx, sqlc.DeleteNodeByPubKeyParams{
Version: int16(v),
@@ -439,7 +453,20 @@ func (s *SQLStore) DeleteNode(ctx context.Context, v lnwire.GossipVersion,
return fmt.Errorf("deleted %d rows, expected 1", rows)
}
- return err
+ // Recompute the preferred mappings. If another version of a
+ // channel or of this node still exists, the upserts point the
+ // mapping at it. If no version remains, the INSERT...SELECT is
+ // a no-op and the CASCADE on the FK already removed the mapping
+ // row when the node was deleted above.
+ for _, scid := range scids {
+ err := db.UpsertPreferredChannel(ctx, scid)
+ if err != nil {
+ return fmt.Errorf("unable to recompute "+
+ "preferred channel: %w", err)
+ }
+ }
+
+ return db.UpsertPreferredNode(ctx, pubKey[:])
}, sqldb.NoOpReset)
if err != nil {
return fmt.Errorf("unable to delete node: %w", err)
@@ -448,6 +475,43 @@ func (s *SQLStore) DeleteNode(ctx context.Context, v lnwire.GossipVersion,
return nil
}
+// nodeChannelSCIDs returns the SCIDs of the given node's channels on one
+// gossip version. An unknown node has none.
+func nodeChannelSCIDs(ctx context.Context, db SQLQueries,
+ v lnwire.GossipVersion, pubKey route.Vertex) ([][]byte, error) {
+
+ nodeID, err := db.GetNodeIDByPubKey(
+ ctx, sqlc.GetNodeIDByPubKeyParams{
+ Version: int16(v),
+ PubKey: pubKey[:],
+ },
+ )
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil, nil
+ }
+ if err != nil {
+ return nil, fmt.Errorf("unable to fetch node: %w", err)
+ }
+
+ rows, err := db.ListChannelsByNodeID(
+ ctx, sqlc.ListChannelsByNodeIDParams{
+ Version: int16(v),
+ NodeID1: nodeID,
+ },
+ )
+ if err != nil {
+ return nil, fmt.Errorf("unable to fetch node channels: %w",
+ err)
+ }
+
+ scids := make([][]byte, 0, len(rows))
+ for _, row := range rows {
+ scids = append(scids, row.GraphChannel.Scid)
+ }
+
+ return scids, nil
+}
+
// FetchNodeFeatures returns the features of the given node. If no features are
// known for the node, an empty feature vector is returned.
//
@@ -1148,17 +1212,12 @@ func (s *SQLStore) ForEachSourceNodeChannel(ctx context.Context,
// early.
//
// NOTE: part of the Store interface.
-func (s *SQLStore) ForEachNode(ctx context.Context, v lnwire.GossipVersion,
+func (s *SQLStore) ForEachNode(ctx context.Context,
cb func(node *models.Node) error, reset func()) error {
return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
- return forEachNodePaginated(
- ctx, s.cfg.QueryCfg, db,
- v, func(_ context.Context, _ int64,
- node *models.Node) error {
-
- return cb(node)
- },
+ return forEachPreferredNodePaginated(
+ ctx, s.cfg.QueryCfg, db, cb,
)
}, reset)
}
@@ -1765,7 +1824,7 @@ func (s *SQLStore) ForEachNodeCached(ctx context.Context,
// page.
allChannels, err := db.ListChannelsForNodeIDs(
ctx, sqlc.ListChannelsForNodeIDsParams{
- Version: int16(lnwire.GossipVersion1),
+ Version: int16(v),
Node1Ids: nodeIDs,
Node2Ids: nodeIDs,
},
@@ -1942,7 +2001,7 @@ func (s *SQLStore) ForEachChannelCacheable(ctx context.Context,
}
edge := buildCacheableChannelInfo(
- row.Scid, row.Capacity.Int64, node1, node2,
+ v, row.Scid, row.Capacity.Int64, node1, node2,
)
dbPol1, dbPol2, err := extractChannelPolicies(row)
@@ -2000,16 +2059,14 @@ func (s *SQLStore) ForEachChannelCacheable(ctx context.Context,
//
// NOTE: part of the Store interface.
func (s *SQLStore) ForEachChannel(ctx context.Context,
- v lnwire.GossipVersion, cb func(*models.ChannelEdgeInfo,
- *models.ChannelEdgePolicy, *models.ChannelEdgePolicy) error,
+ cb func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy) error,
reset func()) error {
- if !isKnownGossipVersion(v) {
- return fmt.Errorf("unsupported gossip version: %d", v)
- }
-
return s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
- return forEachChannelWithPolicies(ctx, db, s.cfg, v, cb)
+ return forEachPreferredChannelWithPolicies(
+ ctx, db, s.cfg, cb,
+ )
}, reset)
}
@@ -2432,7 +2489,25 @@ func (s *SQLStore) DeleteChannelEdges(ctx context.Context,
}
}
- return s.deleteChannels(ctx, db, chanIDsToDelete)
+ err = s.deleteChannels(ctx, db, chanIDsToDelete)
+ if err != nil {
+ return err
+ }
+
+ // The CASCADE on graph_preferred_channels will have
+ // removed the mapping row for any deleted channel. If
+ // another version of the same SCID still exists, we
+ // need to re-insert the mapping.
+ for _, chanID := range chanIDs {
+ scidBytes := channelIDToBytes(chanID)
+ err = db.UpsertPreferredChannel(ctx, scidBytes)
+ if err != nil {
+ return fmt.Errorf("recalc preferred "+
+ "channel(%d): %w", chanID, err)
+ }
+ }
+
+ return nil
}, func() {
edges = nil
@@ -2521,12 +2596,12 @@ func (s *SQLStore) FetchChannelEdgesByID(ctx context.Context,
switch v {
case gossipV1:
edge, err = models.NewV1Channel(
- 0, chainhash.Hash{}, node1,
+ chanID, chainhash.Hash{}, node1,
node2, &models.ChannelV1Fields{},
)
case gossipV2:
edge, err = models.NewV2Channel(
- 0, chainhash.Hash{}, node1,
+ chanID, chainhash.Hash{}, node1,
node2, &models.ChannelV2Fields{},
)
}
@@ -2658,6 +2733,270 @@ func (s *SQLStore) FetchChannelEdgesByOutpoint(ctx context.Context,
return edge, policy1, policy2, nil
}
+var preferredGossipVersionsDescending = []lnwire.GossipVersion{
+ gossipV2, gossipV1,
+}
+
+// FetchChannelEdgesByIDPreferred returns the version of the channel that the
+// preferred-channel mapping selects, so it follows the rule that
+// UpsertPreferredChannel applies: a version with policies wins over a bare
+// one, then the higher version wins. This keeps a v2 channel with no policies
+// from hiding a v1 channel that has valid policy data.
+//
+// If no version of the channel is live but at least one version reports it as
+// a zombie, ErrZombieEdge is returned with the highest such version's zombie
+// edge info populated so callers can resurrect it.
+//
+// NOTE: part of the Store interface.
+func (s *SQLStore) FetchChannelEdgesByIDPreferred(ctx context.Context,
+ chanID uint64) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ var (
+ bestInfo *models.ChannelEdgeInfo
+ bestP1 *models.ChannelEdgePolicy
+ bestP2 *models.ChannelEdgePolicy
+ bestZombie *models.ChannelEdgeInfo
+ chanIDB = channelIDToBytes(chanID)
+ buildLiveEdge = func(ctx context.Context, db SQLQueries,
+ row sqlc.GetChannelBySCIDWithPoliciesRow) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ node1, node2, err := buildNodeVertices(
+ row.GraphNode.PubKey, row.GraphNode_2.PubKey,
+ )
+ if err != nil {
+ return nil, nil, nil, err
+ }
+
+ edge, err := getAndBuildEdgeInfo(
+ ctx, s.cfg, db, row.GraphChannel, node1, node2,
+ )
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "build channel info: %w", err)
+ }
+
+ dbPol1, dbPol2, err := extractChannelPolicies(row)
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "extract channel policies: %w", err)
+ }
+
+ policy1, policy2, err := getAndBuildChanPolicies(
+ ctx, s.cfg.QueryCfg, db, dbPol1, dbPol2,
+ edge.ChannelID, node1, node2,
+ )
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "build channel policies: %w", err)
+ }
+
+ return edge, policy1, policy2, nil
+ }
+ )
+
+ err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
+ v, err := db.GetPreferredChannelVersionBySCID(ctx, chanIDB)
+ switch {
+ case err == nil:
+ row, err := db.GetChannelBySCIDWithPolicies(
+ ctx, sqlc.GetChannelBySCIDWithPoliciesParams{
+ Scid: chanIDB,
+ Version: v,
+ },
+ )
+ if err != nil {
+ return fmt.Errorf("unable to fetch channel: %w",
+ err)
+ }
+
+ bestInfo, bestP1, bestP2, err = buildLiveEdge(
+ ctx, db, row,
+ )
+
+ return err
+
+ case !errors.Is(err, sql.ErrNoRows):
+ return fmt.Errorf("unable to fetch preferred channel "+
+ "version: %w", err)
+ }
+
+ // No version of the channel is live, so report the highest
+ // version that is marked as a zombie, if any.
+ for _, v := range preferredGossipVersionsDescending {
+ zombie, err := db.GetZombieChannel(
+ ctx, sqlc.GetZombieChannelParams{
+ Scid: chanIDB,
+ Version: int16(v),
+ },
+ )
+ if errors.Is(err, sql.ErrNoRows) {
+ continue
+ }
+ if err != nil {
+ return fmt.Errorf("unable to check if channel "+
+ "is zombie: %w", err)
+ }
+
+ bestZombie, err = buildZombieEdge(
+ v, chanID, zombie.NodeKey1, zombie.NodeKey2,
+ )
+ if err != nil {
+ return err
+ }
+
+ return ErrZombieEdge
+ }
+
+ return ErrEdgeNotFound
+ }, func() {
+ bestInfo, bestP1, bestP2, bestZombie = nil, nil, nil, nil
+ })
+ if errors.Is(err, ErrZombieEdge) {
+ return bestZombie, nil, nil, ErrZombieEdge
+ }
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("could not fetch preferred "+
+ "channel: %w", err)
+ }
+
+ return bestInfo, bestP1, bestP2, nil
+}
+
+// FetchChannelEdgesByOutpointPreferred returns the version of the channel that
+// the preferred-channel mapping selects, so it follows the rule that
+// UpsertPreferredChannel applies: a version with policies wins over a bare
+// one, then the higher version wins. This keeps a v2 channel with no policies
+// from hiding a v1 channel that has valid policy data.
+//
+// NOTE: part of the Store interface.
+func (s *SQLStore) FetchChannelEdgesByOutpointPreferred(
+ ctx context.Context, op *wire.OutPoint) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ var (
+ bestInfo *models.ChannelEdgeInfo
+ bestP1 *models.ChannelEdgePolicy
+ bestP2 *models.ChannelEdgePolicy
+ buildLiveEdge = func(ctx context.Context, db SQLQueries,
+ row sqlc.GetChannelByOutpointWithPoliciesRow) (
+ *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+ *models.ChannelEdgePolicy, error) {
+
+ node1, node2, err := buildNodeVertices(
+ row.Node1Pubkey, row.Node2Pubkey,
+ )
+ if err != nil {
+ return nil, nil, nil, err
+ }
+
+ edge, err := getAndBuildEdgeInfo(
+ ctx, s.cfg, db, row.GraphChannel, node1, node2,
+ )
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "build channel info: %w", err)
+ }
+
+ dbPol1, dbPol2, err := extractChannelPolicies(row)
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "extract channel policies: %w", err)
+ }
+
+ policy1, policy2, err := getAndBuildChanPolicies(
+ ctx, s.cfg.QueryCfg, db, dbPol1, dbPol2,
+ edge.ChannelID, node1, node2,
+ )
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("unable to "+
+ "build channel policies: %w", err)
+ }
+
+ return edge, policy1, policy2, nil
+ }
+ )
+
+ // The outpoint is only indexed together with the version, so pass
+ // every known version to let the mapping lookup use that index.
+ versions := make([]int16, 0, len(preferredGossipVersionsDescending))
+ for _, v := range preferredGossipVersionsDescending {
+ versions = append(versions, int16(v))
+ }
+
+ err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
+ v, err := db.GetPreferredChannelVersionByOutpoint(
+ ctx, sqlc.GetPreferredChannelVersionByOutpointParams{
+ Outpoint: op.String(),
+ Versions: versions,
+ },
+ )
+ if errors.Is(err, sql.ErrNoRows) {
+ return ErrEdgeNotFound
+ }
+ if err != nil {
+ return fmt.Errorf("unable to fetch preferred channel "+
+ "version: %w", err)
+ }
+
+ row, err := db.GetChannelByOutpointWithPolicies(
+ ctx, sqlc.GetChannelByOutpointWithPoliciesParams{
+ Outpoint: op.String(),
+ Version: v,
+ },
+ )
+ if err != nil {
+ return fmt.Errorf("unable to fetch channel: %w", err)
+ }
+
+ bestInfo, bestP1, bestP2, err = buildLiveEdge(ctx, db, row)
+
+ return err
+ }, func() {
+ bestInfo, bestP1, bestP2 = nil, nil, nil
+ })
+ if err != nil {
+ return nil, nil, nil, fmt.Errorf("could not fetch preferred "+
+ "channel: %w", err)
+ }
+
+ return bestInfo, bestP1, bestP2, nil
+}
+
+func buildZombieEdge(v lnwire.GossipVersion, chanID uint64, nodeKey1,
+ nodeKey2 []byte) (*models.ChannelEdgeInfo, error) {
+
+ node1, err := route.NewVertexFromBytes(nodeKey1)
+ if err != nil {
+ return nil, err
+ }
+ node2, err := route.NewVertexFromBytes(nodeKey2)
+ if err != nil {
+ return nil, err
+ }
+
+ switch v {
+ case gossipV1:
+ return models.NewV1Channel(
+ chanID, chainhash.Hash{}, node1, node2,
+ &models.ChannelV1Fields{},
+ )
+
+ case gossipV2:
+ return models.NewV2Channel(
+ chanID, chainhash.Hash{}, node1, node2,
+ &models.ChannelV2Fields{},
+ )
+
+ default:
+ return nil, fmt.Errorf("unsupported gossip version: %d", v)
+ }
+}
+
// HasV1ChannelEdge returns true if the database knows of a channel edge
// with the passed channel ID, and false otherwise. If an edge with that ID
// is found within the graph, then two time stamps representing the last time
@@ -3317,6 +3656,10 @@ func (s *SQLStore) PruneGraph(ctx context.Context,
return err
}
+ // Delete all matched channels. GetChannelsByOutpoints
+ // returns every version for a given outpoint, so all
+ // versions are deleted and the CASCADE on
+ // graph_preferred_channels handles cleanup.
err = s.deleteChannels(ctx, db, chansToDelete)
if err != nil {
return fmt.Errorf("unable to delete channels: %w", err)
@@ -3624,9 +3967,22 @@ func (s *SQLStore) pruneGraphNodes(ctx context.Context,
"nodes: %w", err)
}
+ // Recalc preferred node mappings for all affected pub_keys. The
+ // CASCADE may have removed some entries; if another version of the
+ // node still exists, UpsertPreferredNode will re-insert the mapping.
+ // If no version remains, the upsert is a no-op (the INSERT ... SELECT
+ // returns no rows). Note that nodeKeys may contain duplicates if a
+ // node existed in multiple gossip versions and was pruned in all of
+ // them; UpsertPreferredNode is idempotent, so this is harmless.
prunedNodes := make([]route.Vertex, len(nodeKeys))
- for i, nodeKey := range nodeKeys {
- pub, err := route.NewVertexFromBytes(nodeKey)
+ for i, key := range nodeKeys {
+ err = db.UpsertPreferredNode(ctx, key)
+ if err != nil {
+ return nil, fmt.Errorf("recalc preferred "+
+ "node: %w", err)
+ }
+
+ pub, err := route.NewVertexFromBytes(key)
if err != nil {
return nil, fmt.Errorf("unable to parse pubkey "+
"from bytes: %w", err)
@@ -3701,6 +4057,10 @@ func (s *SQLStore) DisconnectBlockAtHeight(ctx context.Context,
removedChans = channelEdges
+ // Delete all matched channels. GetChannelsBySCIDRange
+ // returns every version for a given SCID, so all versions
+ // are deleted and the CASCADE on
+ // graph_preferred_channels handles cleanup.
err = s.deleteChannels(ctx, db, chanIDsToDelete)
if err != nil {
return fmt.Errorf("unable to delete channels: %w", err)
@@ -3861,6 +4221,13 @@ func (s *SQLStore) GraphSession(ctx context.Context,
// sqlNodeTraverser implements the NodeTraverser interface but with a backing
// read only transaction for a consistent view of the graph.
+//
+// ChannelGraph reads through this type whenever its in-memory graph cache
+// can't serve reads. The SQL backend always runs with the cache enabled, but
+// the cache is populated asynchronously at startup and pathfinding runs on
+// these reads until that is done, or for good if it fails. The reads below
+// therefore apply the same cross-version precedence as the cache, so that v2
+// channels and features don't disappear from pathfinding in the meantime.
type sqlNodeTraverser struct {
db SQLQueries
chain chainhash.Hash
@@ -3888,9 +4255,7 @@ func (s *sqlNodeTraverser) ForEachNodeDirectedChannel(
ctx context.Context, nodePub route.Vertex,
cb func(channel *DirectedChannel) error, _ func()) error {
- return forEachNodeDirectedChannel(
- ctx, s.db, lnwire.GossipVersion1, nodePub, cb,
- )
+ return forEachPreferredNodeDirectedChannel(ctx, s.db, nodePub, cb)
}
// FetchNodeFeatures returns the features of the given node. If the node is
@@ -3901,7 +4266,98 @@ func (s *sqlNodeTraverser) FetchNodeFeatures(ctx context.Context,
nodePub route.Vertex) (
*lnwire.FeatureVector, error) {
- return fetchNodeFeatures(ctx, s.db, lnwire.GossipVersion1, nodePub)
+ return fetchPreferredNodeFeatures(ctx, s.db, nodePub)
+}
+
+// forEachPreferredNodeDirectedChannel is the cross-version counterpart of
+// forEachNodeDirectedChannel. It yields each channel of the node once: a
+// channel announced on both gossip versions is taken from the version that the
+// preferred-channel mapping selects, which is the version the graph cache
+// holds for it. The incoming policies carry the node's features as selected by
+// fetchPreferredNodeFeatures.
+func forEachPreferredNodeDirectedChannel(ctx context.Context, db SQLQueries,
+ nodePub route.Vertex, cb func(channel *DirectedChannel) error) error {
+
+ var (
+ channels []*DirectedChannel
+ indices = make(map[uint64]int)
+ )
+ for _, v := range []lnwire.GossipVersion{gossipV1, gossipV2} {
+ collect := func(channel *DirectedChannel) error {
+ i, ok := indices[channel.ChannelID]
+ if !ok {
+ indices[channel.ChannelID] = len(channels)
+ channels = append(channels, channel)
+
+ return nil
+ }
+
+ // The channel is announced on both versions, so let
+ // the preferred mapping decide.
+ scid := channelIDToBytes(channel.ChannelID)
+ preferred, err := db.GetPreferredChannelVersionBySCID(
+ ctx, scid,
+ )
+ if err != nil {
+ return fmt.Errorf("unable to fetch preferred "+
+ "version of channel %v: %w",
+ channel.ChannelID, err)
+ }
+
+ if lnwire.GossipVersion(preferred) == v {
+ channels[i] = channel
+ }
+
+ return nil
+ }
+
+ err := forEachNodeDirectedChannel(
+ ctx, db, v, nodePub, collect,
+ )
+ if err != nil {
+ return err
+ }
+ }
+
+ if len(channels) == 0 {
+ return nil
+ }
+
+ features, err := fetchPreferredNodeFeatures(ctx, db, nodePub)
+ if err != nil {
+ return err
+ }
+
+ for _, channel := range channels {
+ if channel.InPolicy != nil {
+ channel.InPolicy.ToNodeFeatures = features
+ }
+
+ if err := cb(channel); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// fetchPreferredNodeFeatures returns the features of a node across both gossip
+// versions, selected the same way as the graph cache selects them, see
+// preferredFeatures.
+func fetchPreferredNodeFeatures(ctx context.Context, db SQLQueries,
+ nodePub route.Vertex) (*lnwire.FeatureVector, error) {
+
+ v1Features, err := fetchNodeFeatures(ctx, db, gossipV1, nodePub)
+ if err != nil {
+ return nil, err
+ }
+
+ v2Features, err := fetchNodeFeatures(ctx, db, gossipV2, nodePub)
+ if err != nil {
+ return nil, err
+ }
+
+ return preferredFeatures(v1Features, v2Features), nil
}
// forEachNodeDirectedChannel iterates through all channels of a given
@@ -3959,8 +4415,8 @@ func forEachNodeDirectedChannel(ctx context.Context, db SQLQueries,
}
edge := buildCacheableChannelInfo(
- row.GraphChannel.Scid, row.GraphChannel.Capacity.Int64,
- node1, node2,
+ v, row.GraphChannel.Scid,
+ row.GraphChannel.Capacity.Int64, node1, node2,
)
dbPol1, dbPol2, err := extractChannelPolicies(row)
@@ -4283,6 +4739,15 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries,
"policy extra TLVs: %w", err)
}
+ // Adding a policy may change which version is preferred for this
+ // SCID (a version with policies outranks one without).
+ scidBytes := channelIDToBytes(edge.ChannelID)
+ err = tx.UpsertPreferredChannel(ctx, scidBytes)
+ if err != nil {
+ return node1Pub, node2Pub, false, fmt.Errorf("upserting "+
+ "preferred channel(%d): %w", edge.ChannelID, err)
+ }
+
return node1Pub, node2Pub, isNode1, nil
}
@@ -4313,14 +4778,16 @@ func getNodeByPubKey(ctx context.Context, cfg *sqldb.QueryConfig, db SQLQueries,
// buildCacheableChannelInfo builds a models.CachedEdgeInfo instance from the
// provided parameters.
-func buildCacheableChannelInfo(scid []byte, capacity int64, node1Pub,
+func buildCacheableChannelInfo(v lnwire.GossipVersion, scid []byte,
+ capacity int64, node1Pub,
node2Pub route.Vertex) *models.CachedEdgeInfo {
return &models.CachedEdgeInfo{
ChannelID: byteOrder.Uint64(scid),
NodeKey1Bytes: node1Pub,
NodeKey2Bytes: node2Pub,
Capacity: btcutil.Amount(capacity),
+ Version: v,
}
}
@@ -4636,6 +5103,13 @@ func upsertSourceNode(ctx context.Context, db SQLQueries,
node.PubKeyBytes, err)
}
+ // Recompute the preferred node mapping for this pub_key.
+ err = db.UpsertPreferredNode(ctx, node.PubKeyBytes[:])
+ if err != nil {
+ return 0, fmt.Errorf("upserting preferred node(%x): %w",
+ node.PubKeyBytes, err)
+ }
+
// We can exit here if we don't have the announcement yet.
if !node.HaveAnnouncement() {
return nodeID, nil
@@ -4672,6 +5146,15 @@ func upsertNode(ctx context.Context, db SQLQueries,
err)
}
+ // Recompute the preferred node mapping for this pub_key. Even a shell
+ // node may become the preferred entry (e.g. first v2 shell for a key
+ // that previously only existed as a v1 shell).
+ err = db.UpsertPreferredNode(ctx, node.PubKeyBytes[:])
+ if err != nil {
+ return 0, fmt.Errorf("upserting preferred node(%x): %w",
+ node.PubKeyBytes, err)
+ }
+
// We can exit here if we don't have the announcement yet.
if !node.HaveAnnouncement() {
return nodeID, nil
@@ -5155,6 +5638,14 @@ func insertChannel(ctx context.Context, db SQLQueries,
}
}
+ // Recompute the preferred channel mapping for this SCID.
+ scidBytes := channelIDToBytes(edge.ChannelID)
+ err = db.UpsertPreferredChannel(ctx, scidBytes)
+ if err != nil {
+ return fmt.Errorf("upserting preferred channel(%d): %w",
+ edge.ChannelID, err)
+ }
+
return nil
}
@@ -5188,6 +5679,13 @@ func maybeCreateShellNode(ctx context.Context, db SQLQueries,
return 0, fmt.Errorf("unable to create shell node: %w", err)
}
+ // Recompute the preferred node mapping for this pub_key.
+ err = db.UpsertPreferredNode(ctx, pubKey[:])
+ if err != nil {
+ return 0, fmt.Errorf("upserting preferred node(%x): %w",
+ pubKey[:], err)
+ }
+
return id, nil
}
@@ -5959,6 +6457,54 @@ func extractChannelPolicies(row any) (*sqlc.GraphChannelPolicy,
return policy1, policy2, nil
+ case sqlc.ListPreferredChannelsPaginatedRow:
+ if r.Policy1ID.Valid {
+ policy1 = &sqlc.GraphChannelPolicy{
+ ID: r.Policy1ID.Int64,
+ Version: r.Policy1Version.Int16,
+ ChannelID: r.GraphChannel.ID,
+ NodeID: r.Policy1NodeID.Int64,
+ Timelock: r.Policy1Timelock.Int32,
+ FeePpm: r.Policy1FeePpm.Int64,
+ BaseFeeMsat: r.Policy1BaseFeeMsat.Int64,
+ MinHtlcMsat: r.Policy1MinHtlcMsat.Int64,
+ MaxHtlcMsat: r.Policy1MaxHtlcMsat,
+ LastUpdate: r.Policy1LastUpdate,
+ InboundBaseFeeMsat: r.Policy1InboundBaseFeeMsat,
+ InboundFeeRateMilliMsat: r.Policy1InboundFeeRateMilliMsat,
+ Disabled: r.Policy1Disabled,
+ MessageFlags: r.Policy1MessageFlags,
+ ChannelFlags: r.Policy1ChannelFlags,
+ Signature: r.Policy1Signature,
+ BlockHeight: r.Policy1BlockHeight,
+ DisableFlags: r.Policy1DisableFlags,
+ }
+ }
+ if r.Policy2ID.Valid {
+ policy2 = &sqlc.GraphChannelPolicy{
+ ID: r.Policy2ID.Int64,
+ Version: r.Policy2Version.Int16,
+ ChannelID: r.GraphChannel.ID,
+ NodeID: r.Policy2NodeID.Int64,
+ Timelock: r.Policy2Timelock.Int32,
+ FeePpm: r.Policy2FeePpm.Int64,
+ BaseFeeMsat: r.Policy2BaseFeeMsat.Int64,
+ MinHtlcMsat: r.Policy2MinHtlcMsat.Int64,
+ MaxHtlcMsat: r.Policy2MaxHtlcMsat,
+ LastUpdate: r.Policy2LastUpdate,
+ InboundBaseFeeMsat: r.Policy2InboundBaseFeeMsat,
+ InboundFeeRateMilliMsat: r.Policy2InboundFeeRateMilliMsat,
+ Disabled: r.Policy2Disabled,
+ MessageFlags: r.Policy2MessageFlags,
+ ChannelFlags: r.Policy2ChannelFlags,
+ Signature: r.Policy2Signature,
+ BlockHeight: r.Policy2BlockHeight,
+ DisableFlags: r.Policy2DisableFlags,
+ }
+ }
+
+ return policy1, policy2, nil
+
case sqlc.ListChannelsWithPoliciesPaginatedRow:
if r.Policy1ID.Valid {
policy1 = &sqlc.GraphChannelPolicy{
@@ -6528,32 +7074,32 @@ func batchLoadChannelPolicyExtrasHelper(ctx context.Context,
)
}
-// forEachNodePaginated executes a paginated query to process each node in the
-// graph. It uses the provided SQLQueries interface to fetch nodes in batches
-// and applies the provided processNode function to each node.
-func forEachNodePaginated(ctx context.Context, cfg *sqldb.QueryConfig,
- db SQLQueries, protocol lnwire.GossipVersion,
- processNode func(context.Context, int64,
- *models.Node) error) error {
+// forEachPreferredNodePaginated executes a paginated query that yields one
+// preferred node per pubkey across all gossip versions.
+func forEachPreferredNodePaginated(ctx context.Context, cfg *sqldb.QueryConfig,
+ db SQLQueries, processNode func(*models.Node) error) error {
- pageQueryFunc := func(ctx context.Context, lastID int64,
- limit int32) ([]sqlc.GraphNode, error) {
+ pageQueryFunc := func(ctx context.Context, cursor []byte,
+ limit int32) ([]sqlc.ListPreferredNodesPaginatedRow, error) {
- return db.ListNodesPaginated(
- ctx, sqlc.ListNodesPaginatedParams{
- Version: int16(protocol),
- ID: lastID,
- Limit: limit,
+ return db.ListPreferredNodesPaginated(
+ ctx, sqlc.ListPreferredNodesPaginatedParams{
+ PubKey: cursor,
+ Limit: limit,
},
)
}
- extractPageCursor := func(node sqlc.GraphNode) int64 {
- return node.ID
+ extractPageCursor := func(
+ row sqlc.ListPreferredNodesPaginatedRow) []byte {
+
+ return row.GraphNode.PubKey
}
- collectFunc := func(node sqlc.GraphNode) (int64, error) {
- return node.ID, nil
+ collectFunc := func(
+ row sqlc.ListPreferredNodesPaginatedRow) (int64, error) {
+
+ return row.GraphNode.ID, nil
}
batchQueryFunc := func(ctx context.Context,
@@ -6562,63 +7108,65 @@ func forEachNodePaginated(ctx context.Context, cfg *sqldb.QueryConfig,
return batchLoadNodeData(ctx, cfg, db, nodeIDs)
}
- processItem := func(ctx context.Context, dbNode sqlc.GraphNode,
+ processItem := func(_ context.Context,
+ row sqlc.ListPreferredNodesPaginatedRow,
batchData *batchNodeData) error {
+ dbNode := row.GraphNode
node, err := buildNodeWithBatchData(dbNode, batchData)
if err != nil {
- return fmt.Errorf("unable to build "+
- "node(id=%d): %w", dbNode.ID, err)
+ return fmt.Errorf("unable to build node(id=%d): %w",
+ dbNode.ID, err)
}
- return processNode(ctx, dbNode.ID, node)
+ return processNode(node)
}
return sqldb.ExecuteCollectAndBatchWithSharedDataQuery(
- ctx, cfg, int64(-1), pageQueryFunc, extractPageCursor,
+ ctx, cfg, []byte{}, pageQueryFunc, extractPageCursor,
collectFunc, batchQueryFunc, processItem,
)
}
-// forEachChannelWithPolicies executes a paginated query to process each channel
-// with policies in the graph.
-func forEachChannelWithPolicies(ctx context.Context, db SQLQueries,
- cfg *SQLStoreConfig, v lnwire.GossipVersion,
- processChannel func(*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
+// forEachPreferredChannelWithPolicies executes a paginated query that yields
+// one preferred channel per SCID across all gossip versions.
+func forEachPreferredChannelWithPolicies(ctx context.Context,
+ db SQLQueries, cfg *SQLStoreConfig,
+ processChannel func(*models.ChannelEdgeInfo,
+ *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy) error) error {
type channelBatchIDs struct {
channelID int64
policyIDs []int64
}
- pageQueryFunc := func(ctx context.Context, lastID int64,
- limit int32) ([]sqlc.ListChannelsWithPoliciesPaginatedRow,
+ pageQueryFunc := func(ctx context.Context, cursor []byte,
+ limit int32) ([]sqlc.ListPreferredChannelsPaginatedRow,
error) {
- return db.ListChannelsWithPoliciesPaginated(
- ctx, sqlc.ListChannelsWithPoliciesPaginatedParams{
- Version: int16(v),
- ID: lastID,
- Limit: limit,
+ return db.ListPreferredChannelsPaginated(
+ ctx, sqlc.ListPreferredChannelsPaginatedParams{
+ Scid: cursor,
+ Limit: limit,
},
)
}
extractPageCursor := func(
- row sqlc.ListChannelsWithPoliciesPaginatedRow) int64 {
+ row sqlc.ListPreferredChannelsPaginatedRow) []byte {
- return row.GraphChannel.ID
+ return row.GraphChannel.Scid
}
- collectFunc := func(row sqlc.ListChannelsWithPoliciesPaginatedRow) (
+ collectFunc := func(
+ row sqlc.ListPreferredChannelsPaginatedRow) (
channelBatchIDs, error) {
ids := channelBatchIDs{
channelID: row.GraphChannel.ID,
}
- // Extract policy IDs from the row.
dbPol1, dbPol2, err := extractChannelPolicies(row)
if err != nil {
return ids, err
@@ -6654,7 +7202,7 @@ func forEachChannelWithPolicies(ctx context.Context, db SQLQueries,
}
processItem := func(ctx context.Context,
- row sqlc.ListChannelsWithPoliciesPaginatedRow,
+ row sqlc.ListPreferredChannelsPaginatedRow,
batchData *batchChannelData) error {
node1, node2, err := buildNodeVertices(
@@ -6689,7 +7237,7 @@ func forEachChannelWithPolicies(ctx context.Context, db SQLQueries,
}
return sqldb.ExecuteCollectAndBatchWithSharedDataQuery(
- ctx, cfg.QueryCfg, int64(-1), pageQueryFunc, extractPageCursor,
+ ctx, cfg.QueryCfg, []byte{}, pageQueryFunc, extractPageCursor,
collectFunc, batchDataFunc, processItem,
)
}
### rpcserver.go
@@ -6265,11 +6265,10 @@ func (r *rpcServer) DescribeGraph(ctx context.Context,
}
}
- // Obtain the pointer to the V1 channel graph. This will provide a
- // consistent view of the graph due to bolt db's transactional model.
- //
- // TODO(elle): switch to a cross-version graph view when available.
- graph := r.server.v1Graph
+ // Obtain the pointer to the cross-version channel graph. This will
+ // provide a consistent view of the graph due to bolt db's
+ // transactional model.
+ graph := r.server.graphDB
// First iterate through all the known nodes (connected or unconnected
// within the graph), collating their current state into the RPC
@@ -6353,19 +6352,18 @@ func marshalDBEdge(edgeInfo *models.ChannelEdgeInfo,
includeAuthProof bool) *lnrpc.ChannelEdge {
// Make sure the policies match the node they belong to. c1 should point
- // to the policy for NodeKey1, and c2 for NodeKey2.
- if c1 != nil && c1.ChannelFlags&lnwire.ChanUpdateDirection == 1 ||
- c2 != nil && c2.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
-
+ // to the policy for NodeKey1, and c2 for NodeKey2. IsNode1 reads the
+ // direction from the field that each gossip version carries it in.
+ if c1 != nil && !c1.IsNode1() || c2 != nil && c2.IsNode1() {
c2, c1 = c1, c2
}
var lastUpdate int64
if c1 != nil {
- lastUpdate = c1.LastUpdate.Unix()
+ lastUpdate = policyLastUpdate(c1)
}
- if c2 != nil && c2.LastUpdate.Unix() > lastUpdate {
- lastUpdate = c2.LastUpdate.Unix()
+ if c2 != nil && policyLastUpdate(c2) > lastUpdate {
+ lastUpdate = policyLastUpdate(c2)
}
customRecords := marshalExtraOpaqueData(edgeInfo.ExtraOpaqueData)
@@ -6418,7 +6416,7 @@ func marshalPolicyExtraOpaqueData(data []byte) map[uint64][]byte {
func marshalDBRoutingPolicy(
policy *models.ChannelEdgePolicy) *lnrpc.RoutingPolicy {
- disabled := policy.ChannelFlags&lnwire.ChanUpdateDisabled != 0
+ disabled := policy.IsDisabled()
customRecords := marshalPolicyExtraOpaqueData(policy.ExtraOpaqueData)
inboundFee := policy.InboundFee.UnwrapOr(lnwire.Fee{})
@@ -6430,14 +6428,26 @@ func marshalDBRoutingPolicy(
FeeBaseMsat: int64(policy.FeeBaseMSat),
FeeRateMilliMsat: int64(policy.FeeProportionalMillionths),
Disabled: disabled,
- LastUpdate: uint32(policy.LastUpdate.Unix()),
+ LastUpdate: uint32(policyLastUpdate(policy)),
CustomRecords: customRecords,
InboundFeeBaseMsat: inboundFee.BaseFee,
InboundFeeRateMilliMsat: inboundFee.FeeRate,
}
}
+// policyLastUpdate returns the unix time of a policy's last update. A v2 policy
+// is ordered by the block height it was announced at rather than by a
+// timestamp, so it has no unix time, and zero is returned for it. The RPC has
+// no field for the block height yet.
+func policyLastUpdate(policy *models.ChannelEdgePolicy) int64 {
+ if policy.Version != lnwire.GossipVersion1 {
+ return 0
+ }
+
+ return policy.LastUpdate.Unix()
+}
+
// GetNodeMetrics returns all available node metrics calculated from the
// current channel graph.
func (r *rpcServer) GetNodeMetrics(ctx context.Context,
### rpcserver_test.go
@@ -7,7 +7,9 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn/v2"
+ "github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/lnrpc"
+ "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/signal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -153,3 +155,110 @@ func TestRpcCommitmentType(t *testing.T) {
})
}
}
+
+// TestMarshalDBEdgeVersions checks that the graph RPC marshalers read the
+// direction, the disabled state and the last update of a policy from the
+// fields that its gossip version carries them in.
+func TestMarshalDBEdgeVersions(t *testing.T) {
+ t.Parallel()
+
+ lastUpdate := time.Unix(1_700_000_000, 0)
+
+ tests := []struct {
+ name string
+
+ // node1 and node2 are the policies of NodeKey1 and NodeKey2.
+ // The test hands them to marshalDBEdge in both orders, so the
+ // marshaler has to place them by their direction.
+ node1, node2 *models.ChannelEdgePolicy
+
+ wantDisabled bool
+ wantLastUpdate uint32
+ }{
+ {
+ name: "v1",
+ node1: &models.ChannelEdgePolicy{
+ Version: lnwire.GossipVersion1,
+ FeeBaseMSat: 1,
+ LastUpdate: lastUpdate,
+ ChannelFlags: lnwire.
+ ChanUpdateDisabled,
+ },
+ node2: &models.ChannelEdgePolicy{
+ Version: lnwire.GossipVersion1,
+ FeeBaseMSat: 2,
+ LastUpdate: lastUpdate,
+ ChannelFlags: lnwire.ChanUpdateDirection,
+ },
+ wantDisabled: true,
+ wantLastUpdate: uint32(lastUpdate.Unix()),
+ },
+ {
+ // A v2 policy carries its direction in SecondPeer and
+ // its disabled state in DisableFlags, and leaves the
+ // v1 ChannelFlags and LastUpdate unset.
+ name: "v2",
+ node1: &models.ChannelEdgePolicy{
+ Version: lnwire.GossipVersion2,
+ FeeBaseMSat: 1,
+ DisableFlags: lnwire.ChanUpdateDisableIncoming |
+ lnwire.ChanUpdateDisableOutgoing,
+ LastBlockHeight: 800_000,
+ },
+ node2: &models.ChannelEdgePolicy{
+ Version: lnwire.GossipVersion2,
+ FeeBaseMSat: 2,
+ SecondPeer: true,
+ LastBlockHeight: 800_001,
+ },
+ wantDisabled: true,
+
+ // A v2 policy has no unix time, so the last update is
+ // zero rather than the year-2042 value that converting
+ // a zero time.Time to uint32 produces.
+ wantLastUpdate: 0,
+ },
+ }
+
+ for _, test := range tests {
+ for _, reversed := range []bool{false, true} {
+ name := fmt.Sprintf("%s/reversed=%v", test.name,
+ reversed)
+
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+
+ c1, c2 := test.node1, test.node2
+ if reversed {
+ c1, c2 = c2, c1
+ }
+
+ edge := marshalDBEdge(
+ &models.ChannelEdgeInfo{ChannelID: 1},
+ c1, c2, false,
+ )
+
+ require.EqualValues(
+ t, 1, edge.Node1Policy.FeeBaseMsat,
+ )
+ require.EqualValues(
+ t, 2, edge.Node2Policy.FeeBaseMsat,
+ )
+
+ require.Equal(
+ t, test.wantDisabled,
+ edge.Node1Policy.Disabled,
+ )
+ require.False(t, edge.Node2Policy.Disabled)
+
+ require.Equal(
+ t, test.wantLastUpdate,
+ edge.Node1Policy.LastUpdate,
+ )
+ require.Equal(
+ t, test.wantLastUpdate, edge.LastUpdate,
+ )
+ })
+ }
+ }
+}
### server.go
@@ -1041,7 +1041,17 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
IsChannelActive: s.htlcSwitch.HasActiveLink,
ApplyChannelUpdate: s.applyChannelUpdate,
DB: s.chanStateDB,
- Graph: dbs.GraphDB,
+
+ // The manager signs v1 channel updates from the stored
+ // policies, so it reads the v1 graph.
+ //
+ // TODO(gossip-v2): once lnd can build and sign v2 channel
+ // updates, enable and disable a channel on every version it is
+ // announced on, rather than on the preferred version only.
+ // Otherwise nodes on the other version keep seeing the old
+ // status. This means toggling DisableFlags for v2 instead of
+ // ChannelFlags.
+ Graph: v1Graph,
}
chanStatusMgr, err := netann.NewChanStatusManager(chanStatusMgrCfg)
@@ -1622,7 +1632,15 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
deleteAliasEdge := func(scid lnwire.ShortChannelID) (
*models.ChannelEdgePolicy, error) {
- info, e1, e2, err := s.graphDB.FetchChannelEdgesByID(
+ // The funding manager re-adds the channel with the returned
+ // policy and announces it with a v1 channel update, so the
+ // lookup and the deletion both stay on v1.
+ //
+ // TODO(gossip-v2): once the funding manager can announce
+ // channels on v2, delete the alias edge on every version it
+ // was added with, and re-add each version with that version's
+ // policy, instead of using the preferred version.
+ info, e1, e2, err := s.v1Graph.FetchChannelEdgesByID(
context.TODO(), scid.ToUint64(),
)
if errors.Is(err, graphdb.ErrEdgeNotFound) {
@@ -1646,14 +1664,15 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
ourPolicy = e2
}
- if ourPolicy == nil {
+ if info == nil || ourPolicy == nil {
// Something is wrong, so return an error.
return nil, fmt.Errorf("we don't have an edge")
}
err = s.v1Graph.DeleteChannelEdges(
context.TODO(), false, false, scid.ToUint64(),
)
+
return ourPolicy, err
}
### sqldb/migrations.go
@@ -136,6 +136,11 @@ var (
Version: 18,
SchemaVersion: 15,
},
+ {
+ Name: "000016_graph_preferred_lookups",
+ Version: 19,
+ SchemaVersion: 16,
+ },
}, migrationAdditions...)
// ErrMigrationMismatch is returned when a migrated record does not
### sqldb/migrations_dev.go
@@ -10,8 +10,8 @@ package sqldb
// migrations.go) with the next free versions when its schema is final.
var migrationAdditions = []MigrationConfig{
{
- Name: "000016_offers",
- Version: 19,
- SchemaVersion: 16,
+ Name: "000017_offers",
+ Version: 20,
+ SchemaVersion: 17,
},
}
### sqldb/sqlc/graph.sql.go
@@ -2495,6 +2495,59 @@ func (q *Queries) GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByL
return items, nil
}
+const getPreferredChannelVersionByOutpoint = `-- name: GetPreferredChannelVersionByOutpoint :one
+SELECT c.version
+FROM graph_channels c
+ JOIN graph_preferred_channels pc ON pc.channel_id = c.id
+WHERE c.outpoint = $1
+ AND c.version IN (/*SLICE:versions*/?)
+`
+
+type GetPreferredChannelVersionByOutpointParams struct {
+ Outpoint string
+ Versions []int16
+}
+
+// Return the gossip version that the preferred-channel mapping selects for the
+// channel with the given funding outpoint. Every version of a channel shares
+// its outpoint and SCID, so at most one of them has a mapping row. The caller
+// passes the gossip versions to consider, so that the lookup can use the
+// (version, outpoint) index instead of scanning every channel.
+func (q *Queries) GetPreferredChannelVersionByOutpoint(ctx context.Context, arg GetPreferredChannelVersionByOutpointParams) (int16, error) {
+ query := getPreferredChannelVersionByOutpoint
+ var queryParams []interface{}
+ queryParams = append(queryParams, arg.Outpoint)
+ if len(arg.Versions) > 0 {
+ for _, v := range arg.Versions {
+ queryParams = append(queryParams, v)
+ }
+ query = strings.Replace(query, "/*SLICE:versions*/?", makeQueryParams(len(queryParams), len(arg.Versions)), 1)
+ } else {
+ query = strings.Replace(query, "/*SLICE:versions*/?", "NULL", 1)
+ }
+ row := q.db.QueryRowContext(ctx, query, queryParams...)
+ var version int16
+ err := row.Scan(&version)
+ return version, err
+}
+
+const getPreferredChannelVersionBySCID = `-- name: GetPreferredChannelVersionBySCID :one
+SELECT c.version
+FROM graph_preferred_channels pc
+ JOIN graph_channels c ON c.id = pc.channel_id
+WHERE pc.scid = $1
+`
+
+// Return the gossip version that the preferred-channel mapping selects for an
+// SCID, so that readers follow the rule UpsertPreferredChannel applies instead
+// of ranking the versions again.
+func (q *Queries) GetPreferredChannelVersionBySCID(ctx context.Context, scid []byte) (int16, error) {
+ row := q.db.QueryRowContext(ctx, getPreferredChannelVersionBySCID, scid)
+ var version int16
+ err := row.Scan(&version)
+ return version, err
+}
+
const getPruneEntriesForHeights = `-- name: GetPruneEntriesForHeights :many
SELECT block_height, block_hash
FROM graph_prune_log
@@ -4171,38 +4224,222 @@ func (q *Queries) ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndP
return items, nil
}
-const listNodesPaginated = `-- name: ListNodesPaginated :many
-SELECT id, version, pub_key, alias, last_update, color, signature, block_height
-FROM graph_nodes
-WHERE version = $1 AND id > $2
-ORDER BY id
-LIMIT $3
+const listPreferredChannelsPaginated = `-- name: ListPreferredChannelsPaginated :many
+SELECT
+ c.id, c.version, c.scid, c.node_id_1, c.node_id_2, c.outpoint, c.capacity, c.bitcoin_key_1, c.bitcoin_key_2, c.node_1_signature, c.node_2_signature, c.bitcoin_1_signature, c.bitcoin_2_signature, c.signature, c.funding_pk_script, c.merkle_root_hash,
+
+ -- Join node pubkeys
+ n1.pub_key AS node1_pubkey,
+ n2.pub_key AS node2_pubkey,
+
+ -- Node 1 policy
+ cp1.id AS policy_1_id,
+ cp1.node_id AS policy_1_node_id,
+ cp1.version AS policy_1_version,
+ cp1.timelock AS policy_1_timelock,
+ cp1.fee_ppm AS policy_1_fee_ppm,
+ cp1.base_fee_msat AS policy_1_base_fee_msat,
+ cp1.min_htlc_msat AS policy_1_min_htlc_msat,
+ cp1.max_htlc_msat AS policy_1_max_htlc_msat,
+ cp1.last_update AS policy_1_last_update,
+ cp1.disabled AS policy_1_disabled,
+ cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
+ cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
+ cp1.message_flags AS policy1_message_flags,
+ cp1.channel_flags AS policy1_channel_flags,
+ cp1.block_height AS policy1_block_height,
+ cp1.disable_flags AS policy1_disable_flags,
+ cp1.signature AS policy_1_signature,
+
+ -- Node 2 policy
+ cp2.id AS policy_2_id,
+ cp2.node_id AS policy_2_node_id,
+ cp2.version AS policy_2_version,
+ cp2.timelock AS policy_2_timelock,
+ cp2.fee_ppm AS policy_2_fee_ppm,
+ cp2.base_fee_msat AS policy_2_base_fee_msat,
+ cp2.min_htlc_msat AS policy_2_min_htlc_msat,
+ cp2.max_htlc_msat AS policy_2_max_htlc_msat,
+ cp2.last_update AS policy_2_last_update,
+ cp2.disabled AS policy_2_disabled,
+ cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
+ cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
+ cp2.message_flags AS policy2_message_flags,
+ cp2.channel_flags AS policy2_channel_flags,
+ cp2.signature AS policy_2_signature,
+ cp2.block_height AS policy_2_block_height,
+ cp2.disable_flags AS policy_2_disable_flags
+
+FROM graph_preferred_channels pc
+JOIN graph_channels c ON c.id = pc.channel_id
+JOIN graph_nodes n1 ON c.node_id_1 = n1.id
+JOIN graph_nodes n2 ON c.node_id_2 = n2.id
+LEFT JOIN graph_channel_policies cp1
+ ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
+LEFT JOIN graph_channel_policies cp2
+ ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
+WHERE pc.scid > $1
+ORDER BY pc.scid
+LIMIT $2
`
-type ListNodesPaginatedParams struct {
- Version int16
- ID int64
- Limit int32
+type ListPreferredChannelsPaginatedParams struct {
+ Scid []byte
+ Limit int32
+}
+
+type ListPreferredChannelsPaginatedRow struct {
+ GraphChannel GraphChannel
+ Node1Pubkey []byte
+ Node2Pubkey []byte
+ Policy1ID sql.NullInt64
+ Policy1NodeID sql.NullInt64
+ Policy1Version sql.NullInt16
+ Policy1Timelock sql.NullInt32
+ Policy1FeePpm sql.NullInt64
+ Policy1BaseFeeMsat sql.NullInt64
+ Policy1MinHtlcMsat sql.NullInt64
+ Policy1MaxHtlcMsat sql.NullInt64
+ Policy1LastUpdate sql.NullInt64
+ Policy1Disabled sql.NullBool
+ Policy1InboundBaseFeeMsat sql.NullInt64
+ Policy1InboundFeeRateMilliMsat sql.NullInt64
+ Policy1MessageFlags sql.NullInt16
+ Policy1ChannelFlags sql.NullInt16
+ Policy1BlockHeight sql.NullInt64
+ Policy1DisableFlags sql.NullInt16
+ Policy1Signature []byte
+ Policy2ID sql.NullInt64
+ Policy2NodeID sql.NullInt64
+ Policy2Version sql.NullInt16
+ Policy2Timelock sql.NullInt32
+ Policy2FeePpm sql.NullInt64
+ Policy2BaseFeeMsat sql.NullInt64
+ Policy2MinHtlcMsat sql.NullInt64
+ Policy2MaxHtlcMsat sql.NullInt64
+ Policy2LastUpdate sql.NullInt64
+ Policy2Disabled sql.NullBool
+ Policy2InboundBaseFeeMsat sql.NullInt64
+ Policy2InboundFeeRateMilliMsat sql.NullInt64
+ Policy2MessageFlags sql.NullInt16
+ Policy2ChannelFlags sql.NullInt16
+ Policy2Signature []byte
+ Policy2BlockHeight sql.NullInt64
+ Policy2DisableFlags sql.NullInt16
}
-func (q *Queries) ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error) {
- rows, err := q.db.QueryContext(ctx, listNodesPaginated, arg.Version, arg.ID, arg.Limit)
+func (q *Queries) ListPreferredChannelsPaginated(ctx context.Context, arg ListPreferredChannelsPaginatedParams) ([]ListPreferredChannelsPaginatedRow, error) {
+ rows, err := q.db.QueryContext(ctx, listPreferredChannelsPaginated, arg.Scid, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
- var items []GraphNode
+ var items []ListPreferredChannelsPaginatedRow
for rows.Next() {
- var i GraphNode
+ var i ListPreferredChannelsPaginatedRow
if err := rows.Scan(
- &i.ID,
- &i.Version,
- &i.PubKey,
- &i.Alias,
- &i.LastUpdate,
- &i.Color,
- &i.Signature,
- &i.BlockHeight,
+ &i.GraphChannel.ID,
+ &i.GraphChannel.Version,
+ &i.GraphChannel.Scid,
+ &i.GraphChannel.NodeID1,
+ &i.GraphChannel.NodeID2,
+ &i.GraphChannel.Outpoint,
+ &i.GraphChannel.Capacity,
+ &i.GraphChannel.BitcoinKey1,
+ &i.GraphChannel.BitcoinKey2,
+ &i.GraphChannel.Node1Signature,
+ &i.GraphChannel.Node2Signature,
+ &i.GraphChannel.Bitcoin1Signature,
+ &i.GraphChannel.Bitcoin2Signature,
+ &i.GraphChannel.Signature,
+ &i.GraphChannel.FundingPkScript,
+ &i.GraphChannel.MerkleRootHash,
+ &i.Node1Pubkey,
+ &i.Node2Pubkey,
+ &i.Policy1ID,
+ &i.Policy1NodeID,
+ &i.Policy1Version,
+ &i.Policy1Timelock,
+ &i.Policy1FeePpm,
+ &i.Policy1BaseFeeMsat,
+ &i.Policy1MinHtlcMsat,
+ &i.Policy1MaxHtlcMsat,
+ &i.Policy1LastUpdate,
+ &i.Policy1Disabled,
+ &i.Policy1InboundBaseFeeMsat,
+ &i.Policy1InboundFeeRateMilliMsat,
+ &i.Policy1MessageFlags,
+ &i.Policy1ChannelFlags,
+ &i.Policy1BlockHeight,
+ &i.Policy1DisableFlags,
+ &i.Policy1Signature,
+ &i.Policy2ID,
+ &i.Policy2NodeID,
+ &i.Policy2Version,
+ &i.Policy2Timelock,
+ &i.Policy2FeePpm,
+ &i.Policy2BaseFeeMsat,
+ &i.Policy2MinHtlcMsat,
+ &i.Policy2MaxHtlcMsat,
+ &i.Policy2LastUpdate,
+ &i.Policy2Disabled,
+ &i.Policy2InboundBaseFeeMsat,
+ &i.Policy2InboundFeeRateMilliMsat,
+ &i.Policy2MessageFlags,
+ &i.Policy2ChannelFlags,
+ &i.Policy2Signature,
+ &i.Policy2BlockHeight,
+ &i.Policy2DisableFlags,
+ ); err != nil {
+ return nil, err
+ }
+ items = append(items, i)
+ }
+ if err := rows.Close(); err != nil {
+ return nil, err
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ return items, nil
+}
+
+const listPreferredNodesPaginated = `-- name: ListPreferredNodesPaginated :many
+SELECT n.id, n.version, n.pub_key, n.alias, n.last_update, n.color, n.signature, n.block_height
+FROM graph_preferred_nodes pn
+JOIN graph_nodes n ON n.id = pn.node_id
+WHERE pn.pub_key > $1
+ORDER BY pn.pub_key
+LIMIT $2
+`
+
+type ListPreferredNodesPaginatedParams struct {
+ PubKey []byte
+ Limit int32
+}
+
+type ListPreferredNodesPaginatedRow struct {
+ GraphNode GraphNode
+}
+
+func (q *Queries) ListPreferredNodesPaginated(ctx context.Context, arg ListPreferredNodesPaginatedParams) ([]ListPreferredNodesPaginatedRow, error) {
+ rows, err := q.db.QueryContext(ctx, listPreferredNodesPaginated, arg.PubKey, arg.Limit)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var items []ListPreferredNodesPaginatedRow
+ for rows.Next() {
+ var i ListPreferredNodesPaginatedRow
+ if err := rows.Scan(
+ &i.GraphNode.ID,
+ &i.GraphNode.Version,
+ &i.GraphNode.PubKey,
+ &i.GraphNode.Alias,
+ &i.GraphNode.LastUpdate,
+ &i.GraphNode.Color,
+ &i.GraphNode.Signature,
+ &i.GraphNode.BlockHeight,
); err != nil {
return nil, err
}
@@ -4497,6 +4734,57 @@ func (q *Queries) UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTy
return err
}
+const upsertPreferredChannel = `-- name: UpsertPreferredChannel :exec
+/* ─────────────────────────────────────────────
+ graph_preferred_channels table queries
+ ─────────────────────────────────────────────
+*/
+
+INSERT INTO graph_preferred_channels (scid, channel_id)
+SELECT c.scid, c.id
+FROM graph_channels c
+WHERE c.scid = $1
+ORDER BY
+ EXISTS (
+ SELECT 1 FROM graph_channel_policies p
+ WHERE p.channel_id = c.id AND p.version = c.version
+ ) DESC,
+ c.version DESC
+LIMIT 1
+ON CONFLICT (scid) DO UPDATE SET channel_id = EXCLUDED.channel_id
+`
+
+// Recompute the preferred channel for a given SCID and upsert the result.
+// Priority: v2 with policies > v1 with policies > v2 bare > v1 bare.
+func (q *Queries) UpsertPreferredChannel(ctx context.Context, scid []byte) error {
+ _, err := q.db.ExecContext(ctx, upsertPreferredChannel, scid)
+ return err
+}
+
+const upsertPreferredNode = `-- name: UpsertPreferredNode :exec
+/* ─────────────────────────────────────────────
+ graph_preferred_nodes table queries
+ ─────────────────────────────────────────────
+*/
+
+INSERT INTO graph_preferred_nodes (pub_key, node_id)
+SELECT n.pub_key, n.id
+FROM graph_nodes n
+WHERE n.pub_key = $1
+ORDER BY
+ (COALESCE(length(n.signature), 0) > 0) DESC,
+ n.version DESC
+LIMIT 1
+ON CONFLICT (pub_key) DO UPDATE SET node_id = EXCLUDED.node_id
+`
+
+// Recompute the preferred node for a given pub_key and upsert the result.
+// Priority: v2 announced > v1 announced > v2 shell > v1 shell.
+func (q *Queries) UpsertPreferredNode(ctx context.Context, pubKey []byte) error {
+ _, err := q.db.ExecContext(ctx, upsertPreferredNode, pubKey)
+ return err
+}
+
const upsertPruneLogEntry = `-- name: UpsertPruneLogEntry :exec
/* ───────────────────────────���─────────────────
graph_prune_log table queries
### sqldb/sqlc/migrations/000016_graph_preferred_lookups.down.sql
@@ -0,0 +1,3 @@
+DROP TABLE IF EXISTS graph_preferred_channels;
+DROP TABLE IF EXISTS graph_preferred_nodes;
+DROP INDEX IF EXISTS graph_channels_scid_version_idx;
### sqldb/sqlc/migrations/000016_graph_preferred_lookups.up.sql
@@ -0,0 +1,89 @@
+-- Preferred-node mapping: one row per unique pub_key pointing at the "best"
+-- node row across gossip versions. Priority: v2 announced > v1 announced >
+-- v2 shell > v1 shell.
+CREATE TABLE IF NOT EXISTS graph_preferred_nodes (
+ pub_key BLOB PRIMARY KEY,
+ node_id BIGINT NOT NULL REFERENCES graph_nodes(id) ON DELETE CASCADE
+);
+
+-- Index on node_id so cascade deletes from graph_nodes can locate the
+-- referencing rows without a sequential scan.
+CREATE INDEX IF NOT EXISTS graph_preferred_nodes_node_id_idx
+ ON graph_preferred_nodes (node_id);
+
+-- Preferred-channel mapping: one row per unique SCID pointing at the "best"
+-- channel row across gossip versions. Priority: v2 with policies >
+-- v1 with policies > v2 bare > v1 bare.
+CREATE TABLE IF NOT EXISTS graph_preferred_channels (
+ scid BLOB PRIMARY KEY,
+ channel_id BIGINT NOT NULL REFERENCES graph_channels(id) ON DELETE CASCADE
+);
+
+-- Index on channel_id so cascade deletes from graph_channels can locate
+-- the referencing rows without a sequential scan.
+CREATE INDEX IF NOT EXISTS graph_preferred_channels_channel_id_idx
+ ON graph_preferred_channels (channel_id);
+
+-- UpsertPreferredChannel selects all gossip versions by SCID. Keep SCID first
+-- so each channel and policy write can recompute the mapping without a table
+-- scan.
+CREATE INDEX IF NOT EXISTS graph_channels_scid_version_idx
+ ON graph_channels (scid, version);
+
+-- Populate graph_preferred_nodes from the graph_nodes rows that already
+-- existed before this migration. The inner query ranks every node row within
+-- each pub_key group. Announced nodes, identified by a non-empty signature,
+-- outrank shell nodes, and higher gossip versions win within the same
+-- announced/shell class. The outer INSERT keeps only rn = 1, leaving exactly
+-- one preferred node_id per pub_key.
+--
+-- The conflict clause makes this population step idempotent if the migration
+-- is retried after the tables were created and partially populated.
+INSERT INTO graph_preferred_nodes (pub_key, node_id)
+SELECT sub.pub_key, sub.node_id
+FROM (
+ SELECT
+ n.pub_key,
+ n.id AS node_id,
+ ROW_NUMBER() OVER (
+ PARTITION BY n.pub_key
+ ORDER BY
+ (COALESCE(length(n.signature), 0) > 0) DESC,
+ n.version DESC
+ ) AS rn
+ FROM graph_nodes n
+) sub
+WHERE sub.rn = 1
+ON CONFLICT (pub_key) DO UPDATE SET node_id = EXCLUDED.node_id
+WHERE graph_preferred_nodes.node_id <> EXCLUDED.node_id;
+
+-- Populate graph_preferred_channels from the graph_channels rows that already
+-- existed before this migration. The inner query ranks every channel row
+-- within each SCID group. A channel version with at least one policy row
+-- outranks a bare channel version, and higher gossip versions win within the
+-- same policy/bare class. The outer INSERT keeps only rn = 1, leaving exactly
+-- one preferred channel_id per SCID.
+--
+-- The conflict clause makes this population step idempotent if the migration
+-- is retried after the tables were created and partially populated.
+INSERT INTO graph_preferred_channels (scid, channel_id)
+SELECT sub.scid, sub.channel_id
+FROM (
+ SELECT
+ c.scid,
+ c.id AS channel_id,
+ ROW_NUMBER() OVER (
+ PARTITION BY c.scid
+ ORDER BY
+ EXISTS (
+ SELECT 1 FROM graph_channel_policies p
+ WHERE p.channel_id = c.id
+ AND p.version = c.version
+ ) DESC,
+ c.version DESC
+ ) AS rn
+ FROM graph_channels c
+) sub
+WHERE sub.rn = 1
+ON CONFLICT (scid) DO UPDATE SET channel_id = EXCLUDED.channel_id
+WHERE graph_preferred_channels.channel_id <> EXCLUDED.channel_id;
### sqldb/sqlc/migrations/000017_offers.down.sql
[binary or diff unavailable]
### sqldb/sqlc/migrations/000017_offers.up.sql
[binary or diff unavailable]
### sqldb/sqlc/models.go
@@ -123,6 +123,16 @@ type GraphNodeFeature struct {
FeatureBit int32
}
+type GraphPreferredChannel struct {
+ Scid []byte
+ ChannelID int64
+}
+
+type GraphPreferredNode struct {
+ PubKey []byte
+ NodeID int64
+}
+
type GraphPruneLog struct {
BlockHeight int64
BlockHash []byte
### sqldb/sqlc/querier.go
@@ -144,6 +144,16 @@ type Querier interface {
GetNodesByIDs(ctx context.Context, ids []int64) ([]GraphNode, error)
GetNodesByLastUpdateRange(ctx context.Context, arg GetNodesByLastUpdateRangeParams) ([]GraphNode, error)
GetOfferByHash(ctx context.Context, hash []byte) (Offer, error)
+ // Return the gossip version that the preferred-channel mapping selects for the
+ // channel with the given funding outpoint. Every version of a channel shares
+ // its outpoint and SCID, so at most one of them has a mapping row. The caller
+ // passes the gossip versions to consider, so that the lookup can use the
+ // (version, outpoint) index instead of scanning every channel.
+ GetPreferredChannelVersionByOutpoint(ctx context.Context, arg GetPreferredChannelVersionByOutpointParams) (int16, error)
+ // Return the gossip version that the preferred-channel mapping selects for an
+ // SCID, so that readers follow the rule UpsertPreferredChannel applies instead
+ // of ranking the versions again.
+ GetPreferredChannelVersionBySCID(ctx context.Context, scid []byte) (int16, error)
GetPruneEntriesForHeights(ctx context.Context, heights []int64) ([]GraphPruneLog, error)
GetPruneHashByHeight(ctx context.Context, blockHeight int64) ([]byte, error)
GetPruneTip(ctx context.Context) (GraphPruneLog, error)
@@ -232,7 +242,8 @@ type Querier interface {
ListChannelsWithPoliciesForCachePaginated(ctx context.Context, arg ListChannelsWithPoliciesForCachePaginatedParams) ([]ListChannelsWithPoliciesForCachePaginatedRow, error)
ListChannelsWithPoliciesPaginated(ctx context.Context, arg ListChannelsWithPoliciesPaginatedParams) ([]ListChannelsWithPoliciesPaginatedRow, error)
ListNodeIDsAndPubKeys(ctx context.Context, arg ListNodeIDsAndPubKeysParams) ([]ListNodeIDsAndPubKeysRow, error)
- ListNodesPaginated(ctx context.Context, arg ListNodesPaginatedParams) ([]GraphNode, error)
+ ListPreferredChannelsPaginated(ctx context.Context, arg ListPreferredChannelsPaginatedParams) ([]ListPreferredChannelsPaginatedRow, error)
+ ListPreferredNodesPaginated(ctx context.Context, arg ListPreferredNodesPaginatedParams) ([]ListPreferredNodesPaginatedRow, error)
NextInvoiceSettleIndex(ctx context.Context) (int64, error)
NodeExists(ctx context.Context, arg NodeExistsParams) (bool, error)
OnAMPSubInvoiceCanceled(ctx context.Context, arg OnAMPSubInvoiceCanceledParams) error
@@ -257,6 +268,12 @@ type Querier interface {
UpsertNode(ctx context.Context, arg UpsertNodeParams) (int64, error)
UpsertNodeAddress(ctx context.Context, arg UpsertNodeAddressParams) error
UpsertNodeExtraType(ctx context.Context, arg UpsertNodeExtraTypeParams) error
+ // Recompute the preferred channel for a given SCID and upsert the result.
+ // Priority: v2 with policies > v1 with policies > v2 bare > v1 bare.
+ UpsertPreferredChannel(ctx context.Context, scid []byte) error
+ // Recompute the preferred node for a given pub_key and upsert the result.
+ // Priority: v2 announced > v1 announced > v2 shell > v1 shell.
+ UpsertPreferredNode(ctx context.Context, pubKey []byte) error
UpsertPruneLogEntry(ctx context.Context, arg UpsertPruneLogEntryParams) error
// We use a separate upsert for our own node since we want to be less strict
// about the last_update field. For our own node, we always want to
### sqldb/sqlc/queries/graph.sql
@@ -73,12 +73,13 @@ FROM graph_nodes
WHERE pub_key = $1
AND version = $2;
--- name: ListNodesPaginated :many
-SELECT *
-FROM graph_nodes
-WHERE version = $1 AND id > $2
-ORDER BY id
-LIMIT $3;
+-- name: ListPreferredNodesPaginated :many
+SELECT sqlc.embed(n)
+FROM graph_preferred_nodes pn
+JOIN graph_nodes n ON n.id = pn.node_id
+WHERE pn.pub_key > $1
+ORDER BY pn.pub_key
+LIMIT $2;
-- name: ListNodeIDsAndPubKeys :many
SELECT id, pub_key
@@ -978,6 +979,64 @@ WHERE c.version = $1 AND c.id > $2
ORDER BY c.id
LIMIT $3;
+-- name: ListPreferredChannelsPaginated :many
+SELECT
+ sqlc.embed(c),
+
+ -- Join node pubkeys
+ n1.pub_key AS node1_pubkey,
+ n2.pub_key AS node2_pubkey,
+
+ -- Node 1 policy
+ cp1.id AS policy_1_id,
+ cp1.node_id AS policy_1_node_id,
+ cp1.version AS policy_1_version,
+ cp1.timelock AS policy_1_timelock,
+ cp1.fee_ppm AS policy_1_fee_ppm,
+ cp1.base_fee_msat AS policy_1_base_fee_msat,
+ cp1.min_htlc_msat AS policy_1_min_htlc_msat,
+ cp1.max_htlc_msat AS policy_1_max_htlc_msat,
+ cp1.last_update AS policy_1_last_update,
+ cp1.disabled AS policy_1_disabled,
+ cp1.inbound_base_fee_msat AS policy1_inbound_base_fee_msat,
+ cp1.inbound_fee_rate_milli_msat AS policy1_inbound_fee_rate_milli_msat,
+ cp1.message_flags AS policy1_message_flags,
+ cp1.channel_flags AS policy1_channel_flags,
+ cp1.block_height AS policy1_block_height,
+ cp1.disable_flags AS policy1_disable_flags,
+ cp1.signature AS policy_1_signature,
+
+ -- Node 2 policy
+ cp2.id AS policy_2_id,
+ cp2.node_id AS policy_2_node_id,
+ cp2.version AS policy_2_version,
+ cp2.timelock AS policy_2_timelock,
+ cp2.fee_ppm AS policy_2_fee_ppm,
+ cp2.base_fee_msat AS policy_2_base_fee_msat,
+ cp2.min_htlc_msat AS policy_2_min_htlc_msat,
+ cp2.max_htlc_msat AS policy_2_max_htlc_msat,
+ cp2.last_update AS policy_2_last_update,
+ cp2.disabled AS policy_2_disabled,
+ cp2.inbound_base_fee_msat AS policy2_inbound_base_fee_msat,
+ cp2.inbound_fee_rate_milli_msat AS policy2_inbound_fee_rate_milli_msat,
+ cp2.message_flags AS policy2_message_flags,
+ cp2.channel_flags AS policy2_channel_flags,
+ cp2.signature AS policy_2_signature,
+ cp2.block_height AS policy_2_block_height,
+ cp2.disable_flags AS policy_2_disable_flags
+
+FROM graph_preferred_channels pc
+JOIN graph_channels c ON c.id = pc.channel_id
+JOIN graph_nodes n1 ON c.node_id_1 = n1.id
+JOIN graph_nodes n2 ON c.node_id_2 = n2.id
+LEFT JOIN graph_channel_policies cp1
+ ON cp1.channel_id = c.id AND cp1.node_id = c.node_id_1 AND cp1.version = c.version
+LEFT JOIN graph_channel_policies cp2
+ ON cp2.channel_id = c.id AND cp2.node_id = c.node_id_2 AND cp2.version = c.version
+WHERE pc.scid > $1
+ORDER BY pc.scid
+LIMIT $2;
+
-- name: ListChannelsWithPoliciesForCachePaginated :many
SELECT
c.id as id,
@@ -1435,3 +1494,63 @@ ON CONFLICT (channel_id, node_id, version)
channel_flags = EXCLUDED.channel_flags,
signature = EXCLUDED.signature
RETURNING id;
+
+/* ─────────────────────────────────────────────
+ graph_preferred_nodes table queries
+ ─────────────────────────────────────────────
+*/
+
+-- name: UpsertPreferredNode :exec
+-- Recompute the preferred node for a given pub_key and upsert the result.
+-- Priority: v2 announced > v1 announced > v2 shell > v1 shell.
+INSERT INTO graph_preferred_nodes (pub_key, node_id)
+SELECT n.pub_key, n.id
+FROM graph_nodes n
+WHERE n.pub_key = $1
+ORDER BY
+ (COALESCE(length(n.signature), 0) > 0) DESC,
+ n.version DESC
+LIMIT 1
+ON CONFLICT (pub_key) DO UPDATE SET node_id = EXCLUDED.node_id;
+
+/* ─────────────────────────────────────────────
+ graph_preferred_channels table queries
+ ─────────────────────────────────────────────
+*/
+
+-- name: UpsertPreferredChannel :exec
+-- Recompute the preferred channel for a given SCID and upsert the result.
+-- Priority: v2 with policies > v1 with policies > v2 bare > v1 bare.
+INSERT INTO graph_preferred_channels (scid, channel_id)
+SELECT c.scid, c.id
+FROM graph_channels c
+WHERE c.scid = $1
+ORDER BY
+ EXISTS (
+ SELECT 1 FROM graph_channel_policies p
+ WHERE p.channel_id = c.id AND p.version = c.version
+ ) DESC,
+ c.version DESC
+LIMIT 1
+ON CONFLICT (scid) DO UPDATE SET channel_id = EXCLUDED.channel_id;
+
+-- name: GetPreferredChannelVersionBySCID :one
+-- Return the gossip version that the preferred-channel mapping selects for an
+-- SCID, so that readers follow the rule UpsertPreferredChannel applies instead
+-- of ranking the versions again.
+SELECT c.version
+FROM graph_preferred_channels pc
+ JOIN graph_channels c ON c.id = pc.channel_id
+WHERE pc.scid = $1;
+
+-- name: GetPreferredChannelVersionByOutpoint :one
+-- Return the gossip version that the preferred-channel mapping selects for the
+-- channel with the given funding outpoint. Every version of a channel shares
+-- its outpoint and SCID, so at most one of them has a mapping row. The caller
+-- passes the gossip versions to consider, so that the lookup can use the
+-- (version, outpoint) index instead of scanning every channel.
+SELECT c.version
+FROM graph_channels c
+ JOIN graph_preferred_channels pc ON pc.channel_id = c.id
+WHERE c.outpoint = sqlc.arg(outpoint)
+ AND c.version IN (sqlc.slice('versions')/*SLICE:versions*/);Why this scored 40/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.