multi: move payment related code into own package

This commit moves most of the code into its own package. It is
the smallest code move possible without moving import cycles and
keeping the changes to the code base as small as possible during
refactor.
This commit is contained in:
ziggie
2025-08-13 09:19:20 +02:00
parent 77a6b577eb
commit 03af9858d2
26 changed files with 1075 additions and 948 deletions

View File

@@ -22,6 +22,7 @@ import (
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
"github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing"
"github.com/lightningnetwork/lnd/routing/route"
@@ -1488,7 +1489,7 @@ func UnmarshalAMP(reqAMP *lnrpc.AMPRecord) (*record.AMP, error) {
// MarshalHTLCAttempt constructs an RPC HTLCAttempt from the db representation.
func (r *RouterBackend) MarshalHTLCAttempt(
htlc channeldb.HTLCAttempt) (*lnrpc.HTLCAttempt, error) {
htlc paymentsdb.HTLCAttempt) (*lnrpc.HTLCAttempt, error) {
route, err := r.MarshallRoute(&htlc.Route)
if err != nil {
@@ -1529,7 +1530,7 @@ func (r *RouterBackend) MarshalHTLCAttempt(
// marshallHtlcFailure marshalls htlc fail info from the database to its rpc
// representation.
func marshallHtlcFailure(failure *channeldb.HTLCFailInfo) (*lnrpc.Failure,
func marshallHtlcFailure(failure *paymentsdb.HTLCFailInfo) (*lnrpc.Failure,
error) {
rpcFailure := &lnrpc.Failure{
@@ -1537,16 +1538,16 @@ func marshallHtlcFailure(failure *channeldb.HTLCFailInfo) (*lnrpc.Failure,
}
switch failure.Reason {
case channeldb.HTLCFailUnknown:
case paymentsdb.HTLCFailUnknown:
rpcFailure.Code = lnrpc.Failure_UNKNOWN_FAILURE
case channeldb.HTLCFailUnreadable:
case paymentsdb.HTLCFailUnreadable:
rpcFailure.Code = lnrpc.Failure_UNREADABLE_FAILURE
case channeldb.HTLCFailInternal:
case paymentsdb.HTLCFailInternal:
rpcFailure.Code = lnrpc.Failure_INTERNAL_FAILURE
case channeldb.HTLCFailMessage:
case paymentsdb.HTLCFailMessage:
err := marshallWireError(failure.Message, rpcFailure)
if err != nil {
return nil, err
@@ -1743,7 +1744,7 @@ func marshallChannelUpdate(update *lnwire.ChannelUpdate1) *lnrpc.ChannelUpdate {
}
// MarshallPayment marshall a payment to its rpc representation.
func (r *RouterBackend) MarshallPayment(payment *channeldb.MPPayment) (
func (r *RouterBackend) MarshallPayment(payment *paymentsdb.MPPayment) (
*lnrpc.Payment, error) {
// Fetch the payment's preimage and the total paid in fees.
@@ -1813,11 +1814,11 @@ func (r *RouterBackend) MarshallPayment(payment *channeldb.MPPayment) (
// convertPaymentStatus converts a channeldb.PaymentStatus to the type expected
// by the RPC.
func convertPaymentStatus(dbStatus channeldb.PaymentStatus, useInit bool) (
func convertPaymentStatus(dbStatus paymentsdb.PaymentStatus, useInit bool) (
lnrpc.Payment_PaymentStatus, error) {
switch dbStatus {
case channeldb.StatusInitiated:
case paymentsdb.StatusInitiated:
// If the client understands the new status, return it.
if useInit {
return lnrpc.Payment_INITIATED, nil
@@ -1826,13 +1827,13 @@ func convertPaymentStatus(dbStatus channeldb.PaymentStatus, useInit bool) (
// Otherwise remain the old behavior.
return lnrpc.Payment_IN_FLIGHT, nil
case channeldb.StatusInFlight:
case paymentsdb.StatusInFlight:
return lnrpc.Payment_IN_FLIGHT, nil
case channeldb.StatusSucceeded:
case paymentsdb.StatusSucceeded:
return lnrpc.Payment_SUCCEEDED, nil
case channeldb.StatusFailed:
case paymentsdb.StatusFailed:
return lnrpc.Payment_FAILED, nil
default:

View File

@@ -15,7 +15,6 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/lightningnetwork/lnd/aliasmgr"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
@@ -927,7 +926,7 @@ func (s *Server) SendToRouteV2(ctx context.Context,
return nil, err
}
var attempt *channeldb.HTLCAttempt
var attempt *paymentsdb.HTLCAttempt
// Pass route to the router. This call returns the full htlc attempt
// information as it is stored in the database. It is possible that both
@@ -1449,17 +1448,17 @@ func (s *Server) trackPaymentStream(context context.Context,
// No more payment updates.
return nil
}
result := item.(*channeldb.MPPayment)
result := item.(*paymentsdb.MPPayment)
log.Tracef("Payment %v updated to state %v",
result.Info.PaymentIdentifier, result.Status)
// Skip in-flight updates unless requested.
if noInflightUpdates {
if result.Status == channeldb.StatusInitiated {
if result.Status == paymentsdb.StatusInitiated {
continue
}
if result.Status == channeldb.StatusInFlight {
if result.Status == paymentsdb.StatusInFlight {
continue
}
}

View File

@@ -10,6 +10,7 @@ import (
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
"github.com/lightningnetwork/lnd/queue"
"github.com/lightningnetwork/lnd/routing"
"github.com/lightningnetwork/lnd/routing/route"
@@ -129,13 +130,13 @@ func TestTrackPaymentsInflightUpdates(t *testing.T) {
}()
// Enqueue some payment updates on the mock.
towerMock.queue.ChanIn() <- &channeldb.MPPayment{
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{},
Status: channeldb.StatusInFlight,
Status: paymentsdb.StatusInFlight,
}
towerMock.queue.ChanIn() <- &channeldb.MPPayment{
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{},
Status: channeldb.StatusSucceeded,
Status: paymentsdb.StatusSucceeded,
}
// Wait until there's 2 updates or the deadline is exceeded.
@@ -191,13 +192,13 @@ func TestTrackPaymentsNoInflightUpdates(t *testing.T) {
}()
// Enqueue some payment updates on the mock.
towerMock.queue.ChanIn() <- &channeldb.MPPayment{
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{},
Status: channeldb.StatusInFlight,
Status: paymentsdb.StatusInFlight,
}
towerMock.queue.ChanIn() <- &channeldb.MPPayment{
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{},
Status: channeldb.StatusSucceeded,
Status: paymentsdb.StatusSucceeded,
}
// Wait until there's 1 update or the deadline is exceeded.