<p>That would not guarantee that the currently inspected block has a successor. While eventually the network converges on a single best chain, the network does temporarily fork occasionally, when two miners find a block at the same height. If a node were on the chaintip that is being reorged out there could be a block with a greater height, while it is not the successor of the node's current best block. Insofar, we must handle blocks not having successors anyway, so I assume that the <code>nullptr</code> is handled benignly somewhere in the surrounding code (I haven't checked, though).</p>

CC BY-SA 4.0
2022-07-07T18:15:51.793Z
Yeah, you're right. I checked it here, it handled in the `[]` operator. Thanks for your answer. https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L453
Amir reza Riahi
CC BY-SA 4.0
2022-07-07T18:02:01.593Z
2022-07-07T18:02:01.593Z
Murch
<p>I forgot to check the <code>[]</code> operator of the <code>CChain</code> class. The implementation is:</p>
<pre><code>    /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
    CBlockIndex* operator[](int nHeight) const
    {
        if (nHeight &lt; 0 || nHeight &gt;= (int)vChain.size())
            return nullptr;
        return vChain[nHeight];
    }
</code></pre>
<p>So by calling the <code>(*this)[pindex-&gt;nHeight + 1]</code> the operator that is overloaded is called and if the <code>nHeight</code> is greater that the <code>vChain.size()</code> it returns the <code>nullptr</code>.</p>

CC BY-SA 4.0
2022-07-07T18:19:48.040Z
2022-07-07T18:19:48.040Z
Amir reza Riahi
<p>This function <a href="https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L467" rel="nofollow noreferrer">here</a> gives a pointer to a block, and return the succesor block of that pointer.</p>
<pre><code>    /** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
    CBlockIndex* Next(const CBlockIndex* pindex) const
    {
        if (Contains(pindex))
            return (*this)[pindex-&gt;nHeight + 1];
        else
            return nullptr;
    }
</code></pre>
<p>If the given block is containing in the chain, it attempt to return the successor. So if the <code>Chain.Tip()</code> is given, it tries to reach the successor while there is no successor of the tip block. Isn't it better to check if <code>nHeight</code> of the given block is less than the <code>Chain.Tip().nHeight - 1</code> or not?</p>

<bitcoin-core-development>
Shouldn't CChain::Next check if the given block is the tip block or not?
Amir reza Riahi
# Shouldn't CChain::Next check if the given block is the tip block or not?

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

This function [here](https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L467) gives a pointer to a block, and return the succesor block of that pointer.

```
    /** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
    CBlockIndex* Next(const CBlockIndex* pindex) const
    {
        if (Contains(pindex))
            return (*this)[pindex->nHeight + 1];
        else
            return nullptr;
    }

```

If the given block is containing in the chain, it attempt to return the successor. So if the `Chain.Tip()` is given, it tries to reach the successor while there is no successor of the tip block. Isn't it better to check if `nHeight` of the given block is less than the `Chain.Tip().nHeight - 1` or not?

**Tags:** bitcoin-core-development


---

## 2 answers

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

That would not guarantee that the currently inspected block has a successor. While eventually the network converges on a single best chain, the network does temporarily fork occasionally, when two miners find a block at the same height. If a node were on the chaintip that is being reorged out there could be a block with a greater height, while it is not the successor of the node's current best block. Insofar, we must handle blocks not having successors anyway, so I assume that the `nullptr` is handled benignly somewhere in the surrounding code (I haven't checked, though).

> **Comments:**
> **username_0**: Yeah, you're right. I checked it here, it handled in the `[]` operator. Thanks for your answer. https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L453  

---

### Answer by username_0 on 2022-07-07. Score: 0

I forgot to check the `[]` operator of the `CChain` class. The implementation is:

```
    /** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
    CBlockIndex* operator[](int nHeight) const
    {
        if (nHeight < 0 || nHeight >= (int)vChain.size())
            return nullptr;
        return vChain[nHeight];
    }

```

So by calling the `(*this)[pindex->nHeight + 1]` the operator that is overloaded is called and if the `nHeight` is greater that the `vChain.size()` it returns the `nullptr`.


---

