channeldb: let AddrsForNode indicate if the node was found or not
Before this commit, the `(channeldb.DB).AddrsForNode` method treats the results from the channel db and the graph db slightly differently. It errors out if the channel db is unaware of the node in question but does not error out if the graph is unaware of the node. So this commit changes the logic slightly so that a "node-unknown" error from either backing source is not seen as an error.
This commit is contained in:
7 files changed
+112
-49
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
@@ -10,8 +11,9 @@ import (
|
||||
// node. It may combine the results of multiple address sources.
|
||||
type AddrSource interface {
|
||||
// AddrsForNode returns all known addresses for the target node public
|
||||
// key.
|
||||
AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error)
|
||||
// key. The returned boolean must indicate if the given node is unknown
|
||||
// to the backing source.
|
||||
AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr, error)
|
||||
}
|
||||
|
||||
// multiAddrSource is an implementation of AddrSource which gathers all the
|
||||
@@ -31,22 +33,35 @@ func NewMultiAddrSource(sources ...AddrSource) AddrSource {
|
||||
}
|
||||
|
||||
// AddrsForNode returns all known addresses for the target node public key. It
|
||||
// queries all the address sources provided and de-duplicates the results.
|
||||
// queries all the address sources provided and de-duplicates the results. The
|
||||
// returned boolean is false only if none of the backing sources know of the
|
||||
// node.
|
||||
//
|
||||
// NOTE: this implements the AddrSource interface.
|
||||
func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
|
||||
error) {
|
||||
func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) (bool,
|
||||
[]net.Addr, error) {
|
||||
|
||||
if len(c.sources) == 0 {
|
||||
return false, nil, errors.New("no address sources")
|
||||
}
|
||||
|
||||
// The multiple address sources will likely contain duplicate addresses,
|
||||
// so we use a map here to de-dup them.
|
||||
dedupedAddrs := make(map[string]net.Addr)
|
||||
|
||||
// known will be set to true if any backing source is aware of the node.
|
||||
var known bool
|
||||
|
||||
// Iterate over all the address sources and query each one for the
|
||||
// addresses it has for the node in question.
|
||||
for _, src := range c.sources {
|
||||
addrs, err := src.AddrsForNode(nodePub)
|
||||
isKnown, addrs, err := src.AddrsForNode(nodePub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
if isKnown {
|
||||
known = true
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
@@ -60,7 +75,7 @@ func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
return addrs, nil
|
||||
return known, addrs, nil
|
||||
}
|
||||
|
||||
// A compile-time check to ensure that multiAddrSource implements the AddrSource
|
||||
|
||||
Reference in new issue
Block a user