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

lightningnetwork / lnd / 15736109134

18 Jun 2025 02:46PM UTC coverage: 58.197% (-10.1%) from 68.248%
15736109134

Pull #9752

github

web-flow
Merge d2634a68c into 31c74f20f
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 13 new or added lines in 2 files covered. (46.15%)

28331 existing lines in 455 files now uncovered.

97860 of 168153 relevant lines covered (58.2%)

1.81 hits per line

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

10.34
/autopilot/externalscoreattach.go
1
package autopilot
2

3
import (
4
        "context"
5
        "fmt"
6
        "sync"
7

8
        "github.com/btcsuite/btcd/btcutil"
9
)
10

11
// ExternalScoreAttachment is an implementation of the AttachmentHeuristic
12
// interface that allows an external source to provide it with node scores.
13
type ExternalScoreAttachment struct {
14
        // TODO(halseth): persist across restarts.
15
        nodeScores map[NodeID]float64
16

17
        sync.Mutex
18
}
19

20
// NewExternalScoreAttachment creates a new instance of an
21
// ExternalScoreAttachment.
22
func NewExternalScoreAttachment() *ExternalScoreAttachment {
3✔
23
        return &ExternalScoreAttachment{}
3✔
24
}
3✔
25

26
// A compile time assertion to ensure ExternalScoreAttachment meets the
27
// AttachmentHeuristic and ScoreSettable interfaces.
28
var _ AttachmentHeuristic = (*ExternalScoreAttachment)(nil)
29
var _ ScoreSettable = (*ExternalScoreAttachment)(nil)
30

31
// Name returns the name of this heuristic.
32
//
33
// NOTE: This is a part of the AttachmentHeuristic interface.
34
func (s *ExternalScoreAttachment) Name() string {
3✔
35
        return "externalscore"
3✔
36
}
3✔
37

38
// SetNodeScores is used to set the internal map from NodeIDs to scores. The
39
// passed scores must be in the range [0, 1.0]. The fist parameter is the name
40
// of the targeted heuristic, to allow recursively target specific
41
// sub-heuristics. The returned boolean indicates whether the targeted
42
// heuristic was found.
43
//
44
// NOTE: This is a part of the ScoreSettable interface.
45
func (s *ExternalScoreAttachment) SetNodeScores(targetHeuristic string,
UNCOV
46
        newScores map[NodeID]float64) (bool, error) {
×
UNCOV
47

×
UNCOV
48
        // Return if this heuristic wasn't targeted.
×
UNCOV
49
        if targetHeuristic != s.Name() {
×
UNCOV
50
                return false, nil
×
UNCOV
51
        }
×
52

53
        // Since there's a requirement that all score are in the range [0,
54
        // 1.0], we validate them before setting the internal list.
UNCOV
55
        for nID, s := range newScores {
×
UNCOV
56
                if s < 0 || s > 1.0 {
×
57
                        return false, fmt.Errorf("invalid score %v for "+
×
58
                                "nodeID %v", s, nID)
×
59
                }
×
60
        }
61

UNCOV
62
        s.Lock()
×
UNCOV
63
        defer s.Unlock()
×
UNCOV
64

×
UNCOV
65
        s.nodeScores = newScores
×
UNCOV
66
        log.Tracef("Setting %v external scores", len(s.nodeScores))
×
UNCOV
67

×
UNCOV
68
        return true, nil
×
69
}
70

71
// NodeScores is a method that given the current channel graph and current set
72
// of local channels, scores the given nodes according to the preference of
73
// opening a channel of the given size with them. The returned channel
74
// candidates maps the NodeID to a NodeScore for the node.
75
//
76
// The returned scores will be in the range [0, 1.0], where 0 indicates no
77
// improvement in connectivity if a channel is opened to this node, while 1.0
78
// is the maximum possible improvement in connectivity.
79
//
80
// The scores are determined by checking the internal node scores list. Nodes
81
// not known will get a score of 0.
82
//
83
// NOTE: This is a part of the AttachmentHeuristic interface.
84
func (s *ExternalScoreAttachment) NodeScores(_ context.Context, g ChannelGraph,
85
        chans []LocalChannel, chanSize btcutil.Amount,
UNCOV
86
        nodes map[NodeID]struct{}) (map[NodeID]*NodeScore, error) {
×
UNCOV
87

×
UNCOV
88
        existingPeers := make(map[NodeID]struct{})
×
UNCOV
89
        for _, c := range chans {
×
90
                existingPeers[c.Node] = struct{}{}
×
91
        }
×
92

UNCOV
93
        s.Lock()
×
UNCOV
94
        defer s.Unlock()
×
UNCOV
95

×
UNCOV
96
        log.Tracef("External scoring %v nodes, from %v set scores",
×
UNCOV
97
                len(nodes), len(s.nodeScores))
×
UNCOV
98

×
UNCOV
99
        // Fill the map of candidates to return.
×
UNCOV
100
        candidates := make(map[NodeID]*NodeScore)
×
UNCOV
101
        for nID := range nodes {
×
UNCOV
102
                var score float64
×
UNCOV
103
                if nodeScore, ok := s.nodeScores[nID]; ok {
×
UNCOV
104
                        score = nodeScore
×
UNCOV
105
                }
×
106

107
                // If the node is among or existing channel peers, we don't
108
                // need another channel.
UNCOV
109
                if _, ok := existingPeers[nID]; ok {
×
110
                        log.Tracef("Skipping existing peer %x from external "+
×
111
                                "score results", nID[:])
×
112
                        continue
×
113
                }
114

UNCOV
115
                log.Tracef("External score %v given to node %x", score, nID[:])
×
UNCOV
116

×
UNCOV
117
                // Instead of adding a node with score 0 to the returned set,
×
UNCOV
118
                // we just skip it.
×
UNCOV
119
                if score == 0 {
×
UNCOV
120
                        continue
×
121
                }
122

UNCOV
123
                candidates[nID] = &NodeScore{
×
UNCOV
124
                        NodeID: nID,
×
UNCOV
125
                        Score:  score,
×
UNCOV
126
                }
×
127
        }
128

UNCOV
129
        return candidates, nil
×
130
}
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