Merge pull request #1842 from wpaulino/dedup-chan-updates-horizon
channeldb: dedup channel edges returned from ChanUpdatesInHorizon
This commit is contained in:
commit
16b5a67c3a
@ -1176,6 +1176,10 @@ type ChannelEdge struct {
|
|||||||
// ChanUpdatesInHorizon returns all the known channel edges which have at least
|
// ChanUpdatesInHorizon returns all the known channel edges which have at least
|
||||||
// one edge that has an update timestamp within the specified horizon.
|
// one edge that has an update timestamp within the specified horizon.
|
||||||
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, endTime time.Time) ([]ChannelEdge, error) {
|
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, endTime time.Time) ([]ChannelEdge, error) {
|
||||||
|
// To ensure we don't return duplicate ChannelEdges, we'll use an
|
||||||
|
// additional map to keep track of the edges already seen to prevent
|
||||||
|
// re-adding it.
|
||||||
|
edgesSeen := make(map[uint64]struct{})
|
||||||
var edgesInHorizon []ChannelEdge
|
var edgesInHorizon []ChannelEdge
|
||||||
|
|
||||||
err := c.db.View(func(tx *bolt.Tx) error {
|
err := c.db.View(func(tx *bolt.Tx) error {
|
||||||
@ -1219,6 +1223,14 @@ func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, endTime time.Time) ([]Cha
|
|||||||
// chan ID so we can query it in the DB.
|
// chan ID so we can query it in the DB.
|
||||||
chanID := indexKey[8:]
|
chanID := indexKey[8:]
|
||||||
|
|
||||||
|
// If we've already retrieved the info and policies for
|
||||||
|
// this edge, then we can skip it as we don't need to do
|
||||||
|
// so again.
|
||||||
|
chanIDInt := byteOrder.Uint64(chanID)
|
||||||
|
if _, ok := edgesSeen[chanIDInt]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// First, we'll fetch the static edge information.
|
// First, we'll fetch the static edge information.
|
||||||
edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
|
edgeInfo, err := fetchChanEdgeInfo(edgeIndex, chanID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1242,6 +1254,7 @@ func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, endTime time.Time) ([]Cha
|
|||||||
|
|
||||||
// Finally, we'll collate this edge with the rest of
|
// Finally, we'll collate this edge with the rest of
|
||||||
// edges to be returned.
|
// edges to be returned.
|
||||||
|
edgesSeen[chanIDInt] = struct{}{}
|
||||||
edgesInHorizon = append(edgesInHorizon, ChannelEdge{
|
edgesInHorizon = append(edgesInHorizon, ChannelEdge{
|
||||||
Info: &edgeInfo,
|
Info: &edgeInfo,
|
||||||
Policy1: edge1,
|
Policy1: edge1,
|
||||||
|
@ -1398,11 +1398,12 @@ func TestChanUpdatesInHorizon(t *testing.T) {
|
|||||||
t.Fatalf("unable to create channel edge: %v", err)
|
t.Fatalf("unable to create channel edge: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTime := endTime
|
edge1UpdateTime := endTime
|
||||||
endTime = updateTime.Add(time.Second * 10)
|
edge2UpdateTime := edge1UpdateTime.Add(time.Second)
|
||||||
|
endTime = endTime.Add(time.Second * 10)
|
||||||
|
|
||||||
edge1 := newEdgePolicy(
|
edge1 := newEdgePolicy(
|
||||||
chanID.ToUint64(), op, db, updateTime.Unix(),
|
chanID.ToUint64(), op, db, edge1UpdateTime.Unix(),
|
||||||
)
|
)
|
||||||
edge1.Flags = 0
|
edge1.Flags = 0
|
||||||
edge1.Node = node2
|
edge1.Node = node2
|
||||||
@ -1412,7 +1413,7 @@ func TestChanUpdatesInHorizon(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
edge2 := newEdgePolicy(
|
edge2 := newEdgePolicy(
|
||||||
chanID.ToUint64(), op, db, updateTime.Unix(),
|
chanID.ToUint64(), op, db, edge2UpdateTime.Unix(),
|
||||||
)
|
)
|
||||||
edge2.Flags = 1
|
edge2.Flags = 1
|
||||||
edge2.Node = node1
|
edge2.Node = node1
|
||||||
|
Loading…
Reference in New Issue
Block a user