This first commit delays requesting blocks from peers unless their tip has more than nMinimumChainWork
(our anti-DoS threshold). This speeds up initial headers sync by delaying block downloads from starting until we’re closer to being caught up.
The second commit implements a timeout on the initial headers sync: when we send the initial getheaders to a peer, estimate the number of headers we expect to receive, and set a timeout (15 minutes + 1 ms / header).
This is a pretty naive algorithm, but I think the worst case behavior (eg if we’re bottlenecked by our own bandwidth) is that we cycle through peers while downloading headers, at most every 15 minutes – which I think isn’t too awful, since those 15 minute windows should be making progress at least, and this bounds the time a bad peer can interfere with headers sync.
FYI I kicked off a fresh node with this patch, and it took me about 1 minute to have the whole headers chain.
Implements #9068, though this isn’t the exact approach @sipa suggested in that issue.