server: handle case where both policies are not present in fetchLastChanUpdateByOutPoint

This commit is contained in:
Johan T. Halseth 2018-08-09 10:03:14 +02:00
parent 5deffd228c
commit 4581aa313c
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -2964,32 +2964,48 @@ func (s *server) announceChanStatus(op wire.OutPoint, disabled bool) error {
return nil return nil
} }
// fetchLastChanUpdateByOutPoint fetches the latest update for a channel from // fetchLastChanUpdateByOutPoint fetches the latest policy for our direction of
// our point of view. // a channel, and crafts a new ChannelUpdate with this policy. Returns an error
func (s *server) fetchLastChanUpdateByOutPoint(op wire.OutPoint) (*lnwire.ChannelUpdate, error) { // in case our ChannelEdgePolicy is not found in the database.
func (s *server) fetchLastChanUpdateByOutPoint(op wire.OutPoint) (
*lnwire.ChannelUpdate, error) {
// Get the edge info and policies for this channel from the graph.
graph := s.chanDB.ChannelGraph() graph := s.chanDB.ChannelGraph()
info, edge1, edge2, err := graph.FetchChannelEdgesByOutpoint(&op) info, edge1, edge2, err := graph.FetchChannelEdgesByOutpoint(&op)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if edge1 == nil || edge2 == nil { // Helper function to extract the owner of the given policy.
return nil, fmt.Errorf("unable to find channel(%v)", op) owner := func(edge *channeldb.ChannelEdgePolicy) []byte {
var pubKey *btcec.PublicKey
switch {
case edge.Flags&lnwire.ChanUpdateDirection == 0:
pubKey, _ = info.NodeKey1()
case edge.Flags&lnwire.ChanUpdateDirection == 1:
pubKey, _ = info.NodeKey2()
} }
// If we're the outgoing node on the first edge, then that // If pubKey was not found, just return nil.
// means the second edge is our policy. Otherwise, the first if pubKey == nil {
// edge is our policy. return nil
var local *channeldb.ChannelEdgePolicy }
return pubKey.SerializeCompressed()
}
// Extract the channel update from the policy we own, if any.
ourPubKey := s.identityPriv.PubKey().SerializeCompressed() ourPubKey := s.identityPriv.PubKey().SerializeCompressed()
if bytes.Equal(edge1.Node.PubKeyBytes[:], ourPubKey) { if edge1 != nil && bytes.Equal(ourPubKey, owner(edge1)) {
local = edge2 return extractChannelUpdate(info, edge1)
} else {
local = edge1
} }
return extractChannelUpdate(info, local) if edge2 != nil && bytes.Equal(ourPubKey, owner(edge2)) {
return extractChannelUpdate(info, edge2)
}
return nil, fmt.Errorf("unable to find channel(%v)", op)
} }
// extractChannelUpdate retrieves a lnwire.ChannelUpdate message from an edge's // extractChannelUpdate retrieves a lnwire.ChannelUpdate message from an edge's