• 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

20.0
/autopilot/agent_constraints.go
1
package autopilot
2

3
import (
4
        "github.com/btcsuite/btcd/btcutil"
5
)
6

7
// AgentConstraints is an interface the agent will query to determine what
8
// limits it will need to stay inside when opening channels.
9
type AgentConstraints interface {
10
        // ChannelBudget should, given the passed parameters, return whether
11
        // more channels can be opened while still staying within the set
12
        // constraints. If the constraints allow us to open more channels, then
13
        // the first return value will represent the amount of additional funds
14
        // available towards creating channels. The second return value is the
15
        // exact *number* of additional channels available.
16
        ChannelBudget(chans []LocalChannel, balance btcutil.Amount) (
17
                btcutil.Amount, uint32)
18

19
        // MaxPendingOpens returns the maximum number of pending channel
20
        // establishment goroutines that can be lingering. We cap this value in
21
        // order to control the level of parallelism caused by the autopilot
22
        // agent.
23
        MaxPendingOpens() uint16
24

25
        // MinChanSize returns the smallest channel that the autopilot agent
26
        // should create.
27
        MinChanSize() btcutil.Amount
28

29
        // MaxChanSize returns largest channel that the autopilot agent should
30
        // create.
31
        MaxChanSize() btcutil.Amount
32
}
33

34
// agentConstraints is an implementation of the AgentConstraints interface that
35
// indicate the constraints the autopilot agent must adhere to when opening
36
// channels.
37
type agentConstraints struct {
38
        // minChanSize is the smallest channel that the autopilot agent should
39
        // create.
40
        minChanSize btcutil.Amount
41

42
        // maxChanSize is the largest channel that the autopilot agent should
43
        // create.
44
        maxChanSize btcutil.Amount
45

46
        // chanLimit is the maximum number of channels that should be created.
47
        chanLimit uint16
48

49
        // allocation is the percentage of total funds that should be committed
50
        // to automatic channel establishment.
51
        allocation float64
52

53
        // maxPendingOpens is the maximum number of pending channel
54
        // establishment goroutines that can be lingering. We cap this value in
55
        // order to control the level of parallelism caused by the autopilot
56
        // agent.
57
        maxPendingOpens uint16
58
}
59

60
// A compile time assertion to ensure agentConstraints satisfies the
61
// AgentConstraints interface.
62
var _ AgentConstraints = (*agentConstraints)(nil)
63

64
// NewConstraints returns a new AgentConstraints with the given limits.
65
func NewConstraints(minChanSize, maxChanSize btcutil.Amount, chanLimit,
66
        maxPendingOpens uint16, allocation float64) AgentConstraints {
3✔
67

3✔
68
        return &agentConstraints{
3✔
69
                minChanSize:     minChanSize,
3✔
70
                maxChanSize:     maxChanSize,
3✔
71
                chanLimit:       chanLimit,
3✔
72
                allocation:      allocation,
3✔
73
                maxPendingOpens: maxPendingOpens,
3✔
74
        }
3✔
75
}
3✔
76

77
// ChannelBudget should, given the passed parameters, return whether more
78
// channels can be be opened while still staying within the set constraints.
79
// If the constraints allow us to open more channels, then the first return
80
// value will represent the amount of additional funds available towards
81
// creating channels. The second return value is the exact *number* of
82
// additional channels available.
83
//
84
// Note: part of the AgentConstraints interface.
85
func (h *agentConstraints) ChannelBudget(channels []LocalChannel,
UNCOV
86
        funds btcutil.Amount) (btcutil.Amount, uint32) {
×
UNCOV
87

×
UNCOV
88
        // If we're already over our maximum allowed number of channels, then
×
UNCOV
89
        // we'll instruct the controller not to create any more channels.
×
UNCOV
90
        if len(channels) >= int(h.chanLimit) {
×
UNCOV
91
                return 0, 0
×
UNCOV
92
        }
×
93

94
        // The number of additional channels that should be opened is the
95
        // difference between the channel limit, and the number of channels we
96
        // already have open.
UNCOV
97
        numAdditionalChans := uint32(h.chanLimit) - uint32(len(channels))
×
UNCOV
98

×
UNCOV
99
        // First, we'll tally up the total amount of funds that are currently
×
UNCOV
100
        // present within the set of active channels.
×
UNCOV
101
        var totalChanAllocation btcutil.Amount
×
UNCOV
102
        for _, channel := range channels {
×
UNCOV
103
                totalChanAllocation += channel.Balance
×
UNCOV
104
        }
×
105

106
        // With this value known, we'll now compute the total amount of fund
107
        // allocated across regular utxo's and channel utxo's.
UNCOV
108
        totalFunds := funds + totalChanAllocation
×
UNCOV
109

×
UNCOV
110
        // Once the total amount has been computed, we then calculate the
×
UNCOV
111
        // fraction of funds currently allocated to channels.
×
UNCOV
112
        fundsFraction := float64(totalChanAllocation) / float64(totalFunds)
×
UNCOV
113

×
UNCOV
114
        // If this fraction is below our threshold, then we'll return true, to
×
UNCOV
115
        // indicate the controller should call Select to obtain a candidate set
×
UNCOV
116
        // of channels to attempt to open.
×
UNCOV
117
        needMore := fundsFraction < h.allocation
×
UNCOV
118
        if !needMore {
×
UNCOV
119
                return 0, 0
×
UNCOV
120
        }
×
121

122
        // Now that we know we need more funds, we'll compute the amount of
123
        // additional funds we should allocate towards channels.
UNCOV
124
        targetAllocation := btcutil.Amount(float64(totalFunds) * h.allocation)
×
UNCOV
125
        fundsAvailable := targetAllocation - totalChanAllocation
×
UNCOV
126
        return fundsAvailable, numAdditionalChans
×
127
}
128

129
// MaxPendingOpens returns the maximum number of pending channel establishment
130
// goroutines that can be lingering. We cap this value in order to control the
131
// level of parallelism caused by the autopilot agent.
132
//
133
// Note: part of the AgentConstraints interface.
134
func (h *agentConstraints) MaxPendingOpens() uint16 {
×
135
        return h.maxPendingOpens
×
136
}
×
137

138
// MinChanSize returns the smallest channel that the autopilot agent should
139
// create.
140
//
141
// Note: part of the AgentConstraints interface.
142
func (h *agentConstraints) MinChanSize() btcutil.Amount {
×
143
        return h.minChanSize
×
144
}
×
145

146
// MaxChanSize returns largest channel that the autopilot agent should create.
147
//
148
// Note: part of the AgentConstraints interface.
149
func (h *agentConstraints) MaxChanSize() btcutil.Amount {
×
150
        return h.maxChanSize
×
151
}
×
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