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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

100.0
/contractcourt/config.go
1
package contractcourt
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/btcutil"
7
)
8

9
const (
10
        // MinBudgetValue is the minimal budget that we allow when configuring
11
        // the budget used in sweeping outputs. The actual budget can be lower
12
        // if the user decides to NOT set this value.
13
        //
14
        // NOTE: This value is chosen so the linear fee function can increase
15
        // at least 1 sat/kw per block.
16
        MinBudgetValue btcutil.Amount = 1008
17

18
        // MinBudgetRatio is the minimal ratio that we allow when configuring
19
        // the budget ratio used in sweeping outputs.
20
        MinBudgetRatio = 0.001
21

22
        // DefaultBudgetRatio defines a default budget ratio to be used when
23
        // sweeping inputs. This is a large value, which is fine as the final
24
        // fee rate is capped at the max fee rate configured.
25
        DefaultBudgetRatio = 0.5
26
)
27

28
// BudgetConfig is a struct that holds the configuration when offering outputs
29
// to the sweeper.
30
//
31
//nolint:ll
32
type BudgetConfig struct {
33
        ToLocal      btcutil.Amount `long:"tolocal" description:"The amount in satoshis to allocate as the budget to pay fees when sweeping the to_local output. If set, the budget calculated using the ratio (if set) will be capped at this value."`
34
        ToLocalRatio float64        `long:"tolocalratio" description:"The ratio of the value in to_local output to allocate as the budget to pay fees when sweeping it."`
35

36
        AnchorCPFP      btcutil.Amount `long:"anchorcpfp" description:"The amount in satoshis to allocate as the budget to pay fees when CPFPing a force close tx using the anchor output. If set, the budget calculated using the ratio (if set) will be capped at this value."`
37
        AnchorCPFPRatio float64        `long:"anchorcpfpratio" description:"The ratio of a special value to allocate as the budget to pay fees when CPFPing a force close tx using the anchor output. The special value is the sum of all time-sensitive HTLCs on this commitment subtracted by their budgets."`
38

39
        DeadlineHTLC      btcutil.Amount `long:"deadlinehtlc" description:"The amount in satoshis to allocate as the budget to pay fees when sweeping a time-sensitive (first-level) HTLC. If set, the budget calculated using the ratio (if set) will be capped at this value."`
40
        DeadlineHTLCRatio float64        `long:"deadlinehtlcratio" description:"The ratio of the value in a time-sensitive (first-level) HTLC to allocate as the budget to pay fees when sweeping it."`
41

42
        NoDeadlineHTLC      btcutil.Amount `long:"nodeadlinehtlc" description:"The amount in satoshis to allocate as the budget to pay fees when sweeping a non-time-sensitive (second-level) HTLC. If set, the budget calculated using the ratio (if set) will be capped at this value."`
43
        NoDeadlineHTLCRatio float64        `long:"nodeadlinehtlcratio" description:"The ratio of the value in a non-time-sensitive (second-level) HTLC to allocate as the budget to pay fees when sweeping it."`
44
}
45

46
// Validate checks the budget configuration for any invalid values.
47
func (b *BudgetConfig) Validate() error {
13✔
48
        // Exit early if no budget config is set.
13✔
49
        if b == nil {
14✔
50
                return fmt.Errorf("no budget config set")
1✔
51
        }
1✔
52

53
        // Sanity check all fields.
54
        if b.ToLocal != 0 && b.ToLocal < MinBudgetValue {
13✔
55
                return fmt.Errorf("tolocal must be at least %v",
1✔
56
                        MinBudgetValue)
1✔
57
        }
1✔
58
        if b.ToLocalRatio != 0 && b.ToLocalRatio < MinBudgetRatio {
12✔
59
                return fmt.Errorf("tolocalratio must be at least %v",
1✔
60
                        MinBudgetRatio)
1✔
61
        }
1✔
62

63
        if b.AnchorCPFP != 0 && b.AnchorCPFP < MinBudgetValue {
11✔
64
                return fmt.Errorf("anchorcpfp must be at least %v",
1✔
65
                        MinBudgetValue)
1✔
66
        }
1✔
67
        if b.AnchorCPFPRatio != 0 && b.AnchorCPFPRatio < MinBudgetRatio {
10✔
68
                return fmt.Errorf("anchorcpfpratio must be at least %v",
1✔
69
                        MinBudgetRatio)
1✔
70
        }
1✔
71

72
        if b.DeadlineHTLC != 0 && b.DeadlineHTLC < MinBudgetValue {
9✔
73
                return fmt.Errorf("deadlinehtlc must be at least %v",
1✔
74
                        MinBudgetValue)
1✔
75
        }
1✔
76
        if b.DeadlineHTLCRatio != 0 && b.DeadlineHTLCRatio < MinBudgetRatio {
8✔
77
                return fmt.Errorf("deadlinehtlcratio must be at least %v",
1✔
78
                        MinBudgetRatio)
1✔
79
        }
1✔
80

81
        if b.NoDeadlineHTLC != 0 && b.NoDeadlineHTLC < MinBudgetValue {
7✔
82
                return fmt.Errorf("nodeadlinehtlc must be at least %v",
1✔
83
                        MinBudgetValue)
1✔
84
        }
1✔
85
        if b.NoDeadlineHTLCRatio != 0 &&
5✔
86
                b.NoDeadlineHTLCRatio < MinBudgetRatio {
6✔
87

1✔
88
                return fmt.Errorf("nodeadlinehtlcratio must be at least %v",
1✔
89
                        MinBudgetRatio)
1✔
90
        }
1✔
91

92
        return nil
4✔
93
}
94

95
// String returns a human-readable description of the budget configuration.
96
func (b *BudgetConfig) String() string {
4✔
97
        return fmt.Sprintf("tolocal=%v tolocalratio=%v anchorcpfp=%v "+
4✔
98
                "anchorcpfpratio=%v deadlinehtlc=%v deadlinehtlcratio=%v "+
4✔
99
                "nodeadlinehtlc=%v nodeadlinehtlcratio=%v",
4✔
100
                b.ToLocal, b.ToLocalRatio, b.AnchorCPFP, b.AnchorCPFPRatio,
4✔
101
                b.DeadlineHTLC, b.DeadlineHTLCRatio, b.NoDeadlineHTLC,
4✔
102
                b.NoDeadlineHTLCRatio)
4✔
103
}
4✔
104

105
// DefaultSweeperConfig returns the default configuration for the sweeper.
106
func DefaultBudgetConfig() *BudgetConfig {
105✔
107
        return &BudgetConfig{
105✔
108
                ToLocalRatio:        DefaultBudgetRatio,
105✔
109
                AnchorCPFPRatio:     DefaultBudgetRatio,
105✔
110
                DeadlineHTLCRatio:   DefaultBudgetRatio,
105✔
111
                NoDeadlineHTLCRatio: DefaultBudgetRatio,
105✔
112
        }
105✔
113
}
105✔
114

115
// calculateBudget takes an output value, a configured ratio and budget value,
116
// and returns the budget to use for sweeping the output. If the budget value
117
// is set, it will be used as cap.
118
func calculateBudget(value btcutil.Amount, ratio float64,
119
        max btcutil.Amount) btcutil.Amount {
62✔
120

62✔
121
        // If ratio is not set, using the default value.
62✔
122
        if ratio == 0 {
63✔
123
                ratio = DefaultBudgetRatio
1✔
124
        }
1✔
125

126
        budget := value.MulF64(ratio)
62✔
127

62✔
128
        log.Tracef("Calculated budget=%v using value=%v, ratio=%v, cap=%v",
62✔
129
                budget, value, ratio, max)
62✔
130

62✔
131
        if max != 0 && budget > max {
63✔
132
                log.Debugf("Calculated budget=%v is capped at %v", budget, max)
1✔
133
                return max
1✔
134
        }
1✔
135

136
        return budget
61✔
137
}
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