[ad_1]
What I’m interested in is at what point does node start requesting for
blocks? Is it during IBD or does it first download headers from all
peers, create a complete header tree and only then start downloading
blocks for the chain with the most cumulative power and move the
active chain tip?
There are two different processes.
The first is IBD. It is divided in two phases (caused by header pre-sync). In first we select one peer as the sync peer and send him one by one getheaders
requests. As the timestamp of received headers begins to approach the current timestamp, we start sending getheaders
messages to other peers as well. Peer responses with corresponding headers, we validate them and store their tiny identifiers (nothing is stored in block header tree at this moment).
Only after the peers send us all the headers they have (all header messages with less than 2000 objects) we move to the second phase.
In the second phase, we send getheaders
messages only for headers that are part of the chain that has enough cumulative work. There are two conditions: that cumulative work must be within one day of the current active chain tip and greater than a certain predefined value. Since the first condition at the very first moment is a genesis block (and some low difficulty blocks), it will be easily bypassed by malicious peer, however, the second condition will not. At this phase, it does not matter which peer you are requesting (whether it is sync peer or some other), it is only important that we ask some peer for which we know it has given header.
More to read about header pre-synchronization you can find in this post.
The second process works, something like, on events. Whenever a new header is added to the tree (event) this process is triggered. If various necessary conditions are met, a request for the block will be sent to one of the peers that is considered to have it.
When a node is just started, since in initial synchronization we only “talk” to one peer, then as sync peer sends us headers, we validate them and put in the tree, getdata
requests for these blocks will be sent to that sync peer immediately.
Therefore, no! We do not store all headers first, create complete tree and then download blocks. Everything is done simultaneously. However, we do download all headers in the tree for pre-sync first, before doing anything else.
For this and much more other information check this post.
[ad_2]
Source link