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

lightningnetwork / lnd / 12425886916

20 Dec 2024 05:06AM UTC coverage: 49.675% (-7.9%) from 57.578%
12425886916

Pull #9227

github

yyforyongyu
lntest+itest: export `DeriveFundingShim`
Pull Request #9227: Beat [5/4]: fix itests for `blockbeat`

45 of 49 new or added lines in 8 files covered. (91.84%)

26491 existing lines in 430 files now uncovered.

101120 of 203562 relevant lines covered (49.68%)

2.06 hits per line

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

55.56
/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 {
4✔
48
        // Exit early if no budget config is set.
4✔
49
        if b == nil {
4✔
UNCOV
50
                return fmt.Errorf("no budget config set")
×
UNCOV
51
        }
×
52

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

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

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

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

×
UNCOV
88
                return fmt.Errorf("nodeadlinehtlcratio must be at least %v",
×
UNCOV
89
                        MinBudgetRatio)
×
UNCOV
90
        }
×
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 {
4✔
107
        return &BudgetConfig{
4✔
108
                ToLocalRatio:        DefaultBudgetRatio,
4✔
109
                AnchorCPFPRatio:     DefaultBudgetRatio,
4✔
110
                DeadlineHTLCRatio:   DefaultBudgetRatio,
4✔
111
                NoDeadlineHTLCRatio: DefaultBudgetRatio,
4✔
112
        }
4✔
113
}
4✔
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 {
4✔
120

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

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

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

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

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