From ebe0755a97153b12eff823509fc71d74d9f7fe95 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 13 Nov 2023 14:38:28 +0200 Subject: [PATCH] channeldb: make MarkEdgeLive logic reusable This commit is a set-up commit. It extracts the logic from `MarkEdgeLive` to a helper `markEdgeLive` method so that the logic can be called from within other kvdb.Update blocks. This will be used in the next commit. --- channeldb/graph.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/channeldb/graph.go b/channeldb/graph.go index 5d5cb45df..0a1b233e8 100644 --- a/channeldb/graph.go +++ b/channeldb/graph.go @@ -3580,10 +3580,17 @@ func markEdgeZombie(zombieIndex kvdb.RwBucket, chanID uint64, pubKey1, // MarkEdgeLive clears an edge from our zombie index, deeming it as live. func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error { + return c.markEdgeLive(nil, chanID) +} + +// markEdgeLive clears an edge from the zombie index. This method can be called +// with an existing kvdb.RwTx or the argument can be set to nil in which case a +// new transaction will be created. +func (c *ChannelGraph) markEdgeLive(tx kvdb.RwTx, chanID uint64) error { c.cacheMu.Lock() defer c.cacheMu.Unlock() - err := kvdb.Update(c.db, func(tx kvdb.RwTx) error { + dbFn := func(tx kvdb.RwTx) error { edges := tx.ReadWriteBucket(edgeBucket) if edges == nil { return ErrGraphNoEdgesFound @@ -3601,7 +3608,16 @@ func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error { } return zombieIndex.Delete(k[:]) - }, func() {}) + } + + // If the transaction is nil, we'll create a new one. Otherwise, we use + // the existing transaction + var err error + if tx == nil { + err = kvdb.Update(c.db, dbFn, func() {}) + } else { + err = dbFn(tx) + } if err != nil { return err } @@ -3611,11 +3627,12 @@ func (c *ChannelGraph) MarkEdgeLive(chanID uint64) error { // We need to add the channel back into our graph cache, otherwise we // won't use it for path finding. - edgeInfos, err := c.FetchChanInfos([]uint64{chanID}) - if err != nil { - return err - } if c.graphCache != nil { + edgeInfos, err := c.FetchChanInfos([]uint64{chanID}) + if err != nil { + return err + } + for _, edgeInfo := range edgeInfos { c.graphCache.AddChannel( edgeInfo.Info, edgeInfo.Policy1,