channeldb: optimise FetchChannel method

This commit adds a small optimisation to the FetchChannel method.
Instead of iterating over each channel bucket, an identifiable error is
thrown once the wanted channel is found so that the iteration can stop
early.
This commit is contained in:
Elle Mouton
2023-02-02 10:59:15 +02:00
parent aebdd2375c
commit 908cb6060b

View File

@@ -661,6 +661,10 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) (
var ( var (
targetChan *OpenChannel targetChan *OpenChannel
targetChanPoint bytes.Buffer targetChanPoint bytes.Buffer
// errChanFound is used to signal that the channel has been
// found so that iteration through the DB buckets can stop.
errChanFound = errors.New("channel found")
) )
if err := writeOutpoint(&targetChanPoint, &chanPoint); err != nil { if err := writeOutpoint(&targetChanPoint, &chanPoint); err != nil {
@@ -739,7 +743,7 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) (
targetChan = channel targetChan = channel
targetChan.Db = c targetChan.Db = c
return nil return errChanFound
}) })
}) })
} }
@@ -750,7 +754,7 @@ func (c *ChannelStateDB) FetchChannel(tx kvdb.RTx, chanPoint wire.OutPoint) (
} else { } else {
err = chanScan(tx) err = chanScan(tx)
} }
if err != nil { if err != nil && !errors.Is(err, errChanFound) {
return nil, err return nil, err
} }