-
Notifications
You must be signed in to change notification settings - Fork 130
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
fix(dot/sync): fix block request and response logic #1907
Changes from 16 commits
8951703
f0250e9
7b07d9d
bdc5565
3e64089
ad9815b
38f917b
21cb17d
5d921eb
2312609
95431ce
2a9afb9
8985831
866d76a
8e46510
997f988
c2425d8
08d513c
d735eef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -646,9 +646,7 @@ func (cs *chainSync) doSync(req *network.BlockRequestMessage) *workerError { | |
|
||
if req.Direction == network.Descending { | ||
// reverse blocks before pre-validating and placing in ready queue | ||
for i, j := 0, len(resp.BlockData)-1; i < j; i, j = i+1, j-1 { | ||
resp.BlockData[i], resp.BlockData[j] = resp.BlockData[j], resp.BlockData[i] | ||
} | ||
reverseBlockData(resp.BlockData) | ||
} | ||
|
||
// perform some pre-validation of response, error if failure | ||
|
@@ -897,10 +895,18 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) { | |
} else { | ||
// in tip-syncing mode, we know the hash of the block on the fork we wish to sync | ||
start, _ = variadic.NewUint64OrHash(w.startHash) | ||
|
||
// if we're doing descending requests and not at the last (highest starting) request, | ||
// then use number as start block | ||
if w.direction == network.Descending && i != numRequests-1 { | ||
start = variadic.MustNewUint64OrHash(startNumber) | ||
} | ||
} | ||
|
||
var end *common.Hash | ||
if !w.targetHash.IsEmpty() { | ||
if !w.targetHash.IsEmpty() && i == numRequests-1 { | ||
// if we're on our last request (which should contain the target hash), | ||
// then add it | ||
end = &w.targetHash | ||
} | ||
|
||
|
@@ -911,7 +917,21 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) { | |
Direction: w.direction, | ||
Max: &max, | ||
} | ||
startNumber += maxResponseSize | ||
|
||
switch w.direction { | ||
case network.Ascending: | ||
startNumber += maxResponseSize | ||
case network.Descending: | ||
startNumber -= maxResponseSize | ||
} | ||
} | ||
|
||
// if our direction is descending, we want to send out the request with the lowest | ||
// startNumber first | ||
if w.direction == network.Descending { | ||
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. Can we directly do this inside loop instead of outside?
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. what do you mean exactly? |
||
for i, j := 0, len(reqs)-1; i < j; i, j = i+1, j-1 { | ||
reqs[i], reqs[j] = reqs[j], reqs[i] | ||
} | ||
EclesioMeloJunior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return reqs, nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,10 @@ var ( | |
ErrInvalidBlock = errors.New("could not verify block") | ||
|
||
// ErrInvalidBlockRequest is returned when an invalid block request is received | ||
ErrInvalidBlockRequest = errors.New("invalid block request") | ||
ErrInvalidBlockRequest = errors.New("invalid block request") | ||
errInvalidRequestDirection = errors.New("invalid request direction") | ||
errRequestStartTooHigh = errors.New("request start number is higher than our best block") | ||
errFailedToGetEndHashAncestor = errors.New("failed to get ancestor of end block") | ||
Comment on lines
+44
to
+46
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. If we do wrap only the bottom errors, we need to export all of them so a caller can check errors against them. 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. only this package deals with block requests/responses, so I don't think they need to be exported atm 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. Yes, but ultimately aren't these errors returned outside of the 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. I don't think they are, they're only returned to other places in this package I believe (and just logged) |
||
|
||
// chainSync errors | ||
errEmptyBlockData = errors.New("empty block data") | ||
|
@@ -57,6 +60,9 @@ var ( | |
errUnknownParent = errors.New("parent of first block in block response is unknown") | ||
errUnknownBlockForJustification = errors.New("received justification for unknown block") | ||
errFailedToGetParent = errors.New("failed to get parent header") | ||
errNilDescendantNumber = errors.New("descendant number is nil") | ||
errStartAndEndMismatch = errors.New("request start and end hash are not on the same chain") | ||
errFailedToGetDescendant = errors.New("failed to find descendant block") | ||
) | ||
|
||
// ErrNilChannel is returned if a channel is nil | ||
|
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.
Why not if/else? Are we expecting more directions?
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.
no, but I think in general we kinda like switch statements here :p what do you think?
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.
I think
else if
😉But I'm happy with
switch
as well. Although the only thing scaring me is if one additional case is added in the future and this block isn't updated, nothing will be executed whereas an if / else, the else gets executed. But that's a bit silly too I have to admit!