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

lightningnetwork / lnd / 14193549836

01 Apr 2025 10:40AM UTC coverage: 69.046% (+0.007%) from 69.039%
14193549836

Pull #9665

github

web-flow
Merge e8825f209 into b01f4e514
Pull Request #9665: kvdb: bump etcd libs to v3.5.12

133439 of 193262 relevant lines covered (69.05%)

22119.45 hits per line

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

76.92
/lnwallet/chainfee/minfeemanager.go
1
package chainfee
2

3
import (
4
        "sync"
5
        "time"
6
)
7

8
const defaultUpdateInterval = 10 * time.Minute
9

10
// minFeeManager is used to store and update the minimum fee that is required
11
// by a transaction to be accepted to the mempool. The minFeeManager ensures
12
// that the backend used to fetch the fee is not queried too regularly.
13
type minFeeManager struct {
14
        mu                sync.Mutex
15
        minFeePerKW       SatPerKWeight
16
        lastUpdatedTime   time.Time
17
        minUpdateInterval time.Duration
18
        fetchFeeFunc      fetchFee
19
}
20

21
// fetchFee represents a function that can be used to fetch a fee.
22
type fetchFee func() (SatPerKWeight, error)
23

24
// newMinFeeManager creates a new minFeeManager and uses the
25
// given fetchMinFee function to set the minFeePerKW of the minFeeManager.
26
// This function requires the fetchMinFee function to succeed.
27
func newMinFeeManager(minUpdateInterval time.Duration,
28
        fetchMinFee fetchFee) (*minFeeManager, error) {
1✔
29

1✔
30
        minFee, err := fetchMinFee()
1✔
31
        if err != nil {
1✔
32
                return nil, err
×
33
        }
×
34

35
        // Ensure that the minimum fee we use is always clamped by our fee
36
        // floor.
37
        if minFee < FeePerKwFloor {
2✔
38
                minFee = FeePerKwFloor
1✔
39
        }
1✔
40

41
        return &minFeeManager{
1✔
42
                minFeePerKW:       minFee,
1✔
43
                lastUpdatedTime:   time.Now(),
1✔
44
                minUpdateInterval: minUpdateInterval,
1✔
45
                fetchFeeFunc:      fetchMinFee,
1✔
46
        }, nil
1✔
47
}
48

49
// fetchMinFee returns the stored minFeePerKW if it has been updated recently
50
// or if the call to the chain backend fails. Otherwise, it sets the stored
51
// minFeePerKW to the fee returned from the backend and floors it based on
52
// our fee floor.
53
func (m *minFeeManager) fetchMinFee() SatPerKWeight {
2✔
54
        m.mu.Lock()
2✔
55
        defer m.mu.Unlock()
2✔
56

2✔
57
        if time.Since(m.lastUpdatedTime) < m.minUpdateInterval {
3✔
58
                return m.minFeePerKW
1✔
59
        }
1✔
60

61
        newMinFee, err := m.fetchFeeFunc()
1✔
62
        if err != nil {
1✔
63
                log.Errorf("Unable to fetch updated min fee from chain "+
×
64
                        "backend. Using last known min fee instead: %v", err)
×
65

×
66
                return m.minFeePerKW
×
67
        }
×
68

69
        // By default, we'll use the backend node's minimum fee as the
70
        // minimum fee rate we'll propose for transactions. However, if this
71
        // happens to be lower than our fee floor, we'll enforce that instead.
72
        m.minFeePerKW = newMinFee
1✔
73
        if m.minFeePerKW < FeePerKwFloor {
1✔
74
                m.minFeePerKW = FeePerKwFloor
×
75
        }
×
76
        m.lastUpdatedTime = time.Now()
1✔
77

1✔
78
        log.Debugf("Using minimum fee rate of %v sat/kw",
1✔
79
                int64(m.minFeePerKW))
1✔
80

1✔
81
        return m.minFeePerKW
1✔
82
}
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