mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-13 18:10:25 +02:00
itest: refactor testMultiHopOverPrivateChannels
This commit is contained in:
@@ -329,4 +329,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
|||||||
Name: "invoice routing hints",
|
Name: "invoice routing hints",
|
||||||
TestFunc: testInvoiceRoutingHints,
|
TestFunc: testInvoiceRoutingHints,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "multi-hop payments over private channels",
|
||||||
|
TestFunc: testMultiHopOverPrivateChannels,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
@@ -746,9 +746,7 @@ func testInvoiceRoutingHints(ht *lntemp.HarnessTest) {
|
|||||||
|
|
||||||
// testMultiHopOverPrivateChannels tests that private channels can be used as
|
// testMultiHopOverPrivateChannels tests that private channels can be used as
|
||||||
// intermediate hops in a route for payments.
|
// intermediate hops in a route for payments.
|
||||||
func testMultiHopOverPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
|
func testMultiHopOverPrivateChannels(ht *lntemp.HarnessTest) {
|
||||||
ctxb := context.Background()
|
|
||||||
|
|
||||||
// We'll test that multi-hop payments over private channels work as
|
// We'll test that multi-hop payments over private channels work as
|
||||||
// intended. To do so, we'll create the following topology:
|
// intended. To do so, we'll create the following topology:
|
||||||
// private public private
|
// private public private
|
||||||
@@ -757,115 +755,42 @@ func testMultiHopOverPrivateChannels(net *lntest.NetworkHarness, t *harnessTest)
|
|||||||
|
|
||||||
// First, we'll open a private channel between Alice and Bob with Alice
|
// First, we'll open a private channel between Alice and Bob with Alice
|
||||||
// being the funder.
|
// being the funder.
|
||||||
chanPointAlice := openChannelAndAssert(
|
alice, bob := ht.Alice, ht.Bob
|
||||||
t, net, net.Alice, net.Bob,
|
chanPointAlice := ht.OpenChannel(
|
||||||
lntest.OpenChannelParams{
|
alice, bob, lntemp.OpenChannelParams{
|
||||||
Amt: chanAmt,
|
Amt: chanAmt,
|
||||||
Private: true,
|
Private: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
err := net.Alice.WaitForNetworkChannelOpen(chanPointAlice)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("alice didn't see the channel alice <-> bob before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
err = net.Bob.WaitForNetworkChannelOpen(chanPointAlice)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("bob didn't see the channel alice <-> bob before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve Alice's funding outpoint.
|
|
||||||
aliceChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointAlice)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to get txid: %v", err)
|
|
||||||
}
|
|
||||||
aliceFundPoint := wire.OutPoint{
|
|
||||||
Hash: *aliceChanTXID,
|
|
||||||
Index: chanPointAlice.OutputIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next, we'll create Carol's node and open a public channel between
|
// Next, we'll create Carol's node and open a public channel between
|
||||||
// her and Bob with Bob being the funder.
|
// her and Bob with Bob being the funder.
|
||||||
carol := net.NewNode(t.t, "Carol", nil)
|
carol := ht.NewNode("Carol", nil)
|
||||||
defer shutdownAndAssert(net, t, carol)
|
ht.ConnectNodes(bob, carol)
|
||||||
|
chanPointBob := ht.OpenChannel(
|
||||||
net.ConnectNodes(t.t, net.Bob, carol)
|
bob, carol, lntemp.OpenChannelParams{
|
||||||
chanPointBob := openChannelAndAssert(
|
|
||||||
t, net, net.Bob, carol,
|
|
||||||
lntest.OpenChannelParams{
|
|
||||||
Amt: chanAmt,
|
Amt: chanAmt,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
err = net.Bob.WaitForNetworkChannelOpen(chanPointBob)
|
// Alice should know the new channel from Bob.
|
||||||
if err != nil {
|
ht.AssertTopologyChannelOpen(alice, chanPointBob)
|
||||||
t.Fatalf("bob didn't see the channel bob <-> carol before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
err = carol.WaitForNetworkChannelOpen(chanPointBob)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("carol didn't see the channel bob <-> carol before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
err = net.Alice.WaitForNetworkChannelOpen(chanPointBob)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("alice didn't see the channel bob <-> carol before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve Bob's funding outpoint.
|
// Next, we'll create Dave's node and open a private channel between
|
||||||
bobChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointBob)
|
// him and Carol with Carol being the funder.
|
||||||
if err != nil {
|
dave := ht.NewNode("Dave", nil)
|
||||||
t.Fatalf("unable to get txid: %v", err)
|
ht.ConnectNodes(carol, dave)
|
||||||
}
|
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
|
||||||
bobFundPoint := wire.OutPoint{
|
|
||||||
Hash: *bobChanTXID,
|
|
||||||
Index: chanPointBob.OutputIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next, we'll create Dave's node and open a private channel between him
|
chanPointCarol := ht.OpenChannel(
|
||||||
// and Carol with Carol being the funder.
|
carol, dave, lntemp.OpenChannelParams{
|
||||||
dave := net.NewNode(t.t, "Dave", nil)
|
|
||||||
defer shutdownAndAssert(net, t, dave)
|
|
||||||
|
|
||||||
net.ConnectNodes(t.t, carol, dave)
|
|
||||||
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, carol)
|
|
||||||
|
|
||||||
chanPointCarol := openChannelAndAssert(
|
|
||||||
t, net, carol, dave,
|
|
||||||
lntest.OpenChannelParams{
|
|
||||||
Amt: chanAmt,
|
Amt: chanAmt,
|
||||||
Private: true,
|
Private: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
err = carol.WaitForNetworkChannelOpen(chanPointCarol)
|
// Dave should know the channel[Bob<->Carol] from Carol.
|
||||||
if err != nil {
|
ht.AssertTopologyChannelOpen(dave, chanPointBob)
|
||||||
t.Fatalf("carol didn't see the channel carol <-> dave before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
err = dave.WaitForNetworkChannelOpen(chanPointCarol)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("dave didn't see the channel carol <-> dave before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
err = dave.WaitForNetworkChannelOpen(chanPointBob)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("dave didn't see the channel bob <-> carol before "+
|
|
||||||
"timeout: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve Carol's funding point.
|
|
||||||
carolChanTXID, err := lnrpc.GetChanPointFundingTxid(chanPointCarol)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to get txid: %v", err)
|
|
||||||
}
|
|
||||||
carolFundPoint := wire.OutPoint{
|
|
||||||
Hash: *carolChanTXID,
|
|
||||||
Index: chanPointCarol.OutputIndex,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now that all the channels are set up according to the topology from
|
// 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
|
// above, we can proceed to test payments. We'll create an invoice for
|
||||||
@@ -880,21 +805,11 @@ func testMultiHopOverPrivateChannels(net *lntest.NetworkHarness, t *harnessTest)
|
|||||||
Value: paymentAmt,
|
Value: paymentAmt,
|
||||||
Private: true,
|
Private: true,
|
||||||
}
|
}
|
||||||
|
resp := dave.RPC.AddInvoice(invoice)
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
|
||||||
resp, err := dave.AddInvoice(ctxt, invoice)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to add invoice for dave: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let Alice pay the invoice.
|
// Let Alice pay the invoice.
|
||||||
payReqs := []string{resp.PaymentRequest}
|
payReqs := []string{resp.PaymentRequest}
|
||||||
err = completePaymentRequests(
|
ht.CompletePaymentRequests(alice, payReqs)
|
||||||
net.Alice, net.Alice.RouterClient, payReqs, true,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to send payments from alice to dave: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// When asserting the amount of satoshis moved, we'll factor in the
|
// 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
|
// default base fee, as we didn't modify the fee structure when opening
|
||||||
@@ -902,39 +817,34 @@ func testMultiHopOverPrivateChannels(net *lntest.NetworkHarness, t *harnessTest)
|
|||||||
const baseFee = 1
|
const baseFee = 1
|
||||||
|
|
||||||
// Dave should have received 20k satoshis from Carol.
|
// Dave should have received 20k satoshis from Carol.
|
||||||
assertAmountPaid(t, "Carol(local) [private=>] Dave(remote)",
|
ht.AssertAmountPaid("Carol(local) [private=>] Dave(remote)",
|
||||||
dave, carolFundPoint, 0, paymentAmt)
|
dave, chanPointCarol, 0, paymentAmt)
|
||||||
|
|
||||||
// Carol should have sent 20k satoshis to Dave.
|
// Carol should have sent 20k satoshis to Dave.
|
||||||
assertAmountPaid(t, "Carol(local) [private=>] Dave(remote)",
|
ht.AssertAmountPaid("Carol(local) [private=>] Dave(remote)",
|
||||||
carol, carolFundPoint, paymentAmt, 0)
|
carol, chanPointCarol, paymentAmt, 0)
|
||||||
|
|
||||||
// Carol should have received 20k satoshis + fee for one hop from Bob.
|
// Carol should have received 20k satoshis + fee for one hop from Bob.
|
||||||
assertAmountPaid(t, "Bob(local) => Carol(remote)",
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)",
|
||||||
carol, bobFundPoint, 0, paymentAmt+baseFee)
|
carol, chanPointBob, 0, paymentAmt+baseFee)
|
||||||
|
|
||||||
// Bob should have sent 20k satoshis + fee for one hop to Carol.
|
// Bob should have sent 20k satoshis + fee for one hop to Carol.
|
||||||
assertAmountPaid(t, "Bob(local) => Carol(remote)",
|
ht.AssertAmountPaid("Bob(local) => Carol(remote)",
|
||||||
net.Bob, bobFundPoint, paymentAmt+baseFee, 0)
|
bob, chanPointBob, paymentAmt+baseFee, 0)
|
||||||
|
|
||||||
// Bob should have received 20k satoshis + fee for two hops from Alice.
|
// Bob should have received 20k satoshis + fee for two hops from Alice.
|
||||||
assertAmountPaid(t, "Alice(local) [private=>] Bob(remote)", net.Bob,
|
ht.AssertAmountPaid("Alice(local) [private=>] Bob(remote)", bob,
|
||||||
aliceFundPoint, 0, paymentAmt+baseFee*2)
|
chanPointAlice, 0, paymentAmt+baseFee*2)
|
||||||
|
|
||||||
// Alice should have sent 20k satoshis + fee for two hops to Bob.
|
// Alice should have sent 20k satoshis + fee for two hops to Bob.
|
||||||
assertAmountPaid(t, "Alice(local) [private=>] Bob(remote)", net.Alice,
|
ht.AssertAmountPaid("Alice(local) [private=>] Bob(remote)", alice,
|
||||||
aliceFundPoint, paymentAmt+baseFee*2, 0)
|
chanPointAlice, paymentAmt+baseFee*2, 0)
|
||||||
|
|
||||||
// At this point, the payment was successful. We can now close all the
|
// At this point, the payment was successful. We can now close all the
|
||||||
// channels and shutdown the nodes created throughout this test.
|
// channels and shutdown the nodes created throughout this test.
|
||||||
closeChannelAndAssert(t, net, net.Alice, chanPointAlice, false)
|
ht.CloseChannel(alice, chanPointAlice)
|
||||||
closeChannelAndAssert(t, net, net.Bob, chanPointBob, false)
|
ht.CloseChannel(bob, chanPointBob)
|
||||||
closeChannelAndAssert(t, net, carol, chanPointCarol, false)
|
ht.CloseChannel(carol, chanPointCarol)
|
||||||
}
|
|
||||||
|
|
||||||
// computeFee calculates the payment fee as specified in BOLT07.
|
|
||||||
func computeFee(baseFee, feeRate, amt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
|
|
||||||
return baseFee + amt*feeRate/1000000
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// testQueryRoutes checks the response of queryroutes.
|
// testQueryRoutes checks the response of queryroutes.
|
||||||
@@ -1460,3 +1370,8 @@ func testRouteFeeCutoff(net *lntest.NetworkHarness, t *harnessTest) {
|
|||||||
closeChannelAndAssert(t, net, net.Bob, chanPointBobDave, false)
|
closeChannelAndAssert(t, net, net.Bob, chanPointBobDave, false)
|
||||||
closeChannelAndAssert(t, net, carol, chanPointCarolDave, false)
|
closeChannelAndAssert(t, net, carol, chanPointCarolDave, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// computeFee calculates the payment fee as specified in BOLT07.
|
||||||
|
func computeFee(baseFee, feeRate, amt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
|
||||||
|
return baseFee + amt*feeRate/1000000
|
||||||
|
}
|
||||||
|
@@ -8,10 +8,6 @@ var allTestCases = []*testCase{
|
|||||||
name: "single hop invoice",
|
name: "single hop invoice",
|
||||||
test: testSingleHopInvoice,
|
test: testSingleHopInvoice,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "multi-hop payments over private channels",
|
|
||||||
test: testMultiHopOverPrivateChannels,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "multiple channel creation and update subscription",
|
name: "multiple channel creation and update subscription",
|
||||||
test: testBasicChannelCreationAndUpdates,
|
test: testBasicChannelCreationAndUpdates,
|
||||||
|
Reference in New Issue
Block a user