From 311a14a0b66e9a8ac93c8033d01485bc1f680c2e Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 19 Feb 2025 07:03:20 -0300 Subject: [PATCH] graph/db: fix linter issues of old code Since we have renamed a file housing some very old code, the linter has now run on all this code for the first time. So we gotta do some clean-up work here to make it happy. --- docs/release-notes/release-notes-0.19.0.md | 2 + graph/db/kv_store.go | 101 +++++++++++++-------- 2 files changed, 64 insertions(+), 39 deletions(-) diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index 118c1fde5..1c8cefeaf 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -262,6 +262,8 @@ The underlying functionality between those two options remain the same. - [Abstract autopilot access](https://github.com/lightningnetwork/lnd/pull/9480) - [Abstract invoicerpc server access](https://github.com/lightningnetwork/lnd/pull/9516) - [Refactor to hide DB transactions](https://github.com/lightningnetwork/lnd/pull/9513) + - Move the graph cache out of the graph CRUD layer: + - [1](https://github.com/lightningnetwork/lnd/pull/9533) * [Golang was updated to `v1.22.11`](https://github.com/lightningnetwork/lnd/pull/9462). diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index dfe856419..6f327d73b 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -132,8 +132,8 @@ var ( // // The chanID represents the channel ID of the edge and the direction is // one byte representing the direction of the edge. The main purpose of - // this index is to allow pruning disabled channels in a fast way without - // the need to iterate all over the graph. + // this index is to allow pruning disabled channels in a fast way + // without the need to iterate all over the graph. disabledEdgePolicyBucket = []byte("disabled-edge-policy-index") // graphMetaBucket is a top-level bucket which stores various meta-deta @@ -308,7 +308,7 @@ func (c *KVStore) getChannelMap(edges kvdb.RBucket) ( switch { // If the db policy was missing an expected optional field, we // return nil as if the policy was unknown. - case err == ErrEdgePolicyOptionalFieldNotFound: + case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound): return nil case err != nil: @@ -340,10 +340,13 @@ func (c *KVStore) Wipe() error { err := kvdb.Update(c.db, func(tx kvdb.RwTx) error { for _, tlb := range graphTopLevelBuckets { err := tx.DeleteTopLevelBucket(tlb) - if err != nil && err != kvdb.ErrBucketNotFound { + if err != nil && + !errors.Is(err, kvdb.ErrBucketNotFound) { + return err } } + return nil }, func() {}) if err != nil { @@ -395,6 +398,7 @@ func initKVStore(db kvdb.Backend) error { graphMeta := tx.ReadWriteBucket(graphMetaBucket) _, err = graphMeta.CreateBucketIfNotExists(pruneLogBucket) + return err }, func() {}) if err != nil { @@ -550,6 +554,7 @@ func (c *KVStore) forEachNodeDirectedChannel(tx kvdb.RTx, return cb(directedChannel) } + return nodeTraversal(tx, node[:], c.db, dbCallback) } @@ -565,14 +570,14 @@ func (c *KVStore) fetchNodeFeatures(tx kvdb.RTx, // Fallback that uses the database. targetNode, err := c.FetchLightningNodeTx(tx, node) - switch err { + switch { // If the node exists and has features, return them directly. - case nil: + case err == nil: return targetNode.Features, nil // If we couldn't find a node announcement, populate a blank feature // vector. - case ErrGraphNodeNotFound: + case errors.Is(err, ErrGraphNodeNotFound): return lnwire.EmptyFeatureVector(), nil // Otherwise, bubble the error up. @@ -915,7 +920,7 @@ func (c *KVStore) SetSourceNode(node *models.LightningNode) error { // already present node from a node announcement, or to insert a node found in a // channel update. // -// TODO(roasbeef): also need sig of announcement +// TODO(roasbeef): also need sig of announcement. func (c *KVStore) AddLightningNode(node *models.LightningNode, op ...batch.SchedulerOption) error { @@ -984,6 +989,7 @@ func (c *KVStore) LookupAlias(pub *btcec.PublicKey) (string, error) { // TODO(roasbeef): should actually be using the utf-8 // package... alias = string(a) + return nil }, func() { alias = "" @@ -1076,7 +1082,7 @@ func (c *KVStore) AddChannelEdge(edge *models.ChannelEdgeInfo, // Silence ErrEdgeAlreadyExist so that the batch can // succeed, but propagate the error via local state. - if err == ErrEdgeAlreadyExist { + if errors.Is(err, ErrEdgeAlreadyExist) { alreadyExists = true return nil } @@ -1150,7 +1156,7 @@ func (c *KVStore) addChannelEdge(tx kvdb.RwTx, // public key, so subsequent validation and queries can work properly. _, node1Err := fetchLightningNode(nodes, edge.NodeKey1Bytes[:]) switch { - case node1Err == ErrGraphNodeNotFound: + case errors.Is(node1Err, ErrGraphNodeNotFound): node1Shell := models.LightningNode{ PubKeyBytes: edge.NodeKey1Bytes, HaveNodeAnnouncement: false, @@ -1166,7 +1172,7 @@ func (c *KVStore) addChannelEdge(tx kvdb.RwTx, _, node2Err := fetchLightningNode(nodes, edge.NodeKey2Bytes[:]) switch { - case node2Err == ErrGraphNodeNotFound: + case errors.Is(node2Err, ErrGraphNodeNotFound): node2Shell := models.LightningNode{ PubKeyBytes: edge.NodeKey2Bytes, HaveNodeAnnouncement: false, @@ -1206,6 +1212,7 @@ func (c *KVStore) addChannelEdge(tx kvdb.RwTx, if err := WriteOutpoint(&b, &edge.ChannelPoint); err != nil { return err } + return chanIndex.Put(b.Bytes(), chanKey[:]) } @@ -1233,6 +1240,7 @@ func (c *KVStore) HasChannelEdge( upd1Time = time.Unix(entry.upd1Time, 0) upd2Time = time.Unix(entry.upd2Time, 0) exists, isZombie = entry.flags.unpack() + return upd1Time, upd2Time, exists, isZombie, nil } c.cacheMu.RUnlock() @@ -1247,6 +1255,7 @@ func (c *KVStore) HasChannelEdge( upd1Time = time.Unix(entry.upd1Time, 0) upd2Time = time.Unix(entry.upd2Time, 0) exists, isZombie = entry.flags.unpack() + return upd1Time, upd2Time, exists, isZombie, nil } @@ -1791,8 +1800,8 @@ func (c *KVStore) PruneTip() (*chainhash.Hash, uint32, error) { // Once we have the prune tip, the value will be the block hash, // and the key the block height. - copy(tipHash[:], v[:]) - tipHeight = byteOrder.Uint32(k[:]) + copy(tipHash[:], v) + tipHeight = byteOrder.Uint32(k) return nil }, func() {}) @@ -1946,11 +1955,12 @@ func (c *KVStore) HighestChanID() (uint64, error) { // Otherwise, we'll de serialize the channel ID and return it // to the caller. cid = byteOrder.Uint64(lastChanID) + return nil }, func() { cid = 0 }) - if err != nil && err != ErrGraphNoEdgesFound { + if err != nil && !errors.Is(err, ErrGraphNoEdgesFound) { return 0, err } @@ -2035,7 +2045,6 @@ func (c *KVStore) ChanUpdatesInHorizon(startTime, //nolint:ll for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil && bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() { - // We have a new eligible entry, so we'll slice of the // chan ID so we can query it in the DB. chanID := indexKey[8:] @@ -2052,6 +2061,7 @@ func (c *KVStore) ChanUpdatesInHorizon(startTime, hits++ edgesSeen[chanIDInt] = struct{}{} edgesInHorizon = append(edgesInHorizon, channel) + continue } @@ -2110,9 +2120,9 @@ func (c *KVStore) ChanUpdatesInHorizon(startTime, edgesInHorizon = nil }) switch { - case err == ErrGraphNoEdgesFound: + case errors.Is(err, ErrGraphNoEdgesFound): fallthrough - case err == ErrGraphNodesNotFound: + case errors.Is(err, ErrGraphNodesNotFound): break case err != nil: @@ -2170,7 +2180,6 @@ func (c *KVStore) NodeUpdatesInHorizon(startTime, //nolint:ll for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil && bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() { - nodePub := indexKey[8:] node, err := fetchLightningNode(nodes, nodePub) if err != nil { @@ -2185,9 +2194,9 @@ func (c *KVStore) NodeUpdatesInHorizon(startTime, nodesInHorizon = nil }) switch { - case err == ErrGraphNoEdgesFound: + case errors.Is(err, ErrGraphNoEdgesFound): fallthrough - case err == ErrGraphNodesNotFound: + case errors.Is(err, ErrGraphNodesNotFound): break case err != nil: @@ -2294,7 +2303,7 @@ func (c *KVStore) FilterKnownChanIDs(chansInfo []ChannelUpdateInfo, switch { // If we don't know of any edges yet, then we'll return the entire set // of chan IDs specified. - case err == ErrGraphNoEdgesFound: + case errors.Is(err, ErrGraphNoEdgesFound): ogChanIDs := make([]uint64, len(chansInfo)) for i, info := range chansInfo { ogChanIDs[i] = info.ShortChannelID.ToUint64() @@ -2482,7 +2491,7 @@ func (c *KVStore) FilterChannelRange(startHeight, switch { // If we don't know of any channels yet, then there's nothing to // filter, so we'll return an empty slice. - case err == ErrGraphNoEdgesFound || len(channelsPerBlock) == 0: + case errors.Is(err, ErrGraphNoEdgesFound) || len(channelsPerBlock) == 0: return nil, nil case err != nil: @@ -2596,6 +2605,7 @@ func (c *KVStore) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) ( Node2: &node2, }) } + return nil } @@ -2990,7 +3000,7 @@ func (c *KVStore) isPublic(tx kvdb.RTx, nodePub route.Vertex, // Otherwise, we'll continue our search. return nil }) - if err != nil && err != errDone { + if err != nil && !errors.Is(err, errDone) { return false, err } @@ -3113,6 +3123,7 @@ func (c *KVStore) HasLightningNode(nodePub [33]byte) (time.Time, bool, exists = true updateTime = node.LastUpdate + return nil }, func() { updateTime = time.Time{} @@ -3382,6 +3393,7 @@ func (c *KVStore) FetchChannelEdgesByOutpoint(op *wire.OutPoint) ( policy1 = e1 policy2 = e2 + return nil }, func() { edgeInfo = nil @@ -3466,6 +3478,7 @@ func (c *KVStore) FetchChannelEdgesByID(chanID uint64) ( NodeKey1Bytes: pubKey1, NodeKey2Bytes: pubKey2, } + return ErrZombieEdge } @@ -3487,13 +3500,14 @@ func (c *KVStore) FetchChannelEdgesByID(chanID uint64) ( policy1 = e1 policy2 = e2 + return nil }, func() { edgeInfo = nil policy1 = nil policy2 = nil }) - if err == ErrZombieEdge { + if errors.Is(err, ErrZombieEdge) { return edgeInfo, nil, nil, err } if err != nil { @@ -3523,6 +3537,7 @@ func (c *KVStore) IsPublicNode(pubKey [33]byte) (bool, error) { } nodeIsPublic, err = c.isPublic(tx, node.PubKeyBytes, ourPubKey) + return err }, func() { nodeIsPublic = false @@ -3783,6 +3798,7 @@ func (c *KVStore) IsZombieEdge(chanID uint64) (bool, [33]byte, [33]byte) { } isZombie, pubKey1, pubKey2 = isZombieEdge(zombieIndex, chanID) + return nil }, func() { isZombie = false @@ -3936,8 +3952,8 @@ func (c *nodeTraverserSession) FetchNodeFeatures(nodePub route.Vertex) ( return c.db.fetchNodeFeatures(c.tx, nodePub) } -func putLightningNode(nodeBucket kvdb.RwBucket, aliasBucket kvdb.RwBucket, // nolint:dupl - updateIndex kvdb.RwBucket, node *models.LightningNode) error { +func putLightningNode(nodeBucket, aliasBucket, updateIndex kvdb.RwBucket, + node *models.LightningNode) error { var ( scratch [16]byte @@ -4074,6 +4090,7 @@ func fetchLightningNode(nodeBucket kvdb.RBucket, } nodeReader := bytes.NewReader(nodeBytes) + return deserializeLightningNode(nodeReader) } @@ -4219,8 +4236,8 @@ func deserializeLightningNode(r io.Reader) (models.LightningNode, error) { r, 0, MaxAllowedExtraOpaqueBytes, "blob", ) switch { - case err == io.ErrUnexpectedEOF: - case err == io.EOF: + case errors.Is(err, io.ErrUnexpectedEOF): + case errors.Is(err, io.EOF): case err != nil: return models.LightningNode{}, err } @@ -4306,6 +4323,7 @@ func fetchChanEdgeInfo(edgeIndex kvdb.RBucket, } edgeInfoReader := bytes.NewReader(edgeInfoBytes) + return deserializeChanEdgeInfo(edgeInfoReader) } @@ -4377,8 +4395,8 @@ func deserializeChanEdgeInfo(r io.Reader) (models.ChannelEdgeInfo, error) { r, 0, MaxAllowedExtraOpaqueBytes, "blob", ) switch { - case err == io.ErrUnexpectedEOF: - case err == io.EOF: + case errors.Is(err, io.ErrUnexpectedEOF): + case errors.Is(err, io.EOF): case err != nil: return models.ChannelEdgeInfo{}, err } @@ -4415,7 +4433,7 @@ func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy, // An unknown policy value does not have a update time recorded, so // it also does not need to be removed. if edgeBytes := edges.Get(edgeKey[:]); edgeBytes != nil && - !bytes.Equal(edgeBytes[:], unknownPolicy) { + !bytes.Equal(edgeBytes, unknownPolicy) { // In order to delete the old entry, we'll need to obtain the // *prior* update time in order to delete it. To do this, we'll @@ -4429,7 +4447,9 @@ func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy, oldEdgePolicy, err := deserializeChanEdgePolicy( bytes.NewReader(edgeBytes), ) - if err != nil && err != ErrEdgePolicyOptionalFieldNotFound { + if err != nil && + !errors.Is(err, ErrEdgePolicyOptionalFieldNotFound) { + return err } @@ -4457,7 +4477,7 @@ func putChanEdgePolicy(edges kvdb.RwBucket, edge *models.ChannelEdgePolicy, return err } - return edges.Put(edgeKey[:], b.Bytes()[:]) + return edges.Put(edgeKey[:], b.Bytes()) } // updateEdgePolicyDisabledIndex is used to update the disabledEdgePolicyIndex @@ -4514,7 +4534,7 @@ func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte, var edgeKey [33 + 8]byte copy(edgeKey[:], nodePub) - copy(edgeKey[33:], chanID[:]) + copy(edgeKey[33:], chanID) edgeBytes := edges.Get(edgeKey[:]) if edgeBytes == nil { @@ -4522,7 +4542,7 @@ func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte, } // No need to deserialize unknown policy. - if bytes.Equal(edgeBytes[:], unknownPolicy) { + if bytes.Equal(edgeBytes, unknownPolicy) { return nil, nil } @@ -4532,7 +4552,7 @@ func fetchChanEdgePolicy(edges kvdb.RBucket, chanID []byte, switch { // If the db policy was missing an expected optional field, we return // nil as if the policy was unknown. - case err == ErrEdgePolicyOptionalFieldNotFound: + case errors.Is(err, ErrEdgePolicyOptionalFieldNotFound): return nil, nil case err != nil: @@ -4642,6 +4662,7 @@ func serializeChanEdgePolicy(w io.Writer, edge *models.ChannelEdgePolicy, if err := wire.WriteVarBytes(w, 0, opaqueBuf.Bytes()); err != nil { return err } + return nil } @@ -4650,7 +4671,7 @@ func deserializeChanEdgePolicy(r io.Reader) (*models.ChannelEdgePolicy, error) { // found, both an error and a populated policy object are returned. edge, deserializeErr := deserializeChanEdgePolicyRaw(r) if deserializeErr != nil && - deserializeErr != ErrEdgePolicyOptionalFieldNotFound { + !errors.Is(deserializeErr, ErrEdgePolicyOptionalFieldNotFound) { return nil, deserializeErr } @@ -4716,8 +4737,8 @@ func deserializeChanEdgePolicyRaw(r io.Reader) (*models.ChannelEdgePolicy, r, 0, MaxAllowedExtraOpaqueBytes, "blob", ) switch { - case err == io.ErrUnexpectedEOF: - case err == io.EOF: + case errors.Is(err, io.ErrUnexpectedEOF): + case errors.Is(err, io.EOF): case err != nil: return nil, err } @@ -4818,12 +4839,14 @@ func MakeTestGraph(t testing.TB, modifiers ...OptionModifier) (*ChannelGraph, backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr") if err != nil { backendCleanup() + return nil, err } graph, err := NewChannelGraph(backend) if err != nil { backendCleanup() + return nil, err }