discovery: properly store and retrieve edge policies within mockGraphSource

In this commit, we address an issue with our router mock in which it was
not properly storing and retrieving edge policies. Previously, they were
being appended to a slice of policies, but this doesn't always work like
when you attempt to update the same edge twice. Instead, the slice can
only contain up to two entries, each one being the latest version of
each direction.
This commit is contained in:
Wilmer Paulino 2019-03-13 13:35:55 -07:00
parent 3a2c4ec594
commit 95ed11b01b

@ -155,7 +155,16 @@ func (r *mockGraphSource) UpdateEdge(edge *channeldb.ChannelEdgePolicy) error {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
r.edges[edge.ChannelID] = append(r.edges[edge.ChannelID], *edge) if len(r.edges[edge.ChannelID]) == 0 {
r.edges[edge.ChannelID] = make([]channeldb.ChannelEdgePolicy, 2)
}
if edge.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
r.edges[edge.ChannelID][0] = *edge
} else {
r.edges[edge.ChannelID][1] = *edge
}
return nil return nil
} }
@ -226,13 +235,17 @@ func (r *mockGraphSource) GetChannelByID(chanID lnwire.ShortChannelID) (
return &chanInfo, nil, nil, nil return &chanInfo, nil, nil, nil
} }
if len(edges) == 1 { var edge1 *channeldb.ChannelEdgePolicy
edge1 := edges[0] if !reflect.DeepEqual(edges[0], channeldb.ChannelEdgePolicy{}) {
return &chanInfo, &edge1, nil, nil edge1 = &edges[0]
} }
edge1, edge2 := edges[0], edges[1] var edge2 *channeldb.ChannelEdgePolicy
return &chanInfo, &edge1, &edge2, nil if !reflect.DeepEqual(edges[1], channeldb.ChannelEdgePolicy{}) {
edge2 = &edges[1]
}
return &chanInfo, edge1, edge2, nil
} }
func (r *mockGraphSource) FetchLightningNode( func (r *mockGraphSource) FetchLightningNode(
@ -327,11 +340,15 @@ func (r *mockGraphSource) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
} }
switch { switch {
case len(edges) >= 1 && edges[0].ChannelFlags == flags: case flags&lnwire.ChanUpdateDirection == 0 &&
return !edges[0].LastUpdate.Before(timestamp) !reflect.DeepEqual(edges[0], channeldb.ChannelEdgePolicy{}):
case len(edges) >= 2 && edges[1].ChannelFlags == flags: return !timestamp.After(edges[0].LastUpdate)
return !edges[1].LastUpdate.Before(timestamp)
case flags&lnwire.ChanUpdateDirection == 1 &&
!reflect.DeepEqual(edges[1], channeldb.ChannelEdgePolicy{}):
return !timestamp.After(edges[1].LastUpdate)
default: default:
return false return false