From fee6b70519f200e7603bf1e26f70d292ea6c1b85 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Sun, 3 Nov 2024 13:34:16 +0800 Subject: [PATCH] itest: use `ht.CreateSimpleNetwork` whenever applicable So we won't forget to assert the topology after opening a chain of channels. --- itest/lnd_experimental_endorsement.go | 34 ++---- itest/lnd_forward_interceptor_test.go | 163 +++++++++----------------- itest/lnd_payment_test.go | 32 ++--- itest/lnd_quiescence_test.go | 13 +- 4 files changed, 86 insertions(+), 156 deletions(-) diff --git a/itest/lnd_experimental_endorsement.go b/itest/lnd_experimental_endorsement.go index 5a1f8fffc..43e29c2be 100644 --- a/itest/lnd_experimental_endorsement.go +++ b/itest/lnd_experimental_endorsement.go @@ -7,6 +7,7 @@ import ( "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lntest" + "github.com/lightningnetwork/lnd/lntest/node" "github.com/lightningnetwork/lnd/lntest/rpc" "github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntypes" @@ -24,37 +25,18 @@ func testExperimentalEndorsement(ht *lntest.HarnessTest) { // testEndorsement sets up a 5 hop network and tests propagation of // experimental endorsement signals. func testEndorsement(ht *lntest.HarnessTest, aliceEndorse bool) { - alice := ht.NewNodeWithCoins("Alice", nil) - bob := ht.NewNodeWithCoins("Bob", nil) - carol := ht.NewNode( - "carol", []string{"--protocol.no-experimental-endorsement"}, + cfg := node.CfgAnchor + carolCfg := append( + []string{"--protocol.no-experimental-endorsement"}, cfg..., ) - dave := ht.NewNode("dave", nil) - eve := ht.NewNode("eve", nil) + cfgs := [][]string{cfg, cfg, carolCfg, cfg, cfg} - ht.EnsureConnected(alice, bob) - ht.EnsureConnected(bob, carol) - ht.EnsureConnected(carol, dave) - ht.EnsureConnected(dave, eve) - - ht.FundCoins(btcutil.SatoshiPerBitcoin, carol) - ht.FundCoins(btcutil.SatoshiPerBitcoin, dave) - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - {Local: carol, Remote: dave, Param: p}, - {Local: dave, Remote: eve, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - _, cpBC, cpCD, cpDE := resp[0], resp[1], resp[2], resp[3] - // Make sure Alice is aware of Bob=>Carol=>Dave=>Eve channels. - ht.AssertChannelInGraph(alice, cpBC) - ht.AssertChannelInGraph(alice, cpCD) - ht.AssertChannelInGraph(alice, cpDE) + _, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol, dave, eve := nodes[0], nodes[1], nodes[2], nodes[3], + nodes[4] bobIntercept, cancelBob := bob.RPC.HtlcInterceptor() defer cancelBob() diff --git a/itest/lnd_forward_interceptor_test.go b/itest/lnd_forward_interceptor_test.go index 70227be24..93012c98f 100644 --- a/itest/lnd_forward_interceptor_test.go +++ b/itest/lnd_forward_interceptor_test.go @@ -42,23 +42,24 @@ type interceptorTestCase struct { // testForwardInterceptorDedupHtlc tests that upon reconnection, duplicate // HTLCs aren't re-notified using the HTLC interceptor API. func testForwardInterceptorDedupHtlc(ht *lntest.HarnessTest) { - // Initialize the test context with 3 connected nodes. - ts := newInterceptorTestScenario(ht) - - alice, bob, carol := ts.alice, ts.bob, ts.carol - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpAB, cpBC := resp[0], resp[1] - // Make sure Alice is aware of channel Bob=>Carol. - ht.AssertChannelInGraph(alice, cpBC) + // Initialize the test context with 3 connected nodes. + cfgs := [][]string{nil, nil, nil} + + // Open and wait for channels. + chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol := nodes[0], nodes[1], nodes[2] + cpAB := chanPoints[0] + + // Init the scenario. + ts := &interceptorTestScenario{ + ht: ht, + alice: alice, + bob: bob, + carol: carol, + } // Connect the interceptor. interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor() @@ -190,22 +191,24 @@ func testForwardInterceptorDedupHtlc(ht *lntest.HarnessTest) { // 4. When Interceptor disconnects it resumes all held htlcs, which result in // valid payment (invoice is settled). func testForwardInterceptorBasic(ht *lntest.HarnessTest) { - ts := newInterceptorTestScenario(ht) - - alice, bob, carol := ts.alice, ts.bob, ts.carol - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpAB, cpBC := resp[0], resp[1] - // Make sure Alice is aware of channel Bob=>Carol. - ht.AssertChannelInGraph(alice, cpBC) + // Initialize the test context with 3 connected nodes. + cfgs := [][]string{nil, nil, nil} + + // Open and wait for channels. + chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol := nodes[0], nodes[1], nodes[2] + cpAB := chanPoints[0] + + // Init the scenario. + ts := &interceptorTestScenario{ + ht: ht, + alice: alice, + bob: bob, + carol: carol, + } // Connect the interceptor. interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor() @@ -346,23 +349,23 @@ func testForwardInterceptorBasic(ht *lntest.HarnessTest) { // testForwardInterceptorModifiedHtlc tests that the interceptor can modify the // amount and custom records of an intercepted HTLC and resume it. func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) { - // Initialize the test context with 3 connected nodes. - ts := newInterceptorTestScenario(ht) - - alice, bob, carol := ts.alice, ts.bob, ts.carol - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpBC := resp[1] - // Make sure Alice is aware of channel Bob=>Carol. - ht.AssertChannelInGraph(alice, cpBC) + // Initialize the test context with 3 connected nodes. + cfgs := [][]string{nil, nil, nil} + + // Open and wait for channels. + _, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol := nodes[0], nodes[1], nodes[2] + + // Init the scenario. + ts := &interceptorTestScenario{ + ht: ht, + alice: alice, + bob: bob, + carol: carol, + } // Connect an interceptor to Bob's node. bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor() @@ -449,24 +452,15 @@ func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) { // wire custom records provided by the sender of a payment as part of the // update_add_htlc message. func testForwardInterceptorWireRecords(ht *lntest.HarnessTest) { - // Initialize the test context with 3 connected nodes. - ts := newInterceptorTestScenario(ht) - - alice, bob, carol, dave := ts.alice, ts.bob, ts.carol, ts.dave - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - {Local: carol, Remote: dave, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpBC := resp[1] - // Make sure Alice is aware of channel Bob=>Carol. - ht.AssertChannelInGraph(alice, cpBC) + // Initialize the test context with 4 connected nodes. + cfgs := [][]string{nil, nil, nil, nil} + + // Open and wait for channels. + _, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol, dave := nodes[0], nodes[1], nodes[2], nodes[3] // Connect an interceptor to Bob's node. bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor() @@ -574,25 +568,15 @@ func testForwardInterceptorWireRecords(ht *lntest.HarnessTest) { // update_add_htlc message and that those records are persisted correctly and // re-sent on node restart. func testForwardInterceptorRestart(ht *lntest.HarnessTest) { - // Initialize the test context with 3 connected nodes. - ts := newInterceptorTestScenario(ht) - - alice, bob, carol, dave := ts.alice, ts.bob, ts.carol, ts.dave - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - {Local: carol, Remote: dave, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpBC, cpCD := resp[1], resp[2] - // Make sure Alice is aware of channels Bob=>Carol and Carol=>Dave. - ht.AssertChannelInGraph(alice, cpBC) - ht.AssertChannelInGraph(alice, cpCD) + // Initialize the test context with 4 connected nodes. + cfgs := [][]string{nil, nil, nil, nil} + + // Open and wait for channels. + _, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol, dave := nodes[0], nodes[1], nodes[2], nodes[3] // Connect an interceptor to Bob's node. bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor() @@ -730,39 +714,8 @@ func testForwardInterceptorRestart(ht *lntest.HarnessTest) { // interceptorTestScenario is a helper struct to hold the test context and // provide the needed functionality. type interceptorTestScenario struct { - ht *lntest.HarnessTest - alice, bob, carol, dave *node.HarnessNode -} - -// newInterceptorTestScenario initializes a new test scenario with three nodes -// and connects them to have the following topology, -// -// Alice --> Bob --> Carol --> Dave -// -// Among them, Alice and Bob are standby nodes and Carol is a new node. -func newInterceptorTestScenario( - ht *lntest.HarnessTest) *interceptorTestScenario { - - alice := ht.NewNodeWithCoins("Alice", nil) - bob := ht.NewNodeWithCoins("bob", nil) - - carol := ht.NewNode("carol", nil) - dave := ht.NewNode("dave", nil) - - ht.EnsureConnected(alice, bob) - ht.EnsureConnected(bob, carol) - ht.EnsureConnected(carol, dave) - - // So that carol can open channels. - ht.FundCoins(btcutil.SatoshiPerBitcoin, carol) - - return &interceptorTestScenario{ - ht: ht, - alice: alice, - bob: bob, - carol: carol, - dave: dave, - } + ht *lntest.HarnessTest + alice, bob, carol *node.HarnessNode } // prepareTestCases prepares 4 tests: diff --git a/itest/lnd_payment_test.go b/itest/lnd_payment_test.go index 74adc4ffa..8f9b2bc74 100644 --- a/itest/lnd_payment_test.go +++ b/itest/lnd_payment_test.go @@ -1070,23 +1070,16 @@ func assertChannelState(ht *lntest.HarnessTest, hn *node.HarnessNode, // 5.) Alice observes a failed OR succeeded payment with failure reason // FAILURE_REASON_CANCELED which suppresses further payment attempts. func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) { - // Initialize the test context with 3 connected nodes. - ts := newInterceptorTestScenario(ht) - - alice, bob, carol := ts.alice, ts.bob, ts.carol - - // Open and wait for channels. const chanAmt = btcutil.Amount(300000) p := lntest.OpenChannelParams{Amt: chanAmt} - reqs := []*lntest.OpenChannelRequest{ - {Local: alice, Remote: bob, Param: p}, - {Local: bob, Remote: carol, Param: p}, - } - resp := ht.OpenMultiChannelsAsync(reqs) - cpAB, cpBC := resp[0], resp[1] - // Make sure Alice is aware of channel Bob=>Carol. - ht.AssertChannelInGraph(alice, cpBC) + // Initialize the test context with 3 connected nodes. + cfgs := [][]string{nil, nil, nil} + + // Open and wait for channels. + chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p) + alice, bob, carol := nodes[0], nodes[1], nodes[2] + cpAB := chanPoints[0] // Connect the interceptor. interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor() @@ -1096,7 +1089,8 @@ func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) { // htlc even though the payment context was canceled before invoice // settlement. sendPaymentInterceptAndCancel( - ht, ts, cpAB, routerrpc.ResolveHoldForwardAction_RESUME, + ht, alice, bob, carol, cpAB, + routerrpc.ResolveHoldForwardAction_RESUME, lnrpc.Payment_SUCCEEDED, interceptor, ) @@ -1106,20 +1100,18 @@ func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) { // Note that we'd have to reset Alice's mission control if we tested the // htlc fail case before the htlc resume case. sendPaymentInterceptAndCancel( - ht, ts, cpAB, routerrpc.ResolveHoldForwardAction_FAIL, + ht, alice, bob, carol, cpAB, + routerrpc.ResolveHoldForwardAction_FAIL, lnrpc.Payment_FAILED, interceptor, ) } func sendPaymentInterceptAndCancel(ht *lntest.HarnessTest, - ts *interceptorTestScenario, cpAB *lnrpc.ChannelPoint, + alice, bob, carol *node.HarnessNode, cpAB *lnrpc.ChannelPoint, interceptorAction routerrpc.ResolveHoldForwardAction, expectedPaymentStatus lnrpc.Payment_PaymentStatus, interceptor rpc.InterceptorClient) { - // Prepare the test cases. - alice, bob, carol := ts.alice, ts.bob, ts.carol - // Prepare the test cases. addResponse := carol.RPC.AddInvoice(&lnrpc.Invoice{ ValueMsat: 1000, diff --git a/itest/lnd_quiescence_test.go b/itest/lnd_quiescence_test.go index c698b41e4..d38ee6c78 100644 --- a/itest/lnd_quiescence_test.go +++ b/itest/lnd_quiescence_test.go @@ -6,6 +6,7 @@ import ( "github.com/lightningnetwork/lnd/lnrpc/devrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lntest" + "github.com/lightningnetwork/lnd/lntest/node" "github.com/stretchr/testify/require" ) @@ -16,12 +17,14 @@ import ( // NOTE FOR REVIEW: this could be improved by blasting the channel with HTLC // traffic on both sides to increase the surface area of the change under test. func testQuiescence(ht *lntest.HarnessTest) { - alice := ht.NewNodeWithCoins("Alice", nil) - bob := ht.NewNode("Bob", nil) + cfg := node.CfgAnchor + chanPoints, nodes := ht.CreateSimpleNetwork( + [][]string{cfg, cfg}, lntest.OpenChannelParams{ + Amt: btcutil.Amount(1000000), + }) - chanPoint := ht.OpenChannel(bob, alice, lntest.OpenChannelParams{ - Amt: btcutil.Amount(1000000), - }) + alice, bob := nodes[0], nodes[1] + chanPoint := chanPoints[0] res := alice.RPC.Quiesce(&devrpc.QuiescenceRequest{ ChanId: chanPoint,