-
Notifications
You must be signed in to change notification settings - Fork 186
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
neutrino+query+rescan: improve rescan speed #236
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d983c73
neutrino+query: use work dispatcher for GetBlock
ellemouton fd76d6f
neutrino+query: use work dispatcher for GetCFilter
ellemouton 2b2c363
query: separate goroutine for filter db writes
ellemouton 0e99431
rescan: wait till current or end height reached
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 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 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 |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"github.com/lightninglabs/neutrino/query" | ||
|
||
"github.com/btcsuite/btcd/blockchain" | ||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
"github.com/btcsuite/btcd/wire" | ||
|
@@ -947,75 +949,96 @@ func (s *ChainService) GetBlock(blockHash chainhash.Hash, | |
getData := wire.NewMsgGetData() | ||
_ = getData.AddInvVect(inv) | ||
|
||
// The block is only updated from the checkResponse function argument, | ||
// which is always called single-threadedly. We don't check the block | ||
// until after the query is finished, so we can just write to it | ||
// naively. | ||
var foundBlock *btcutil.Block | ||
s.queryPeers( | ||
// Send a wire.GetDataMsg | ||
getData, | ||
|
||
// Check responses and if we get one that matches, end the | ||
// query early. | ||
func(sp *ServerPeer, resp wire.Message, | ||
quit chan<- struct{}) { | ||
switch response := resp.(type) { | ||
// We're only interested in "block" messages. | ||
case *wire.MsgBlock: | ||
// Only keep this going if we haven't already | ||
// found a block, or we risk closing an already | ||
// closed channel. | ||
if foundBlock != nil { | ||
return | ||
request := &query.Request{ | ||
Req: getData, | ||
HandleResp: func(req, resp wire.Message, peer string) query.Progress { | ||
// The request must have been a "getdata" msg. | ||
_, ok := req.(*wire.MsgGetData) | ||
if !ok { | ||
return query.Progress{ | ||
Finished: false, | ||
Progressed: false, | ||
} | ||
} | ||
|
||
// If this isn't our block, ignore it. | ||
if response.BlockHash() != blockHash { | ||
return | ||
// We're only interested in "block" messages. | ||
response, ok := resp.(*wire.MsgBlock) | ||
if !ok { | ||
return query.Progress{ | ||
Finished: false, | ||
Progressed: false, | ||
} | ||
block := btcutil.NewBlock(response) | ||
} | ||
|
||
// Only set height if btcutil hasn't | ||
// automagically put one in. | ||
if block.Height() == btcutil.BlockHeightUnknown { | ||
block.SetHeight(int32(height)) | ||
// If this isn't the block we asked for, ignore it. | ||
if response.BlockHash() != blockHash { | ||
return query.Progress{ | ||
Finished: false, | ||
Progressed: false, | ||
} | ||
} | ||
|
||
// If this claims our block but doesn't pass | ||
// the sanity check, the peer is trying to | ||
// bamboozle us. Disconnect it. | ||
if err := blockchain.CheckBlockSanity( | ||
block, | ||
// We don't need to check PoW because | ||
// by the time we get here, it's been | ||
// checked during header | ||
// synchronization | ||
s.chainParams.PowLimit, | ||
s.timeSource, | ||
); err != nil { | ||
log.Warnf("Invalid block for %s "+ | ||
"received from %s -- "+ | ||
"disconnecting peer", blockHash, | ||
sp.Addr()) | ||
sp.Disconnect() | ||
return | ||
block := btcutil.NewBlock(response) | ||
|
||
// Only set height if btcutil hasn't | ||
// automagically put one in. | ||
if block.Height() == btcutil.BlockHeightUnknown { | ||
block.SetHeight(int32(height)) | ||
} | ||
|
||
// If this claims our block but doesn't pass | ||
// the sanity check, the peer is trying to | ||
// bamboozle us. | ||
if err := blockchain.CheckBlockSanity( | ||
block, | ||
// We don't need to check PoW because | ||
// by the time we get here, it's been | ||
// checked during header | ||
// synchronization | ||
s.chainParams.PowLimit, | ||
s.timeSource, | ||
); err != nil { | ||
log.Warnf("Invalid block for %s "+ | ||
"received from %s -- ", | ||
blockHash, peer) | ||
fmt.Println(err) | ||
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. Prior logic would disconnect here, but now we'll continue... Seems worthy of a future spin off to propagate a "ban worthy" error back up to the main scheduler. |
||
|
||
return query.Progress{ | ||
Finished: false, | ||
Progressed: false, | ||
} | ||
} | ||
|
||
// TODO(roasbeef): modify CheckBlockSanity to | ||
// also check witness commitment | ||
// TODO(roasbeef): modify CheckBlockSanity to | ||
// also check witness commitment | ||
|
||
// At this point, the block matches what we | ||
// know about it and we declare it sane. We can | ||
// kill the query and pass the response back to | ||
// the caller. | ||
foundBlock = block | ||
close(quit) | ||
default: | ||
// At this point, the block matches what we | ||
// know about it and we declare it sane. We can | ||
// kill the query and pass the response back to | ||
// the caller. | ||
foundBlock = block | ||
return query.Progress{ | ||
Finished: true, | ||
Progressed: true, | ||
} | ||
}, | ||
options..., | ||
} | ||
|
||
errChan := s.queryDispatcher.Query( | ||
[]*query.Request{request}, query.Encoding(qo.encoding), | ||
query.Cancel(s.quit), | ||
) | ||
|
||
select { | ||
case err := <-errChan: | ||
if err != nil { | ||
return nil, err | ||
} | ||
case <-s.quit: | ||
return nil, ErrShuttingDown | ||
} | ||
|
||
if foundBlock == nil { | ||
return nil, fmt.Errorf("couldn't retrieve block %s from "+ | ||
"network", blockHash) | ||
|
This file contains 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
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.
Maybe it's worth it to include the error to the
log.Warnf
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.
oh haha yeah that was left over from debugging 🙈 will fix. thanks!