From 898e4ef854b53e8ebe70e95b15a6caf02456e029 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 3 Feb 2026 17:26:08 +0200 Subject: [PATCH] graph/db: add gossip version aliases Introduce gossipV1 and gossipV2 package-level aliases in sql_store.go to reduce verbosity in version switch statements. Leave hard-coded v1/v2 call sites untouched so remaining upgrades are obvious. In other words, it is now easy to see where our remaining work in the sql_store.go file is by just searching for instances of `lnwire.GossipVersion1`. --- graph/db/sql_store.go | 78 +++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index a6b05dad6..c219082ee 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -33,6 +33,11 @@ import ( "github.com/lightningnetwork/lnd/tor" ) +const ( + gossipV1 = lnwire.GossipVersion1 + gossipV2 = lnwire.GossipVersion2 +) + // SQLQueries is a subset of the sqlc.Querier interface that can be used to // execute queries against the SQL graph tables. // @@ -305,7 +310,7 @@ func (s *SQLStore) HasV1Node(ctx context.Context, err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { dbNode, err := db.GetNodeByPubKey( ctx, sqlc.GetNodeByPubKeyParams{ - Version: int16(lnwire.GossipVersion1), + Version: int16(gossipV1), PubKey: pubKey[:], }, ) @@ -869,11 +874,11 @@ func (s *SQLStore) updateEdgeCache(e *models.ChannelEdgePolicy, // during the next query for this edge. if entry, ok := s.rejectCache.get(e.Version, e.ChannelID); ok { switch e.Version { - case lnwire.GossipVersion1: + case gossipV1: updateRejectCacheEntryV1( &entry, isUpdate1, e.LastUpdate, ) - case lnwire.GossipVersion2: + case gossipV2: updateRejectCacheEntryV2( &entry, isUpdate1, e.LastBlockHeight, ) @@ -2242,7 +2247,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, // We'll query the cache with the shared lock held to allow multiple // readers to access values in the cache concurrently if they exist. s.cacheMu.RLock() - if entry, ok := s.rejectCache.get(lnwire.GossipVersion1, chanID); ok { + if entry, ok := s.rejectCache.get(gossipV1, chanID); ok { s.cacheMu.RUnlock() node1LastUpdate = time.Unix(entry.upd1Time, 0) node2LastUpdate = time.Unix(entry.upd2Time, 0) @@ -2258,7 +2263,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, // The item was not found with the shared lock, so we'll acquire the // exclusive lock and check the cache again in case another method added // the entry to the cache while no lock was held. - if entry, ok := s.rejectCache.get(lnwire.GossipVersion1, chanID); ok { + if entry, ok := s.rejectCache.get(gossipV1, chanID); ok { node1LastUpdate = time.Unix(entry.upd1Time, 0) node2LastUpdate = time.Unix(entry.upd2Time, 0) exists, isZombie = entry.flags.unpack() @@ -2271,7 +2276,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, channel, err := db.GetChannelBySCID( ctx, sqlc.GetChannelBySCIDParams{ Scid: chanIDB, - Version: int16(lnwire.GossipVersion1), + Version: int16(gossipV1), }, ) if errors.Is(err, sql.ErrNoRows) { @@ -2279,7 +2284,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, isZombie, err = db.IsZombieChannel( ctx, sqlc.IsZombieChannelParams{ Scid: chanIDB, - Version: int16(lnwire.GossipVersion1), + Version: int16(gossipV1), }, ) if err != nil { @@ -2296,7 +2301,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, policy1, err := db.GetChannelPolicyByChannelAndNode( ctx, sqlc.GetChannelPolicyByChannelAndNodeParams{ - Version: int16(lnwire.GossipVersion1), + Version: int16(gossipV1), ChannelID: channel.ID, NodeID: channel.NodeID1, }, @@ -2310,7 +2315,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, policy2, err := db.GetChannelPolicyByChannelAndNode( ctx, sqlc.GetChannelPolicyByChannelAndNodeParams{ - Version: int16(lnwire.GossipVersion1), + Version: int16(gossipV1), ChannelID: channel.ID, NodeID: channel.NodeID2, }, @@ -2330,7 +2335,7 @@ func (s *SQLStore) HasV1ChannelEdge(chanID uint64) (time.Time, time.Time, bool, } s.rejectCache.insert( - lnwire.GossipVersion1, chanID, + gossipV1, chanID, newRejectCacheEntryV1( node1LastUpdate, node2LastUpdate, exists, isZombie, @@ -2427,13 +2432,13 @@ func (s *SQLStore) HasChannelEdge(v lnwire.GossipVersion, err) } else if err == nil { switch v { - case lnwire.GossipVersion1: + case gossipV1: if policy1.LastUpdate.Valid { node1LastUpdate = time.Unix( policy1.LastUpdate.Int64, 0, ) } - case lnwire.GossipVersion2: + case gossipV2: if policy1.BlockHeight.Valid { node1Block = uint32( policy1.BlockHeight.Int64, @@ -2454,13 +2459,13 @@ func (s *SQLStore) HasChannelEdge(v lnwire.GossipVersion, err) } else if err == nil { switch v { - case lnwire.GossipVersion1: + case gossipV1: if policy2.LastUpdate.Valid { node2LastUpdate = time.Unix( policy2.LastUpdate.Int64, 0, ) } - case lnwire.GossipVersion2: + case gossipV2: if policy2.BlockHeight.Valid { node2Block = uint32( policy2.BlockHeight.Int64, @@ -2478,11 +2483,11 @@ func (s *SQLStore) HasChannelEdge(v lnwire.GossipVersion, var entry rejectCacheEntry switch v { - case lnwire.GossipVersion1: + case gossipV1: entry = newRejectCacheEntryV1( node1LastUpdate, node2LastUpdate, exists, isZombie, ) - case lnwire.GossipVersion2: + case gossipV2: entry = newRejectCacheEntryV2( node1Block, node2Block, exists, isZombie, ) @@ -2544,9 +2549,9 @@ func (s *SQLStore) IsPublicNode(v lnwire.GossipVersion, pubKey [33]byte) (bool, err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error { var err error switch v { - case lnwire.GossipVersion1: + case gossipV1: isPublic, err = db.IsPublicV1Node(ctx, pubKey[:]) - case lnwire.GossipVersion2: + case gossipV2: isPublic, err = db.IsPublicV2Node(ctx, pubKey[:]) } @@ -3205,7 +3210,7 @@ func (s *SQLStore) AddEdgeProof(scid lnwire.ShortChannelID, err error ) switch proof.Version { - case lnwire.GossipVersion1: + case gossipV1: res, err = db.AddV1ChannelProof( ctx, sqlc.AddV1ChannelProofParams{ Scid: scidBytes, @@ -3216,7 +3221,7 @@ func (s *SQLStore) AddEdgeProof(scid lnwire.ShortChannelID, }, ) - case lnwire.GossipVersion2: + case gossipV2: res, err = db.AddV2ChannelProof( ctx, sqlc.AddV2ChannelProofParams{ Scid: scidBytes, @@ -3691,7 +3696,8 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries, Signature: edge.SigBytes, } - if version == lnwire.GossipVersion1 { + switch version { + case gossipV1: params.LastUpdate = sqldb.SQLInt64(edge.LastUpdate.Unix()) params.Disabled = sql.NullBool{ Valid: true, @@ -3701,7 +3707,7 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries, Valid: edge.MessageFlags.HasMaxHtlc(), Int64: int64(edge.MaxHTLC), } - } else { + case gossipV2: params.BlockHeight = sqldb.SQLInt64( int64(edge.LastBlockHeight), ) @@ -3718,7 +3724,7 @@ func updateChanEdgePolicy(ctx context.Context, tx SQLQueries, // Convert the flat extra opaque data into a map of TLV types to // values. extra := edge.ExtraSignedFields - if version == lnwire.GossipVersion1 { + if version == gossipV1 { extra, err = marshalExtraOpaqueData(edge.ExtraOpaqueData) if err != nil { return node1Pub, node2Pub, false, fmt.Errorf( @@ -3794,9 +3800,9 @@ func buildNode(ctx context.Context, cfg *sqldb.QueryConfig, db SQLQueries, // and supported. func isKnownGossipVersion(v lnwire.GossipVersion) bool { switch v { - case lnwire.GossipVersion1: + case gossipV1: return true - case lnwire.GossipVersion2: + case gossipV2: return true default: return false @@ -3870,7 +3876,7 @@ func buildNodeWithBatchData(dbNode sqlc.GraphNode, // Use preloaded extra fields. if extraFields, exists := batchData.extraFields[dbNode.ID]; exists { - if v == lnwire.GossipVersion1 { + if v == gossipV1 { records := lnwire.CustomRecords(extraFields) recs, err := records.Serialize() if err != nil { @@ -3963,7 +3969,7 @@ func upsertNodeAncillaryData(ctx context.Context, db SQLQueries, // Convert the flat extra opaque data into a map of TLV types to // values. extra := node.ExtraSignedFields - if node.Version == lnwire.GossipVersion1 { + if node.Version == gossipV1 { extra, err = marshalExtraOpaqueData(node.ExtraOpaqueData) if err != nil { return fmt.Errorf("unable to marshal extra opaque "+ @@ -4002,10 +4008,10 @@ func populateNodeParams(node *models.Node, }) switch node.Version { - case lnwire.GossipVersion1: + case gossipV1: lastUpdate = sqldb.SQLInt64(node.LastUpdate.Unix()) - case lnwire.GossipVersion2: + case gossipV2: lastBlockHeight = sqldb.SQLInt64(int64(node.LastBlockHeight)) default: @@ -4582,7 +4588,7 @@ func insertChannel(ctx context.Context, db SQLQueries, // Finally, insert any extra TLV fields in the channel announcement. extra := edge.ExtraSignedFields - if v == lnwire.GossipVersion1 { + if v == gossipV1 { extra, err = marshalExtraOpaqueData(edge.ExtraOpaqueData) if err != nil { return fmt.Errorf("unable to marshal extra opaque "+ @@ -4728,7 +4734,7 @@ func buildEdgeInfoWithBatchData(chain chainhash.Hash, // Build the appropriate channel based on version. var channel *models.ChannelEdgeInfo switch v { - case lnwire.GossipVersion1: + case gossipV1: // For v1, serialize extras into ExtraOpaqueData. recs, err := lnwire.CustomRecords(extras).Serialize() if err != nil { @@ -4777,7 +4783,7 @@ func buildEdgeInfoWithBatchData(chain chainhash.Hash, ) } - case lnwire.GossipVersion2: + case gossipV2: v2Fields := &models.ChannelV2Fields{ ExtraSignedFields: extras, } @@ -4879,14 +4885,14 @@ func getAndBuildChanPolicies(ctx context.Context, cfg *sqldb.QueryConfig, // TODO(elle): update to support v2 policies. if dbPol1 != nil && - lnwire.GossipVersion(dbPol1.Version) != lnwire.GossipVersion1 { + lnwire.GossipVersion(dbPol1.Version) != gossipV1 { return nil, nil, fmt.Errorf("unsupported policy1 version: %d", dbPol1.Version) } if dbPol2 != nil && - lnwire.GossipVersion(dbPol2.Version) != lnwire.GossipVersion1 { + lnwire.GossipVersion(dbPol2.Version) != gossipV1 { return nil, nil, fmt.Errorf("unsupported policy2 version: %d", dbPol2.Version) @@ -4991,7 +4997,7 @@ func buildChanPolicy(isNode1 bool, dbPolicy sqlc.GraphChannelPolicy, InboundFee: inboundFee, } - if p.Version == lnwire.GossipVersion1 { + if p.Version != gossipV2 { recs, err := lnwire.CustomRecords(extras).Serialize() if err != nil { return nil, fmt.Errorf("unable to serialize extra "+ @@ -6358,7 +6364,7 @@ func handleZombieMarking(ctx context.Context, db SQLQueries, if strictZombiePruning { // TODO(elle): update for V2 last update times. - if v != lnwire.GossipVersion1 { + if v != gossipV1 { return fmt.Errorf("strict zombie pruning only "+ "supported for gossip v1, got %v", v) }