Skip to content

Commit 0e70a1b

Browse files
committed
Merge bitcoin/bitcoin#26066: wallet: Refactor and document CoinControl
daba957 refactor: Make ListSelected return vector (Sebastian Falbesoner) 9477662 wallet: Move CoinCointrol definitions to .cpp (Aurèle Oulès) 1db23da wallet: Use std::optional for GetExternalOutput and fixups (Aurèle Oulès) becc45b scripted-diff: Rename setSelected->m_selected_inputs (Aurèle Oulès) Pull request description: - Moves CoinControl function definitions from `coincontrol.h` to `coincontrol.cpp` - Adds more documentation - Renames class member for an improved comprehension - Use `std::optional` for `GetExternalOutput` ACKs for top commit: achow101: ACK daba957 Xekyo: ACK daba957 Tree-SHA512: 3bf2dc834a3246c2f53f8c55154258e605fcb169431d3f7b156931f33c7e3b1ae28e03e16b37f9140a827890eb7798be485b2c36bfc23ff29bb01763f289a07c
2 parents 8f5da89 + daba957 commit 0e70a1b

File tree

4 files changed

+133
-83
lines changed

4 files changed

+133
-83
lines changed

src/qt/coincontroldialog.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ void CoinControlDialog::updateLabels(CCoinControl& m_coin_control, WalletModel *
412412
unsigned int nQuantity = 0;
413413
bool fWitness = false;
414414

415-
std::vector<COutPoint> vCoinControl;
416-
m_coin_control.ListSelected(vCoinControl);
415+
auto vCoinControl{m_coin_control.ListSelected()};
417416

418417
size_t i = 0;
419418
for (const auto& out : model->wallet().getCoins(vCoinControl)) {

src/wallet/coincontrol.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,72 @@ CCoinControl::CCoinControl()
1111
{
1212
m_avoid_partial_spends = gArgs.GetBoolArg("-avoidpartialspends", DEFAULT_AVOIDPARTIALSPENDS);
1313
}
14+
15+
bool CCoinControl::HasSelected() const
16+
{
17+
return !m_selected_inputs.empty();
18+
}
19+
20+
bool CCoinControl::IsSelected(const COutPoint& output) const
21+
{
22+
return m_selected_inputs.count(output) > 0;
23+
}
24+
25+
bool CCoinControl::IsExternalSelected(const COutPoint& output) const
26+
{
27+
return m_external_txouts.count(output) > 0;
28+
}
29+
30+
std::optional<CTxOut> CCoinControl::GetExternalOutput(const COutPoint& outpoint) const
31+
{
32+
const auto ext_it = m_external_txouts.find(outpoint);
33+
if (ext_it == m_external_txouts.end()) {
34+
return std::nullopt;
35+
}
36+
37+
return std::make_optional(ext_it->second);
38+
}
39+
40+
void CCoinControl::Select(const COutPoint& output)
41+
{
42+
m_selected_inputs.insert(output);
43+
}
44+
45+
void CCoinControl::SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
46+
{
47+
m_selected_inputs.insert(outpoint);
48+
m_external_txouts.emplace(outpoint, txout);
49+
}
50+
51+
void CCoinControl::UnSelect(const COutPoint& output)
52+
{
53+
m_selected_inputs.erase(output);
54+
}
55+
56+
void CCoinControl::UnSelectAll()
57+
{
58+
m_selected_inputs.clear();
59+
}
60+
61+
std::vector<COutPoint> CCoinControl::ListSelected() const
62+
{
63+
return {m_selected_inputs.begin(), m_selected_inputs.end()};
64+
}
65+
66+
void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
67+
{
68+
m_input_weights[outpoint] = weight;
69+
}
70+
71+
bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
72+
{
73+
return m_input_weights.count(outpoint) > 0;
74+
}
75+
76+
int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
77+
{
78+
auto it = m_input_weights.find(outpoint);
79+
assert(it != m_input_weights.end());
80+
return it->second;
81+
}
1482
} // namespace wallet

src/wallet/coincontrol.h

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include <script/signingprovider.h>
1414
#include <script/standard.h>
1515

16-
#include <optional>
1716
#include <algorithm>
1817
#include <map>
18+
#include <optional>
1919
#include <set>
2020

2121
namespace wallet {
@@ -63,76 +63,62 @@ class CCoinControl
6363

6464
CCoinControl();
6565

66-
bool HasSelected() const
67-
{
68-
return (setSelected.size() > 0);
69-
}
70-
71-
bool IsSelected(const COutPoint& output) const
72-
{
73-
return (setSelected.count(output) > 0);
74-
}
75-
76-
bool IsExternalSelected(const COutPoint& output) const
77-
{
78-
return (m_external_txouts.count(output) > 0);
79-
}
80-
81-
bool GetExternalOutput(const COutPoint& outpoint, CTxOut& txout) const
82-
{
83-
const auto ext_it = m_external_txouts.find(outpoint);
84-
if (ext_it == m_external_txouts.end()) {
85-
return false;
86-
}
87-
txout = ext_it->second;
88-
return true;
89-
}
90-
91-
void Select(const COutPoint& output)
92-
{
93-
setSelected.insert(output);
94-
}
95-
96-
void SelectExternal(const COutPoint& outpoint, const CTxOut& txout)
97-
{
98-
setSelected.insert(outpoint);
99-
m_external_txouts.emplace(outpoint, txout);
100-
}
101-
102-
void UnSelect(const COutPoint& output)
103-
{
104-
setSelected.erase(output);
105-
}
106-
107-
void UnSelectAll()
108-
{
109-
setSelected.clear();
110-
}
111-
112-
void ListSelected(std::vector<COutPoint>& vOutpoints) const
113-
{
114-
vOutpoints.assign(setSelected.begin(), setSelected.end());
115-
}
116-
117-
void SetInputWeight(const COutPoint& outpoint, int64_t weight)
118-
{
119-
m_input_weights[outpoint] = weight;
120-
}
121-
122-
bool HasInputWeight(const COutPoint& outpoint) const
123-
{
124-
return m_input_weights.count(outpoint) > 0;
125-
}
126-
127-
int64_t GetInputWeight(const COutPoint& outpoint) const
128-
{
129-
auto it = m_input_weights.find(outpoint);
130-
assert(it != m_input_weights.end());
131-
return it->second;
132-
}
66+
/**
67+
* Returns true if there are pre-selected inputs.
68+
*/
69+
bool HasSelected() const;
70+
/**
71+
* Returns true if the given output is pre-selected.
72+
*/
73+
bool IsSelected(const COutPoint& output) const;
74+
/**
75+
* Returns true if the given output is selected as an external input.
76+
*/
77+
bool IsExternalSelected(const COutPoint& output) const;
78+
/**
79+
* Returns the external output for the given outpoint if it exists.
80+
*/
81+
std::optional<CTxOut> GetExternalOutput(const COutPoint& outpoint) const;
82+
/**
83+
* Lock-in the given output for spending.
84+
* The output will be included in the transaction even if it's not the most optimal choice.
85+
*/
86+
void Select(const COutPoint& output);
87+
/**
88+
* Lock-in the given output as an external input for spending because it is not in the wallet.
89+
* The output will be included in the transaction even if it's not the most optimal choice.
90+
*/
91+
void SelectExternal(const COutPoint& outpoint, const CTxOut& txout);
92+
/**
93+
* Unselects the given output.
94+
*/
95+
void UnSelect(const COutPoint& output);
96+
/**
97+
* Unselects all outputs.
98+
*/
99+
void UnSelectAll();
100+
/**
101+
* List the selected inputs.
102+
*/
103+
std::vector<COutPoint> ListSelected() const;
104+
/**
105+
* Set an input's weight.
106+
*/
107+
void SetInputWeight(const COutPoint& outpoint, int64_t weight);
108+
/**
109+
* Returns true if the input weight is set.
110+
*/
111+
bool HasInputWeight(const COutPoint& outpoint) const;
112+
/**
113+
* Returns the input weight.
114+
*/
115+
int64_t GetInputWeight(const COutPoint& outpoint) const;
133116

134117
private:
135-
std::set<COutPoint> setSelected;
118+
//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
119+
std::set<COutPoint> m_selected_inputs;
120+
//! Map of external inputs to include in the transaction
121+
//! These are not in the wallet, so we need to track them separately
136122
std::map<COutPoint, CTxOut> m_external_txouts;
137123
//! Map of COutPoints to the maximum weight for that input
138124
std::map<COutPoint, int64_t> m_input_weights;

src/wallet/spend.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet,
5252
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, const CCoinControl* coin_control)
5353
{
5454
CMutableTransaction txNew(tx);
55-
if (!wallet->DummySignTx(txNew, txouts, coin_control)) {
56-
return TxSize{-1, -1};
57-
}
55+
if (!wallet->DummySignTx(txNew, txouts, coin_control)) return TxSize{-1, -1};
5856
CTransaction ctx(txNew);
5957
int64_t vsize = GetVirtualTransactionSize(ctx);
6058
int64_t weight = GetTransactionWeight(ctx);
@@ -72,11 +70,9 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *walle
7270
assert(input.prevout.n < mi->second.tx->vout.size());
7371
txouts.emplace_back(mi->second.tx->vout.at(input.prevout.n));
7472
} else if (coin_control) {
75-
CTxOut txout;
76-
if (!coin_control->GetExternalOutput(input.prevout, txout)) {
77-
return TxSize{-1, -1};
78-
}
79-
txouts.emplace_back(txout);
73+
const auto& txout{coin_control->GetExternalOutput(input.prevout)};
74+
if (!txout) return TxSize{-1, -1};
75+
txouts.emplace_back(*txout);
8076
} else {
8177
return TxSize{-1, -1};
8278
}
@@ -163,10 +159,8 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
163159
const CoinSelectionParams& coin_selection_params) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
164160
{
165161
PreSelectedInputs result;
166-
std::vector<COutPoint> vPresetInputs;
167-
coin_control.ListSelected(vPresetInputs);
168162
const bool can_grind_r = wallet.CanGrindR();
169-
for (const COutPoint& outpoint : vPresetInputs) {
163+
for (const COutPoint& outpoint : coin_control.ListSelected()) {
170164
int input_bytes = -1;
171165
CTxOut txout;
172166
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
@@ -178,9 +172,12 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
178172
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
179173
} else {
180174
// The input is external. We did not find the tx in mapWallet.
181-
if (!coin_control.GetExternalOutput(outpoint, txout)) {
175+
const auto out{coin_control.GetExternalOutput(outpoint)};
176+
if (!out) {
182177
return util::Error{strprintf(_("Not found pre-selected input %s"), outpoint.ToString())};
183178
}
179+
180+
txout = *out;
184181
}
185182

186183
if (input_bytes == -1) {

0 commit comments

Comments
 (0)