From fafee85358397289aa4c6b799d2603a5d89e83a2 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 15 May 2025 14:32:58 +0200 Subject: [PATCH] remove unused GetDestinationForKey It is only used in test. There it is problematic, because it sometimes relies on m_default_address_type. If the default were changed to BECH32M, those tests would fail the assert(false). So just use PKHash{} in all tests and remove GetDestinationForKey. --- src/bench/wallet_migration.cpp | 2 +- src/outputtype.cpp | 21 --------------------- src/outputtype.h | 6 ------ src/qt/test/addressbooktests.cpp | 8 +++----- src/qt/test/wallettests.cpp | 4 ++-- src/test/fuzz/key.cpp | 2 +- 6 files changed, 7 insertions(+), 36 deletions(-) diff --git a/src/bench/wallet_migration.cpp b/src/bench/wallet_migration.cpp index b5e512f3693..49591f5fc25 100644 --- a/src/bench/wallet_migration.cpp +++ b/src/bench/wallet_migration.cpp @@ -37,7 +37,7 @@ static void WalletMigration(benchmark::Bench& bench) for (int w = 0; w < NUM_WATCH_ONLY_ADDR; ++w) { CKey key = GenerateRandomKey(); LOCK(wallet->cs_wallet); - const auto& dest = GetDestinationForKey(key.GetPubKey(), OutputType::LEGACY); + const PKHash dest{key.GetPubKey()}; const CScript& script = scripts_watch_only.emplace_back(GetScriptForDestination(dest)); assert(legacy_spkm->LoadWatchOnly(script)); assert(wallet->SetAddressBook(dest, strprintf("watch_%d", w), /*purpose=*/std::nullopt)); diff --git a/src/outputtype.cpp b/src/outputtype.cpp index 702efe81c63..b4acc56c265 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -46,27 +46,6 @@ const std::string& FormatOutputType(OutputType type) assert(false); } -CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type) -{ - switch (type) { - case OutputType::LEGACY: return PKHash(key); - case OutputType::P2SH_SEGWIT: - case OutputType::BECH32: { - if (!key.IsCompressed()) return PKHash(key); - CTxDestination witdest = WitnessV0KeyHash(key); - CScript witprog = GetScriptForDestination(witdest); - if (type == OutputType::P2SH_SEGWIT) { - return ScriptHash(witprog); - } else { - return witdest; - } - } - case OutputType::BECH32M: - case OutputType::UNKNOWN: {} // This function should never be used with BECH32M or UNKNOWN, so let it assert - } // no default case, so the compiler can warn about missing cases - assert(false); -} - CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type) { // Add script to keystore diff --git a/src/outputtype.h b/src/outputtype.h index 69dfdd67de1..7e625b0e9da 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -32,12 +32,6 @@ static constexpr auto OUTPUT_TYPES = std::array{ std::optional ParseOutputType(const std::string& str); const std::string& FormatOutputType(OutputType type); -/** - * Get a destination of the requested type (if possible) to the specified key. - * The caller must make sure LearnRelatedScripts has been called beforehand. - */ -CTxDestination GetDestinationForKey(const CPubKey& key, OutputType); - /** * Get a destination of the requested type (if possible) to the specified script. * This function will automatically add the script (and any other diff --git a/src/qt/test/addressbooktests.cpp b/src/qt/test/addressbooktests.cpp index 3d5cb4a863a..507d26a44c7 100644 --- a/src/qt/test/addressbooktests.cpp +++ b/src/qt/test/addressbooktests.cpp @@ -84,13 +84,11 @@ void TestAddAddressesToSendBook(interfaces::Node& node) wallet->SetupDescriptorScriptPubKeyMans(); } - auto build_address = [&wallet]() { + auto build_address{[]() { CKey key = GenerateRandomKey(); - CTxDestination dest(GetDestinationForKey( - key.GetPubKey(), wallet->m_default_address_type)); - + const PKHash dest{key.GetPubKey()}; return std::make_pair(dest, QString::fromStdString(EncodeDestination(dest))); - }; + }}; CTxDestination r_key_dest, s_key_dest; diff --git a/src/qt/test/wallettests.cpp b/src/qt/test/wallettests.cpp index 4cbb8f35034..7985b18b156 100644 --- a/src/qt/test/wallettests.cpp +++ b/src/qt/test/wallettests.cpp @@ -217,7 +217,7 @@ std::shared_ptr SetupDescriptorsWallet(interfaces::Node& node, TestChai WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1); auto spk_manager = *Assert(wallet->AddWalletDescriptor(w_desc, provider, "", false)); assert(spk_manager); - CTxDestination dest = GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type); + const PKHash dest{test.coinbaseKey.GetPubKey()}; wallet->SetAddressBook(dest, "", wallet::AddressPurpose::RECEIVE); wallet->SetLastBlockProcessed(105, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash())); SyncUpWallet(wallet, node); @@ -406,7 +406,7 @@ void TestGUIWatchOnly(interfaces::Node& node, TestChain100Setup& test) sendCoinsDialog.findChild("labelBalance")); // Set change address - sendCoinsDialog.getCoinControl()->destChange = GetDestinationForKey(test.coinbaseKey.GetPubKey(), OutputType::LEGACY); + sendCoinsDialog.getCoinControl()->destChange = PKHash{test.coinbaseKey.GetPubKey()}; // Time to reject "save" PSBT dialog ('SendCoins' locks the main thread until the dialog receives the event). QTimer timer; diff --git a/src/test/fuzz/key.cpp b/src/test/fuzz/key.cpp index 046a666ee30..d851e33adc5 100644 --- a/src/test/fuzz/key.cpp +++ b/src/test/fuzz/key.cpp @@ -174,7 +174,7 @@ FUZZ_TARGET(key, .init = initialize_key) assert(v_solutions_ret_tx_multisig[2].size() == 1); OutputType output_type{}; - const CTxDestination tx_destination = GetDestinationForKey(pubkey, output_type); + const CTxDestination tx_destination{PKHash{pubkey}}; assert(output_type == OutputType::LEGACY); assert(IsValidDestination(tx_destination)); assert(PKHash{pubkey} == *std::get_if(&tx_destination));