mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-28 15:13:04 +01:00
routing+routerrpc: notify full payment structures
This commit fixes the inconsistency between the payment state as reported by routerrpc.SendPayment/routerrpc.TrackPayment and the main rpc ListPayments call. In addition to that, payment state changes are now sent out for every state change. This opens the door to user interfaces giving more feedback to the user about the payment process. This is especially interesting for multi-part payments.
This commit is contained in:
@@ -55,7 +55,7 @@ func TestControlTowerSubscribeUnknown(t *testing.T) {
|
||||
pControl := NewControlTower(channeldb.NewPaymentControl(db))
|
||||
|
||||
// Subscription should fail when the payment is not known.
|
||||
_, _, err = pControl.SubscribePayment(lntypes.Hash{1})
|
||||
_, err = pControl.SubscribePayment(lntypes.Hash{1})
|
||||
if err != channeldb.ErrPaymentNotInitiated {
|
||||
t.Fatal("expected subscribe to fail for unknown payment")
|
||||
}
|
||||
@@ -86,13 +86,10 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
|
||||
|
||||
// Subscription should succeed and immediately report the InFlight
|
||||
// status.
|
||||
inFlight, subscriber1, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
subscriber1, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("expected subscribe to succeed, but got: %v", err)
|
||||
}
|
||||
if !inFlight {
|
||||
t.Fatalf("unexpected payment to be in flight")
|
||||
}
|
||||
|
||||
// Register an attempt.
|
||||
err = pControl.RegisterAttempt(info.PaymentHash, attempt)
|
||||
@@ -101,13 +98,10 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
// Register a second subscriber after the first attempt has started.
|
||||
inFlight, subscriber2, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
subscriber2, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("expected subscribe to succeed, but got: %v", err)
|
||||
}
|
||||
if !inFlight {
|
||||
t.Fatalf("unexpected payment to be in flight")
|
||||
}
|
||||
|
||||
// Mark the payment as successful.
|
||||
err = pControl.SettleAttempt(
|
||||
@@ -121,32 +115,33 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
// Register a third subscriber after the payment succeeded.
|
||||
inFlight, subscriber3, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
subscriber3, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("expected subscribe to succeed, but got: %v", err)
|
||||
}
|
||||
if inFlight {
|
||||
t.Fatalf("expected payment to be finished")
|
||||
}
|
||||
|
||||
// We expect all subscribers to now report the final outcome followed by
|
||||
// no other events.
|
||||
subscribers := []chan PaymentResult{
|
||||
subscribers := []*ControlTowerSubscriber{
|
||||
subscriber1, subscriber2, subscriber3,
|
||||
}
|
||||
|
||||
for _, s := range subscribers {
|
||||
var result PaymentResult
|
||||
select {
|
||||
case result = <-s:
|
||||
case <-time.After(testTimeout):
|
||||
t.Fatal("timeout waiting for payment result")
|
||||
var result *channeldb.MPPayment
|
||||
for result == nil || result.Status == channeldb.StatusInFlight {
|
||||
select {
|
||||
case item := <-s.Updates:
|
||||
result = item.(*channeldb.MPPayment)
|
||||
case <-time.After(testTimeout):
|
||||
t.Fatal("timeout waiting for payment result")
|
||||
}
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
if result.Status != channeldb.StatusSucceeded {
|
||||
t.Fatal("unexpected payment state")
|
||||
}
|
||||
if result.Preimage != preimg {
|
||||
settle, _ := result.TerminalInfo()
|
||||
if settle.Preimage != preimg {
|
||||
t.Fatal("unexpected preimage")
|
||||
}
|
||||
if len(result.HTLCs) != 1 {
|
||||
@@ -161,7 +156,7 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
|
||||
|
||||
// After the final event, we expect the channel to be closed.
|
||||
select {
|
||||
case _, ok := <-s:
|
||||
case _, ok := <-s.Updates:
|
||||
if ok {
|
||||
t.Fatal("expected channel to be closed")
|
||||
}
|
||||
@@ -204,7 +199,7 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt bool) {
|
||||
}
|
||||
|
||||
// Subscription should succeed.
|
||||
_, subscriber1, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
subscriber1, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("expected subscribe to succeed, but got: %v", err)
|
||||
}
|
||||
@@ -235,29 +230,29 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt bool) {
|
||||
}
|
||||
|
||||
// Register a second subscriber after the payment failed.
|
||||
inFlight, subscriber2, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
subscriber2, err := pControl.SubscribePayment(info.PaymentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("expected subscribe to succeed, but got: %v", err)
|
||||
}
|
||||
if inFlight {
|
||||
t.Fatalf("expected payment to be finished")
|
||||
}
|
||||
|
||||
// We expect all subscribers to now report the final outcome followed by
|
||||
// no other events.
|
||||
subscribers := []chan PaymentResult{
|
||||
subscribers := []*ControlTowerSubscriber{
|
||||
subscriber1, subscriber2,
|
||||
}
|
||||
|
||||
for _, s := range subscribers {
|
||||
var result PaymentResult
|
||||
select {
|
||||
case result = <-s:
|
||||
case <-time.After(testTimeout):
|
||||
t.Fatal("timeout waiting for payment result")
|
||||
var result *channeldb.MPPayment
|
||||
for result == nil || result.Status == channeldb.StatusInFlight {
|
||||
select {
|
||||
case item := <-s.Updates:
|
||||
result = item.(*channeldb.MPPayment)
|
||||
case <-time.After(testTimeout):
|
||||
t.Fatal("timeout waiting for payment result")
|
||||
}
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
if result.Status == channeldb.StatusSucceeded {
|
||||
t.Fatal("unexpected payment state")
|
||||
}
|
||||
|
||||
@@ -282,13 +277,13 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt bool) {
|
||||
len(result.HTLCs))
|
||||
}
|
||||
|
||||
if result.FailureReason != channeldb.FailureReasonTimeout {
|
||||
if *result.FailureReason != channeldb.FailureReasonTimeout {
|
||||
t.Fatal("unexpected failure reason")
|
||||
}
|
||||
|
||||
// After the final event, we expect the channel to be closed.
|
||||
select {
|
||||
case _, ok := <-s:
|
||||
case _, ok := <-s.Updates:
|
||||
if ok {
|
||||
t.Fatal("expected channel to be closed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user