Skip to content

Commit

Permalink
htlcswitch/test: test canceled invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Feb 3, 2019
1 parent 05c3722 commit c218675
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
57 changes: 57 additions & 0 deletions htlcswitch/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
11 changes: 11 additions & 0 deletions htlcswitch/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c218675

Please sign in to comment.