bugfix: Mark CNoDestination and PubKeyDestination constructor explicit

This should fix the bug reported in
https://github.com/bitcoin/bitcoin/pull/28246#discussion_r1371640502,
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.
This commit is contained in:
MarcoFalke 2023-10-25 12:13:44 +02:00
parent fa5ccc4137
commit 1111475b41
No known key found for this signature in database
5 changed files with 12 additions and 11 deletions

View File

@ -15,15 +15,16 @@
#include <variant> #include <variant>
#include <vector> #include <vector>
class CNoDestination { class CNoDestination
{
private: private:
CScript m_script; CScript m_script;
public: public:
CNoDestination() = default; CNoDestination() = default;
CNoDestination(const CScript& script) : m_script(script) {} explicit CNoDestination(const CScript& script) : m_script(script) {}
const CScript& GetScript() const { return m_script; } const CScript& GetScript() const LIFETIMEBOUND { return m_script; }
friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); } friend bool operator==(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() == b.GetScript(); }
friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); } friend bool operator<(const CNoDestination& a, const CNoDestination& b) { return a.GetScript() < b.GetScript(); }
@ -34,7 +35,7 @@ private:
CPubKey m_pubkey; CPubKey m_pubkey;
public: public:
PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {} explicit PubKeyDestination(const CPubKey& pubkey) : m_pubkey(pubkey) {}
const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; } const CPubKey& GetPubKey() const LIFETIMEBOUND { return m_pubkey; }

View File

@ -94,13 +94,14 @@ static void WalletCreateTx(benchmark::Bench& bench, const OutputType output_type
} }
// Generate destinations // Generate destinations
CScript dest = GetScriptForDestination(getNewDestination(wallet, output_type)); const auto dest{getNewDestination(wallet, output_type)};
// Generate chain; each coinbase will have two outputs to fill-up the wallet // Generate chain; each coinbase will have two outputs to fill-up the wallet
const auto& params = Params(); const auto& params = Params();
const CScript coinbase_out{GetScriptForDestination(dest)};
unsigned int chain_size = 5000; // 5k blocks means 10k UTXO for the wallet (minus 200 due COINBASE_MATURITY) unsigned int chain_size = 5000; // 5k blocks means 10k UTXO for the wallet (minus 200 due COINBASE_MATURITY)
for (unsigned int i = 0; i < chain_size; ++i) { for (unsigned int i = 0; i < chain_size; ++i) {
generateFakeBlock(params, test_setup->m_node, wallet, dest); generateFakeBlock(params, test_setup->m_node, wallet, coinbase_out);
} }
// Check available balance // Check available balance

View File

@ -187,8 +187,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
setAddress.insert(rcp.address); setAddress.insert(rcp.address);
++nAddresses; ++nAddresses;
CScript scriptPubKey = GetScriptForDestination(DecodeDestination(rcp.address.toStdString())); CRecipient recipient{DecodeDestination(rcp.address.toStdString()), rcp.amount, rcp.fSubtractFeeFromAmount};
CRecipient recipient = {scriptPubKey, rcp.amount, rcp.fSubtractFeeFromAmount};
vecSend.push_back(recipient); vecSend.push_back(recipient);
total += rcp.amount; total += rcp.amount;

View File

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

View File

@ -605,7 +605,7 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
// returns the coin associated with the change address underneath the // returns the coin associated with the change address underneath the
// coinbaseKey pubkey, even though the change address has a different // coinbaseKey pubkey, even though the change address has a different
// pubkey. // pubkey.
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, /*subtract_fee=*/false}); AddTx(CRecipient{PubKeyDestination{{}}, 1 * COIN, /*subtract_fee=*/false});
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
list = ListCoins(*wallet); list = ListCoins(*wallet);