2019-12-12 12:56:52 +03:00
|
|
|
package itest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-03-31 13:44:18 +03:00
|
|
|
"encoding/hex"
|
2019-12-12 12:56:52 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2020-03-31 13:44:18 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2019-12-12 12:56:52 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2019-12-12 13:06:00 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-11-03 16:00:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-12-12 13:06:00 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2021-03-03 21:00:14 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-12 12:56:52 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func testSingleHopInvoice(net *lntest.NetworkHarness, t *harnessTest) {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice being
|
|
|
|
// the sole funder of the channel.
|
|
|
|
ctxt, _ := context.WithTimeout(ctxb, channelOpenTimeout)
|
|
|
|
chanAmt := btcutil.Amount(100000)
|
|
|
|
chanPoint := openChannelAndAssert(
|
|
|
|
ctxt, t, net, net.Alice, net.Bob,
|
|
|
|
lntest.OpenChannelParams{
|
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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("A"), 32)
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
RPreimage: preimage,
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
invoiceResp, err := net.Bob.AddInvoice(ctxb, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for Alice to recognize and advertise the new channel generated
|
|
|
|
// above.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
err = net.Alice.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("alice didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bob didn't advertise channel before "+
|
|
|
|
"timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the invoice for Bob added, send a payment towards Alice paying
|
|
|
|
// to the above generated invoice.
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-31 13:44:18 +03:00
|
|
|
resp := sendAndAssertSuccess(
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, t, net.Alice,
|
2020-03-31 13:44:18 +03:00
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if hex.EncodeToString(preimage) != resp.PaymentPreimage {
|
2019-12-12 12:56:52 +03:00
|
|
|
t.Fatalf("preimage mismatch: expected %v, got %v", preimage,
|
2020-03-31 13:44:18 +03:00
|
|
|
resp.PaymentPreimage)
|
2019-12-12 12:56:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bob's invoice should now be found and marked as settled.
|
|
|
|
payHash := &lnrpc.PaymentHash{
|
|
|
|
RHash: invoiceResp.RHash,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
dbInvoice, err := net.Bob.LookupInvoice(ctxt, payHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to lookup invoice: %v", err)
|
|
|
|
}
|
2021-04-30 13:35:53 +03:00
|
|
|
if !dbInvoice.Settled { // nolint:staticcheck
|
2019-12-12 12:56:52 +03:00
|
|
|
t.Fatalf("bob's invoice should be marked as settled: %v",
|
|
|
|
spew.Sdump(dbInvoice))
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the payment completed all balance related stats should be
|
|
|
|
// properly updated.
|
|
|
|
err = wait.NoError(
|
|
|
|
assertAmountSent(paymentAmt, net.Alice, net.Bob),
|
|
|
|
3*time.Second,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create another invoice for Bob, this time leaving off the preimage
|
|
|
|
// to one will be randomly generated. We'll test the proper
|
|
|
|
// encoding/decoding of the zpay32 payment requests.
|
|
|
|
invoice = &lnrpc.Invoice{
|
|
|
|
Memo: "test3",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next send another payment, but this time using a zpay32 encoded
|
|
|
|
// invoice rather than manually specifying the payment details.
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-31 13:44:18 +03:00
|
|
|
sendAndAssertSuccess(
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, t, net.Alice,
|
2020-03-31 13:44:18 +03:00
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
},
|
|
|
|
)
|
2019-12-12 12:56:52 +03:00
|
|
|
|
|
|
|
// The second payment should also have succeeded, with the balances
|
|
|
|
// being update accordingly.
|
|
|
|
err = wait.NoError(
|
|
|
|
assertAmountSent(2*paymentAmt, net.Alice, net.Bob),
|
|
|
|
3*time.Second,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// Next send a keysend payment.
|
2019-12-12 13:06:00 +03:00
|
|
|
keySendPreimage := lntypes.Preimage{3, 4, 5, 11}
|
|
|
|
keySendHash := keySendPreimage.Hash()
|
|
|
|
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
2020-03-31 13:44:18 +03:00
|
|
|
sendAndAssertSuccess(
|
2021-03-31 14:26:56 +03:00
|
|
|
ctxt, t, net.Alice,
|
2020-03-31 13:44:18 +03:00
|
|
|
&routerrpc.SendPaymentRequest{
|
|
|
|
Dest: net.Bob.PubKey[:],
|
|
|
|
Amt: paymentAmt,
|
|
|
|
FinalCltvDelta: 40,
|
|
|
|
PaymentHash: keySendHash[:],
|
|
|
|
DestCustomRecords: map[uint64][]byte{
|
|
|
|
record.KeySendType: keySendPreimage[:],
|
|
|
|
},
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
2019-12-12 13:06:00 +03:00
|
|
|
},
|
2020-03-31 13:44:18 +03:00
|
|
|
)
|
2019-12-12 13:06:00 +03:00
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
// The keysend payment should also have succeeded, with the balances
|
2019-12-12 13:06:00 +03:00
|
|
|
// being update accordingly.
|
|
|
|
err = wait.NoError(
|
|
|
|
assertAmountSent(3*paymentAmt, net.Alice, net.Bob),
|
|
|
|
3*time.Second,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:00:14 +03:00
|
|
|
// Assert that the invoice has the proper AMP fields set, since the
|
|
|
|
// legacy keysend payment should have been promoted into an AMP payment
|
|
|
|
// internally.
|
|
|
|
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
|
|
keysendInvoice, err := net.Bob.LookupInvoice(
|
|
|
|
ctxt, &lnrpc.PaymentHash{
|
|
|
|
RHash: keySendHash[:],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t.t, err)
|
|
|
|
require.Equal(t.t, 1, len(keysendInvoice.Htlcs))
|
|
|
|
htlc := keysendInvoice.Htlcs[0]
|
|
|
|
require.Equal(t.t, uint64(0), htlc.MppTotalAmtMsat)
|
|
|
|
require.Nil(t.t, htlc.Amp)
|
|
|
|
|
2019-11-03 16:00:27 +03:00
|
|
|
// Now create an invoice and specify routing hints.
|
|
|
|
// We will test that the routing hints are encoded properly.
|
|
|
|
hintChannel := lnwire.ShortChannelID{BlockHeight: 10}
|
|
|
|
bobPubKey := hex.EncodeToString(net.Bob.PubKey[:])
|
|
|
|
hints := []*lnrpc.RouteHint{
|
|
|
|
{
|
|
|
|
HopHints: []*lnrpc.HopHint{
|
|
|
|
{
|
|
|
|
NodeId: bobPubKey,
|
|
|
|
ChanId: hintChannel.ToUint64(),
|
|
|
|
FeeBaseMsat: 1,
|
|
|
|
FeeProportionalMillionths: 1000000,
|
|
|
|
CltvExpiryDelta: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice = &lnrpc.Invoice{
|
|
|
|
Memo: "hints",
|
|
|
|
Value: paymentAmt,
|
|
|
|
RouteHints: hints,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt, _ = context.WithTimeout(ctxt, defaultTimeout)
|
|
|
|
invoiceResp, err = net.Bob.AddInvoice(ctxt, invoice)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add invoice: %v", err)
|
|
|
|
}
|
|
|
|
payreq, err := net.Bob.DecodePayReq(ctxt, &lnrpc.PayReqString{PayReq: invoiceResp.PaymentRequest})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode payment request %v", err)
|
|
|
|
}
|
|
|
|
if len(payreq.RouteHints) != 1 {
|
|
|
|
t.Fatalf("expected one routing hint")
|
|
|
|
}
|
|
|
|
routingHint := payreq.RouteHints[0]
|
|
|
|
if len(routingHint.HopHints) != 1 {
|
|
|
|
t.Fatalf("expected one hop hint")
|
|
|
|
}
|
|
|
|
hopHint := routingHint.HopHints[0]
|
|
|
|
if hopHint.FeeProportionalMillionths != 1000000 {
|
|
|
|
t.Fatalf("wrong FeeProportionalMillionths %v",
|
|
|
|
hopHint.FeeProportionalMillionths)
|
|
|
|
}
|
|
|
|
if hopHint.NodeId != bobPubKey {
|
|
|
|
t.Fatalf("wrong NodeId %v",
|
|
|
|
hopHint.NodeId)
|
|
|
|
}
|
|
|
|
if hopHint.ChanId != hintChannel.ToUint64() {
|
|
|
|
t.Fatalf("wrong ChanId %v",
|
|
|
|
hopHint.ChanId)
|
|
|
|
}
|
|
|
|
if hopHint.FeeBaseMsat != 1 {
|
|
|
|
t.Fatalf("wrong FeeBaseMsat %v",
|
|
|
|
hopHint.FeeBaseMsat)
|
|
|
|
}
|
|
|
|
if hopHint.CltvExpiryDelta != 20 {
|
|
|
|
t.Fatalf("wrong CltvExpiryDelta %v",
|
|
|
|
hopHint.CltvExpiryDelta)
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:56:52 +03:00
|
|
|
ctxt, _ = context.WithTimeout(ctxb, channelCloseTimeout)
|
|
|
|
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
|
|
|
|
}
|