What does "Activating Best Chain" mean?

7

1

What does "Activating Best Chain" mean? My Bitcoin Core doesn't freeze, it just takes a while. I'm curious as to what it's doing.

H. C. Barton

Posted 2015-11-26T02:03:06.433

Reputation: 125

Answers

10

Bitcoin Core maintains two databases:

  • The block index
  • The chain state (or UTXO set)

The first one just contains a list of all blocks we know about, valid and invalid. It contains all forks we have ever heard about, and all branches that result from it. It also contains information about where on disk these are stored. It does however not contain any information about which blocks we consider part of the "best chain".

The second one contains a snapshot of all existing unspent transaction outputs up to one particular block (and it also stores the hash of this block it is "synchronized" to). The process of validating blocks consists of "applying" the effects of blocks to this state, and possibly undoing those effects when the block turns out to be invalid, or we need to reorganize to another branch.

At startup, it is possible that the first database contains blocks that could be valid but aren't part of the snapshot the second database represents. This can happen when you manually overwrite the block index (restoring from a backup, for example), but more commonly: because Bitcoin crashed after downloading some blocks that ended up being written to the block index, but the resulting chain state was never dumped to disk. When this happens, we will activate the best chain, loading blocks from the second database and applying them to the first (while doing full validation on them in the process).

Since Bitcoin Core heavily caches the chain state in memory, it is possible that there are hundreds or thousands of blocks missing from the chain state after an unclean shutdown. It can take a while to validate them again.

TL;DR: It's verifying blocks that were downloaded already but not processed yet (because the result of processing wasn't properly written to disk, most likely).

Pieter Wuille

Posted 2015-11-26T02:03:06.433

Reputation: 54 032

Thank you, I think I understand. So it is looking through the local database and selecting the longest chain that was properly written?H. C. Barton 2015-11-26T17:49:28.897

Selecting, processing, verifying, and applying its effects.Pieter Wuille 2015-11-26T17:50:22.567

1This sounds like a good answer. However, I have noticed that it doesn't always take a bad shutdown to make it say "Activating Best Chain", sometimes terminating it cleanly before it fully synchronizes can make it say that when it comes back on. Also, most bad shutdowns, at least for me, kill everything and make it start back at 2009.H. C. Barton 2016-03-21T02:54:00.013

I've noticed the same as H.C.Barton, that is, I've got the "Activating Best Chain" message after a clean shut down. In this case, @PieterWuille: do you think this is a bug?knocte 2016-06-02T10:46:50.777

No it is not. It can mean that you downloaded some blocks, but you shut down Bitcoin Core before they were fully processed. In 0.13, the "Activating best chain" step will run in the background while the rest of the application is running already.Pieter Wuille 2016-06-02T11:03:58.347

3

Appending blocks to the longest blockchain fork.

         _ _ _ _ _ _ _this is the best chain
 _ _ _ _/
        \_ _ this is NOT

Farghaly

Posted 2015-11-26T02:03:06.433

Reputation: 849

1So it's going online and finding a blockchain fork to start downloading?Or is it finding the longest chain on the hard drive? If so, why would there be more than one chain in your local blockchain?H. C. Barton 2015-11-26T16:34:24.437

This is technically correct but very brief and confusing :)Pieter Wuille 2015-11-26T16:55:19.883