Skip to content

Commit

Permalink
Merge pull request #9566 from yyforyongyu/improve-itest
Browse files Browse the repository at this point in the history
lntest+itest: change the method signature of `AssertPaymentStatus`
  • Loading branch information
guggero authored Feb 28, 2025
2 parents dfd43c9 + 2d5a2ce commit dc0ba72
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion itest/lnd_experimental_endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func testEndorsement(ht *lntest.HarnessTest, aliceEndorse bool) {

var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)
}

func validateEndorsedAndResume(ht *lntest.HarnessTest,
Expand Down
8 changes: 4 additions & 4 deletions itest/lnd_forward_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func testForwardInterceptorDedupHtlc(ht *lntest.HarnessTest) {
// We expect one in flight payment since we held the htlcs.
var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)

// At this point if we have more than one held htlcs then we should
// fail. This means we hold the same htlc twice which is a risk we want
Expand Down Expand Up @@ -275,7 +275,7 @@ func testForwardInterceptorBasic(ht *lntest.HarnessTest) {
copy(preimage[:], testCase.invoice.RPreimage)

payment := ht.AssertPaymentStatus(
alice, preimage, lnrpc.Payment_IN_FLIGHT,
alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT,
)
expectedAmt := testCase.invoice.ValueMsat
require.Equal(ht, expectedAmt, payment.ValueMsat,
Expand Down Expand Up @@ -408,7 +408,7 @@ func testForwardInterceptorRestart(ht *lntest.HarnessTest) {
// The payment should now be in flight.
var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)

// We don't resume the payment on Carol, so it should be held there.
// We now restart first Bob, then Alice, so we can make sure we've
Expand Down Expand Up @@ -456,7 +456,7 @@ func testForwardInterceptorRestart(ht *lntest.HarnessTest) {

// Assert that the payment was successful.
ht.AssertPaymentStatus(
alice, preimage, lnrpc.Payment_SUCCEEDED,
alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED,
func(p *lnrpc.Payment) error {
recordsEqual := reflect.DeepEqual(
lntest.CustomRecordsWithUnendorsed(
Expand Down
8 changes: 3 additions & 5 deletions itest/lnd_hold_persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ func testHoldInvoicePersistence(ht *lntest.HarnessTest) {
payStream := alice.RPC.TrackPaymentV2(hash[:])
ht.ReceiveTrackPayment(payStream)

ht.AssertPaymentStatus(
alice, preimg, lnrpc.Payment_IN_FLIGHT,
)
ht.AssertPaymentStatus(alice, hash, lnrpc.Payment_IN_FLIGHT)
}

// Settle invoices half the invoices, cancel the rest.
Expand All @@ -211,11 +209,11 @@ func testHoldInvoicePersistence(ht *lntest.HarnessTest) {
for i, preimg := range preimages {
if i%2 == 0 {
ht.AssertPaymentStatus(
alice, preimg, lnrpc.Payment_SUCCEEDED,
alice, preimg.Hash(), lnrpc.Payment_SUCCEEDED,
)
} else {
payment := ht.AssertPaymentStatus(
alice, preimg, lnrpc.Payment_FAILED,
alice, preimg.Hash(), lnrpc.Payment_FAILED,
)
require.Equal(ht, reason, payment.FailureReason,
"wrong failure reason")
Expand Down
4 changes: 2 additions & 2 deletions itest/lnd_htlc_timeout_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func testHtlcTimeoutResolverExtractPreimageRemote(ht *lntest.HarnessTest) {
// Finally, check that the Alice's payment is marked as succeeded as
// Bob has settled the htlc using the preimage extracted from Carol's
// 2nd level success tx.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)

// Mine a block to clean the mempool.
ht.MineBlocksAndAssertNumTxes(1, 2)
Expand Down Expand Up @@ -371,7 +371,7 @@ func testHtlcTimeoutResolverExtractPreimageLocal(ht *lntest.HarnessTest) {
// Finally, check that the Alice's payment is marked as succeeded as
// Bob has settled the htlc using the preimage extracted from Carol's
// direct spend tx.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)

// NOTE: for non-standby nodes there's no need to clean up the force
// close as long as the mempool is cleaned.
Expand Down
2 changes: 1 addition & 1 deletion itest/lnd_mpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func testSendToRouteMultiPath(ht *lntest.HarnessTest) {
copy(preimage[:], invoices[0].RPreimage)

payment := ht.AssertPaymentStatus(
hn, preimage, lnrpc.Payment_SUCCEEDED,
hn, preimage.Hash(), lnrpc.Payment_SUCCEEDED,
)

htlcs := payment.Htlcs
Expand Down
10 changes: 5 additions & 5 deletions itest/lnd_multi-hop_force_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ func runMultiHopReceiverPreimageClaim(ht *lntest.HarnessTest,
ht.AssertNumActiveHtlcs(alice, 0)

// Check that the Alice's payment is correctly marked succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)

// Carol's pending channel report should now show two outputs under
// limbo: her commitment output, as well as the second-layer claim
Expand Down Expand Up @@ -1918,7 +1918,7 @@ func runLocalClaimIncomingHTLC(ht *lntest.HarnessTest,

// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)
}

// testLocalClaimIncomingHTLCLeasedZeroConf tests
Expand Down Expand Up @@ -2221,7 +2221,7 @@ func runLocalClaimIncomingHTLCLeased(ht *lntest.HarnessTest,

// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)
}

// testLocalPreimageClaimAnchorZeroConf tests `runLocalPreimageClaim` with
Expand Down Expand Up @@ -2575,7 +2575,7 @@ func runLocalPreimageClaim(ht *lntest.HarnessTest,

// Finally, check that the Alice's payment is correctly marked
// succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)
}

// testLocalPreimageClaimLeasedZeroConf tests `runLocalPreimageClaim` with
Expand Down Expand Up @@ -2839,7 +2839,7 @@ func runLocalPreimageClaimLeased(ht *lntest.HarnessTest,
ht.AssertInvoiceState(stream, lnrpc.Invoice_SETTLED)

// Check that the Alice's payment is correctly marked succeeded.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)

// With the script-enforced lease commitment type, Alice and Bob still
// haven't been able to sweep their respective commit outputs due to
Expand Down
38 changes: 22 additions & 16 deletions itest/lnd_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ func testPaymentSucceededHTLCRemoteSwept(ht *lntest.HarnessTest) {

// We also check the payments are marked as IN_FLIGHT in Alice's
// database.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(
alice, dustPreimage.Hash(), lnrpc.Payment_IN_FLIGHT,
)

// Bob should have two incoming HTLC.
ht.AssertIncomingHTLCActive(bob, chanPoint, payHash[:])
Expand Down Expand Up @@ -166,12 +168,12 @@ func testPaymentSucceededHTLCRemoteSwept(ht *lntest.HarnessTest) {
dustPayStream := alice.RPC.TrackPaymentV2(dustPayHash[:])

// Check that the dust payment is failed in both the stream and DB.
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_FAILED)
ht.AssertPaymentStatus(alice, dustPreimage.Hash(), lnrpc.Payment_FAILED)
ht.AssertPaymentStatusFromStream(dustPayStream, lnrpc.Payment_FAILED)

// We expect the non-dust payment to marked as succeeded in Alice's
// database and also from her stream.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_SUCCEEDED)
ht.AssertPaymentStatusFromStream(payStream, lnrpc.Payment_SUCCEEDED)
}

Expand Down Expand Up @@ -263,8 +265,10 @@ func runTestPaymentHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {

// We also check the payments are marked as IN_FLIGHT in Alice's
// database.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(
alice, dustPreimage.Hash(), lnrpc.Payment_IN_FLIGHT,
)

// Bob should have two incoming HTLC.
ht.AssertIncomingHTLCActive(bob, chanPoint, payHash[:])
Expand Down Expand Up @@ -310,14 +314,14 @@ func runTestPaymentHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {
// mark the non-dust payment as succeeded.
//
// Check that the dust payment is failed in both the stream and DB.
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_FAILED)
ht.AssertPaymentStatus(alice, dustPreimage.Hash(), lnrpc.Payment_FAILED)
ht.AssertPaymentStatusFromStream(dustPayStream, lnrpc.Payment_FAILED)

// Check that the non-dust payment is still in-flight.
//
// NOTE: we don't check the payment status from the stream here as
// there's no new status being sent.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)

// We now have two possible cases for the non-dust payment:
// - Bob stays offline, and Alice will sweep her outgoing HTLC, which
Expand All @@ -335,7 +339,7 @@ func runTestPaymentHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {

// We expect the non-dust payment to marked as failed in Alice's
// database and also from her stream.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_FAILED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_FAILED)
ht.AssertPaymentStatusFromStream(payStream, lnrpc.Payment_FAILED)
}

Expand Down Expand Up @@ -1114,7 +1118,7 @@ func sendPaymentInterceptAndCancel(ht *lntest.HarnessTest,
var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
payment := ht.AssertPaymentStatus(
alice, preimage, lnrpc.Payment_IN_FLIGHT,
alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT,
)
reasonNone := lnrpc.PaymentFailureReason_FAILURE_REASON_NONE
require.Equal(ht, reasonNone, payment.FailureReason)
Expand All @@ -1127,7 +1131,7 @@ func sendPaymentInterceptAndCancel(ht *lntest.HarnessTest,
require.NoError(ht, err, "failed to send request")

// Assert that the payment status is as expected.
ht.AssertPaymentStatus(alice, preimage, expectedPaymentStatus)
ht.AssertPaymentStatus(alice, preimage.Hash(), expectedPaymentStatus)

// Since the payment context was cancelled, no further payment attempts
// should've been made, and we observe FAILURE_REASON_CANCELED.
Expand Down Expand Up @@ -1245,7 +1249,7 @@ func runSendToRouteFailHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {
}()

// Check that the payment is in-flight.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)

// Construct a route to send the dust payment.
go func() {
Expand Down Expand Up @@ -1281,7 +1285,9 @@ func runSendToRouteFailHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {
}()

// Check that the dust payment is in-flight.
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(
alice, dustPreimage.Hash(), lnrpc.Payment_IN_FLIGHT,
)

// Bob should have two incoming HTLC.
ht.AssertIncomingHTLCActive(bob, chanPoint, payHash[:])
Expand Down Expand Up @@ -1323,14 +1329,14 @@ func runSendToRouteFailHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {
// HTLC is resolved onchain.
//
// Check that the dust payment is failed in both the stream and DB.
ht.AssertPaymentStatus(alice, dustPreimage, lnrpc.Payment_FAILED)
ht.AssertPaymentStatus(alice, dustPreimage.Hash(), lnrpc.Payment_FAILED)
ht.AssertPaymentStatusFromStream(dustPayStream, lnrpc.Payment_FAILED)

// Check that the non-dust payment is still in-flight.
//
// NOTE: we don't check the payment status from the stream here as
// there's no new status being sent.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_IN_FLIGHT)

// We now have two possible cases for the non-dust payment:
// - Bob stays offline, and Alice will sweep her outgoing HTLC, which
Expand All @@ -1348,6 +1354,6 @@ func runSendToRouteFailHTLCTimeout(ht *lntest.HarnessTest, restartAlice bool) {

// We expect the non-dust payment to marked as failed in Alice's
// database and also from her stream.
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_FAILED)
ht.AssertPaymentStatus(alice, preimage.Hash(), lnrpc.Payment_FAILED)
ht.AssertPaymentStatusFromStream(payStream, lnrpc.Payment_FAILED)
}
2 changes: 1 addition & 1 deletion itest/lnd_route_blinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func (b *blindedForwardTest) sendToRoute(route *lnrpc.Route,
require.NoError(b.ht, err)

pmt := b.ht.AssertPaymentStatus(
b.alice, preimage, lnrpc.Payment_FAILED,
b.alice, preimage.Hash(), lnrpc.Payment_FAILED,
)
require.Len(b.ht, pmt.Htlcs, 1)

Expand Down
18 changes: 10 additions & 8 deletions lntest/harness_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,16 +1550,15 @@ func (h *HarnessTest) findPayment(hn *node.HarnessNode,
// PaymentCheck is a function that checks a payment for a specific condition.
type PaymentCheck func(*lnrpc.Payment) error

// AssertPaymentStatus asserts that the given node list a payment with the
// given preimage has the expected status. It also checks that the payment has
// the expected preimage, which is empty when it's not settled and matches the
// given preimage when it's succeeded.
// AssertPaymentStatus asserts that the given node list a payment with the given
// payment hash has the expected status. It also checks that the payment has the
// expected preimage, which is empty when it's not settled and matches the given
// preimage when it's succeeded.
func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,
preimage lntypes.Preimage, status lnrpc.Payment_PaymentStatus,
payHash lntypes.Hash, status lnrpc.Payment_PaymentStatus,
checks ...PaymentCheck) *lnrpc.Payment {

var target *lnrpc.Payment
payHash := preimage.Hash()

err := wait.NoError(func() error {
p, err := h.findPayment(hn, payHash.String())
Expand All @@ -1581,8 +1580,11 @@ func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,
// If this expected status is SUCCEEDED, we expect the final
// preimage.
case lnrpc.Payment_SUCCEEDED:
require.Equal(h, preimage.String(), target.PaymentPreimage,
"preimage not match")
preimage, err := lntypes.MakePreimageFromStr(
target.PaymentPreimage,
)
require.NoError(h, err, "fail to make preimage")
require.Equal(h, payHash, preimage.Hash(), "preimage not match")

// Otherwise we expect an all-zero preimage.
default:
Expand Down

0 comments on commit dc0ba72

Please sign in to comment.