|
| 1 | +package itest |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcutil" |
| 7 | + "github.com/lightningnetwork/lnd/lntest" |
| 8 | + "github.com/lightningnetwork/lnd/lntest/wait" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// testPeerBootstrapping tests that a node is able to use persisted node |
| 13 | +// announcements to bootstrap its peer connections. This is done by |
| 14 | +// connecting a node to a channel network so that it syncs the necessary gossip |
| 15 | +// and then: |
| 16 | +// 1. Asserting that on a restart where bootstrapping is _disabled_, the node |
| 17 | +// does not connect to any peers. |
| 18 | +// 2. Asserting that on a restart where bootstrapping is _enabled_, the node |
| 19 | +// does connect to peers. |
| 20 | +func testPeerBootstrapping(ht *lntest.HarnessTest) { |
| 21 | + // 1) Set up the following node/channel network. |
| 22 | + // Alice <- Bob <- Charlie |
| 23 | + _, nodes := ht.CreateSimpleNetwork( |
| 24 | + [][]string{nil, nil, nil}, lntest.OpenChannelParams{ |
| 25 | + Amt: btcutil.Amount(100000), |
| 26 | + }, |
| 27 | + ) |
| 28 | + carol, bob, alice := nodes[0], nodes[1], nodes[2] |
| 29 | + |
| 30 | + // Assert that they all know about each other and the channels. |
| 31 | + ht.AssertNumEdges(alice, 2, false) |
| 32 | + ht.AssertNumEdges(bob, 2, false) |
| 33 | + ht.AssertNumEdges(carol, 2, false) |
| 34 | + |
| 35 | + // Spin up a new node, Dave. |
| 36 | + dave := ht.NewNode("Dave", nil) |
| 37 | + |
| 38 | + // Explicitly assert that Dave was started with bootstrapping disabled. |
| 39 | + require.False(ht, dave.Cfg.WithPeerBootstrap) |
| 40 | + |
| 41 | + // Assert that Dave's current view of the graph is empty. |
| 42 | + ht.AssertNumEdges(dave, 0, false) |
| 43 | + |
| 44 | + // Now, connect Dave to Alice and wait for gossip sync to complete. |
| 45 | + ht.EnsureConnected(dave, alice) |
| 46 | + ht.AssertNumEdges(dave, 2, false) |
| 47 | + |
| 48 | + // Disconnect Dave from Alice and restart Dave. Since Alice and Dave |
| 49 | + // did not have channels between them, the nodes should not reconnect |
| 50 | + // to each other. |
| 51 | + ht.DisconnectNodes(dave, alice) |
| 52 | + ht.RestartNode(dave) |
| 53 | + |
| 54 | + // Dave still has bootstrapping disabled and so it should not connect |
| 55 | + // to any peers. |
| 56 | + err := wait.Invariant(func() bool { |
| 57 | + peerResp := dave.RPC.ListPeers() |
| 58 | + |
| 59 | + return len(peerResp.Peers) == 0 |
| 60 | + }, time.Second*5) |
| 61 | + require.NoError(ht, err) |
| 62 | + |
| 63 | + // Dave should still know about the full graph though since it was |
| 64 | + // synced previously from Alice. |
| 65 | + ht.AssertNumEdges(dave, 2, false) |
| 66 | + |
| 67 | + // Restart Dave again but this time with bootstrapping enabled. |
| 68 | + dave.Cfg.WithPeerBootstrap = true |
| 69 | + ht.RestartNode(dave) |
| 70 | + |
| 71 | + // Show that Dave now does connect to some peers. The default minimum |
| 72 | + // number of peers that will be connected to is 3, so we |
| 73 | + // expect Dave to connect to all three nodes in the network. |
| 74 | + ht.AssertConnected(dave, alice) |
| 75 | + ht.AssertConnected(dave, bob) |
| 76 | + ht.AssertConnected(dave, carol) |
| 77 | +} |
0 commit comments