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

lightningnetwork / lnd / 14358372723

09 Apr 2025 01:26PM UTC coverage: 56.696% (-12.3%) from 69.037%
14358372723

Pull #9696

github

web-flow
Merge e2837e400 into 867d27d68
Pull Request #9696: Add `development_guidelines.md` for both human and machine

107055 of 188823 relevant lines covered (56.7%)

22721.56 hits per line

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

0.0
/lnrpc/routerrpc/router_server_deprecated.go
1
package routerrpc
2

3
import (
4
        "context"
5
        "encoding/hex"
6
        "errors"
7
        "fmt"
8

9
        "github.com/lightningnetwork/lnd/lnrpc"
10
        "github.com/lightningnetwork/lnd/lnwire"
11
        "github.com/lightningnetwork/lnd/routing/route"
12
)
13

14
// legacyTrackPaymentServer is a wrapper struct that transforms a stream of main
15
// rpc payment structs into the legacy PaymentStatus format.
16
type legacyTrackPaymentServer struct {
17
        Router_TrackPaymentServer
18
}
19

20
// Send converts a Payment object and sends it as a PaymentStatus object on the
21
// embedded stream.
22
func (i *legacyTrackPaymentServer) Send(p *lnrpc.Payment) error {
×
23
        var state PaymentState
×
24
        switch p.Status {
×
25
        case lnrpc.Payment_IN_FLIGHT:
×
26
                state = PaymentState_IN_FLIGHT
×
27
        case lnrpc.Payment_SUCCEEDED:
×
28
                state = PaymentState_SUCCEEDED
×
29
        case lnrpc.Payment_FAILED:
×
30
                switch p.FailureReason {
×
31
                case lnrpc.PaymentFailureReason_FAILURE_REASON_NONE:
×
32
                        return fmt.Errorf("expected fail reason")
×
33

34
                case lnrpc.PaymentFailureReason_FAILURE_REASON_TIMEOUT:
×
35
                        state = PaymentState_FAILED_TIMEOUT
×
36

37
                case lnrpc.PaymentFailureReason_FAILURE_REASON_NO_ROUTE:
×
38
                        state = PaymentState_FAILED_NO_ROUTE
×
39

40
                case lnrpc.PaymentFailureReason_FAILURE_REASON_ERROR:
×
41
                        state = PaymentState_FAILED_ERROR
×
42

43
                case lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS:
×
44
                        state = PaymentState_FAILED_INCORRECT_PAYMENT_DETAILS
×
45

46
                case lnrpc.PaymentFailureReason_FAILURE_REASON_INSUFFICIENT_BALANCE:
×
47
                        state = PaymentState_FAILED_INSUFFICIENT_BALANCE
×
48

49
                default:
×
50
                        return fmt.Errorf("unknown failure reason %v",
×
51
                                p.FailureReason)
×
52
                }
53
        default:
×
54
                return fmt.Errorf("unknown state %v", p.Status)
×
55
        }
56

57
        preimage, err := hex.DecodeString(p.PaymentPreimage)
×
58
        if err != nil {
×
59
                return err
×
60
        }
×
61

62
        legacyState := PaymentStatus{
×
63
                State:    state,
×
64
                Preimage: preimage,
×
65
                Htlcs:    p.Htlcs,
×
66
        }
×
67

×
68
        return i.Router_TrackPaymentServer.Send(&legacyState)
×
69
}
70

71
// TrackPayment returns a stream of payment state updates. The stream is
72
// closed when the payment completes.
73
func (s *Server) TrackPayment(request *TrackPaymentRequest,
74
        stream Router_TrackPaymentServer) error {
×
75

×
76
        legacyStream := legacyTrackPaymentServer{
×
77
                Router_TrackPaymentServer: stream,
×
78
        }
×
79
        return s.TrackPaymentV2(request, &legacyStream)
×
80
}
×
81

82
// SendPayment attempts to route a payment described by the passed
83
// PaymentRequest to the final destination. If we are unable to route the
84
// payment, or cannot find a route that satisfies the constraints in the
85
// PaymentRequest, then an error will be returned. Otherwise, the payment
86
// pre-image, along with the final route will be returned.
87
func (s *Server) SendPayment(request *SendPaymentRequest,
88
        stream Router_SendPaymentServer) error {
×
89

×
90
        if request.MaxParts > 1 {
×
91
                return errors.New("for multi-part payments, use SendPaymentV2")
×
92
        }
×
93

94
        legacyStream := legacyTrackPaymentServer{
×
95
                Router_TrackPaymentServer: stream,
×
96
        }
×
97
        return s.SendPaymentV2(request, &legacyStream)
×
98
}
99

100
// SendToRoute sends a payment through a predefined route. The response of this
101
// call contains structured error information.
102
func (s *Server) SendToRoute(ctx context.Context,
103
        req *SendToRouteRequest) (*SendToRouteResponse, error) {
×
104

×
105
        resp, err := s.SendToRouteV2(ctx, req)
×
106
        if err != nil {
×
107
                return nil, err
×
108
        }
×
109

110
        if resp == nil {
×
111
                return nil, nil
×
112
        }
×
113

114
        // Need to convert to legacy response message because proto identifiers
115
        // don't line up.
116
        legacyResp := &SendToRouteResponse{
×
117
                Preimage: resp.Preimage,
×
118
                Failure:  resp.Failure,
×
119
        }
×
120

×
121
        return legacyResp, nil
×
122
}
123

124
// QueryProbability returns the current success probability estimate for a
125
// given node pair and amount.
126
func (s *Server) QueryProbability(_ context.Context,
127
        req *QueryProbabilityRequest) (*QueryProbabilityResponse, error) {
×
128

×
129
        fromNode, err := route.NewVertexFromBytes(req.FromNode)
×
130
        if err != nil {
×
131
                return nil, err
×
132
        }
×
133

134
        toNode, err := route.NewVertexFromBytes(req.ToNode)
×
135
        if err != nil {
×
136
                return nil, err
×
137
        }
×
138

139
        amt := lnwire.MilliSatoshi(req.AmtMsat)
×
140

×
141
        // Compute the probability.
×
142
        var prob float64
×
143
        mc := s.cfg.RouterBackend.MissionControl
×
144
        capacity, err := s.cfg.RouterBackend.FetchAmountPairCapacity(
×
145
                fromNode, toNode, amt,
×
146
        )
×
147

×
148
        // If we cannot query the capacity this means that either we don't have
×
149
        // information available or that the channel fails min/maxHtlc
×
150
        // constraints, so we return a zero probability.
×
151
        if err != nil {
×
152
                log.Errorf("Cannot fetch capacity: %v", err)
×
153
        } else {
×
154
                prob = mc.GetProbability(fromNode, toNode, amt, capacity)
×
155
        }
×
156

157
        history := mc.GetPairHistorySnapshot(fromNode, toNode)
×
158

×
159
        return &QueryProbabilityResponse{
×
160
                Probability: prob,
×
161
                History:     toRPCPairData(&history),
×
162
        }, nil
×
163
}
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