Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <wallet/test/util.h>7#include <chain.h>8#include <key.h>9#include <key_io.h>10#include <test/util/setup_common.h>11#include <validationinterface.h>12#include <wallet/context.h>13#include <wallet/wallet.h>14#include <wallet/walletdb.h>16#include <sqlite3.h>18#include <memory>20namespace wallet {21std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)22{23auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());24{25LOCK2(wallet->cs_wallet, ::cs_main);26wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());27}28{29LOCK(wallet->cs_wallet);30wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);31wallet->SetupDescriptorScriptPubKeyMans();33FlatSigningProvider provider;34std::string error;35auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);36assert(descs.size() == 1);37auto& desc = descs.at(0);38WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);39Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false));40}41WalletRescanReserver reserver(*wallet);42reserver.reserve();43CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);44assert(result.status == CWallet::ScanResult::SUCCESS);45assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());46assert(*result.last_scanned_height == cchain.Height());47assert(result.last_failed_block.IsNull());48return wallet;49}51std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)52{53bilingual_str _error;54std::vector<bilingual_str> _warnings;55auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, /*born_encrypted=*/false, _error, _warnings);56NotifyWalletLoaded(context, wallet);57if (context.chain) {58wallet->postInitProcess();59}60return wallet;61}63std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context)64{65DatabaseOptions options;66options.require_create = true;67options.create_flags = WALLET_FLAG_DESCRIPTORS;68DatabaseStatus status;69bilingual_str error;70auto database = MakeWalletDatabase("", options, status, error);71return TestCreateWallet(std::move(database), context, options.create_flags);72}75std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context)76{77bilingual_str error;78std::vector<bilingual_str> warnings;79auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings);80NotifyWalletLoaded(context, wallet);81if (context.chain) {82wallet->postInitProcess();83}84return wallet;85}87std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)88{89DatabaseOptions options;90options.require_existing = true;91DatabaseStatus status;92bilingual_str error;93auto database = MakeWalletDatabase("", options, status, error);94return TestLoadWallet(std::move(database), context);95}97void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)98{99// Calls SyncWithValidationInterfaceQueue100wallet->chain().waitForNotificationsIfTipChanged({});101wallet->m_chain_notifications_handler.reset();102WaitForDeleteWallet(std::move(wallet));103}105std::string getnewaddress(CWallet& w)106{107constexpr auto output_type = OutputType::BECH32;108return EncodeDestination(getNewDestination(w, output_type));109}111CTxDestination getNewDestination(CWallet& w, OutputType output_type)112{113return *Assert(w.GetNewDestination(output_type, ""));114}116MockableSQLiteDatabase::MockableSQLiteDatabase()117: InMemoryWalletDatabase()118{}120std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase()121{122return std::make_unique<MockableSQLiteDatabase>();123}125wallet::DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)126{127keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);129FlatSigningProvider keys;130std::string error;131auto parsed_descs = Parse(desc_str, keys, error, false);132Assert(success == (!parsed_descs.empty()));133if (!success) return nullptr;134auto& desc = parsed_descs.at(0);136const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;138WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);140LOCK(keystore.cs_wallet);141auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));142return &spkm.value().get();143};144} // namespace wallet
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.