Skip to content

Commit cb8844e

Browse files
committed
Merge bitcoin/bitcoin#28728: wallet: [bugfix] Mark CNoDestination and PubKeyDestination constructor explicit
1111475 bugfix: Mark CNoDestination and PubKeyDestination constructor explicit (MarcoFalke) fa5ccc4 iwyu: Export prevector.h from script.h (MarcoFalke) Pull request description: It seems confusing to allow any script, even one with a corresponding address, to silently convert to `CNoDestination`. Make the converstion `explicit` in the code, and fix any bugs that were previously introduced. In a follow-up, the class can be renamed, or the documentation can be updated to better reflect what the code does. ACKs for top commit: josibake: ACK bitcoin/bitcoin@1111475 achow101: ACK 1111475 furszy: Code review ACK 1111475 Tree-SHA512: d8b5f54d0cd8649a31e227ef164bb13e5b81ee9820f1976fd70c7a0de6841fba72d549c2f63e351c8cdda37dceb4763eca203e1c8ef385f46d9da6f1855c39ec
2 parents 2a349f9 + 1111475 commit cb8844e

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

src/addresstype.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
#ifndef BITCOIN_ADDRESSTYPE_H
66
#define BITCOIN_ADDRESSTYPE_H
77

8+
#include <attributes.h>
89
#include <pubkey.h>
910
#include <script/script.h>
1011
#include <uint256.h>
1112
#include <util/hash_type.h>
1213

13-
#include <variant>
1414
#include <algorithm>
15+
#include <variant>
16+
#include <vector>
1517

16-
class CNoDestination {
18+
class CNoDestination
19+
{
1720
private:
1821
CScript m_script;
1922

2023
public:
2124
CNoDestination() = default;
22-
CNoDestination(const CScript& script) : m_script(script) {}
25+
explicit CNoDestination(const CScript& script) : m_script(script) {}
2326

24-
const CScript& GetScript() const { return m_script; }
27+
const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
2528

2629
friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
2730
friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
@@ -32,7 +35,7 @@ struct PubKeyDestination {
3235
CPubKey m_pubkey;
3336

3437
public:
35-
PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
38+
explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
3639

3740
const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }
3841

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/script/script.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2021 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <script/script.h>
77

8+
#include <crypto/common.h>
89
#include <hash.h>
10+
#include <uint256.h>
11+
#include <util/hash_type.h>
912
#include <util/strencodings.h>
1013

1114
#include <string>

src/script/script.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,18 +8,19 @@
88

99
#include <attributes.h>
1010
#include <crypto/common.h>
11-
#include <prevector.h>
11+
#include <prevector.h> // IWYU pragma: export
1212
#include <serialize.h>
1313
#include <uint256.h>
1414
#include <util/hash_type.h>
1515

16-
#include <assert.h>
17-
#include <climits>
16+
#include <cassert>
17+
#include <cstdint>
18+
#include <cstring>
1819
#include <limits>
1920
#include <stdexcept>
20-
#include <stdint.h>
21-
#include <string.h>
2221
#include <string>
22+
#include <type_traits>
23+
#include <utility>
2324
#include <vector>
2425

2526
// Maximum number of bytes pushable to the stack

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)