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

lightningnetwork / lnd / 13408822928

19 Feb 2025 08:59AM UTC coverage: 41.123% (-17.7%) from 58.794%
13408822928

Pull #9521

github

web-flow
Merge d2f397b3c into 0e8786348
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

92496 of 224923 relevant lines covered (41.12%)

18825.83 hits per line

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

88.89
/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 {
10✔
48
        // Exit early if no budget config is set.
10✔
49
        if b == nil {
11✔
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 {
10✔
55
                return fmt.Errorf("tolocal must be at least %v",
1✔
56
                        MinBudgetValue)
1✔
57
        }
1✔
58
        if b.ToLocalRatio != 0 && b.ToLocalRatio < MinBudgetRatio {
9✔
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 {
8✔
64
                return fmt.Errorf("anchorcpfp must be at least %v",
1✔
65
                        MinBudgetValue)
1✔
66
        }
1✔
67
        if b.AnchorCPFPRatio != 0 && b.AnchorCPFPRatio < MinBudgetRatio {
7✔
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 {
6✔
73
                return fmt.Errorf("deadlinehtlc must be at least %v",
1✔
74
                        MinBudgetValue)
1✔
75
        }
1✔
76
        if b.DeadlineHTLCRatio != 0 && b.DeadlineHTLCRatio < MinBudgetRatio {
5✔
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 {
4✔
82
                return fmt.Errorf("nodeadlinehtlc must be at least %v",
1✔
83
                        MinBudgetValue)
1✔
84
        }
1✔
85
        if b.NoDeadlineHTLCRatio != 0 &&
2✔
86
                b.NoDeadlineHTLCRatio < MinBudgetRatio {
3✔
87

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

92
        return nil
1✔
93
}
94

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

105
// DefaultSweeperConfig returns the default configuration for the sweeper.
106
func DefaultBudgetConfig() *BudgetConfig {
101✔
107
        return &BudgetConfig{
101✔
108
                ToLocalRatio:        DefaultBudgetRatio,
101✔
109
                AnchorCPFPRatio:     DefaultBudgetRatio,
101✔
110
                DeadlineHTLCRatio:   DefaultBudgetRatio,
101✔
111
                NoDeadlineHTLCRatio: DefaultBudgetRatio,
101✔
112
        }
101✔
113
}
101✔
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 {
59✔
120

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

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

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

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

136
        return budget
58✔
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