Skip to content

Commit 5321786

Browse files
committed
coincontrol: Replace HasInputWeight with returning optional from Get
1 parent e1abfb5 commit 5321786

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

src/wallet/coincontrol.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,10 @@ void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
7272
m_selected[outpoint].SetInputWeight(weight);
7373
}
7474

75-
bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
75+
std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
7676
{
7777
const auto it = m_selected.find(outpoint);
78-
return it != m_selected.end() && it->second.HasInputWeight();
79-
}
80-
81-
int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
82-
{
83-
return m_selected.at(outpoint).GetInputWeight();
78+
return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
8479
}
8580

8681
void PreselectedInput::SetTxOut(const CTxOut& txout)
@@ -104,14 +99,8 @@ void PreselectedInput::SetInputWeight(int64_t weight)
10499
m_weight = weight;
105100
}
106101

107-
int64_t PreselectedInput::GetInputWeight() const
108-
{
109-
assert(m_weight.has_value());
110-
return m_weight.value();
111-
}
112-
113-
bool PreselectedInput::HasInputWeight() const
102+
std::optional<int64_t> PreselectedInput::GetInputWeight() const
114103
{
115-
return m_weight.has_value();
104+
return m_weight;
116105
}
117106
} // namespace wallet

src/wallet/coincontrol.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class PreselectedInput
4646
/** Set the weight for this input. */
4747
void SetInputWeight(int64_t weight);
4848
/** Retrieve the input weight for this input. */
49-
int64_t GetInputWeight() const;
50-
/** Return whether the input weight is set. */
51-
bool HasInputWeight() const;
49+
std::optional<int64_t> GetInputWeight() const;
5250
};
5351

5452
/** Coin Control Features. */
@@ -131,14 +129,10 @@ class CCoinControl
131129
* Set an input's weight.
132130
*/
133131
void SetInputWeight(const COutPoint& outpoint, int64_t weight);
134-
/**
135-
* Returns true if the input weight is set.
136-
*/
137-
bool HasInputWeight(const COutPoint& outpoint) const;
138132
/**
139133
* Returns the input weight.
140134
*/
141-
int64_t GetInputWeight(const COutPoint& outpoint) const;
135+
std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
142136

143137
private:
144138
//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)

src/wallet/spend.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ static std::optional<int64_t> GetSignedTxinWeight(const CWallet* wallet, const C
117117
const bool can_grind_r)
118118
{
119119
// If weight was provided, use that.
120-
if (coin_control && coin_control->HasInputWeight(txin.prevout)) {
121-
return coin_control->GetInputWeight(txin.prevout);
120+
std::optional<int64_t> weight;
121+
if (coin_control && (weight = coin_control->GetInputWeight(txin.prevout))) {
122+
return weight.value();
122123
}
123124

124125
// Otherwise, use the maximum satisfaction size provided by the descriptor.
@@ -261,15 +262,20 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
261262
const bool can_grind_r = wallet.CanGrindR();
262263
std::map<COutPoint, CAmount> map_of_bump_fees = wallet.chain().calculateIndividualBumpFees(coin_control.ListSelected(), coin_selection_params.m_effective_feerate);
263264
for (const COutPoint& outpoint : coin_control.ListSelected()) {
264-
int input_bytes = -1;
265+
int64_t input_bytes = coin_control.GetInputWeight(outpoint).value_or(-1);
266+
if (input_bytes != -1) {
267+
input_bytes = GetVirtualTransactionSize(input_bytes, 0, 0);
268+
}
265269
CTxOut txout;
266270
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
267271
// Clearly invalid input, fail
268272
if (ptr_wtx->tx->vout.size() <= outpoint.n) {
269273
return util::Error{strprintf(_("Invalid pre-selected input %s"), outpoint.ToString())};
270274
}
271275
txout = ptr_wtx->tx->vout.at(outpoint.n);
272-
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
276+
if (input_bytes == -1) {
277+
input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
278+
}
273279
} else {
274280
// The input is external. We did not find the tx in mapWallet.
275281
const auto out{coin_control.GetExternalOutput(outpoint)};
@@ -284,11 +290,6 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
284290
input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, can_grind_r, &coin_control);
285291
}
286292

287-
// If available, override calculated size with coin control specified size
288-
if (coin_control.HasInputWeight(outpoint)) {
289-
input_bytes = GetVirtualTransactionSize(coin_control.GetInputWeight(outpoint), 0, 0);
290-
}
291-
292293
if (input_bytes == -1) {
293294
return util::Error{strprintf(_("Not solvable pre-selected input %s"), outpoint.ToString())}; // Not solvable, can't estimate size for fee
294295
}

src/wallet/test/fuzz/coincontrol.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
7676
(void)coin_control.SetInputWeight(out_point, weight);
7777
},
7878
[&] {
79-
// Condition to avoid the assertion in GetInputWeight
80-
if (coin_control.HasInputWeight(out_point)) {
81-
(void)coin_control.GetInputWeight(out_point);
82-
}
79+
(void)coin_control.GetInputWeight(out_point);
8380
});
8481
}
8582
}

0 commit comments

Comments
 (0)