-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[6/7]: discovery: sync gossip using block heights & ranges #8255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ellemouton
wants to merge
5
commits into
lightningnetwork:elle-g175-thread-chan-update
Choose a base branch
from
ellemouton:g175-gossip-sync
base: elle-g175-thread-chan-update
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
deb4c2b
discovery: Filter ChanUpdate2 messages
ellemouton c248f60
channeldb: Update ChanUpdatesInHorizon
ellemouton 332cad3
discovery: update UpdatesInHorizon
ellemouton 6e8d8e1
discovery: start sending block heights in range query
ellemouton d95e2f3
docs: update release notes
ellemouton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2129,7 +2129,7 @@ type ChannelEdge struct { | |
// ChanUpdatesInHorizon returns all the known channel edges which have at least | ||
// one edge that has an update timestamp within the specified horizon. | ||
func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, | ||
endTime time.Time) ([]ChannelEdge, error) { | ||
endTime time.Time, startBlock, endBlock uint32) ([]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 | ||
|
@@ -2138,50 +2138,30 @@ func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, | |
var edgesToCache map[uint64]ChannelEdge | ||
var edgesInHorizon []ChannelEdge | ||
|
||
c.cacheMu.Lock() | ||
defer c.cacheMu.Unlock() | ||
|
||
var hits int | ||
err := kvdb.View(c.db, func(tx kvdb.RTx) error { | ||
edges := tx.ReadBucket(edgeBucket) | ||
if edges == nil { | ||
return ErrGraphNoEdgesFound | ||
} | ||
edgeIndex := edges.NestedReadBucket(edgeIndexBucket) | ||
if edgeIndex == nil { | ||
return ErrGraphNoEdgesFound | ||
} | ||
edgeUpdateIndex := edges.NestedReadBucket(edgeUpdateIndexBucket) | ||
fetchUpdates := func(tx kvdb.RTx, edges, edgeIndex, nodes kvdb.RBucket, | ||
updateIndexBkt []byte, startBytes, endBytes []byte, | ||
chanIDFromKey func([]byte) []byte) error { | ||
|
||
edgeUpdateIndex := edges.NestedReadBucket(updateIndexBkt) | ||
if edgeUpdateIndex == nil { | ||
return ErrGraphNoEdgesFound | ||
} | ||
|
||
nodes := tx.ReadBucket(nodeBucket) | ||
if nodes == nil { | ||
return ErrGraphNodesNotFound | ||
} | ||
|
||
// We'll now obtain a cursor to perform a range query within | ||
// the index to find all channels within the horizon. | ||
updateCursor := edgeUpdateIndex.ReadCursor() | ||
|
||
var startTimeBytes, endTimeBytes [8 + 8]byte | ||
byteOrder.PutUint64( | ||
startTimeBytes[:8], uint64(startTime.Unix()), | ||
) | ||
byteOrder.PutUint64( | ||
endTimeBytes[:8], uint64(endTime.Unix()), | ||
) | ||
|
||
// With our start and end times constructed, we'll step through | ||
// the index collecting the info and policy of each update of | ||
// each channel that has a last update within the time range. | ||
for indexKey, _ := updateCursor.Seek(startTimeBytes[:]); indexKey != nil && | ||
bytes.Compare(indexKey, endTimeBytes[:]) <= 0; indexKey, _ = updateCursor.Next() { | ||
//nolint:lll | ||
for indexKey, _ := updateCursor.Seek(startBytes); indexKey != nil && | ||
bytes.Compare(indexKey, endBytes) <= 0; indexKey, _ = updateCursor.Next() { //nolint:whitespace | ||
|
||
// We have a new eligible entry, so we'll slice of the | ||
// chan ID so we can query it in the DB. | ||
chanID := indexKey[8:] | ||
chanID := chanIDFromKey(indexKey) | ||
|
||
// If we've already retrieved the info and policies for | ||
// this edge, then we can skip it as we don't need to do | ||
|
@@ -2218,16 +2198,15 @@ func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, | |
err) | ||
} | ||
|
||
var ( | ||
node1Bytes = edgeInfo.Node1Bytes() | ||
node2Bytes = edgeInfo.Node2Bytes() | ||
) | ||
node1Bytes := edgeInfo.Node1Bytes() | ||
|
||
node1, err := fetchLightningNode(nodes, node1Bytes[:]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
node2Bytes := edgeInfo.Node2Bytes() | ||
|
||
node2, err := fetchLightningNode(nodes, node2Bytes[:]) | ||
if err != nil { | ||
return err | ||
|
@@ -2247,6 +2226,66 @@ func (c *ChannelGraph) ChanUpdatesInHorizon(startTime, | |
edgesToCache[chanIDInt] = channel | ||
} | ||
|
||
return nil | ||
} | ||
|
||
c.cacheMu.Lock() | ||
defer c.cacheMu.Unlock() | ||
|
||
err := kvdb.View(c.db, func(tx kvdb.RTx) error { | ||
edges := tx.ReadBucket(edgeBucket) | ||
if edges == nil { | ||
return ErrGraphNoEdgesFound | ||
} | ||
edgeIndex := edges.NestedReadBucket(edgeIndexBucket) | ||
if edgeIndex == nil { | ||
return ErrGraphNoEdgesFound | ||
} | ||
|
||
nodes := tx.ReadBucket(nodeBucket) | ||
if nodes == nil { | ||
return ErrGraphNodesNotFound | ||
} | ||
|
||
var startTimeBytes, endTimeBytes [8 + 8]byte | ||
byteOrder.PutUint64( | ||
startTimeBytes[:8], uint64(startTime.Unix()), | ||
) | ||
byteOrder.PutUint64( | ||
endTimeBytes[:8], uint64(endTime.Unix()), | ||
) | ||
|
||
var noEdgesFound bool | ||
err := fetchUpdates( | ||
tx, edges, edgeIndex, nodes, edgeUpdateIndexBucket, | ||
startTimeBytes[:], endTimeBytes[:], | ||
func(key []byte) []byte { | ||
return key[8:] | ||
}, | ||
) | ||
if errors.Is(err, ErrGraphNoEdgesFound) { | ||
noEdgesFound = true | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
var startBlockBytes, endBlockBytes [4 + 8]byte | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here re the key lengths. |
||
byteOrder.PutUint32(startTimeBytes[:4], startBlock) | ||
byteOrder.PutUint32(endTimeBytes[:4], endBlock) | ||
|
||
err = fetchUpdates( | ||
tx, edges, edgeIndex, nodes, edgeUpdate2IndexBucket, | ||
startBlockBytes[:], endBlockBytes[:], | ||
func(key []byte) []byte { | ||
return key[4:] | ||
}, | ||
) | ||
if errors.Is(err, ErrGraphNoEdgesFound) && noEdgesFound { | ||
return err | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, func() { | ||
edgesSeen = make(map[uint64]struct{}) | ||
|
@@ -3664,7 +3703,9 @@ func (c *ChannelGraph) FetchOtherNode(tx kvdb.RTx, | |
// otherwise we can use the existing db transaction. | ||
var err error | ||
if tx == nil { | ||
err = kvdb.View(c.db, fetchNodeFunc, func() { targetNode = nil }) | ||
err = kvdb.View(c.db, fetchNodeFunc, func() { | ||
targetNode = nil | ||
}) | ||
} else { | ||
err = fetchNodeFunc(tx) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's throw up these magic numbers into top level constants.