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

lightningnetwork / lnd / 17190810624

24 Aug 2025 03:58PM UTC coverage: 66.74% (+9.4%) from 57.321%
17190810624

Pull #10167

github

web-flow
Merge 33cec4f6a into 0c2f045f5
Pull Request #10167: multi: bump Go to 1.24.6

6 of 40 new or added lines in 10 files covered. (15.0%)

12 existing lines in 6 files now uncovered.

135947 of 203696 relevant lines covered (66.74%)

21470.9 hits per line

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

0.0
/lntest/fee_service.go
1
package lntest
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "io"
7
        "net/http"
8
        "sync"
9
        "testing"
10

11
        "github.com/lightningnetwork/lnd"
12
        "github.com/lightningnetwork/lnd/lntest/port"
13
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
14
        "github.com/stretchr/testify/require"
15
)
16

17
// WebFeeService defines an interface that's used to provide fee estimation
18
// service used in the integration tests. It must provide an URL so that a lnd
19
// node can be started with the flag `--fee.url` and uses the customized fee
20
// estimator.
21
type WebFeeService interface {
22
        // Start starts the service.
23
        Start() error
24

25
        // Stop stops the service.
26
        Stop() error
27

28
        // URL returns the service's endpoint.
29
        URL() string
30

31
        // SetFeeRate sets the estimated fee rate for a given confirmation
32
        // target.
33
        SetFeeRate(feeRate chainfee.SatPerKWeight, conf uint32)
34

35
        // SetMinRelayFeerate sets a min relay feerate.
36
        SetMinRelayFeerate(fee chainfee.SatPerKVByte)
37

38
        // Reset resets the fee rate map to the default value.
39
        Reset()
40
}
41

42
const (
43
        // feeServiceTarget is the confirmation target for which a fee estimate
44
        // is returned. Requests for higher confirmation targets will fall back
45
        // to this.
46
        feeServiceTarget = 1
47

48
        // DefaultFeeRateSatPerKw specifies the default fee rate used in the
49
        // tests.
50
        DefaultFeeRateSatPerKw = 12500
51
)
52

53
// FeeService runs a web service that provides fee estimation information.
54
type FeeService struct {
55
        *testing.T
56

57
        feeRateMap      map[uint32]uint32
58
        minRelayFeerate chainfee.SatPerKVByte
59
        url             string
60

61
        srv  *http.Server
62
        wg   sync.WaitGroup
63
        lock sync.Mutex
64
}
65

66
// Compile-time check for the WebFeeService interface.
67
var _ WebFeeService = (*FeeService)(nil)
68

69
// NewFeeService spins up a go-routine to serve fee estimates.
70
func NewFeeService(t *testing.T) *FeeService {
×
71
        t.Helper()
×
72

×
73
        port := port.NextAvailablePort()
×
74
        f := FeeService{
×
75
                T: t,
×
76
                url: fmt.Sprintf(
×
77
                        "http://localhost:%v/fee-estimates.json", port,
×
78
                ),
×
79
        }
×
80

×
81
        // Initialize default fee estimate.
×
82
        f.feeRateMap = map[uint32]uint32{
×
83
                feeServiceTarget: DefaultFeeRateSatPerKw,
×
84
        }
×
85
        f.minRelayFeerate = chainfee.FeePerKwFloor.FeePerKVByte()
×
86

×
87
        listenAddr := fmt.Sprintf(":%v", port)
×
88
        mux := http.NewServeMux()
×
89
        mux.HandleFunc("/fee-estimates.json", f.handleRequest)
×
90

×
91
        f.srv = &http.Server{
×
92
                Addr:              listenAddr,
×
93
                Handler:           mux,
×
94
                ReadHeaderTimeout: lnd.DefaultHTTPHeaderTimeout,
×
95
        }
×
96

×
97
        return &f
×
98
}
×
99

100
// Start starts the web server.
101
func (f *FeeService) Start() error {
×
102
        f.wg.Add(1)
×
103
        go func() {
×
104
                defer f.wg.Done()
×
105

×
106
                if err := f.srv.ListenAndServe(); err != http.ErrServerClosed {
×
107
                        require.NoErrorf(f, err, "cannot start fee api")
×
108
                }
×
109
        }()
110

111
        return nil
×
112
}
113

114
// handleRequest handles a client request for fee estimates.
115
func (f *FeeService) handleRequest(w http.ResponseWriter, _ *http.Request) {
×
116
        f.lock.Lock()
×
117
        bytes, err := json.Marshal(
×
118
                chainfee.WebAPIResponse{
×
119
                        FeeByBlockTarget: f.feeRateMap,
×
120
                        MinRelayFeerate:  f.minRelayFeerate,
×
121
                },
×
122
        )
×
123
        f.lock.Unlock()
×
124
        require.NoErrorf(f, err, "cannot serialize estimates")
×
125

×
126
        _, err = io.WriteString(w, string(bytes))
×
127
        require.NoError(f, err, "cannot send estimates")
×
128
}
×
129

130
// Stop stops the web server.
131
func (f *FeeService) Stop() error {
×
NEW
132
        err := f.srv.Shutdown(f.Context())
×
133
        require.NoError(f, err, "cannot stop fee api")
×
134

×
135
        f.wg.Wait()
×
136

×
137
        return nil
×
138
}
×
139

140
// SetFeeRate sets a fee for the given confirmation target.
141
func (f *FeeService) SetFeeRate(fee chainfee.SatPerKWeight, conf uint32) {
×
142
        f.lock.Lock()
×
143
        defer f.lock.Unlock()
×
144

×
145
        f.feeRateMap[conf] = uint32(fee.FeePerKVByte())
×
146
}
×
147

148
// SetMinRelayFeerate sets a min relay feerate.
149
func (f *FeeService) SetMinRelayFeerate(fee chainfee.SatPerKVByte) {
×
150
        f.lock.Lock()
×
151
        defer f.lock.Unlock()
×
152

×
153
        f.minRelayFeerate = fee
×
154
}
×
155

156
// Reset resets the fee rate map to the default value.
157
func (f *FeeService) Reset() {
×
158
        f.lock.Lock()
×
159
        f.feeRateMap = make(map[uint32]uint32)
×
160
        f.minRelayFeerate = chainfee.FeePerKwFloor.FeePerKVByte()
×
161
        f.lock.Unlock()
×
162

×
163
        // Initialize default fee estimate.
×
164
        f.SetFeeRate(DefaultFeeRateSatPerKw, 1)
×
165
}
×
166

167
// URL returns the service endpoint.
168
func (f *FeeService) URL() string {
×
169
        return f.url
×
170
}
×
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