aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-01-23 16:59:25 -0500
committerAndrew Chow <github@achow101.com>2023-12-08 14:55:14 -0500
commit5321786b9d90eaf35059bb07e6beaaa2cbb257ac (patch)
treef7e73d72fb1df3c59ae2383c5bbdddc2ed1d4eca
parente1abfb5b2037ca4fe5a05aa578030c8016491c8b (diff)
downloadbitcoin-5321786b9d90eaf35059bb07e6beaaa2cbb257ac.tar.xz
coincontrol: Replace HasInputWeight with returning optional from Get
-rw-r--r--src/wallet/coincontrol.cpp19
-rw-r--r--src/wallet/coincontrol.h10
-rw-r--r--src/wallet/spend.cpp19
-rw-r--r--src/wallet/test/fuzz/coincontrol.cpp5
4 files changed, 17 insertions, 36 deletions
diff --git a/src/wallet/coincontrol.cpp b/src/wallet/coincontrol.cpp
index a4f3bdac23..94218b45c1 100644
--- a/src/wallet/coincontrol.cpp
+++ b/src/wallet/coincontrol.cpp
@@ -72,15 +72,10 @@ void CCoinControl::SetInputWeight(const COutPoint& outpoint, int64_t weight)
m_selected[outpoint].SetInputWeight(weight);
}
-bool CCoinControl::HasInputWeight(const COutPoint& outpoint) const
+std::optional<int64_t> CCoinControl::GetInputWeight(const COutPoint& outpoint) const
{
const auto it = m_selected.find(outpoint);
- return it != m_selected.end() && it->second.HasInputWeight();
-}
-
-int64_t CCoinControl::GetInputWeight(const COutPoint& outpoint) const
-{
- return m_selected.at(outpoint).GetInputWeight();
+ return it != m_selected.end() ? it->second.GetInputWeight() : std::nullopt;
}
void PreselectedInput::SetTxOut(const CTxOut& txout)
@@ -104,14 +99,8 @@ void PreselectedInput::SetInputWeight(int64_t weight)
m_weight = weight;
}
-int64_t PreselectedInput::GetInputWeight() const
-{
- assert(m_weight.has_value());
- return m_weight.value();
-}
-
-bool PreselectedInput::HasInputWeight() const
+std::optional<int64_t> PreselectedInput::GetInputWeight() const
{
- return m_weight.has_value();
+ return m_weight;
}
} // namespace wallet
diff --git a/src/wallet/coincontrol.h b/src/wallet/coincontrol.h
index 4f87d13d99..1d1f5c4da2 100644
--- a/src/wallet/coincontrol.h
+++ b/src/wallet/coincontrol.h
@@ -46,9 +46,7 @@ public:
/** Set the weight for this input. */
void SetInputWeight(int64_t weight);
/** Retrieve the input weight for this input. */
- int64_t GetInputWeight() const;
- /** Return whether the input weight is set. */
- bool HasInputWeight() const;
+ std::optional<int64_t> GetInputWeight() const;
};
/** Coin Control Features. */
@@ -132,13 +130,9 @@ public:
*/
void SetInputWeight(const COutPoint& outpoint, int64_t weight);
/**
- * Returns true if the input weight is set.
- */
- bool HasInputWeight(const COutPoint& outpoint) const;
- /**
* Returns the input weight.
*/
- int64_t GetInputWeight(const COutPoint& outpoint) const;
+ std::optional<int64_t> GetInputWeight(const COutPoint& outpoint) const;
private:
//! Selected inputs (inputs that will be used, regardless of whether they're optimal or not)
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index 35583642a5..2fb238fccd 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -117,8 +117,9 @@ static std::optional<int64_t> GetSignedTxinWeight(const CWallet* wallet, const C
const bool can_grind_r)
{
// If weight was provided, use that.
- if (coin_control && coin_control->HasInputWeight(txin.prevout)) {
- return coin_control->GetInputWeight(txin.prevout);
+ std::optional<int64_t> weight;
+ if (coin_control && (weight = coin_control->GetInputWeight(txin.prevout))) {
+ return weight.value();
}
// Otherwise, use the maximum satisfaction size provided by the descriptor.
@@ -261,7 +262,10 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
const bool can_grind_r = wallet.CanGrindR();
std::map<COutPoint, CAmount> map_of_bump_fees = wallet.chain().calculateIndividualBumpFees(coin_control.ListSelected(), coin_selection_params.m_effective_feerate);
for (const COutPoint& outpoint : coin_control.ListSelected()) {
- int input_bytes = -1;
+ int64_t input_bytes = coin_control.GetInputWeight(outpoint).value_or(-1);
+ if (input_bytes != -1) {
+ input_bytes = GetVirtualTransactionSize(input_bytes, 0, 0);
+ }
CTxOut txout;
if (auto ptr_wtx = wallet.GetWalletTx(outpoint.hash)) {
// Clearly invalid input, fail
@@ -269,7 +273,9 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
return util::Error{strprintf(_("Invalid pre-selected input %s"), outpoint.ToString())};
}
txout = ptr_wtx->tx->vout.at(outpoint.n);
- input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
+ if (input_bytes == -1) {
+ input_bytes = CalculateMaximumSignedInputSize(txout, &wallet, &coin_control);
+ }
} else {
// The input is external. We did not find the tx in mapWallet.
const auto out{coin_control.GetExternalOutput(outpoint)};
@@ -284,11 +290,6 @@ util::Result<PreSelectedInputs> FetchSelectedInputs(const CWallet& wallet, const
input_bytes = CalculateMaximumSignedInputSize(txout, outpoint, &coin_control.m_external_provider, can_grind_r, &coin_control);
}
- // If available, override calculated size with coin control specified size
- if (coin_control.HasInputWeight(outpoint)) {
- input_bytes = GetVirtualTransactionSize(coin_control.GetInputWeight(outpoint), 0, 0);
- }
-
if (input_bytes == -1) {
return util::Error{strprintf(_("Not solvable pre-selected input %s"), outpoint.ToString())}; // Not solvable, can't estimate size for fee
}
diff --git a/src/wallet/test/fuzz/coincontrol.cpp b/src/wallet/test/fuzz/coincontrol.cpp
index 0f71f28df2..64ed4f245d 100644
--- a/src/wallet/test/fuzz/coincontrol.cpp
+++ b/src/wallet/test/fuzz/coincontrol.cpp
@@ -76,10 +76,7 @@ FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
(void)coin_control.SetInputWeight(out_point, weight);
},
[&] {
- // Condition to avoid the assertion in GetInputWeight
- if (coin_control.HasInputWeight(out_point)) {
- (void)coin_control.GetInputWeight(out_point);
- }
+ (void)coin_control.GetInputWeight(out_point);
});
}
}