github.com/decred/dcrlnd@v0.7.6/lnrpc/routerrpc/router_server_deprecated.go (about)

     1  package routerrpc
     2  
     3  import (
     4  	"context"
     5  	"encoding/hex"
     6  	"errors"
     7  	"fmt"
     8  
     9  	"github.com/decred/dcrlnd/lnrpc"
    10  )
    11  
    12  // legacyTrackPaymentServer is a wrapper struct that transforms a stream of main
    13  // rpc payment structs into the legacy PaymentStatus format.
    14  type legacyTrackPaymentServer struct {
    15  	Router_TrackPaymentServer
    16  }
    17  
    18  // Send converts a Payment object and sends it as a PaymentStatus object on the
    19  // embedded stream.
    20  func (i *legacyTrackPaymentServer) Send(p *lnrpc.Payment) error {
    21  	var state PaymentState
    22  	switch p.Status {
    23  	case lnrpc.Payment_IN_FLIGHT:
    24  		state = PaymentState_IN_FLIGHT
    25  	case lnrpc.Payment_SUCCEEDED:
    26  		state = PaymentState_SUCCEEDED
    27  	case lnrpc.Payment_FAILED:
    28  		switch p.FailureReason {
    29  		case lnrpc.PaymentFailureReason_FAILURE_REASON_NONE:
    30  			return fmt.Errorf("expected fail reason")
    31  
    32  		case lnrpc.PaymentFailureReason_FAILURE_REASON_TIMEOUT:
    33  			state = PaymentState_FAILED_TIMEOUT
    34  
    35  		case lnrpc.PaymentFailureReason_FAILURE_REASON_NO_ROUTE:
    36  			state = PaymentState_FAILED_NO_ROUTE
    37  
    38  		case lnrpc.PaymentFailureReason_FAILURE_REASON_ERROR:
    39  			state = PaymentState_FAILED_ERROR
    40  
    41  		case lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS:
    42  			state = PaymentState_FAILED_INCORRECT_PAYMENT_DETAILS
    43  
    44  		case lnrpc.PaymentFailureReason_FAILURE_REASON_INSUFFICIENT_BALANCE:
    45  			state = PaymentState_FAILED_INSUFFICIENT_BALANCE
    46  
    47  		default:
    48  			return fmt.Errorf("unknown failure reason %v",
    49  				p.FailureReason)
    50  		}
    51  	default:
    52  		return fmt.Errorf("unknown state %v", p.Status)
    53  	}
    54  
    55  	preimage, err := hex.DecodeString(p.PaymentPreimage)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	legacyState := PaymentStatus{
    61  		State:    state,
    62  		Preimage: preimage,
    63  		Htlcs:    p.Htlcs,
    64  	}
    65  
    66  	return i.Router_TrackPaymentServer.Send(&legacyState)
    67  }
    68  
    69  // TrackPayment returns a stream of payment state updates. The stream is
    70  // closed when the payment completes.
    71  func (s *Server) TrackPayment(request *TrackPaymentRequest,
    72  	stream Router_TrackPaymentServer) error {
    73  
    74  	legacyStream := legacyTrackPaymentServer{
    75  		Router_TrackPaymentServer: stream,
    76  	}
    77  	return s.TrackPaymentV2(request, &legacyStream)
    78  }
    79  
    80  // SendPayment attempts to route a payment described by the passed
    81  // PaymentRequest to the final destination. If we are unable to route the
    82  // payment, or cannot find a route that satisfies the constraints in the
    83  // PaymentRequest, then an error will be returned. Otherwise, the payment
    84  // pre-image, along with the final route will be returned.
    85  func (s *Server) SendPayment(request *SendPaymentRequest,
    86  	stream Router_SendPaymentServer) error {
    87  
    88  	if request.MaxParts > 1 {
    89  		return errors.New("for multi-part payments, use SendPaymentV2")
    90  	}
    91  
    92  	legacyStream := legacyTrackPaymentServer{
    93  		Router_TrackPaymentServer: stream,
    94  	}
    95  	return s.SendPaymentV2(request, &legacyStream)
    96  }
    97  
    98  // SendToRoute sends a payment through a predefined route. The response of this
    99  // call contains structured error information.
   100  func (s *Server) SendToRoute(ctx context.Context,
   101  	req *SendToRouteRequest) (*SendToRouteResponse, error) {
   102  
   103  	resp, err := s.SendToRouteV2(ctx, req)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	if resp == nil {
   109  		return nil, nil
   110  	}
   111  
   112  	// Need to convert to legacy response message because proto identifiers
   113  	// don't line up.
   114  	legacyResp := &SendToRouteResponse{
   115  		Preimage: resp.Preimage,
   116  		Failure:  resp.Failure,
   117  	}
   118  
   119  	return legacyResp, err
   120  }