lnd_network_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package itest
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/lightningnetwork/lnd/lncfg"
  7. "github.com/lightningnetwork/lnd/lnrpc"
  8. "github.com/lightningnetwork/lnd/lntest"
  9. "github.com/lightningnetwork/lnd/lntest/node"
  10. "github.com/lightningnetwork/lnd/lntest/port"
  11. "github.com/lightningnetwork/lnd/lntest/wait"
  12. "github.com/stretchr/testify/require"
  13. )
  14. // testNetworkConnectionTimeout checks that the connectiontimeout is taking
  15. // effect. It creates a node with a small connection timeout value, and
  16. // connects it to a non-routable IP address.
  17. func testNetworkConnectionTimeout(ht *lntest.HarnessTest) {
  18. // Bind to a random port on localhost but never actually accept any
  19. // connections. This makes any connection attempts timeout.
  20. l, err := net.Listen("tcp", "127.0.0.1:0")
  21. require.NoError(ht, err)
  22. defer l.Close()
  23. var (
  24. // testPub is a random public key for testing only.
  25. testPub = "0332bda7da70fefe4b6ab92f53b3c4f4ee7999" +
  26. "f312284a8e89c8670bb3f67dbee2"
  27. // testHost is the previously bound address that will never
  28. // accept any conns.
  29. testHost = l.Addr().String()
  30. )
  31. // First, test the global timeout settings.
  32. // Create Carol with a connection timeout of 1 millisecond.
  33. carol := ht.NewNode("Carol", []string{"--connectiontimeout=1ms"})
  34. // Try to connect Carol to a non-routable IP address, which should give
  35. // us a timeout error.
  36. req := &lnrpc.ConnectPeerRequest{
  37. Addr: &lnrpc.LightningAddress{
  38. Pubkey: testPub,
  39. Host: testHost,
  40. },
  41. }
  42. // assertTimeoutError asserts that a connection timeout error is
  43. // raised. A context with a default timeout is used to make the
  44. // request. If our customized connection timeout is less than the
  45. // default, we won't see the request context times out, instead a
  46. // network connection timeout will be returned.
  47. assertTimeoutError := func(hn *node.HarnessNode,
  48. req *lnrpc.ConnectPeerRequest) {
  49. err := hn.RPC.ConnectPeerAssertErr(req)
  50. // Check that the network returns a timeout error.
  51. require.Containsf(ht, err.Error(), "i/o timeout",
  52. "expected to get a timeout error, instead got: %v", err)
  53. }
  54. assertTimeoutError(carol, req)
  55. // Second, test timeout on the connect peer request.
  56. // Create Dave with the default timeout setting.
  57. dave := ht.NewNode("Dave", nil)
  58. // Try to connect Dave to a non-routable IP address, using a timeout
  59. // value of 1s, which should give us a timeout error immediately.
  60. req = &lnrpc.ConnectPeerRequest{
  61. Addr: &lnrpc.LightningAddress{
  62. Pubkey: testPub,
  63. Host: testHost,
  64. },
  65. Timeout: 1,
  66. }
  67. assertTimeoutError(dave, req)
  68. }
  69. // testReconnectAfterIPChange verifies that if a persistent inbound node changes
  70. // its listening address then it's peer will still be able to reconnect to it.
  71. func testReconnectAfterIPChange(ht *lntest.HarnessTest) {
  72. // In this test, the following network will be set up. A single
  73. // dash line represents a peer connection and a double dash line
  74. // represents a channel.
  75. // Charlie will create a connection to Dave so that Dave is the inbound
  76. // peer. This will be made a persistent connection for Charlie so that
  77. // Charlie will attempt to reconnect to Dave if Dave restarts.
  78. // A channel will be opened between Dave and Alice to ensure that any
  79. // NodeAnnouncements that Dave sends will reach Alice.
  80. // The connection between Alice and Charlie ensures that Charlie
  81. // receives all of Dave's NodeAnnouncements.
  82. // The desired behaviour is that if Dave changes his P2P IP address then
  83. // Charlie should still be able to reconnect to him.
  84. //
  85. // /------- Charlie <-----\
  86. // | |
  87. // v |
  88. // Dave <===============> Alice
  89. // The first thing we will test is the case where Dave advertises two
  90. // external IP addresses and then switches from the first one listed
  91. // to the second one listed. The desired behaviour is that Charlie will
  92. // attempt both of Dave's advertised addresses when attempting to
  93. // reconnect.
  94. // Create a new node, Charlie.
  95. charlie := ht.NewNode("Charlie", nil)
  96. // We derive an extra port for Dave, and we initialise his node with
  97. // the port advertised as `--externalip` arguments.
  98. ip2 := port.NextAvailablePort()
  99. // Create a new node, Dave, which will initialize a P2P port for him.
  100. daveArgs := []string{fmt.Sprintf("--externalip=127.0.0.1:%d", ip2)}
  101. dave := ht.NewNode("Dave", daveArgs)
  102. // We now have two ports, the initial P2P port from creating the node,
  103. // and the `externalip` specified above.
  104. advertisedAddrs := []string{
  105. fmt.Sprintf("127.0.0.1:%d", dave.Cfg.P2PPort),
  106. fmt.Sprintf("127.0.0.1:%d", ip2),
  107. }
  108. // Connect Alice to Dave and Charlie.
  109. alice := ht.Alice
  110. ht.ConnectNodes(alice, dave)
  111. ht.ConnectNodes(alice, charlie)
  112. // We'll then go ahead and open a channel between Alice and Dave. This
  113. // ensures that Charlie receives the node announcement from Alice as
  114. // part of the announcement broadcast.
  115. chanPoint := ht.OpenChannel(
  116. alice, dave, lntest.OpenChannelParams{Amt: 1000000},
  117. )
  118. // waitForNodeAnnouncement is a closure used to wait on the given graph
  119. // subscription for a node announcement from a node with the given
  120. // public key. It also waits for the node announcement that advertises
  121. // a particular set of addresses.
  122. waitForNodeAnnouncement := func(nodePubKey string, addrs []string) {
  123. err := wait.NoError(func() error {
  124. // Expect to have at least 1 node announcement now.
  125. updates := ht.AssertNumNodeAnns(charlie, nodePubKey, 1)
  126. // Get latest node update from the node.
  127. update := updates[len(updates)-1]
  128. addrMap := make(map[string]bool)
  129. for _, addr := range update.NodeAddresses {
  130. addrMap[addr.GetAddr()] = true
  131. }
  132. // Check that our wanted addresses can be found from
  133. // the node update.
  134. for _, addr := range addrs {
  135. if !addrMap[addr] {
  136. return fmt.Errorf("address %s not "+
  137. "found", addr)
  138. }
  139. }
  140. return nil
  141. }, defaultTimeout)
  142. require.NoError(ht, err, "timeout checking node ann")
  143. }
  144. // Wait for Charlie to receive Dave's initial NodeAnnouncement.
  145. waitForNodeAnnouncement(dave.PubKeyStr, advertisedAddrs)
  146. // Now create a persistent connection between Charlie and Dave with no
  147. // channels. Charlie is the outbound node and Dave is the inbound node.
  148. ht.ConnectNodesPerm(charlie, dave)
  149. // Change Dave's P2P port to the second IP address that he advertised
  150. // and restart his node.
  151. dave.Cfg.P2PPort = ip2
  152. ht.RestartNode(dave)
  153. // assert that Dave and Charlie reconnect successfully after Dave
  154. // changes to his second advertised address.
  155. ht.AssertConnected(dave, charlie)
  156. // Next we test the case where Dave changes his listening address to one
  157. // that was not listed in his original advertised addresses. The desired
  158. // behaviour is that Charlie will update his connection requests to Dave
  159. // when he receives the Node Announcement from Dave with his updated
  160. // address.
  161. // Change Dave's listening port and restart.
  162. dave.Cfg.P2PPort = port.NextAvailablePort()
  163. dave.Cfg.ExtraArgs = []string{
  164. fmt.Sprintf(
  165. "--externalip=127.0.0.1:%d", dave.Cfg.P2PPort,
  166. ),
  167. }
  168. ht.RestartNode(dave)
  169. // Show that Charlie does receive Dave's new listening address in
  170. // a Node Announcement.
  171. waitForNodeAnnouncement(
  172. dave.PubKeyStr,
  173. []string{fmt.Sprintf("127.0.0.1:%d", dave.Cfg.P2PPort)},
  174. )
  175. // assert that Dave and Charlie do reconnect after Dave changes his P2P
  176. // address to one not listed in Dave's original advertised list of
  177. // addresses.
  178. ht.AssertConnected(dave, charlie)
  179. // Finally, close the channel.
  180. ht.CloseChannel(alice, chanPoint)
  181. }
  182. // testAddPeerConfig tests that the "--addpeer" config flag successfully adds
  183. // a new peer.
  184. func testAddPeerConfig(ht *lntest.HarnessTest) {
  185. alice := ht.Alice
  186. info := alice.RPC.GetInfo()
  187. alicePeerAddress := info.Uris[0]
  188. // Create a new node (Carol) with Alice as a peer.
  189. args := []string{fmt.Sprintf("--addpeer=%v", alicePeerAddress)}
  190. carol := ht.NewNode("Carol", args)
  191. // TODO(yy): remove this once the peer conn race is fixed.
  192. time.Sleep(1 * time.Second)
  193. ht.EnsureConnected(alice, carol)
  194. // If we list Carol's peers, Alice should already be
  195. // listed as one, since we specified her using the
  196. // addpeer flag.
  197. listPeersResp := carol.RPC.ListPeers()
  198. parsedPeerAddr, err := lncfg.ParseLNAddressString(
  199. alicePeerAddress, "9735", net.ResolveTCPAddr,
  200. )
  201. require.NoError(ht, err)
  202. parsedKeyStr := fmt.Sprintf(
  203. "%x", parsedPeerAddr.IdentityKey.SerializeCompressed(),
  204. )
  205. require.Equal(ht, parsedKeyStr, listPeersResp.Peers[0].PubKey)
  206. }