multi: move PaymentCreationInfo to payment pkg

This commit is contained in:
ziggie
2025-08-12 14:03:13 +02:00
parent d138e23919
commit df9bac2ecf
11 changed files with 58 additions and 75 deletions

View File

@@ -1,37 +0,0 @@
package channeldb
import (
"fmt"
"time"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
)
// PaymentCreationInfo is the information necessary to have ready when
// initiating a payment, moving it into state InFlight.
type PaymentCreationInfo struct {
// PaymentIdentifier is the hash this payment is paying to in case of
// non-AMP payments, and the SetID for AMP payments.
PaymentIdentifier lntypes.Hash
// Value is the amount we are paying.
Value lnwire.MilliSatoshi
// CreationTime is the time when this payment was initiated.
CreationTime time.Time
// PaymentRequest is the full payment request, if any.
PaymentRequest []byte
// FirstHopCustomRecords are the TLV records that are to be sent to the
// first hop of this payment. These records will be transmitted via the
// wire message only and therefore do not affect the onion payload size.
FirstHopCustomRecords lnwire.CustomRecords
}
// String returns a human-readable description of the payment creation info.
func (p *PaymentCreationInfo) String() string {
return fmt.Sprintf("payment_id=%v, amount=%v, created_at=%v",
p.PaymentIdentifier, p.Value, p.CreationTime)
}

View File

@@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/channeldb"
graphdb "github.com/lightningnetwork/lnd/graph/db" graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -131,11 +130,11 @@ func TestTrackPaymentsInflightUpdates(t *testing.T) {
// Enqueue some payment updates on the mock. // Enqueue some payment updates on the mock.
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{ towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{}, Info: &paymentsdb.PaymentCreationInfo{},
Status: paymentsdb.StatusInFlight, Status: paymentsdb.StatusInFlight,
} }
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{ towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{}, Info: &paymentsdb.PaymentCreationInfo{},
Status: paymentsdb.StatusSucceeded, Status: paymentsdb.StatusSucceeded,
} }
@@ -193,11 +192,11 @@ func TestTrackPaymentsNoInflightUpdates(t *testing.T) {
// Enqueue some payment updates on the mock. // Enqueue some payment updates on the mock.
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{ towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{}, Info: &paymentsdb.PaymentCreationInfo{},
Status: paymentsdb.StatusInFlight, Status: paymentsdb.StatusInFlight,
} }
towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{ towerMock.queue.ChanIn() <- &paymentsdb.MPPayment{
Info: &channeldb.PaymentCreationInfo{}, Info: &paymentsdb.PaymentCreationInfo{},
Status: paymentsdb.StatusSucceeded, Status: paymentsdb.StatusSucceeded,
} }

View File

@@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -94,11 +93,11 @@ func deserializeDuplicateHTLCAttemptInfo(r io.Reader) (
} }
func deserializeDuplicatePaymentCreationInfo(r io.Reader) ( func deserializeDuplicatePaymentCreationInfo(r io.Reader) (
*channeldb.PaymentCreationInfo, error) { *PaymentCreationInfo, error) {
var scratch [8]byte var scratch [8]byte
c := &channeldb.PaymentCreationInfo{} c := &PaymentCreationInfo{}
if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil { if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil {
return nil, err return nil, err

View File

@@ -186,7 +186,7 @@ func initKVStore(db kvdb.Backend) error {
// method returns successfully, the payment is guaranteed to be in the InFlight // method returns successfully, the payment is guaranteed to be in the InFlight
// state. // state.
func (p *KVPaymentsDB) InitPayment(paymentHash lntypes.Hash, func (p *KVPaymentsDB) InitPayment(paymentHash lntypes.Hash,
info *channeldb.PaymentCreationInfo) error { info *PaymentCreationInfo) error {
// Obtain a new sequence number for this payment. This is used // Obtain a new sequence number for this payment. This is used
// to sort the payments in order of creation, and also acts as // to sort the payments in order of creation, and also acts as
@@ -943,7 +943,7 @@ func (p *KVPaymentsDB) FetchPayments() ([]*MPPayment, error) {
return payments, nil return payments, nil
} }
func fetchCreationInfo(bucket kvdb.RBucket) (*channeldb.PaymentCreationInfo, error) { func fetchCreationInfo(bucket kvdb.RBucket) (*PaymentCreationInfo, error) {
b := bucket.Get(paymentCreationInfoKey) b := bucket.Get(paymentCreationInfoKey)
if b == nil { if b == nil {
return nil, fmt.Errorf("creation info not found") return nil, fmt.Errorf("creation info not found")
@@ -1621,7 +1621,7 @@ func fetchSequenceNumbers(paymentBucket kvdb.RBucket) ([][]byte, error) {
return sequenceNumbers, nil return sequenceNumbers, nil
} }
func serializePaymentCreationInfo(w io.Writer, c *channeldb.PaymentCreationInfo) error { func serializePaymentCreationInfo(w io.Writer, c *PaymentCreationInfo) error {
var scratch [8]byte var scratch [8]byte
if _, err := w.Write(c.PaymentIdentifier[:]); err != nil { if _, err := w.Write(c.PaymentIdentifier[:]); err != nil {
@@ -1658,12 +1658,12 @@ func serializePaymentCreationInfo(w io.Writer, c *channeldb.PaymentCreationInfo)
return nil return nil
} }
func deserializePaymentCreationInfo(r io.Reader) (*channeldb.PaymentCreationInfo, func deserializePaymentCreationInfo(r io.Reader) (*PaymentCreationInfo,
error) { error) {
var scratch [8]byte var scratch [8]byte
c := &channeldb.PaymentCreationInfo{} c := &PaymentCreationInfo{}
if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil { if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil {
return nil, err return nil, err

View File

@@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -32,7 +31,7 @@ func genPreimage() ([32]byte, error) {
return preimage, nil return preimage, nil
} }
func genInfo(t *testing.T) (*channeldb.PaymentCreationInfo, *HTLCAttemptInfo, func genInfo(t *testing.T) (*PaymentCreationInfo, *HTLCAttemptInfo,
lntypes.Preimage, error) { lntypes.Preimage, error) {
preimage, err := genPreimage() preimage, err := genPreimage()
@@ -50,7 +49,7 @@ func genInfo(t *testing.T) (*channeldb.PaymentCreationInfo, *HTLCAttemptInfo,
) )
require.NoError(t, err) require.NoError(t, err)
return &channeldb.PaymentCreationInfo{ return &PaymentCreationInfo{
PaymentIdentifier: rhash, PaymentIdentifier: rhash,
Value: testRoute.ReceiverAmt(), Value: testRoute.ReceiverAmt(),
CreationTime: time.Unix(time.Now().Unix(), 0), CreationTime: time.Unix(time.Now().Unix(), 0),
@@ -1156,7 +1155,7 @@ type htlcStatus struct {
// assertPaymentInfo retrieves the payment referred to by hash and verifies the // assertPaymentInfo retrieves the payment referred to by hash and verifies the
// expected values. // expected values.
func assertPaymentInfo(t *testing.T, p *KVPaymentsDB, hash lntypes.Hash, func assertPaymentInfo(t *testing.T, p *KVPaymentsDB, hash lntypes.Hash,
c *channeldb.PaymentCreationInfo, f *FailureReason, c *PaymentCreationInfo, f *FailureReason,
a *htlcStatus) { a *htlcStatus) {
t.Helper() t.Helper()
@@ -1397,7 +1396,7 @@ func assertPayments(t *testing.T, paymentDB *KVPaymentsDB,
require.Equal(t, payments, p) require.Equal(t, payments, p)
} }
func makeFakeInfo(t *testing.T) (*channeldb.PaymentCreationInfo, func makeFakeInfo(t *testing.T) (*PaymentCreationInfo,
*HTLCAttemptInfo) { *HTLCAttemptInfo) {
var preimg lntypes.Preimage var preimg lntypes.Preimage
@@ -1405,7 +1404,7 @@ func makeFakeInfo(t *testing.T) (*channeldb.PaymentCreationInfo,
hash := preimg.Hash() hash := preimg.Hash()
c := &channeldb.PaymentCreationInfo{ c := &PaymentCreationInfo{
PaymentIdentifier: hash, PaymentIdentifier: hash,
Value: 1000, Value: 1000,
// Use single second precision to avoid false positive test // Use single second precision to avoid false positive test

View File

@@ -12,7 +12,6 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
sphinx "github.com/lightningnetwork/lightning-onion" sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils" "github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -76,6 +75,34 @@ func (r FailureReason) String() string {
return "unknown" return "unknown"
} }
// PaymentCreationInfo is the information necessary to have ready when
// initiating a payment, moving it into state InFlight.
type PaymentCreationInfo struct {
// PaymentIdentifier is the hash this payment is paying to in case of
// non-AMP payments, and the SetID for AMP payments.
PaymentIdentifier lntypes.Hash
// Value is the amount we are paying.
Value lnwire.MilliSatoshi
// CreationTime is the time when this payment was initiated.
CreationTime time.Time
// PaymentRequest is the full payment request, if any.
PaymentRequest []byte
// FirstHopCustomRecords are the TLV records that are to be sent to the
// first hop of this payment. These records will be transmitted via the
// wire message only and therefore do not affect the onion payload size.
FirstHopCustomRecords lnwire.CustomRecords
}
// String returns a human-readable description of the payment creation info.
func (p *PaymentCreationInfo) String() string {
return fmt.Sprintf("payment_id=%v, amount=%v, created_at=%v",
p.PaymentIdentifier, p.Value, p.CreationTime)
}
// HTLCAttemptInfo contains static information about a specific HTLC attempt // HTLCAttemptInfo contains static information about a specific HTLC attempt
// for a payment. This information is used by the router to handle any errors // for a payment. This information is used by the router to handle any errors
// coming back after an attempt is made, and to query the switch about the // coming back after an attempt is made, and to query the switch about the
@@ -298,7 +325,7 @@ type MPPayment struct {
// Info holds all static information about this payment, and is // Info holds all static information about this payment, and is
// populated when the payment is initiated. // populated when the payment is initiated.
Info *channeldb.PaymentCreationInfo Info *PaymentCreationInfo
// HTLCs holds the information about individual HTLCs that we send in // HTLCs holds the information about individual HTLCs that we send in
// order to make the payment. // order to make the payment.

View File

@@ -12,7 +12,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
@@ -694,7 +693,7 @@ func TestPaymentSetState(t *testing.T) {
t.Parallel() t.Parallel()
// Attach the payment info. // Attach the payment info.
info := &channeldb.PaymentCreationInfo{ info := &PaymentCreationInfo{
Value: lnwire.MilliSatoshi(tc.totalAmt), Value: lnwire.MilliSatoshi(tc.totalAmt),
} }
tc.payment.Info = info tc.payment.Info = info
@@ -824,7 +823,7 @@ func TestNeedWaitAttempts(t *testing.T) {
tc := tc tc := tc
p := &MPPayment{ p := &MPPayment{
Info: &channeldb.PaymentCreationInfo{ Info: &PaymentCreationInfo{
PaymentIdentifier: [32]byte{1, 2, 3}, PaymentIdentifier: [32]byte{1, 2, 3},
}, },
Status: tc.status, Status: tc.status,
@@ -1002,7 +1001,7 @@ func TestAllowMoreAttempts(t *testing.T) {
tc := tc tc := tc
p := &MPPayment{ p := &MPPayment{
Info: &channeldb.PaymentCreationInfo{ Info: &PaymentCreationInfo{
PaymentIdentifier: [32]byte{1, 2, 3}, PaymentIdentifier: [32]byte{1, 2, 3},
}, },
Status: tc.status, Status: tc.status,

View File

@@ -3,7 +3,6 @@ package routing
import ( import (
"sync" "sync"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/multimutex" "github.com/lightningnetwork/lnd/multimutex"
paymentsdb "github.com/lightningnetwork/lnd/payments/db" paymentsdb "github.com/lightningnetwork/lnd/payments/db"
@@ -50,7 +49,7 @@ type DBMPPayment interface {
type ControlTower interface { type ControlTower interface {
// This method checks that no succeeded payment exist for this payment // This method checks that no succeeded payment exist for this payment
// hash. // hash.
InitPayment(lntypes.Hash, *channeldb.PaymentCreationInfo) error InitPayment(lntypes.Hash, *paymentsdb.PaymentCreationInfo) error
// DeleteFailedAttempts removes all failed HTLCs from the db. It should // DeleteFailedAttempts removes all failed HTLCs from the db. It should
// be called for a given payment whenever all inflight htlcs are // be called for a given payment whenever all inflight htlcs are
@@ -185,7 +184,7 @@ func NewControlTower(db *paymentsdb.KVPaymentsDB) ControlTower {
// method returns successfully, the payment is guaranteed to be in the // method returns successfully, the payment is guaranteed to be in the
// Initiated state. // Initiated state.
func (p *controlTower) InitPayment(paymentHash lntypes.Hash, func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
info *channeldb.PaymentCreationInfo) error { info *paymentsdb.PaymentCreationInfo) error {
err := p.db.InitPayment(paymentHash, info) err := p.db.InitPayment(paymentHash, info)
if err != nil { if err != nil {

View File

@@ -559,7 +559,7 @@ func initDB(t *testing.T) *channeldb.DB {
) )
} }
func genInfo() (*channeldb.PaymentCreationInfo, *paymentsdb.HTLCAttemptInfo, func genInfo() (*paymentsdb.PaymentCreationInfo, *paymentsdb.HTLCAttemptInfo,
lntypes.Preimage, error) { lntypes.Preimage, error) {
preimage, err := genPreimage() preimage, err := genPreimage()
@@ -579,7 +579,7 @@ func genInfo() (*channeldb.PaymentCreationInfo, *paymentsdb.HTLCAttemptInfo,
return nil, nil, lntypes.Preimage{}, err return nil, nil, lntypes.Preimage{}, err
} }
return &channeldb.PaymentCreationInfo{ return &paymentsdb.PaymentCreationInfo{
PaymentIdentifier: rhash, PaymentIdentifier: rhash,
Value: testRoute.ReceiverAmt(), Value: testRoute.ReceiverAmt(),
CreationTime: time.Unix(time.Now().Unix(), 0), CreationTime: time.Unix(time.Now().Unix(), 0),

View File

@@ -7,7 +7,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
@@ -248,7 +247,7 @@ func (m *mockPayerOld) CleanStore(pids map[uint64]struct{}) error {
} }
type initArgs struct { type initArgs struct {
c *channeldb.PaymentCreationInfo c *paymentsdb.PaymentCreationInfo
} }
type registerAttemptArgs struct { type registerAttemptArgs struct {
@@ -268,7 +267,7 @@ type failPaymentArgs struct {
} }
type testPayment struct { type testPayment struct {
info channeldb.PaymentCreationInfo info paymentsdb.PaymentCreationInfo
attempts []paymentsdb.HTLCAttempt attempts []paymentsdb.HTLCAttempt
} }
@@ -298,7 +297,7 @@ func makeMockControlTower() *mockControlTowerOld {
} }
func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash, func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
c *channeldb.PaymentCreationInfo) error { c *paymentsdb.PaymentCreationInfo) error {
if m.init != nil { if m.init != nil {
m.init <- initArgs{c} m.init <- initArgs{c}
@@ -733,7 +732,7 @@ type mockControlTower struct {
var _ ControlTower = (*mockControlTower)(nil) var _ ControlTower = (*mockControlTower)(nil)
func (m *mockControlTower) InitPayment(phash lntypes.Hash, func (m *mockControlTower) InitPayment(phash lntypes.Hash,
c *channeldb.PaymentCreationInfo) error { c *paymentsdb.PaymentCreationInfo) error {
args := m.Called(phash, c) args := m.Called(phash, c)
return args.Error(0) return args.Error(0)

View File

@@ -15,7 +15,6 @@ import (
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/amp" "github.com/lightningnetwork/lnd/amp"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/graph/db/models"
@@ -999,7 +998,7 @@ func (r *ChannelRouter) PreparePayment(payment *LightningPayment) (
// already in-flight. // already in-flight.
// //
// TODO(roasbeef): store records as part of creation info? // TODO(roasbeef): store records as part of creation info?
info := &channeldb.PaymentCreationInfo{ info := &paymentsdb.PaymentCreationInfo{
PaymentIdentifier: payment.Identifier(), PaymentIdentifier: payment.Identifier(),
Value: payment.Amount, Value: payment.Amount,
CreationTime: r.cfg.Clock.Now(), CreationTime: r.cfg.Clock.Now(),
@@ -1122,7 +1121,7 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route,
// Record this payment hash with the ControlTower, ensuring it is not // Record this payment hash with the ControlTower, ensuring it is not
// already in-flight. // already in-flight.
info := &channeldb.PaymentCreationInfo{ info := &paymentsdb.PaymentCreationInfo{
PaymentIdentifier: paymentIdentifier, PaymentIdentifier: paymentIdentifier,
Value: amt, Value: amt,
CreationTime: r.cfg.Clock.Now(), CreationTime: r.cfg.Clock.Now(),