Skip to content

Commit c5c4d6b

Browse files
committed
multi: move payment code from channeldb to payments lib
1 parent a86b978 commit c5c4d6b

29 files changed

+2065
-1980
lines changed

channeldb/duplicate_payments.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightningnetwork/lnd/kvdb"
1212
"github.com/lightningnetwork/lnd/lntypes"
1313
"github.com/lightningnetwork/lnd/lnwire"
14+
pymtpkg "github.com/lightningnetwork/lnd/payments"
1415
"github.com/lightningnetwork/lnd/routing/route"
1516
)
1617

@@ -61,20 +62,20 @@ type duplicateHTLCAttemptInfo struct {
6162

6263
// fetchDuplicatePaymentStatus fetches the payment status of the payment. If
6364
// the payment isn't found, it will return error `ErrPaymentNotInitiated`.
64-
func fetchDuplicatePaymentStatus(bucket kvdb.RBucket) (PaymentStatus, error) {
65+
func fetchDuplicatePaymentStatus(bucket kvdb.RBucket) (pymtpkg.PaymentStatus, error) {
6566
if bucket.Get(duplicatePaymentSettleInfoKey) != nil {
66-
return StatusSucceeded, nil
67+
return pymtpkg.StatusSucceeded, nil
6768
}
6869

6970
if bucket.Get(duplicatePaymentFailInfoKey) != nil {
70-
return StatusFailed, nil
71+
return pymtpkg.StatusFailed, nil
7172
}
7273

7374
if bucket.Get(duplicatePaymentCreationInfoKey) != nil {
74-
return StatusInFlight, nil
75+
return pymtpkg.StatusInFlight, nil
7576
}
7677

77-
return 0, ErrPaymentNotInitiated
78+
return 0, pymtpkg.ErrPaymentNotInitiated
7879
}
7980

8081
func deserializeDuplicateHTLCAttemptInfo(r io.Reader) (
@@ -93,11 +94,11 @@ func deserializeDuplicateHTLCAttemptInfo(r io.Reader) (
9394
}
9495

9596
func deserializeDuplicatePaymentCreationInfo(r io.Reader) (
96-
*PaymentCreationInfo, error) {
97+
*pymtpkg.PaymentCreationInfo, error) {
9798

9899
var scratch [8]byte
99100

100-
c := &PaymentCreationInfo{}
101+
c := &pymtpkg.PaymentCreationInfo{}
101102

102103
if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil {
103104
return nil, err
@@ -129,7 +130,7 @@ func deserializeDuplicatePaymentCreationInfo(r io.Reader) (
129130
return c, nil
130131
}
131132

132-
func fetchDuplicatePayment(bucket kvdb.RBucket) (*MPPayment, error) {
133+
func fetchDuplicatePayment(bucket kvdb.RBucket) (*pymtpkg.MPPayment, error) {
133134
seqBytes := bucket.Get(duplicatePaymentSequenceKey)
134135
if seqBytes == nil {
135136
return nil, fmt.Errorf("sequence number not found")
@@ -156,14 +157,14 @@ func fetchDuplicatePayment(bucket kvdb.RBucket) (*MPPayment, error) {
156157
}
157158

158159
// Get failure reason if available.
159-
var failureReason *FailureReason
160+
var failureReason *pymtpkg.FailureReason
160161
b = bucket.Get(duplicatePaymentFailInfoKey)
161162
if b != nil {
162-
reason := FailureReason(b[0])
163+
reason := pymtpkg.FailureReason(b[0])
163164
failureReason = &reason
164165
}
165166

166-
payment := &MPPayment{
167+
payment := &pymtpkg.MPPayment{
167168
SequenceNum: sequenceNum,
168169
Info: creationInfo,
169170
FailureReason: failureReason,
@@ -179,13 +180,13 @@ func fetchDuplicatePayment(bucket kvdb.RBucket) (*MPPayment, error) {
179180
return nil, err
180181
}
181182

182-
htlc := HTLCAttempt{
183-
HTLCAttemptInfo: HTLCAttemptInfo{
184-
AttemptID: attempt.attemptID,
185-
Route: attempt.route,
186-
sessionKey: attempt.sessionKey,
183+
htlc := pymtpkg.HTLCAttempt{
184+
HTLCAttemptInfo: pymtpkg.HTLCAttemptInfo{
185+
AttemptID: attempt.attemptID,
186+
Route: attempt.route,
187187
},
188188
}
189+
htlc.SetSessionKey(attempt.sessionKey)
189190

190191
// Get the payment preimage. This is only found for
191192
// successful payments.
@@ -194,27 +195,27 @@ func fetchDuplicatePayment(bucket kvdb.RBucket) (*MPPayment, error) {
194195
var preimg lntypes.Preimage
195196
copy(preimg[:], b)
196197

197-
htlc.Settle = &HTLCSettleInfo{
198+
htlc.Settle = &pymtpkg.HTLCSettleInfo{
198199
Preimage: preimg,
199200
SettleTime: time.Time{},
200201
}
201202
} else {
202203
// Otherwise the payment must have failed.
203-
htlc.Failure = &HTLCFailInfo{
204+
htlc.Failure = &pymtpkg.HTLCFailInfo{
204205
FailTime: time.Time{},
205206
}
206207
}
207208

208-
payment.HTLCs = []HTLCAttempt{htlc}
209+
payment.HTLCs = []pymtpkg.HTLCAttempt{htlc}
209210
}
210211

211212
return payment, nil
212213
}
213214

214-
func fetchDuplicatePayments(paymentHashBucket kvdb.RBucket) ([]*MPPayment,
215-
error) {
215+
func fetchDuplicatePayments(
216+
paymentHashBucket kvdb.RBucket) ([]*pymtpkg.MPPayment, error) {
216217

217-
var payments []*MPPayment
218+
var payments []*pymtpkg.MPPayment
218219

219220
// For older versions of lnd, duplicate payments to a payment has was
220221
// possible. These will be found in a sub-bucket indexed by their

0 commit comments

Comments
 (0)