Skip to content

Commit a8f6954

Browse files
committed
Merge bitcoin/bitcoin#25748: refactor: Avoid copies in FlatSigningProvider Merge
fa3f15f refactor: Avoid copies in FlatSigningProvider Merge (MacroFake) Pull request description: `Merge` will create several copies unconditionally: * To initialize the args `a`, and `b` * `ret`, which is the merge of the two args So change the code to let the caller decide how many copies they need/want: * `a`, and `b` must be explicitly moved or copied by the caller * `ret` is no longer needed, as `a` can be used for it in place "for free" ACKs for top commit: achow101: ACK fa3f15f furszy: looks good, ACK fa3f15f ryanofsky: Code review ACK fa3f15f. Confirmed that all the places `std::move` was added the argument actually did seem safe to move from. Compiler enforces that temporary copies are explicitly created in non-move cases. Tree-SHA512: 7c027ccdea1549cd9f37403344ecbb76e008adf545f6ce52996bf95e89eb7dc89af6cb31435a9289d6f2eea1c416961b2fb96348bc8a211d550728f1d99ac49c
2 parents a75b779 + fa3f15f commit a8f6954

File tree

6 files changed

+19
-24
lines changed

6 files changed

+19
-24
lines changed

src/script/descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ class DescriptorImpl : public Descriptor
644644
assert(outscripts.size() == 1);
645645
subscripts.emplace_back(std::move(outscripts[0]));
646646
}
647-
out = Merge(std::move(out), std::move(subprovider));
647+
out.Merge(std::move(subprovider));
648648

649649
std::vector<CPubKey> pubkeys;
650650
pubkeys.reserve(entries.size());

src/script/signingprovider.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,14 @@ bool FlatSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, Tapro
7777
return LookupHelper(tr_trees, output_key, builder);
7878
}
7979

80-
FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b)
81-
{
82-
FlatSigningProvider ret;
83-
ret.scripts = a.scripts;
84-
ret.scripts.insert(b.scripts.begin(), b.scripts.end());
85-
ret.pubkeys = a.pubkeys;
86-
ret.pubkeys.insert(b.pubkeys.begin(), b.pubkeys.end());
87-
ret.keys = a.keys;
88-
ret.keys.insert(b.keys.begin(), b.keys.end());
89-
ret.origins = a.origins;
90-
ret.origins.insert(b.origins.begin(), b.origins.end());
91-
ret.tr_trees = a.tr_trees;
92-
ret.tr_trees.insert(b.tr_trees.begin(), b.tr_trees.end());
93-
return ret;
80+
FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
81+
{
82+
scripts.merge(b.scripts);
83+
pubkeys.merge(b.pubkeys);
84+
keys.merge(b.keys);
85+
origins.merge(b.origins);
86+
tr_trees.merge(b.tr_trees);
87+
return *this;
9488
}
9589

9690
void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)

src/script/signingprovider.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_SCRIPT_SIGNINGPROVIDER_H
77
#define BITCOIN_SCRIPT_SIGNINGPROVIDER_H
88

9+
#include <attributes.h>
910
#include <key.h>
1011
#include <pubkey.h>
1112
#include <script/keyorigin.h>
@@ -85,9 +86,9 @@ struct FlatSigningProvider final : public SigningProvider
8586
bool GetKey(const CKeyID& keyid, CKey& key) const override;
8687
bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
8788
bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
88-
};
8989

90-
FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b);
90+
FlatSigningProvider& Merge(FlatSigningProvider&& b) LIFETIMEBOUND;
91+
};
9192

9293
/** Fillable signing provider that keeps keys in an address->secret map */
9394
class FillableSigningProvider : public SigningProvider

src/test/descriptor_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void DoCheck(const std::string& prv, const std::string& pub, const std::string&
312312
txdata.Init(spend, std::move(utxos), /*force=*/true);
313313
MutableTransactionSignatureCreator creator{spend, 0, CAmount{0}, &txdata, SIGHASH_DEFAULT};
314314
SignatureData sigdata;
315-
BOOST_CHECK_MESSAGE(ProduceSignature(Merge(keys_priv, script_provider), creator, spks[n], sigdata), prv);
315+
BOOST_CHECK_MESSAGE(ProduceSignature(FlatSigningProvider{keys_priv}.Merge(FlatSigningProvider{script_provider}), creator, spks[n], sigdata), prv);
316316
}
317317

318318
/* Infer a descriptor from the generated script, and verify its solvability and that it roundtrips. */

src/wallet/rpc/spend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void FundTransaction(CWallet& wallet, CMutableTransaction& tx, CAmount& fee_out,
644644
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor '%s': %s", desc_str, error));
645645
}
646646
desc->Expand(0, desc_out, scripts_temp, desc_out);
647-
coinControl.m_external_provider = Merge(coinControl.m_external_provider, desc_out);
647+
coinControl.m_external_provider.Merge(std::move(desc_out));
648648
}
649649
}
650650
}

src/wallet/scriptpubkeyman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,7 @@ std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvid
20852085
// Fetch SigningProvider from cache to avoid re-deriving
20862086
auto it = m_map_signing_providers.find(index);
20872087
if (it != m_map_signing_providers.end()) {
2088-
*out_keys = Merge(*out_keys, it->second);
2088+
out_keys->Merge(FlatSigningProvider{it->second});
20892089
} else {
20902090
// Get the scripts, keys, and key origins for this script
20912091
std::vector<CScript> scripts_temp;
@@ -2122,7 +2122,7 @@ bool DescriptorScriptPubKeyMan::SignTransaction(CMutableTransaction& tx, const s
21222122
if (!coin_keys) {
21232123
continue;
21242124
}
2125-
*keys = Merge(*keys, *coin_keys);
2125+
keys->Merge(std::move(*coin_keys));
21262126
}
21272127

21282128
return ::SignTransaction(tx, keys.get(), coins, sighash, input_errors);
@@ -2183,15 +2183,15 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
21832183
std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
21842184
std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, sign);
21852185
if (script_keys) {
2186-
*keys = Merge(*keys, *script_keys);
2186+
keys->Merge(std::move(*script_keys));
21872187
} else {
21882188
// Maybe there are pubkeys listed that we can sign for
21892189
script_keys = std::make_unique<FlatSigningProvider>();
21902190
for (const auto& pk_pair : input.hd_keypaths) {
21912191
const CPubKey& pubkey = pk_pair.first;
21922192
std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
21932193
if (pk_keys) {
2194-
*keys = Merge(*keys, *pk_keys);
2194+
keys->Merge(std::move(*pk_keys));
21952195
}
21962196
}
21972197
for (const auto& pk_pair : input.m_tap_bip32_paths) {
@@ -2203,7 +2203,7 @@ TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction&
22032203
fullpubkey.Set(b, b + 33);
22042204
std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(fullpubkey);
22052205
if (pk_keys) {
2206-
*keys = Merge(*keys, *pk_keys);
2206+
keys->Merge(std::move(*pk_keys));
22072207
}
22082208
}
22092209
}

0 commit comments

Comments
 (0)