• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12069189652

28 Nov 2024 12:47PM UTC coverage: 58.936% (-0.003%) from 58.939%
12069189652

push

github

web-flow
Merge pull request #9236 from ellemouton/moveGraphDBCode

[1/3] Graph RIP: refactor+graph: move all graph related DB code to the graph package

505 of 618 new or added lines in 48 files covered. (81.72%)

102 existing lines in 17 files now uncovered.

133419 of 226381 relevant lines covered (58.94%)

19519.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/channeldb/addr_source.go
1
package channeldb
2

3
import (
4
        "errors"
5
        "net"
6

7
        "github.com/btcsuite/btcd/btcec/v2"
8
)
9

10
// AddrSource is an interface that allow us to get the addresses for a target
11
// node. It may combine the results of multiple address sources.
12
type AddrSource interface {
13
        // AddrsForNode returns all known addresses for the target node public
14
        // key. The returned boolean must indicate if the given node is unknown
15
        // to the backing source.
16
        AddrsForNode(nodePub *btcec.PublicKey) (bool, []net.Addr, error)
17
}
18

19
// multiAddrSource is an implementation of AddrSource which gathers all the
20
// known addresses for a given node from multiple backends and de-duplicates the
21
// results.
22
type multiAddrSource struct {
23
        sources []AddrSource
24
}
25

26
// NewMultiAddrSource constructs a new AddrSource which will query all the
27
// provided sources for a node's addresses and will then de-duplicate the
28
// results.
29
func NewMultiAddrSource(sources ...AddrSource) AddrSource {
7✔
30
        return &multiAddrSource{
7✔
31
                sources: sources,
7✔
32
        }
7✔
33
}
7✔
34

35
// AddrsForNode returns all known addresses for the target node public key. It
36
// queries all the address sources provided and de-duplicates the results. The
37
// returned boolean is false only if none of the backing sources know of the
38
// node.
39
//
40
// NOTE: this implements the AddrSource interface.
41
func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) (bool,
42
        []net.Addr, error) {
7✔
43

7✔
44
        if len(c.sources) == 0 {
7✔
NEW
45
                return false, nil, errors.New("no address sources")
×
NEW
46
        }
×
47

48
        // The multiple address sources will likely contain duplicate addresses,
49
        // so we use a map here to de-dup them.
50
        dedupedAddrs := make(map[string]net.Addr)
7✔
51

7✔
52
        // known will be set to true if any backing source is aware of the node.
7✔
53
        var known bool
7✔
54

7✔
55
        // Iterate over all the address sources and query each one for the
7✔
56
        // addresses it has for the node in question.
7✔
57
        for _, src := range c.sources {
18✔
58
                isKnown, addrs, err := src.AddrsForNode(nodePub)
11✔
59
                if err != nil {
11✔
NEW
60
                        return false, nil, err
×
NEW
61
                }
×
62

63
                if isKnown {
19✔
64
                        known = true
8✔
65
                }
8✔
66

67
                for _, addr := range addrs {
21✔
68
                        dedupedAddrs[addr.String()] = addr
10✔
69
                }
10✔
70
        }
71

72
        // Convert the map into a list we can return.
73
        addrs := make([]net.Addr, 0, len(dedupedAddrs))
7✔
74
        for _, addr := range dedupedAddrs {
16✔
75
                addrs = append(addrs, addr)
9✔
76
        }
9✔
77

78
        return known, addrs, nil
7✔
79
}
80

81
// A compile-time check to ensure that multiAddrSource implements the AddrSource
82
// interface.
83
var _ AddrSource = (*multiAddrSource)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc