<p>The answer is that it doesn't use LevelDB for this.</p>
<p>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.</p>
<p>To build locator objects, it simply looks in this vector at specific indexes.</p>

CC BY-SA 4.0
2020-10-22T21:34:42.603Z
So every time bitcoin core receives a new block it has to update both this data and the database, am I right?
giacomocaironi
CC BY-SA 4.0
2020-10-22T21:35:42.907Z
Indeed, but the in-memory structure is very cheap to update.
Pieter Wuille
CC BY-SA 4.0
2020-10-22T21:18:38.047Z
2020-10-22T21:18:38.047Z
Pieter Wuille
<p>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 <a href="https://en.bitcoin.it/wiki/Protocol_documentation#getblocks" rel="nofollow noreferrer">wiki</a> and I have seen that after the 10 most recent block hashes, it goes back exponentially until the genesis block.</p>
<p>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.</p>

<network><leveldb>
How does bitcoin find the block locator hashes in the leveldb database?
giacomocaironi
# How does bitcoin find the block locator hashes in the leveldb database?

(Asked by: username_0 on 2020-10-22)

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](https://en.bitcoin.it/wiki/Protocol_documentation#getblocks) 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.

**Tags:** network, leveldb


---

## 1 answers

### Answer by username_1 on 2020-10-22. Score: 0 | Accepted answer

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.

> **Comments:**
> **username_0**: So every time bitcoin core receives a new block it has to update both this data and the database, am I right?  
> **username_1**: Indeed, but the in-memory structure is very cheap to update.  

---

