2021-06-28 21:27:14 +02:00
|
|
|
package itest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2021-07-27 12:41:56 +02:00
|
|
|
"fmt"
|
2021-06-28 21:27:14 +02:00
|
|
|
"time"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2021-06-28 21:27:14 +02:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2022-08-04 01:14:26 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntemp"
|
2021-06-28 21:27:14 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2021-07-27 12:41:56 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-28 21:27:14 +02:00
|
|
|
)
|
|
|
|
|
2022-08-04 01:14:26 +02:00
|
|
|
func testListPayments(ht *lntemp.HarnessTest) {
|
|
|
|
alice, bob := ht.Alice, ht.Bob
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Check that there are no payments before test.
|
2022-08-04 01:14:26 +02:00
|
|
|
ht.AssertNumPayments(alice, 0)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
2022-08-04 01:14:26 +02:00
|
|
|
chanPoint := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-06-28 21:27:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now that the channel is open, create an invoice for Bob which
|
|
|
|
// expects a payment of 1000 satoshis from Alice paid via a particular
|
|
|
|
// preimage.
|
|
|
|
const paymentAmt = 1000
|
|
|
|
preimage := bytes.Repeat([]byte("B"), 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2022-08-04 01:14:26 +02:00
|
|
|
invoiceResp := bob.RPC.AddInvoice(invoice)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// With the invoice for Bob added, send a payment towards Alice paying
|
|
|
|
// to the above generated invoice.
|
2022-08-04 01:14:26 +02:00
|
|
|
payReqs := []string{invoiceResp.PaymentRequest}
|
|
|
|
ht.CompletePaymentRequests(alice, payReqs)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Grab Alice's list of payments, she should show the existence of
|
|
|
|
// exactly one payment.
|
2022-08-04 01:14:26 +02:00
|
|
|
p := ht.AssertNumPayments(alice, 1)[0]
|
2021-06-28 21:27:14 +02:00
|
|
|
path := p.Htlcs[len(p.Htlcs)-1].Route.Hops
|
|
|
|
|
|
|
|
// Ensure that the stored path shows a direct payment to Bob with no
|
|
|
|
// other nodes in-between.
|
2022-08-04 01:14:26 +02:00
|
|
|
require.Len(ht, path, 1, "wrong number of routes in path")
|
|
|
|
require.Equal(ht, bob.PubKeyStr, path[0].PubKey, "wrong pub key")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// The payment amount should also match our previous payment directly.
|
2022-08-04 01:14:26 +02:00
|
|
|
require.EqualValues(ht, paymentAmt, p.ValueSat, "incorrect sat amount")
|
|
|
|
require.EqualValues(ht, paymentAmt*1000, p.ValueMsat,
|
|
|
|
"incorrect msat amount")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// The payment hash (or r-hash) should have been stored correctly.
|
|
|
|
correctRHash := hex.EncodeToString(invoiceResp.RHash)
|
2022-08-04 01:14:26 +02:00
|
|
|
require.Equal(ht, correctRHash, p.PaymentHash, "incorrect RHash")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-08-04 01:14:26 +02:00
|
|
|
// As we made a single-hop direct payment, there should have been no
|
|
|
|
// fee applied.
|
|
|
|
require.Zero(ht, p.FeeSat, "fee should be 0")
|
|
|
|
require.Zero(ht, p.FeeMsat, "fee should be 0")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-11-18 20:30:28 +01:00
|
|
|
// Now verify that the payment request returned by the rpc matches the
|
|
|
|
// invoice that we paid.
|
2022-08-04 01:14:26 +02:00
|
|
|
require.Equal(ht, invoiceResp.PaymentRequest, p.PaymentRequest,
|
|
|
|
"incorrect payreq")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-11-18 20:30:28 +01:00
|
|
|
// We now check the timestamp filters in `ListPayments`.
|
|
|
|
//
|
|
|
|
// Use a start date long time ago should return us the payment.
|
|
|
|
req := &lnrpc.ListPaymentsRequest{
|
|
|
|
CreationDateStart: 1227035905,
|
|
|
|
}
|
|
|
|
resp := alice.RPC.ListPayments(req)
|
|
|
|
require.Len(ht, resp.Payments, 1)
|
|
|
|
|
|
|
|
// Use an end date long time ago should return us nothing.
|
|
|
|
req = &lnrpc.ListPaymentsRequest{
|
|
|
|
CreationDateEnd: 1227035905,
|
|
|
|
}
|
|
|
|
resp = alice.RPC.ListPayments(req)
|
|
|
|
require.Empty(ht, resp.Payments)
|
|
|
|
|
|
|
|
// Use a start date far in the future should return us nothing.
|
|
|
|
req = &lnrpc.ListPaymentsRequest{
|
|
|
|
CreationDateStart: 5392552705,
|
|
|
|
}
|
|
|
|
resp = alice.RPC.ListPayments(req)
|
|
|
|
require.Empty(ht, resp.Payments)
|
|
|
|
|
|
|
|
// Use an end date far in the future should return us the payment.
|
|
|
|
req = &lnrpc.ListPaymentsRequest{
|
|
|
|
CreationDateEnd: 5392552705,
|
|
|
|
}
|
|
|
|
resp = alice.RPC.ListPayments(req)
|
|
|
|
require.Len(ht, resp.Payments, 1)
|
|
|
|
|
|
|
|
// We now do the same check for `ListInvoices`
|
|
|
|
//
|
|
|
|
// Use a start date long time ago should return us the invoice.
|
|
|
|
invReq := &lnrpc.ListInvoiceRequest{
|
|
|
|
CreationDateStart: 1227035905,
|
|
|
|
}
|
|
|
|
invResp := bob.RPC.ListInvoices(invReq)
|
|
|
|
require.Len(ht, invResp.Invoices, 1)
|
|
|
|
|
|
|
|
// Use an end date long time ago should return us nothing.
|
|
|
|
invReq = &lnrpc.ListInvoiceRequest{
|
|
|
|
CreationDateEnd: 1227035905,
|
|
|
|
}
|
|
|
|
invResp = bob.RPC.ListInvoices(invReq)
|
|
|
|
require.Empty(ht, invResp.Invoices)
|
|
|
|
|
|
|
|
// Use a start date far in the future should return us nothing.
|
|
|
|
invReq = &lnrpc.ListInvoiceRequest{
|
|
|
|
CreationDateStart: 5392552705,
|
|
|
|
}
|
|
|
|
invResp = bob.RPC.ListInvoices(invReq)
|
|
|
|
require.Empty(ht, invResp.Invoices)
|
|
|
|
|
|
|
|
// Use an end date far in the future should return us the invoice.
|
|
|
|
invReq = &lnrpc.ListInvoiceRequest{
|
|
|
|
CreationDateEnd: 5392552705,
|
|
|
|
}
|
|
|
|
invResp = bob.RPC.ListInvoices(invReq)
|
|
|
|
require.Len(ht, invResp.Invoices, 1)
|
|
|
|
|
2021-06-28 21:27:14 +02:00
|
|
|
// Delete all payments from Alice. DB should have no payments.
|
2022-08-04 01:14:26 +02:00
|
|
|
alice.RPC.DeleteAllPayments()
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Check that there are no payments after test.
|
2022-08-04 01:14:26 +02:00
|
|
|
ht.AssertNumPayments(alice, 0)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Close the channel.
|
|
|
|
defer ht.CloseChannel(alice, chanPoint)
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testPaymentFollowingChannelOpen tests that the channel transition from
|
|
|
|
// 'pending' to 'open' state does not cause any inconsistencies within other
|
|
|
|
// subsystems trying to update the channel state in the db. We follow this
|
|
|
|
// transition with a payment that updates the commitment state and verify that
|
|
|
|
// the pending state is up to date.
|
2022-08-04 02:25:45 +02:00
|
|
|
func testPaymentFollowingChannelOpen(ht *lntemp.HarnessTest) {
|
2021-06-28 21:27:14 +02:00
|
|
|
const paymentAmt = btcutil.Amount(100)
|
|
|
|
channelCapacity := paymentAmt * 1000
|
|
|
|
|
|
|
|
// We first establish a channel between Alice and Bob.
|
2022-08-04 02:25:45 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
p := lntemp.OpenChannelParams{
|
|
|
|
Amt: channelCapacity,
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
2022-08-04 02:25:45 +02:00
|
|
|
pendingUpdate := ht.OpenChannelAssertPending(alice, bob, p)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// At this point, the channel's funding transaction will have been
|
|
|
|
// broadcast, but not confirmed. Alice and Bob's nodes
|
|
|
|
// should reflect this when queried via RPC.
|
2022-08-04 02:25:45 +02:00
|
|
|
ht.AssertNodesNumPendingOpenChannels(alice, bob, 1)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We are restarting Bob's node to let the link be created for the
|
|
|
|
// pending channel.
|
2022-08-04 02:25:45 +02:00
|
|
|
ht.RestartNode(bob)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We ensure that Bob reconnects to Alice.
|
2022-08-04 02:25:45 +02:00
|
|
|
ht.EnsureConnected(bob, alice)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-08-04 02:25:45 +02:00
|
|
|
// We mine six blocks for the channel to be confirmed.
|
|
|
|
ht.MineBlocksAndAssertNumTxes(6, 1)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We verify that the channel is open from both nodes point of view.
|
2022-08-04 02:25:45 +02:00
|
|
|
chanPoint := lntemp.ChanPointFromPendingUpdate(pendingUpdate)
|
|
|
|
ht.AssertNodesNumPendingOpenChannels(alice, bob, 0)
|
|
|
|
ht.AssertChannelExists(alice, chanPoint)
|
|
|
|
ht.AssertChannelExists(bob, chanPoint)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// With the channel open, we'll create invoices for Bob that Alice will
|
|
|
|
// pay to in order to advance the state of the channel.
|
2022-08-04 02:25:45 +02:00
|
|
|
bobPayReqs, _, _ := ht.CreatePayReqs(bob, paymentAmt, 1)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Send payment to Bob so that a channel update to disk will be
|
|
|
|
// executed.
|
2022-08-04 02:25:45 +02:00
|
|
|
ht.CompletePaymentRequests(alice, []string{bobPayReqs[0]})
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-08-04 02:25:45 +02:00
|
|
|
// 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)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
2022-08-04 02:25:45 +02:00
|
|
|
ht.CloseChannel(alice, chanPoint)
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testAsyncPayments tests the performance of the async payments.
|
|
|
|
func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
2021-10-14 15:42:59 +02:00
|
|
|
runAsyncPayments(net, t, net.Alice, net.Bob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// runAsyncPayments tests the performance of the async payments.
|
|
|
|
func runAsyncPayments(net *lntest.NetworkHarness, t *harnessTest, alice,
|
|
|
|
bob *lntest.HarnessNode) {
|
|
|
|
|
2021-06-28 21:27:14 +02:00
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const (
|
|
|
|
paymentAmt = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
// First establish a channel with a capacity equals to the overall
|
|
|
|
// amount of payments, between Alice and Bob, at the end of the test
|
|
|
|
// Alice should send all money from her side to Bob.
|
|
|
|
channelCapacity := btcutil.Amount(paymentAmt * 2000)
|
|
|
|
chanPoint := openChannelAndAssert(
|
2021-10-14 15:42:59 +02:00
|
|
|
t, net, alice, bob,
|
2021-06-28 21:27:14 +02:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: channelCapacity,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-10-14 15:42:59 +02:00
|
|
|
info, err := getChanInfo(alice)
|
2021-06-28 21:27:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll create a number of invoices equal the max number of HTLCs that
|
|
|
|
// can be carried in one direction. The number on the commitment will
|
|
|
|
// likely be lower, but we can't guarantee that any more HTLCs will
|
|
|
|
// succeed due to the limited path diversity and inability of the router
|
|
|
|
// to retry via another path.
|
|
|
|
numInvoices := int(input.MaxHTLCNumber / 2)
|
|
|
|
|
|
|
|
bobAmt := int64(numInvoices * paymentAmt)
|
|
|
|
aliceAmt := info.LocalBalance - bobAmt
|
|
|
|
|
|
|
|
// With the channel open, we'll create invoices for Bob that Alice
|
|
|
|
// will pay to in order to advance the state of the channel.
|
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
2021-10-14 15:42:59 +02:00
|
|
|
bob, paymentAmt, numInvoices,
|
2021-06-28 21:27:14 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
2021-09-14 12:40:02 +02:00
|
|
|
err = alice.WaitForNetworkChannelOpen(chanPoint)
|
2021-06-28 21:27:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simultaneously send payments from Alice to Bob using of Bob's payment
|
|
|
|
// hashes generated above.
|
|
|
|
now := time.Now()
|
|
|
|
errChan := make(chan error)
|
|
|
|
statusChan := make(chan *lnrpc.Payment)
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
payReq := bobPayReqs[i]
|
|
|
|
go func() {
|
2021-09-14 12:40:02 +02:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, lntest.AsyncBenchmarkTimeout)
|
2021-10-14 15:42:59 +02:00
|
|
|
stream, err := alice.RouterClient.SendPaymentV2(
|
2021-06-28 21:27:14 +02:00
|
|
|
ctxt,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
statusChan <- result
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until all the payments have settled.
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
select {
|
|
|
|
case result := <-statusChan:
|
|
|
|
if result.Status == lnrpc.Payment_SUCCEEDED {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Fatalf("payment error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All payments have been sent, mark the finish time.
|
|
|
|
timeTaken := time.Since(now)
|
|
|
|
|
|
|
|
// Next query for Bob's and Alice's channel states, in order to confirm
|
|
|
|
// that all payment have been successful transmitted.
|
|
|
|
|
|
|
|
// Wait for the revocation to be received so alice no longer has pending
|
|
|
|
// htlcs listed and has correct balances. This is needed due to the fact
|
|
|
|
// that we now pipeline the settles.
|
|
|
|
err = wait.Predicate(func() bool {
|
2021-10-14 15:42:59 +02:00
|
|
|
aliceChan, err := getChanInfo(alice)
|
2021-06-28 21:27:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(aliceChan.PendingHtlcs) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if aliceChan.RemoteBalance != bobAmt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if aliceChan.LocalBalance != aliceAmt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2021-07-27 12:41:56 +02:00
|
|
|
}, defaultTimeout)
|
2021-06-28 21:27:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to assert alice's pending htlcs and/or remote/local balance")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Bob to receive revocation from Alice.
|
2021-07-27 12:41:56 +02:00
|
|
|
err = wait.NoError(func() error {
|
2021-10-14 15:42:59 +02:00
|
|
|
bobChan, err := getChanInfo(bob)
|
2021-07-27 12:41:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2021-07-27 12:41:56 +02:00
|
|
|
if len(bobChan.PendingHtlcs) != 0 {
|
|
|
|
return fmt.Errorf("bob's pending htlcs is incorrect, "+
|
|
|
|
"got %v, expected %v",
|
|
|
|
len(bobChan.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobChan.LocalBalance != bobAmt {
|
|
|
|
return fmt.Errorf("bob's local balance is incorrect, "+
|
|
|
|
"got %v, expected %v", bobChan.LocalBalance,
|
|
|
|
bobAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobChan.RemoteBalance != aliceAmt {
|
|
|
|
return fmt.Errorf("bob's remote balance is incorrect, "+
|
|
|
|
"got %v, expected %v", bobChan.RemoteBalance,
|
|
|
|
aliceAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
t.Log("\tBenchmark info: Elapsed time: ", timeTaken)
|
|
|
|
t.Log("\tBenchmark info: TPS: ", float64(numInvoices)/timeTaken.Seconds())
|
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
2021-10-14 15:42:59 +02:00
|
|
|
closeChannelAndAssert(t, net, alice, chanPoint, false)
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testBidirectionalAsyncPayments tests that nodes are able to send the
|
|
|
|
// payments to each other in async manner without blocking.
|
|
|
|
func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
const (
|
|
|
|
paymentAmt = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
// First establish a channel with a capacity equals to the overall
|
|
|
|
// amount of payments, between Alice and Bob, at the end of the test
|
|
|
|
// Alice should send all money from her side to Bob.
|
|
|
|
chanPoint := openChannelAndAssert(
|
2021-08-19 14:12:50 +02:00
|
|
|
t, net, net.Alice, net.Bob,
|
2021-06-28 21:27:14 +02:00
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: paymentAmt * 2000,
|
|
|
|
PushAmt: paymentAmt * 1000,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-08-19 20:14:19 +02:00
|
|
|
info, err := getChanInfo(net.Alice)
|
2021-06-28 21:27:14 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice channel info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll create a number of invoices equal the max number of HTLCs that
|
|
|
|
// can be carried in one direction. The number on the commitment will
|
|
|
|
// likely be lower, but we can't guarantee that any more HTLCs will
|
|
|
|
// succeed due to the limited path diversity and inability of the router
|
|
|
|
// to retry via another path.
|
|
|
|
numInvoices := int(input.MaxHTLCNumber / 2)
|
|
|
|
|
|
|
|
// Nodes should exchange the same amount of money and because of this
|
|
|
|
// at the end balances should remain the same.
|
|
|
|
aliceAmt := info.LocalBalance
|
|
|
|
bobAmt := info.RemoteBalance
|
|
|
|
|
|
|
|
// With the channel open, we'll create invoices for Bob that Alice
|
|
|
|
// will pay to in order to advance the state of the channel.
|
|
|
|
bobPayReqs, _, _, err := createPayReqs(
|
|
|
|
net.Bob, paymentAmt, numInvoices,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the channel open, we'll create invoices for Alice that Bob
|
|
|
|
// will pay to in order to advance the state of the channel.
|
|
|
|
alicePayReqs, _, _, err := createPayReqs(
|
|
|
|
net.Alice, paymentAmt, numInvoices,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create pay reqs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to receive the channel edge from the funding manager.
|
2021-09-14 12:40:02 +02:00
|
|
|
if err = net.Alice.WaitForNetworkChannelOpen(chanPoint); err != nil {
|
2021-06-28 21:27:14 +02:00
|
|
|
t.Fatalf("alice didn't see the alice->bob channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
2021-09-14 12:40:02 +02:00
|
|
|
if err = net.Bob.WaitForNetworkChannelOpen(chanPoint); err != nil {
|
2021-06-28 21:27:14 +02:00
|
|
|
t.Fatalf("bob didn't see the bob->alice channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset mission control to prevent previous payment results from
|
|
|
|
// interfering with this test. A new channel has been opened, but
|
|
|
|
// mission control operates on node pairs.
|
2021-09-14 12:40:02 +02:00
|
|
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
2021-06-28 21:27:14 +02:00
|
|
|
_, err = net.Alice.RouterClient.ResetMissionControl(
|
|
|
|
ctxt, &routerrpc.ResetMissionControlRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to reset mc for alice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send payments from Alice to Bob and from Bob to Alice in async
|
|
|
|
// manner.
|
|
|
|
errChan := make(chan error)
|
|
|
|
statusChan := make(chan *lnrpc.Payment)
|
|
|
|
|
|
|
|
send := func(node *lntest.HarnessNode, payReq string) {
|
|
|
|
go func() {
|
|
|
|
ctxt, _ = context.WithTimeout(
|
|
|
|
ctxb, lntest.AsyncBenchmarkTimeout,
|
|
|
|
)
|
|
|
|
stream, err := node.RouterClient.SendPaymentV2(
|
|
|
|
ctxt,
|
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: payReq,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
result, err := getPaymentResult(stream)
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
statusChan <- result
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
|
|
|
send(net.Bob, alicePayReqs[i])
|
|
|
|
send(net.Alice, bobPayReqs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect all payments to succeed.
|
|
|
|
for i := 0; i < 2*numInvoices; i++ {
|
|
|
|
select {
|
|
|
|
case result := <-statusChan:
|
|
|
|
if result.Status != lnrpc.Payment_SUCCEEDED {
|
|
|
|
t.Fatalf("payment error: %v", result.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Fatalf("payment error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice and Bob to receive revocations messages, and update
|
|
|
|
// states, i.e. balance info.
|
2021-07-27 12:41:56 +02:00
|
|
|
err = wait.NoError(func() error {
|
2021-08-19 20:14:19 +02:00
|
|
|
aliceInfo, err := getChanInfo(net.Alice)
|
2021-07-27 12:41:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get alice's channel info: %v", err)
|
|
|
|
}
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2021-07-27 12:41:56 +02:00
|
|
|
if aliceInfo.RemoteBalance != bobAmt {
|
|
|
|
return fmt.Errorf("alice's remote balance is incorrect, "+
|
|
|
|
"got %v, expected %v", aliceInfo.RemoteBalance,
|
|
|
|
bobAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if aliceInfo.LocalBalance != aliceAmt {
|
|
|
|
return fmt.Errorf("alice's local balance is incorrect, "+
|
|
|
|
"got %v, expected %v", aliceInfo.LocalBalance,
|
|
|
|
aliceAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(aliceInfo.PendingHtlcs) != 0 {
|
|
|
|
return fmt.Errorf("alice's pending htlcs is incorrect, "+
|
|
|
|
"got %v expected %v",
|
|
|
|
len(aliceInfo.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Next query for Bob's and Alice's channel states, in order to confirm
|
|
|
|
// that all payment have been successful transmitted.
|
2021-08-06 14:12:11 +02:00
|
|
|
err = wait.NoError(func() error {
|
2021-08-19 20:14:19 +02:00
|
|
|
bobInfo, err := getChanInfo(net.Bob)
|
2021-08-06 14:12:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get bob's channel info: %v", err)
|
|
|
|
}
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2021-08-06 14:12:11 +02:00
|
|
|
if bobInfo.LocalBalance != bobAmt {
|
|
|
|
return fmt.Errorf("bob's local balance is incorrect, "+
|
|
|
|
"got %v, expected %v", bobInfo.LocalBalance,
|
|
|
|
bobAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bobInfo.RemoteBalance != aliceAmt {
|
|
|
|
return fmt.Errorf("bob's remote balance is incorrect, "+
|
|
|
|
"got %v, expected %v", bobInfo.RemoteBalance,
|
|
|
|
aliceAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bobInfo.PendingHtlcs) != 0 {
|
|
|
|
return fmt.Errorf("bob's pending htlcs is incorrect, "+
|
|
|
|
"got %v, expected %v",
|
|
|
|
len(bobInfo.PendingHtlcs), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, defaultTimeout)
|
|
|
|
require.NoError(t.t, err)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Finally, immediately close the channel. This function will also
|
|
|
|
// block until the channel is closed and will additionally assert the
|
|
|
|
// relevant channel closing post conditions.
|
2021-08-11 06:29:40 +02:00
|
|
|
closeChannelAndAssert(t, net, net.Alice, chanPoint, false)
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 02:37:28 +02:00
|
|
|
func testInvoiceSubscriptions(ht *lntemp.HarnessTest) {
|
2021-06-28 21:27:14 +02:00
|
|
|
const chanAmt = btcutil.Amount(500000)
|
|
|
|
|
2022-08-04 02:37:28 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
|
|
|
|
// Create a new invoice subscription client for Bob, the notification
|
|
|
|
// should be dispatched shortly below.
|
|
|
|
req := &lnrpc.InvoiceSubscription{}
|
|
|
|
bobInvoiceSubscription := bob.RPC.SubscribeInvoices(req)
|
|
|
|
|
2021-06-28 21:27:14 +02:00
|
|
|
// Open a channel with 500k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2022-08-04 02:37:28 +02:00
|
|
|
chanPoint := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-06-28 21:27:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Next create a new invoice for Bob requesting 1k satoshis.
|
|
|
|
const paymentAmt = 1000
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
2022-08-04 02:37:28 +02:00
|
|
|
RPreimage: ht.Random32Bytes(),
|
2021-06-28 21:27:14 +02:00
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2022-08-04 02:37:28 +02:00
|
|
|
invoiceResp := bob.RPC.AddInvoice(invoice)
|
2021-06-28 21:27:14 +02:00
|
|
|
lastAddIndex := invoiceResp.AddIndex
|
|
|
|
|
2022-08-04 02:37:28 +02:00
|
|
|
// With the above invoice added, we should receive an update event.
|
|
|
|
invoiceUpdate := ht.ReceiveInvoiceUpdate(bobInvoiceSubscription)
|
|
|
|
require.NotEqual(ht, lnrpc.Invoice_SETTLED, invoiceUpdate.State,
|
|
|
|
"invoice should not be settled")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// With the assertion above set up, send a payment from Alice to Bob
|
|
|
|
// which should finalize and settle the invoice.
|
2022-08-04 02:37:28 +02:00
|
|
|
ht.CompletePaymentRequests(alice, []string{invoiceResp.PaymentRequest})
|
|
|
|
|
|
|
|
// The invoice update should exactly match the invoice created
|
|
|
|
// above, but should now be settled and have SettleDate
|
|
|
|
invoiceUpdate = ht.ReceiveInvoiceUpdate(bobInvoiceSubscription)
|
|
|
|
require.Equal(ht, lnrpc.Invoice_SETTLED, invoiceUpdate.State,
|
|
|
|
"invoice not settled but should be")
|
|
|
|
require.NotZero(ht, invoiceUpdate.SettleDate,
|
|
|
|
"invoice should have non zero settle date, but doesn't")
|
|
|
|
require.Equal(ht, invoice.RPreimage, invoiceUpdate.RPreimage,
|
|
|
|
"payment preimages don't match")
|
|
|
|
require.NotZero(ht, invoiceUpdate.SettleIndex,
|
|
|
|
"invoice should have settle index")
|
|
|
|
settleIndex := invoiceUpdate.SettleIndex
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We'll now add 3 more invoices to Bob's invoice registry.
|
|
|
|
const numInvoices = 3
|
2022-08-04 02:37:28 +02:00
|
|
|
payReqs, _, newInvoices := ht.CreatePayReqs(
|
|
|
|
bob, paymentAmt, numInvoices,
|
2021-06-28 21:27:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now that the set of invoices has been added, we'll re-register for
|
|
|
|
// streaming invoice notifications for Bob, this time specifying the
|
|
|
|
// add invoice of the last prior invoice.
|
2022-08-04 02:37:28 +02:00
|
|
|
req = &lnrpc.InvoiceSubscription{AddIndex: lastAddIndex}
|
|
|
|
bobInvoiceSubscription = bob.RPC.SubscribeInvoices(req)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// Since we specified a value of the prior add index above, we should
|
|
|
|
// now immediately get the invoices we just added as we should get the
|
|
|
|
// backlog of notifications.
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
2022-08-04 02:37:28 +02:00
|
|
|
invoiceUpdate := ht.ReceiveInvoiceUpdate(bobInvoiceSubscription)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We should now get the ith invoice we added, as they should
|
|
|
|
// be returned in order.
|
2022-08-04 02:37:28 +02:00
|
|
|
require.NotEqual(ht, lnrpc.Invoice_SETTLED, invoiceUpdate.State,
|
|
|
|
"should have only received add events")
|
|
|
|
|
2021-06-28 21:27:14 +02:00
|
|
|
originalInvoice := newInvoices[i]
|
|
|
|
rHash := sha256.Sum256(originalInvoice.RPreimage)
|
2022-08-04 02:37:28 +02:00
|
|
|
require.Equal(ht, rHash[:], invoiceUpdate.RHash,
|
|
|
|
"invoices have mismatched payment hashes")
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now have Bob settle out the remainder of these invoices so we
|
|
|
|
// can test that all settled invoices are properly notified.
|
2022-08-04 02:37:28 +02:00
|
|
|
ht.CompletePaymentRequests(alice, payReqs)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// With the set of invoices paid, we'll now cancel the old
|
|
|
|
// subscription, and create a new one for Bob, this time using the
|
|
|
|
// settle index to obtain the backlog of settled invoices.
|
|
|
|
req = &lnrpc.InvoiceSubscription{
|
|
|
|
SettleIndex: settleIndex,
|
|
|
|
}
|
2022-08-04 02:37:28 +02:00
|
|
|
bobInvoiceSubscription = bob.RPC.SubscribeInvoices(req)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// As we specified the index of the past settle index, we should now
|
|
|
|
// receive notifications for the three HTLCs that we just settled. As
|
|
|
|
// the order that the HTLCs will be settled in is partially randomized,
|
|
|
|
// we'll use a map to assert that the proper set has been settled.
|
|
|
|
settledInvoices := make(map[[32]byte]struct{})
|
|
|
|
for _, invoice := range newInvoices {
|
|
|
|
rHash := sha256.Sum256(invoice.RPreimage)
|
|
|
|
settledInvoices[rHash] = struct{}{}
|
|
|
|
}
|
|
|
|
for i := 0; i < numInvoices; i++ {
|
2022-08-04 02:37:28 +02:00
|
|
|
invoiceUpdate := ht.ReceiveInvoiceUpdate(bobInvoiceSubscription)
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
// We should now get the ith invoice we added, as they should
|
|
|
|
// be returned in order.
|
2022-08-04 02:37:28 +02:00
|
|
|
require.Equal(ht, lnrpc.Invoice_SETTLED, invoiceUpdate.State,
|
|
|
|
"should have only received settle events")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
var rHash [32]byte
|
|
|
|
copy(rHash[:], invoiceUpdate.RHash)
|
2022-08-04 02:37:28 +02:00
|
|
|
require.Contains(ht, settledInvoices, rHash,
|
|
|
|
"unknown invoice settled")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
|
|
|
delete(settledInvoices, rHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, all the invoices should be fully settled.
|
2022-08-04 02:37:28 +02:00
|
|
|
require.Empty(ht, settledInvoices, "not all invoices settled")
|
2021-06-28 21:27:14 +02:00
|
|
|
|
2022-08-04 02:37:28 +02:00
|
|
|
// 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, chanPoint)
|
2021-06-28 21:27:14 +02:00
|
|
|
}
|