htlcswitch/test: test canceled invoice

This commit is contained in:
Joost Jager 2019-01-29 14:54:08 +01:00
parent f80fb286d8
commit 077852cd4e
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
2 changed files with 68 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -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.