itest+lntemp: refactor testTrackPayments

This commit is contained in:
yyforyongyu committed 2023-01-18 10:53:58 +08:00
1 parent 368b3337bd
commit 1cb7ea1a0c
4 files changed
+52 -53

No files matched your search

+12
View File
@@ -177,3 +177,15 @@ func (h *HarnessRPC) HtlcInterceptor() (InterceptorClient, context.CancelFunc) {
return resp, cancel return resp, cancel
} }
type TrackPaymentsClient routerrpc.Router_TrackPaymentsClient
// TrackPayments makes a RPC call to the node's RouterClient and asserts.
func (h *HarnessRPC) TrackPayments(
req *routerrpc.TrackPaymentsRequest) TrackPaymentsClient {
resp, err := h.Router.TrackPayments(h.runCtx, req)
h.NoError(err, "TrackPayments")
return resp
}
+4
View File
@@ -489,4 +489,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "taproot coop close", Name: "taproot coop close",
TestFunc: testTaprootCoopClose, TestFunc: testTaprootCoopClose,
}, },
{
Name: "trackpayments",
TestFunc: testTrackPayments,
},
} }
-4
View File
@@ -8,10 +8,6 @@ var allTestCases = []*testCase{
name: "async bidirectional payments", name: "async bidirectional payments",
test: testBidirectionalAsyncPayments, test: testBidirectionalAsyncPayments,
}, },
{
name: "trackpayments",
test: testTrackPayments,
},
{ {
name: "open channel fee policy", name: "open channel fee policy",
test: testOpenChannelUpdateFeePolicy, test: testOpenChannelUpdateFeePolicy,
+36 -49
View File
@@ -1,102 +1,89 @@
package itest package itest
import ( import (
"context"
"encoding/hex" "encoding/hex"
"time"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntemp"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// testTrackPayments tests whether a client that calls the TrackPayments api // testTrackPayments tests whether a client that calls the TrackPayments api
// receives payment updates. // receives payment updates.
func testTrackPayments(net *lntest.NetworkHarness, t *harnessTest) { func testTrackPayments(ht *lntemp.HarnessTest) {
// Open a channel between alice and bob. // Open a channel between alice and bob.
net.EnsureConnected(t.t, net.Alice, net.Bob) alice, bob := ht.Alice, ht.Bob
channel := openChannelAndAssert( channel := ht.OpenChannel(
t, net, net.Alice, net.Bob, alice, bob, lntemp.OpenChannelParams{
lntest.OpenChannelParams{
Amt: btcutil.Amount(300000), Amt: btcutil.Amount(300000),
}, },
) )
defer closeChannelAndAssert(t, net, net.Alice, channel, true)
err := net.Alice.WaitForNetworkChannelOpen(channel)
require.NoError(t.t, err, "unable to wait for channel to open")
ctxb := context.Background()
ctxt, cancelTracker := context.WithCancel(ctxb)
defer cancelTracker()
// Call the TrackPayments api to listen for payment updates. // Call the TrackPayments api to listen for payment updates.
tracker, err := net.Alice.RouterClient.TrackPayments( req := &routerrpc.TrackPaymentsRequest{
ctxt, NoInflightUpdates: false,
&routerrpc.TrackPaymentsRequest{ }
NoInflightUpdates: false, tracker := alice.RPC.TrackPayments(req)
},
)
require.NoError(t.t, err, "failed to call TrackPayments successfully.")
// Create an invoice from bob. // Create an invoice from bob.
var amountMsat int64 = 1000 var amountMsat int64 = 1000
invoiceResp, err := net.Bob.AddInvoice( invoiceResp := bob.RPC.AddInvoice(
ctxb,
&lnrpc.Invoice{ &lnrpc.Invoice{
ValueMsat: amountMsat, ValueMsat: amountMsat,
}, },
) )
require.NoError(t.t, err, "unable to add invoice.") invoice := bob.RPC.LookupInvoice(invoiceResp.RHash)
invoice, err := net.Bob.LookupInvoice(
ctxb,
&lnrpc.PaymentHash{
RHashStr: hex.EncodeToString(invoiceResp.RHash),
},
)
require.NoError(t.t, err, "unable to find invoice.")
// Send payment from alice to bob. // Send payment from alice to bob.
paymentClient, err := net.Alice.RouterClient.SendPaymentV2( paymentClient := alice.RPC.SendPayment(
ctxb,
&routerrpc.SendPaymentRequest{ &routerrpc.SendPaymentRequest{
PaymentRequest: invoice.PaymentRequest, PaymentRequest: invoice.PaymentRequest,
TimeoutSeconds: 60, TimeoutSeconds: 60,
}, },
) )
require.NoError(t.t, err, "unable to send payment.")
// Make sure the payment doesn't error due to invalid parameters or so. // Make sure the payment doesn't error due to invalid parameters or so.
_, err = paymentClient.Recv() _, err := paymentClient.Recv()
require.NoError(t.t, err, "unable to get payment update.") require.NoError(ht, err, "unable to get payment update.")
// Assert the first payment update is an inflight update. // Assert the first payment update is an inflight update.
update1, err := tracker.Recv() update1, err := tracker.Recv()
require.NoError(t.t, err, "unable to receive payment update 1.") require.NoError(ht, err, "unable to receive payment update 1.")
require.Equal( require.Equal(
t.t, lnrpc.PaymentFailureReason_FAILURE_REASON_NONE, ht, lnrpc.PaymentFailureReason_FAILURE_REASON_NONE,
update1.FailureReason, update1.FailureReason,
) )
require.Equal(t.t, lnrpc.Payment_IN_FLIGHT, update1.Status) require.Equal(ht, lnrpc.Payment_IN_FLIGHT, update1.Status)
require.Equal(t.t, invoice.PaymentRequest, update1.PaymentRequest) require.Equal(ht, invoice.PaymentRequest, update1.PaymentRequest)
require.Equal(t.t, amountMsat, update1.ValueMsat) require.Equal(ht, amountMsat, update1.ValueMsat)
// Assert the second payment update is a payment success update. // Assert the second payment update is a payment success update.
update2, err := tracker.Recv() update2, err := tracker.Recv()
require.NoError(t.t, err, "unable to receive payment update 2.") require.NoError(ht, err, "unable to receive payment update 2.")
require.Equal( require.Equal(
t.t, lnrpc.PaymentFailureReason_FAILURE_REASON_NONE, ht, lnrpc.PaymentFailureReason_FAILURE_REASON_NONE,
update2.FailureReason, update2.FailureReason,
) )
require.Equal(t.t, lnrpc.Payment_SUCCEEDED, update2.Status) require.Equal(ht, lnrpc.Payment_SUCCEEDED, update2.Status)
require.Equal(t.t, invoice.PaymentRequest, update2.PaymentRequest) require.Equal(ht, invoice.PaymentRequest, update2.PaymentRequest)
require.Equal(t.t, amountMsat, update2.ValueMsat) require.Equal(ht, amountMsat, update2.ValueMsat)
require.Equal( require.Equal(
t.t, hex.EncodeToString(invoice.RPreimage), ht, hex.EncodeToString(invoice.RPreimage),
update2.PaymentPreimage, update2.PaymentPreimage,
) )
// TODO(yy): remove the sleep once the following bug is fixed.
// When the invoice is reported settled, the commitment dance is not
// yet finished, which can cause an error when closing the channel,
// saying there's active HTLCs. We need to investigate this issue and
// reverse the order to, first finish the commitment dance, then report
// the invoice as settled.
time.Sleep(2 * time.Second)
ht.CloseChannel(alice, channel)
} }