<blockquote>
<ol>
<li>What is the <code>mapBlocksUnknownParent</code> variable? Where is it useful?</li>
</ol>
</blockquote>
<p>It keeps a map of blocks in the blk*.dat files whose parent is unknown, indexed by the block hash of that parent.</p>
<blockquote>
<ol start="2">
<li>What does <code>LoadExternalBlockFile</code> function do? (by its name I deduce that it reads <code>blk???.dat</code> files and load them into the memory at the start up. But I'm not sure)</li>
</ol>
</blockquote>
<p>It deals with importing blocks from external files. This means not from the network, and not blocks already in our block database. It happens in two contexts:</p>
<ul>
<li>When a file is specified on the command line using <code>-loadblock=&lt;FILE&gt;</code>; this can be used to import blocks e.g. given to you on portable media (say, a USB stick).</li>
<li>When running with <code>-reindex</code>, in which case the block database is wiped, but the blk*.dat files are kept, and then this &quot;importing&quot; operation is run on these still existing blk*.dat files.</li>
</ul>
<blockquote>
<ol start="3">
<li>When is it possible to have blocks with unknown parents? While AFAIK we always reach blocks through their ancestors, not descendants. So in which situation we have a block with unknown parent?</li>
</ol>
</blockquote>
<p>When requesting blocks from the network, we only ask for blocks whose parents are known (at least the headers must be known), so indeed, this variable isn't useful in that context. But the blocks are not downloaded in order, and the blk*.dat files contain blocks in the order they were received.</p>
<p>This means that when using <code>-reindex</code> to rebuild the database, some blocks will be encountered out-of-order, meaning blocks will be read whose parent isn't known yet. To deal with this situation, <code>LoadExternalBlockFile</code> keeps track of these blocks without known parent. When the parent is encountered and processed, the children will be processed too.</p>
<blockquote>
<ol start="4">
<li>Why this variable is considered global while it is defined inside of a function scope? Is this because the variable is <code>static</code>?</li>
</ol>
</blockquote>
<p>Exactly. <code>static</code> variables in function scopes are effectively globals that can only be accessed from that function. Only one instance of the variable exists, shared between all invocations of that function.</p>

CC BY-SA 4.0
2022-07-08T12:41:46.417Z
2022-07-08T12:41:46.417Z
Pieter Wuille
<p>I was reviewing this <a href="https://github.com/bitcoin/bitcoin/pull/25571" rel="nofollow noreferrer">pull request</a>. It was an attempt to remove the global variable <code>mapBlocksUnknownParent</code>. I found the definition <a href="https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L4263" rel="nofollow noreferrer">here</a>. In the <code>CChainState::LoadExternalBlockFile</code> function.</p>
<pre><code>    // Map of disk positions for blocks with unknown parent (only used for reindex)
    static std::multimap&lt;uint256, FlatFilePos&gt; mapBlocksUnknownParent;
</code></pre>
<h3>Questions:</h3>
<ol>
<li><p>What is the <code>mapBlocksUnknownParent</code> variable? Where is it useful?</p>
</li>
<li><p>What does <code>LoadExternalBlockFile</code> function do? (by its name I deduce that it reads <code>blk???.dat</code> files and load them into the memory at the start up, but I'm not sure)</p>
</li>
<li><p>When it is possible to have blocks with unknown parents? While AFAIK we always reach blocks through their ancestors, not descendants. So in which situation we have a block with unknown parent?</p>
</li>
<li><p>Why this variable is considered global while it is defined inside of a function scope? Is this because the variable is <code>static</code>?</p>
</li>
</ol>

<bitcoin-core-development><source-code>
what is the mapBlocksUnknownParent variable?
Amir reza Riahi
# what is the mapBlocksUnknownParent variable?

(Asked by: username_0 on 2022-07-08)

I was reviewing this [pull request](https://github.com/bitcoin/bitcoin/pull/25571). It was an attempt to remove the global variable `mapBlocksUnknownParent`. I found the definition [here](https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L4263). In the `CChainState::LoadExternalBlockFile` function.

```
    // Map of disk positions for blocks with unknown parent (only used for reindex)
    static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent;

```

### Questions:

1. What is the `mapBlocksUnknownParent` variable? Where is it useful?
2. What does `LoadExternalBlockFile` function do? (by its name I deduce that it reads `blk???.dat` files and load them into the memory at the start up, but I'm not sure)
3. When it is possible to have blocks with unknown parents? While AFAIK we always reach blocks through their ancestors, not descendants. So in which situation we have a block with unknown parent?
4. Why this variable is considered global while it is defined inside of a function scope? Is this because the variable is `static`?

**Tags:** bitcoin-core-development, source-code


---

## 1 answers

### Answer by username_1 on 2022-07-08. Score: 2 | Accepted answer

> 1. What is the `mapBlocksUnknownParent` variable? Where is it useful?

It keeps a map of blocks in the blk\*.dat files whose parent is unknown, indexed by the block hash of that parent.

> 2. What does `LoadExternalBlockFile` function do? (by its name I deduce that it reads `blk???.dat` files and load them into the memory at the start up. But I'm not sure)

It deals with importing blocks from external files. This means not from the network, and not blocks already in our block database. It happens in two contexts:

* When a file is specified on the command line using `-loadblock=<FILE>`; this can be used to import blocks e.g. given to you on portable media (say, a USB stick).
* When running with `-reindex`, in which case the block database is wiped, but the blk\*.dat files are kept, and then this "importing" operation is run on these still existing blk\*.dat files.

> 3. When is it possible to have blocks with unknown parents? While AFAIK we always reach blocks through their ancestors, not descendants. So in which situation we have a block with unknown parent?

When requesting blocks from the network, we only ask for blocks whose parents are known (at least the headers must be known), so indeed, this variable isn't useful in that context. But the blocks are not downloaded in order, and the blk\*.dat files contain blocks in the order they were received.

This means that when using `-reindex` to rebuild the database, some blocks will be encountered out-of-order, meaning blocks will be read whose parent isn't known yet. To deal with this situation, `LoadExternalBlockFile` keeps track of these blocks without known parent. When the parent is encountered and processed, the children will be processed too.

> 4. Why this variable is considered global while it is defined inside of a function scope? Is this because the variable is `static`?

Exactly. `static` variables in function scopes are effectively globals that can only be accessed from that function. Only one instance of the variable exists, shared between all invocations of that function.


---

