From 1189f0ea6cbaecaf9973c50c7e4f473832e25753 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 3 Feb 2019 15:01:31 -0800 Subject: [PATCH] server: use chanInfo instead of chanPolicy to establish channel peer conns In this commit, we modify the way we attempt to locate the our channel peer to establish a persistent connection on start up. Before this commit, we would use the channel policy pointing to the peer to locate the node as it's embedded in the struct. However, at times it's currently possible for the channel policy to not be found in the database if the remote nodes announces before we finalize the process on our end. This can at times lead to a panic as the pointer isn't checked before attempting to access it. To remedy this, we now instead use the channel info which will _always_ be there if the channel is known. --- server.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/server.go b/server.go index e47bee78..ed560807 100644 --- a/server.go +++ b/server.go @@ -1680,19 +1680,39 @@ func (s *server) establishPersistentConnections() error { if err != nil { return err } + // TODO(roasbeef): instead iterate over link nodes and query graph for // each of the nodes. + selfPub := s.identityPriv.PubKey().SerializeCompressed() err = sourceNode.ForEachChannel(nil, func( - _ *bbolt.Tx, - _ *channeldb.ChannelEdgeInfo, + tx *bbolt.Tx, + chanInfo *channeldb.ChannelEdgeInfo, policy, _ *channeldb.ChannelEdgePolicy) error { - pubStr := string(policy.Node.PubKeyBytes[:]) + // If the remote party has announced the channel to us, but we + // haven't yet, then we won't have a policy. However, we don't + // need this to connect to the peer, so we'll log it and move on. + if policy == nil { + srvrLog.Warnf("No channel policy found for "+ + "ChannelPoint(%v): ", chanInfo.ChannelPoint) + } - // Add all unique addresses from channel graph/NodeAnnouncements - // to the list of addresses we'll connect to for this peer. + // We'll now fetch the peer opposite from us within this + // channel so we can queue up a direct connection to them. + channelPeer, err := chanInfo.FetchOtherNode(tx, selfPub) + if err != nil { + return fmt.Errorf("unable to fetch channel peer for "+ + "ChannelPoint(%v): %v", chanInfo.ChannelPoint, + err) + } + + pubStr := string(channelPeer.PubKeyBytes[:]) + + // Add all unique addresses from channel + // graph/NodeAnnouncements to the list of addresses we'll + // connect to for this peer. addrSet := make(map[string]net.Addr) - for _, addr := range policy.Node.Addresses { + for _, addr := range channelPeer.Addresses { switch addr.(type) { case *net.TCPAddr: addrSet[addr.String()] = addr @@ -1734,7 +1754,7 @@ func (s *server) establishPersistentConnections() error { n := &nodeAddresses{ addresses: addrs, } - n.pubKey, err = policy.Node.PubKey() + n.pubKey, err = channelPeer.PubKey() if err != nil { return err }