itest: refactor testMultiHopSendToRoute

This commit is contained in:
yyforyongyu committed 2023-01-16 16:49:20 +08:00
1 parent c5785967aa
commit 787883870b
3 files changed
+45 -128

No files matched your search

+4
View File
@@ -313,4 +313,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "single-hop send to route", Name: "single-hop send to route",
TestFunc: testSingleHopSendToRoute, TestFunc: testSingleHopSendToRoute,
}, },
{
Name: "multi-hop send to route",
TestFunc: testMultiHopSendToRoute,
},
} }
+41 -124
View File
@@ -292,15 +292,15 @@ func testSingleHopSendToRouteCase(ht *lntemp.HarnessTest,
// //
// We'll query the daemon for routes from Alice to Carol and then // We'll query the daemon for routes from Alice to Carol and then
// send payments through the routes. // send payments through the routes.
func testMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) { func testMultiHopSendToRoute(ht *lntemp.HarnessTest) {
t.t.Run("with cache", func(tt *testing.T) { ht.Run("with cache", func(tt *testing.T) {
ht := newHarnessTest(tt, t.lndHarness) st := ht.Subtest(tt)
runMultiHopSendToRoute(net, ht, true) runMultiHopSendToRoute(st, true)
}) })
if *dbBackendFlag == "bbolt" { if *dbBackendFlag == "bbolt" {
t.t.Run("without cache", func(tt *testing.T) { ht.Run("without cache", func(tt *testing.T) {
ht := newHarnessTest(tt, t.lndHarness) st := ht.Subtest(tt)
runMultiHopSendToRoute(net, ht, false) runMultiHopSendToRoute(st, false)
}) })
} }
} }
@@ -312,104 +312,45 @@ func testMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
// //
// We'll query the daemon for routes from Alice to Carol and then // We'll query the daemon for routes from Alice to Carol and then
// send payments through the routes. // send payments through the routes.
func runMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest, func runMultiHopSendToRoute(ht *lntemp.HarnessTest, useGraphCache bool) {
useGraphCache bool) {
ctxb := context.Background()
var opts []string var opts []string
if !useGraphCache { if !useGraphCache {
opts = append(opts, "--db.no-graph-cache") opts = append(opts, "--db.no-graph-cache")
} }
alice := net.NewNode(t.t, "Alice", opts) alice, bob := ht.Alice, ht.Bob
defer shutdownAndAssert(net, t, alice) ht.RestartNodeWithExtraArgs(alice, opts)
net.ConnectNodes(t.t, alice, net.Bob) ht.EnsureConnected(alice, bob)
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, alice)
const chanAmt = btcutil.Amount(100000) const chanAmt = btcutil.Amount(100000)
var networkChans []*lnrpc.ChannelPoint
// Open a channel with 100k satoshis between Alice and Bob with Alice // Open a channel with 100k satoshis between Alice and Bob with Alice
// being the sole funder of the channel. // being the sole funder of the channel.
chanPointAlice := openChannelAndAssert( chanPointAlice := ht.OpenChannel(
t, net, alice, net.Bob, alice, bob, lntemp.OpenChannelParams{Amt: chanAmt},
lntest.OpenChannelParams{
Amt: chanAmt,
},
) )
networkChans = append(networkChans, chanPointAlice) defer ht.CloseChannel(alice, chanPointAlice)
aliceChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointAlice) // Create Carol and establish a channel from Bob. Bob is the sole
if err != nil { // funder of the channel with 100k satoshis. The network topology
t.Fatalf("unable to get txid: %v", err) // should look like:
}
aliceFundPoint := wire.OutPoint{
Hash: *aliceChanTXID,
Index: chanPointAlice.OutputIndex,
}
// 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:
// Alice -> Bob -> Carol // Alice -> Bob -> Carol
carol := net.NewNode(t.t, "Carol", nil) carol := ht.NewNode("Carol", nil)
defer shutdownAndAssert(net, t, carol) ht.ConnectNodes(carol, bob)
net.ConnectNodes(t.t, carol, net.Bob) chanPointBob := ht.OpenChannel(
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, net.Bob) bob, carol, lntemp.OpenChannelParams{Amt: chanAmt},
chanPointBob := openChannelAndAssert(
t, net, net.Bob, carol,
lntest.OpenChannelParams{
Amt: chanAmt,
},
) )
networkChans = append(networkChans, chanPointBob) defer ht.CloseChannel(carol, chanPointBob)
bobChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointBob)
if err != nil {
t.Fatalf("unable to get txid: %v", err)
}
bobFundPoint := wire.OutPoint{
Hash: *bobChanTXID,
Index: chanPointBob.OutputIndex,
}
// Wait for all nodes to have seen all channels. // Create 5 invoices for Carol, which expect a payment from Alice for
nodes := []*lntest.HarnessNode{alice, net.Bob, carol} // 1k satoshis with a different preimage each time.
nodeNames := []string{"Alice", "Bob", "Carol"}
for _, chanPoint := range networkChans {
for i, node := range nodes {
txid, err := lnrpc.GetChanPointFundingTxid(chanPoint)
if err != nil {
t.Fatalf("unable to get txid: %v", err)
}
point := wire.OutPoint{
Hash: *txid,
Index: chanPoint.OutputIndex,
}
err = node.WaitForNetworkChannelOpen(chanPoint)
if err != nil {
t.Fatalf("%s(%d): timeout waiting for "+
"channel(%s) open: %v", nodeNames[i],
node.NodeID, point, err)
}
}
}
// Create 5 invoices for Carol, which expect a payment from Alice for 1k
// satoshis with a different preimage each time.
const ( const (
numPayments = 5 numPayments = 5
paymentAmt = 1000 paymentAmt = 1000
) )
_, rHashes, invoices, err := createPayReqs( _, rHashes, invoices := ht.CreatePayReqs(carol, paymentAmt, numPayments)
carol, paymentAmt, numPayments,
)
if err != nil {
t.Fatalf("unable to create pay reqs: %v", err)
}
// Construct a route from Alice to Carol for each of the invoices // Construct a route from Alice to Carol for each of the invoices
// created above. We set FinalCltvDelta to 40 since by default // created above. We set FinalCltvDelta to 40 since by default
@@ -420,30 +361,15 @@ func runMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest,
Amt: paymentAmt, Amt: paymentAmt,
FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta, FinalCltvDelta: chainreg.DefaultBitcoinTimeLockDelta,
} }
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) routes := alice.RPC.QueryRoutes(routesReq)
routes, err := alice.QueryRoutes(ctxt, routesReq)
if err != nil {
t.Fatalf("unable to get route: %v", err)
}
// We'll wait for all parties to recognize the new channels within the
// network.
err = carol.WaitForNetworkChannelOpen(chanPointBob)
if err != nil {
t.Fatalf("bob didn't advertise his channel in time: %v", err)
}
time.Sleep(time.Millisecond * 50)
// Using Alice as the source, pay to the 5 invoices from Carol created // Using Alice as the source, pay to the 5 invoices from Carol created
// above. // above.
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
for i, rHash := range rHashes { for i, rHash := range rHashes {
// Manually set the MPP payload a new for each payment since // Manually set the MPP payload a new for each payment since
// the payment addr will change with each invoice, although we // the payment addr will change with each invoice, although we
// can re-use the route itself. // can re-use the route itself.
route := *routes.Routes[0] route := routes.Routes[0]
route.Hops[len(route.Hops)-1].TlvPayload = true route.Hops[len(route.Hops)-1].TlvPayload = true
route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{ route.Hops[len(route.Hops)-1].MppRecord = &lnrpc.MPPRecord{
PaymentAddr: invoices[i].PaymentAddr, PaymentAddr: invoices[i].PaymentAddr,
@@ -454,16 +380,10 @@ func runMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest,
sendReq := &routerrpc.SendToRouteRequest{ sendReq := &routerrpc.SendToRouteRequest{
PaymentHash: rHash, PaymentHash: rHash,
Route: &route, Route: route,
}
resp, err := alice.RouterClient.SendToRouteV2(ctxt, sendReq)
if err != nil {
t.Fatalf("unable to send payment: %v", err)
}
if resp.Failure != nil {
t.Fatalf("received payment error: %v", resp.Failure)
} }
resp := alice.RPC.SendToRouteV2(sendReq)
require.Nil(ht, resp.Failure, "received payment error")
} }
// When asserting the amount of satoshis moved, we'll factor in the // When asserting the amount of satoshis moved, we'll factor in the
@@ -472,23 +392,20 @@ func runMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest,
const baseFee = 1 const baseFee = 1
// At this point all the channels within our proto network should be // At this point all the channels within our proto network should be
// shifted by 5k satoshis in the direction of Carol, the sink within the // shifted by 5k satoshis in the direction of Carol, the sink within
// payment flow generated above. The order of asserts corresponds to // the payment flow generated above. The order of asserts corresponds
// increasing of time is needed to embed the HTLC in commitment // to increasing of time is needed to embed the HTLC in commitment
// transaction, in channel Alice->Bob->Carol, order is Carol, Bob, // transaction, in channel Alice->Bob->Carol, order is Carol, Bob,
// Alice. // Alice.
const amountPaid = int64(5000) const amountPaid = int64(5000)
assertAmountPaid(t, "Bob(local) => Carol(remote)", carol, ht.AssertAmountPaid("Bob(local) => Carol(remote)", carol,
bobFundPoint, int64(0), amountPaid) chanPointBob, int64(0), amountPaid)
assertAmountPaid(t, "Bob(local) => Carol(remote)", net.Bob, ht.AssertAmountPaid("Bob(local) => Carol(remote)", bob,
bobFundPoint, amountPaid, int64(0)) chanPointBob, amountPaid, int64(0))
assertAmountPaid(t, "Alice(local) => Bob(remote)", net.Bob, ht.AssertAmountPaid("Alice(local) => Bob(remote)", bob,
aliceFundPoint, int64(0), amountPaid+(baseFee*numPayments)) chanPointAlice, int64(0), amountPaid+(baseFee*numPayments))
assertAmountPaid(t, "Alice(local) => Bob(remote)", alice, ht.AssertAmountPaid("Alice(local) => Bob(remote)", alice,
aliceFundPoint, amountPaid+(baseFee*numPayments), int64(0)) chanPointAlice, amountPaid+(baseFee*numPayments), int64(0))
closeChannelAndAssert(t, net, alice, chanPointAlice, false)
closeChannelAndAssert(t, net, carol, chanPointBob, false)
} }
// testSendToRouteErrorPropagation tests propagation of errors that occur // testSendToRouteErrorPropagation tests propagation of errors that occur
-4
View File
@@ -8,10 +8,6 @@ var allTestCases = []*testCase{
name: "single hop invoice", name: "single hop invoice",
test: testSingleHopInvoice, test: testSingleHopInvoice,
}, },
{
name: "multi-hop send to route",
test: testMultiHopSendToRoute,
},
{ {
name: "send to route error propagation", name: "send to route error propagation",
test: testSendToRouteErrorPropagation, test: testSendToRouteErrorPropagation,