Skip to content

Commit 1111475

Browse files
author
MarcoFalke
committed
bugfix: Mark CNoDestination and PubKeyDestination constructor explicit
This should fix the bug reported in bitcoin/bitcoin#28246 (comment), which caused the GUI to not detect the destination type of recipients, thus picking the wrong change destination type. Also, add missing lifetimebound attribute to a getter method.
1 parent fa5ccc4 commit 1111475

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

src/addresstype.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
#include <variant>
1616
#include <vector>
1717

18-
class CNoDestination {
18+
class CNoDestination
19+
{
1920
private:
2021
CScript m_script;
2122

2223
public:
2324
CNoDestination() = default;
24-
CNoDestination(const CScript& script) : m_script(script) {}
25+
explicit CNoDestination(const CScript& script) : m_script(script) {}
2526

26-
const CScript& GetScript() const { return m_script; }
27+
const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
2728

2829
friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
2930
friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
@@ -34,7 +35,7 @@ struct PubKeyDestination {
3435
CPubKey m_pubkey;
3536

3637
public:
37-
PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
38+
explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
3839

3940
const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
4041

src/bench/wallet_create_tx.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type
9494
}
9595

9696
// Generate destinations
97-
CScript dest = GetScriptForDestination(getNewDestination(wallet, output_type));
97+
const auto dest{getNewDestination(wallet, output_type)};
9898

9999
// Generate chain; each coinbase will have two outputs to fill-up the wallet
100100
const auto& params = Params();
101+
const CScript coinbase_out{GetScriptForDestination(dest)};
101102
unsigned int chain_size = 5000; // 5k blocks means 10k UTXO for the wallet (minus 200 due COINBASE_MATURITY)
102103
for (unsigned int i = 0; i < chain_size; ++i) {
103-
generateFakeBlock(params, test_setup->m_node, wallet, dest);
104+
generateFakeBlock(params, test_setup->m_node, wallet, coinbase_out);
104105
}
105106

106107
// Check available balance
@@ -185,4 +186,4 @@ static void WalletAvailableCoins(benchmark::Bench& bench) { AvailableCoins(bench
185186

186187
BENCHMARK(WalletCreateTxUseOnlyPresetInputs, benchmark::PriorityLevel::LOW)
187188
BENCHMARK(WalletCreateTxUsePresetInputsAndCoinSelection, benchmark::PriorityLevel::LOW)
188-
BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW);
189+
BENCHMARK(WalletAvailableCoins, benchmark::PriorityLevel::LOW);

src/qt/walletmodel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
187187
setAddress.insert(rcp.address);
188188
++nAddresses;
189189

190-
CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString()));
191-
CRecipient recipient = {scriptPubKey, rcp.amount, rcp.fSubtractFeeFromAmount};
190+
CRecipient recipient{DecodeDestination(rcp.address.toStdString()), rcp.amount, rcp.fSubtractFeeFromAmount};
192191
vecSend.push_back(recipient);
193192

194193
total += rcp.amount;

src/wallet/test/spend_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_duplicated_preset_inputs_test, TestChain100Setup)
7878

7979
// Try to create a tx that spends more than what preset inputs + wallet selected inputs are covering for.
8080
// The wallet can cover up to 200 BTC, and the tx target is 299 BTC.
81-
std::vector<CRecipient> recipients = {{GetScriptForDestination(*Assert(wallet->GetNewDestination(OutputType::BECH32, "dummy"))),
81+
std::vector<CRecipient> recipients{{*Assert(wallet->GetNewDestination(OutputType::BECH32, "dummy")),
8282
/*nAmount=*/299 * COIN, /*fSubtractFeeFromAmount=*/true}};
8383
CCoinControl coin_control;
8484
coin_control.m_allow_other_inputs = true;

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
605605
// returns the coin associated with the change address underneath the
606606
// coinbaseKey pubkey, even though the change address has a different
607607
// pubkey.
608-
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, /*subtract_fee=*/false});
608+
AddTx(CRecipient{PubKeyDestination{{}}, 1 * COIN, /*subtract_fee=*/false});
609609
{
610610
LOCK(wallet->cs_wallet);
611611
list = ListCoins(*wallet);

0 commit comments

Comments
 (0)