0
1
Let's say a user joins the Bitcoin network and downloads the latest state of the blockchain (all blocks, which includes the forks/uncles). How does this user find/determine the active blockchain on which they should build upon? I'm not asking about difficulty levels or the amount of work that gets put into the chain, but as to how this one user finds that longest chain and is sure that there are no more longer chains.
I imagine the simple (and only?) way to do this would be to check every single block the user has, and then start building upon the one which has the most work put into it. However, since nodes also store the numerous forks, evaluating every single block and its "path" throughout Bitcoin's existence is likely to take a while (and continue to take longer as time goes on).
A potential 'shortcut' would be to simply look up recent blocks, eg. search for blocks that were submitted in the last 48 hours, and then just take the longest valid chain from there, but then how would one decide on a particular timeframe? (24h? 48? last week? last month?) It would still in theory be possible that the current longest chain has been published a long time ago and other nodes are acting maliciously by trying to ignore it, so this approach doesn't seem valid in my opinion.
I know Bitcoin uses LevelDB to store metadata about blocks for quick look up, which would help speed up the evaluation of all the blocks, but is there are more pragmatic approach that Bitcoin's code takes?
Also, is this evaluation done from scratch (ie. from the genesis block) each time the user boots up, or is the information about the longest chain stored somewhere on disk for quick future retrieval when the user rejoins?
So, when a user (Alice) connects to the network and communicates with others peers (Bob and Charlie), Alice only asks for the header of the top block of their active chain, and not for their whole blockchain state (forks included)? Then from this information Alice works her way backwards to the genesis block.. if some block in the sequence is not found by Alice, she asks that same user for the lost block. If Alice eventually has the whole blockchain sequence to the genesis block, and the chain is deemed valid, then from all the valid chains Alice picks the one with most work? – E. Kruminis – 2019-08-09T19:12:04.270
1Kinda. 1. Alice asks each peer to tell them their best height. Then she asks the peer with the highest accumulated difficulty to provide her with the full chain of headers from her current block to their chaintip. After validating the header chain, Alice's node builds up from the genesis block towards the chaintip. Only if she learns about a better chaintip or finds an invalid block does she change the chaintip she's working towards. The sync happens from genesis block to chaintip. – Murch – 2019-08-09T20:45:02.933