How does a node find the active chain?

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?
Source: StackExchange News


Leave a Reply

Your email address will not be published. Required fields are marked *