<p>Yes, <code>CreateDummyWalletDatabase</code> is only invoked from tests and benchmarks.</p>
<p>It's used because for tests we don't want an actual database file on disk, and even creating one is annoying (slows things down, more data to clean up afterwards, ...).</p>

CC BY-SA 4.0
2022-08-19T17:41:04.260Z
If it is just for tests and benchs, why put it in the source code? Couldn't it be in the tests files?
Amir reza Riahi
CC BY-SA 4.0
2022-08-19T17:35:32.693Z
2022-08-19T17:35:32.693Z
Pieter Wuille
<p><a href="https://github.com/bitcoin/bitcoin/blob/master/src/wallet/db.h#L160" rel="nofollow noreferrer">Here</a> and <a href="https://github.com/bitcoin/bitcoin/blob/master/src/wallet/db.h#L182" rel="nofollow noreferrer">here</a> are two classes called respectively <code>DummyBatch</code> and <code>DummyDatabase</code>:</p>
<pre><code>/** 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&amp; 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 &quot;dummy&quot;; }
    std::string Format() override { return &quot;dummy&quot;; }
    std::unique_ptr&lt;DatabaseBatch&gt; MakeBatch(bool flush_on_close = true) override { return std::make_unique&lt;DummyBatch&gt;(); }
};
</code></pre>
<pre><code>/** RAII class that provides access to a DummyDatabase. Never fails. */
class DummyBatch : public DatabaseBatch
{
private:
    bool ReadKey(CDataStream&amp;&amp; key, CDataStream&amp; value) override { return true; }
    bool WriteKey(CDataStream&amp;&amp; key, CDataStream&amp;&amp; value, bool overwrite=true) override { return true; }
    bool EraseKey(CDataStream&amp;&amp; key) override { return true; }
    bool HasKey(CDataStream&amp;&amp; key) override { return true; }

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

    bool StartCursor() override { return true; }
    bool ReadAtCursor(CDataStream&amp; ssKey, CDataStream&amp; ssValue, bool&amp; complete) override { return true; }
    void CloseCursor() override {}
    bool TxnBegin() override { return true; }
    bool TxnCommit() override { return true; }
    bool TxnAbort() override { return true; }
};
</code></pre>
<p>As shown in the code above, the <code>DummyBatch</code> is used in the <code>DummyDatabase</code>. The only usage I found for the <code>DummyDatabase</code> was <a href="https://github.com/bitcoin/bitcoin/blob/v23.0/src/wallet/walletdb.cpp#L1184" rel="nofollow noreferrer">here</a>:</p>
<pre><code>/** Return object for accessing dummy database with no read/write capabilities. */
std::unique_ptr&lt;WalletDatabase&gt; CreateDummyWalletDatabase()
{
    return std::make_unique&lt;DummyDatabase&gt;();
}
</code></pre>
<p>I can't understand what is the usage of such class which absolutely does nothing. Is it for testing or something else?</p>

<bitcoin-core-development>
What are dummy batch and dummy database classes in the Bitcoin Core?
Amir reza Riahi
# What are dummy batch and dummy database classes in the Bitcoin Core?

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

Here and here are two classes called respectively DummyBatch and DummyDatabase:
/** 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 MakeBatch(bool flush_on_close = true) override { return std::make_unique(); }
};

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

As shown in the code above, the DummyBatch is used in the DummyDatabase. The only usage I found for the DummyDatabase was here:
/** Return object for accessing dummy database with no read/write capabilities. */
std::unique_ptr CreateDummyWalletDatabase()
{
    return std::make_unique();
}

I can't understand what is the usage of such class which absolutely does nothing. Is it for testing or something else?

**Tags:** bitcoin-core-development


---

## 1 answers

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

Yes, `CreateDummyWalletDatabase` is only invoked from tests and benchmarks.

It's used because for tests we don't want an actual database file on disk, and even creating one is annoying (slows things down, more data to clean up afterwards, ...).

> **Comments:**
> **username_0**: If it is just for tests and benchs, why put it in the source code? Couldn't it be in the tests files?  

---

