From 654c5ea61adb0e576fc2bba0f14f14590f39fbb1 Mon Sep 17 00:00:00 2001 From: bryanvu Date: Wed, 22 Feb 2017 16:24:22 -0800 Subject: [PATCH] config: added support, tests for --externalip config option Minor change to server.go to add ExternalIPs to channeldb.LightningNode. Also, added a test that utilizes this functionality and exercises multiple addresses in NodeAnnouncement. --- channeldb/graph_test.go | 4 ++- lnd_test.go | 66 +++++++++++++++++++++++++++++++++++++++++ server.go | 14 +++++---- 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/channeldb/graph_test.go b/channeldb/graph_test.go index 36eadefe..e8170f30 100644 --- a/channeldb/graph_test.go +++ b/channeldb/graph_test.go @@ -23,7 +23,9 @@ import ( var ( testAddr = &net.TCPAddr{IP: (net.IP)([]byte{0xA, 0x0, 0x0, 0x1}), Port: 9000} - testAddrs = []net.Addr{testAddr} + anotherAddr, _ = net.ResolveTCPAddr("tcp", + "[2001:db8:85a3:0:0:8a2e:370:7334]:80") + testAddrs = []net.Addr{testAddr, anotherAddr} randSource = prand.NewSource(time.Now().Unix()) randInts = prand.New(randSource) diff --git a/lnd_test.go b/lnd_test.go index 92d5f265..77b2f36f 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -2069,6 +2069,68 @@ func testGraphTopologyNotifications(net *networkHarness, t *harnessTest) { } } +// testNodeAnnouncement ensures that when a node is started with one or more +// external IP addresses specified on the command line, that those addresses +// announced to the network and reported in the network graph. +func testNodeAnnouncement(net *networkHarness, t *harnessTest) { + timeout := time.Duration(time.Second * 15) + ctxb := context.Background() + ctxt, _ := context.WithTimeout(ctxb, timeout) + + ipAddresses := map[string]bool{ + "192.168.1.1:8333": true, + "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337": true, + "127.0.0.1:8335": true, + } + + var lndArgs []string + for address, _ := range ipAddresses { + lndArgs = append(lndArgs, "--externalip="+address) + } + + dave, err := net.NewNode(lndArgs) + if err != nil { + t.Fatalf("unable to create new nodes: %v", err) + } + + if err := net.ConnectNodes(ctxb, net.Alice, dave); err != nil { + t.Fatalf("unable to connect bob to carol: %v", err) + } + + chanAmt := btcutil.Amount(btcutil.SatoshiPerBitcoin / 2) + chanPointAlice := openChannelAndAssert(ctxt, t, net, net.Alice, dave, + chanAmt, 0) + + req := &lnrpc.ChannelGraphRequest{} + chanGraph, err := net.Alice.DescribeGraph(ctxb, req) + if err != nil { + t.Fatalf("unable to query for alice's routing table: %v", err) + } + + for _, node := range chanGraph.Nodes { + if node.PubKey == dave.PubKeyStr { + for _, address := range node.Addresses { + addrStr := address.String() + + // parse the IP address from the string + // representation of the TCPAddr + parts := strings.Split(addrStr, "\"") + if ipAddresses[parts[3]] { + delete(ipAddresses, parts[3]) + } else { + t.Fatalf("unexpected IP address: %v", + parts[3]) + } + } + } + } + if len(ipAddresses) != 0 { + t.Fatalf("expected IP addresses not in channel "+ + "graph: %v", ipAddresses) + } + closeChannelAndAssert(ctxt, t, net, net.Alice, chanPointAlice, false) +} + type testCase struct { name string test func(net *networkHarness, t *harnessTest) @@ -2124,6 +2186,10 @@ var testsCases = []*testCase{ test: testHtlcErrorPropagation, }, // TODO(roasbeef): multi-path integration test + { + name: "node announcement", + test: testNodeAnnouncement, + }, { // TODO(roasbeef): test always needs to be last as Bob's state // is borked since we trick him into attempting to cheat Alice? diff --git a/server.go b/server.go index 3461c098..a10cfc6e 100644 --- a/server.go +++ b/server.go @@ -154,12 +154,16 @@ func newServer(listenAddrs []string, notifier chainntnfs.ChainNotifier, debugPre[:], debugHash[:]) } - // TODO(roasbeef): add --externalip flag? - selfAddr, ok := listeners[0].Addr().(*net.TCPAddr) - if !ok { - return nil, fmt.Errorf("default listener must be TCP") + // If external IP addresses have been specified, add those to the list + // of this server's addresses. + selfAddrs := make([]net.Addr, 0) + for _, ip := range cfg.ExternalIPs { + addr, err := net.ResolveTCPAddr("tcp", ip) + if err != nil { + return nil, err + } + selfAddrs = append(selfAddrs, addr) } - selfAddrs := []net.Addr{selfAddr} chanGraph := chanDB.ChannelGraph() self := &channeldb.LightningNode{