Download request more headers on ext (#1858)

* use lru

* add comment

* add comment
This commit is contained in:
Alex Sharov 2021-05-02 14:12:17 +07:00 committed by GitHub
parent 8a74b65ba2
commit 7eb088e862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -507,9 +507,22 @@ func (cs *ControlServerImpl) blockHeaders(ctx context.Context, req *proto_sentry
if segments, penalty, err := cs.hd.SplitIntoSegments(headersRaw, headers); err == nil { if segments, penalty, err := cs.hd.SplitIntoSegments(headersRaw, headers); err == nil {
if penalty == headerdownload.NoPenalty { if penalty == headerdownload.NoPenalty {
var canRequestMore bool
for _, segment := range segments { for _, segment := range segments {
cs.hd.ProcessSegment(segment, false /* newBlock */) requestMore := cs.hd.ProcessSegment(segment, false /* newBlock */)
canRequestMore = canRequestMore || requestMore
} }
if canRequestMore {
currentTime := uint64(time.Now().Unix())
if req := cs.hd.RequestMoreHeaders(currentTime); req != nil {
if peer := cs.sendHeaderRequest(ctx, req); peer != nil {
cs.hd.SentRequest(req, currentTime, 5 /* timeout */)
log.Debug("Sent request", "height", req.Number)
}
}
}
} else { } else {
outreq := proto_sentry.PenalizePeerRequest{ outreq := proto_sentry.PenalizePeerRequest{
PeerId: req.PeerId, PeerId: req.PeerId,

View File

@ -204,7 +204,7 @@ func (hd *HeaderDownload) StageReadyChannel() chan struct{} {
// ExtendDown extends some working trees down from the anchor, using given chain segment // ExtendDown extends some working trees down from the anchor, using given chain segment
// it creates a new anchor and collects all the links from the attached anchors to it // it creates a new anchor and collects all the links from the attached anchors to it
func (hd *HeaderDownload) extendDown(segment *ChainSegment, start, end int) error { func (hd *HeaderDownload) extendDown(segment *ChainSegment, start, end int) error {
// Find attachement anchor again // Find attachment anchor again
anchorHeader := segment.Headers[start] anchorHeader := segment.Headers[start]
if anchor, attaching := hd.anchors[anchorHeader.Hash()]; attaching { if anchor, attaching := hd.anchors[anchorHeader.Hash()]; attaching {
anchorPreverified := false anchorPreverified := false
@ -733,8 +733,11 @@ func (hi *HeaderInserter) AnythingDone() bool {
return hi.newCanonical return hi.newCanonical
} }
//nolint:interfacer // ProcessSegment - handling single segment.
func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool) { // If segment were processed by extendDown or newAnchor method, then it returns `requestMore=true`
// it allows higher-level algo immediately request more headers without waiting all stages precessing,
// speeds up visibility of new blocks
func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool) (requestMore bool) {
log.Debug("processSegment", "from", segment.Headers[0].Number.Uint64(), "to", segment.Headers[len(segment.Headers)-1].Number.Uint64()) log.Debug("processSegment", "from", segment.Headers[0].Number.Uint64(), "to", segment.Headers[len(segment.Headers)-1].Number.Uint64())
hd.lock.Lock() hd.lock.Lock()
defer hd.lock.Unlock() defer hd.lock.Unlock()
@ -767,6 +770,7 @@ func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool) {
log.Error("ExtendDown failed", "error", err) log.Error("ExtendDown failed", "error", err)
return return
} }
requestMore = true
log.Debug("Extended Down", "start", startNum, "end", endNum) log.Debug("Extended Down", "start", startNum, "end", endNum)
} }
} else if foundTip { } else if foundTip {
@ -784,6 +788,7 @@ func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool) {
log.Error("NewAnchor failed", "error", err) log.Error("NewAnchor failed", "error", err)
return return
} }
requestMore = true
log.Debug("NewAnchor", "start", startNum, "end", endNum) log.Debug("NewAnchor", "start", startNum, "end", endNum)
} }
//log.Info(hd.anchorState()) //log.Info(hd.anchorState())
@ -819,6 +824,8 @@ func (hd *HeaderDownload) ProcessSegment(segment *ChainSegment, newBlock bool) {
} }
} }
} }
return requestMore
} }
func (hd *HeaderDownload) TopSeenHeight() uint64 { func (hd *HeaderDownload) TopSeenHeight() uint64 {