diff --git a/channeldb/duplicate_payments.go b/channeldb/duplicate_payments.go index 5d23764b..0338fab7 100644 --- a/channeldb/duplicate_payments.go +++ b/channeldb/duplicate_payments.go @@ -99,7 +99,7 @@ func deserializeDuplicatePaymentCreationInfo(r io.Reader) ( c := &PaymentCreationInfo{} - if _, err := io.ReadFull(r, c.PaymentHash[:]); err != nil { + if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil { return nil, err } diff --git a/channeldb/payment_control.go b/channeldb/payment_control.go index a5023567..d7b4e743 100644 --- a/channeldb/payment_control.go +++ b/channeldb/payment_control.go @@ -173,8 +173,10 @@ func (p *PaymentControl) InitPayment(paymentHash lntypes.Hash, // Once we have obtained a sequence number, we add an entry // to our index bucket which will map the sequence number to - // our payment hash. - err = createPaymentIndexEntry(tx, sequenceNum, info.PaymentHash) + // our payment identifier. + err = createPaymentIndexEntry( + tx, sequenceNum, info.PaymentIdentifier, + ) if err != nil { return err } @@ -220,12 +222,12 @@ const paymentIndexTypeHash paymentIndexType = 0 // createPaymentIndexEntry creates a payment hash typed index for a payment. The // index produced contains a payment index type (which can be used in future to -// signal different payment index types) and the payment hash. +// signal different payment index types) and the payment identifier. func createPaymentIndexEntry(tx kvdb.RwTx, sequenceNumber []byte, - hash lntypes.Hash) error { + id lntypes.Hash) error { var b bytes.Buffer - if err := WriteElements(&b, paymentIndexTypeHash, hash[:]); err != nil { + if err := WriteElements(&b, paymentIndexTypeHash, id[:]); err != nil { return err } diff --git a/channeldb/payment_control_test.go b/channeldb/payment_control_test.go index 5bf6a05d..7d293c7f 100644 --- a/channeldb/payment_control_test.go +++ b/channeldb/payment_control_test.go @@ -38,10 +38,10 @@ func genInfo() (*PaymentCreationInfo, *HTLCAttemptInfo, rhash := sha256.Sum256(preimage[:]) return &PaymentCreationInfo{ - PaymentHash: rhash, - Value: testRoute.ReceiverAmt(), - CreationTime: time.Unix(time.Now().Unix(), 0), - PaymentRequest: []byte("hola"), + PaymentIdentifier: rhash, + Value: testRoute.ReceiverAmt(), + CreationTime: time.Unix(time.Now().Unix(), 0), + PaymentRequest: []byte("hola"), }, &HTLCAttemptInfo{ AttemptID: 0, @@ -70,63 +70,63 @@ func TestPaymentControlSwitchFail(t *testing.T) { } // Sends base htlc message which initiate StatusInFlight. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - assertPaymentIndex(t, pControl, info.PaymentHash) - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentIndex(t, pControl, info.PaymentIdentifier) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, nil, + t, pControl, info.PaymentIdentifier, info, nil, nil, ) // Fail the payment, which should moved it to Failed. failReason := FailureReasonNoRoute - _, err = pControl.Fail(info.PaymentHash, failReason) + _, err = pControl.Fail(info.PaymentIdentifier, failReason) if err != nil { t.Fatalf("unable to fail payment hash: %v", err) } // Verify the status is indeed Failed. - assertPaymentStatus(t, pControl, info.PaymentHash, StatusFailed) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed) assertPaymentInfo( - t, pControl, info.PaymentHash, info, &failReason, nil, + t, pControl, info.PaymentIdentifier, info, &failReason, nil, ) // Lookup the payment so we can get its old sequence number before it is // overwritten. - payment, err := pControl.FetchPayment(info.PaymentHash) + payment, err := pControl.FetchPayment(info.PaymentIdentifier) assert.NoError(t, err) // Sends the htlc again, which should succeed since the prior payment // failed. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } // Check that our index has been updated, and the old index has been // removed. - assertPaymentIndex(t, pControl, info.PaymentHash) + assertPaymentIndex(t, pControl, info.PaymentIdentifier) assertNoIndex(t, pControl, payment.SequenceNum) - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, nil, + t, pControl, info.PaymentIdentifier, info, nil, nil, ) // Record a new attempt. In this test scenario, the attempt fails. // However, this is not communicated to control tower in the current // implementation. It only registers the initiation of the attempt. - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to register attempt: %v", err) } htlcReason := HTLCFailUnreadable _, err = pControl.FailAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCFailInfo{ Reason: htlcReason, }, @@ -134,35 +134,35 @@ func TestPaymentControlSwitchFail(t *testing.T) { if err != nil { t.Fatal(err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) htlc := &htlcStatus{ HTLCAttemptInfo: attempt, failure: &htlcReason, } - assertPaymentInfo(t, pControl, info.PaymentHash, info, nil, htlc) + assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc) // Record another attempt. attempt.AttemptID = 1 - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) htlc = &htlcStatus{ HTLCAttemptInfo: attempt, } assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) // Settle the attempt and verify that status was changed to // StatusSucceeded. payment, err = pControl.SettleAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -183,16 +183,16 @@ func TestPaymentControlSwitchFail(t *testing.T) { spew.Sdump(payment.HTLCs[0].Route), err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) htlc.settle = &preimg assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) // Attempt a final payment, which should now fail since the prior // payment succeed. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != ErrAlreadyPaid { t.Fatalf("unable to send htlc message: %v", err) } @@ -219,42 +219,42 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) { // Sends base htlc message which initiate base status and move it to // StatusInFlight and verifies that it was changed. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - assertPaymentIndex(t, pControl, info.PaymentHash) - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentIndex(t, pControl, info.PaymentIdentifier) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, nil, + t, pControl, info.PaymentIdentifier, info, nil, nil, ) // Try to initiate double sending of htlc message with the same // payment hash, should result in error indicating that payment has // already been sent. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != ErrPaymentInFlight { t.Fatalf("payment control wrong behaviour: " + "double sending must trigger ErrPaymentInFlight error") } // Record an attempt. - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) htlc := &htlcStatus{ HTLCAttemptInfo: attempt, } assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) // Sends base htlc message which initiate StatusInFlight. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != ErrPaymentInFlight { t.Fatalf("payment control wrong behaviour: " + "double sending must trigger ErrPaymentInFlight error") @@ -262,7 +262,7 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) { // After settling, the error should be ErrAlreadyPaid. _, err = pControl.SettleAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -270,12 +270,12 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) { if err != nil { t.Fatalf("error shouldn't have been received, got: %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) htlc.settle = &preimg - assertPaymentInfo(t, pControl, info.PaymentHash, info, nil, htlc) + assertPaymentInfo(t, pControl, info.PaymentIdentifier, info, nil, htlc) - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != ErrAlreadyPaid { t.Fatalf("unable to send htlc message: %v", err) } @@ -302,7 +302,7 @@ func TestPaymentControlSuccessesWithoutInFlight(t *testing.T) { // Attempt to complete the payment should fail. _, err = pControl.SettleAttempt( - info.PaymentHash, 0, + info.PaymentIdentifier, 0, &HTLCSettleInfo{ Preimage: preimg, }, @@ -311,7 +311,7 @@ func TestPaymentControlSuccessesWithoutInFlight(t *testing.T) { t.Fatalf("expected ErrPaymentNotInitiated, got %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusUnknown) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusUnknown) } // TestPaymentControlFailsWithoutInFlight checks that a strict payment @@ -334,12 +334,12 @@ func TestPaymentControlFailsWithoutInFlight(t *testing.T) { } // Calling Fail should return an error. - _, err = pControl.Fail(info.PaymentHash, FailureReasonNoRoute) + _, err = pControl.Fail(info.PaymentIdentifier, FailureReasonNoRoute) if err != ErrPaymentNotInitiated { t.Fatalf("expected ErrPaymentNotInitiated, got %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusUnknown) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusUnknown) } // TestPaymentControlDeleteNonInFlight checks that calling DeletePayments only @@ -397,11 +397,11 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) { } // Sends base htlc message which initiate StatusInFlight. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } @@ -414,7 +414,7 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) { // Fail the payment attempt. htlcFailure := HTLCFailUnreadable _, err := pControl.FailAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCFailInfo{ Reason: htlcFailure, }, @@ -425,23 +425,23 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) { // Fail the payment, which should moved it to Failed. failReason := FailureReasonNoRoute - _, err = pControl.Fail(info.PaymentHash, failReason) + _, err = pControl.Fail(info.PaymentIdentifier, failReason) if err != nil { t.Fatalf("unable to fail payment hash: %v", err) } // Verify the status is indeed Failed. - assertPaymentStatus(t, pControl, info.PaymentHash, StatusFailed) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusFailed) htlc.failure = &htlcFailure assertPaymentInfo( - t, pControl, info.PaymentHash, info, + t, pControl, info.PaymentIdentifier, info, &failReason, htlc, ) } else if p.success { // Verifies that status was changed to StatusSucceeded. _, err := pControl.SettleAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -450,18 +450,18 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) { t.Fatalf("error shouldn't have been received, got: %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusSucceeded) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusSucceeded) htlc.settle = &preimg assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) numSuccess++ } else { - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) numInflight++ @@ -471,7 +471,7 @@ func TestPaymentControlDeleteNonInFligt(t *testing.T) { // add one. if p.hasDuplicate { appendDuplicatePayment( - t, pControl.db, info.PaymentHash, + t, pControl.db, info.PaymentIdentifier, uint64(duplicateSeqNr), preimg, ) duplicateSeqNr++ @@ -582,20 +582,20 @@ func TestPaymentControlDeletePayments(t *testing.T) { attemptID++ // Init the payment. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } // Register and fail the first attempt for all three payments. - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } htlcFailure := HTLCFailUnreadable _, err = pControl.FailAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCFailInfo{ Reason: htlcFailure, }, @@ -609,7 +609,7 @@ func TestPaymentControlDeletePayments(t *testing.T) { attempt.AttemptID = attemptID attemptID++ - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } @@ -620,7 +620,7 @@ func TestPaymentControlDeletePayments(t *testing.T) { case 0: htlcFailure := HTLCFailUnreadable _, err = pControl.FailAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCFailInfo{ Reason: htlcFailure, }, @@ -630,7 +630,7 @@ func TestPaymentControlDeletePayments(t *testing.T) { } failReason := FailureReasonNoRoute - _, err = pControl.Fail(info.PaymentHash, failReason) + _, err = pControl.Fail(info.PaymentIdentifier, failReason) if err != nil { t.Fatalf("unable to fail payment hash: %v", err) } @@ -638,7 +638,7 @@ func TestPaymentControlDeletePayments(t *testing.T) { // Settle the attempt case 1: _, err := pControl.SettleAttempt( - info.PaymentHash, attempt.AttemptID, + info.PaymentIdentifier, attempt.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -807,15 +807,15 @@ func TestPaymentControlMultiShard(t *testing.T) { } // Init the payment, moving it to the StatusInFlight state. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } - assertPaymentIndex(t, pControl, info.PaymentHash) - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentIndex(t, pControl, info.PaymentIdentifier) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, nil, + t, pControl, info.PaymentIdentifier, info, nil, nil, ) // Create three unique attempts we'll use for the test, and @@ -834,19 +834,19 @@ func TestPaymentControlMultiShard(t *testing.T) { a.AttemptID = i attempts = append(attempts, &a) - _, err = pControl.RegisterAttempt(info.PaymentHash, &a) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &a) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } assertPaymentStatus( - t, pControl, info.PaymentHash, StatusInFlight, + t, pControl, info.PaymentIdentifier, StatusInFlight, ) htlc := &htlcStatus{ HTLCAttemptInfo: &a, } assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) } @@ -855,7 +855,7 @@ func TestPaymentControlMultiShard(t *testing.T) { // will be too large. b := *attempt b.AttemptID = 3 - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrValueExceedsAmt { t.Fatalf("expected ErrValueExceedsAmt, got: %v", err) @@ -865,7 +865,7 @@ func TestPaymentControlMultiShard(t *testing.T) { a := attempts[1] htlcFail := HTLCFailUnreadable _, err = pControl.FailAttempt( - info.PaymentHash, a.AttemptID, + info.PaymentIdentifier, a.AttemptID, &HTLCFailInfo{ Reason: htlcFail, }, @@ -879,11 +879,11 @@ func TestPaymentControlMultiShard(t *testing.T) { failure: &htlcFail, } assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) // Payment should still be in-flight. - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) // Depending on the test case, settle or fail the first attempt. a = attempts[0] @@ -894,7 +894,7 @@ func TestPaymentControlMultiShard(t *testing.T) { var firstFailReason *FailureReason if test.settleFirst { _, err := pControl.SettleAttempt( - info.PaymentHash, a.AttemptID, + info.PaymentIdentifier, a.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -907,11 +907,11 @@ func TestPaymentControlMultiShard(t *testing.T) { // Assert that the HTLC has had the preimage recorded. htlc.settle = &preimg assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) } else { _, err := pControl.FailAttempt( - info.PaymentHash, a.AttemptID, + info.PaymentIdentifier, a.AttemptID, &HTLCFailInfo{ Reason: htlcFail, }, @@ -924,13 +924,13 @@ func TestPaymentControlMultiShard(t *testing.T) { // Assert the failure was recorded. htlc.failure = &htlcFail assertPaymentInfo( - t, pControl, info.PaymentHash, info, nil, htlc, + t, pControl, info.PaymentIdentifier, info, nil, htlc, ) // We also record a payment level fail, to move it into // a terminal state. failReason := FailureReasonNoRoute - _, err = pControl.Fail(info.PaymentHash, failReason) + _, err = pControl.Fail(info.PaymentIdentifier, failReason) if err != nil { t.Fatalf("unable to fail payment hash: %v", err) } @@ -942,18 +942,18 @@ func TestPaymentControlMultiShard(t *testing.T) { // The payment should still be considered in-flight, since there // is still an active HTLC. - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) // Try to register yet another attempt. This should fail now // that the payment has reached a terminal condition. b = *attempt b.AttemptID = 3 - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrPaymentTerminal { t.Fatalf("expected ErrPaymentTerminal, got: %v", err) } - assertPaymentStatus(t, pControl, info.PaymentHash, StatusInFlight) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, StatusInFlight) // Settle or fail the remaining attempt based on the testcase. a = attempts[2] @@ -963,7 +963,7 @@ func TestPaymentControlMultiShard(t *testing.T) { if test.settleLast { // Settle the last outstanding attempt. _, err = pControl.SettleAttempt( - info.PaymentHash, a.AttemptID, + info.PaymentIdentifier, a.AttemptID, &HTLCSettleInfo{ Preimage: preimg, }, @@ -975,13 +975,13 @@ func TestPaymentControlMultiShard(t *testing.T) { htlc.settle = &preimg assertPaymentInfo( - t, pControl, info.PaymentHash, info, + t, pControl, info.PaymentIdentifier, info, firstFailReason, htlc, ) } else { // Fail the attempt. _, err := pControl.FailAttempt( - info.PaymentHash, a.AttemptID, + info.PaymentIdentifier, a.AttemptID, &HTLCFailInfo{ Reason: htlcFail, }, @@ -994,7 +994,7 @@ func TestPaymentControlMultiShard(t *testing.T) { // Assert the failure was recorded. htlc.failure = &htlcFail assertPaymentInfo( - t, pControl, info.PaymentHash, info, + t, pControl, info.PaymentIdentifier, info, firstFailReason, htlc, ) @@ -1003,7 +1003,7 @@ func TestPaymentControlMultiShard(t *testing.T) { // write a terminal failure to the database without // syncing. failReason := FailureReasonPaymentDetails - _, err = pControl.Fail(info.PaymentHash, failReason) + _, err = pControl.Fail(info.PaymentIdentifier, failReason) if err != nil { t.Fatalf("unable to fail payment hash: %v", err) } @@ -1017,10 +1017,10 @@ func TestPaymentControlMultiShard(t *testing.T) { finalStatus = StatusSucceeded } - assertPaymentStatus(t, pControl, info.PaymentHash, finalStatus) + assertPaymentStatus(t, pControl, info.PaymentIdentifier, finalStatus) // Finally assert we cannot register more attempts. - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) require.Equal(t, ErrPaymentTerminal, err) } @@ -1053,7 +1053,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { } // Init the payment. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } @@ -1068,7 +1068,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { info.Value, [32]byte{1}, ) - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } @@ -1077,7 +1077,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { b := *attempt b.AttemptID = 1 b.Route.FinalHop().MPP = nil - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrMPPayment { t.Fatalf("expected ErrMPPayment, got: %v", err) } @@ -1086,7 +1086,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { b.Route.FinalHop().MPP = record.NewMPP( info.Value, [32]byte{2}, ) - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrMPPPaymentAddrMismatch { t.Fatalf("expected ErrMPPPaymentAddrMismatch, got: %v", err) } @@ -1095,7 +1095,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { b.Route.FinalHop().MPP = record.NewMPP( info.Value/2, [32]byte{1}, ) - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrMPPTotalAmountMismatch { t.Fatalf("expected ErrMPPTotalAmountMismatch, got: %v", err) } @@ -1107,13 +1107,13 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { t.Fatalf("unable to generate htlc message: %v", err) } - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } attempt.Route.FinalHop().MPP = nil - _, err = pControl.RegisterAttempt(info.PaymentHash, attempt) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt) if err != nil { t.Fatalf("unable to send htlc message: %v", err) } @@ -1125,7 +1125,7 @@ func TestPaymentControlMPPRecordValidation(t *testing.T) { info.Value, [32]byte{1}, ) - _, err = pControl.RegisterAttempt(info.PaymentHash, &b) + _, err = pControl.RegisterAttempt(info.PaymentIdentifier, &b) if err != ErrNonMPPayment { t.Fatalf("expected ErrNonMPPayment, got: %v", err) } diff --git a/channeldb/payments.go b/channeldb/payments.go index 2ffd76a0..96748d6f 100644 --- a/channeldb/payments.go +++ b/channeldb/payments.go @@ -216,8 +216,9 @@ func (ps PaymentStatus) String() string { // PaymentCreationInfo is the information necessary to have ready when // initiating a payment, moving it into state InFlight. type PaymentCreationInfo struct { - // PaymentHash is the hash this payment is paying to. - PaymentHash lntypes.Hash + // 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 @@ -856,7 +857,7 @@ func fetchSequenceNumbers(paymentBucket kvdb.RBucket) ([][]byte, error) { func serializePaymentCreationInfo(w io.Writer, c *PaymentCreationInfo) error { var scratch [8]byte - if _, err := w.Write(c.PaymentHash[:]); err != nil { + if _, err := w.Write(c.PaymentIdentifier[:]); err != nil { return err } @@ -886,7 +887,7 @@ func deserializePaymentCreationInfo(r io.Reader) (*PaymentCreationInfo, error) { c := &PaymentCreationInfo{} - if _, err := io.ReadFull(r, c.PaymentHash[:]); err != nil { + if _, err := io.ReadFull(r, c.PaymentIdentifier[:]); err != nil { return nil, err } diff --git a/channeldb/payments_test.go b/channeldb/payments_test.go index 8974865a..c7f6e98a 100644 --- a/channeldb/payments_test.go +++ b/channeldb/payments_test.go @@ -60,8 +60,8 @@ func makeFakeInfo() (*PaymentCreationInfo, *HTLCAttemptInfo) { hash := preimg.Hash() c := &PaymentCreationInfo{ - PaymentHash: hash, - Value: 1000, + PaymentIdentifier: hash, + Value: 1000, // Use single second precision to avoid false positive test // failures due to the monotonic time component. CreationTime: time.Unix(time.Now().Unix(), 0), @@ -433,7 +433,7 @@ func TestQueryPayments(t *testing.T) { } // Create a new payment entry in the database. - err = pControl.InitPayment(info.PaymentHash, info) + err = pControl.InitPayment(info.PaymentIdentifier, info) if err != nil { t.Fatalf("unable to initialize "+ "payment in database: %v", err) @@ -442,11 +442,11 @@ func TestQueryPayments(t *testing.T) { // Immediately delete the payment with index 2. if i == 1 { pmt, err := pControl.FetchPayment( - info.PaymentHash, + info.PaymentIdentifier, ) require.NoError(t, err) - deletePayment(t, db, info.PaymentHash, + deletePayment(t, db, info.PaymentIdentifier, pmt.SequenceNum) } @@ -456,13 +456,13 @@ func TestQueryPayments(t *testing.T) { // duplicate payments will always be succeeded. if i == (nonDuplicatePayments - 1) { pmt, err := pControl.FetchPayment( - info.PaymentHash, + info.PaymentIdentifier, ) require.NoError(t, err) appendDuplicatePayment( t, pControl.db, - info.PaymentHash, + info.PaymentIdentifier, pmt.SequenceNum+1, preimg, ) @@ -529,12 +529,12 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) { require.NoError(t, err) // Create a new payment entry in the database. - err = pControl.InitPayment(noDuplicates.PaymentHash, noDuplicates) + err = pControl.InitPayment(noDuplicates.PaymentIdentifier, noDuplicates) require.NoError(t, err) // Fetch the payment so we can get its sequence nr. noDuplicatesPayment, err := pControl.FetchPayment( - noDuplicates.PaymentHash, + noDuplicates.PaymentIdentifier, ) require.NoError(t, err) @@ -543,12 +543,12 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) { require.NoError(t, err) // Create a new payment entry in the database. - err = pControl.InitPayment(hasDuplicates.PaymentHash, hasDuplicates) + err = pControl.InitPayment(hasDuplicates.PaymentIdentifier, hasDuplicates) require.NoError(t, err) // Fetch the payment so we can get its sequence nr. hasDuplicatesPayment, err := pControl.FetchPayment( - hasDuplicates.PaymentHash, + hasDuplicates.PaymentIdentifier, ) require.NoError(t, err) @@ -561,10 +561,10 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) { // Add two duplicates to our second payment. appendDuplicatePayment( - t, db, hasDuplicates.PaymentHash, duplicateOneSeqNr, preimg, + t, db, hasDuplicates.PaymentIdentifier, duplicateOneSeqNr, preimg, ) appendDuplicatePayment( - t, db, hasDuplicates.PaymentHash, duplicateTwoSeqNr, preimg, + t, db, hasDuplicates.PaymentIdentifier, duplicateTwoSeqNr, preimg, ) tests := []struct { @@ -575,37 +575,37 @@ func TestFetchPaymentWithSequenceNumber(t *testing.T) { }{ { name: "lookup payment without duplicates", - paymentHash: noDuplicates.PaymentHash, + paymentHash: noDuplicates.PaymentIdentifier, sequenceNumber: noDuplicatesPayment.SequenceNum, expectedErr: nil, }, { name: "lookup payment with duplicates", - paymentHash: hasDuplicates.PaymentHash, + paymentHash: hasDuplicates.PaymentIdentifier, sequenceNumber: hasDuplicatesPayment.SequenceNum, expectedErr: nil, }, { name: "lookup first duplicate", - paymentHash: hasDuplicates.PaymentHash, + paymentHash: hasDuplicates.PaymentIdentifier, sequenceNumber: duplicateOneSeqNr, expectedErr: nil, }, { name: "lookup second duplicate", - paymentHash: hasDuplicates.PaymentHash, + paymentHash: hasDuplicates.PaymentIdentifier, sequenceNumber: duplicateTwoSeqNr, expectedErr: nil, }, { name: "lookup non-existent duplicate", - paymentHash: hasDuplicates.PaymentHash, + paymentHash: hasDuplicates.PaymentIdentifier, sequenceNumber: 999999, expectedErr: ErrDuplicateNotFound, }, { name: "lookup duplicate, no duplicates bucket", - paymentHash: noDuplicates.PaymentHash, + paymentHash: noDuplicates.PaymentIdentifier, sequenceNumber: duplicateTwoSeqNr, expectedErr: ErrNoDuplicateBucket, }, diff --git a/lnrpc/routerrpc/router_backend.go b/lnrpc/routerrpc/router_backend.go index 66fab66e..9cf2c710 100644 --- a/lnrpc/routerrpc/router_backend.go +++ b/lnrpc/routerrpc/router_backend.go @@ -698,7 +698,11 @@ func (r *RouterBackend) extractIntentFromSendRequest( payIntent.MaxParts = 1 } - copy(payIntent.PaymentHash[:], payReq.PaymentHash[:]) + err = payIntent.SetPaymentHash(*payReq.PaymentHash) + if err != nil { + return nil, err + } + destKey := payReq.Destination.SerializeCompressed() copy(payIntent.Target[:], destKey) @@ -737,7 +741,15 @@ func (r *RouterBackend) extractIntentFromSendRequest( payIntent.Amount = reqAmt // Payment hash. - copy(payIntent.PaymentHash[:], rpcPayReq.PaymentHash) + paymentHash, err := lntypes.MakeHash(rpcPayReq.PaymentHash) + if err != nil { + return nil, err + } + + err = payIntent.SetPaymentHash(paymentHash) + if err != nil { + return nil, err + } // Parse destination feature bits. features, err := UnmarshalFeatures(rpcPayReq.DestFeatures) @@ -1217,7 +1229,7 @@ func (r *RouterBackend) MarshallPayment(payment *channeldb.MPPayment) ( htlcs = append(htlcs, htlc) } - paymentHash := payment.Info.PaymentHash + paymentID := payment.Info.PaymentIdentifier creationTimeNS := MarshalTimeNano(payment.Info.CreationTime) failureReason, err := marshallPaymentFailureReason( @@ -1228,7 +1240,8 @@ func (r *RouterBackend) MarshallPayment(payment *channeldb.MPPayment) ( } return &lnrpc.Payment{ - PaymentHash: hex.EncodeToString(paymentHash[:]), + // TODO: set this to setID for AMP-payments? + PaymentHash: hex.EncodeToString(paymentID[:]), Value: satValue, ValueMsat: msatValue, ValueSat: satValue, diff --git a/lnrpc/routerrpc/router_server.go b/lnrpc/routerrpc/router_server.go index ba9d9a9e..8c40517d 100644 --- a/lnrpc/routerrpc/router_server.go +++ b/lnrpc/routerrpc/router_server.go @@ -316,21 +316,21 @@ func (s *Server) SendPaymentV2(req *SendPaymentRequest, if err == channeldb.ErrPaymentInFlight || err == channeldb.ErrAlreadyPaid { - log.Debugf("SendPayment async result for hash %x: %v", - payment.PaymentHash, err) + log.Debugf("SendPayment async result for payment %x: %v", + payment.Identifier(), err) return status.Error( codes.AlreadyExists, err.Error(), ) } - log.Errorf("SendPayment async error for hash %x: %v", - payment.PaymentHash, err) + log.Errorf("SendPayment async error for payment %x: %v", + payment.Identifier(), err) return err } - return s.trackPayment(payment.PaymentHash, stream, req.NoInflightUpdates) + return s.trackPayment(payment.Identifier(), stream, req.NoInflightUpdates) } // EstimateRouteFee allows callers to obtain a lower bound w.r.t how much it @@ -719,14 +719,14 @@ func (s *Server) TrackPaymentV2(request *TrackPaymentRequest, } // trackPayment writes payment status updates to the provided stream. -func (s *Server) trackPayment(paymentHash lntypes.Hash, +func (s *Server) trackPayment(identifier lntypes.Hash, stream Router_TrackPaymentV2Server, noInflightUpdates bool) error { router := s.cfg.RouterBackend // Subscribe to the outcome of this payment. subscription, err := router.Tower.SubscribePayment( - paymentHash, + identifier, ) switch { case err == channeldb.ErrPaymentNotInitiated: @@ -769,7 +769,7 @@ func (s *Server) trackPayment(paymentHash lntypes.Hash, return errServerShuttingDown case <-stream.Context().Done(): - log.Debugf("Payment status stream %v canceled", paymentHash) + log.Debugf("Payment status stream %v canceled", identifier) return stream.Context().Err() } } diff --git a/lntest/itest/log_error_whitelist.txt b/lntest/itest/log_error_whitelist.txt index 08b3419f..ec164c9d 100644 --- a/lntest/itest/log_error_whitelist.txt +++ b/lntest/itest/log_error_whitelist.txt @@ -36,16 +36,16 @@