From 077852cd4e468c953b27e900a8adde21243f1459 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 29 Jan 2019 14:54:08 +0100 Subject: [PATCH] htlcswitch/test: test canceled invoice --- htlcswitch/link_test.go | 57 ++++++++++++++++++++++++++++++++++++++++ htlcswitch/test_utils.go | 11 ++++++++ 2 files changed, 68 insertions(+) diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 94336172..c0b6e2a1 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -5201,3 +5201,60 @@ func TestHtlcSatisfyPolicy(t *testing.T) { } }) } + +// TestChannelLinkCanceledInvoice in this test checks the interaction +// between Alice and Bob for a canceled invoice. +func TestChannelLinkCanceledInvoice(t *testing.T) { + t.Parallel() + + // Setup a alice-bob network. + aliceChannel, bobChannel, cleanUp, err := createTwoClusterChannels( + btcutil.SatoshiPerBitcoin*3, + btcutil.SatoshiPerBitcoin*5) + if err != nil { + t.Fatalf("unable to create channel: %v", err) + } + defer cleanUp() + + n := newTwoHopNetwork(t, aliceChannel, bobChannel, testStartingHeight) + if err := n.start(); err != nil { + t.Fatal(err) + } + defer n.stop() + + // Prepare an alice -> bob payment. + amount := lnwire.NewMSatFromSatoshis(btcutil.SatoshiPerBitcoin) + htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight, + n.bobChannelLink) + + firstHop := n.bobChannelLink.ShortChanID() + + invoice, payFunc, err := preparePayment( + n.aliceServer, n.bobServer, firstHop, hops, amount, htlcAmt, + totalTimelock, + ) + if err != nil { + t.Fatalf("unable to prepare the payment: %v", err) + } + + // Cancel the invoice at bob's end. + hash := invoice.Terms.PaymentPreimage.Hash() + err = n.bobServer.registry.CancelInvoice(hash) + if err != nil { + t.Fatal(err) + } + + // Have Alice fire the payment. + err = waitForPayFuncResult(payFunc, 30*time.Second) + + // Because the invoice is canceled, we expect an unknown payment hash + // result. + fErr, ok := err.(*ForwardingError) + if !ok { + t.Fatalf("expected ForwardingError, but got %v", err) + } + _, ok = fErr.FailureMessage.(*lnwire.FailUnknownPaymentHash) + if !ok { + t.Fatalf("expected unknown payment hash, but got %v", err) + } +} diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 3b14b33f..58bcd856 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -670,6 +670,17 @@ func waitForPaymentResult(c chan error, d time.Duration) error { } } +// waitForPayFuncResult executes the given function and waits for a result with +// a timeout. +func waitForPayFuncResult(payFunc func() error, d time.Duration) error { + errChan := make(chan error) + go func() { + errChan <- payFunc() + }() + + return waitForPaymentResult(errChan, d) +} + // makePayment takes the destination node and amount as input, sends the // payment and returns the error channel to wait for error to be received and // invoice in order to check its status after the payment finished.