<p>Bitcoin Core maintains a LevelDB database (a key-value database) that contains the chain status data. This data includes the block headers, the block locations on disk, the UTXO set, and a few other things.</p>

<p>For blocks, the chainstate database has the block hashes as the key and block headers with some extra data appended as the values. The extra data also includes the block's location in the blk*.dat files (file number and byte offset within the file for the beginning of the block). So looking calling <code>getblock</code> just means that the key-value pair for the block hash is taken from the LevelDB database which I believe is O(nlog(n)) (IIRC it uses some kind of tree as the backing structure). To get the block itself, the lookup is O(1) as it just has to read a specific file at a specific offset.</p>

<p>Very few RPC calls in Bitcoin Core actually do any sort of searching through the blockchain. Most things are either stored in a database or a reference to its location is stored in the database so the item itself can quickly be retrieved by or following a database read.</p>

CC BY-SA 4.0
2018-05-15T02:02:48.990Z
2018-05-15T02:02:48.990Z
Ava Chow
<p>Is there a way to see the time/memory complexity of Bitcoin's API? For example, <strong>bitcoin-cli getblock "some-header-hash"</strong> fetches the block with a matching hash. </p>

<p>How is this implemented under the hood? Does it do a linear scan of the entire blockchain all the time or does it keep a (key, value) table where keys are header hashes? I'm concerned with the implementation as given in <a href="http://chainquery.com/bitcoin-api" rel="nofollow noreferrer">http://chainquery.com/bitcoin-api</a>.</p>

<api>
Time/memory complexity of Bitcoin API calls
SpiderRico
# Time/memory complexity of Bitcoin API calls

(Asked by: username_0 on 2018-05-15)

Is there a way to see the time/memory complexity of Bitcoin's API? For example, **bitcoin-cli getblock "some-header-hash"** fetches the block with a matching hash.

How is this implemented under the hood? Does it do a linear scan of the entire blockchain all the time or does it keep a (key, value) table where keys are header hashes? I'm concerned with the implementation as given in <http://chainquery.com/bitcoin-api>.

**Tags:** api


---

## 1 answers

### Answer by username_1 on 2018-05-15. Score: 1 | Accepted answer

Bitcoin Core maintains a LevelDB database (a key-value database) that contains the chain status data. This data includes the block headers, the block locations on disk, the UTXO set, and a few other things.

For blocks, the chainstate database has the block hashes as the key and block headers with some extra data appended as the values. The extra data also includes the block's location in the blk\*.dat files (file number and byte offset within the file for the beginning of the block). So looking calling `getblock` just means that the key-value pair for the block hash is taken from the LevelDB database which I believe is O(nlog(n)) (IIRC it uses some kind of tree as the backing structure). To get the block itself, the lookup is O(1) as it just has to read a specific file at a specific offset.

Very few RPC calls in Bitcoin Core actually do any sort of searching through the blockchain. Most things are either stored in a database or a reference to its location is stored in the database so the item itself can quickly be retrieved by or following a database read.


---

