0

I am trying to understand how bitcoin core builds the list of block locator hashes needed for sendheaders and getheaders messages. I have looked on the wiki and I have seen that after the 10 most recent block hashes, it goes back exponentially until the genesis block.

So it seems that I have to get a block hash knowing its index in the blockchain. But how can bitcoin core find a block hash by its index? From my understanding leveldb lets you query data only by a key, and in this case the key is what I want.

1 Answer 1

0

The answer is that it doesn't use LevelDB for this.

It has an in-memory data structure with all known block headers, each pointing back to its parent. It also has a vector with pointers to all block headers in the currently active chain.

To build locator objects, it simply looks in this vector at specific indexes.

2
  • So every time bitcoin core receives a new block it has to update both this data and the database, am I right? Oct 22, 2020 at 21:34
  • Indeed, but the in-memory structure is very cheap to update. Oct 22, 2020 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.