-
Notifications
You must be signed in to change notification settings - Fork 292
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
Conversation
b3dfcd4
to
a73b902
Compare
blockchain/chain.go
Outdated
// the best chain, so there is nothing more to do. | ||
if startNode != nil { | ||
b.heightLock.RLock() | ||
if startNode.height+1 < int64(len(b.mainNodesByHeight)) { |
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.
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.
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.
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.
a73b902
to
a9dd488
Compare
a9dd488
to
23abca8
Compare
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.
23abca8
to
4f6ed9a
Compare
This requires PRs #1229, #1230, and #1237.
This refactors the code that locates blocks (inventory discovery) out of
server
and intoblockchain
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
andLocateHeaders
. 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 newLocateHeaders
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.