Skip to content
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

multi: Refactor and optimize inv discovery. #1239

Merged
merged 1 commit into from
Jun 1, 2018

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented May 29, 2018

This requires PRs #1229, #1230, and #1237.

This refactors the code that locates blocks (inventory discovery) out of server and into blockchain where it can make use of the fact that all block nodes are now in memory and more easily be tested. As an aside, it really belongs in blockchain anyways since it's purely dealing with the block index and best chain.

In order to do this reasonably efficiently, a new memory-only height to block node mapping for the main chain is introduced to allow efficient forward traversal of the main chain. This is ultimately intended to be replaced by a chain view.

Since the network will be moving to header-based semantics, this also provides an additional optimization to allow headers to be located directly versus needing to first discover the hashes and then fetch the headers.

The new functions are named LocateBlocks and LocateHeaders. The former returns a slice of located hashes and the latter returns a slice of located headers.

Finally, it also updates the RPC server getheaders call and related plumbing to use the new LocateHeaders function.

A comprehensive suite of tests is provided to ensure both functions behave correctly for both correct and incorrect block locators.

This is work towards #1145 and fixes #427.

@davecgh davecgh mentioned this pull request May 29, 2018
28 tasks
@davecgh davecgh force-pushed the blockchain_locateblocks branch 2 times, most recently from b3dfcd4 to a73b902 Compare May 30, 2018 02:40
// the best chain, so there is nothing more to do.
if startNode != nil {
b.heightLock.RLock()
if startNode.height+1 < int64(len(b.mainNodesByHeight)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder whether mainNodesByHeight should be a slice rather than a map, where the index in the slice is the node's block height.

Copy link
Member Author

@davecgh davecgh May 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good observation. The eventual chain view will in fact use a slice, but for now this is a map because it will simplify the transition. That said, there really is no need for this length check here. It should just be setting startNode to the result of the lookup, which will either be the next node or nil as desired. The length check was actually because I forgot it was a map for a moment and thought it was a slice!

Changed.

@davecgh davecgh added this to the 1.3.0 milestone Jun 1, 2018
This refactors the code that locates blocks (inventory discovery) out of
server and into blockchain where it can make use of the fact that all
block nodes are now in memory and more easily be tested.  As an aside,
it really belongs in blockchain anyways since it's purely dealing with
the block index and best chain.

In order to do this reasonably efficiently, a new memory-only height
to block node mapping for the main chain is introduced to allow
efficient forward traversal of the main chain.  This is ultimately
intended to be replaced by a chain view.

Since the network will be moving to header-based semantics, this also
provides an additional optimization to allow headers to be located
directly versus needing to first discover the hashes and then fetch the
headers.

The new functions are named LocateBlocks and LocateHeaders. The former
returns a slice of located hashes and the latter returns a slice of
located headers.

Finally, it also updates the RPC server getheaders call and related
plumbing to use the new LocateHeaders function.

A comprehensive suite of tests is provided to ensure both functions
behave correctly for both correct and incorrect block locators.
@davecgh davecgh merged commit 4f6ed9a into decred:master Jun 1, 2018
@davecgh davecgh deleted the blockchain_locateblocks branch June 1, 2018 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getheaders does not handle sidechains correctly at all
3 participants