Bitcoin-Core-Development

比特幣核心中的虛擬批處理和虛擬數據庫類是什麼?

  • August 19, 2022

這里這裡是兩個類,分別稱為DummyBatchDummyDatabase

/** A dummy WalletDatabase that does nothing and never fails. Only used by unit tests.
**/
class DummyDatabase : public WalletDatabase
{
public:
   void Open() override {};
   void AddRef() override {}
   void RemoveRef() override {}
   bool Rewrite(const char* pszSkip=nullptr) override { return true; }
   bool Backup(const std::string& strDest) const override { return true; }
   void Close() override {}
   void Flush() override {}
   bool PeriodicFlush() override { return true; }
   void IncrementUpdateCounter() override { ++nUpdateCounter; }
   void ReloadDbEnv() override {}
   std::string Filename() override { return "dummy"; }
   std::string Format() override { return "dummy"; }
   std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<DummyBatch>(); }
};
/** RAII class that provides access to a DummyDatabase. Never fails. */
class DummyBatch : public DatabaseBatch
{
private:
   bool ReadKey(CDataStream&& key, CDataStream& value) override { return true; }
   bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) override { return true; }
   bool EraseKey(CDataStream&& key) override { return true; }
   bool HasKey(CDataStream&& key) override { return true; }

public:
   void Flush() override {}
   void Close() override {}

   bool StartCursor() override { return true; }
   bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
   void CloseCursor() override {}
   bool TxnBegin() override { return true; }
   bool TxnCommit() override { return true; }
   bool TxnAbort() override { return true; }
};

如上面的程式碼所示,DummyBatchDummyDatabase. 我發現的唯一用法DummyDatabase在這裡

/** Return object for accessing dummy database with no read/write capabilities. */
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
{
   return std::make_unique<DummyDatabase>();
}

我不明白這種絕對什麼都不做的類的用途是什麼。是為了測試還是其他?

是的,CreateDummyWalletDatabase僅從測試和基準中呼叫。

之所以使用它,是因為對於測試,我們不希望磁碟上有一個實際的數據庫文件,甚至創建一個也很煩人(減慢速度,事後清理更多數據,…)。

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