mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-06 04:59:20 +01: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
241 lines
7.4 KiB
C++
241 lines
7.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.
|
|
|
|
#include <wallet/test/util.h>
|
|
|
|
#include <chain.h>
|
|
#include <key.h>
|
|
#include <key_io.h>
|
|
#include <streams.h>
|
|
#include <test/util/setup_common.h>
|
|
#include <validationinterface.h>
|
|
#include <wallet/context.h>
|
|
#include <wallet/wallet.h>
|
|
#include <wallet/walletdb.h>
|
|
|
|
#include <memory>
|
|
|
|
namespace wallet {
|
|
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
|
|
{
|
|
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
|
|
{
|
|
LOCK2(wallet->cs_wallet, ::cs_main);
|
|
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
|
|
}
|
|
{
|
|
LOCK(wallet->cs_wallet);
|
|
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
|
|
wallet->SetupDescriptorScriptPubKeyMans();
|
|
|
|
FlatSigningProvider provider;
|
|
std::string error;
|
|
auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
|
|
assert(descs.size() == 1);
|
|
auto& desc = descs.at(0);
|
|
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
|
|
Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false));
|
|
}
|
|
WalletRescanReserver reserver(*wallet);
|
|
reserver.reserve();
|
|
CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
|
|
assert(result.status == CWallet::ScanResult::SUCCESS);
|
|
assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
|
|
assert(*result.last_scanned_height == cchain.Height());
|
|
assert(result.last_failed_block.IsNull());
|
|
return wallet;
|
|
}
|
|
|
|
std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
|
|
{
|
|
bilingual_str _error;
|
|
std::vector<bilingual_str> _warnings;
|
|
auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, _error, _warnings);
|
|
NotifyWalletLoaded(context, wallet);
|
|
if (context.chain) {
|
|
wallet->postInitProcess();
|
|
}
|
|
return wallet;
|
|
}
|
|
|
|
std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context)
|
|
{
|
|
DatabaseOptions options;
|
|
options.require_create = true;
|
|
options.create_flags = WALLET_FLAG_DESCRIPTORS;
|
|
DatabaseStatus status;
|
|
bilingual_str error;
|
|
std::vector<bilingual_str> warnings;
|
|
auto database = MakeWalletDatabase("", options, status, error);
|
|
return TestCreateWallet(std::move(database), context, options.create_flags);
|
|
}
|
|
|
|
|
|
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context)
|
|
{
|
|
bilingual_str error;
|
|
std::vector<bilingual_str> warnings;
|
|
auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings);
|
|
NotifyWalletLoaded(context, wallet);
|
|
if (context.chain) {
|
|
wallet->postInitProcess();
|
|
}
|
|
return wallet;
|
|
}
|
|
|
|
std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
|
|
{
|
|
DatabaseOptions options;
|
|
options.require_existing = true;
|
|
DatabaseStatus status;
|
|
bilingual_str error;
|
|
std::vector<bilingual_str> warnings;
|
|
auto database = MakeWalletDatabase("", options, status, error);
|
|
return TestLoadWallet(std::move(database), context);
|
|
}
|
|
|
|
void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
|
|
{
|
|
// Calls SyncWithValidationInterfaceQueue
|
|
wallet->chain().waitForNotificationsIfTipChanged({});
|
|
wallet->m_chain_notifications_handler.reset();
|
|
WaitForDeleteWallet(std::move(wallet));
|
|
}
|
|
|
|
std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database)
|
|
{
|
|
return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records);
|
|
}
|
|
|
|
std::string getnewaddress(CWallet& w)
|
|
{
|
|
constexpr auto output_type = OutputType::BECH32;
|
|
return EncodeDestination(getNewDestination(w, output_type));
|
|
}
|
|
|
|
CTxDestination getNewDestination(CWallet& w, OutputType output_type)
|
|
{
|
|
return *Assert(w.GetNewDestination(output_type, ""));
|
|
}
|
|
|
|
MockableCursor::MockableCursor(const MockableData& records, bool pass, std::span<const std::byte> prefix)
|
|
{
|
|
m_pass = pass;
|
|
std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix});
|
|
}
|
|
|
|
DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
|
|
{
|
|
if (!m_pass) {
|
|
return Status::FAIL;
|
|
}
|
|
if (m_cursor == m_cursor_end) {
|
|
return Status::DONE;
|
|
}
|
|
key.clear();
|
|
value.clear();
|
|
const auto& [key_data, value_data] = *m_cursor;
|
|
key.write(key_data);
|
|
value.write(value_data);
|
|
m_cursor++;
|
|
return Status::MORE;
|
|
}
|
|
|
|
bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
const auto& it = m_records.find(key_data);
|
|
if (it == m_records.end()) {
|
|
return false;
|
|
}
|
|
value.clear();
|
|
value.write(it->second);
|
|
return true;
|
|
}
|
|
|
|
bool MockableBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
SerializeData value_data{value.begin(), value.end()};
|
|
auto [it, inserted] = m_records.emplace(key_data, value_data);
|
|
if (!inserted && overwrite) { // Overwrite if requested
|
|
it->second = value_data;
|
|
inserted = true;
|
|
}
|
|
return inserted;
|
|
}
|
|
|
|
bool MockableBatch::EraseKey(DataStream&& key)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
m_records.erase(key_data);
|
|
return true;
|
|
}
|
|
|
|
bool MockableBatch::HasKey(DataStream&& key)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
SerializeData key_data{key.begin(), key.end()};
|
|
return m_records.contains(key_data);
|
|
}
|
|
|
|
bool MockableBatch::ErasePrefix(std::span<const std::byte> prefix)
|
|
{
|
|
if (!m_pass) {
|
|
return false;
|
|
}
|
|
auto it = m_records.begin();
|
|
while (it != m_records.end()) {
|
|
auto& key = it->first;
|
|
if (key.size() < prefix.size() || std::search(key.begin(), key.end(), prefix.begin(), prefix.end()) != key.begin()) {
|
|
it++;
|
|
continue;
|
|
}
|
|
it = m_records.erase(it);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records)
|
|
{
|
|
return std::make_unique<MockableDatabase>(records);
|
|
}
|
|
|
|
MockableDatabase& GetMockableDatabase(CWallet& wallet)
|
|
{
|
|
return dynamic_cast<MockableDatabase&>(wallet.GetDatabase());
|
|
}
|
|
|
|
wallet::DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
|
|
{
|
|
keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
|
|
|
|
FlatSigningProvider keys;
|
|
std::string error;
|
|
auto parsed_descs = Parse(desc_str, keys, error, false);
|
|
Assert(success == (!parsed_descs.empty()));
|
|
if (!success) return nullptr;
|
|
auto& desc = parsed_descs.at(0);
|
|
|
|
const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
|
|
|
|
WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
|
|
|
|
LOCK(keystore.cs_wallet);
|
|
auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
|
|
return &spkm.value().get();
|
|
};
|
|
} // namespace wallet
|