Bitcoin-Core-Development

AttachChain 函式在比特幣核心中是如何工作的?

  • September 16, 2022

在函式AttachChain有一段程式碼:

   // 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;
           }
       }
   }

我見過CBlockLocator建構子,沒有參數,它什麼都不做,所以vHave成員locator總是空的。所以下if一條語句永遠不會執行,因為:

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

總是false。我想我在這裡遺漏了一些東西,因為如果我是正確的,if 語句就像這樣if(0),所以它沒有用。

ReadBestBlock不看locator給出的,而是從錢包數據庫中讀取特定記錄並將數據載入到提供的locator. locator用作輸出參數。

ReadBestBlock當它成功找到一條記錄並讀取它時返回true,如果有錯誤則返回false。所以這段程式碼檢查最佳塊記錄的讀取是否成功,以及讀取的定位器是否有數據。

引用自:https://bitcoin.stackexchange.com/questions/115249