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

@@ -15,6 +15,7 @@ import (
"github.com/lightningnetwork/lnd/lntest/wait"
"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/route"
"github.com/stretchr/testify/mock"
@@ -116,7 +117,7 @@ func newTestPaymentLifecycle(t *testing.T) (*paymentLifecycle, *mockers) {
// Overwrite the collectResultAsync to focus on testing the payment
// lifecycle within the goroutine.
resultCollector := func(attempt *channeldb.HTLCAttempt) {
resultCollector := func(attempt *paymentsdb.HTLCAttempt) {
mockers.collectResultsCount++
}
p.resultCollector = resultCollector
@@ -147,7 +148,7 @@ func setupTestPaymentLifecycle(t *testing.T) (*paymentLifecycle, *mockers) {
m.payment, nil,
).Once()
htlcs := []channeldb.HTLCAttempt{}
htlcs := []paymentsdb.HTLCAttempt{}
m.payment.On("InFlightHTLCs").Return(htlcs).Once()
return p, m
@@ -258,11 +259,11 @@ func createDummyRoute(t *testing.T, amt lnwire.MilliSatoshi) *route.Route {
}
func makeSettledAttempt(t *testing.T, total int,
preimage lntypes.Preimage) *channeldb.HTLCAttempt {
preimage lntypes.Preimage) *paymentsdb.HTLCAttempt {
a := &channeldb.HTLCAttempt{
a := &paymentsdb.HTLCAttempt{
HTLCAttemptInfo: makeAttemptInfo(t, total),
Settle: &channeldb.HTLCSettleInfo{Preimage: preimage},
Settle: &paymentsdb.HTLCSettleInfo{Preimage: preimage},
}
hash := preimage.Hash()
@@ -271,18 +272,18 @@ func makeSettledAttempt(t *testing.T, total int,
return a
}
func makeFailedAttempt(t *testing.T, total int) *channeldb.HTLCAttempt {
return &channeldb.HTLCAttempt{
func makeFailedAttempt(t *testing.T, total int) *paymentsdb.HTLCAttempt {
return &paymentsdb.HTLCAttempt{
HTLCAttemptInfo: makeAttemptInfo(t, total),
Failure: &channeldb.HTLCFailInfo{
Reason: channeldb.HTLCFailInternal,
Failure: &paymentsdb.HTLCFailInfo{
Reason: paymentsdb.HTLCFailInternal,
},
}
}
func makeAttemptInfo(t *testing.T, amt int) channeldb.HTLCAttemptInfo {
func makeAttemptInfo(t *testing.T, amt int) paymentsdb.HTLCAttemptInfo {
rt := createDummyRoute(t, lnwire.MilliSatoshi(amt))
return channeldb.HTLCAttemptInfo{
return paymentsdb.HTLCAttemptInfo{
Route: *rt,
Hash: &lntypes.Hash{1, 2, 3},
}
@@ -378,7 +379,7 @@ func TestRequestRouteSucceed(t *testing.T) {
p.paySession = paySession
// Create a dummy payment state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
NumAttemptsInFlight: 1,
RemainingAmt: 1,
FeesPaid: 100,
@@ -415,7 +416,7 @@ func TestRequestRouteHandleCriticalErr(t *testing.T) {
p.paySession = paySession
// Create a dummy payment state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
NumAttemptsInFlight: 1,
RemainingAmt: 1,
FeesPaid: 100,
@@ -449,7 +450,7 @@ func TestRequestRouteHandleNoRouteErr(t *testing.T) {
p, m := newTestPaymentLifecycle(t)
// Create a dummy payment state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
NumAttemptsInFlight: 1,
RemainingAmt: 1,
FeesPaid: 100,
@@ -498,7 +499,7 @@ func TestRequestRouteFailPaymentError(t *testing.T) {
p.paySession = paySession
// Create a dummy payment state with zero inflight attempts.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
NumAttemptsInFlight: 0,
RemainingAmt: 1,
FeesPaid: 100,
@@ -819,14 +820,14 @@ func TestResumePaymentFailOnTimeout(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
// NOTE: GetStatus is only used to populate the logs which is not
// critical, so we loosen the checks on how many times it's been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. make the timeout happens instantly and sleep one millisecond to
// make sure it timed out.
@@ -875,7 +876,7 @@ func TestResumePaymentFailOnTimeoutErr(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// Quit the router to return an error.
close(p.router.quit)
@@ -910,14 +911,14 @@ func TestResumePaymentFailContextCancel(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
// NOTE: GetStatus is only used to populate the logs which is not
// critical, so we loosen the checks on how many times it's been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. Cancel the context and skip the FailPayment error to trigger the
// context cancellation of the payment.
@@ -963,7 +964,7 @@ func TestResumePaymentFailOnStepErr(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -971,7 +972,7 @@ func TestResumePaymentFailOnStepErr(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. decideNextStep now returns an error.
m.payment.On("AllowMoreAttempts").Return(false, errDummy).Once()
@@ -1001,7 +1002,7 @@ func TestResumePaymentFailOnRequestRouteErr(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -1009,7 +1010,7 @@ func TestResumePaymentFailOnRequestRouteErr(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. decideNextStep now returns stepProceed.
m.payment.On("AllowMoreAttempts").Return(true, nil).Once()
@@ -1047,7 +1048,7 @@ func TestResumePaymentFailOnRegisterAttemptErr(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -1055,7 +1056,7 @@ func TestResumePaymentFailOnRegisterAttemptErr(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. decideNextStep now returns stepProceed.
m.payment.On("AllowMoreAttempts").Return(true, nil).Once()
@@ -1107,7 +1108,7 @@ func TestResumePaymentFailOnSendAttemptErr(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -1115,7 +1116,7 @@ func TestResumePaymentFailOnSendAttemptErr(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 3. decideNextStep now returns stepProceed.
m.payment.On("AllowMoreAttempts").Return(true, nil).Once()
@@ -1199,7 +1200,7 @@ func TestResumePaymentSuccess(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 1.2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -1207,7 +1208,7 @@ func TestResumePaymentSuccess(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 1.3. decideNextStep now returns stepProceed.
m.payment.On("AllowMoreAttempts").Return(true, nil).Once()
@@ -1300,7 +1301,7 @@ func TestResumePaymentSuccessWithTwoAttempts(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 1.2. calls `GetState` and return the state.
ps := &channeldb.MPPaymentState{
ps := &paymentsdb.MPPaymentState{
RemainingAmt: paymentAmt,
}
m.payment.On("GetState").Return(ps).Once()
@@ -1308,7 +1309,7 @@ func TestResumePaymentSuccessWithTwoAttempts(t *testing.T) {
// NOTE: GetStatus is only used to populate the logs which is
// not critical so we loosen the checks on how many times it's
// been called.
m.payment.On("GetStatus").Return(channeldb.StatusInFlight)
m.payment.On("GetStatus").Return(paymentsdb.StatusInFlight)
// 1.3. decideNextStep now returns stepProceed.
m.payment.On("AllowMoreAttempts").Return(true, nil).Once()
@@ -1833,7 +1834,7 @@ func TestReloadInflightAttemptsLegacy(t *testing.T) {
m.control.On("FetchPayment", p.identifier).Return(m.payment, nil).Once()
// 2. calls `InFlightHTLCs` and return the attempt.
attempts := []channeldb.HTLCAttempt{*attempt}
attempts := []paymentsdb.HTLCAttempt{*attempt}
m.payment.On("InFlightHTLCs").Return(attempts).Once()
// 3. Mock the htlcswitch to return a the result chan.