mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-06 21:57:54 +02:00
db2effaca4scripted-diff: refactor: CWallet::Create() -> CreateNew() (David Gumberg)27e021ebc0wallet: Correctly log stats for encrypted messages. (David Gumberg)d8bec61be2wallet: remove loading logic from CWallet::Create (David Gumberg)f35acc893frefactor: wallet: Factor out `WriteVersion()` from `PopulateWalletFromDB()` (David Gumberg)e12ff8aca0test: wallet: Split create and load (David Gumberg)70dbc79b09wallet: Use CWallet::LoadExisting() for loading existing wallets. (David Gumberg)ae66e01164wallet: Create separate function for wallet load (David Gumberg)bc69070416refactor: Wallet stats logging in its own function (David Gumberg)a9d64cd49cwallet: Remove redundant birth time update (David Gumberg)b4a49cc727wallet: Move argument parsing to before DB load (David Gumberg)b15a94a618refactor: Split out wallet argument loading (David Gumberg)a02c4a82d8refactor: Move -walletbroadcast setting init (David Gumberg)411caf7281wallet: refactor: PopulateWalletFromDB use switch statement. (David Gumberg)a48e23f566refactor: wallet: move error handling to PopulateWalletFromDB() (David Gumberg)0972785fd7wallet: Delete unnecessary PopulateWalletFromDB() calls (David Gumberg)f0a046094escripted-diff: refactor: CWallet::LoadWallet->PopulateWalletFromDB (David Gumberg) Pull request description: This PR is mostly a refactor which splits out logic used for creating wallets and for loading wallets, both of which are presently contained in `CWallet::Create()` into `CWallet::CreateNew()` and `CWallet::LoadExisting()` The real win of this PR is that `CWallet::Create()` uses a very bad heuristic for trying to guess whether or not it is supposed to be creating a new wallet or loading an existing wallet:370c592612/src/wallet/wallet.cpp (L2882-L2885)This heuristic assumes that wallets with no `ScriptPubKeyMans` are being created, which sounds reasonable, but as demonstrated in #32112 and #32111, this can happen when the user tries to load a wallet file that is corrupted, both issues are fixed by this PR and any other misbehavior for wallet files which succeeded the broken heuristic's sniff test for new wallets. It was already the case that every caller of `CWallet::Create()` knows whether it is creating a wallet or loading one, so we can avoid replacing this bad heuristic with another one, and just shift the burden to the caller. ACKs for top commit: achow101: ACKdb2effaca4polespinasa: approach ACKdb2effaca4w0xlt: reACKdb2effaca4murchandamus: ACKdb2effaca4rkrux: ACKdb2effaca4Tree-SHA512: c28d60e0a3001058da3fd2bdbe0726c7ebe742a4b900a1dee2e5132eccc22e49619cb747a99b4032b000eafd4aa2fdd4ec244c32be2012aba809fdc94b5f6ecd
126 lines
4.4 KiB
C++
126 lines
4.4 KiB
C++
// Copyright (c) 2021-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_WALLET_TEST_UTIL_H
|
|
#define BITCOIN_WALLET_TEST_UTIL_H
|
|
|
|
#include <addresstype.h>
|
|
#include <wallet/db.h>
|
|
#include <wallet/scriptpubkeyman.h>
|
|
|
|
#include <memory>
|
|
|
|
class ArgsManager;
|
|
class CChain;
|
|
class CKey;
|
|
enum class OutputType;
|
|
namespace interfaces {
|
|
class Chain;
|
|
} // namespace interfaces
|
|
|
|
namespace wallet {
|
|
class CWallet;
|
|
class WalletDatabase;
|
|
struct WalletContext;
|
|
|
|
static const DatabaseFormat DATABASE_FORMATS[] = {
|
|
DatabaseFormat::SQLITE,
|
|
};
|
|
|
|
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
|
|
|
|
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
|
|
|
|
std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
|
|
std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
|
|
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
|
|
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context);
|
|
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
|
|
|
|
// Creates a copy of the provided database
|
|
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
|
|
|
|
/** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
|
|
std::string getnewaddress(CWallet& w);
|
|
/** Returns a new destination, of an specific type, from the wallet */
|
|
CTxDestination getNewDestination(CWallet& w, OutputType output_type);
|
|
|
|
using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
|
|
|
|
class MockableCursor: public DatabaseCursor
|
|
{
|
|
public:
|
|
MockableData::const_iterator m_cursor;
|
|
MockableData::const_iterator m_cursor_end;
|
|
bool m_pass;
|
|
|
|
explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
|
|
MockableCursor(const MockableData& records, bool pass, std::span<const std::byte> prefix);
|
|
~MockableCursor() = default;
|
|
|
|
Status Next(DataStream& key, DataStream& value) override;
|
|
};
|
|
|
|
class MockableBatch : public DatabaseBatch
|
|
{
|
|
private:
|
|
MockableData& m_records;
|
|
bool m_pass;
|
|
|
|
bool ReadKey(DataStream&& key, DataStream& value) override;
|
|
bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
|
|
bool EraseKey(DataStream&& key) override;
|
|
bool HasKey(DataStream&& key) override;
|
|
bool ErasePrefix(std::span<const std::byte> prefix) override;
|
|
|
|
public:
|
|
explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
|
|
~MockableBatch() = default;
|
|
|
|
void Close() override {}
|
|
|
|
std::unique_ptr<DatabaseCursor> GetNewCursor() override
|
|
{
|
|
return std::make_unique<MockableCursor>(m_records, m_pass);
|
|
}
|
|
std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override {
|
|
return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
|
|
}
|
|
bool TxnBegin() override { return m_pass; }
|
|
bool TxnCommit() override { return m_pass; }
|
|
bool TxnAbort() override { return m_pass; }
|
|
bool HasActiveTxn() override { return false; }
|
|
};
|
|
|
|
/** A WalletDatabase whose contents and return values can be modified as needed for testing
|
|
**/
|
|
class MockableDatabase : public WalletDatabase
|
|
{
|
|
public:
|
|
MockableData m_records;
|
|
bool m_pass{true};
|
|
|
|
MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
|
|
~MockableDatabase() = default;
|
|
|
|
void Open() override {}
|
|
|
|
bool Rewrite() override { return m_pass; }
|
|
bool Backup(const std::string& strDest) const override { return m_pass; }
|
|
void Close() override {}
|
|
|
|
std::string Filename() override { return "mockable"; }
|
|
std::vector<fs::path> Files() override { return {}; }
|
|
std::string Format() override { return "mock"; }
|
|
std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableBatch>(m_records, m_pass); }
|
|
};
|
|
|
|
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
|
|
MockableDatabase& GetMockableDatabase(CWallet& wallet);
|
|
|
|
DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, bool success);
|
|
} // namespace wallet
|
|
|
|
#endif // BITCOIN_WALLET_TEST_UTIL_H
|