Skip to content

Commit

Permalink
itest+lntest: assert payment is failed once the htlc times out
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Jan 27, 2025
1 parent f6a6eb0 commit fd3c5c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
30 changes: 26 additions & 4 deletions itest/lnd_multi-hop_force_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,11 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
// We'll create two random payment hashes unknown to carol, then send
// each of them by manually specifying the HTLC details.
carolPubKey := carol.PubKey[:]
dustPayHash := ht.Random32Bytes()
payHash := ht.Random32Bytes()

preimageDust := ht.RandomPreimage()
preimage := ht.RandomPreimage()
dustPayHash := preimageDust.Hash()
payHash := preimage.Hash()

// If this is a taproot channel, then we'll need to make some manual
// route hints so Alice can actually find a route.
Expand All @@ -370,7 +373,7 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
req := &routerrpc.SendPaymentRequest{
Dest: carolPubKey,
Amt: int64(dustHtlcAmt),
PaymentHash: dustPayHash,
PaymentHash: dustPayHash[:],
FinalCltvDelta: finalCltvDelta,
FeeLimitMsat: noFeeLimitMsat,
RouteHints: routeHints,
Expand All @@ -380,7 +383,7 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
req = &routerrpc.SendPaymentRequest{
Dest: carolPubKey,
Amt: int64(htlcAmt),
PaymentHash: payHash,
PaymentHash: payHash[:],
FinalCltvDelta: finalCltvDelta,
FeeLimitMsat: noFeeLimitMsat,
RouteHints: routeHints,
Expand Down Expand Up @@ -530,6 +533,25 @@ func runLocalClaimOutgoingHTLC(ht *lntest.HarnessTest,
// Once this transaction has been confirmed, Bob should detect that he
// no longer has any pending channels.
ht.AssertNumPendingForceClose(bob, 0)

// Now that Bob has claimed his HTLCs, Alice should mark the two
// payments as failed.
//
// Alice will mark this payment as failed with no route as the only
// route she has is Alice->Bob->Carol. This won't be the case if she
// has a second route, as another attempt will be tried.
//
// TODO(yy): we should instead mark this payment as timed out if she has
// a second route to try this payment, which is the timeout set by Alice
// when sending the payment.
expectedReason := lnrpc.PaymentFailureReason_FAILURE_REASON_NO_ROUTE
p := ht.AssertPaymentFailureReason(alice, preimage, expectedReason)
require.Equal(ht, lnrpc.Failure_PERMANENT_CHANNEL_FAILURE,
p.Htlcs[0].Failure.Code)

p = ht.AssertPaymentFailureReason(alice, preimageDust, expectedReason)
require.Equal(ht, lnrpc.Failure_PERMANENT_CHANNEL_FAILURE,
p.Htlcs[0].Failure.Code)
}

// testMultiHopReceiverPreimageClaimAnchor tests
Expand Down
14 changes: 11 additions & 3 deletions lntest/harness_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1600,8 +1600,11 @@ func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,

// AssertPaymentFailureReason asserts that the given node lists a payment with
// the given preimage which has the expected failure reason.
func (h *HarnessTest) AssertPaymentFailureReason(hn *node.HarnessNode,
preimage lntypes.Preimage, reason lnrpc.PaymentFailureReason) {
func (h *HarnessTest) AssertPaymentFailureReason(
hn *node.HarnessNode, preimage lntypes.Preimage,
reason lnrpc.PaymentFailureReason) *lnrpc.Payment {

var payment *lnrpc.Payment

payHash := preimage.Hash()
err := wait.NoError(func() error {
Expand All @@ -1610,14 +1613,19 @@ func (h *HarnessTest) AssertPaymentFailureReason(hn *node.HarnessNode,
return err
}

payment = p

if reason == p.FailureReason {
return nil
}

return fmt.Errorf("payment: %v failure reason not match, "+
"want %s got %s", payHash, reason, p.Status)
"want %s(%d) got %s(%d)", payHash, reason, reason,
p.FailureReason, p.FailureReason)
}, DefaultTimeout)
require.NoError(h, err, "timeout checking payment failure reason")

return payment
}

// AssertActiveNodesSynced asserts all active nodes have synced to the chain.
Expand Down

0 comments on commit fd3c5c5

Please sign in to comment.