2021-04-16 10:41:51 +02:00
|
|
|
package itest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2021-04-16 10:41:51 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chainreg"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2022-08-08 19:20:00 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntemp"
|
2022-08-08 19:47:50 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntemp/node"
|
2021-04-16 10:41:51 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type singleHopSendToRouteCase struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
// streaming tests streaming SendToRoute if true, otherwise tests
|
|
|
|
// synchronous SenToRoute.
|
|
|
|
streaming bool
|
|
|
|
|
|
|
|
// routerrpc submits the request to the routerrpc subserver if true,
|
|
|
|
// otherwise submits to the main rpc server.
|
|
|
|
routerrpc bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var singleHopSendToRouteCases = []singleHopSendToRouteCase{
|
|
|
|
{
|
|
|
|
name: "regular main sync",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular main stream",
|
|
|
|
streaming: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular routerrpc sync",
|
|
|
|
routerrpc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp main sync",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp main stream",
|
|
|
|
streaming: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mpp routerrpc sync",
|
|
|
|
routerrpc: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSingleHopSendToRoute tests that payments are properly processed through a
|
|
|
|
// provided route with a single hop. We'll create the following network
|
|
|
|
// topology:
|
2022-08-22 20:58:42 +02:00
|
|
|
//
|
|
|
|
// Carol --100k--> Dave
|
|
|
|
//
|
2021-04-16 10:41:51 +02:00
|
|
|
// We'll query the daemon for routes from Carol to Dave and then send payments
|
|
|
|
// by feeding the route back into the various SendToRoute RPC methods. Here we
|
|
|
|
// test all three SendToRoute endpoints, forcing each to perform both a regular
|
|
|
|
// payment and an MPP payment.
|
2022-08-08 19:20:00 +02:00
|
|
|
func testSingleHopSendToRoute(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
for _, test := range singleHopSendToRouteCases {
|
|
|
|
test := test
|
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
ht.Run(test.name, func(t1 *testing.T) {
|
|
|
|
st := ht.Subtest(t1)
|
|
|
|
testSingleHopSendToRouteCase(st, test)
|
2021-04-16 10:41:51 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
func testSingleHopSendToRouteCase(ht *lntemp.HarnessTest,
|
2021-04-16 10:41:51 +02:00
|
|
|
test singleHopSendToRouteCase) {
|
|
|
|
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
const paymentAmtSat = 1000
|
|
|
|
const numPayments = 5
|
|
|
|
const amountPaid = int64(numPayments * paymentAmtSat)
|
|
|
|
|
|
|
|
// Create Carol and Dave, then establish a channel between them. Carol
|
|
|
|
// is the sole funder of the channel with 100k satoshis. The network
|
|
|
|
// topology should look like:
|
|
|
|
// Carol -> 100k -> Dave
|
2022-08-08 19:20:00 +02:00
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
dave := ht.NewNode("Dave", nil)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
ht.ConnectNodes(carol, dave)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Carol and Dave with Carol
|
|
|
|
// being the sole funder of the channel.
|
2022-08-08 19:20:00 +02:00
|
|
|
chanPointCarol := ht.OpenChannel(
|
|
|
|
carol, dave, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
2022-08-08 19:20:00 +02:00
|
|
|
defer ht.CloseChannel(carol, chanPointCarol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Create invoices for Dave, which expect a payment from Carol.
|
2022-08-08 19:20:00 +02:00
|
|
|
payReqs, rHashes, _ := ht.CreatePayReqs(
|
2021-04-16 10:41:51 +02:00
|
|
|
dave, paymentAmtSat, numPayments,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reconstruct payment addresses.
|
|
|
|
var payAddrs [][]byte
|
|
|
|
for _, payReq := range payReqs {
|
2022-08-08 19:20:00 +02:00
|
|
|
resp := dave.RPC.DecodePayReq(payReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
payAddrs = append(payAddrs, resp.PaymentAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert Carol and Dave are synced to the chain before proceeding, to
|
|
|
|
// ensure the queried route will have a valid final CLTV once the HTLC
|
|
|
|
// reaches Dave.
|
2022-08-08 19:20:00 +02:00
|
|
|
_, minerHeight := ht.Miner.GetBestBlock()
|
|
|
|
ht.WaitForNodeBlockHeight(carol, minerHeight)
|
|
|
|
ht.WaitForNodeBlockHeight(dave, minerHeight)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Query for routes to pay from Carol to Dave using the default CLTV
|
|
|
|
// config.
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmtSat,
|
|
|
|
}
|
2022-08-08 19:20:00 +02:00
|
|
|
routes := carol.RPC.QueryRoutes(routesReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// There should only be one route to try, so take the first item.
|
|
|
|
r := routes.Routes[0]
|
|
|
|
|
|
|
|
// Construct a closure that will set MPP fields on the route, which
|
|
|
|
// allows us to test MPP payments.
|
|
|
|
setMPPFields := func(i int) {
|
|
|
|
hop := r.Hops[len(r.Hops)-1]
|
|
|
|
hop.TlvPayload = true
|
|
|
|
hop.MppRecord = &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: payAddrs[i],
|
|
|
|
TotalAmtMsat: paymentAmtSat * 1000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct closures for each of the payment types covered:
|
|
|
|
// - main rpc server sync
|
|
|
|
// - main rpc server streaming
|
|
|
|
// - routerrpc server sync
|
|
|
|
sendToRouteSync := func() {
|
|
|
|
for i, rHash := range rHashes {
|
|
|
|
setMPPFields(i)
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: r,
|
|
|
|
}
|
2022-08-08 19:20:00 +02:00
|
|
|
resp := carol.RPC.SendToRouteSync(sendReq)
|
|
|
|
require.Emptyf(ht, resp.PaymentError,
|
|
|
|
"received payment error from %s: %v",
|
|
|
|
carol.Name(), resp.PaymentError)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sendToRouteStream := func() {
|
2022-08-08 19:20:00 +02:00
|
|
|
alicePayStream := carol.RPC.SendToRoute()
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
for i, rHash := range rHashes {
|
|
|
|
setMPPFields(i)
|
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: routes.Routes[0],
|
|
|
|
}
|
|
|
|
err := alicePayStream.Send(sendReq)
|
2022-08-08 19:20:00 +02:00
|
|
|
require.NoError(ht, err, "unable to send payment")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2023-01-17 00:34:07 +01:00
|
|
|
resp, err := ht.ReceiveSendToRouteUpdate(alicePayStream)
|
2022-08-08 19:20:00 +02:00
|
|
|
require.NoError(ht, err, "unable to receive stream")
|
|
|
|
require.Emptyf(ht, resp.PaymentError,
|
|
|
|
"received payment error from %s: %v",
|
|
|
|
carol.Name(), resp.PaymentError)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sendToRouteRouterRPC := func() {
|
|
|
|
for i, rHash := range rHashes {
|
|
|
|
setMPPFields(i)
|
|
|
|
|
|
|
|
sendReq := &routerrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: r,
|
|
|
|
}
|
2022-08-08 19:20:00 +02:00
|
|
|
resp := carol.RPC.SendToRouteV2(sendReq)
|
|
|
|
require.Nilf(ht, resp.Failure, "received payment "+
|
|
|
|
"error from %s", carol.Name())
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using Carol as the node as the source, send the payments
|
2022-08-08 19:20:00 +02:00
|
|
|
// synchronously via the routerrpc's SendToRoute, or via the main RPC
|
2021-04-16 10:41:51 +02:00
|
|
|
// server's SendToRoute streaming or sync calls.
|
|
|
|
switch {
|
|
|
|
case !test.routerrpc && test.streaming:
|
|
|
|
sendToRouteStream()
|
|
|
|
case !test.routerrpc && !test.streaming:
|
|
|
|
sendToRouteSync()
|
|
|
|
case test.routerrpc && !test.streaming:
|
|
|
|
sendToRouteRouterRPC()
|
|
|
|
default:
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Fail(ht, "routerrpc does not support "+
|
|
|
|
"streaming send_to_route")
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the payment's from Carol's PoV have the correct payment
|
|
|
|
// hash and amount.
|
2022-08-08 19:20:00 +02:00
|
|
|
payments := ht.AssertNumPayments(carol, numPayments)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
for i, p := range payments {
|
2021-04-16 10:41:51 +02:00
|
|
|
// Assert that the payment hashes for each payment match up.
|
|
|
|
rHashHex := hex.EncodeToString(rHashes[i])
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Equalf(ht, rHashHex, p.PaymentHash,
|
|
|
|
"incorrect payment hash for payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
// Assert that each payment has no invoice since the payment
|
|
|
|
// was completed using SendToRoute.
|
|
|
|
require.Emptyf(ht, p.PaymentRequest,
|
|
|
|
"incorrect payment request for payment: %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Assert the payment amount is correct.
|
2022-08-08 19:20:00 +02:00
|
|
|
require.EqualValues(ht, paymentAmtSat, p.ValueSat,
|
|
|
|
"incorrect payment amt for payment %d, ", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Assert exactly one htlc was made.
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Lenf(ht, p.Htlcs, 1,
|
|
|
|
"expected 1 htlc for payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Assert the htlc's route is populated.
|
|
|
|
htlc := p.Htlcs[0]
|
2022-08-08 19:20:00 +02:00
|
|
|
require.NotNilf(ht, htlc.Route,
|
|
|
|
"expected route for payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Assert the hop has exactly one hop.
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Lenf(ht, htlc.Route.Hops, 1,
|
|
|
|
"expected 1 hop for payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// If this is an MPP test, assert the MPP record's fields are
|
|
|
|
// properly populated. Otherwise the hop should not have an MPP
|
|
|
|
// record.
|
|
|
|
hop := htlc.Route.Hops[0]
|
2022-08-08 19:20:00 +02:00
|
|
|
require.NotNilf(ht, hop.MppRecord,
|
|
|
|
"expected mpp record for mpp payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
require.EqualValues(ht, paymentAmtSat*1000,
|
|
|
|
hop.MppRecord.TotalAmtMsat,
|
|
|
|
"incorrect mpp total msat for payment %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
expAddr := payAddrs[i]
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Equal(ht, expAddr, hop.MppRecord.PaymentAddr,
|
|
|
|
"incorrect mpp payment addr for payment %d ", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the invoices's from Dave's PoV have the correct payment
|
|
|
|
// hash and amount.
|
2022-08-08 19:20:00 +02:00
|
|
|
invoices := ht.AssertNumInvoices(dave, numPayments)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:20:00 +02:00
|
|
|
for i, inv := range invoices {
|
2021-04-16 10:41:51 +02:00
|
|
|
// Assert that the payment hashes match up.
|
2022-08-08 19:20:00 +02:00
|
|
|
require.Equal(ht, rHashes[i], inv.RHash,
|
|
|
|
"incorrect payment hash for invoice %d", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Assert that the amount paid to the invoice is correct.
|
2022-08-08 19:20:00 +02:00
|
|
|
require.EqualValues(ht, paymentAmtSat, inv.AmtPaidSat,
|
|
|
|
"incorrect payment amt for invoice %d, ", i)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
|
|
|
// shifted by 5k satoshis in the direction of Dave, the sink within the
|
|
|
|
// payment flow generated above. The order of asserts corresponds to
|
|
|
|
// increasing of time is needed to embed the HTLC in commitment
|
|
|
|
// transaction, in channel Carol->Dave, order is Dave and then Carol.
|
2022-08-08 19:20:00 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) => Dave(remote)", dave,
|
|
|
|
chanPointCarol, int64(0), amountPaid)
|
|
|
|
ht.AssertAmountPaid("Carol(local) => Dave(remote)", carol,
|
|
|
|
chanPointCarol, amountPaid, int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopSendToRoute tests that payments are properly processed
|
|
|
|
// through a provided route. We'll create the following network topology:
|
2022-08-22 20:58:42 +02:00
|
|
|
//
|
|
|
|
// Alice --100k--> Bob --100k--> Carol
|
|
|
|
//
|
2021-04-16 10:41:51 +02:00
|
|
|
// We'll query the daemon for routes from Alice to Carol and then
|
|
|
|
// send payments through the routes.
|
2022-08-08 19:24:05 +02:00
|
|
|
func testMultiHopSendToRoute(ht *lntemp.HarnessTest) {
|
|
|
|
ht.Run("with cache", func(tt *testing.T) {
|
|
|
|
st := ht.Subtest(tt)
|
|
|
|
runMultiHopSendToRoute(st, true)
|
2021-10-21 13:55:25 +02:00
|
|
|
})
|
|
|
|
if *dbBackendFlag == "bbolt" {
|
2022-08-08 19:24:05 +02:00
|
|
|
ht.Run("without cache", func(tt *testing.T) {
|
|
|
|
st := ht.Subtest(tt)
|
|
|
|
runMultiHopSendToRoute(st, false)
|
2021-10-21 13:55:25 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runMultiHopSendToRoute tests that payments are properly processed
|
|
|
|
// through a provided route. We'll create the following network topology:
|
2022-08-22 20:58:42 +02:00
|
|
|
//
|
|
|
|
// Alice --100k--> Bob --100k--> Carol
|
|
|
|
//
|
2021-10-21 13:55:25 +02:00
|
|
|
// We'll query the daemon for routes from Alice to Carol and then
|
|
|
|
// send payments through the routes.
|
2022-08-08 19:24:05 +02:00
|
|
|
func runMultiHopSendToRoute(ht *lntemp.HarnessTest, useGraphCache bool) {
|
2021-10-21 13:55:25 +02:00
|
|
|
var opts []string
|
|
|
|
if !useGraphCache {
|
|
|
|
opts = append(opts, "--db.no-graph-cache")
|
|
|
|
}
|
|
|
|
|
2022-08-08 19:24:05 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
ht.RestartNodeWithExtraArgs(alice, opts)
|
2021-10-21 13:55:25 +02:00
|
|
|
|
2022-08-08 19:24:05 +02:00
|
|
|
ht.EnsureConnected(alice, bob)
|
2021-10-21 13:55:25 +02:00
|
|
|
|
2021-04-16 10:41:51 +02:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2022-08-08 19:24:05 +02:00
|
|
|
chanPointAlice := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
2022-08-08 19:24:05 +02:00
|
|
|
defer ht.CloseChannel(alice, chanPointAlice)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:24:05 +02:00
|
|
|
// Create Carol and establish a channel from Bob. Bob is the sole
|
|
|
|
// funder of the channel with 100k satoshis. The network topology
|
|
|
|
// should look like:
|
2021-04-16 10:41:51 +02:00
|
|
|
// Alice -> Bob -> Carol
|
2022-08-08 19:24:05 +02:00
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.ConnectNodes(carol, bob)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:24:05 +02:00
|
|
|
chanPointBob := ht.OpenChannel(
|
|
|
|
bob, carol, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
2022-08-08 19:24:05 +02:00
|
|
|
defer ht.CloseChannel(carol, chanPointBob)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:24:05 +02:00
|
|
|
// Create 5 invoices for Carol, which expect a payment from Alice for
|
|
|
|
// 1k satoshis with a different preimage each time.
|
2021-04-16 10:41:51 +02:00
|
|
|
const (
|
|
|
|
numPayments = 5
|
|
|
|
paymentAmt = 1000
|
|
|
|
)
|
2022-08-08 19:24:05 +02:00
|
|
|
_, rHashes, invoices := ht.CreatePayReqs(carol, paymentAmt, numPayments)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Construct a route from Alice to Carol for each of the invoices
|
|
|
|
// created above. We set FinalCltvDelta to 40 since by default
|
|
|
|
// QueryRoutes returns the last hop with a final cltv delta of 9 where
|
|
|
|
// as the default in htlcswitch is 40.
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: carol.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
|
|
|
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta,
|
|
|
|
}
|
2022-08-08 19:24:05 +02:00
|
|
|
routes := alice.RPC.QueryRoutes(routesReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Using Alice as the source, pay to the 5 invoices from Carol created
|
|
|
|
// above.
|
|
|
|
for i, rHash := range rHashes {
|
|
|
|
// Manually set the MPP payload a new for each payment since
|
|
|
|
// the payment addr will change with each invoice, although we
|
|
|
|
// can re-use the route itself.
|
2022-08-08 19:24:05 +02:00
|
|
|
route := routes.Routes[0]
|
2021-04-16 10:41:51 +02:00
|
|
|
route.Hops[len(route.Hops)-1].TlvPayload = true
|
|
|
|
route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{
|
|
|
|
PaymentAddr: invoices[i].PaymentAddr,
|
|
|
|
TotalAmtMsat: int64(
|
|
|
|
lnwire.NewMSatFromSatoshis(paymentAmt),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
sendReq := &routerrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
2022-08-08 19:24:05 +02:00
|
|
|
Route: route,
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
2022-08-08 19:24:05 +02:00
|
|
|
resp := alice.RPC.SendToRouteV2(sendReq)
|
|
|
|
require.Nil(ht, resp.Failure, "received payment error")
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// When asserting the amount of satoshis moved, we'll factor in the
|
|
|
|
// default base fee, as we didn't modify the fee structure when
|
|
|
|
// creating the seed nodes in the network.
|
|
|
|
const baseFee = 1
|
|
|
|
|
|
|
|
// At this point all the channels within our proto network should be
|
2022-08-08 19:24:05 +02:00
|
|
|
// shifted by 5k satoshis in the direction of Carol, the sink within
|
|
|
|
// the payment flow generated above. The order of asserts corresponds
|
|
|
|
// to increasing of time is needed to embed the HTLC in commitment
|
2021-04-16 10:41:51 +02:00
|
|
|
// transaction, in channel Alice->Bob->Carol, order is Carol, Bob,
|
|
|
|
// Alice.
|
|
|
|
const amountPaid = int64(5000)
|
2022-08-08 19:24:05 +02:00
|
|
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)", carol,
|
|
|
|
chanPointBob, int64(0), amountPaid)
|
|
|
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)", bob,
|
|
|
|
chanPointBob, amountPaid, int64(0))
|
|
|
|
ht.AssertAmountPaid("Alice(local) => Bob(remote)", bob,
|
|
|
|
chanPointAlice, int64(0), amountPaid+(baseFee*numPayments))
|
|
|
|
ht.AssertAmountPaid("Alice(local) => Bob(remote)", alice,
|
|
|
|
chanPointAlice, amountPaid+(baseFee*numPayments), int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testSendToRouteErrorPropagation tests propagation of errors that occur
|
|
|
|
// while processing a multi-hop payment through an unknown route.
|
2022-08-08 19:27:10 +02:00
|
|
|
func testSendToRouteErrorPropagation(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// Open a channel with 100k satoshis between Alice and Bob with Alice
|
|
|
|
// being the sole funder of the channel.
|
2022-08-08 19:27:10 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
chanPointAlice := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create a new nodes (Carol and Charlie), load her with some funds,
|
|
|
|
// then establish a connection between Carol and Charlie with a channel
|
|
|
|
// that has identical capacity to the one created above.Then we will
|
|
|
|
// get route via queryroutes call which will be fake route for Alice ->
|
|
|
|
// Bob graph.
|
|
|
|
//
|
2022-08-08 19:27:10 +02:00
|
|
|
// The network topology should now look like:
|
|
|
|
// Alice -> Bob; Carol -> Charlie.
|
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:27:10 +02:00
|
|
|
charlie := ht.NewNode("Charlie", nil)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, charlie)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:27:10 +02:00
|
|
|
ht.ConnectNodes(carol, charlie)
|
|
|
|
ht.OpenChannel(carol, charlie, lntemp.OpenChannelParams{Amt: chanAmt})
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Query routes from Carol to Charlie which will be an invalid route
|
|
|
|
// for Alice -> Bob.
|
|
|
|
fakeReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: charlie.PubKeyStr,
|
|
|
|
Amt: int64(1),
|
|
|
|
}
|
2022-08-08 19:27:10 +02:00
|
|
|
fakeRoute := carol.RPC.QueryRoutes(fakeReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:27:10 +02:00
|
|
|
// Create 1 invoice for Bob, which expect a payment from Alice for 1k
|
|
|
|
// satoshis.
|
2021-04-16 10:41:51 +02:00
|
|
|
const paymentAmt = 1000
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "testing",
|
|
|
|
Value: paymentAmt,
|
|
|
|
}
|
2022-08-08 19:27:10 +02:00
|
|
|
resp := bob.RPC.AddInvoice(invoice)
|
2021-04-16 10:41:51 +02:00
|
|
|
rHash := resp.RHash
|
|
|
|
|
2022-08-08 19:27:10 +02:00
|
|
|
// Using Alice as the source, pay to the invoice from Bob.
|
|
|
|
alicePayStream := alice.RPC.SendToRoute()
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
sendReq := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
|
|
|
Route: fakeRoute.Routes[0],
|
|
|
|
}
|
2022-08-08 19:27:10 +02:00
|
|
|
err := alicePayStream.Send(sendReq)
|
|
|
|
require.NoError(ht, err, "unable to send payment")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// At this place we should get an rpc error with notification
|
|
|
|
// that edge is not found on hop(0)
|
2022-08-08 19:27:10 +02:00
|
|
|
event, err := ht.ReceiveSendToRouteUpdate(alicePayStream)
|
|
|
|
require.NoError(ht, err, "payment stream has been closed but fake "+
|
|
|
|
"route has consumed")
|
|
|
|
require.Contains(ht, event.PaymentError, "UnknownNextPeer")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:27:10 +02:00
|
|
|
ht.CloseChannel(alice, chanPointAlice)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testPrivateChannels tests that a private channel can be used for
|
|
|
|
// routing by the two endpoints of the channel, but is not known by
|
|
|
|
// the rest of the nodes in the graph.
|
2022-08-08 19:30:45 +02:00
|
|
|
func testPrivateChannels(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// We create the following topology:
|
|
|
|
//
|
|
|
|
// Dave --100k--> Alice --200k--> Bob
|
|
|
|
// ^ ^
|
|
|
|
// | |
|
|
|
|
// 100k 100k
|
|
|
|
// | |
|
|
|
|
// +---- Carol ----+
|
|
|
|
//
|
|
|
|
// where the 100k channel between Carol and Alice is private.
|
|
|
|
|
|
|
|
// Open a channel with 200k satoshis between Alice and Bob.
|
2022-08-08 19:30:45 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
chanPointAlice := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt * 2},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Dave, and a channel to Alice of 100k.
|
2022-08-08 19:30:45 +02:00
|
|
|
dave := ht.NewNode("Dave", nil)
|
|
|
|
ht.ConnectNodes(dave, alice)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, dave)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:30:45 +02:00
|
|
|
chanPointDave := ht.OpenChannel(
|
|
|
|
dave, alice, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Next, we'll create Carol and establish a channel from her to
|
|
|
|
// Dave of 100k.
|
2022-08-08 19:30:45 +02:00
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.ConnectNodes(carol, dave)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:30:45 +02:00
|
|
|
chanPointCarol := ht.OpenChannel(
|
|
|
|
carol, dave, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now create a _private_ channel directly between Carol and
|
|
|
|
// Alice of 100k.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.ConnectNodes(carol, alice)
|
|
|
|
chanPointPrivate := ht.OpenChannel(
|
|
|
|
carol, alice, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
2022-08-08 19:30:45 +02:00
|
|
|
defer ht.CloseChannel(carol, chanPointPrivate)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// The channel should be available for payments between Carol and Alice.
|
|
|
|
// We check this by sending payments from Carol to Bob, that
|
|
|
|
// collectively would deplete at least one of Carol's channels.
|
|
|
|
|
|
|
|
// Create 2 invoices for Bob, each of 70k satoshis. Since each of
|
|
|
|
// Carol's channels is of size 100k, these payments cannot succeed
|
|
|
|
// by only using one of the channels.
|
|
|
|
const numPayments = 2
|
|
|
|
const paymentAmt = 70000
|
2022-08-08 19:30:45 +02:00
|
|
|
payReqs, _, _ := ht.CreatePayReqs(bob, paymentAmt, numPayments)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Let Carol pay the invoices.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.CompletePaymentRequests(carol, payReqs)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// When asserting the amount of satoshis moved, we'll factor in the
|
|
|
|
// default base fee, as we didn't modify the fee structure when
|
|
|
|
// creating the seed nodes in the network.
|
|
|
|
const baseFee = 1
|
|
|
|
|
|
|
|
// Bob should have received 140k satoshis from Alice.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Alice(local) => Bob(remote)", bob,
|
|
|
|
chanPointAlice, int64(0), 2*paymentAmt)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Alice sent 140k to Bob.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Alice(local) => Bob(remote)", alice,
|
|
|
|
chanPointAlice, 2*paymentAmt, int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Alice received 70k + fee from Dave.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Dave(local) => Alice(remote)", alice,
|
|
|
|
chanPointDave, int64(0), paymentAmt+baseFee)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Dave sent 70k+fee to Alice.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Dave(local) => Alice(remote)", dave,
|
|
|
|
chanPointDave, paymentAmt+baseFee, int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Dave received 70k+fee of two hops from Carol.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) => Dave(remote)", dave,
|
|
|
|
chanPointCarol, int64(0), paymentAmt+baseFee*2)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Carol sent 70k+fee of two hops to Dave.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) => Dave(remote)", carol,
|
|
|
|
chanPointCarol, paymentAmt+baseFee*2, int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Alice received 70k+fee from Carol.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) [private=>] Alice(remote)",
|
|
|
|
alice, chanPointPrivate, int64(0), paymentAmt+baseFee)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Carol sent 70k+fee to Alice.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) [private=>] Alice(remote)",
|
|
|
|
carol, chanPointPrivate, paymentAmt+baseFee, int64(0))
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Alice should also be able to route payments using this channel,
|
|
|
|
// so send two payments of 60k back to Carol.
|
|
|
|
const paymentAmt60k = 60000
|
2022-08-08 19:30:45 +02:00
|
|
|
payReqs, _, _ = ht.CreatePayReqs(carol, paymentAmt60k, numPayments)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Let Bob pay the invoices.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.CompletePaymentRequests(alice, payReqs)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:30:45 +02:00
|
|
|
// Carol and Alice should know about 4, while Bob and Dave should only
|
|
|
|
// know about 3, since one channel is private.
|
|
|
|
ht.AssertNumEdges(alice, 4, true)
|
|
|
|
ht.AssertNumEdges(alice, 3, false)
|
|
|
|
ht.AssertNumEdges(bob, 3, true)
|
|
|
|
ht.AssertNumEdges(carol, 4, true)
|
|
|
|
ht.AssertNumEdges(carol, 3, false)
|
|
|
|
ht.AssertNumEdges(dave, 3, true)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Close all channels.
|
2022-08-08 19:30:45 +02:00
|
|
|
ht.CloseChannel(alice, chanPointAlice)
|
|
|
|
ht.CloseChannel(dave, chanPointDave)
|
|
|
|
ht.CloseChannel(carol, chanPointCarol)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testInvoiceRoutingHints tests that the routing hints for an invoice are
|
|
|
|
// created properly.
|
2022-08-08 19:36:00 +02:00
|
|
|
func testInvoiceRoutingHints(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// Throughout this test, we'll be opening a channel between Alice and
|
|
|
|
// several other parties.
|
|
|
|
//
|
|
|
|
// First, we'll create a private channel between Alice and Bob. This
|
|
|
|
// will be the only channel that will be considered as a routing hint
|
|
|
|
// throughout this test. We'll include a push amount since we currently
|
2022-08-08 19:36:00 +02:00
|
|
|
// require channels to have enough remote balance to cover the
|
|
|
|
// invoice's payment.
|
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
chanPointBob := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
// Then, we'll create Carol's node and open a public channel between
|
|
|
|
// her and Alice. This channel will not be considered as a routing hint
|
|
|
|
// due to it being public.
|
|
|
|
carol := ht.NewNode("Carol", nil)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
ht.ConnectNodes(alice, carol)
|
|
|
|
chanPointCarol := ht.OpenChannel(
|
|
|
|
alice, carol, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// We'll also create a public channel between Bob and Carol to ensure
|
2022-08-08 19:36:00 +02:00
|
|
|
// that Bob gets selected as the only routing hint. We do this as we
|
|
|
|
// should only include routing hints for nodes that are publicly
|
2021-04-16 10:41:51 +02:00
|
|
|
// advertised, otherwise we'd end up leaking information about nodes
|
|
|
|
// that wish to stay unadvertised.
|
2022-08-08 19:36:00 +02:00
|
|
|
ht.ConnectNodes(bob, carol)
|
|
|
|
chanPointBobCarol := ht.OpenChannel(
|
|
|
|
bob, carol, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
// Then, we'll create Dave's node and open a private channel between
|
|
|
|
// him and Alice. We will not include a push amount in order to not
|
|
|
|
// consider this channel as a routing hint as it will not have enough
|
|
|
|
// remote balance for the invoice's amount.
|
|
|
|
dave := ht.NewNode("Dave", nil)
|
|
|
|
|
|
|
|
ht.ConnectNodes(alice, dave)
|
|
|
|
chanPointDave := ht.OpenChannel(
|
|
|
|
alice, dave, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Finally, we'll create Eve's node and open a private channel between
|
2022-08-08 19:36:00 +02:00
|
|
|
// her and Alice. This time though, we'll take Eve's node down after
|
|
|
|
// the channel has been created to avoid populating routing hints for
|
2021-04-16 10:41:51 +02:00
|
|
|
// inactive channels.
|
2022-08-08 19:36:00 +02:00
|
|
|
eve := ht.NewNode("Eve", nil)
|
|
|
|
ht.ConnectNodes(alice, eve)
|
|
|
|
chanPointEve := ht.OpenChannel(
|
|
|
|
alice, eve, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
PushAmt: chanAmt / 2,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
// Now that the channels are open, we'll disconnect the connection
|
|
|
|
// between Alice and Eve and then take down Eve's node.
|
|
|
|
ht.DisconnectNodes(alice, eve)
|
|
|
|
ht.Shutdown(eve)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// We'll need the short channel ID of the channel between Alice and Bob
|
|
|
|
// to make sure the routing hint is for this channel.
|
2022-08-08 19:36:00 +02:00
|
|
|
aliceBobChanID := ht.QueryChannelByChanPoint(alice, chanPointBob).ChanId
|
2022-10-26 20:56:44 +02:00
|
|
|
|
|
|
|
checkInvoiceHints := func(invoice *lnrpc.Invoice) {
|
|
|
|
// Due to the way the channels were set up above, the channel
|
|
|
|
// between Alice and Bob should be the only channel used as a
|
|
|
|
// routing hint.
|
|
|
|
var decoded *lnrpc.PayReq
|
2022-08-08 19:36:00 +02:00
|
|
|
err := wait.NoError(func() error {
|
|
|
|
resp := alice.RPC.AddInvoice(invoice)
|
2022-10-26 20:56:44 +02:00
|
|
|
|
|
|
|
// We'll decode the invoice's payment request to
|
|
|
|
// determine which channels were used as routing hints.
|
2022-08-08 19:36:00 +02:00
|
|
|
decoded = alice.RPC.DecodePayReq(resp.PaymentRequest)
|
2022-10-26 20:56:44 +02:00
|
|
|
|
|
|
|
if len(decoded.RouteHints) != 1 {
|
2022-08-08 19:36:00 +02:00
|
|
|
return fmt.Errorf("expected one route hint, "+
|
|
|
|
"got %d", len(decoded.RouteHints))
|
2022-10-26 20:56:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
return nil
|
2022-10-26 20:56:44 +02:00
|
|
|
}, defaultTimeout)
|
2022-08-08 19:36:00 +02:00
|
|
|
require.NoError(ht, err, "timeout checking invoice hints")
|
2022-10-26 20:56:44 +02:00
|
|
|
|
|
|
|
hops := decoded.RouteHints[0].HopHints
|
2022-08-08 19:36:00 +02:00
|
|
|
require.Len(ht, hops, 1, "expected one hop in route hint")
|
2022-10-26 20:56:44 +02:00
|
|
|
|
2022-08-08 19:36:00 +02:00
|
|
|
chanID := hops[0].ChanId
|
|
|
|
require.Equal(ht, aliceBobChanID, chanID, "chanID mismatch")
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:56:44 +02:00
|
|
|
// Create an invoice for Alice that will populate the routing hints.
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "routing hints",
|
|
|
|
Value: int64(chanAmt / 4),
|
|
|
|
Private: true,
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
2022-10-26 20:56:44 +02:00
|
|
|
checkInvoiceHints(invoice)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-10-26 19:57:52 +02:00
|
|
|
// Create another invoice for Alice with no value and ensure it still
|
|
|
|
// populates routing hints.
|
|
|
|
invoice = &lnrpc.Invoice{
|
|
|
|
Memo: "routing hints with no amount",
|
|
|
|
Value: 0,
|
|
|
|
Private: true,
|
|
|
|
}
|
|
|
|
checkInvoiceHints(invoice)
|
|
|
|
|
2021-04-16 10:41:51 +02:00
|
|
|
// Now that we've confirmed the routing hints were added correctly, we
|
|
|
|
// can close all the channels and shut down all the nodes created.
|
2022-08-08 19:36:00 +02:00
|
|
|
ht.CloseChannel(alice, chanPointBob)
|
|
|
|
ht.CloseChannel(alice, chanPointCarol)
|
|
|
|
ht.CloseChannel(bob, chanPointBobCarol)
|
|
|
|
ht.CloseChannel(alice, chanPointDave)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// The channel between Alice and Eve should be force closed since Eve
|
|
|
|
// is offline.
|
2022-08-08 19:36:00 +02:00
|
|
|
ht.ForceCloseChannel(alice, chanPointEve)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testMultiHopOverPrivateChannels tests that private channels can be used as
|
|
|
|
// intermediate hops in a route for payments.
|
2022-08-08 19:39:19 +02:00
|
|
|
func testMultiHopOverPrivateChannels(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
// We'll test that multi-hop payments over private channels work as
|
|
|
|
// intended. To do so, we'll create the following topology:
|
|
|
|
// private public private
|
|
|
|
// Alice <--100k--> Bob <--100k--> Carol <--100k--> Dave
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// First, we'll open a private channel between Alice and Bob with Alice
|
|
|
|
// being the funder.
|
2022-08-08 19:39:19 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
chanPointAlice := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Next, we'll create Carol's node and open a public channel between
|
|
|
|
// her and Bob with Bob being the funder.
|
2022-08-08 19:39:19 +02:00
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.ConnectNodes(bob, carol)
|
|
|
|
chanPointBob := ht.OpenChannel(
|
|
|
|
bob, carol, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-08 19:39:19 +02:00
|
|
|
// Alice should know the new channel from Bob.
|
|
|
|
ht.AssertTopologyChannelOpen(alice, chanPointBob)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:39:19 +02:00
|
|
|
// Next, we'll create Dave's node and open a private channel between
|
|
|
|
// him and Carol with Carol being the funder.
|
|
|
|
dave := ht.NewNode("Dave", nil)
|
|
|
|
ht.ConnectNodes(carol, dave)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:39:19 +02:00
|
|
|
chanPointCarol := ht.OpenChannel(
|
|
|
|
carol, dave, lntemp.OpenChannelParams{
|
2021-04-16 10:41:51 +02:00
|
|
|
Amt: chanAmt,
|
|
|
|
Private: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-08-08 19:39:19 +02:00
|
|
|
// Dave should know the channel[Bob<->Carol] from Carol.
|
|
|
|
ht.AssertTopologyChannelOpen(dave, chanPointBob)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Now that all the channels are set up according to the topology from
|
|
|
|
// above, we can proceed to test payments. We'll create an invoice for
|
|
|
|
// Dave of 20k satoshis and pay it with Alice. Since there is no public
|
|
|
|
// route from Alice to Dave, we'll need to use the private channel
|
|
|
|
// between Carol and Dave as a routing hint encoded in the invoice.
|
|
|
|
const paymentAmt = 20000
|
|
|
|
|
|
|
|
// Create the invoice for Dave.
|
|
|
|
invoice := &lnrpc.Invoice{
|
|
|
|
Memo: "two hopz!",
|
|
|
|
Value: paymentAmt,
|
|
|
|
Private: true,
|
|
|
|
}
|
2022-08-08 19:39:19 +02:00
|
|
|
resp := dave.RPC.AddInvoice(invoice)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Let Alice pay the invoice.
|
|
|
|
payReqs := []string{resp.PaymentRequest}
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.CompletePaymentRequests(alice, payReqs)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// When asserting the amount of satoshis moved, we'll factor in the
|
|
|
|
// default base fee, as we didn't modify the fee structure when opening
|
|
|
|
// the channels.
|
|
|
|
const baseFee = 1
|
|
|
|
|
|
|
|
// Dave should have received 20k satoshis from Carol.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) [private=>] Dave(remote)",
|
|
|
|
dave, chanPointCarol, 0, paymentAmt)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Carol should have sent 20k satoshis to Dave.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Carol(local) [private=>] Dave(remote)",
|
|
|
|
carol, chanPointCarol, paymentAmt, 0)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Carol should have received 20k satoshis + fee for one hop from Bob.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)",
|
|
|
|
carol, chanPointBob, 0, paymentAmt+baseFee)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Bob should have sent 20k satoshis + fee for one hop to Carol.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)",
|
|
|
|
bob, chanPointBob, paymentAmt+baseFee, 0)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Bob should have received 20k satoshis + fee for two hops from Alice.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Alice(local) [private=>] Bob(remote)", bob,
|
|
|
|
chanPointAlice, 0, paymentAmt+baseFee*2)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Alice should have sent 20k satoshis + fee for two hops to Bob.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.AssertAmountPaid("Alice(local) [private=>] Bob(remote)", alice,
|
|
|
|
chanPointAlice, paymentAmt+baseFee*2, 0)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// At this point, the payment was successful. We can now close all the
|
|
|
|
// channels and shutdown the nodes created throughout this test.
|
2022-08-08 19:39:19 +02:00
|
|
|
ht.CloseChannel(alice, chanPointAlice)
|
|
|
|
ht.CloseChannel(bob, chanPointBob)
|
|
|
|
ht.CloseChannel(carol, chanPointCarol)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testQueryRoutes checks the response of queryroutes.
|
|
|
|
// We'll create the following network topology:
|
2022-08-22 20:58:42 +02:00
|
|
|
//
|
|
|
|
// Alice --> Bob --> Carol --> Dave
|
|
|
|
//
|
2021-04-16 10:41:51 +02:00
|
|
|
// and query the daemon for routes from Alice to Dave.
|
2022-08-08 19:47:50 +02:00
|
|
|
func testQueryRoutes(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
// Grab Alice and Bob from the standby nodes.
|
|
|
|
alice, bob := ht.Alice, ht.Bob
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
// Create Carol and connect her to Bob. We also send her some coins for
|
|
|
|
// channel opening.
|
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.ConnectNodes(carol, bob)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
// Create Dave and connect him to Carol.
|
|
|
|
dave := ht.NewNode("Dave", nil)
|
|
|
|
ht.ConnectNodes(dave, carol)
|
|
|
|
|
|
|
|
// We now proceed to open channels:
|
|
|
|
// Alice=>Bob, Bob=>Carol and Carol=>Dave.
|
|
|
|
p := lntemp.OpenChannelParams{Amt: chanAmt}
|
|
|
|
reqs := []*lntemp.OpenChannelRequest{
|
|
|
|
{Local: alice, Remote: bob, Param: p},
|
|
|
|
{Local: bob, Remote: carol, Param: p},
|
|
|
|
{Local: carol, Remote: dave, Param: p},
|
|
|
|
}
|
|
|
|
resp := ht.OpenMultiChannelsAsync(reqs)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
// Extract channel points from the response.
|
|
|
|
chanPointAlice := resp[0]
|
|
|
|
chanPointBob := resp[1]
|
|
|
|
chanPointCarol := resp[2]
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
// Before we continue, give Alice some time to catch up with the newly
|
|
|
|
// opened channels.
|
|
|
|
ht.AssertTopologyChannelOpen(alice, chanPointBob)
|
|
|
|
ht.AssertTopologyChannelOpen(alice, chanPointCarol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Query for routes to pay from Alice to Dave.
|
|
|
|
const paymentAmt = 1000
|
|
|
|
routesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
|
|
|
}
|
2022-08-08 19:47:50 +02:00
|
|
|
routesRes := ht.QueryRoutesAndRetry(alice, routesReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
const mSat = 1000
|
|
|
|
feePerHopMSat := computeFee(1000, 1, paymentAmt*mSat)
|
|
|
|
|
|
|
|
for i, route := range routesRes.Routes {
|
|
|
|
expectedTotalFeesMSat :=
|
|
|
|
lnwire.MilliSatoshi(len(route.Hops)-1) * feePerHopMSat
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
expectedTotalAmtMSat :=
|
|
|
|
(paymentAmt * mSat) + expectedTotalFeesMSat
|
|
|
|
|
|
|
|
if route.TotalFees != route.TotalFeesMsat/mSat {
|
2022-08-08 19:47:50 +02:00
|
|
|
ht.Fatalf("route %v: total fees %v (msat) does not "+
|
2021-04-16 10:41:51 +02:00
|
|
|
"round down to %v (sat)",
|
2022-10-25 15:27:40 +02:00
|
|
|
i, route.TotalFeesMsat, route.TotalFees)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
if route.TotalFeesMsat != int64(expectedTotalFeesMSat) {
|
2022-10-25 15:27:40 +02:00
|
|
|
ht.Fatalf("route %v: total fees in msat expected %v "+
|
|
|
|
"got %v", i, expectedTotalFeesMSat,
|
|
|
|
route.TotalFeesMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
if route.TotalAmt != route.TotalAmtMsat/mSat {
|
2022-08-08 19:47:50 +02:00
|
|
|
ht.Fatalf("route %v: total amt %v (msat) does not "+
|
2021-04-16 10:41:51 +02:00
|
|
|
"round down to %v (sat)",
|
2022-10-25 15:27:40 +02:00
|
|
|
i, route.TotalAmtMsat, route.TotalAmt)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
if route.TotalAmtMsat != int64(expectedTotalAmtMSat) {
|
2022-10-25 15:27:40 +02:00
|
|
|
ht.Fatalf("route %v: total amt in msat expected %v "+
|
|
|
|
"got %v", i, expectedTotalAmtMSat,
|
|
|
|
route.TotalAmtMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
// For all hops except the last, we check that fee equals
|
|
|
|
// feePerHop and amount to forward deducts feePerHop on each
|
|
|
|
// hop.
|
2021-04-16 10:41:51 +02:00
|
|
|
expectedAmtToForwardMSat := expectedTotalAmtMSat
|
|
|
|
for j, hop := range route.Hops[:len(route.Hops)-1] {
|
|
|
|
expectedAmtToForwardMSat -= feePerHopMSat
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
if hop.Fee != hop.FeeMsat/mSat {
|
|
|
|
ht.Fatalf("route %v hop %v: fee %v (msat) "+
|
|
|
|
"does not round down to %v (sat)",
|
|
|
|
i, j, hop.FeeMsat, hop.Fee)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
if hop.FeeMsat != int64(feePerHopMSat) {
|
2022-10-25 15:27:40 +02:00
|
|
|
ht.Fatalf("route %v hop %v: fee in msat "+
|
|
|
|
"expected %v got %v",
|
2021-04-16 10:41:51 +02:00
|
|
|
i, j, feePerHopMSat, hop.FeeMsat)
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
if hop.AmtToForward != hop.AmtToForwardMsat/mSat {
|
|
|
|
ht.Fatalf("route %v hop %v: amt to forward %v"+
|
|
|
|
"(msat) does not round down to %v(sat)",
|
|
|
|
i, j, hop.AmtToForwardMsat,
|
|
|
|
hop.AmtToForward)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
2022-10-25 15:27:40 +02:00
|
|
|
if hop.AmtToForwardMsat !=
|
|
|
|
int64(expectedAmtToForwardMSat) {
|
|
|
|
|
|
|
|
ht.Fatalf("route %v hop %v: amt to forward "+
|
|
|
|
"in msat expected %v got %v",
|
|
|
|
i, j, expectedAmtToForwardMSat,
|
|
|
|
hop.AmtToForwardMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-25 15:27:40 +02:00
|
|
|
|
|
|
|
// Last hop should have zero fee and amount to forward should
|
|
|
|
// equal payment amount.
|
2021-04-16 10:41:51 +02:00
|
|
|
hop := route.Hops[len(route.Hops)-1]
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
if hop.Fee != 0 || hop.FeeMsat != 0 {
|
|
|
|
ht.Fatalf("route %v hop %v: fee expected 0 got %v "+
|
|
|
|
"(sat) %v (msat)", i, len(route.Hops)-1,
|
|
|
|
hop.Fee, hop.FeeMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 15:27:40 +02:00
|
|
|
if hop.AmtToForward != hop.AmtToForwardMsat/mSat {
|
|
|
|
ht.Fatalf("route %v hop %v: amt to forward %v (msat) "+
|
|
|
|
"does not round down to %v (sat)", i,
|
|
|
|
len(route.Hops)-1, hop.AmtToForwardMsat,
|
|
|
|
hop.AmtToForward)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
if hop.AmtToForwardMsat != paymentAmt*mSat {
|
2022-08-08 19:47:50 +02:00
|
|
|
ht.Fatalf("route %v hop %v: amt to forward in msat "+
|
2022-10-25 15:27:40 +02:00
|
|
|
"expected %v got %v", i, len(route.Hops)-1,
|
|
|
|
paymentAmt*mSat, hop.AmtToForwardMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// While we're here, we test updating mission control's config values
|
|
|
|
// and assert that they are correctly updated and check that our mission
|
|
|
|
// control import function updates appropriately.
|
2022-08-08 19:47:50 +02:00
|
|
|
testMissionControlCfg(ht.T, alice)
|
|
|
|
testMissionControlImport(ht, alice, bob.PubKey[:], carol.PubKey[:])
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// We clean up the test case by closing channels that were created for
|
|
|
|
// the duration of the tests.
|
2022-08-08 19:47:50 +02:00
|
|
|
ht.CloseChannel(alice, chanPointAlice)
|
|
|
|
ht.CloseChannel(bob, chanPointBob)
|
|
|
|
ht.CloseChannel(carol, chanPointCarol)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testMissionControlCfg tests getting and setting of a node's mission control
|
|
|
|
// config, resetting to the original values after testing so that no other
|
|
|
|
// tests are affected.
|
2022-08-08 19:47:50 +02:00
|
|
|
func testMissionControlCfg(t *testing.T, hn *node.HarnessNode) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// Getting and setting does not alter the configuration.
|
|
|
|
startCfg := hn.RPC.GetMissionControlConfig().Config
|
|
|
|
hn.RPC.SetMissionControlConfig(startCfg)
|
|
|
|
resp := hn.RPC.GetMissionControlConfig()
|
|
|
|
require.True(t, proto.Equal(startCfg, resp.Config))
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// We test that setting and getting leads to the same config if all
|
|
|
|
// fields are set.
|
2021-04-16 10:41:51 +02:00
|
|
|
cfg := &routerrpc.MissionControlConfig{
|
|
|
|
MaximumPaymentResults: 30,
|
|
|
|
MinimumFailureRelaxInterval: 60,
|
2022-11-22 09:18:41 +01:00
|
|
|
Model: routerrpc.
|
|
|
|
MissionControlConfig_APRIORI,
|
|
|
|
EstimatorConfig: &routerrpc.MissionControlConfig_Apriori{
|
|
|
|
Apriori: &routerrpc.AprioriParameters{
|
|
|
|
HalfLifeSeconds: 8000,
|
|
|
|
HopProbability: 0.8,
|
|
|
|
Weight: 0.3,
|
|
|
|
},
|
|
|
|
},
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
2022-11-22 09:18:41 +01:00
|
|
|
hn.RPC.SetMissionControlConfig(cfg)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// The deprecated fields should be populated.
|
|
|
|
cfg.HalfLifeSeconds = 8000
|
|
|
|
cfg.HopProbability = 0.8
|
|
|
|
cfg.Weight = 0.3
|
|
|
|
respCfg := hn.RPC.GetMissionControlConfig().Config
|
|
|
|
require.True(t, proto.Equal(cfg, respCfg))
|
|
|
|
|
|
|
|
// Switching to another estimator is possible.
|
|
|
|
cfg = &routerrpc.MissionControlConfig{
|
|
|
|
Model: routerrpc.
|
|
|
|
MissionControlConfig_BIMODAL,
|
|
|
|
EstimatorConfig: &routerrpc.MissionControlConfig_Bimodal{
|
|
|
|
Bimodal: &routerrpc.BimodalParameters{
|
|
|
|
ScaleMsat: 1_000,
|
|
|
|
DecayTime: 500,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-08 19:47:50 +02:00
|
|
|
hn.RPC.SetMissionControlConfig(cfg)
|
2022-11-22 09:18:41 +01:00
|
|
|
respCfg = hn.RPC.GetMissionControlConfig().Config
|
|
|
|
require.NotNil(t, respCfg.GetBimodal())
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// If parameters are not set in the request, they will have zero values
|
|
|
|
// after.
|
|
|
|
require.Zero(t, respCfg.MaximumPaymentResults)
|
|
|
|
require.Zero(t, respCfg.MinimumFailureRelaxInterval)
|
|
|
|
require.Zero(t, respCfg.GetBimodal().NodeWeight)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// Setting deprecated values will initialize the apriori estimator.
|
|
|
|
cfg = &routerrpc.MissionControlConfig{
|
|
|
|
MaximumPaymentResults: 30,
|
|
|
|
MinimumFailureRelaxInterval: 60,
|
|
|
|
HopProbability: 0.8,
|
|
|
|
Weight: 0.3,
|
|
|
|
HalfLifeSeconds: 8000,
|
|
|
|
}
|
|
|
|
hn.RPC.SetMissionControlConfig(cfg)
|
|
|
|
respCfg = hn.RPC.GetMissionControlConfig().Config
|
|
|
|
require.NotNil(t, respCfg.GetApriori())
|
|
|
|
|
|
|
|
// Setting the wrong config results in an error.
|
|
|
|
cfg = &routerrpc.MissionControlConfig{
|
|
|
|
Model: routerrpc.
|
|
|
|
MissionControlConfig_APRIORI,
|
|
|
|
EstimatorConfig: &routerrpc.MissionControlConfig_Bimodal{
|
|
|
|
Bimodal: &routerrpc.BimodalParameters{
|
|
|
|
ScaleMsat: 1_000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hn.RPC.SetMissionControlConfigAssertErr(cfg)
|
2022-08-08 19:47:50 +02:00
|
|
|
|
2022-11-22 09:18:41 +01:00
|
|
|
// Undo any changes.
|
|
|
|
hn.RPC.SetMissionControlConfig(startCfg)
|
2022-08-08 19:47:50 +02:00
|
|
|
resp = hn.RPC.GetMissionControlConfig()
|
2022-11-22 09:18:41 +01:00
|
|
|
require.True(t, proto.Equal(startCfg, resp.Config))
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testMissionControlImport tests import of mission control results from an
|
|
|
|
// external source.
|
2022-08-08 19:47:50 +02:00
|
|
|
func testMissionControlImport(ht *lntemp.HarnessTest, hn *node.HarnessNode,
|
2021-04-16 10:41:51 +02:00
|
|
|
fromNode, toNode []byte) {
|
|
|
|
|
|
|
|
// Reset mission control so that our query will return the default
|
|
|
|
// probability for our first request.
|
2022-08-08 19:47:50 +02:00
|
|
|
hn.RPC.ResetMissionControl()
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Get our baseline probability for a 10 msat hop between our target
|
|
|
|
// nodes.
|
|
|
|
var amount int64 = 10
|
|
|
|
probReq := &routerrpc.QueryProbabilityRequest{
|
|
|
|
FromNode: fromNode,
|
|
|
|
ToNode: toNode,
|
|
|
|
AmtMsat: amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
importHistory := &routerrpc.PairData{
|
|
|
|
FailTime: time.Now().Unix(),
|
|
|
|
FailAmtMsat: amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that our history is not already equal to the value we want to
|
|
|
|
// set. This should not happen because we have just cleared our state.
|
2022-08-08 19:47:50 +02:00
|
|
|
resp1 := hn.RPC.QueryProbability(probReq)
|
|
|
|
require.Zero(ht, resp1.History.FailTime)
|
|
|
|
require.Zero(ht, resp1.History.FailAmtMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Now, we import a single entry which tracks a failure of the amount
|
|
|
|
// we want to query between our nodes.
|
|
|
|
req := &routerrpc.XImportMissionControlRequest{
|
|
|
|
Pairs: []*routerrpc.PairHistory{
|
|
|
|
{
|
|
|
|
NodeFrom: fromNode,
|
|
|
|
NodeTo: toNode,
|
|
|
|
History: importHistory,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-08-08 19:47:50 +02:00
|
|
|
hn.RPC.XImportMissionControl(req)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 19:47:50 +02:00
|
|
|
resp2 := hn.RPC.QueryProbability(probReq)
|
|
|
|
require.Equal(ht, importHistory.FailTime, resp2.History.FailTime)
|
|
|
|
require.Equal(ht, importHistory.FailAmtMsat, resp2.History.FailAmtMsat)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Finally, check that we will fail if inconsistent sat/msat values are
|
|
|
|
// set.
|
|
|
|
importHistory.FailAmtSat = amount * 2
|
2022-08-08 19:47:50 +02:00
|
|
|
hn.RPC.XImportMissionControlAssertErr(req)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testRouteFeeCutoff tests that we are able to prevent querying routes and
|
|
|
|
// sending payments that incur a fee higher than the fee limit.
|
2022-08-08 20:00:03 +02:00
|
|
|
func testRouteFeeCutoff(ht *lntemp.HarnessTest) {
|
2021-04-16 10:41:51 +02:00
|
|
|
// For this test, we'll create the following topology:
|
|
|
|
//
|
|
|
|
// --- Bob ---
|
|
|
|
// / \
|
|
|
|
// Alice ---- ---- Dave
|
|
|
|
// \ /
|
|
|
|
// -- Carol --
|
|
|
|
//
|
|
|
|
// Alice will attempt to send payments to Dave that should not incur a
|
|
|
|
// fee greater than the fee limit expressed as a percentage of the
|
|
|
|
// amount and as a fixed amount of satoshis.
|
|
|
|
const chanAmt = btcutil.Amount(100000)
|
|
|
|
|
|
|
|
// Open a channel between Alice and Bob.
|
2022-08-08 20:00:03 +02:00
|
|
|
alice, bob := ht.Alice, ht.Bob
|
|
|
|
chanPointAliceBob := ht.OpenChannel(
|
|
|
|
alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Carol's node and open a channel between her and Alice with
|
|
|
|
// Alice being the funder.
|
2022-08-08 20:00:03 +02:00
|
|
|
carol := ht.NewNode("Carol", nil)
|
|
|
|
ht.ConnectNodes(carol, alice)
|
|
|
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 20:00:03 +02:00
|
|
|
chanPointAliceCarol := ht.OpenChannel(
|
|
|
|
alice, carol, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create Dave's node and open a channel between him and Bob with Bob
|
|
|
|
// being the funder.
|
2022-08-08 20:00:03 +02:00
|
|
|
dave := ht.NewNode("Dave", nil)
|
|
|
|
ht.ConnectNodes(dave, bob)
|
|
|
|
chanPointBobDave := ht.OpenChannel(
|
|
|
|
bob, dave, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Open a channel between Carol and Dave.
|
2022-08-08 20:00:03 +02:00
|
|
|
ht.ConnectNodes(carol, dave)
|
|
|
|
chanPointCarolDave := ht.OpenChannel(
|
|
|
|
carol, dave, lntemp.OpenChannelParams{Amt: chanAmt},
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Now that all the channels were set up, we'll wait for all the nodes
|
|
|
|
// to have seen all the channels.
|
2022-08-08 20:00:03 +02:00
|
|
|
nodes := []*node.HarnessNode{alice, bob, carol, dave}
|
2021-04-16 10:41:51 +02:00
|
|
|
networkChans := []*lnrpc.ChannelPoint{
|
|
|
|
chanPointAliceBob, chanPointAliceCarol, chanPointBobDave,
|
|
|
|
chanPointCarolDave,
|
|
|
|
}
|
|
|
|
for _, chanPoint := range networkChans {
|
2022-08-08 20:00:03 +02:00
|
|
|
for _, node := range nodes {
|
|
|
|
ht.AssertTopologyChannelOpen(node, chanPoint)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The payments should only be successful across the route:
|
|
|
|
// Alice -> Bob -> Dave
|
|
|
|
// Therefore, we'll update the fee policy on Carol's side for the
|
|
|
|
// channel between her and Dave to invalidate the route:
|
|
|
|
// Alice -> Carol -> Dave
|
|
|
|
baseFee := int64(10000)
|
|
|
|
feeRate := int64(5)
|
|
|
|
timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta)
|
2022-08-12 10:02:32 +02:00
|
|
|
maxHtlc := lntemp.CalculateMaxHtlc(chanAmt)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
expectedPolicy := &lnrpc.RoutingPolicy{
|
|
|
|
FeeBaseMsat: baseFee,
|
|
|
|
FeeRateMilliMsat: testFeeBase * feeRate,
|
|
|
|
TimeLockDelta: timeLockDelta,
|
|
|
|
MinHtlc: 1000, // default value
|
|
|
|
MaxHtlcMsat: maxHtlc,
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFeeReq := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: float64(feeRate),
|
|
|
|
TimeLockDelta: timeLockDelta,
|
|
|
|
MaxHtlcMsat: maxHtlc,
|
|
|
|
Scope: &lnrpc.PolicyUpdateRequest_ChanPoint{
|
|
|
|
ChanPoint: chanPointCarolDave,
|
|
|
|
},
|
|
|
|
}
|
2022-08-08 20:00:03 +02:00
|
|
|
carol.RPC.UpdateChannelPolicy(updateFeeReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
// Wait for Alice to receive the channel update from Carol.
|
2022-08-08 20:00:03 +02:00
|
|
|
ht.AssertChannelPolicyUpdate(
|
|
|
|
alice, carol, expectedPolicy, chanPointCarolDave, false,
|
2021-04-16 10:41:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// We'll also need the channel IDs for Bob's channels in order to
|
|
|
|
// confirm the route of the payments.
|
2022-08-08 20:00:03 +02:00
|
|
|
channel := ht.QueryChannelByChanPoint(bob, chanPointAliceBob)
|
|
|
|
aliceBobChanID := channel.ChanId
|
|
|
|
require.NotZero(ht, aliceBobChanID,
|
|
|
|
"channel between alice and bob not found")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
2022-08-08 20:00:03 +02:00
|
|
|
channel = ht.QueryChannelByChanPoint(bob, chanPointBobDave)
|
|
|
|
bobDaveChanID := channel.ChanId
|
|
|
|
require.NotZero(ht, bobDaveChanID,
|
|
|
|
"channel between bob and dave not found")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
hopChanIDs := []uint64{aliceBobChanID, bobDaveChanID}
|
|
|
|
|
|
|
|
// checkRoute is a helper closure to ensure the route contains the
|
|
|
|
// correct intermediate hops.
|
|
|
|
checkRoute := func(route *lnrpc.Route) {
|
2022-08-08 20:00:03 +02:00
|
|
|
require.Len(ht, route.Hops, 2, "expected two hops")
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
for i, hop := range route.Hops {
|
2022-08-08 20:00:03 +02:00
|
|
|
require.Equal(ht, hopChanIDs[i], hop.ChanId,
|
|
|
|
"hop chan id not match")
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll be attempting to send two payments from Alice to Dave. One will
|
|
|
|
// have a fee cutoff expressed as a percentage of the amount and the
|
|
|
|
// other will have it expressed as a fixed amount of satoshis.
|
|
|
|
const paymentAmt = 100
|
|
|
|
carolFee := computeFee(lnwire.MilliSatoshi(baseFee), 1, paymentAmt)
|
|
|
|
|
|
|
|
// testFeeCutoff is a helper closure that will ensure the different
|
|
|
|
// types of fee limits work as intended when querying routes and sending
|
|
|
|
// payments.
|
|
|
|
testFeeCutoff := func(feeLimit *lnrpc.FeeLimit) {
|
|
|
|
queryRoutesReq := &lnrpc.QueryRoutesRequest{
|
|
|
|
PubKey: dave.PubKeyStr,
|
|
|
|
Amt: paymentAmt,
|
|
|
|
FeeLimit: feeLimit,
|
|
|
|
}
|
2022-08-08 20:00:03 +02:00
|
|
|
routesResp := alice.RPC.QueryRoutes(queryRoutesReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
checkRoute(routesResp.Routes[0])
|
|
|
|
|
|
|
|
invoice := &lnrpc.Invoice{Value: paymentAmt}
|
2022-08-08 20:00:03 +02:00
|
|
|
invoiceResp := dave.RPC.AddInvoice(invoice)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
sendReq := &routerrpc.SendPaymentRequest{
|
|
|
|
PaymentRequest: invoiceResp.PaymentRequest,
|
|
|
|
TimeoutSeconds: 60,
|
|
|
|
FeeLimitMsat: noFeeLimitMsat,
|
|
|
|
}
|
|
|
|
switch limit := feeLimit.Limit.(type) {
|
|
|
|
case *lnrpc.FeeLimit_Fixed:
|
|
|
|
sendReq.FeeLimitMsat = 1000 * limit.Fixed
|
|
|
|
case *lnrpc.FeeLimit_Percent:
|
|
|
|
sendReq.FeeLimitMsat = 1000 * paymentAmt * limit.Percent / 100
|
|
|
|
}
|
|
|
|
|
2022-08-08 20:00:03 +02:00
|
|
|
result := ht.SendPaymentAssertSettled(alice, sendReq)
|
2021-04-16 10:41:51 +02:00
|
|
|
|
|
|
|
checkRoute(result.Htlcs[0].Route)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll start off using percentages first. Since the fee along the
|
|
|
|
// route using Carol as an intermediate hop is 10% of the payment's
|
|
|
|
// amount, we'll use a lower percentage in order to invalid that route.
|
|
|
|
feeLimitPercent := &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_Percent{
|
|
|
|
Percent: baseFee/1000 - 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testFeeCutoff(feeLimitPercent)
|
|
|
|
|
|
|
|
// Now we'll test using fixed fee limit amounts. Since we computed the
|
|
|
|
// fee for the route using Carol as an intermediate hop earlier, we can
|
|
|
|
// use a smaller value in order to invalidate that route.
|
|
|
|
feeLimitFixed := &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_Fixed{
|
|
|
|
Fixed: int64(carolFee.ToSatoshis()) - 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testFeeCutoff(feeLimitFixed)
|
|
|
|
|
2022-08-08 20:00:03 +02:00
|
|
|
// TODO(yy): remove the sleep once the following bug is fixed. When the
|
|
|
|
// payment is reported as settled by Carol, it's expected the
|
|
|
|
// commitment dance is finished and all subsequent states have been
|
|
|
|
// updated. Yet we'd receive the error `cannot co-op close channel with
|
|
|
|
// active htlcs` or `link failed to shutdown` if we close the channel.
|
|
|
|
// We need to investigate the order of settling the payments and
|
|
|
|
// updating commitments to understand and fix .
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2021-04-16 10:41:51 +02:00
|
|
|
// Once we're done, close the channels and shut down the nodes created
|
|
|
|
// throughout this test.
|
2022-08-08 20:00:03 +02:00
|
|
|
ht.CloseChannel(alice, chanPointAliceBob)
|
|
|
|
ht.CloseChannel(alice, chanPointAliceCarol)
|
|
|
|
ht.CloseChannel(bob, chanPointBobDave)
|
|
|
|
ht.CloseChannel(carol, chanPointCarolDave)
|
2021-04-16 10:41:51 +02:00
|
|
|
}
|
2022-08-08 19:39:19 +02:00
|
|
|
|
|
|
|
// computeFee calculates the payment fee as specified in BOLT07.
|
|
|
|
func computeFee(baseFee, feeRate, amt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
|
|
|
|
return baseFee + amt*feeRate/1000000
|
|
|
|
}
|