<p><code>ReadBestBlock</code> doesn't look at the <code>locator</code> it is given, rather it reads a specific record from the wallet database and loads the data into the provided <code>locator</code>. <code>locator</code> is used as an output parameter.</p>
<p><code>ReadBestBlock</code> returns true when it successfully finds a record and reads it, and it returns false if there is an error. So this code checks that the reading of the best block record was successful and that the read locator has data.</p>

CC BY-SA 4.0
2022-09-16T15:13:17.440Z
2022-09-16T15:13:17.440Z
Ava Chow
<p><a href="https://github.com/bitcoin/bitcoin/blob/968765973b5bfde1ee4ad2fb5c19e24bce63ad0e/src/wallet/wallet.cpp#L2925" rel="nofollow noreferrer">Here</a> in the function <code>AttachChain</code> there is block of code:</p>
<pre><code>    // Unless allowed, ensure wallet files are not reused across chains:
    if (!gArgs.GetBoolArg(&quot;-walletcrosschain&quot;, DEFAULT_WALLETCROSSCHAIN)) {
        WalletBatch batch(walletInstance-&gt;GetDatabase());
        CBlockLocator locator;
        if (batch.ReadBestBlock(locator) &amp;&amp; locator.vHave.size() &gt; 0 &amp;&amp; chain.getHeight()) {
            // Wallet is assumed to be from another chain, if genesis block in the active
            // chain differs from the genesis block known to the wallet.
            if (chain.getBlockHash(0) != locator.vHave.back()) {
                error = Untranslated(&quot;Wallet files should not be reused across chains. Restart bitcoind with -walletcrosschain to override.&quot;);
                return false;
            }
        }
    }
</code></pre>
<p>I've seen the <code>CBlockLocator</code> contructor and with no argument, it does nothing so the <code>vHave</code> member of <code>locator</code> is always empty. So next <code>if</code> statement will never execute because:</p>
<pre><code>batch.ReadBestBlock(locator) &amp;&amp; locator.vHave.size() &gt; 0 &amp;&amp; chain.getHeight()
</code></pre>
<p>Is alway <code>false</code>. I think I'm missing something here, because if I'm correct the if statement is like <code>if(0)</code> and so it's not useful.</p>

<bitcoin-core-development><c++>
How does AttachChain function works in the Bitcoin Core?
Amir reza Riahi
# How does AttachChain function works in the Bitcoin Core?

(Asked by: username_0 on 2022-09-16)

[Here](https://github.com/bitcoin/bitcoin/blob/968765973b5bfde1ee4ad2fb5c19e24bce63ad0e/src/wallet/wallet.cpp#L2925) in the function `AttachChain` there is block of code:

```
    // Unless allowed, ensure wallet files are not reused across chains:
    if (!gArgs.GetBoolArg("-walletcrosschain", DEFAULT_WALLETCROSSCHAIN)) {
        WalletBatch batch(walletInstance->GetDatabase());
        CBlockLocator locator;
        if (batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()) {
            // Wallet is assumed to be from another chain, if genesis block in the active
            // chain differs from the genesis block known to the wallet.
            if (chain.getBlockHash(0) != locator.vHave.back()) {
                error = Untranslated("Wallet files should not be reused across chains. Restart bitcoind with -walletcrosschain to override.");
                return false;
            }
        }
    }

```

I've seen the `CBlockLocator` contructor and with no argument, it does nothing so the `vHave` member of `locator` is always empty. So next `if` statement will never execute because:

```
batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()

```

Is alway `false`. I think I'm missing something here, because if I'm correct the if statement is like `if(0)` and so it's not useful.

**Tags:** bitcoin-core-development, c++


---

## 1 answers

### Answer by username_1 on 2022-09-16. Score: 2 | Accepted answer

`ReadBestBlock` doesn't look at the `locator` it is given, rather it reads a specific record from the wallet database and loads the data into the provided `locator`. `locator` is used as an output parameter.

`ReadBestBlock` returns true when it successfully finds a record and reads it, and it returns false if there is an error. So this code checks that the reading of the best block record was successful and that the read locator has data.


---

