refactor: Avoid copies in FlatSigningProvider Merge

This commit is contained in:
MacroFake 2022-07-30 14:27:47 +02:00
parent bf3f05f41d
commit fa3f15f2dd
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
6 changed files with 18 additions and 23 deletions

View File

@ -644,7 +644,7 @@ public:
assert(outscripts.size() == 1); assert(outscripts.size() == 1);
subscripts.emplace_back(std::move(outscripts[0])); subscripts.emplace_back(std::move(outscripts[0]));
} }
out = Merge(std::move(out), std::move(subprovider)); out.Merge(std::move(subprovider));
std::vector<CPubKey> pubkeys; std::vector<CPubKey> pubkeys;
pubkeys.reserve(entries.size()); pubkeys.reserve(entries.size());

View File

@ -77,20 +77,14 @@ bool FlatSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, Tapro
return LookupHelper(tr_trees, output_key, builder); return LookupHelper(tr_trees, output_key, builder);
} }
FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b) FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
{ {
FlatSigningProvider ret; scripts.merge(b.scripts);
ret.scripts = a.scripts; pubkeys.merge(b.pubkeys);
ret.scripts.insert(b.scripts.begin(), b.scripts.end()); keys.merge(b.keys);
ret.pubkeys = a.pubkeys; origins.merge(b.origins);
ret.pubkeys.insert(b.pubkeys.begin(), b.pubkeys.end()); tr_trees.merge(b.tr_trees);
ret.keys = a.keys; return *this;
ret.keys.insert(b.keys.begin(), b.keys.end());
ret.origins = a.origins;
ret.origins.insert(b.origins.begin(), b.origins.end());
ret.tr_trees = a.tr_trees;
ret.tr_trees.insert(b.tr_trees.begin(), b.tr_trees.end());
return ret;
} }
void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)

View File

@ -6,6 +6,7 @@
#ifndef BITCOIN_SCRIPT_SIGNINGPROVIDER_H #ifndef BITCOIN_SCRIPT_SIGNINGPROVIDER_H
#define BITCOIN_SCRIPT_SIGNINGPROVIDER_H #define BITCOIN_SCRIPT_SIGNINGPROVIDER_H
#include <attributes.h>
#include <key.h> #include <key.h>
#include <pubkey.h> #include <pubkey.h>
#include <script/keyorigin.h> #include <script/keyorigin.h>
@ -85,9 +86,9 @@ struct FlatSigningProvider final : public SigningProvider
bool GetKey(const CKeyID& keyid, CKey& key) const override; bool GetKey(const CKeyID& keyid, CKey& key) const override;
bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override; bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override; bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
};
FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b); FlatSigningProvider& Merge(FlatSigningProvider&& b) LIFETIMEBOUND;
};
/** Fillable signing provider that keeps keys in an address->secret map */ /** Fillable signing provider that keeps keys in an address->secret map */
class FillableSigningProvider : public SigningProvider class FillableSigningProvider : public SigningProvider

View File

@ -312,7 +312,7 @@ void DoCheck(const std::string& prv, const std::string& pub, const std::string&
txdata.Init(spend, std::move(utxos), /*force=*/true); txdata.Init(spend, std::move(utxos), /*force=*/true);
MutableTransactionSignatureCreator creator{spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT}; MutableTransactionSignatureCreator creator{spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT};
SignatureData sigdata; SignatureData sigdata;
BOOST_CHECK_MESSAGE(ProduceSignature(Merge(keys_priv, script_provider), creator, spks[n], sigdata), prv); BOOST_CHECK_MESSAGE(ProduceSignature(FlatSigningProvider{keys_priv}.Merge(FlatSigningProvider{script_provider}), creator, spks[n], sigdata), prv);
} }
/* Infer a descriptor from the generated script, and verify its solvability and that it roundtrips. */ /* Infer a descriptor from the generated script, and verify its solvability and that it roundtrips. */

View File

@ -644,7 +644,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out,
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor '%s': %s", desc_str, error)); throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor '%s': %s", desc_str, error));
} }
desc->Expand(0, desc_out, scripts_temp, desc_out); desc->Expand(0, desc_out, scripts_temp, desc_out);
coinControl.m_external_provider = Merge(coinControl.m_external_provider, desc_out); coinControl.m_external_provider.Merge(std::move(desc_out));
} }
} }
} }

View File

@ -2080,7 +2080,7 @@ std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvid
// Fetch SigningProvider from cache to avoid re-deriving // Fetch SigningProvider from cache to avoid re-deriving
auto it = m_map_signing_providers.find(index); auto it = m_map_signing_providers.find(index);
if (it != m_map_signing_providers.end()) { if (it != m_map_signing_providers.end()) {
*out_keys = Merge(*out_keys, it->second); out_keys->Merge(FlatSigningProvider{it->second});
} else { } else {
// Get the scripts, keys, and key origins for this script // Get the scripts, keys, and key origins for this script
std::vector<CScript> scripts_temp; std::vector<CScript> scripts_temp;
@ -2117,7 +2117,7 @@ bool DescriptorScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const s
if (!coin_keys) { if (!coin_keys) {
continue; continue;
} }
*keys = Merge(*keys, *coin_keys); keys->Merge(std::move(*coin_keys));
} }
return ::SignTransaction(tx, keys.get(), coins, sighash, input_errors); return ::SignTransaction(tx, keys.get(), coins, sighash, input_errors);
@ -2178,7 +2178,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>(); std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, sign); std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, sign);
if (script_keys) { if (script_keys) {
*keys = Merge(*keys, *script_keys); keys->Merge(std::move(*script_keys));
} else { } else {
// Maybe there are pubkeys listed that we can sign for // Maybe there are pubkeys listed that we can sign for
script_keys = std::make_unique<FlatSigningProvider>(); script_keys = std::make_unique<FlatSigningProvider>();
@ -2186,7 +2186,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
const CPubKey& pubkey = pk_pair.first; const CPubKey& pubkey = pk_pair.first;
std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey); std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
if (pk_keys) { if (pk_keys) {
*keys = Merge(*keys, *pk_keys); keys->Merge(std::move(*pk_keys));
} }
} }
for (const auto& pk_pair : input.m_tap_bip32_paths) { for (const auto& pk_pair : input.m_tap_bip32_paths) {
@ -2198,7 +2198,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
fullpubkey.Set(b, b + 33); fullpubkey.Set(b, b + 33);
std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(fullpubkey); std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(fullpubkey);
if (pk_keys) { if (pk_keys) {
*keys = Merge(*keys, *pk_keys); keys->Merge(std::move(*pk_keys));
} }
} }
} }